gls.go 19 KB

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