From 5b96d93d13f5c196f459d98bef73e7332ce98c2b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 13 Nov 2022 22:21:45 +0900 Subject: [PATCH] internal/graphicsdriver/opengl/gl: refactoring --- .../opengl/gl/conversions_cgo.go | 25 ------------------ .../opengl/gl/conversions_nocgo.go | 26 ------------------- .../opengl/gl/default_notpurego.go | 15 +++++------ .../opengl/gl/default_purego.go | 16 ++++++++++++ .../opengl/gl/procaddr_others.go | 20 +++----------- 5 files changed, 26 insertions(+), 76 deletions(-) delete mode 100644 internal/graphicsdriver/opengl/gl/conversions_cgo.go delete mode 100644 internal/graphicsdriver/opengl/gl/conversions_nocgo.go diff --git a/internal/graphicsdriver/opengl/gl/conversions_cgo.go b/internal/graphicsdriver/opengl/gl/conversions_cgo.go deleted file mode 100644 index 3e7214403..000000000 --- a/internal/graphicsdriver/opengl/gl/conversions_cgo.go +++ /dev/null @@ -1,25 +0,0 @@ -// SPDX-License-Identifier: MIT -// SPDX-FileCopyrightText: 2014 Eric Woroshow - -//go:build !android && !darwin && !js && !windows && !opengles - -package gl - -import ( - "unsafe" -) - -// #include -import "C" - -// cStr takes a Go string (with or without null-termination) -// and returns the C counterpart. -// -// The returned free function must be called once you are done using the string -// in order to free the memory. -func cStr(str string) (cstr *byte, free func()) { - cs := C.CString(str) - return (*byte)(unsafe.Pointer(cs)), func() { - C.free(unsafe.Pointer(cs)) - } -} diff --git a/internal/graphicsdriver/opengl/gl/conversions_nocgo.go b/internal/graphicsdriver/opengl/gl/conversions_nocgo.go deleted file mode 100644 index 9550ab480..000000000 --- a/internal/graphicsdriver/opengl/gl/conversions_nocgo.go +++ /dev/null @@ -1,26 +0,0 @@ -// SPDX-License-Identifier: MIT -// SPDX-FileCopyrightText: 2014 Eric Woroshow - -//go:build darwin || windows - -package gl - -import ( - "runtime" -) - -// cStr takes a Go string (with or without null-termination) -// and returns the C counterpart. -// -// The returned free function must be called once you are done using the string -// in order to free the memory. -func cStr(str string) (cstr *byte, free func()) { - bs := []byte(str) - if len(bs) == 0 || bs[len(bs)-1] != 0 { - bs = append(bs, 0) - } - return &bs[0], func() { - runtime.KeepAlive(bs) - bs = nil - } -} diff --git a/internal/graphicsdriver/opengl/gl/default_notpurego.go b/internal/graphicsdriver/opengl/gl/default_notpurego.go index 7efbf3949..9c6741a1d 100644 --- a/internal/graphicsdriver/opengl/gl/default_notpurego.go +++ b/internal/graphicsdriver/opengl/gl/default_notpurego.go @@ -5,8 +5,6 @@ package gl -// #cgo linux,!nintendosdk freebsd,!nintendosdk openbsd,!nintendosdk pkg-config: gl -// // #ifndef APIENTRY // #define APIENTRY // #endif @@ -20,6 +18,7 @@ package gl // #endif // // #include +// #include // // typedef unsigned int GLenum; // typedef unsigned char GLboolean; @@ -416,8 +415,8 @@ func (c *defaultContext) AttachShader(program uint32, shader uint32) { } func (c *defaultContext) BindAttribLocation(program uint32, index uint32, name string) { - cname, free := cStr(name) - defer free() + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) C.glowBindAttribLocation(c.gpBindAttribLocation, (C.GLuint)(program), (C.GLuint)(index), (*C.GLchar)(unsafe.Pointer(cname))) } @@ -604,8 +603,8 @@ func (c *defaultContext) GetShaderiv(dst []int32, shader uint32, pname uint32) { } func (c *defaultContext) GetUniformLocation(program uint32, name string) int32 { - cname, free := cStr(name) - defer free() + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) ret := C.glowGetUniformLocation(c.gpGetUniformLocation, (C.GLuint)(program), (*C.GLchar)(unsafe.Pointer(cname))) return int32(ret) } @@ -651,8 +650,8 @@ func (c *defaultContext) Scissor(x int32, y int32, width int32, height int32) { } func (c *defaultContext) ShaderSource(shader uint32, xstring string) { - cstring, free := cStr(xstring) - defer free() + cstring := C.CString(xstring) + defer C.free(unsafe.Pointer(cstring)) C.glowShaderSource(c.gpShaderSource, (C.GLuint)(shader), 1, (**C.GLchar)(unsafe.Pointer(&cstring)), nil) } diff --git a/internal/graphicsdriver/opengl/gl/default_purego.go b/internal/graphicsdriver/opengl/gl/default_purego.go index 55c718ce8..1e08bb49f 100644 --- a/internal/graphicsdriver/opengl/gl/default_purego.go +++ b/internal/graphicsdriver/opengl/gl/default_purego.go @@ -679,3 +679,19 @@ func (c *defaultContext) Init() error { } return nil } + +// cStr takes a Go string (with or without null-termination) +// and returns the C counterpart. +// +// The returned free function must be called once you are done using the string +// in order to free the memory. +func cStr(str string) (cstr *byte, free func()) { + bs := []byte(str) + if len(bs) == 0 || bs[len(bs)-1] != 0 { + bs = append(bs, 0) + } + return &bs[0], func() { + runtime.KeepAlive(bs) + bs = nil + } +} diff --git a/internal/graphicsdriver/opengl/gl/procaddr_others.go b/internal/graphicsdriver/opengl/gl/procaddr_others.go index 1a2e9b2a9..da13b29e7 100644 --- a/internal/graphicsdriver/opengl/gl/procaddr_others.go +++ b/internal/graphicsdriver/opengl/gl/procaddr_others.go @@ -3,18 +3,6 @@ //go:build !android && !darwin && !js && !windows && !opengles -// This file implements GlowGetProcAddress for every supported platform. The -// correct version is chosen automatically based on build tags: -// -// darwin: CGL -// linux freebsd openbsd: GLX -// -// Use of EGL instead of the platform's default (listed above) is made possible -// via the "egl" build tag. -// -// It is also possible to install your own function outside this package for -// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc. - package gl /* @@ -24,18 +12,16 @@ package gl #cgo egl,!nintendosdk pkg-config: egl #cgo nintendosdk LDFLAGS: -Wl,-unresolved-symbols=ignore-all -// Check the EGL tag first as it takes priority over the platform's default -// configuration of WGL/GLX/CGL. #if defined(TAG_EGL) #include #include - static void* GlowGetProcAddress_gl21(const char* name) { + static void* getProcAddress(const char* name) { return eglGetProcAddress(name); } #elif defined(TAG_POSIX) #include #include - static void* GlowGetProcAddress_gl21(const char* name) { + static void* getProcAddress(const char* name) { return glXGetProcAddress((const GLubyte *) name); } #endif @@ -47,5 +33,5 @@ import "unsafe" func getProcAddress(namea string) unsafe.Pointer { cname := C.CString(namea) defer C.free(unsafe.Pointer(cname)) - return C.GlowGetProcAddress_gl21(cname) + return C.getProcAddress(cname) }