From f4a1f90d9286123e1a0994aa7dc0ae4e4935e3e9 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 17 May 2020 14:28:13 +0900 Subject: [PATCH] graphicsdriver/opengl/gl: Reduce reflect usage --- .../opengl/gl/conversions_notwindows.go | 34 +++++++++---------- .../opengl/gl/conversions_windows.go | 34 +++++++++---------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/internal/graphicsdriver/opengl/gl/conversions_notwindows.go b/internal/graphicsdriver/opengl/gl/conversions_notwindows.go index 56703d56b..ba71aad63 100644 --- a/internal/graphicsdriver/opengl/gl/conversions_notwindows.go +++ b/internal/graphicsdriver/opengl/gl/conversions_notwindows.go @@ -28,25 +28,23 @@ func Ptr(data interface{}) unsafe.Pointer { return unsafe.Pointer(nil) } var addr unsafe.Pointer - v := reflect.ValueOf(data) - switch v.Type().Kind() { - case reflect.Ptr: - e := v.Elem() - switch e.Kind() { - case - reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, - reflect.Float32, reflect.Float64: - addr = unsafe.Pointer(e.UnsafeAddr()) - default: - panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind())) - } - case reflect.Uintptr: - addr = unsafe.Pointer(v.Pointer()) - case reflect.Slice: - addr = unsafe.Pointer(v.Index(0).UnsafeAddr()) + switch v := data.(type) { + case *uint8: + addr = unsafe.Pointer(v) + case *uint16: + addr = unsafe.Pointer(v) + case *float32: + addr = unsafe.Pointer(v) + case uintptr: + 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 %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type())) + 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 } diff --git a/internal/graphicsdriver/opengl/gl/conversions_windows.go b/internal/graphicsdriver/opengl/gl/conversions_windows.go index 2f5a6ae1e..3d90940f5 100644 --- a/internal/graphicsdriver/opengl/gl/conversions_windows.go +++ b/internal/graphicsdriver/opengl/gl/conversions_windows.go @@ -23,25 +23,23 @@ func Ptr(data interface{}) unsafe.Pointer { return unsafe.Pointer(nil) } var addr unsafe.Pointer - v := reflect.ValueOf(data) - switch v.Type().Kind() { - case reflect.Ptr: - e := v.Elem() - switch e.Kind() { - case - reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, - reflect.Float32, reflect.Float64: - addr = unsafe.Pointer(e.UnsafeAddr()) - default: - panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind())) - } - case reflect.Uintptr: - addr = unsafe.Pointer(v.Pointer()) - case reflect.Slice: - addr = unsafe.Pointer(v.Index(0).UnsafeAddr()) + switch v := data.(type) { + case *uint8: + addr = unsafe.Pointer(v) + case *uint16: + addr = unsafe.Pointer(v) + case *float32: + addr = unsafe.Pointer(v) + case uintptr: + 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 %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type())) + 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 }