internal/graphicsdriver/metal: bug fix: the default driver must be initialized on the main thread

Closes #2147
This commit is contained in:
Hajime Hoshi 2022-06-17 14:56:31 +09:00
parent b43312fe80
commit 07a558b38d

View File

@ -346,6 +346,14 @@ const (
noStencil
)
var creatingSystemDefaultDeviceSucceeded bool
func init() {
// mtl.CreateSystemDefaultDevice must be called on the main thread (#2147).
_, ok := mtl.CreateSystemDefaultDevice()
creatingSystemDefaultDeviceSucceeded = ok
}
// NewGraphics creates an implementation of graphicsdriver.Graphcis for Metal.
// The returned graphics value is nil iff the error is not nil.
func NewGraphics() (graphicsdriver.Graphics, error) {
@ -353,13 +361,10 @@ func NewGraphics() (graphicsdriver.Graphics, error) {
return nil, fmt.Errorf("metal: Metal is not supported in this environment")
}
// Initialize isMetalAvailable on the main thread.
// TODO: Now ui.newGraphicsDriver is called on the main thread. Add an assertion.
// 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 _, ok := mtl.CreateSystemDefaultDevice(); !ok {
if !creatingSystemDefaultDeviceSucceeded {
return nil, fmt.Errorf("metal: mtl.CreateSystemDefaultDevice failed")
}