internal/graphicsdriver/opengl/gl: refactoring

This commit is contained in:
Hajime Hoshi 2022-11-13 22:21:45 +09:00
parent 78f8ddbda8
commit 5b96d93d13
5 changed files with 26 additions and 76 deletions

View File

@ -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 <stdlib.h>
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))
}
}

View File

@ -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
}
}

View File

@ -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 <stdint.h>
// #include <stdlib.h>
//
// 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)
}

View File

@ -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
}
}

View File

@ -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 <stdlib.h>
#include <EGL/egl.h>
static void* GlowGetProcAddress_gl21(const char* name) {
static void* getProcAddress(const char* name) {
return eglGetProcAddress(name);
}
#elif defined(TAG_POSIX)
#include <stdlib.h>
#include <GL/glx.h>
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)
}