conversions.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Code generated by glow (https://github.com/go-gl/glow). DO NOT EDIT.
  2. package gles2
  3. import (
  4. "fmt"
  5. "reflect"
  6. "strings"
  7. "unsafe"
  8. )
  9. // #include <stdlib.h>
  10. import "C"
  11. // Ptr takes a slice or pointer (to a singular scalar value or the first
  12. // element of an array or slice) and returns its GL-compatible address.
  13. //
  14. // For example:
  15. //
  16. // var data []uint8
  17. // ...
  18. // gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
  19. func Ptr(data interface{}) unsafe.Pointer {
  20. if data == nil {
  21. return unsafe.Pointer(nil)
  22. }
  23. var addr unsafe.Pointer
  24. v := reflect.ValueOf(data)
  25. switch v.Type().Kind() {
  26. case reflect.Ptr:
  27. e := v.Elem()
  28. switch e.Kind() {
  29. case
  30. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  31. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  32. reflect.Float32, reflect.Float64:
  33. addr = unsafe.Pointer(e.UnsafeAddr())
  34. default:
  35. 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()))
  36. }
  37. case reflect.Uintptr:
  38. addr = unsafe.Pointer(data.(uintptr))
  39. case reflect.Slice:
  40. addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
  41. default:
  42. 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()))
  43. }
  44. return addr
  45. }
  46. // PtrOffset takes a pointer offset and returns a GL-compatible pointer.
  47. // Originally intended for functions such as glVertexAttribPointer that take pointer
  48. // parameters also for offsets, since Go 1.14 this is no longer recommended.
  49. //
  50. // Use a corresponding offset-compatible variant of the function instead.
  51. // For example, for gl.VertexAttribPointer() there is gl.VertexAttribPointerWithOffset().
  52. //
  53. // See https://github.com/go-gl/gl#go-114-and-checkptr for more details on the checkptr detector.
  54. // See https://github.com/go-gl/glow#overloads, about adding new overloads.
  55. //
  56. // Deprecated: Use more appropriate overload function instead
  57. func PtrOffset(offset int) unsafe.Pointer {
  58. return unsafe.Pointer(uintptr(offset))
  59. }
  60. // Str takes a null-terminated Go string and returns its GL-compatible address.
  61. // This function reaches into Go string storage in an unsafe way so the caller
  62. // must ensure the string is not garbage collected.
  63. func Str(str string) *uint8 {
  64. if !strings.HasSuffix(str, "\x00") {
  65. panic("str argument missing null terminator: " + str)
  66. }
  67. header := (*reflect.StringHeader)(unsafe.Pointer(&str))
  68. return (*uint8)(unsafe.Pointer(header.Data))
  69. }
  70. // GoStr takes a null-terminated string returned by OpenGL and constructs a
  71. // corresponding Go string.
  72. func GoStr(cstr *uint8) string {
  73. return C.GoString((*C.char)(unsafe.Pointer(cstr)))
  74. }
  75. // Strs takes a list of Go strings (with or without null-termination) and
  76. // returns their C counterpart.
  77. //
  78. // The returned free function must be called once you are done using the strings
  79. // in order to free the memory.
  80. //
  81. // If no strings are provided as a parameter this function will panic.
  82. func Strs(strs ...string) (cstrs **uint8, free func()) {
  83. if len(strs) == 0 {
  84. panic("Strs: expected at least 1 string")
  85. }
  86. // Allocate a contiguous array large enough to hold all the strings' contents.
  87. n := 0
  88. for i := range strs {
  89. n += len(strs[i])
  90. }
  91. if n == 0 {
  92. n = 1 // avoid allocating zero bytes in case all strings are empty.
  93. }
  94. data := C.malloc(C.size_t(n))
  95. // Copy all the strings into data.
  96. dataSlice := (*[1 << 30]byte)(data)[:n]
  97. css := make([]*uint8, len(strs)) // Populated with pointers to each string.
  98. offset := 0
  99. for i := range strs {
  100. copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
  101. css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
  102. offset += len(strs[i])
  103. }
  104. return (**uint8)(&css[0]), func() { C.free(data) }
  105. }