2021-06-13 07:19:33 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package gl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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:
|
|
|
|
//
|
2022-11-09 04:51:02 +01:00
|
|
|
// var data []byte
|
2022-08-03 13:48:02 +02:00
|
|
|
// ...
|
2022-10-24 16:22:14 +02:00
|
|
|
// gl.TexImage2D(glconst.TEXTURE_2D, ..., glconst.UNSIGNED_BYTE, gl.Ptr(&data[0]))
|
2022-11-03 07:33:09 +01:00
|
|
|
func Ptr(data any) unsafe.Pointer {
|
2021-06-13 07:19:33 +02:00
|
|
|
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])
|
2022-11-12 10:38:15 +01:00
|
|
|
case []uint32:
|
|
|
|
addr = unsafe.Pointer(&v[0])
|
2021-06-13 07:19:33 +02:00
|
|
|
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
|
|
|
|
}
|