From 07a558b38d699db264ff5bbc01095fa00e48ca50 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 17 Jun 2022 14:56:31 +0900 Subject: [PATCH] internal/graphicsdriver/metal: bug fix: the default driver must be initialized on the main thread Closes #2147 --- internal/graphicsdriver/metal/graphics_darwin.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/graphicsdriver/metal/graphics_darwin.go b/internal/graphicsdriver/metal/graphics_darwin.go index 6a0067755..f141ae15b 100644 --- a/internal/graphicsdriver/metal/graphics_darwin.go +++ b/internal/graphicsdriver/metal/graphics_darwin.go @@ -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") }