From 337d8d51138092c89246c69807f993ac60b02637 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 8 Jul 2021 02:09:04 +0900 Subject: [PATCH] internal/graphicsdriver/metal: Separate GCing the buffers Updates #1196 --- internal/graphicsdriver/metal/graphics.go | 38 +++++++++++------------ 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/internal/graphicsdriver/metal/graphics.go b/internal/graphicsdriver/metal/graphics.go index 6d86a51d4..0643b4e6d 100644 --- a/internal/graphicsdriver/metal/graphics.go +++ b/internal/graphicsdriver/metal/graphics.go @@ -388,13 +388,7 @@ func pow2(x uintptr) uintptr { return p2 } -func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer { - if g.cb == (mtl.CommandBuffer{}) { - g.cb = g.cq.MakeCommandBuffer() - } - - var newBuf mtl.Buffer - +func (g *Graphics) gcBuffers() { for cb, bs := range g.buffers { // If the command buffer still lives, the buffer must not be updated. // TODO: Handle an error? @@ -403,10 +397,6 @@ func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer { } for _, b := range bs { - if newBuf == (mtl.Buffer{}) && b.Length() >= length { - newBuf = b - continue - } if g.unusedBuffers == nil { g.unusedBuffers = map[mtl.Buffer]struct{}{} } @@ -416,15 +406,6 @@ func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer { cb.Release() } - for b := range g.unusedBuffers { - if b.Length() >= length { - newBuf = b - delete(g.unusedBuffers, b) - break - } - } - - // GC unused buffers. const maxUnusedBuffers = 10 if len(g.unusedBuffers) > maxUnusedBuffers { bufs := make([]mtl.Buffer, 0, len(g.unusedBuffers)) @@ -439,6 +420,21 @@ func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer { b.Release() } } +} + +func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer { + if g.cb == (mtl.CommandBuffer{}) { + g.cb = g.cq.MakeCommandBuffer() + } + + var newBuf mtl.Buffer + for b := range g.unusedBuffers { + if b.Length() >= length { + newBuf = b + delete(g.unusedBuffers, b) + break + } + } if newBuf == (mtl.Buffer{}) { newBuf = g.view.getMTLDevice().MakeBufferWithLength(pow2(length), resourceStorageMode) @@ -1228,6 +1224,8 @@ func (i *Image) mtlTexture() mtl.Texture { return mtl.Texture{} } g.screenDrawable = drawable + // After nextDrawable, it is expected some command buffers are completed. + g.gcBuffers() } return g.screenDrawable.Texture() }