gls.go 18 KB

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