graphicsdriver/opengl/gl: Reduce reflect usage

This commit is contained in:
Hajime Hoshi 2020-05-17 14:28:13 +09:00
parent 050b788843
commit f4a1f90d92
2 changed files with 32 additions and 36 deletions

View File

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

View File

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