From 974a7ab596ba0ef6776c4c34a04ad66a8c5e70d9 Mon Sep 17 00:00:00 2001 From: ernestrc Date: Sun, 23 Jun 2024 23:52:08 -0700 Subject: [PATCH] Update image.DrawTriangles to re-use indices Reduces allocations and GC overhead for programs that call this method hundreds of times per rendered frame. --- image.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/image.go b/image.go index 58ee22640..7f51a3e34 100644 --- a/image.go +++ b/image.go @@ -43,6 +43,9 @@ type Image struct { // tmpVertices must not be reused until ui.Image.Draw* is called. tmpVertices []float32 + // tmpIndices must not be reused until ui.Image.Draw* is called. + tmpIndices []uint32 + // tmpUniforms must not be reused until ui.Image.Draw* is called. tmpUniforms []uint32 @@ -514,7 +517,7 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o vs[i*graphics.VertexFloatCount+7] = v.ColorA * ca } } - is := make([]uint32, len(indices)) + is := i.ensureTmpIndices(len(indices)) for i := range is { is[i] = uint32(indices[i]) } @@ -664,7 +667,7 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader vs[i*graphics.VertexFloatCount+7] = v.ColorA } - is := make([]uint32, len(indices)) + is := i.ensureTmpIndices(len(indices)) for i := range is { is[i] = uint32(indices[i]) } @@ -1265,6 +1268,13 @@ func (i *Image) ensureTmpVertices(n int) []float32 { return i.tmpVertices[:n] } +func (i *Image) ensureTmpIndices(n int) []uint32 { + if cap(i.tmpIndices) < n { + i.tmpIndices = make([]uint32, n) + } + return i.tmpIndices[:n] +} + // private implements FinalScreen. func (*Image) private() { }