diff --git a/gl/egl.go b/gl/egl.go new file mode 100644 index 000000000..cbf3d5917 --- /dev/null +++ b/gl/egl.go @@ -0,0 +1,59 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build linux +// +build linux + +package gl + +// #include +// #cgo LDFLAGS: -lEGL +import "C" + +const ( + versionES2 = "GL_ES_2_0" + versionES3 = "GL_ES_3_0" +) + +func init() { + display := C.eglGetDisplay(C.EGL_DEFAULT_DISPLAY) + + if C.eglInitialize(display, nil, nil) == C.EGL_FALSE { + return + } + defer C.eglTerminate(display) + + attributes := []C.EGLint{ + C.EGL_RED_SIZE, 1, + C.EGL_GREEN_SIZE, 1, + C.EGL_BLUE_SIZE, 1, + C.EGL_NONE, + } + + var ( + config C.EGLConfig + count C.EGLint + ) + if C.eglChooseConfig(display, &attributes[0], &config, 1, &count) == C.EGL_FALSE { + return + } + + C.eglBindAPI(C.EGL_OPENGL_ES_API) + + attributes = []C.EGLint{ + C.EGL_CONTEXT_CLIENT_VERSION, 3, + C.EGL_NONE, + } + + context := C.eglCreateContext(display, config, C.EGLContext(C.EGL_NO_CONTEXT), &attributes[0]) + if context == nil { + version = versionES2 + + return + } + + C.eglDestroyContext(display, context) + + version = versionES3 +} diff --git a/gl/work.go b/gl/work.go index 73b566d9d..663b6b747 100644 --- a/gl/work.go +++ b/gl/work.go @@ -84,16 +84,18 @@ func NewContext() (Context, Worker) { work: make(chan call, workbufLen), retvalue: make(chan C.uintptr_t), } - if C.GLES_VERSION == "GL_ES_2_0" { + if version == "GL_ES_2_0" { return glctx, glctx } return context3{glctx}, glctx } -// Version returns a GL ES version string, either "GL_ES_2_0" or "GL_ES_3_0". -// Future versions of the gl package may return "GL_ES_3_1". +// version is determined at runtime on platforms that support EGL. +var version = C.GLES_VERSION + +// Version returns a GL ES Version string such as "GL_ES_2_0" or "GL_ES_3_0". func Version() string { - return C.GLES_VERSION + return version } func (ctx *context) enqueue(c call) uintptr { @@ -148,11 +150,8 @@ func (ctx *context) DoWork() { } } -func init() { - if unsafe.Sizeof(C.GLint(0)) != unsafe.Sizeof(int32(0)) { - panic("GLint is not an int32") - } -} +// GLint should be the same size as int32 +var _ [unsafe.Sizeof(C.GLint(0))]struct{} = [unsafe.Sizeof(int32(0))]struct{}{} // cString creates C string off the Go heap. // ret is a *char.