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: 2022 The Ebitengine Authors
//go:build !darwin && !js && !windows && !playstation5
//go:build nintendosdk
package gl

View File

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

View File

@ -16,38 +16,18 @@
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 (
"fmt"
"os"
"runtime"
"strings"
"unsafe"
"github.com/ebitengine/purego"
)
var (
libGL unsafe.Pointer
libGLES unsafe.Pointer
libGL uintptr
libGLES uintptr
)
func (c *defaultContext) init() error {
@ -69,10 +49,8 @@ func (c *defaultContext) init() error {
if !preferES {
// 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"} {
cname := C.CString(name)
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
C.free(unsafe.Pointer(cname))
if lib != nil {
lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
if err == nil {
libGL = lib
return nil
}
@ -81,10 +59,8 @@ func (c *defaultContext) init() error {
// Try OpenGL ES.
for _, name := range []string{"libGLESv2.so", "libGLESv2.so.2", "libGLESv2.so.1", "libGLESv2.so.0"} {
cname := C.CString(name)
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
C.free(unsafe.Pointer(cname))
if lib != nil {
lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
if err == nil {
libGLES = lib
c.isES = true
return nil
@ -96,19 +72,32 @@ func (c *defaultContext) init() error {
func (c *defaultContext) getProcAddress(name string) (uintptr, error) {
if c.isES {
return getProcAddressGLES(name), nil
return getProcAddressGLES(name)
}
return getProcAddressGL(name), nil
return getProcAddressGL(name)
}
func getProcAddressGL(name string) uintptr {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return uintptr(C.getProcAddressGL(libGL, cname))
var glXGetProcAddress func(name string) uintptr
func getProcAddressGL(name string) (uintptr, error) {
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 {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return uintptr(C.getProcAddressGLES(libGLES, cname))
func getProcAddressGLES(name string) (uintptr, error) {
proc, err := purego.Dlsym(libGLES, name)
if err != nil {
return 0, err
}
return proc, nil
}