gls.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gls
  5. // #include <stdlib.h>
  6. // #include "glcorearb.h"
  7. // #include "glapi.h"
  8. import "C"
  9. import (
  10. "fmt"
  11. "math"
  12. "reflect"
  13. "unsafe"
  14. )
  15. // GLS encapsulates the state of an OpenGL context which should be
  16. // associated with a single Window.
  17. type GLS struct {
  18. stats Stats // statistics
  19. Prog *Program // current active program
  20. programs map[*Program]bool // programs cache
  21. checkErrors bool // check openGL API errors flag
  22. viewportX int32 // cached last set viewport x
  23. viewportY int32 // cached last set viewport y
  24. viewportWidth int32 // cached last set viewport width
  25. viewportHeight int32 // cached last set viewport height
  26. lineWidth float32 // cached last set line width
  27. sideView int // cached last set triangle side view mode
  28. depthFunc uint32 // cached last set depth function
  29. depthMask int // cached last set depth mask
  30. capabilities map[int]int // cached capabilities (Enable/Disable)
  31. blendEquation uint32 // cached last set blend equation value
  32. blendSrc uint32 // cached last set blend src value
  33. blendDst uint32 // cached last set blend equation destination value
  34. blendEquationRGB uint32 // cached last set blend equation rgb value
  35. blendEquationAlpha uint32 // cached last set blend equation alpha value
  36. blendSrcRGB uint32 // cached last set blend src rgb
  37. blendSrcAlpha uint32 // cached last set blend src alpha value
  38. blendDstRGB uint32 // cached last set blend destination rgb value
  39. blendDstAlpha uint32 // cached last set blend destination alpha value
  40. polygonOffsetFactor float32 // cached last set polygon offset factor
  41. polygonOffsetUnits float32 // cached last set polygon offset units
  42. gobuf []byte // conversion buffer with GO memory
  43. cbuf []byte // conversion buffer with C memory
  44. }
  45. // Stats contains several counter
  46. type Stats struct {
  47. Vaos int // Number of Vertex Array Objects
  48. Vbos int // Number of Vertex Buffer Objects
  49. Textures int // Number of Textures
  50. Caphits uint64 // Cummulative number of hits for Enable/Disable
  51. Unisets uint64 // Cummulative number of uniform sets
  52. Drawcalls uint64 // Cummulative number of draw calls
  53. }
  54. // Polygon side view.
  55. const (
  56. FrontSide = iota + 1
  57. BackSide
  58. DoubleSide
  59. )
  60. const (
  61. capUndef = 0
  62. capDisabled = 1
  63. capEnabled = 2
  64. uintUndef = math.MaxUint32
  65. intFalse = 0
  66. intTrue = 1
  67. )
  68. // New creates and returns a new instance of an GLS object
  69. // which encapsulates the state of an OpenGL context
  70. // This should be called only after an active OpenGL context
  71. // was established, such as by creating a new window.
  72. func New() (*GLS, error) {
  73. gs := new(GLS)
  74. gs.reset()
  75. // Load OpenGL functions
  76. err := C.glapiLoad()
  77. if err != 0 {
  78. return nil, fmt.Errorf("Error loading OpenGL")
  79. }
  80. gs.SetDefaultState()
  81. gs.checkErrors = true
  82. // Preallocates conversion buffers
  83. size := 1 * 1024
  84. gs.gobuf = make([]byte, size)
  85. p := C.malloc(C.size_t(size))
  86. gs.cbuf = (*[1 << 30]byte)(unsafe.Pointer(p))[:size:size]
  87. return gs, nil
  88. }
  89. // SetCheckErrors enables/disables checking for errors after the
  90. // call of any OpenGL function. It is enabled by default but
  91. // could be disabled after an application is stable to improve the performance.
  92. func (gs *GLS) SetCheckErrors(enable bool) {
  93. if enable {
  94. C.glapiCheckError(1)
  95. } else {
  96. C.glapiCheckError(1)
  97. }
  98. gs.checkErrors = enable
  99. }
  100. // ChecksErrors returns if error checking is enabled or not.
  101. func (gs *GLS) CheckErrors() bool {
  102. return gs.checkErrors
  103. }
  104. // reset resets the internal state kept of the OpenGL
  105. func (gs *GLS) reset() {
  106. gs.lineWidth = 0.0
  107. gs.sideView = uintUndef
  108. gs.depthFunc = 0
  109. gs.depthMask = uintUndef
  110. gs.capabilities = make(map[int]int)
  111. gs.programs = make(map[*Program]bool)
  112. gs.Prog = nil
  113. gs.blendEquation = uintUndef
  114. gs.blendSrc = uintUndef
  115. gs.blendDst = uintUndef
  116. gs.blendEquationRGB = 0
  117. gs.blendEquationAlpha = 0
  118. gs.blendSrcRGB = uintUndef
  119. gs.blendSrcAlpha = uintUndef
  120. gs.blendDstRGB = uintUndef
  121. gs.blendDstAlpha = uintUndef
  122. gs.polygonOffsetFactor = -1
  123. gs.polygonOffsetUnits = -1
  124. }
  125. func (gs *GLS) SetDefaultState() {
  126. C.glClearColor(0, 0, 0, 1)
  127. C.glClearDepth(1)
  128. C.glClearStencil(0)
  129. gs.Enable(DEPTH_TEST)
  130. gs.DepthFunc(LEQUAL)
  131. gs.FrontFace(CCW)
  132. gs.CullFace(BACK)
  133. gs.Enable(CULL_FACE)
  134. gs.Enable(BLEND)
  135. gs.BlendEquation(FUNC_ADD)
  136. gs.BlendFunc(SRC_ALPHA, ONE_MINUS_SRC_ALPHA)
  137. gs.Enable(VERTEX_PROGRAM_POINT_SIZE)
  138. gs.Enable(PROGRAM_POINT_SIZE)
  139. gs.Enable(MULTISAMPLE)
  140. gs.Enable(POLYGON_OFFSET_FILL)
  141. gs.Enable(POLYGON_OFFSET_LINE)
  142. gs.Enable(POLYGON_OFFSET_POINT)
  143. }
  144. // Stats copy the current values of the internal statistics structure
  145. // to the specified pointer.
  146. func (gs *GLS) Stats(s *Stats) {
  147. *s = gs.stats
  148. }
  149. func (gs *GLS) ActiveTexture(texture uint32) {
  150. C.glActiveTexture(C.GLenum(texture))
  151. }
  152. func (gs *GLS) AttachShader(program, shader uint32) {
  153. C.glAttachShader(C.GLuint(program), C.GLuint(shader))
  154. }
  155. func (gs *GLS) BindBuffer(target int, vbo uint32) {
  156. C.glBindBuffer(C.GLenum(target), C.GLuint(vbo))
  157. }
  158. func (gs *GLS) BindTexture(target int, tex uint32) {
  159. C.glBindTexture(C.GLenum(target), C.GLuint(tex))
  160. }
  161. func (gs *GLS) BindVertexArray(vao uint32) {
  162. C.glBindVertexArray(C.GLuint(vao))
  163. }
  164. func (gs *GLS) BlendEquation(mode uint32) {
  165. if gs.blendEquation == mode {
  166. return
  167. }
  168. C.glBlendEquation(C.GLenum(mode))
  169. gs.blendEquation = mode
  170. }
  171. func (gs *GLS) BlendEquationSeparate(modeRGB uint32, modeAlpha uint32) {
  172. if gs.blendEquationRGB == modeRGB && gs.blendEquationAlpha == modeAlpha {
  173. return
  174. }
  175. C.glBlendEquationSeparate(C.GLenum(modeRGB), C.GLenum(modeAlpha))
  176. gs.blendEquationRGB = modeRGB
  177. gs.blendEquationAlpha = modeAlpha
  178. }
  179. func (gs *GLS) BlendFunc(sfactor, dfactor uint32) {
  180. if gs.blendSrc == sfactor && gs.blendDst == dfactor {
  181. return
  182. }
  183. C.glBlendFunc(C.GLenum(sfactor), C.GLenum(dfactor))
  184. gs.blendSrc = sfactor
  185. gs.blendDst = dfactor
  186. }
  187. func (gs *GLS) BlendFuncSeparate(srcRGB uint32, dstRGB uint32, srcAlpha uint32, dstAlpha uint32) {
  188. if gs.blendSrcRGB == srcRGB && gs.blendDstRGB == dstRGB &&
  189. gs.blendSrcAlpha == srcAlpha && gs.blendDstAlpha == dstAlpha {
  190. return
  191. }
  192. C.glBlendFuncSeparate(C.GLenum(srcRGB), C.GLenum(dstRGB), C.GLenum(srcAlpha), C.GLenum(dstAlpha))
  193. gs.blendSrcRGB = srcRGB
  194. gs.blendDstRGB = dstRGB
  195. gs.blendSrcAlpha = srcAlpha
  196. gs.blendDstAlpha = dstAlpha
  197. }
  198. func (gs *GLS) BufferData(target uint32, size int, data interface{}, usage uint32) {
  199. C.glBufferData(C.GLenum(target), C.GLsizeiptr(size), ptr(data), C.GLenum(usage))
  200. }
  201. func (gs *GLS) ClearColor(r, g, b, a float32) {
  202. C.glClearColor(C.GLfloat(r), C.GLfloat(g), C.GLfloat(b), C.GLfloat(a))
  203. }
  204. func (gs *GLS) Clear(mask uint) {
  205. C.glClear(C.GLbitfield(mask))
  206. }
  207. func (gs *GLS) CompileShader(shader uint32) {
  208. C.glCompileShader(C.GLuint(shader))
  209. }
  210. func (gs *GLS) CreateProgram() uint32 {
  211. p := C.glCreateProgram()
  212. return uint32(p)
  213. }
  214. func (gs *GLS) CreateShader(stype uint32) uint32 {
  215. h := C.glCreateShader(C.GLenum(stype))
  216. return uint32(h)
  217. }
  218. func (gs *GLS) DeleteBuffers(vbos ...uint32) {
  219. C.glDeleteBuffers(C.GLsizei(len(vbos)), (*C.GLuint)(&vbos[0]))
  220. }
  221. func (gs *GLS) DeleteShader(shader uint32) {
  222. C.glDeleteShader(C.GLuint(shader))
  223. }
  224. func (gs *GLS) DeleteProgram(program uint32) {
  225. C.glDeleteProgram(C.GLuint(program))
  226. }
  227. func (gs *GLS) DeleteTextures(tex ...uint32) {
  228. C.glDeleteTextures(C.GLsizei(len(tex)), (*C.GLuint)(&tex[0]))
  229. gs.stats.Textures -= len(tex)
  230. }
  231. func (gs *GLS) DeleteVertexArrays(vaos ...uint32) {
  232. C.glDeleteVertexArrays(C.GLsizei(len(vaos)), (*C.GLuint)(&vaos[0]))
  233. }
  234. func (gs *GLS) DepthFunc(mode uint32) {
  235. if gs.depthFunc == mode {
  236. return
  237. }
  238. C.glDepthFunc(C.GLenum(mode))
  239. gs.depthFunc = mode
  240. }
  241. func (gs *GLS) DepthMask(flag bool) {
  242. if gs.depthMask == intTrue && flag {
  243. return
  244. }
  245. if gs.depthMask == intFalse && !flag {
  246. return
  247. }
  248. C.glDepthMask(bool2c(flag))
  249. if flag {
  250. gs.depthMask = intTrue
  251. } else {
  252. gs.depthMask = intFalse
  253. }
  254. }
  255. func (gs *GLS) DrawArrays(mode uint32, first int32, count int32) {
  256. C.glDrawArrays(C.GLenum(mode), C.GLint(first), C.GLsizei(count))
  257. gs.stats.Drawcalls++
  258. }
  259. func (gs *GLS) DrawElements(mode uint32, count int32, itype uint32, start uint32) {
  260. C.glDrawElements(C.GLenum(mode), C.GLsizei(count), C.GLenum(itype), unsafe.Pointer(uintptr(start)))
  261. gs.stats.Drawcalls++
  262. }
  263. func (gs *GLS) Enable(cap int) {
  264. if gs.capabilities[cap] == capEnabled {
  265. gs.stats.Caphits++
  266. return
  267. }
  268. C.glEnable(C.GLenum(cap))
  269. gs.capabilities[cap] = capEnabled
  270. }
  271. func (gs *GLS) EnableVertexAttribArray(index uint32) {
  272. C.glEnableVertexAttribArray(C.GLuint(index))
  273. }
  274. func (gs *GLS) Disable(cap int) {
  275. if gs.capabilities[cap] == capDisabled {
  276. gs.stats.Caphits++
  277. return
  278. }
  279. C.glDisable(C.GLenum(cap))
  280. gs.capabilities[cap] = capDisabled
  281. }
  282. func (gs *GLS) CullFace(mode uint32) {
  283. C.glCullFace(C.GLenum(mode))
  284. }
  285. func (gs *GLS) FrontFace(mode uint32) {
  286. C.glFrontFace(C.GLenum(mode))
  287. }
  288. func (gs *GLS) GenBuffer() uint32 {
  289. var buf uint32
  290. C.glGenBuffers(1, (*C.GLuint)(&buf))
  291. gs.stats.Vbos++
  292. return buf
  293. }
  294. func (gs *GLS) GenerateMipmap(target uint32) {
  295. C.glGenerateMipmap(C.GLenum(target))
  296. }
  297. func (gs *GLS) GenTexture() uint32 {
  298. var tex uint32
  299. C.glGenTextures(1, (*C.GLuint)(&tex))
  300. gs.stats.Textures++
  301. return tex
  302. }
  303. func (gs *GLS) GenVertexArray() uint32 {
  304. var vao uint32
  305. C.glGenVertexArrays(1, (*C.GLuint)(&vao))
  306. gs.stats.Vaos++
  307. return vao
  308. }
  309. func (gs *GLS) GetAttribLocation(program uint32, name string) int32 {
  310. loc := C.glGetAttribLocation(C.GLuint(program), gs.gobufStr(name))
  311. return int32(loc)
  312. }
  313. func (gs *GLS) GetProgramiv(program, pname uint32, params *int32) {
  314. C.glGetProgramiv(C.GLuint(program), C.GLenum(pname), (*C.GLint)(params))
  315. }
  316. // GetProgramInfoLog returns the information log for the specified program object.
  317. func (gs *GLS) GetProgramInfoLog(program uint32) string {
  318. var length int32
  319. gs.GetProgramiv(program, INFO_LOG_LENGTH, &length)
  320. if length == 0 {
  321. return ""
  322. }
  323. C.glGetProgramInfoLog(C.GLuint(program), C.GLsizei(length), nil, gs.gobufSize(uint32(length)))
  324. return string(gs.gobuf[:length])
  325. }
  326. // GetShaderInfoLog returns the information log for the specified shader object.
  327. func (gs *GLS) GetShaderInfoLog(shader uint32) string {
  328. var length int32
  329. gs.GetShaderiv(shader, INFO_LOG_LENGTH, &length)
  330. if length == 0 {
  331. return ""
  332. }
  333. C.glGetShaderInfoLog(C.GLuint(shader), C.GLsizei(length), nil, gs.gobufSize(uint32(length)))
  334. return string(gs.gobuf[:length])
  335. }
  336. func (gs *GLS) GetString(name uint32) string {
  337. cs := C.glGetString(C.GLenum(name))
  338. return C.GoString((*C.char)(unsafe.Pointer(cs)))
  339. }
  340. // GetUniformLocation returns the location of a uniform variable for the specified program.
  341. func (gs *GLS) GetUniformLocation(program uint32, name string) int32 {
  342. loc := C.glGetUniformLocation(C.GLuint(program), gs.gobufStr(name))
  343. return int32(loc)
  344. }
  345. func (gs *GLS) GetViewport() (x, y, width, height int32) {
  346. return gs.viewportX, gs.viewportY, gs.viewportWidth, gs.viewportHeight
  347. }
  348. func (gs *GLS) LineWidth(width float32) {
  349. if gs.lineWidth == width {
  350. return
  351. }
  352. C.glLineWidth(C.GLfloat(width))
  353. gs.lineWidth = width
  354. }
  355. func (gs *GLS) LinkProgram(program uint32) {
  356. C.glLinkProgram(C.GLuint(program))
  357. }
  358. func (gs *GLS) SetDepthTest(enable bool) {
  359. if enable {
  360. gs.Enable(DEPTH_TEST)
  361. } else {
  362. gs.Disable(DEPTH_TEST)
  363. }
  364. }
  365. func (gs *GLS) SetSideView(mode int) {
  366. if gs.sideView == mode {
  367. return
  368. }
  369. switch mode {
  370. // Default: show only the front size
  371. case FrontSide:
  372. gs.Enable(CULL_FACE)
  373. C.glFrontFace(CCW)
  374. // Show only the back side
  375. case BackSide:
  376. gs.Enable(CULL_FACE)
  377. C.glFrontFace(CW)
  378. // Show both sides
  379. case DoubleSide:
  380. gs.Disable(CULL_FACE)
  381. default:
  382. panic("SetSideView() invalid mode")
  383. }
  384. gs.sideView = mode
  385. }
  386. func (gs *GLS) GetShaderiv(shader, pname uint32, params *int32) {
  387. C.glGetShaderiv(C.GLuint(shader), C.GLenum(pname), (*C.GLint)(params))
  388. }
  389. func (gs *GLS) ShaderSource(shader uint32, src string) {
  390. csource := gs.cbufStr(src)
  391. C.glShaderSource(C.GLuint(shader), 1, (**C.GLchar)(unsafe.Pointer(&csource)), nil)
  392. }
  393. func (gs *GLS) TexImage2D(target uint32, level int32, iformat int32, width int32, height int32, border int32, format uint32, itype uint32, data interface{}) {
  394. C.glTexImage2D(C.GLenum(target),
  395. C.GLint(level),
  396. C.GLint(iformat),
  397. C.GLsizei(width),
  398. C.GLsizei(height),
  399. C.GLint(border),
  400. C.GLenum(format),
  401. C.GLenum(itype),
  402. ptr(data))
  403. }
  404. func (gs *GLS) TexParameteri(target uint32, pname uint32, param int32) {
  405. C.glTexParameteri(C.GLenum(target), C.GLenum(pname), C.GLint(param))
  406. }
  407. func (gs *GLS) PolygonMode(face, mode int) {
  408. C.glPolygonMode(C.GLenum(face), C.GLenum(mode))
  409. }
  410. func (gs *GLS) PolygonOffset(factor float32, units float32) {
  411. if gs.polygonOffsetFactor == factor && gs.polygonOffsetUnits == units {
  412. return
  413. }
  414. C.glPolygonOffset(C.GLfloat(factor), C.GLfloat(units))
  415. gs.polygonOffsetFactor = factor
  416. gs.polygonOffsetUnits = units
  417. }
  418. func (gs *GLS) Uniform1i(location int32, v0 int32) {
  419. C.glUniform1i(C.GLint(location), C.GLint(v0))
  420. gs.stats.Unisets++
  421. }
  422. func (gs *GLS) Uniform1f(location int32, v0 float32) {
  423. C.glUniform1f(C.GLint(location), C.GLfloat(v0))
  424. gs.stats.Unisets++
  425. }
  426. func (gs *GLS) Uniform2f(location int32, v0, v1 float32) {
  427. C.glUniform2f(C.GLint(location), C.GLfloat(v0), C.GLfloat(v1))
  428. gs.stats.Unisets++
  429. }
  430. func (gs *GLS) Uniform3f(location int32, v0, v1, v2 float32) {
  431. C.glUniform3f(C.GLint(location), C.GLfloat(v0), C.GLfloat(v1), C.GLfloat(v2))
  432. gs.stats.Unisets++
  433. }
  434. func (gs *GLS) Uniform4f(location int32, v0, v1, v2, v3 float32) {
  435. C.glUniform4f(C.GLint(location), C.GLfloat(v0), C.GLfloat(v1), C.GLfloat(v2), C.GLfloat(v3))
  436. gs.stats.Unisets++
  437. }
  438. func (gs *GLS) UniformMatrix3fv(location int32, count int32, transpose bool, pm *float32) {
  439. C.glUniformMatrix3fv(C.GLint(location), C.GLsizei(count), bool2c(transpose), (*C.GLfloat)(pm))
  440. gs.stats.Unisets++
  441. }
  442. func (gs *GLS) UniformMatrix4fv(location int32, count int32, transpose bool, pm *float32) {
  443. C.glUniformMatrix4fv(C.GLint(location), C.GLsizei(count), bool2c(transpose), (*C.GLfloat)(pm))
  444. gs.stats.Unisets++
  445. }
  446. func (gs *GLS) Uniform1fv(location int32, count int32, v []float32) {
  447. C.glUniform1fv(C.GLint(location), C.GLsizei(count), (*C.GLfloat)(&v[0]))
  448. gs.stats.Unisets++
  449. }
  450. func (gs *GLS) Uniform2fv(location int32, count int32, v []float32) {
  451. C.glUniform2fv(C.GLint(location), C.GLsizei(count), (*C.GLfloat)(&v[0]))
  452. gs.stats.Unisets++
  453. }
  454. func (gs *GLS) Uniform3fv(location int32, count int32, v []float32) {
  455. C.glUniform3fv(C.GLint(location), C.GLsizei(count), (*C.GLfloat)(&v[0]))
  456. gs.stats.Unisets++
  457. }
  458. func (gs *GLS) Uniform4fv(location int32, count int32, v []float32) {
  459. C.glUniform4fv(C.GLint(location), C.GLsizei(count), (*C.GLfloat)(&v[0]))
  460. gs.stats.Unisets++
  461. }
  462. func (gs *GLS) VertexAttribPointer(index uint32, size int32, xtype uint32, normalized bool, stride int32, offset uint32) {
  463. C.glVertexAttribPointer(C.GLuint(index), C.GLint(size), C.GLenum(xtype), bool2c(normalized), C.GLsizei(stride), unsafe.Pointer(uintptr(offset)))
  464. }
  465. func (gs *GLS) Viewport(x, y, width, height int32) {
  466. C.glViewport(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height))
  467. gs.viewportX = x
  468. gs.viewportY = y
  469. gs.viewportWidth = width
  470. gs.viewportHeight = height
  471. }
  472. // Use set this program as the current program.
  473. func (gs *GLS) UseProgram(prog *Program) {
  474. if prog.handle == 0 {
  475. panic("Invalid program")
  476. }
  477. C.glUseProgram(C.GLuint(prog.handle))
  478. gs.Prog = prog
  479. // Inserts program in cache if not already there.
  480. if !gs.programs[prog] {
  481. gs.programs[prog] = true
  482. log.Debug("New Program activated. Total: %d", len(gs.programs))
  483. }
  484. }
  485. // Ptr takes a slice or pointer (to a singular scalar value or the first
  486. // element of an array or slice) and returns its GL-compatible address.
  487. //
  488. // For example:
  489. //
  490. // var data []uint8
  491. // ...
  492. // gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
  493. func ptr(data interface{}) unsafe.Pointer {
  494. if data == nil {
  495. return unsafe.Pointer(nil)
  496. }
  497. var addr unsafe.Pointer
  498. v := reflect.ValueOf(data)
  499. switch v.Type().Kind() {
  500. case reflect.Ptr:
  501. e := v.Elem()
  502. switch e.Kind() {
  503. case
  504. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  505. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  506. reflect.Float32, reflect.Float64:
  507. addr = unsafe.Pointer(e.UnsafeAddr())
  508. default:
  509. 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()))
  510. }
  511. case reflect.Uintptr:
  512. addr = unsafe.Pointer(v.Pointer())
  513. case reflect.Slice:
  514. addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
  515. default:
  516. 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()))
  517. }
  518. return addr
  519. }
  520. // bool2c convert a Go bool to C.GLboolean
  521. func bool2c(b bool) C.GLboolean {
  522. if b {
  523. return C.GLboolean(1)
  524. }
  525. return C.GLboolean(0)
  526. }
  527. // gobufSize returns a pointer to static buffer with the specified size not including the terminator.
  528. // If there is available space, there is no memory allocation.
  529. func (gs *GLS) gobufSize(size uint32) *C.GLchar {
  530. if size+1 > uint32(len(gs.gobuf)) {
  531. gs.gobuf = make([]byte, size+1)
  532. }
  533. return (*C.GLchar)(unsafe.Pointer(&gs.gobuf[0]))
  534. }
  535. // gobufStr converts a Go String to a C string by copying it to a static buffer
  536. // and returning a pointer to the start of the buffer.
  537. // If there is available space, there is no memory allocation.
  538. func (gs *GLS) gobufStr(s string) *C.GLchar {
  539. p := gs.gobufSize(uint32(len(s) + 1))
  540. copy(gs.gobuf, s)
  541. gs.gobuf[len(s)] = 0
  542. return p
  543. }
  544. // cbufSize returns a pointer to static buffer with C memory
  545. // If there is available space, there is no memory allocation.
  546. func (gs *GLS) cbufSize(size uint32) *C.GLchar {
  547. if size > uint32(len(gs.cbuf)) {
  548. if len(gs.cbuf) > 0 {
  549. C.free(unsafe.Pointer(&gs.cbuf[0]))
  550. }
  551. p := C.malloc(C.size_t(size))
  552. gs.cbuf = (*[1 << 30]byte)(unsafe.Pointer(p))[:size:size]
  553. }
  554. return (*C.GLchar)(unsafe.Pointer(&gs.cbuf[0]))
  555. }
  556. // cbufStr converts a Go String to a C string by copying it to a single pre-allocated buffer
  557. // using C memory and returning a pointer to the start of the buffer.
  558. // If there is available space, there is no memory allocation.
  559. func (gs *GLS) cbufStr(s string) *C.GLchar {
  560. p := gs.cbufSize(uint32(len(s) + 1))
  561. copy(gs.cbuf, s)
  562. gs.cbuf[len(s)] = 0
  563. return p
  564. }