internal/graphicscommand: Optimization: Pool drawTrianglesCommand objects

This commit is contained in:
Hajime Hoshi 2021-10-29 23:41:47 +09:00
parent d3ac199aa9
commit b1a442f86a

View File

@ -53,6 +53,26 @@ type size struct {
height float32 height float32
} }
type drawTrianglesCommandPool struct {
pool []*drawTrianglesCommand
}
func (p *drawTrianglesCommandPool) get() *drawTrianglesCommand {
if len(p.pool) == 0 {
return &drawTrianglesCommand{}
}
v := p.pool[len(p.pool)-1]
p.pool = p.pool[:len(p.pool)-1]
return v
}
func (p *drawTrianglesCommandPool) put(v *drawTrianglesCommand) {
if len(p.pool) >= 1024 {
return
}
p.pool = append(p.pool, v)
}
// commandQueue is a command queue for drawing commands. // commandQueue is a command queue for drawing commands.
type commandQueue struct { type commandQueue struct {
// commands is a queue of drawing commands. // commands is a queue of drawing commands.
@ -77,6 +97,8 @@ type commandQueue struct {
tmpNumIndices int tmpNumIndices int
nextIndex int nextIndex int
drawTrianglesCommandPool drawTrianglesCommandPool
err error err error
} }
@ -166,22 +188,21 @@ func (q *commandQueue) EnqueueDrawTrianglesCommand(dst *Image, srcs [graphics.Sh
} }
} }
c := &drawTrianglesCommand{ c := q.drawTrianglesCommandPool.get()
dst: dst, c.dst = dst
srcs: srcs, c.srcs = srcs
offsets: offsets, c.offsets = offsets
vertices: q.lastVertices(len(vertices)), c.vertices = q.lastVertices(len(vertices))
nindices: len(indices), c.nindices = len(indices)
color: color, c.color = color
mode: mode, c.mode = mode
filter: filter, c.filter = filter
address: address, c.address = address
dstRegion: dstRegion, c.dstRegion = dstRegion
srcRegion: srcRegion, c.srcRegion = srcRegion
shader: shader, c.shader = shader
uniforms: uniforms, c.uniforms = uniforms
evenOdd: evenOdd, c.evenOdd = evenOdd
}
q.commands = append(q.commands, c) q.commands = append(q.commands, c)
} }
@ -309,7 +330,10 @@ func (q *commandQueue) flush() error {
// Release the commands explicitly (#1803). // Release the commands explicitly (#1803).
// Apparently, the part of a slice between len and cap-1 still holds references. // Apparently, the part of a slice between len and cap-1 still holds references.
// Then, resetting the length by [:0] doesn't release the references. // Then, resetting the length by [:0] doesn't release the references.
for i := range q.commands { for i, c := range q.commands {
if c, ok := c.(*drawTrianglesCommand); ok {
q.drawTrianglesCommandPool.put(c)
}
q.commands[i] = nil q.commands[i] = nil
} }
q.commands = q.commands[:0] q.commands = q.commands[:0]