Update image.DrawTriangles to re-use indices

Reduces allocations and GC overhead for programs that
call this method hundreds of times per rendered frame.
This commit is contained in:
ernestrc 2024-06-23 23:52:08 -07:00
parent e3af35e6ff
commit 974a7ab596
No known key found for this signature in database
GPG Key ID: 064D4ABCFA6D9338

View File

@ -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() {
}