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"
|
|
|
|
"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 {
|
2024-07-31 14:35:20 +02:00
|
|
|
// TODO: Use multiple %w-s as of Go 1.20.
|
|
|
|
var errors []string
|
|
|
|
|
2024-08-09 18:21:35 +02:00
|
|
|
// Try OpenGL ES first. Some machines like Android and Raspberry Pi might work only with 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-08-09 18:21:35 +02:00
|
|
|
// Try OpenGL next.
|
|
|
|
// Usually libGL.so or libGL.so.1 is used. libGL.so.2 might exist only on NetBSD.
|
|
|
|
// 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
|
|
|
|
for _, name := range []string{"libGL.so", "libGL.so.2", "libGL.so.1", "libGL.so.0"} {
|
|
|
|
lib, err := purego.Dlopen(name, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
|
|
|
|
if err == nil {
|
|
|
|
libGL = lib
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
errors = append(errors, fmt.Sprintf("%s: %v", name, err))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|