mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
internal/ui: refactoring: remove dependency on the package mtl
This commit is contained in:
parent
6f3c6d6c1b
commit
176e984a58
@ -346,9 +346,24 @@ const (
|
|||||||
noStencil
|
noStencil
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// isMetalAvailable reports whether Metal is available or not.
|
||||||
|
//
|
||||||
|
// On old mac devices like iMac 2011, Metal is not supported (#779).
|
||||||
|
var isMetalAvailable bool
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Initialize isMetalAvailable on the main thread.
|
||||||
|
// 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.
|
||||||
|
_, isMetalAvailable = mtl.CreateSystemDefaultDevice()
|
||||||
|
}
|
||||||
|
|
||||||
var theGraphics Graphics
|
var theGraphics Graphics
|
||||||
|
|
||||||
func Get() *Graphics {
|
func Get() *Graphics {
|
||||||
|
if !isMetalAvailable {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return &theGraphics
|
return &theGraphics
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -460,10 +460,10 @@ type Device struct {
|
|||||||
// CreateSystemDefaultDevice returns the preferred system default Metal device.
|
// CreateSystemDefaultDevice returns the preferred system default Metal device.
|
||||||
//
|
//
|
||||||
// Reference: https://developer.apple.com/documentation/metal/1433401-mtlcreatesystemdefaultdevice.
|
// Reference: https://developer.apple.com/documentation/metal/1433401-mtlcreatesystemdefaultdevice.
|
||||||
func CreateSystemDefaultDevice() (Device, error) {
|
func CreateSystemDefaultDevice() (Device, bool) {
|
||||||
d := C.CreateSystemDefaultDevice()
|
d := C.CreateSystemDefaultDevice()
|
||||||
if d.Device == nil {
|
if d.Device == nil {
|
||||||
return Device{}, errors.New("Metal is not supported on this system")
|
return Device{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return Device{
|
return Device{
|
||||||
@ -471,7 +471,7 @@ func CreateSystemDefaultDevice() (Device, error) {
|
|||||||
Headless: d.Headless != 0,
|
Headless: d.Headless != 0,
|
||||||
LowPower: d.LowPower != 0,
|
LowPower: d.LowPower != 0,
|
||||||
Name: C.GoString(d.Name),
|
Name: C.GoString(d.Name),
|
||||||
}, nil
|
}, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Device returns the underlying id<MTLDevice> pointer.
|
// Device returns the underlying id<MTLDevice> pointer.
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package metal
|
package metal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/ca"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/ca"
|
||||||
@ -76,10 +77,10 @@ func (v *view) colorPixelFormat() mtl.PixelFormat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *view) initialize() error {
|
func (v *view) initialize() error {
|
||||||
var err error
|
var ok bool
|
||||||
v.device, err = mtl.CreateSystemDefaultDevice()
|
v.device, ok = mtl.CreateSystemDefaultDevice()
|
||||||
if err != nil {
|
if !ok {
|
||||||
return err
|
return errors.New("metal: Metal is not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
v.ml = ca.MakeMetalLayer()
|
v.ml = ca.MakeMetalLayer()
|
||||||
|
@ -38,24 +38,19 @@ import (
|
|||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
|
||||||
)
|
)
|
||||||
|
|
||||||
var graphics graphicsdriver.Graphics
|
var graphics graphicsdriver.Graphics
|
||||||
|
|
||||||
func supportsMetal() bool {
|
func supportsMetal() bool {
|
||||||
// On old mac devices like iMac 2011, Metal is not supported (#779).
|
|
||||||
if _, err := mtl.CreateSystemDefaultDevice(); err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// On macOS 10.11 El Capitan, there is a rendering issue on Metal (#781).
|
// On macOS 10.11 El Capitan, there is a rendering issue on Metal (#781).
|
||||||
// Use the OpenGL in macOS 10.11 or older.
|
// Use the OpenGL in macOS 10.11 or older.
|
||||||
if C.getMacOSMajorVersion() <= 10 && C.getMacOSMinorVersion() <= 11 {
|
if C.getMacOSMajorVersion() <= 10 && C.getMacOSMinorVersion() <= 11 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
|
return metal.Get() != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var graphicsOnce sync.Once
|
var graphicsOnce sync.Once
|
||||||
|
@ -23,7 +23,6 @@ import (
|
|||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -32,8 +31,8 @@ func Graphics() graphicsdriver.Graphics {
|
|||||||
return opengl.Get()
|
return opengl.Get()
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := mtl.CreateSystemDefaultDevice(); err != nil {
|
if metal.Get() == nil {
|
||||||
panic(fmt.Sprintf("mobile: mtl.CreateSystemDefaultDevice failed on iOS: %v", err))
|
panic(fmt.Sprintf("ui: Metal is not available on this iOS device"))
|
||||||
}
|
}
|
||||||
return metal.Get()
|
return metal.Get()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user