internal/graphicsdriver/metal: refactoring

This commit is contained in:
Hajime Hoshi
2025-08-27 23:19:22 +09:00
parent 46fd8a1a52
commit 4a0468f29a
4 changed files with 25 additions and 15 deletions

View File

@@ -860,7 +860,6 @@ func (i *Image) mtlTexture() mtl.Texture {
if i.screen { if i.screen {
g := i.graphics g := i.graphics
if g.screenDrawable == (ca.MetalDrawable{}) { if g.screenDrawable == (ca.MetalDrawable{}) {
i.graphics.view.waitForDisplayLinkOutputCallback()
drawable := g.view.nextDrawable() drawable := g.view.nextDrawable()
if drawable == (ca.MetalDrawable{}) { if drawable == (ca.MetalDrawable{}) {
return mtl.Texture{} return mtl.Texture{}

View File

@@ -97,20 +97,11 @@ func (v *view) initialize(device mtl.Device, colorSpace graphicsdriver.ColorSpac
v.ml.SetMaximumDrawableCount(maximumDrawableCount) v.ml.SetMaximumDrawableCount(maximumDrawableCount)
v.initializeDisplayLink() v.initializeOS()
return nil return nil
} }
func (v *view) nextDrawable() ca.MetalDrawable {
d, err := v.ml.NextDrawable()
if err != nil {
// Drawable is nil. This can happen at the initial state. Let's wait and see.
return ca.MetalDrawable{}
}
return d
}
type fence struct { type fence struct {
value uint64 value uint64
lastValue uint64 lastValue uint64

View File

@@ -39,6 +39,7 @@ import "C"
import ( import (
"unsafe" "unsafe"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/ca"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
) )
@@ -65,10 +66,15 @@ const (
resourceStorageMode = mtl.ResourceStorageModeShared resourceStorageMode = mtl.ResourceStorageModeShared
) )
func (v *view) initializeDisplayLink() { func (v *view) nextDrawable() ca.MetalDrawable {
// Do nothing. d, err := v.ml.NextDrawable()
if err != nil {
// Drawable is nil. This can happen at the initial state. Let's wait and see.
return ca.MetalDrawable{}
}
return d
} }
func (v *view) waitForDisplayLinkOutputCallback() { func (v *view) initializeOS() {
// Do nothing. // Do nothing.
} }

View File

@@ -28,6 +28,7 @@ import (
"github.com/ebitengine/purego/objc" "github.com/ebitengine/purego/objc"
"github.com/hajimehoshi/ebiten/v2/internal/cocoa" "github.com/hajimehoshi/ebiten/v2/internal/cocoa"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/ca"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
) )
@@ -61,7 +62,7 @@ const (
resourceStorageMode = mtl.ResourceStorageModeManaged resourceStorageMode = mtl.ResourceStorageModeManaged
) )
func (v *view) initializeDisplayLink() { func (v *view) initializeOS() {
v.fence = newFence() v.fence = newFence()
// TODO: CVDisplayLink APIs are deprecated in macOS 10.15 and later. // TODO: CVDisplayLink APIs are deprecated in macOS 10.15 and later.
@@ -97,3 +98,16 @@ func ebitengine_DisplayLinkOutputCallback(displayLinkRef C.CVDisplayLinkRef, inN
view.fence.advance() view.fence.advance()
return 0 return 0
} }
func (v *view) nextDrawable() ca.MetalDrawable {
// TODO: Use CAMetalDisplayLink if available.
v.waitForDisplayLinkOutputCallback()
d, err := v.ml.NextDrawable()
if err != nil {
// Drawable is nil. This can happen at the initial state. Let's wait and see.
return ca.MetalDrawable{}
}
return d
}