2022-11-13 19:42:37 +01:00
|
|
|
// Copyright 2022 The Ebitengine Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-11-14 15:02:59 +01:00
|
|
|
//go:build !darwin && !js && !nintendosdk && !windows
|
2022-11-13 19:42:37 +01:00
|
|
|
|
|
|
|
package gl
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -ldl
|
|
|
|
//
|
|
|
|
// #include <dlfcn.h>
|
|
|
|
// #include <stdlib.h>
|
|
|
|
//
|
2023-01-07 09:01:00 +01:00
|
|
|
// static void* getProcAddressGL(void* libGL, const char* name) {
|
2022-11-13 19:42:37 +01:00
|
|
|
// static void*(*glXGetProcAddress)(const char*);
|
|
|
|
// if (!glXGetProcAddress) {
|
2023-01-07 09:01:00 +01:00
|
|
|
// glXGetProcAddress = dlsym(libGL, "glXGetProcAddress");
|
2022-11-13 19:42:37 +01:00
|
|
|
// if (!glXGetProcAddress) {
|
2023-01-07 09:01:00 +01:00
|
|
|
// glXGetProcAddress = dlsym(libGL, "glXGetProcAddressARB");
|
2022-11-13 19:42:37 +01:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return glXGetProcAddress(name);
|
|
|
|
// }
|
|
|
|
//
|
2023-01-07 09:01:00 +01:00
|
|
|
// static void* getProcAddressGLES(void* libGLES, const char* name) {
|
|
|
|
// return dlsym(libGLES, name);
|
2022-11-13 19:42:37 +01:00
|
|
|
// }
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-11-14 15:02:59 +01:00
|
|
|
"runtime"
|
2022-11-13 19:42:37 +01:00
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2023-01-07 09:01:00 +01:00
|
|
|
var (
|
|
|
|
libGL unsafe.Pointer
|
|
|
|
libGLES unsafe.Pointer
|
|
|
|
)
|
|
|
|
|
2022-11-13 19:42:37 +01:00
|
|
|
func (c *defaultContext) init() error {
|
|
|
|
var preferES bool
|
2022-11-14 15:02:59 +01:00
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
preferES = true
|
|
|
|
}
|
|
|
|
if !preferES {
|
|
|
|
for _, t := range strings.Split(os.Getenv("EBITENGINE_OPENGL"), ",") {
|
|
|
|
switch strings.TrimSpace(t) {
|
|
|
|
case "es":
|
|
|
|
preferES = true
|
|
|
|
break
|
|
|
|
}
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
// Try OpenGL first. OpenGL is preferable as this doesn't cause context losses.
|
2022-11-13 19:42:37 +01:00
|
|
|
if !preferES {
|
2023-01-13 07:05:57 +01:00
|
|
|
// 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"} {
|
2023-01-06 19:01:28 +01:00
|
|
|
cname := C.CString(name)
|
|
|
|
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
|
|
|
|
C.free(unsafe.Pointer(cname))
|
|
|
|
if lib != nil {
|
2023-01-07 09:01:00 +01:00
|
|
|
libGL = lib
|
2023-01-06 19:01:28 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try OpenGL ES.
|
2023-01-13 07:05:57 +01:00
|
|
|
for _, name := range []string{"libGLESv2.so", "libGLESv2.so.2", "libGLESv2.so.1", "libGLESv2.so.0"} {
|
2023-01-06 19:01:28 +01:00
|
|
|
cname := C.CString(name)
|
|
|
|
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
|
|
|
|
C.free(unsafe.Pointer(cname))
|
|
|
|
if lib != nil {
|
2023-01-07 09:01:00 +01:00
|
|
|
libGLES = lib
|
2023-01-06 19:46:30 +01:00
|
|
|
c.isES = true
|
2023-01-06 19:01:28 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-06 19:07:27 +01:00
|
|
|
return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so")
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
|
|
|
|
2023-04-22 08:09:24 +02:00
|
|
|
func (c *defaultContext) getProcAddress(name string) (uintptr, error) {
|
2022-11-13 19:42:37 +01:00
|
|
|
if c.isES {
|
2023-04-22 08:09:24 +02:00
|
|
|
return getProcAddressGLES(name), nil
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
2023-04-22 08:09:24 +02:00
|
|
|
return getProcAddressGL(name), nil
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
|
|
|
|
2023-04-22 08:09:24 +02:00
|
|
|
func getProcAddressGL(name string) uintptr {
|
2022-11-13 19:42:37 +01:00
|
|
|
cname := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cname))
|
2023-04-22 08:09:24 +02:00
|
|
|
return uintptr(C.getProcAddressGL(libGL, cname))
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
|
|
|
|
2023-04-22 08:09:24 +02:00
|
|
|
func getProcAddressGLES(name string) uintptr {
|
2022-11-14 18:41:59 +01:00
|
|
|
name = strings.TrimSuffix(name, "EXT")
|
2022-11-13 19:42:37 +01:00
|
|
|
cname := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cname))
|
2023-04-22 08:09:24 +02:00
|
|
|
return uintptr(C.getProcAddressGLES(libGLES, cname))
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|