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-06 19:01:28 +01:00
|
|
|
// static void* libGLPtr;
|
|
|
|
// static void* libGLESPtr;
|
2023-01-06 15:55:19 +01:00
|
|
|
//
|
2023-01-06 19:01:28 +01:00
|
|
|
// static void setLibGL(void* lib) {
|
|
|
|
// libGLPtr = lib;
|
2023-01-06 15:55:19 +01:00
|
|
|
// }
|
|
|
|
//
|
2023-01-06 19:01:28 +01:00
|
|
|
// static void setLibGLES(void* lib) {
|
|
|
|
// libGLESPtr = lib;
|
2023-01-06 15:55:19 +01:00
|
|
|
// }
|
|
|
|
//
|
2022-11-13 19:42:37 +01:00
|
|
|
// static void* libGL() {
|
2023-01-06 19:01:28 +01:00
|
|
|
// return libGLPtr;
|
2022-11-13 19:42:37 +01:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// static void* libGLES() {
|
2023-01-06 19:01:28 +01:00
|
|
|
// return libGLESPtr;
|
2022-11-13 19:42:37 +01:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// static void* getProcAddressGL(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(const char* name) {
|
|
|
|
// return dlsym(libGLES(), name);
|
|
|
|
// }
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-01-06 15:55:19 +01:00
|
|
|
"path/filepath"
|
2022-11-14 15:02:59 +01:00
|
|
|
"runtime"
|
2023-01-06 15:55:19 +01:00
|
|
|
"sort"
|
2022-11-13 19:42:37 +01:00
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2023-01-06 19:01:28 +01:00
|
|
|
// listLibs returns an appropriate library file paths based on the given library paths and the library name as a prefix.
|
|
|
|
// Note that the found libraries might not be available e.g. due to architecture mismatches.
|
|
|
|
func listLibs(libraryPaths []string, libName string) ([]string, error) {
|
|
|
|
// LD_LIBRARY_PATH might be empty. Use the original name as a candidate.
|
|
|
|
libNames := []string{libName}
|
|
|
|
|
2023-01-06 15:55:19 +01:00
|
|
|
// Look for a library file. In some environments like Steam, a library with the exactly same name might not exist (#2523).
|
|
|
|
// For example, libGL.so.1 might exist instead of libGL.so.
|
|
|
|
for _, dir := range libraryPaths {
|
2023-01-06 19:01:28 +01:00
|
|
|
libs, err := listLibsInDirectory(dir, libName)
|
2023-01-06 15:55:19 +01:00
|
|
|
if err != nil {
|
2023-01-06 19:01:28 +01:00
|
|
|
return nil, err
|
2023-01-06 15:55:19 +01:00
|
|
|
}
|
|
|
|
if len(libs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-06 19:07:27 +01:00
|
|
|
// The file names are sorted in the alphabetical order.
|
2023-01-06 15:55:19 +01:00
|
|
|
// TODO: What is the best version to use?
|
2023-01-06 19:01:28 +01:00
|
|
|
sort.Strings(libs)
|
|
|
|
|
|
|
|
libNames = append(libNames, libs...)
|
2023-01-06 15:55:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-06 19:01:28 +01:00
|
|
|
return libNames, nil
|
2023-01-06 15:55:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-06 19:01:28 +01:00
|
|
|
// listLibsInDirectory returns library file paths with the given prefix in the directory.
|
|
|
|
// Note that the found libraries might not be available e.g. due to architecture mismatches.
|
|
|
|
func listLibsInDirectory(dir string, prefix string) ([]string, error) {
|
2023-01-06 15:55:19 +01:00
|
|
|
ents, err := os.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var files []string
|
|
|
|
for _, ent := range ents {
|
|
|
|
if ent.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ent.Name() == prefix {
|
2023-01-06 19:01:28 +01:00
|
|
|
files = append(files, filepath.Join(dir, ent.Name()))
|
2023-01-06 15:55:19 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(ent.Name(), prefix+".") {
|
2023-01-06 19:01:28 +01:00
|
|
|
files = append(files, filepath.Join(dir, ent.Name()))
|
2023-01-06 15:55:19 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
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-06 15:55:19 +01:00
|
|
|
libraryPaths := filepath.SplitList(os.Getenv("LD_LIBRARY_PATH"))
|
|
|
|
|
2022-11-13 19:42:37 +01:00
|
|
|
// Try OpenGL first. OpenGL is preferrable as this doesn't cause context losts.
|
|
|
|
if !preferES {
|
2023-01-06 19:01:28 +01:00
|
|
|
names, err := listLibs(libraryPaths, "libGL.so")
|
2023-01-06 15:55:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-06 19:01:28 +01:00
|
|
|
for _, name := range names {
|
|
|
|
cname := C.CString(name)
|
|
|
|
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
|
|
|
|
C.free(unsafe.Pointer(cname))
|
|
|
|
if lib != nil {
|
|
|
|
C.setLibGL(lib)
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try OpenGL ES.
|
2023-01-06 19:01:28 +01:00
|
|
|
names, err := listLibs(libraryPaths, "libGLESv2.so")
|
2023-01-06 15:55:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-06 19:01:28 +01:00
|
|
|
for _, name := range names {
|
|
|
|
cname := C.CString(name)
|
|
|
|
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
|
|
|
|
C.free(unsafe.Pointer(cname))
|
|
|
|
if lib != nil {
|
|
|
|
C.setLibGLES(lib)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultContext) getProcAddress(name string) unsafe.Pointer {
|
|
|
|
if c.isES {
|
|
|
|
return getProcAddressGLES(name)
|
|
|
|
}
|
|
|
|
return getProcAddressGL(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProcAddressGL(name string) unsafe.Pointer {
|
|
|
|
cname := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cname))
|
|
|
|
return C.getProcAddressGL(cname)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProcAddressGLES(name string) unsafe.Pointer {
|
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))
|
|
|
|
return C.getProcAddressGLES(cname)
|
|
|
|
}
|