Revert "internal/graphicsdriver/metal: Do not retain MTLCommandBuffer for MTLBuffer"

This reverts commit e0fbfc2bb0.

Reason: Rendering issue

Closes #1699
This commit is contained in:
Hajime Hoshi 2021-07-08 00:27:33 +09:00
parent 6213c17abc
commit b314b6b9b6
2 changed files with 45 additions and 32 deletions

View File

@ -317,7 +317,8 @@ type Graphics struct {
screenDrawable ca.MetalDrawable screenDrawable ca.MetalDrawable
buffers map[mtl.Buffer]int buffers map[mtl.CommandBuffer][]mtl.Buffer
unusedBuffers map[mtl.Buffer]struct{}
lastDstTexture mtl.Texture lastDstTexture mtl.Texture
lastStencilMode stencilMode lastStencilMode stencilMode
@ -387,40 +388,54 @@ func pow2(x uintptr) uintptr {
return p2 return p2
} }
func (g *Graphics) ageBuffers() {
for b, age := range g.buffers {
if age <= maximumDrawableCount {
g.buffers[b]++
}
}
}
func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer { func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer {
if g.cb == (mtl.CommandBuffer{}) {
g.cb = g.cq.MakeCommandBuffer()
}
var newBuf mtl.Buffer var newBuf mtl.Buffer
var availBufs []mtl.Buffer for cb, bs := range g.buffers {
for b, age := range g.buffers { // If the command buffer still lives, the buffer must not be updated.
// If the buffer is too young, its command buffer might still live. // TODO: Handle an error?
// The buffer must not be updated in this case. if cb.Status() != mtl.CommandBufferStatusCompleted {
if age <= maximumDrawableCount {
continue continue
} }
if newBuf == (mtl.Buffer{}) && b.Length() >= length { for _, b := range bs {
newBuf = b if newBuf == (mtl.Buffer{}) && b.Length() >= length {
continue newBuf = b
continue
}
if g.unusedBuffers == nil {
g.unusedBuffers = map[mtl.Buffer]struct{}{}
}
g.unusedBuffers[b] = struct{}{}
}
delete(g.buffers, cb)
cb.Release()
}
for b := range g.unusedBuffers {
if b.Length() >= length {
newBuf = b
delete(g.unusedBuffers, b)
break
} }
availBufs = append(availBufs, b)
} }
// GC unused buffers. // GC unused buffers.
const maxUnusedBuffers = 10 const maxUnusedBuffers = 10
if len(availBufs) > maxUnusedBuffers { if len(g.unusedBuffers) > maxUnusedBuffers {
sort.Slice(availBufs, func(a, b int) bool { bufs := make([]mtl.Buffer, 0, len(g.unusedBuffers))
return availBufs[a].Length() > availBufs[b].Length() for b := range g.unusedBuffers {
bufs = append(bufs, b)
}
sort.Slice(bufs, func(a, b int) bool {
return bufs[a].Length() > bufs[b].Length()
}) })
for _, b := range availBufs[maxUnusedBuffers:] { for _, b := range bufs[maxUnusedBuffers:] {
delete(g.buffers, b) delete(g.unusedBuffers, b)
b.Release() b.Release()
} }
} }
@ -428,10 +443,14 @@ func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer {
if newBuf == (mtl.Buffer{}) { if newBuf == (mtl.Buffer{}) {
newBuf = g.view.getMTLDevice().MakeBufferWithLength(pow2(length), resourceStorageMode) newBuf = g.view.getMTLDevice().MakeBufferWithLength(pow2(length), resourceStorageMode)
} }
if g.buffers == nil { if g.buffers == nil {
g.buffers = map[mtl.Buffer]int{} g.buffers = map[mtl.CommandBuffer][]mtl.Buffer{}
} }
g.buffers[newBuf] = 0 if _, ok := g.buffers[g.cb]; !ok {
g.cb.Retain()
}
g.buffers[g.cb] = append(g.buffers[g.cb], newBuf)
return newBuf return newBuf
} }
@ -1209,10 +1228,6 @@ func (i *Image) mtlTexture() mtl.Texture {
return mtl.Texture{} return mtl.Texture{}
} }
g.screenDrawable = drawable g.screenDrawable = drawable
// nextDrawable blocks until the new drawable is available.
// If nextDrawable returns a valid drawable, this means that at least one drawable is already processed.
g.ageBuffers()
} }
return g.screenDrawable.Texture() return g.screenDrawable.Texture()
} }

View File

@ -24,8 +24,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
) )
const maximumDrawableCount = 3
type view struct { type view struct {
window uintptr window uintptr
uiview uintptr uiview uintptr
@ -71,7 +69,7 @@ func (v *view) reset() error {
// MTLPixelFormatBGRA8Unorm_sRGB, MTLPixelFormatRGBA16Float, MTLPixelFormatBGRA10_XR, or // MTLPixelFormatBGRA8Unorm_sRGB, MTLPixelFormatRGBA16Float, MTLPixelFormatBGRA10_XR, or
// MTLPixelFormatBGRA10_XR_sRGB. // MTLPixelFormatBGRA10_XR_sRGB.
v.ml.SetPixelFormat(mtl.PixelFormatBGRA8UNorm) v.ml.SetPixelFormat(mtl.PixelFormatBGRA8UNorm)
v.ml.SetMaximumDrawableCount(maximumDrawableCount) v.ml.SetMaximumDrawableCount(3)
// The vsync state might be reset. Set the state again (#1364). // The vsync state might be reset. Set the state again (#1364).
v.ml.SetDisplaySyncEnabled(v.vsync) v.ml.SetDisplaySyncEnabled(v.vsync)