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
)
var creatingSystemDefaultDeviceSucceeded bool
var systemDefaultDevice mtl.Device
func init() {
// mtl.CreateSystemDefaultDevice must be called on the main thread (#2147).
_, err := mtl.CreateSystemDefaultDevice()
creatingSystemDefaultDeviceSucceeded = err == nil
d, err := mtl.CreateSystemDefaultDevice()
if err == nil {
systemDefaultDevice = d
}
}
// 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).
// 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.
if !creatingSystemDefaultDeviceSucceeded {
if systemDefaultDevice == (mtl.Device{}) {
return nil, fmt.Errorf("metal: mtl.CreateSystemDefaultDevice failed")
}
g := &Graphics{}
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(); err != nil {
if err := g.view.initialize(systemDefaultDevice); err != nil {
return nil, err
}
}
@ -386,7 +387,7 @@ func (g *Graphics) Initialize() error {
if runtime.GOOS == "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
}
}

View File

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