update purego to v0.3.0-alpha

There are breaking changes between purego v0.2.0 and v0.3.0-alpha.
This commit is contained in:
Hajime Hoshi 2023-03-04 01:08:08 +09:00
parent 809ad991c2
commit 7998e4d31d
10 changed files with 114 additions and 49 deletions

4
go.mod
View File

@ -3,11 +3,11 @@ module github.com/hajimehoshi/ebiten/v2
go 1.18 go 1.18
require ( 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/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b
github.com/hajimehoshi/bitmapfont/v2 v2.2.3 github.com/hajimehoshi/bitmapfont/v2 v2.2.3
github.com/hajimehoshi/go-mp3 v0.3.4 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/jakecoffman/cp v1.2.1
github.com/jezek/xgb v1.1.0 github.com/jezek/xgb v1.1.0
github.com/jfreymuth/oggvorbis v1.0.5 github.com/jfreymuth/oggvorbis v1.0.5

8
go.sum
View File

@ -1,6 +1,6 @@
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= 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.3.0-alpha h1:7eoxaayPodJU34x1xmFE9INXm9h9quDoREmCum07z8E=
github.com/ebitengine/purego v0.2.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= 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 h1:GgabKamyOYguHqHjSkDACcgoPIz3w0Dis/zJ1wyHHHU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= 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= 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 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68=
github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo= 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.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.20230303144707-bd76255e9f1e h1:vlS3vRDiR7acJ7/rZgU/ZMtO2r2xjMX//DzP+opi4t0=
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/go.mod h1:jqq61RF13bcB4FM2PuciF4lCL0i+GMlabqUleUWOh8U=
github.com/jakecoffman/cp v1.2.1 h1:zkhc2Gpo9l4NLUZfeG3j33+3bQD7MkqPa+n5PdX+5mI= 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/jakecoffman/cp v1.2.1/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk= github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=

View File

@ -52,15 +52,35 @@ const (
) )
var ( var (
corefoundation = purego.Dlopen("CoreFoundation.framework/CoreFoundation", purego.RTLD_LAZY|purego.RTLD_GLOBAL) kCFTypeDictionaryKeyCallBacks uintptr
kCFTypeDictionaryValueCallBacks uintptr
kCFTypeDictionaryKeyCallBacks = purego.Dlsym(corefoundation, "kCFTypeDictionaryKeyCallBacks") kCFTypeArrayCallBacks uintptr
kCFTypeDictionaryValueCallBacks = purego.Dlsym(corefoundation, "kCFTypeDictionaryValueCallBacks") kCFRunLoopDefaultMode uintptr
kCFTypeArrayCallBacks = purego.Dlsym(corefoundation, "kCFTypeArrayCallBacks")
kCFRunLoopDefaultMode = purego.Dlsym(corefoundation, "kCFRunLoopDefaultMode")
) )
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(&_CFNumberCreate, corefoundation, "CFNumberCreate")
purego.RegisterLibFunc(&_CFNumberGetValue, corefoundation, "CFNumberGetValue") purego.RegisterLibFunc(&_CFNumberGetValue, corefoundation, "CFNumberGetValue")
purego.RegisterLibFunc(&_CFArrayCreate, corefoundation, "CFArrayCreate") purego.RegisterLibFunc(&_CFArrayCreate, corefoundation, "CFArrayCreate")
@ -73,6 +93,8 @@ func init() {
purego.RegisterLibFunc(&_CFGetTypeID, corefoundation, "CFGetTypeID") purego.RegisterLibFunc(&_CFGetTypeID, corefoundation, "CFGetTypeID")
purego.RegisterLibFunc(&_CFStringGetCString, corefoundation, "CFStringGetCString") purego.RegisterLibFunc(&_CFStringGetCString, corefoundation, "CFStringGetCString")
purego.RegisterLibFunc(&_CFStringCreateWithCString, corefoundation, "CFStringCreateWithCString") purego.RegisterLibFunc(&_CFStringCreateWithCString, corefoundation, "CFStringCreateWithCString")
return nil
} }
var ( var (

View File

@ -86,8 +86,11 @@ type (
type _IOHIDDeviceCallback func(context unsafe.Pointer, result _IOReturn, sender unsafe.Pointer, device _IOHIDDeviceRef) type _IOHIDDeviceCallback func(context unsafe.Pointer, result _IOReturn, sender unsafe.Pointer, device _IOHIDDeviceRef)
func init() { func initializeIOKit() error {
iokit := purego.Dlopen("IOKit.framework/IOKit", purego.RTLD_LAZY|purego.RTLD_GLOBAL) 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(&_IOHIDElementGetTypeID, iokit, "IOHIDElementGetTypeID")
purego.RegisterLibFunc(&_IOHIDManagerCreate, iokit, "IOHIDManagerCreate") purego.RegisterLibFunc(&_IOHIDManagerCreate, iokit, "IOHIDManagerCreate")
@ -105,6 +108,8 @@ func init() {
purego.RegisterLibFunc(&_IOHIDDeviceGetValue, iokit, "IOHIDDeviceGetValue") purego.RegisterLibFunc(&_IOHIDDeviceGetValue, iokit, "IOHIDDeviceGetValue")
purego.RegisterLibFunc(&_IOHIDValueGetIntegerValue, iokit, "IOHIDValueGetIntegerValue") purego.RegisterLibFunc(&_IOHIDValueGetIntegerValue, iokit, "IOHIDValueGetIntegerValue")
purego.RegisterLibFunc(&_IOHIDDeviceCopyMatchingElements, iokit, "IOHIDDeviceCopyMatchingElements") purego.RegisterLibFunc(&_IOHIDDeviceCopyMatchingElements, iokit, "IOHIDDeviceCopyMatchingElements")
return nil
} }
var ( var (

View File

@ -40,6 +40,13 @@ func newNativeGamepadsImpl() nativeGamepads {
} }
func (g *nativeGamepadsImpl) init(gamepads *gamepads) error { func (g *nativeGamepadsImpl) init(gamepads *gamepads) error {
if err := initializeCF(); err != nil {
return err
}
if err := initializeIOKit(); err != nil {
return err
}
var dicts []_CFDictionaryRef var dicts []_CFDictionaryRef
page := kHIDPage_GenericDesktop page := kHIDPage_GenericDesktop

View File

@ -48,24 +48,37 @@ type MetalLayer struct {
metalLayer objc.ID 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. // MakeMetalLayer creates a new Core Animation Metal layer.
// //
// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer. // 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")) layer := objc.ID(objc.GetClass("CAMetalLayer")).Send(objc.RegisterName("new"))
if runtime.GOOS != "ios" { 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) 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. // Layer implements the Layer interface.

View File

@ -78,8 +78,8 @@ var creatingSystemDefaultDeviceSucceeded bool
func init() { func init() {
// mtl.CreateSystemDefaultDevice must be called on the main thread (#2147). // mtl.CreateSystemDefaultDevice must be called on the main thread (#2147).
_, ok := mtl.CreateSystemDefaultDevice() _, err := mtl.CreateSystemDefaultDevice()
creatingSystemDefaultDeviceSucceeded = ok creatingSystemDefaultDeviceSucceeded = err == nil
} }
// NewGraphics creates an implementation of graphicsdriver.Graphics for Metal. // NewGraphics creates an implementation of graphicsdriver.Graphics for Metal.

View File

@ -355,11 +355,6 @@ const (
CommandBufferStatusError CommandBufferStatus = 5 // Execution of the command buffer was aborted due to an error during execution. 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 // Resource represents a memory allocation for storing specialized data
// that is accessible to the GPU. // that is accessible to the GPU.
// //
@ -573,10 +568,20 @@ var (
// 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, bool) { func CreateSystemDefaultDevice() (Device, error) {
d, _, _ := purego.SyscallN(_MTLCreateSystemDefaultDevice) 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 { if d == 0 {
return Device{}, false return Device{}, fmt.Errorf("mtl: MTLCreateSystemDefaultDevice returned 0")
} }
var ( var (
headless bool headless bool
@ -594,7 +599,7 @@ func CreateSystemDefaultDevice() (Device, bool) {
Headless: headless, Headless: headless,
LowPower: lowPower, LowPower: lowPower,
Name: name, Name: name,
}, true }, nil
} }
// Device returns the underlying id<MTLDevice> pointer. // Device returns the underlying id<MTLDevice> pointer.

View File

@ -15,7 +15,6 @@
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"
@ -60,13 +59,17 @@ func (v *view) colorPixelFormat() mtl.PixelFormat {
} }
func (v *view) initialize() error { func (v *view) initialize() error {
var ok bool d, err := mtl.CreateSystemDefaultDevice()
v.device, ok = mtl.CreateSystemDefaultDevice() if err != nil {
if !ok { return err
return errors.New("metal: Metal is not supported")
} }
v.device = d
v.ml = ca.MakeMetalLayer() ml, err := ca.MakeMetalLayer()
if err != nil {
return err
}
v.ml = ml
v.ml.SetDevice(v.device) v.ml.SetDevice(v.device)
// https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat // https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat
// //

View File

@ -26,21 +26,31 @@ var (
) )
func (c *defaultContext) init() error { func (c *defaultContext) init() error {
opengl = purego.Dlopen("OpenGLES.framework/OpenGLES", purego.RTLD_LAZY|purego.RTLD_GLOBAL) lib, errGLES := purego.Dlopen("OpenGLES.framework/OpenGLES", purego.RTLD_LAZY|purego.RTLD_GLOBAL)
if opengl != 0 { if errGLES == nil {
c.isES = true c.isES = true
opengl = lib
return nil 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 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 { func (c *defaultContext) getProcAddress(name string) uintptr {
if c.isES { if c.isES {
name = strings.TrimSuffix(name, "EXT") 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
} }