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.
|
|
|
|
|
2024-08-05 14:18:51 +02:00
|
|
|
//go:build (freebsd || linux || netbsd || openbsd) && !nintendosdk && !playstation5
|
2022-11-13 19:42:37 +01:00
|
|
|
|
|
|
|
package gl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-08-05 14:18:51 +02:00
|
|
|
"runtime"
|
2022-11-13 19:42:37 +01:00
|
|
|
"strings"
|
2024-04-06 19:04:02 +02:00
|
|
|
|
|
|
|
"github.com/ebitengine/purego"
|
2022-11-13 19:42:37 +01:00
|
|
|
)
|
|
|
|
|
2023-01-07 09:01:00 +01:00
|
|
|
var (
|
2024-04-06 19:04:02 +02:00
|
|
|
libGL uintptr
|
|
|
|
libGLES uintptr
|
2023-01-07 09:01:00 +01:00
|
|
|
)
|
|
|
|
|
2022-11-13 19:42:37 +01:00
|
|
|
func (c *defaultContext) init() error {
|
|
|
|
var preferES bool
|
2024-08-05 14:18:51 +02: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 14:35:20 +02:00
|
|
|
// TODO: Use multiple %w-s as of Go 1.20.
|
|
|
|
var errors []string
|
|
|
|
|
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.
|
2024-04-13 15:23:50 +02:00
|
|
|
// TODO: Should "libOpenGL.so.0" [1] and "libGLX.so.0" [2] be added? These were added as of GLFW 3.3.9.
|
|
|
|
// [1] https://github.com/glfw/glfw/commit/55aad3c37b67f17279378db52da0a3ab81bbf26d
|
|
|
|
// [2] https://github.com/glfw/glfw/commit/c18851f52ec9704eb06464058a600845ec1eada1
|
2023-01-13 07:05:57 +01:00
|
|
|
for _, name := range []string{"libGL.so", "libGL.so.2", "libGL.so.1", "libGL.so.0"} {
|
2024-04-06 19:04:02 +02:00
|
|
|
lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
|
|
|
|
if err == nil {
|
2023-01-07 09:01:00 +01:00
|
|
|
libGL = lib
|
2023-01-06 19:01:28 +01:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-31 14:35:20 +02:00
|
|
|
errors = append(errors, fmt.Sprintf("%s: %v", name, err))
|
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"} {
|
2024-04-06 19:04:02 +02:00
|
|
|
lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
|
|
|
|
if err == 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
|
|
|
|
}
|
2024-07-31 14:35:20 +02:00
|
|
|
errors = append(errors, fmt.Sprintf("%s: %v", name, err))
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
|
|
|
|
2024-07-31 14:35:20 +02:00
|
|
|
return fmt.Errorf("gl: failed to load libGL.so and libGLESv2.so: %s", strings.Join(errors, ", "))
|
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 {
|
2024-04-06 19:04:02 +02:00
|
|
|
return getProcAddressGLES(name)
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
2024-04-06 19:04:02 +02:00
|
|
|
return getProcAddressGL(name)
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 19:04:02 +02:00
|
|
|
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
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|
|
|
|
|
2024-04-06 19:04:02 +02:00
|
|
|
func getProcAddressGLES(name string) (uintptr, error) {
|
|
|
|
proc, err := purego.Dlsym(libGLES, name)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return proc, nil
|
2022-11-13 19:42:37 +01:00
|
|
|
}
|