internal/graphicsdriver/gl: use PureGo for Linux and UNIX

Updates #2284
This commit is contained in:
Hajime Hoshi 2024-04-07 02:04:02 +09:00
parent d6f7b50387
commit 5701206229
3 changed files with 33 additions and 44 deletions

View File

@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2014 Eric Woroshow // SPDX-FileCopyrightText: 2014 Eric Woroshow
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors // SPDX-FileCopyrightText: 2022 The Ebitengine Authors
//go:build !darwin && !js && !windows && !playstation5 //go:build nintendosdk
package gl package gl

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//go:build darwin || windows //go:build (darwin || freebsd || linux || netbsd || openbsd || windows) && !nintendosdk && !playstation5
package gl package gl

View File

@ -16,38 +16,18 @@
package gl package gl
// #cgo LDFLAGS: -ldl
//
// #include <dlfcn.h>
// #include <stdlib.h>
//
// static void* getProcAddressGL(void* libGL, const char* name) {
// static void*(*glXGetProcAddress)(const char*);
// if (!glXGetProcAddress) {
// glXGetProcAddress = dlsym(libGL, "glXGetProcAddress");
// if (!glXGetProcAddress) {
// glXGetProcAddress = dlsym(libGL, "glXGetProcAddressARB");
// }
// }
// return glXGetProcAddress(name);
// }
//
// static void* getProcAddressGLES(void* libGLES, const char* name) {
// return dlsym(libGLES, name);
// }
import "C"
import ( import (
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
"strings" "strings"
"unsafe"
"github.com/ebitengine/purego"
) )
var ( var (
libGL unsafe.Pointer libGL uintptr
libGLES unsafe.Pointer libGLES uintptr
) )
func (c *defaultContext) init() error { func (c *defaultContext) init() error {
@ -69,10 +49,8 @@ func (c *defaultContext) init() error {
if !preferES { if !preferES {
// Usually libGL.so or libGL.so.1 is used. libGL.so.2 might exist only on NetBSD. // Usually libGL.so or libGL.so.1 is used. libGL.so.2 might exist only on NetBSD.
for _, name := range []string{"libGL.so", "libGL.so.2", "libGL.so.1", "libGL.so.0"} { for _, name := range []string{"libGL.so", "libGL.so.2", "libGL.so.1", "libGL.so.0"} {
cname := C.CString(name) lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL) if err == nil {
C.free(unsafe.Pointer(cname))
if lib != nil {
libGL = lib libGL = lib
return nil return nil
} }
@ -81,10 +59,8 @@ func (c *defaultContext) init() error {
// Try OpenGL ES. // Try OpenGL ES.
for _, name := range []string{"libGLESv2.so", "libGLESv2.so.2", "libGLESv2.so.1", "libGLESv2.so.0"} { for _, name := range []string{"libGLESv2.so", "libGLESv2.so.2", "libGLESv2.so.1", "libGLESv2.so.0"} {
cname := C.CString(name) lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL) if err == nil {
C.free(unsafe.Pointer(cname))
if lib != nil {
libGLES = lib libGLES = lib
c.isES = true c.isES = true
return nil return nil
@ -96,19 +72,32 @@ func (c *defaultContext) init() error {
func (c *defaultContext) getProcAddress(name string) (uintptr, error) { func (c *defaultContext) getProcAddress(name string) (uintptr, error) {
if c.isES { if c.isES {
return getProcAddressGLES(name), nil return getProcAddressGLES(name)
} }
return getProcAddressGL(name), nil return getProcAddressGL(name)
} }
func getProcAddressGL(name string) uintptr { var glXGetProcAddress func(name string) uintptr
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname)) func getProcAddressGL(name string) (uintptr, error) {
return uintptr(C.getProcAddressGL(libGL, cname)) if glXGetProcAddress == nil {
if _, err := purego.Dlsym(libGL, "glXGetProcAddress"); err == nil {
purego.RegisterLibFunc(&glXGetProcAddress, libGL, "glXGetProcAddress")
} else if _, err := purego.Dlsym(libGL, "glXGetProcAddressARB"); err == nil {
purego.RegisterLibFunc(&glXGetProcAddress, libGL, "glXGetProcAddressARB")
}
}
if glXGetProcAddress == nil {
return 0, fmt.Errorf("gl: failed to find glXGetProcAddress or glXGetProcAddressARB in libGL.so")
}
return glXGetProcAddress(name), nil
} }
func getProcAddressGLES(name string) uintptr { func getProcAddressGLES(name string) (uintptr, error) {
cname := C.CString(name) proc, err := purego.Dlsym(libGLES, name)
defer C.free(unsafe.Pointer(cname)) if err != nil {
return uintptr(C.getProcAddressGLES(libGLES, cname)) return 0, err
}
return proc, nil
} }