mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
ebiten: update image.DrawTriangles* to re-use indices (#3026)
Reduces allocations and GC overhead for programs that call this method hundreds of times per rendered frame.
This commit is contained in:
parent
e3af35e6ff
commit
9d4f88c992
14
image.go
14
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() {
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user