From a1ad87a2622fbe654f0d1776407def03fd2f91ec Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 9 Nov 2022 16:07:30 +0900 Subject: [PATCH] internal/graphicsdriver/opengl/gl: refactoring --- internal/graphicsdriver/opengl/context_gl.go | 12 +++---- .../opengl/gl/conversions_cgo.go | 27 ++++----------- .../opengl/gl/conversions_nocgo.go | 33 ++++++------------- 3 files changed, 23 insertions(+), 49 deletions(-) diff --git a/internal/graphicsdriver/opengl/context_gl.go b/internal/graphicsdriver/opengl/context_gl.go index b23db99c3..2f415f49f 100644 --- a/internal/graphicsdriver/opengl/context_gl.go +++ b/internal/graphicsdriver/opengl/context_gl.go @@ -290,8 +290,8 @@ func (c *context) newShader(shaderType uint32, source string) (shader, error) { if s == 0 { return 0, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType) } - cSources, free := gl.Strs(source + "\x00") - gl.ShaderSource(uint32(s), 1, cSources, nil) + cSources, free := gl.CStr(source) + gl.ShaderSource(uint32(s), 1, &cSources, nil) free() gl.CompileShader(s) @@ -325,8 +325,8 @@ func (c *context) newProgram(shaders []shader, attributes []string) (program, er } for i, name := range attributes { - l, free := gl.Strs(name + "\x00") - gl.BindAttribLocation(p, uint32(i), *l) + l, free := gl.CStr(name) + gl.BindAttribLocation(p, uint32(i), l) free() } @@ -360,8 +360,8 @@ func (c *context) deleteProgram(p program) { } func (c *context) getUniformLocationImpl(p program, location string) uniformLocation { - l, free := gl.Strs(location + "\x00") - uniform := uniformLocation(gl.GetUniformLocation(uint32(p), *l)) + l, free := gl.CStr(location) + uniform := uniformLocation(gl.GetUniformLocation(uint32(p), l)) free() return uniform } diff --git a/internal/graphicsdriver/opengl/gl/conversions_cgo.go b/internal/graphicsdriver/opengl/gl/conversions_cgo.go index 87aa0375e..982b881ea 100644 --- a/internal/graphicsdriver/opengl/gl/conversions_cgo.go +++ b/internal/graphicsdriver/opengl/gl/conversions_cgo.go @@ -17,27 +17,14 @@ func GoStr(cstr *byte) string { return C.GoString((*C.char)(unsafe.Pointer(cstr))) } -// Strs takes a list of Go strings (with or without null-termination) and -// returns their C counterpart. +// 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 strings +// The returned free function must be called once you are done using the string // in order to free the memory. -// -// If no strings are provided as a parameter this function will panic. -func Strs(strs ...string) (cstrs **byte, free func()) { - if len(strs) == 0 { - panic("gl: expected at least 1 string at Strs") - } - - css := make([]*byte, 0, len(strs)) - for _, str := range strs { - cs := C.CString(str) - css = append(css, (*byte)(unsafe.Pointer(cs))) - } - - return (**byte)(&css[0]), func() { - for _, cs := range css { - C.free(unsafe.Pointer(cs)) - } +func CStr(str string) (cstr *byte, free func()) { + cs := C.CString(str) + return 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 index c878c8d90..5dcbe7b00 100644 --- a/internal/graphicsdriver/opengl/gl/conversions_nocgo.go +++ b/internal/graphicsdriver/opengl/gl/conversions_nocgo.go @@ -26,31 +26,18 @@ func GoStr(cstr *byte) string { return "" } -// Strs takes a list of Go strings (with or without null-termination) and -// returns their C counterpart. +// 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 strings +// The returned free function must be called once you are done using the string // in order to free the memory. -// -// If no strings are provided as a parameter this function will panic. -func Strs(strs ...string) (cstrs **byte, free func()) { - if len(strs) == 0 { - panic("gl: expected at least 1 string at Strs") +func CStr(str string) (cstr *byte, free func()) { + bs := []byte(str) + if len(bs) == 0 || bs[len(bs)-1] != 0 { + bs = append(bs, 0) } - - pinned := make([][]byte, 0, len(strs)) - ptrs := make([]*byte, 0, len(strs)) - for _, str := range strs { - bs := []byte(str) - if len(bs) == 0 || bs[len(bs)-1] != 0 { - bs = append(bs, 0) - } - pinned = append(pinned, bs) - ptrs = append(ptrs, &bs[0]) - } - - return &ptrs[0], func() { - runtime.KeepAlive(pinned) - pinned = nil + return &bs[0], func() { + runtime.KeepAlive(bs) + bs = nil } }