gls-desktop.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. // +build !wasm
  5. package gls
  6. // #include <stdlib.h>
  7. // #include "glcorearb.h"
  8. // #include "glapi.h"
  9. import "C"
  10. import (
  11. "fmt"
  12. "reflect"
  13. "unsafe"
  14. )
  15. // GLS encapsulates the state of an OpenGL context and contains
  16. // methods to call OpenGL functions.
  17. type GLS struct {
  18. stats Stats // statistics
  19. prog *Program // current active shader program
  20. programs map[*Program]bool // shader programs cache
  21. checkErrors bool // check openGL API errors flag
  22. // Cache OpenGL state to avoid making unnecessary API calls
  23. activeTexture uint32 // cached last set active texture unit
  24. viewportX int32 // cached last set viewport x
  25. viewportY int32 // cached last set viewport y
  26. viewportWidth int32 // cached last set viewport width
  27. viewportHeight int32 // cached last set viewport height
  28. lineWidth float32 // cached last set line width
  29. sideView int // cached last set triangle side view mode
  30. frontFace uint32 // cached last set glFrontFace value
  31. depthFunc uint32 // cached last set depth function
  32. depthMask int // cached last set depth mask
  33. //stencilFunc
  34. stencilMask uint32 // cached last set stencil mask
  35. capabilities map[int]int // cached capabilities (Enable/Disable)
  36. blendEquation uint32 // cached last set blend equation value
  37. blendSrc uint32 // cached last set blend src value
  38. blendDst uint32 // cached last set blend equation destination value
  39. blendEquationRGB uint32 // cached last set blend equation rgb value
  40. blendEquationAlpha uint32 // cached last set blend equation alpha value
  41. blendSrcRGB uint32 // cached last set blend src rgb
  42. blendSrcAlpha uint32 // cached last set blend src alpha value
  43. blendDstRGB uint32 // cached last set blend destination rgb value
  44. blendDstAlpha uint32 // cached last set blend destination alpha value
  45. polygonModeFace uint32 // cached last set polygon mode face
  46. polygonModeMode uint32 // cached last set polygon mode mode
  47. polygonOffsetFactor float32 // cached last set polygon offset factor
  48. polygonOffsetUnits float32 // cached last set polygon offset units
  49. gobuf []byte // conversion buffer with GO memory
  50. cbuf []byte // conversion buffer with C memory
  51. }
  52. // New creates and returns a new instance of a GLS object,
  53. // which encapsulates the state of an OpenGL context.
  54. // This should be called only after an active OpenGL context
  55. // is established, such as by creating a new window.
  56. func New() (*GLS, error) {
  57. gs := new(GLS)
  58. gs.reset()
  59. // Load OpenGL functions
  60. err := C.glapiLoad()
  61. if err != 0 {
  62. return nil, fmt.Errorf("Error loading OpenGL")
  63. }
  64. gs.setDefaultState()
  65. gs.checkErrors = true
  66. // Preallocate conversion buffers
  67. size := 1 * 1024
  68. gs.gobuf = make([]byte, size)
  69. p := C.malloc(C.size_t(size))
  70. gs.cbuf = (*[1 << 30]byte)(unsafe.Pointer(p))[:size:size]
  71. return gs, nil
  72. }
  73. // SetCheckErrors enables/disables checking for errors after the
  74. // call of any OpenGL function. It is enabled by default but
  75. // could be disabled after an application is stable to improve the performance.
  76. func (gs *GLS) SetCheckErrors(enable bool) {
  77. if enable {
  78. C.glapiCheckError(1)
  79. } else {
  80. C.glapiCheckError(0)
  81. }
  82. gs.checkErrors = enable
  83. }
  84. // CheckErrors returns if error checking is enabled or not.
  85. func (gs *GLS) CheckErrors() bool {
  86. return gs.checkErrors
  87. }
  88. // reset resets the internal state kept of the OpenGL
  89. func (gs *GLS) reset() {
  90. gs.lineWidth = 0.0
  91. gs.sideView = uintUndef
  92. gs.frontFace = 0
  93. gs.depthFunc = 0
  94. gs.depthMask = uintUndef
  95. gs.capabilities = make(map[int]int)
  96. gs.programs = make(map[*Program]bool)
  97. gs.prog = nil
  98. gs.activeTexture = uintUndef
  99. gs.blendEquation = uintUndef
  100. gs.blendSrc = uintUndef
  101. gs.blendDst = uintUndef
  102. gs.blendEquationRGB = 0
  103. gs.blendEquationAlpha = 0
  104. gs.blendSrcRGB = uintUndef
  105. gs.blendSrcAlpha = uintUndef
  106. gs.blendDstRGB = uintUndef
  107. gs.blendDstAlpha = uintUndef
  108. gs.polygonModeFace = 0
  109. gs.polygonModeMode = 0
  110. gs.polygonOffsetFactor = -1
  111. gs.polygonOffsetUnits = -1
  112. }
  113. // setDefaultState is used internally to set the initial state of OpenGL
  114. // for this context.
  115. func (gs *GLS) setDefaultState() {
  116. gs.ClearColor(0, 0, 0, 1)
  117. gs.ClearDepth(1)
  118. gs.ClearStencil(0)
  119. gs.Enable(DEPTH_TEST)
  120. //gs.DepthMask(true)
  121. gs.DepthFunc(LEQUAL)
  122. gs.FrontFace(CCW)
  123. gs.CullFace(BACK)
  124. gs.Enable(CULL_FACE)
  125. gs.Enable(BLEND)
  126. gs.BlendEquation(FUNC_ADD)
  127. gs.BlendFunc(SRC_ALPHA, ONE_MINUS_SRC_ALPHA)
  128. gs.Enable(VERTEX_PROGRAM_POINT_SIZE)
  129. gs.Enable(PROGRAM_POINT_SIZE)
  130. gs.Enable(MULTISAMPLE)
  131. gs.Enable(POLYGON_OFFSET_FILL)
  132. gs.Enable(POLYGON_OFFSET_LINE)
  133. gs.Enable(POLYGON_OFFSET_POINT)
  134. }
  135. // Stats copy the current values of the internal statistics structure
  136. // to the specified pointer.
  137. func (gs *GLS) Stats(s *Stats) {
  138. *s = gs.stats
  139. s.Shaders = len(gs.programs)
  140. }
  141. // ActiveTexture selects which texture unit subsequent texture state calls
  142. // will affect. The number of texture units an implementation supports is
  143. // implementation dependent, but must be at least 48 in GL 3.3.
  144. func (gs *GLS) ActiveTexture(texture uint32) {
  145. if gs.activeTexture == texture {
  146. return
  147. }
  148. C.glActiveTexture(C.GLenum(texture))
  149. gs.activeTexture = texture
  150. }
  151. // AttachShader attaches the specified shader object to the specified program object.
  152. func (gs *GLS) AttachShader(program, shader uint32) {
  153. C.glAttachShader(C.GLuint(program), C.GLuint(shader))
  154. }
  155. // BindBuffer binds a buffer object to the specified buffer binding point.
  156. func (gs *GLS) BindBuffer(target int, vbo uint32) {
  157. C.glBindBuffer(C.GLenum(target), C.GLuint(vbo))
  158. }
  159. // BindTexture lets you create or use a named texture.
  160. func (gs *GLS) BindTexture(target int, tex uint32) {
  161. C.glBindTexture(C.GLenum(target), C.GLuint(tex))
  162. }
  163. // BindVertexArray binds the vertex array object.
  164. func (gs *GLS) BindVertexArray(vao uint32) {
  165. C.glBindVertexArray(C.GLuint(vao))
  166. }
  167. // BlendEquation sets the blend equations for all draw buffers.
  168. func (gs *GLS) BlendEquation(mode uint32) {
  169. if gs.blendEquation == mode {
  170. return
  171. }
  172. C.glBlendEquation(C.GLenum(mode))
  173. gs.blendEquation = mode
  174. }
  175. // BlendEquationSeparate sets the blend equations for all draw buffers
  176. // allowing different equations for the RGB and alpha components.
  177. func (gs *GLS) BlendEquationSeparate(modeRGB uint32, modeAlpha uint32) {
  178. if gs.blendEquationRGB == modeRGB && gs.blendEquationAlpha == modeAlpha {
  179. return
  180. }
  181. C.glBlendEquationSeparate(C.GLenum(modeRGB), C.GLenum(modeAlpha))
  182. gs.blendEquationRGB = modeRGB
  183. gs.blendEquationAlpha = modeAlpha
  184. }
  185. // BlendFunc defines the operation of blending for
  186. // all draw buffers when blending is enabled.
  187. func (gs *GLS) BlendFunc(sfactor, dfactor uint32) {
  188. if gs.blendSrc == sfactor && gs.blendDst == dfactor {
  189. return
  190. }
  191. C.glBlendFunc(C.GLenum(sfactor), C.GLenum(dfactor))
  192. gs.blendSrc = sfactor
  193. gs.blendDst = dfactor
  194. }
  195. // BlendFuncSeparate defines the operation of blending for all draw buffers when blending
  196. // is enabled, allowing different operations for the RGB and alpha components.
  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. // BufferData creates a new data store for the buffer object currently
  209. // bound to target, deleting any pre-existing data store.
  210. func (gs *GLS) BufferData(target uint32, size int, data interface{}, usage uint32) {
  211. C.glBufferData(C.GLenum(target), C.GLsizeiptr(size), ptr(data), C.GLenum(usage))
  212. }
  213. // ClearColor specifies the red, green, blue, and alpha values
  214. // used by glClear to clear the color buffers.
  215. func (gs *GLS) ClearColor(r, g, b, a float32) {
  216. C.glClearColor(C.GLfloat(r), C.GLfloat(g), C.GLfloat(b), C.GLfloat(a))
  217. }
  218. // ClearDepth specifies the depth value used by Clear to clear the depth buffer.
  219. func (gs *GLS) ClearDepth(v float32) {
  220. C.glClearDepth(C.GLclampd(v))
  221. }
  222. // ClearStencil specifies the index used by Clear to clear the stencil buffer.
  223. func (gs *GLS) ClearStencil(v int32) {
  224. C.glClearStencil(C.GLint(v))
  225. }
  226. // Clear sets the bitplane area of the window to values previously
  227. // selected by ClearColor, ClearDepth, and ClearStencil.
  228. func (gs *GLS) Clear(mask uint) {
  229. C.glClear(C.GLbitfield(mask))
  230. }
  231. // CompileShader compiles the source code strings that
  232. // have been stored in the specified shader object.
  233. func (gs *GLS) CompileShader(shader uint32) {
  234. C.glCompileShader(C.GLuint(shader))
  235. }
  236. // CreateProgram creates an empty program object and returns
  237. // a non-zero value by which it can be referenced.
  238. func (gs *GLS) CreateProgram() uint32 {
  239. p := C.glCreateProgram()
  240. return uint32(p)
  241. }
  242. // CreateShader creates an empty shader object and returns
  243. // a non-zero value by which it can be referenced.
  244. func (gs *GLS) CreateShader(stype uint32) uint32 {
  245. h := C.glCreateShader(C.GLenum(stype))
  246. return uint32(h)
  247. }
  248. // DeleteBuffers deletes n​buffer objects named
  249. // by the elements of the provided array.
  250. func (gs *GLS) DeleteBuffers(bufs ...uint32) {
  251. C.glDeleteBuffers(C.GLsizei(len(bufs)), (*C.GLuint)(&bufs[0]))
  252. gs.stats.Buffers -= len(bufs)
  253. }
  254. // DeleteShader frees the memory and invalidates the name
  255. // associated with the specified shader object.
  256. func (gs *GLS) DeleteShader(shader uint32) {
  257. C.glDeleteShader(C.GLuint(shader))
  258. }
  259. // DeleteProgram frees the memory and invalidates the name
  260. // associated with the specified program object.
  261. func (gs *GLS) DeleteProgram(program uint32) {
  262. C.glDeleteProgram(C.GLuint(program))
  263. }
  264. // DeleteTextures deletes n​textures named
  265. // by the elements of the provided array.
  266. func (gs *GLS) DeleteTextures(tex ...uint32) {
  267. C.glDeleteTextures(C.GLsizei(len(tex)), (*C.GLuint)(&tex[0]))
  268. gs.stats.Textures -= len(tex)
  269. }
  270. // DeleteVertexArrays deletes n​vertex array objects named
  271. // by the elements of the provided array.
  272. func (gs *GLS) DeleteVertexArrays(vaos ...uint32) {
  273. C.glDeleteVertexArrays(C.GLsizei(len(vaos)), (*C.GLuint)(&vaos[0]))
  274. gs.stats.Vaos -= len(vaos)
  275. }
  276. // ReadPixels returns the current rendered image.
  277. // x, y: specifies the window coordinates of the first pixel that is read from the frame buffer.
  278. // width, height: specifies the dimensions of the pixel rectangle.
  279. // format: specifies the format of the pixel data.
  280. // format_type: specifies the data type of the pixel data.
  281. // more information: http://docs.gl/gl3/glReadPixels
  282. func (gs *GLS) ReadPixels(x, y, width, height, format, formatType int) []byte {
  283. size := uint32((width - x) * (height - y) * 4)
  284. C.glReadPixels(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height), C.GLenum(format), C.GLenum(formatType), unsafe.Pointer(gs.gobufSize(size)))
  285. return gs.gobuf[:size]
  286. }
  287. // DepthFunc specifies the function used to compare each incoming pixel
  288. // depth value with the depth value present in the depth buffer.
  289. func (gs *GLS) DepthFunc(mode uint32) {
  290. if gs.depthFunc == mode {
  291. return
  292. }
  293. C.glDepthFunc(C.GLenum(mode))
  294. gs.depthFunc = mode
  295. }
  296. // DepthMask enables or disables writing into the depth buffer.
  297. func (gs *GLS) DepthMask(flag bool) {
  298. if gs.depthMask == intTrue && flag {
  299. return
  300. }
  301. if gs.depthMask == intFalse && !flag {
  302. return
  303. }
  304. C.glDepthMask(bool2c(flag))
  305. if flag {
  306. gs.depthMask = intTrue
  307. } else {
  308. gs.depthMask = intFalse
  309. }
  310. }
  311. func (gs *GLS) StencilOp(fail, zfail, zpass uint32) {
  312. // TODO save state
  313. C.glStencilOp(C.GLenum(fail), C.GLenum(zfail), C.GLenum(zpass))
  314. }
  315. func (gs *GLS) StencilFunc(mode uint32, ref int32, mask uint32) {
  316. // TODO save state
  317. C.glStencilFunc(C.GLenum(mode), C.GLint(ref), C.GLuint(mask))
  318. }
  319. // TODO doc
  320. // StencilMask enables or disables writing into the stencil buffer.
  321. func (gs *GLS) StencilMask(mask uint32) {
  322. if gs.stencilMask == mask {
  323. return
  324. }
  325. C.glStencilMask(C.GLuint(mask))
  326. gs.stencilMask = mask
  327. }
  328. // DrawArrays renders primitives from array data.
  329. func (gs *GLS) DrawArrays(mode uint32, first int32, count int32) {
  330. C.glDrawArrays(C.GLenum(mode), C.GLint(first), C.GLsizei(count))
  331. gs.stats.Drawcalls++
  332. }
  333. // DrawElements renders primitives from array data.
  334. func (gs *GLS) DrawElements(mode uint32, count int32, itype uint32, start uint32) {
  335. C.glDrawElements(C.GLenum(mode), C.GLsizei(count), C.GLenum(itype), unsafe.Pointer(uintptr(start)))
  336. gs.stats.Drawcalls++
  337. }
  338. // Enable enables the specified capability.
  339. func (gs *GLS) Enable(cap int) {
  340. if gs.capabilities[cap] == capEnabled {
  341. gs.stats.Caphits++
  342. return
  343. }
  344. C.glEnable(C.GLenum(cap))
  345. gs.capabilities[cap] = capEnabled
  346. }
  347. // Disable disables the specified capability.
  348. func (gs *GLS) Disable(cap int) {
  349. if gs.capabilities[cap] == capDisabled {
  350. gs.stats.Caphits++
  351. return
  352. }
  353. C.glDisable(C.GLenum(cap))
  354. gs.capabilities[cap] = capDisabled
  355. }
  356. // EnableVertexAttribArray enables a generic vertex attribute array.
  357. func (gs *GLS) EnableVertexAttribArray(index uint32) {
  358. C.glEnableVertexAttribArray(C.GLuint(index))
  359. }
  360. // CullFace specifies whether front- or back-facing facets can be culled.
  361. func (gs *GLS) CullFace(mode uint32) {
  362. C.glCullFace(C.GLenum(mode))
  363. }
  364. // FrontFace defines front- and back-facing polygons.
  365. func (gs *GLS) FrontFace(mode uint32) {
  366. if gs.frontFace == mode {
  367. return
  368. }
  369. C.glFrontFace(C.GLenum(mode))
  370. gs.frontFace = mode
  371. }
  372. // GenBuffer generates a ​buffer object name.
  373. func (gs *GLS) GenBuffer() uint32 {
  374. var buf uint32
  375. C.glGenBuffers(1, (*C.GLuint)(&buf))
  376. gs.stats.Buffers++
  377. return buf
  378. }
  379. // GenerateMipmap generates mipmaps for the specified texture target.
  380. func (gs *GLS) GenerateMipmap(target uint32) {
  381. C.glGenerateMipmap(C.GLenum(target))
  382. }
  383. // GenTexture generates a texture object name.
  384. func (gs *GLS) GenTexture() uint32 {
  385. var tex uint32
  386. C.glGenTextures(1, (*C.GLuint)(&tex))
  387. gs.stats.Textures++
  388. return tex
  389. }
  390. // GenVertexArray generates a vertex array object name.
  391. func (gs *GLS) GenVertexArray() uint32 {
  392. var vao uint32
  393. C.glGenVertexArrays(1, (*C.GLuint)(&vao))
  394. gs.stats.Vaos++
  395. return vao
  396. }
  397. // GetAttribLocation returns the location of the specified attribute variable.
  398. func (gs *GLS) GetAttribLocation(program uint32, name string) int32 {
  399. loc := C.glGetAttribLocation(C.GLuint(program), gs.gobufStr(name))
  400. return int32(loc)
  401. }
  402. // GetProgramiv returns the specified parameter from the specified program object.
  403. func (gs *GLS) GetProgramiv(program, pname uint32, params *int32) {
  404. C.glGetProgramiv(C.GLuint(program), C.GLenum(pname), (*C.GLint)(params))
  405. }
  406. // GetProgramInfoLog returns the information log for the specified program object.
  407. func (gs *GLS) GetProgramInfoLog(program uint32) string {
  408. var length int32
  409. gs.GetProgramiv(program, INFO_LOG_LENGTH, &length)
  410. if length == 0 {
  411. return ""
  412. }
  413. C.glGetProgramInfoLog(C.GLuint(program), C.GLsizei(length), nil, gs.gobufSize(uint32(length)))
  414. return string(gs.gobuf[:length])
  415. }
  416. // GetShaderInfoLog returns the information log for the specified shader object.
  417. func (gs *GLS) GetShaderInfoLog(shader uint32) string {
  418. var length int32
  419. gs.GetShaderiv(shader, INFO_LOG_LENGTH, &length)
  420. if length == 0 {
  421. return ""
  422. }
  423. C.glGetShaderInfoLog(C.GLuint(shader), C.GLsizei(length), nil, gs.gobufSize(uint32(length)))
  424. return string(gs.gobuf[:length])
  425. }
  426. // GetString returns a string describing the specified aspect of the current GL connection.
  427. func (gs *GLS) GetString(name uint32) string {
  428. cs := C.glGetString(C.GLenum(name))
  429. return C.GoString((*C.char)(unsafe.Pointer(cs)))
  430. }
  431. // GetUniformLocation returns the location of a uniform variable for the specified program.
  432. func (gs *GLS) GetUniformLocation(program uint32, name string) int32 {
  433. loc := C.glGetUniformLocation(C.GLuint(program), gs.gobufStr(name))
  434. return int32(loc)
  435. }
  436. // GetViewport returns the current viewport information.
  437. func (gs *GLS) GetViewport() (x, y, width, height int32) {
  438. return gs.viewportX, gs.viewportY, gs.viewportWidth, gs.viewportHeight
  439. }
  440. // LineWidth specifies the rasterized width of both aliased and antialiased lines.
  441. func (gs *GLS) LineWidth(width float32) {
  442. if gs.lineWidth == width {
  443. return
  444. }
  445. C.glLineWidth(C.GLfloat(width))
  446. gs.lineWidth = width
  447. }
  448. // LinkProgram links the specified program object.
  449. func (gs *GLS) LinkProgram(program uint32) {
  450. C.glLinkProgram(C.GLuint(program))
  451. }
  452. // GetShaderiv returns the specified parameter from the specified shader object.
  453. func (gs *GLS) GetShaderiv(shader, pname uint32, params *int32) {
  454. C.glGetShaderiv(C.GLuint(shader), C.GLenum(pname), (*C.GLint)(params))
  455. }
  456. // Scissor defines the scissor box rectangle in window coordinates.
  457. func (gs *GLS) Scissor(x, y int32, width, height uint32) {
  458. C.glScissor(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height))
  459. }
  460. // ShaderSource sets the source code for the specified shader object.
  461. func (gs *GLS) ShaderSource(shader uint32, src string) {
  462. csource := gs.cbufStr(src)
  463. C.glShaderSource(C.GLuint(shader), 1, (**C.GLchar)(unsafe.Pointer(&csource)), nil)
  464. }
  465. // TexImage2D specifies a two-dimensional texture image.
  466. func (gs *GLS) TexImage2D(target uint32, level int32, iformat int32, width int32, height int32, format uint32, itype uint32, data interface{}) {
  467. C.glTexImage2D(C.GLenum(target),
  468. C.GLint(level),
  469. C.GLint(iformat),
  470. C.GLsizei(width),
  471. C.GLsizei(height),
  472. C.GLint(0),
  473. C.GLenum(format),
  474. C.GLenum(itype),
  475. ptr(data))
  476. }
  477. // CompressedTexImage2D specifies a two-dimensional compressed texture image.
  478. func (gs *GLS) CompressedTexImage2D(target uint32, level uint32, iformat uint32, width int32, height int32, size int32, data interface{}) {
  479. C.glCompressedTexImage2D(C.GLenum(target),
  480. C.GLint(level),
  481. C.GLenum(iformat),
  482. C.GLsizei(width),
  483. C.GLsizei(height),
  484. C.GLint(0),
  485. C.GLsizei(size),
  486. ptr(data))
  487. }
  488. // TexParameteri sets the specified texture parameter on the specified texture.
  489. func (gs *GLS) TexParameteri(target uint32, pname uint32, param int32) {
  490. C.glTexParameteri(C.GLenum(target), C.GLenum(pname), C.GLint(param))
  491. }
  492. // PolygonMode controls the interpretation of polygons for rasterization.
  493. func (gs *GLS) PolygonMode(face, mode uint32) {
  494. if gs.polygonModeFace == face && gs.polygonModeMode == mode {
  495. return
  496. }
  497. C.glPolygonMode(C.GLenum(face), C.GLenum(mode))
  498. gs.polygonModeFace = face
  499. gs.polygonModeMode = mode
  500. }
  501. // PolygonOffset sets the scale and units used to calculate depth values.
  502. func (gs *GLS) PolygonOffset(factor float32, units float32) {
  503. if gs.polygonOffsetFactor == factor && gs.polygonOffsetUnits == units {
  504. return
  505. }
  506. C.glPolygonOffset(C.GLfloat(factor), C.GLfloat(units))
  507. gs.polygonOffsetFactor = factor
  508. gs.polygonOffsetUnits = units
  509. }
  510. // Uniform1i sets the value of an int uniform variable for the current program object.
  511. func (gs *GLS) Uniform1i(location int32, v0 int32) {
  512. C.glUniform1i(C.GLint(location), C.GLint(v0))
  513. gs.stats.Unisets++
  514. }
  515. // Uniform1f sets the value of a float uniform variable for the current program object.
  516. func (gs *GLS) Uniform1f(location int32, v0 float32) {
  517. C.glUniform1f(C.GLint(location), C.GLfloat(v0))
  518. gs.stats.Unisets++
  519. }
  520. // Uniform2f sets the value of a vec2 uniform variable for the current program object.
  521. func (gs *GLS) Uniform2f(location int32, v0, v1 float32) {
  522. C.glUniform2f(C.GLint(location), C.GLfloat(v0), C.GLfloat(v1))
  523. gs.stats.Unisets++
  524. }
  525. // Uniform3f sets the value of a vec3 uniform variable for the current program object.
  526. func (gs *GLS) Uniform3f(location int32, v0, v1, v2 float32) {
  527. C.glUniform3f(C.GLint(location), C.GLfloat(v0), C.GLfloat(v1), C.GLfloat(v2))
  528. gs.stats.Unisets++
  529. }
  530. // Uniform4f sets the value of a vec4 uniform variable for the current program object.
  531. func (gs *GLS) Uniform4f(location int32, v0, v1, v2, v3 float32) {
  532. C.glUniform4f(C.GLint(location), C.GLfloat(v0), C.GLfloat(v1), C.GLfloat(v2), C.GLfloat(v3))
  533. gs.stats.Unisets++
  534. }
  535. // UniformMatrix3fv sets the value of one or many 3x3 float matrices for the current program object.
  536. func (gs *GLS) UniformMatrix3fv(location int32, count int32, transpose bool, pm *float32) {
  537. C.glUniformMatrix3fv(C.GLint(location), C.GLsizei(count), bool2c(transpose), (*C.GLfloat)(pm))
  538. gs.stats.Unisets++
  539. }
  540. // UniformMatrix4fv sets the value of one or many 4x4 float matrices for the current program object.
  541. func (gs *GLS) UniformMatrix4fv(location int32, count int32, transpose bool, pm *float32) {
  542. C.glUniformMatrix4fv(C.GLint(location), C.GLsizei(count), bool2c(transpose), (*C.GLfloat)(pm))
  543. gs.stats.Unisets++
  544. }
  545. // Uniform1fv sets the value of one or many float uniform variables for the current program object.
  546. func (gs *GLS) Uniform1fv(location int32, count int32, v *float32) {
  547. C.glUniform1fv(C.GLint(location), C.GLsizei(count), (*C.GLfloat)(v))
  548. gs.stats.Unisets++
  549. }
  550. // Uniform2fv sets the value of one or many vec2 uniform variables for the current program object.
  551. func (gs *GLS) Uniform2fv(location int32, count int32, v *float32) {
  552. C.glUniform2fv(C.GLint(location), C.GLsizei(count), (*C.GLfloat)(v))
  553. gs.stats.Unisets++
  554. }
  555. // Uniform3fv sets the value of one or many vec3 uniform variables for the current program object.
  556. func (gs *GLS) Uniform3fv(location int32, count int32, v *float32) {
  557. C.glUniform3fv(C.GLint(location), C.GLsizei(count), (*C.GLfloat)(v))
  558. gs.stats.Unisets++
  559. }
  560. // Uniform4fv sets the value of one or many vec4 uniform variables for the current program object.
  561. func (gs *GLS) Uniform4fv(location int32, count int32, v *float32) {
  562. C.glUniform4fv(C.GLint(location), C.GLsizei(count), (*C.GLfloat)(v))
  563. gs.stats.Unisets++
  564. }
  565. // VertexAttribPointer defines an array of generic vertex attribute data.
  566. func (gs *GLS) VertexAttribPointer(index uint32, size int32, xtype uint32, normalized bool, stride int32, offset uint32) {
  567. C.glVertexAttribPointer(C.GLuint(index), C.GLint(size), C.GLenum(xtype), bool2c(normalized), C.GLsizei(stride), unsafe.Pointer(uintptr(offset)))
  568. }
  569. // Viewport sets the viewport.
  570. func (gs *GLS) Viewport(x, y, width, height int32) {
  571. C.glViewport(C.GLint(x), C.GLint(y), C.GLsizei(width), C.GLsizei(height))
  572. gs.viewportX = x
  573. gs.viewportY = y
  574. gs.viewportWidth = width
  575. gs.viewportHeight = height
  576. }
  577. // UseProgram sets the specified program as the current program.
  578. func (gs *GLS) UseProgram(prog *Program) {
  579. if prog.handle == 0 {
  580. panic("Invalid program")
  581. }
  582. C.glUseProgram(C.GLuint(prog.handle))
  583. gs.prog = prog
  584. // Inserts program in cache if not already there.
  585. if !gs.programs[prog] {
  586. gs.programs[prog] = true
  587. log.Debug("New Program activated. Total: %d", len(gs.programs))
  588. }
  589. }
  590. // Ptr takes a slice or pointer (to a singular scalar value or the first
  591. // element of an array or slice) and returns its GL-compatible address.
  592. //
  593. // For example:
  594. //
  595. // var data []uint8
  596. // ...
  597. // gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
  598. func ptr(data interface{}) unsafe.Pointer {
  599. if data == nil {
  600. return unsafe.Pointer(nil)
  601. }
  602. var addr unsafe.Pointer
  603. v := reflect.ValueOf(data)
  604. switch v.Type().Kind() {
  605. case reflect.Ptr:
  606. e := v.Elem()
  607. switch e.Kind() {
  608. case
  609. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  610. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  611. reflect.Float32, reflect.Float64:
  612. addr = unsafe.Pointer(e.UnsafeAddr())
  613. default:
  614. 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()))
  615. }
  616. case reflect.Uintptr:
  617. addr = unsafe.Pointer(v.Pointer())
  618. case reflect.Slice:
  619. addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
  620. default:
  621. 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()))
  622. }
  623. return addr
  624. }
  625. // bool2c convert a Go bool to C.GLboolean
  626. func bool2c(b bool) C.GLboolean {
  627. if b {
  628. return C.GLboolean(1)
  629. }
  630. return C.GLboolean(0)
  631. }
  632. // gobufSize returns a pointer to static buffer with the specified size not including the terminator.
  633. // If there is available space, there is no memory allocation.
  634. func (gs *GLS) gobufSize(size uint32) *C.GLchar {
  635. if size+1 > uint32(len(gs.gobuf)) {
  636. gs.gobuf = make([]byte, size+1)
  637. }
  638. return (*C.GLchar)(unsafe.Pointer(&gs.gobuf[0]))
  639. }
  640. // gobufStr converts a Go String to a C string by copying it to a static buffer
  641. // and returning a pointer to the start of the buffer.
  642. // If there is available space, there is no memory allocation.
  643. func (gs *GLS) gobufStr(s string) *C.GLchar {
  644. p := gs.gobufSize(uint32(len(s) + 1))
  645. copy(gs.gobuf, s)
  646. gs.gobuf[len(s)] = 0
  647. return p
  648. }
  649. // cbufSize returns a pointer to static buffer with C memory
  650. // If there is available space, there is no memory allocation.
  651. func (gs *GLS) cbufSize(size uint32) *C.GLchar {
  652. if size > uint32(len(gs.cbuf)) {
  653. if len(gs.cbuf) > 0 {
  654. C.free(unsafe.Pointer(&gs.cbuf[0]))
  655. }
  656. p := C.malloc(C.size_t(size))
  657. gs.cbuf = (*[1 << 30]byte)(unsafe.Pointer(p))[:size:size]
  658. }
  659. return (*C.GLchar)(unsafe.Pointer(&gs.cbuf[0]))
  660. }
  661. // cbufStr converts a Go String to a C string by copying it to a single pre-allocated buffer
  662. // using C memory and returning a pointer to the start of the buffer.
  663. // If there is available space, there is no memory allocation.
  664. func (gs *GLS) cbufStr(s string) *C.GLchar {
  665. p := gs.cbufSize(uint32(len(s) + 1))
  666. copy(gs.cbuf, s)
  667. gs.cbuf[len(s)] = 0
  668. return p
  669. }