From 7998e4d31de68a6e4d02d2105570d6786f2c37ff Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 4 Mar 2023 01:08:08 +0900 Subject: [PATCH] update purego to v0.3.0-alpha There are breaking changes between purego v0.2.0 and v0.3.0-alpha. --- go.mod | 4 +-- go.sum | 8 ++--- internal/gamepad/api_cf_darwin.go | 36 +++++++++++++++---- internal/gamepad/api_hid_darwin.go | 9 +++-- internal/gamepad/gamepad_darwin.go | 7 ++++ internal/graphicsdriver/metal/ca/ca_darwin.go | 35 ++++++++++++------ .../graphicsdriver/metal/graphics_darwin.go | 4 +-- .../graphicsdriver/metal/mtl/mtl_darwin.go | 23 +++++++----- internal/graphicsdriver/metal/view_darwin.go | 15 ++++---- .../opengl/gl/procaddr_darwin.go | 22 ++++++++---- 10 files changed, 114 insertions(+), 49 deletions(-) diff --git a/go.mod b/go.mod index 4721259f6..a996c27e9 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,11 @@ module github.com/hajimehoshi/ebiten/v2 go 1.18 require ( - github.com/ebitengine/purego v0.2.0 + github.com/ebitengine/purego v0.3.0-alpha github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b github.com/hajimehoshi/bitmapfont/v2 v2.2.3 github.com/hajimehoshi/go-mp3 v0.3.4 - github.com/hajimehoshi/oto/v2 v2.4.0-alpha.12.0.20230303060416-96fac77ab05c + github.com/hajimehoshi/oto/v2 v2.4.0-alpha.12.0.20230303144707-bd76255e9f1e github.com/jakecoffman/cp v1.2.1 github.com/jezek/xgb v1.1.0 github.com/jfreymuth/oggvorbis v1.0.5 diff --git a/go.sum b/go.sum index 2d49aa6a1..979dc1490 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/ebitengine/purego v0.2.0 h1:Zf5ZJkd16tTTZn786BBe2R4NdPRs5imPWYUW4bG6tz0= -github.com/ebitengine/purego v0.2.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= +github.com/ebitengine/purego v0.3.0-alpha h1:7eoxaayPodJU34x1xmFE9INXm9h9quDoREmCum07z8E= +github.com/ebitengine/purego v0.3.0-alpha/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b h1:GgabKamyOYguHqHjSkDACcgoPIz3w0Dis/zJ1wyHHHU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/hajimehoshi/bitmapfont/v2 v2.2.3 h1:jmq/TMNj352V062Tr5e3hAoipkoxCbY1JWTzor0zNps= @@ -8,8 +8,8 @@ github.com/hajimehoshi/bitmapfont/v2 v2.2.3/go.mod h1:sWM8ejdkGSXaQGlZcegMRx4DyE github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68= github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo= github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo= -github.com/hajimehoshi/oto/v2 v2.4.0-alpha.12.0.20230303060416-96fac77ab05c h1:Zz5h4yaLbb0QgZR8b4yeoq9/Tf4nIfrwWYK7+QEKJmY= -github.com/hajimehoshi/oto/v2 v2.4.0-alpha.12.0.20230303060416-96fac77ab05c/go.mod h1:Rx9ZsZ6xwfVYYx1tc1BsvbjeKo5R990kIr/oJrRlDhQ= +github.com/hajimehoshi/oto/v2 v2.4.0-alpha.12.0.20230303144707-bd76255e9f1e h1:vlS3vRDiR7acJ7/rZgU/ZMtO2r2xjMX//DzP+opi4t0= +github.com/hajimehoshi/oto/v2 v2.4.0-alpha.12.0.20230303144707-bd76255e9f1e/go.mod h1:jqq61RF13bcB4FM2PuciF4lCL0i+GMlabqUleUWOh8U= github.com/jakecoffman/cp v1.2.1 h1:zkhc2Gpo9l4NLUZfeG3j33+3bQD7MkqPa+n5PdX+5mI= github.com/jakecoffman/cp v1.2.1/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg= github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk= diff --git a/internal/gamepad/api_cf_darwin.go b/internal/gamepad/api_cf_darwin.go index 4fcef1241..da0da64db 100644 --- a/internal/gamepad/api_cf_darwin.go +++ b/internal/gamepad/api_cf_darwin.go @@ -52,15 +52,35 @@ const ( ) var ( - corefoundation = purego.Dlopen("CoreFoundation.framework/CoreFoundation", purego.RTLD_LAZY|purego.RTLD_GLOBAL) - - kCFTypeDictionaryKeyCallBacks = purego.Dlsym(corefoundation, "kCFTypeDictionaryKeyCallBacks") - kCFTypeDictionaryValueCallBacks = purego.Dlsym(corefoundation, "kCFTypeDictionaryValueCallBacks") - kCFTypeArrayCallBacks = purego.Dlsym(corefoundation, "kCFTypeArrayCallBacks") - kCFRunLoopDefaultMode = purego.Dlsym(corefoundation, "kCFRunLoopDefaultMode") + kCFTypeDictionaryKeyCallBacks uintptr + kCFTypeDictionaryValueCallBacks uintptr + kCFTypeArrayCallBacks uintptr + kCFRunLoopDefaultMode uintptr ) -func init() { +func initializeCF() error { + corefoundation, err := purego.Dlopen("CoreFoundation.framework/CoreFoundation", purego.RTLD_LAZY|purego.RTLD_GLOBAL) + if err != nil { + return err + } + + kCFTypeDictionaryKeyCallBacks, err = purego.Dlsym(corefoundation, "kCFTypeDictionaryKeyCallBacks") + if err != nil { + return err + } + kCFTypeDictionaryValueCallBacks, err = purego.Dlsym(corefoundation, "kCFTypeDictionaryValueCallBacks") + if err != nil { + return err + } + kCFTypeArrayCallBacks, err = purego.Dlsym(corefoundation, "kCFTypeArrayCallBacks") + if err != nil { + return err + } + kCFRunLoopDefaultMode, err = purego.Dlsym(corefoundation, "kCFRunLoopDefaultMode") + if err != nil { + return err + } + purego.RegisterLibFunc(&_CFNumberCreate, corefoundation, "CFNumberCreate") purego.RegisterLibFunc(&_CFNumberGetValue, corefoundation, "CFNumberGetValue") purego.RegisterLibFunc(&_CFArrayCreate, corefoundation, "CFArrayCreate") @@ -73,6 +93,8 @@ func init() { purego.RegisterLibFunc(&_CFGetTypeID, corefoundation, "CFGetTypeID") purego.RegisterLibFunc(&_CFStringGetCString, corefoundation, "CFStringGetCString") purego.RegisterLibFunc(&_CFStringCreateWithCString, corefoundation, "CFStringCreateWithCString") + + return nil } var ( diff --git a/internal/gamepad/api_hid_darwin.go b/internal/gamepad/api_hid_darwin.go index fa0339e7c..7d5b35857 100644 --- a/internal/gamepad/api_hid_darwin.go +++ b/internal/gamepad/api_hid_darwin.go @@ -86,8 +86,11 @@ type ( type _IOHIDDeviceCallback func(context unsafe.Pointer, result _IOReturn, sender unsafe.Pointer, device _IOHIDDeviceRef) -func init() { - iokit := purego.Dlopen("IOKit.framework/IOKit", purego.RTLD_LAZY|purego.RTLD_GLOBAL) +func initializeIOKit() error { + iokit, err := purego.Dlopen("IOKit.framework/IOKit", purego.RTLD_LAZY|purego.RTLD_GLOBAL) + if err != nil { + return err + } purego.RegisterLibFunc(&_IOHIDElementGetTypeID, iokit, "IOHIDElementGetTypeID") purego.RegisterLibFunc(&_IOHIDManagerCreate, iokit, "IOHIDManagerCreate") @@ -105,6 +108,8 @@ func init() { purego.RegisterLibFunc(&_IOHIDDeviceGetValue, iokit, "IOHIDDeviceGetValue") purego.RegisterLibFunc(&_IOHIDValueGetIntegerValue, iokit, "IOHIDValueGetIntegerValue") purego.RegisterLibFunc(&_IOHIDDeviceCopyMatchingElements, iokit, "IOHIDDeviceCopyMatchingElements") + + return nil } var ( diff --git a/internal/gamepad/gamepad_darwin.go b/internal/gamepad/gamepad_darwin.go index d573032f8..01b76bb43 100644 --- a/internal/gamepad/gamepad_darwin.go +++ b/internal/gamepad/gamepad_darwin.go @@ -40,6 +40,13 @@ func newNativeGamepadsImpl() nativeGamepads { } func (g *nativeGamepadsImpl) init(gamepads *gamepads) error { + if err := initializeCF(); err != nil { + return err + } + if err := initializeIOKit(); err != nil { + return err + } + var dicts []_CFDictionaryRef page := kHIDPage_GenericDesktop diff --git a/internal/graphicsdriver/metal/ca/ca_darwin.go b/internal/graphicsdriver/metal/ca/ca_darwin.go index 30c69a300..378c6c4cb 100644 --- a/internal/graphicsdriver/metal/ca/ca_darwin.go +++ b/internal/graphicsdriver/metal/ca/ca_darwin.go @@ -48,24 +48,37 @@ type MetalLayer struct { metalLayer objc.ID } -var ( - coreGraphics = purego.Dlopen("/System/Library/Frameworks/CoreGraphics.framework/Versions/Current/CoreGraphics", purego.RTLD_LAZY|purego.RTLD_GLOBAL) - _CGColorSpaceCreateWithName = purego.Dlsym(coreGraphics, "CGColorSpaceCreateWithName") - _CGColorSpaceRelease = purego.Dlsym(coreGraphics, "CGColorSpaceRelease") - kCGColorSpaceDisplayP3 = purego.Dlsym(coreGraphics, "kCGColorSpaceDisplayP3") -) - // MakeMetalLayer creates a new Core Animation Metal layer. // // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer. -func MakeMetalLayer() MetalLayer { +func MakeMetalLayer() (MetalLayer, error) { + coreGraphics, err := purego.Dlopen("/System/Library/Frameworks/CoreGraphics.framework/Versions/Current/CoreGraphics", purego.RTLD_LAZY|purego.RTLD_GLOBAL) + if err != nil { + return MetalLayer{}, err + } + + cgColorSpaceCreateWithName, err := purego.Dlsym(coreGraphics, "CGColorSpaceCreateWithName") + if err != nil { + return MetalLayer{}, err + } + + cgColorSpaceRelease, err := purego.Dlsym(coreGraphics, "CGColorSpaceRelease") + if err != nil { + return MetalLayer{}, err + } + + kCGColorSpaceDisplayP3, err := purego.Dlsym(coreGraphics, "kCGColorSpaceDisplayP3") + if err != nil { + return MetalLayer{}, err + } + layer := objc.ID(objc.GetClass("CAMetalLayer")).Send(objc.RegisterName("new")) if runtime.GOOS != "ios" { - colorspace, _, _ := purego.SyscallN(_CGColorSpaceCreateWithName, **(**uintptr)(unsafe.Pointer(&kCGColorSpaceDisplayP3))) // Dlsym returns pointer to symbol so dereference it + colorspace, _, _ := purego.SyscallN(cgColorSpaceCreateWithName, **(**uintptr)(unsafe.Pointer(&kCGColorSpaceDisplayP3))) // Dlsym returns pointer to symbol so dereference it layer.Send(objc.RegisterName("setColorspace:"), colorspace) - purego.SyscallN(_CGColorSpaceRelease, colorspace) + purego.SyscallN(cgColorSpaceRelease, colorspace) } - return MetalLayer{layer} + return MetalLayer{layer}, nil } // Layer implements the Layer interface. diff --git a/internal/graphicsdriver/metal/graphics_darwin.go b/internal/graphicsdriver/metal/graphics_darwin.go index fb733eddf..60cb34f14 100644 --- a/internal/graphicsdriver/metal/graphics_darwin.go +++ b/internal/graphicsdriver/metal/graphics_darwin.go @@ -78,8 +78,8 @@ var creatingSystemDefaultDeviceSucceeded bool func init() { // mtl.CreateSystemDefaultDevice must be called on the main thread (#2147). - _, ok := mtl.CreateSystemDefaultDevice() - creatingSystemDefaultDeviceSucceeded = ok + _, err := mtl.CreateSystemDefaultDevice() + creatingSystemDefaultDeviceSucceeded = err == nil } // NewGraphics creates an implementation of graphicsdriver.Graphics for Metal. diff --git a/internal/graphicsdriver/metal/mtl/mtl_darwin.go b/internal/graphicsdriver/metal/mtl/mtl_darwin.go index 822ea02e0..5e0cdeac0 100644 --- a/internal/graphicsdriver/metal/mtl/mtl_darwin.go +++ b/internal/graphicsdriver/metal/mtl/mtl_darwin.go @@ -355,11 +355,6 @@ const ( CommandBufferStatusError CommandBufferStatus = 5 // Execution of the command buffer was aborted due to an error during execution. ) -var ( - metal = purego.Dlopen("Metal.framework/Metal", purego.RTLD_LAZY|purego.RTLD_GLOBAL) - _MTLCreateSystemDefaultDevice = purego.Dlsym(metal, "MTLCreateSystemDefaultDevice") -) - // Resource represents a memory allocation for storing specialized data // that is accessible to the GPU. // @@ -573,10 +568,20 @@ var ( // CreateSystemDefaultDevice returns the preferred system default Metal device. // // Reference: https://developer.apple.com/documentation/metal/1433401-mtlcreatesystemdefaultdevice. -func CreateSystemDefaultDevice() (Device, bool) { - d, _, _ := purego.SyscallN(_MTLCreateSystemDefaultDevice) +func CreateSystemDefaultDevice() (Device, error) { + metal, err := purego.Dlopen("Metal.framework/Metal", purego.RTLD_LAZY|purego.RTLD_GLOBAL) + if err != nil { + return Device{}, err + } + + mtlCreateSystemDefaultDevice, err := purego.Dlsym(metal, "MTLCreateSystemDefaultDevice") + if err != nil { + return Device{}, err + } + + d, _, _ := purego.SyscallN(mtlCreateSystemDefaultDevice) if d == 0 { - return Device{}, false + return Device{}, fmt.Errorf("mtl: MTLCreateSystemDefaultDevice returned 0") } var ( headless bool @@ -594,7 +599,7 @@ func CreateSystemDefaultDevice() (Device, bool) { Headless: headless, LowPower: lowPower, Name: name, - }, true + }, nil } // Device returns the underlying id pointer. diff --git a/internal/graphicsdriver/metal/view_darwin.go b/internal/graphicsdriver/metal/view_darwin.go index d62d9ce5e..f1a876923 100644 --- a/internal/graphicsdriver/metal/view_darwin.go +++ b/internal/graphicsdriver/metal/view_darwin.go @@ -15,7 +15,6 @@ package metal import ( - "errors" "sync" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/ca" @@ -60,13 +59,17 @@ func (v *view) colorPixelFormat() mtl.PixelFormat { } func (v *view) initialize() error { - var ok bool - v.device, ok = mtl.CreateSystemDefaultDevice() - if !ok { - return errors.New("metal: Metal is not supported") + d, err := mtl.CreateSystemDefaultDevice() + if err != nil { + return err } + v.device = d - v.ml = ca.MakeMetalLayer() + ml, err := ca.MakeMetalLayer() + if err != nil { + return err + } + v.ml = ml v.ml.SetDevice(v.device) // https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat // diff --git a/internal/graphicsdriver/opengl/gl/procaddr_darwin.go b/internal/graphicsdriver/opengl/gl/procaddr_darwin.go index ada2dbd48..6ef568636 100644 --- a/internal/graphicsdriver/opengl/gl/procaddr_darwin.go +++ b/internal/graphicsdriver/opengl/gl/procaddr_darwin.go @@ -26,21 +26,31 @@ var ( ) func (c *defaultContext) init() error { - opengl = purego.Dlopen("OpenGLES.framework/OpenGLES", purego.RTLD_LAZY|purego.RTLD_GLOBAL) - if opengl != 0 { + lib, errGLES := purego.Dlopen("OpenGLES.framework/OpenGLES", purego.RTLD_LAZY|purego.RTLD_GLOBAL) + if errGLES == nil { c.isES = true + opengl = lib return nil } - opengl = purego.Dlopen("OpenGL.framework/OpenGL", purego.RTLD_LAZY|purego.RTLD_GLOBAL) - if opengl != 0 { + + lib, errGL := purego.Dlopen("OpenGL.framework/OpenGL", purego.RTLD_LAZY|purego.RTLD_GLOBAL) + if errGL == nil { + opengl = lib return nil } - return fmt.Errorf("gl: failed to load OpenGL.framework and OpenGLES.framework") + + // TODO: Use multiple %w-s as of Go 1.20 + return fmt.Errorf("gl: failed to load: OpenGL.framework: %v, OpenGLES.framework: %v", errGL, errGLES) } func (c *defaultContext) getProcAddress(name string) uintptr { if c.isES { name = strings.TrimSuffix(name, "EXT") } - return purego.Dlsym(opengl, name) + proc, err := purego.Dlsym(opengl, name) + if err != nil { + // The proc is not found. + return 0 + } + return proc }