internal/graphicsdriver/metal: refactoring

Updates #2599
This commit is contained in:
Hajime Hoshi 2023-03-13 21:22:35 +09:00
parent 903b5ab046
commit 65fb9d56ba
2 changed files with 10 additions and 13 deletions

View File

@ -74,12 +74,14 @@ const (
noStencil noStencil
) )
var creatingSystemDefaultDeviceSucceeded bool var systemDefaultDevice mtl.Device
func init() { func init() {
// mtl.CreateSystemDefaultDevice must be called on the main thread (#2147). // mtl.CreateSystemDefaultDevice must be called on the main thread (#2147).
_, err := mtl.CreateSystemDefaultDevice() d, err := mtl.CreateSystemDefaultDevice()
creatingSystemDefaultDeviceSucceeded = err == nil if err == nil {
systemDefaultDevice = d
}
} }
// NewGraphics creates an implementation of graphicsdriver.Graphics for Metal. // NewGraphics creates an implementation of graphicsdriver.Graphics for Metal.
@ -88,15 +90,14 @@ func NewGraphics() (graphicsdriver.Graphics, error) {
// On old mac devices like iMac 2011, Metal is not supported (#779). // On old mac devices like iMac 2011, Metal is not supported (#779).
// TODO: Is there a better way to check whether Metal is available or not? // TODO: Is there a better way to check whether Metal is available or not?
// It seems OK to call MTLCreateSystemDefaultDevice multiple times, so this should be fine. // It seems OK to call MTLCreateSystemDefaultDevice multiple times, so this should be fine.
if !creatingSystemDefaultDeviceSucceeded { if systemDefaultDevice == (mtl.Device{}) {
return nil, fmt.Errorf("metal: mtl.CreateSystemDefaultDevice failed") return nil, fmt.Errorf("metal: mtl.CreateSystemDefaultDevice failed")
} }
g := &Graphics{} g := &Graphics{}
if runtime.GOOS != "ios" { if runtime.GOOS != "ios" {
// Initializing a Metal device and a layer must be done in the main thread on macOS. if err := g.view.initialize(systemDefaultDevice); err != nil {
if err := g.view.initialize(); err != nil {
return nil, err return nil, err
} }
} }
@ -386,7 +387,7 @@ func (g *Graphics) Initialize() error {
if runtime.GOOS == "ios" { if runtime.GOOS == "ios" {
// Initializing a Metal device and a layer must be done in the render thread on iOS. // Initializing a Metal device and a layer must be done in the render thread on iOS.
if err := g.view.initialize(); err != nil { if err := g.view.initialize(systemDefaultDevice); err != nil {
return err return err
} }
} }

View File

@ -58,12 +58,8 @@ func (v *view) colorPixelFormat() mtl.PixelFormat {
return v.ml.PixelFormat() return v.ml.PixelFormat()
} }
func (v *view) initialize() error { func (v *view) initialize(device mtl.Device) error {
d, err := mtl.CreateSystemDefaultDevice() v.device = device
if err != nil {
return err
}
v.device = d
ml, err := ca.MakeMetalLayer() ml, err := ca.MakeMetalLayer()
if err != nil { if err != nil {