From 584c6524ee831824b646bd5d47f15e34c03d56c7 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 13 Jun 2021 14:19:33 +0900 Subject: [PATCH] internal/graphicsdriver/opengl: Refactring --- .../graphicsdriver/opengl/gl/conversions.go | 53 +++++++++++++++++++ .../opengl/gl/conversions_notwindows.go | 46 ---------------- .../opengl/gl/conversions_windows.go | 45 ---------------- 3 files changed, 53 insertions(+), 91 deletions(-) create mode 100644 internal/graphicsdriver/opengl/gl/conversions.go diff --git a/internal/graphicsdriver/opengl/gl/conversions.go b/internal/graphicsdriver/opengl/gl/conversions.go new file mode 100644 index 000000000..db5cb108b --- /dev/null +++ b/internal/graphicsdriver/opengl/gl/conversions.go @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: MIT + +package gl + +import ( + "fmt" + "reflect" + "strings" + "unsafe" +) + +// Ptr takes a slice or pointer (to a singular scalar value or the first +// element of an array or slice) and returns its GL-compatible address. +// +// For example: +// +// var data []uint8 +// ... +// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0])) +func Ptr(data interface{}) unsafe.Pointer { + if data == nil { + return unsafe.Pointer(nil) + } + var addr unsafe.Pointer + switch v := data.(type) { + case *uint8: + addr = unsafe.Pointer(v) + case *uint16: + addr = unsafe.Pointer(v) + case *float32: + addr = unsafe.Pointer(v) + case []uint8: + addr = unsafe.Pointer(&v[0]) + case []uint16: + addr = unsafe.Pointer(&v[0]) + case []float32: + addr = unsafe.Pointer(&v[0]) + default: + panic(fmt.Errorf("unsupported type %T; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v)) + } + return addr +} + +// Str takes a null-terminated Go string and returns its GL-compatible address. +// This function reaches into Go string storage in an unsafe way so the caller +// must ensure the string is not garbage collected. +func Str(str string) *uint8 { + if !strings.HasSuffix(str, "\x00") { + panic("str argument missing null terminator: " + str) + } + header := (*reflect.StringHeader)(unsafe.Pointer(&str)) + return (*uint8)(unsafe.Pointer(header.Data)) +} diff --git a/internal/graphicsdriver/opengl/gl/conversions_notwindows.go b/internal/graphicsdriver/opengl/gl/conversions_notwindows.go index 77f6dfe8a..9d436eadd 100644 --- a/internal/graphicsdriver/opengl/gl/conversions_notwindows.go +++ b/internal/graphicsdriver/opengl/gl/conversions_notwindows.go @@ -6,58 +6,12 @@ package gl import ( - "fmt" - "reflect" - "strings" "unsafe" ) // #include import "C" -// Ptr takes a slice or pointer (to a singular scalar value or the first -// element of an array or slice) and returns its GL-compatible address. -// -// For example: -// -// var data []uint8 -// ... -// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0])) -func Ptr(data interface{}) unsafe.Pointer { - if data == nil { - return unsafe.Pointer(nil) - } - var addr unsafe.Pointer - switch v := data.(type) { - case *uint8: - addr = unsafe.Pointer(v) - case *uint16: - addr = unsafe.Pointer(v) - case *float32: - addr = unsafe.Pointer(v) - case []uint8: - addr = unsafe.Pointer(&v[0]) - case []uint16: - addr = unsafe.Pointer(&v[0]) - case []float32: - addr = unsafe.Pointer(&v[0]) - default: - panic(fmt.Errorf("unsupported type %T; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v)) - } - return addr -} - -// Str takes a null-terminated Go string and returns its GL-compatible address. -// This function reaches into Go string storage in an unsafe way so the caller -// must ensure the string is not garbage collected. -func Str(str string) *uint8 { - if !strings.HasSuffix(str, "\x00") { - panic("str argument missing null terminator: " + str) - } - header := (*reflect.StringHeader)(unsafe.Pointer(&str)) - return (*uint8)(unsafe.Pointer(header.Data)) -} - // GoStr takes a null-terminated string returned by OpenGL and constructs a // corresponding Go string. func GoStr(cstr *uint8) string { diff --git a/internal/graphicsdriver/opengl/gl/conversions_windows.go b/internal/graphicsdriver/opengl/gl/conversions_windows.go index 3f8f29311..f8d96b30f 100644 --- a/internal/graphicsdriver/opengl/gl/conversions_windows.go +++ b/internal/graphicsdriver/opengl/gl/conversions_windows.go @@ -3,56 +3,11 @@ package gl import ( - "fmt" - "reflect" "runtime" "strings" "unsafe" ) -// Ptr takes a slice or pointer (to a singular scalar value or the first -// element of an array or slice) and returns its GL-compatible address. -// -// For example: -// -// var data []uint8 -// ... -// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0])) -func Ptr(data interface{}) unsafe.Pointer { - if data == nil { - return unsafe.Pointer(nil) - } - var addr unsafe.Pointer - switch v := data.(type) { - case *uint8: - addr = unsafe.Pointer(v) - case *uint16: - addr = unsafe.Pointer(v) - case *float32: - addr = unsafe.Pointer(v) - case []uint8: - addr = unsafe.Pointer(&v[0]) - case []uint16: - addr = unsafe.Pointer(&v[0]) - case []float32: - addr = unsafe.Pointer(&v[0]) - default: - panic(fmt.Errorf("unsupported type %T; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v)) - } - return addr -} - -// Str takes a null-terminated Go string and returns its GL-compatible address. -// This function reaches into Go string storage in an unsafe way so the caller -// must ensure the string is not garbage collected. -func Str(str string) *uint8 { - if !strings.HasSuffix(str, "\x00") { - panic("str argument missing null terminator: " + str) - } - header := (*reflect.StringHeader)(unsafe.Pointer(&str)) - return (*uint8)(unsafe.Pointer(header.Data)) -} - // GoStr takes a null-terminated string returned by OpenGL and constructs a // corresponding Go string. func GoStr(cstr *uint8) string {