gls-wasm.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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. import (
  7. "fmt"
  8. "math"
  9. "syscall/js"
  10. "unsafe"
  11. )
  12. // GLS encapsulates the state of a WebGL context and contains
  13. // methods to call WebGL functions.
  14. type GLS struct {
  15. stats Stats // statistics
  16. prog *Program // current active shader program
  17. programs map[*Program]bool // shader programs cache
  18. checkErrors bool // check openGL API errors flag
  19. // Cache WebGL state to avoid making unnecessary API calls
  20. activeTexture uint32 // cached last set active texture unit
  21. viewportX int32 // cached last set viewport x
  22. viewportY int32 // cached last set viewport y
  23. viewportWidth int32 // cached last set viewport width
  24. viewportHeight int32 // cached last set viewport height
  25. lineWidth float32 // cached last set line width
  26. sideView int // cached last set triangle side view mode
  27. frontFace uint32 // cached last set glFrontFace value
  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. polygonModeFace uint32 // cached last set polygon mode face
  41. polygonModeMode uint32 // cached last set polygon mode mode
  42. polygonOffsetFactor float32 // cached last set polygon offset factor
  43. polygonOffsetUnits float32 // cached last set polygon offset units
  44. // js.Value storage maps
  45. programMap map[uint32]js.Value
  46. shaderMap map[uint32]js.Value
  47. bufferMap map[uint32]js.Value
  48. framebufferMap map[uint32]js.Value
  49. renderbufferMap map[uint32]js.Value
  50. textureMap map[uint32]js.Value
  51. uniformMap map[uint32]js.Value
  52. vertexArrayMap map[uint32]js.Value
  53. // Next free index to be used for each map
  54. programMapIndex uint32
  55. shaderMapIndex uint32
  56. bufferMapIndex uint32
  57. framebufferMapIndex uint32
  58. renderbufferMapIndex uint32
  59. textureMapIndex uint32
  60. uniformMapIndex uint32
  61. vertexArrayMapIndex uint32
  62. // Canvas and WebGL Context
  63. canvas js.Value
  64. gl js.Value
  65. }
  66. // Stats contains counters of WebGL resources being used as well
  67. // the cumulative numbers of some WebGL calls for performance evaluation.
  68. type Stats struct {
  69. Shaders int // Current number of shader programs
  70. Vaos int // Number of Vertex Array Objects
  71. Buffers int // Number of Buffer Objects
  72. Textures int // Number of Textures
  73. Caphits uint64 // Cumulative number of hits for Enable/Disable
  74. UnilocHits uint64 // Cumulative number of uniform location cache hits
  75. UnilocMiss uint64 // Cumulative number of uniform location cache misses
  76. Unisets uint64 // Cumulative number of uniform sets
  77. Drawcalls uint64 // Cumulative number of draw calls
  78. }
  79. const (
  80. capUndef = 0
  81. capDisabled = 1
  82. capEnabled = 2
  83. uintUndef = math.MaxUint32
  84. intFalse = 0
  85. intTrue = 1
  86. )
  87. // New creates and returns a new instance of a GLS object,
  88. // which encapsulates the state of an WebGL context.
  89. // This should be called only after an active WebGL context
  90. // is established, such as by creating a new window.
  91. func New() (*GLS, error) {
  92. gs := new(GLS)
  93. gs.reset()
  94. gs.checkErrors = true
  95. // Create js.Value storage maps
  96. gs.programMap = make(map[uint32]js.Value)
  97. gs.shaderMap = make(map[uint32]js.Value)
  98. gs.bufferMap = make(map[uint32]js.Value)
  99. gs.framebufferMap = make(map[uint32]js.Value)
  100. gs.renderbufferMap = make(map[uint32]js.Value)
  101. gs.textureMap = make(map[uint32]js.Value)
  102. gs.uniformMap = make(map[uint32]js.Value)
  103. gs.vertexArrayMap = make(map[uint32]js.Value)
  104. // Initialize indexes to be used with the maps above
  105. gs.programMapIndex = 1
  106. gs.shaderMapIndex = 1
  107. gs.bufferMapIndex = 1
  108. gs.framebufferMapIndex = 1
  109. gs.renderbufferMapIndex = 1
  110. gs.textureMapIndex = 1
  111. gs.uniformMapIndex = 1
  112. gs.vertexArrayMapIndex = 1
  113. // Create canvas and get reference to WebGL context
  114. doc := js.Global().Get("document")
  115. gs.canvas = doc.Call("createElement", "canvas")
  116. gs.gl = gs.canvas.Call("getContext", "webgl2")
  117. if gs.gl == js.Undefined() {
  118. return nil, fmt.Errorf("Browser doesn't support WebGL2")
  119. }
  120. gs.setDefaultState()
  121. return gs, nil
  122. }
  123. // Canvas returns the associated WebGL canvas.
  124. func (gs *GLS) Canvas() js.Value {
  125. return gs.canvas
  126. }
  127. // SetCheckErrors enables/disables checking for errors after the
  128. // call of any WebGL function. It is enabled by default but
  129. // could be disabled after an application is stable to improve the performance.
  130. func (gs *GLS) SetCheckErrors(enable bool) {
  131. gs.checkErrors = enable
  132. }
  133. // CheckErrors returns if error checking is enabled or not.
  134. func (gs *GLS) CheckErrors() bool {
  135. return gs.checkErrors
  136. }
  137. // reset resets the internal state kept of the WebGL
  138. func (gs *GLS) reset() {
  139. gs.lineWidth = 0.0
  140. gs.sideView = uintUndef
  141. gs.frontFace = 0
  142. gs.depthFunc = 0
  143. gs.depthMask = uintUndef
  144. gs.capabilities = make(map[int]int)
  145. gs.programs = make(map[*Program]bool)
  146. gs.prog = nil
  147. gs.activeTexture = uintUndef
  148. gs.blendEquation = uintUndef
  149. gs.blendSrc = uintUndef
  150. gs.blendDst = uintUndef
  151. gs.blendEquationRGB = 0
  152. gs.blendEquationAlpha = 0
  153. gs.blendSrcRGB = uintUndef
  154. gs.blendSrcAlpha = uintUndef
  155. gs.blendDstRGB = uintUndef
  156. gs.blendDstAlpha = uintUndef
  157. gs.polygonModeFace = 0
  158. gs.polygonModeMode = 0
  159. gs.polygonOffsetFactor = -1
  160. gs.polygonOffsetUnits = -1
  161. }
  162. // setDefaultState is used internally to set the initial state of WebGL
  163. // for this context.
  164. func (gs *GLS) setDefaultState() {
  165. gs.ClearColor(0, 0, 0, 1)
  166. gs.ClearDepth(1)
  167. gs.ClearStencil(0)
  168. gs.Enable(DEPTH_TEST)
  169. gs.DepthFunc(LEQUAL)
  170. gs.FrontFace(CCW)
  171. gs.CullFace(BACK)
  172. gs.Enable(CULL_FACE)
  173. gs.Enable(BLEND)
  174. gs.BlendEquation(FUNC_ADD)
  175. gs.BlendFunc(SRC_ALPHA, ONE_MINUS_SRC_ALPHA)
  176. // TODO commented constants not available in WebGL
  177. //gs.Enable(VERTEX_PROGRAM_POINT_SIZE)
  178. //gs.Enable(PROGRAM_POINT_SIZE)
  179. //gs.Enable(MULTISAMPLE)
  180. gs.Enable(POLYGON_OFFSET_FILL)
  181. //gs.Enable(POLYGON_OFFSET_LINE)
  182. //gs.Enable(POLYGON_OFFSET_POINT)
  183. }
  184. // Stats copy the current values of the internal statistics structure
  185. // to the specified pointer.
  186. func (gs *GLS) Stats(s *Stats) {
  187. *s = gs.stats
  188. s.Shaders = len(gs.programs)
  189. }
  190. // ActiveTexture selects which texture unit subsequent texture state calls
  191. // will affect. The number of texture units an implementation supports is
  192. // implementation dependent, but must be at least 48 in GL 3.3.
  193. func (gs *GLS) ActiveTexture(texture uint32) {
  194. if gs.activeTexture == texture {
  195. return
  196. }
  197. gs.gl.Call("activeTexture", int(texture))
  198. gs.checkError("ActiveTexture")
  199. gs.activeTexture = texture
  200. }
  201. // AttachShader attaches the specified shader object to the specified program object.
  202. func (gs *GLS) AttachShader(program, shader uint32) {
  203. gs.gl.Call("attachShader", gs.programMap[program], gs.shaderMap[shader])
  204. gs.checkError("AttachShader")
  205. }
  206. // BindBuffer binds a buffer object to the specified buffer binding point.
  207. func (gs *GLS) BindBuffer(target int, vbo uint32) {
  208. gs.gl.Call("bindBuffer", target, gs.bufferMap[vbo])
  209. gs.checkError("BindBuffer")
  210. }
  211. // BindTexture lets you create or use a named texture.
  212. func (gs *GLS) BindTexture(target int, tex uint32) {
  213. gs.gl.Call("bindTexture", target, gs.textureMap[tex])
  214. gs.checkError("BindTexture")
  215. }
  216. // BindVertexArray binds the vertex array object.
  217. func (gs *GLS) BindVertexArray(vao uint32) {
  218. gs.gl.Call("bindVertexArray", gs.vertexArrayMap[vao])
  219. gs.checkError("BindVertexArray")
  220. }
  221. // BlendEquation sets the blend equations for all draw buffers.
  222. func (gs *GLS) BlendEquation(mode uint32) {
  223. if gs.blendEquation == mode {
  224. return
  225. }
  226. gs.gl.Call("blendEquation", int(mode))
  227. gs.checkError("BlendEquation")
  228. gs.blendEquation = mode
  229. }
  230. // BlendEquationSeparate sets the blend equations for all draw buffers
  231. // allowing different equations for the RGB and alpha components.
  232. func (gs *GLS) BlendEquationSeparate(modeRGB uint32, modeAlpha uint32) {
  233. if gs.blendEquationRGB == modeRGB && gs.blendEquationAlpha == modeAlpha {
  234. return
  235. }
  236. gs.gl.Call("blendEquationSeparate", int(modeRGB), int(modeAlpha))
  237. gs.checkError("BlendEquationSeparate")
  238. gs.blendEquationRGB = modeRGB
  239. gs.blendEquationAlpha = modeAlpha
  240. }
  241. // BlendFunc defines the operation of blending for
  242. // all draw buffers when blending is enabled.
  243. func (gs *GLS) BlendFunc(sfactor, dfactor uint32) {
  244. if gs.blendSrc == sfactor && gs.blendDst == dfactor {
  245. return
  246. }
  247. gs.gl.Call("blendFunc", int(sfactor), int(dfactor))
  248. gs.checkError("BlendFunc")
  249. gs.blendSrc = sfactor
  250. gs.blendDst = dfactor
  251. }
  252. // BlendFuncSeparate defines the operation of blending for all draw buffers when blending
  253. // is enabled, allowing different operations for the RGB and alpha components.
  254. func (gs *GLS) BlendFuncSeparate(srcRGB uint32, dstRGB uint32, srcAlpha uint32, dstAlpha uint32) {
  255. if gs.blendSrcRGB == srcRGB && gs.blendDstRGB == dstRGB &&
  256. gs.blendSrcAlpha == srcAlpha && gs.blendDstAlpha == dstAlpha {
  257. return
  258. }
  259. gs.gl.Call("blendFuncSeparate", int(srcRGB), int(dstRGB), int(srcAlpha), int(dstAlpha))
  260. gs.checkError("BlendFuncSeparate")
  261. gs.blendSrcRGB = srcRGB
  262. gs.blendDstRGB = dstRGB
  263. gs.blendSrcAlpha = srcAlpha
  264. gs.blendDstAlpha = dstAlpha
  265. }
  266. // BufferData creates a new data store for the buffer object currently
  267. // bound to target, deleting any pre-existing data store.
  268. func (gs *GLS) BufferData(target uint32, size int, data interface{}, usage uint32) {
  269. dataTA := js.TypedArrayOf(data)
  270. gs.gl.Call("bufferData", int(target), dataTA, int(usage))
  271. gs.checkError("BufferData")
  272. dataTA.Release()
  273. }
  274. // ClearColor specifies the red, green, blue, and alpha values
  275. // used by glClear to clear the color buffers.
  276. func (gs *GLS) ClearColor(r, g, b, a float32) {
  277. gs.gl.Call("clearColor", r, g, b, a)
  278. gs.checkError("ClearColor")
  279. }
  280. // ClearDepth specifies the depth value used by Clear to clear the depth buffer.
  281. func (gs *GLS) ClearDepth(v float32) {
  282. gs.gl.Call("clearDepth", v)
  283. gs.checkError("ClearDepth")
  284. }
  285. // ClearStencil specifies the index used by Clear to clear the stencil buffer.
  286. func (gs *GLS) ClearStencil(v int32) {
  287. gs.gl.Call("clearStencil", int(v))
  288. gs.checkError("ClearStencil")
  289. }
  290. // Clear sets the bitplane area of the window to values previously
  291. // selected by ClearColor, ClearDepth, and ClearStencil.
  292. func (gs *GLS) Clear(mask uint) {
  293. gs.gl.Call("clear", int(mask))
  294. gs.checkError("Clear")
  295. }
  296. // CompileShader compiles the source code strings that
  297. // have been stored in the specified shader object.
  298. func (gs *GLS) CompileShader(shader uint32) {
  299. gs.gl.Call("compileShader", gs.shaderMap[shader])
  300. gs.checkError("CompileShader")
  301. }
  302. // CreateProgram creates an empty program object and returns
  303. // a non-zero value by which it can be referenced.
  304. func (gs *GLS) CreateProgram() uint32 {
  305. gs.programMap[gs.programMapIndex] = gs.gl.Call("createProgram")
  306. gs.checkError("CreateProgram")
  307. idx := gs.programMapIndex
  308. gs.programMapIndex++
  309. return idx
  310. }
  311. // CreateShader creates an empty shader object and returns
  312. // a non-zero value by which it can be referenced.
  313. func (gs *GLS) CreateShader(stype uint32) uint32 {
  314. gs.shaderMap[gs.shaderMapIndex] = gs.gl.Call("createShader", int(stype))
  315. gs.checkError("CreateShader")
  316. idx := gs.shaderMapIndex
  317. gs.shaderMapIndex++
  318. return idx
  319. }
  320. // DeleteBuffers deletes n​buffer objects named
  321. // by the elements of the provided array.
  322. func (gs *GLS) DeleteBuffers(bufs ...uint32) {
  323. for _, buf := range bufs {
  324. gs.gl.Call("deleteBuffer", gs.bufferMap[buf])
  325. gs.checkError("DeleteBuffers")
  326. gs.stats.Buffers--
  327. delete(gs.bufferMap, buf)
  328. }
  329. }
  330. // DeleteShader frees the memory and invalidates the name
  331. // associated with the specified shader object.
  332. func (gs *GLS) DeleteShader(shader uint32) {
  333. gs.gl.Call("deleteShader", gs.shaderMap[shader])
  334. gs.checkError("DeleteShader")
  335. delete(gs.shaderMap, shader)
  336. }
  337. // DeleteProgram frees the memory and invalidates the name
  338. // associated with the specified program object.
  339. func (gs *GLS) DeleteProgram(program uint32) {
  340. gs.gl.Call("deleteProgram", gs.programMap[program])
  341. gs.checkError("DeleteProgram")
  342. delete(gs.programMap, program)
  343. }
  344. // DeleteTextures deletes n​textures named
  345. // by the elements of the provided array.
  346. func (gs *GLS) DeleteTextures(tex ...uint32) {
  347. for _, t := range tex {
  348. gs.gl.Call("deleteTexture", gs.textureMap[t])
  349. gs.checkError("DeleteTextures")
  350. delete(gs.textureMap, t)
  351. gs.stats.Textures--
  352. }
  353. }
  354. // DeleteVertexArrays deletes n​vertex array objects named
  355. // by the elements of the provided array.
  356. func (gs *GLS) DeleteVertexArrays(vaos ...uint32) {
  357. for _, v := range vaos {
  358. gs.gl.Call("deleteVertexArray", gs.vertexArrayMap[v])
  359. gs.checkError("DeleteVertexArrays")
  360. delete(gs.vertexArrayMap, v)
  361. gs.stats.Vaos--
  362. }
  363. }
  364. // DepthFunc specifies the function used to compare each incoming pixel
  365. // depth value with the depth value present in the depth buffer.
  366. func (gs *GLS) DepthFunc(mode uint32) {
  367. if gs.depthFunc == mode {
  368. return
  369. }
  370. gs.gl.Call("depthFunc", int(mode))
  371. gs.checkError("DepthFunc")
  372. gs.depthFunc = mode
  373. }
  374. // DepthMask enables or disables writing into the depth buffer.
  375. func (gs *GLS) DepthMask(flag bool) {
  376. if gs.depthMask == intTrue && flag {
  377. return
  378. }
  379. if gs.depthMask == intFalse && !flag {
  380. return
  381. }
  382. gs.gl.Call("depthMask", flag)
  383. gs.checkError("DepthMask")
  384. if flag {
  385. gs.depthMask = intTrue
  386. } else {
  387. gs.depthMask = intFalse
  388. }
  389. }
  390. // DrawArrays renders primitives from array data.
  391. func (gs *GLS) DrawArrays(mode uint32, first int32, count int32) {
  392. gs.gl.Call("drawArrays", int(mode), first, count)
  393. gs.checkError("DrawArrays")
  394. gs.stats.Drawcalls++
  395. }
  396. // DrawElements renders primitives from array data.
  397. func (gs *GLS) DrawElements(mode uint32, count int32, itype uint32, start uint32) {
  398. gs.gl.Call("drawElements", int(mode), count, int(itype), start)
  399. gs.checkError("DrawElements")
  400. gs.stats.Drawcalls++
  401. }
  402. // Enable enables the specified capability.
  403. func (gs *GLS) Enable(cap int) {
  404. if gs.capabilities[cap] == capEnabled {
  405. gs.stats.Caphits++
  406. return
  407. }
  408. gs.gl.Call("enable", int32(cap))
  409. gs.checkError("Enable")
  410. gs.capabilities[cap] = capEnabled
  411. }
  412. // Disable disables the specified capability.
  413. func (gs *GLS) Disable(cap int) {
  414. if gs.capabilities[cap] == capDisabled {
  415. gs.stats.Caphits++
  416. return
  417. }
  418. gs.gl.Call("disable", cap)
  419. gs.checkError("Disable")
  420. gs.capabilities[cap] = capDisabled
  421. }
  422. // EnableVertexAttribArray enables a generic vertex attribute array.
  423. func (gs *GLS) EnableVertexAttribArray(index uint32) {
  424. gs.gl.Call("enableVertexAttribArray", index)
  425. gs.checkError("EnableVertexAttribArray")
  426. }
  427. // CullFace specifies whether front- or back-facing facets can be culled.
  428. func (gs *GLS) CullFace(mode uint32) {
  429. gs.gl.Call("cullFace", int(mode))
  430. gs.checkError("CullFace")
  431. }
  432. // FrontFace defines front- and back-facing polygons.
  433. func (gs *GLS) FrontFace(mode uint32) {
  434. if gs.frontFace == mode {
  435. return
  436. }
  437. gs.gl.Call("frontFace", int(mode))
  438. gs.checkError("FrontFace")
  439. gs.frontFace = mode
  440. }
  441. func (gs *GLS) GenBuffer() uint32 {
  442. gs.bufferMap[gs.bufferMapIndex] = gs.gl.Call("createBuffer")
  443. gs.checkError("CreateBuffer")
  444. idx := gs.bufferMapIndex
  445. gs.bufferMapIndex++
  446. gs.stats.Buffers++
  447. return idx
  448. }
  449. // GenerateMipmap generates mipmaps for the specified texture target.
  450. func (gs *GLS) GenerateMipmap(target uint32) {
  451. gs.gl.Call("generateMipmap", int(target))
  452. gs.checkError("GenerateMipmap")
  453. }
  454. // GenTexture generates a texture object name.
  455. func (gs *GLS) GenTexture() uint32 {
  456. gs.textureMap[gs.textureMapIndex] = gs.gl.Call("createTexture")
  457. gs.checkError("GenTexture")
  458. idx := gs.textureMapIndex
  459. gs.textureMapIndex++
  460. gs.stats.Textures++
  461. return idx
  462. }
  463. // GenVertexArray generates a vertex array object name.
  464. func (gs *GLS) GenVertexArray() uint32 {
  465. gs.vertexArrayMap[gs.vertexArrayMapIndex] = gs.gl.Call("createVertexArray")
  466. gs.checkError("GenVertexArray")
  467. idx := gs.vertexArrayMapIndex
  468. gs.vertexArrayMapIndex++
  469. gs.stats.Vaos++
  470. return idx
  471. }
  472. // GetAttribLocation returns the location of the specified attribute variable.
  473. func (gs *GLS) GetAttribLocation(program uint32, name string) int32 {
  474. loc := gs.gl.Call("getAttribLocation", gs.programMap[program], name).Int()
  475. gs.checkError("GetAttribLocation")
  476. return int32(loc)
  477. }
  478. // GetProgramiv returns the specified parameter from the specified program object.
  479. func (gs *GLS) GetProgramiv(program, pname uint32, params *int32) {
  480. sparam := gs.gl.Call("getProgramParameter", gs.programMap[program], int(pname))
  481. gs.checkError("GetProgramiv")
  482. switch pname {
  483. case DELETE_STATUS, LINK_STATUS, VALIDATE_STATUS:
  484. if sparam.Bool() {
  485. *params = TRUE
  486. } else {
  487. *params = FALSE
  488. }
  489. default:
  490. *params = int32(sparam.Int())
  491. }
  492. }
  493. // GetProgramInfoLog returns the information log for the specified program object.
  494. func (gs *GLS) GetProgramInfoLog(program uint32) string {
  495. res := gs.gl.Call("getProgramInfoLog", gs.programMap[program]).String()
  496. gs.checkError("GetProgramInfoLog")
  497. return res
  498. }
  499. // GetShaderInfoLog returns the information log for the specified shader object.
  500. func (gs *GLS) GetShaderInfoLog(shader uint32) string {
  501. res := gs.gl.Call("getShaderInfoLog", gs.shaderMap[shader]).String()
  502. gs.checkError("GetShaderInfoLog")
  503. return res
  504. }
  505. // GetString returns a string describing the specified aspect of the current GL connection.
  506. func (gs *GLS) GetString(name uint32) string {
  507. res := gs.gl.Call("getParameter", int(name)).String()
  508. gs.checkError("GetString")
  509. return res
  510. }
  511. // GetUniformLocation returns the location of a uniform variable for the specified program.
  512. func (gs *GLS) GetUniformLocation(program uint32, name string) int32 {
  513. loc := gs.gl.Call("getUniformLocation", gs.programMap[program], name)
  514. if loc == js.Null() {
  515. return -1
  516. }
  517. gs.uniformMap[gs.uniformMapIndex] = loc
  518. gs.checkError("GetUniformLocation")
  519. idx := gs.uniformMapIndex
  520. gs.uniformMapIndex++
  521. return int32(idx)
  522. }
  523. // GetViewport returns the current viewport information.
  524. func (gs *GLS) GetViewport() (x, y, width, height int32) {
  525. return gs.viewportX, gs.viewportY, gs.viewportWidth, gs.viewportHeight
  526. }
  527. // LineWidth specifies the rasterized width of both aliased and antialiased lines.
  528. func (gs *GLS) LineWidth(width float32) {
  529. if gs.lineWidth == width {
  530. return
  531. }
  532. gs.gl.Call("lineWidth", width)
  533. gs.checkError("LineWidth")
  534. gs.lineWidth = width
  535. }
  536. // LinkProgram links the specified program object.
  537. func (gs *GLS) LinkProgram(program uint32) {
  538. gs.gl.Call("linkProgram", gs.programMap[program])
  539. gs.checkError("LinkProgram")
  540. }
  541. // GetShaderiv returns the specified parameter from the specified shader object.
  542. func (gs *GLS) GetShaderiv(shader, pname uint32, params *int32) {
  543. sparam := gs.gl.Call("getShaderParameter", gs.shaderMap[shader], int(pname))
  544. gs.checkError("GetShaderiv")
  545. switch pname {
  546. case DELETE_STATUS, COMPILE_STATUS:
  547. if sparam.Bool() {
  548. *params = TRUE
  549. } else {
  550. *params = FALSE
  551. }
  552. default:
  553. *params = int32(sparam.Int())
  554. }
  555. }
  556. // Scissor defines the scissor box rectangle in window coordinates.
  557. func (gs *GLS) Scissor(x, y int32, width, height uint32) {
  558. gs.gl.Call("scissor", x, y, int(width), int(height))
  559. gs.checkError("Scissor")
  560. }
  561. // ShaderSource sets the source code for the specified shader object.
  562. func (gs *GLS) ShaderSource(shader uint32, src string) {
  563. gs.gl.Call("shaderSource", gs.shaderMap[shader], src)
  564. gs.checkError("ShaderSource")
  565. }
  566. // TexImage2D specifies a two-dimensional texture image.
  567. func (gs *GLS) TexImage2D(target uint32, level int32, iformat int32, width int32, height int32, format uint32, itype uint32, data interface{}) {
  568. dataTA := js.TypedArrayOf(data)
  569. gs.gl.Call("texImage2D", int(target), level, iformat, width, height, 0, int(format), int(itype), dataTA)
  570. gs.checkError("TexImage2D")
  571. dataTA.Release()
  572. }
  573. // TexParameteri sets the specified texture parameter on the specified texture.
  574. func (gs *GLS) TexParameteri(target uint32, pname uint32, param int32) {
  575. gs.gl.Call("texParameteri", int(target), int(pname), param)
  576. gs.checkError("TexParameteri")
  577. }
  578. // PolygonMode controls the interpretation of polygons for rasterization.
  579. func (gs *GLS) PolygonMode(face, mode uint32) {
  580. log.Warn("PolygonMode not available in WebGL")
  581. }
  582. // PolygonOffset sets the scale and units used to calculate depth values.
  583. func (gs *GLS) PolygonOffset(factor float32, units float32) {
  584. if gs.polygonOffsetFactor == factor && gs.polygonOffsetUnits == units {
  585. return
  586. }
  587. gs.gl.Call("polygonOffset", factor, units)
  588. gs.checkError("PolygonOffset")
  589. gs.polygonOffsetFactor = factor
  590. gs.polygonOffsetUnits = units
  591. }
  592. // Uniform1i sets the value of an int uniform variable for the current program object.
  593. func (gs *GLS) Uniform1i(location int32, v0 int32) {
  594. gs.gl.Call("uniform1i", gs.uniformMap[uint32(location)], v0)
  595. gs.checkError("Uniform1i")
  596. gs.stats.Unisets++
  597. }
  598. // Uniform1f sets the value of a float uniform variable for the current program object.
  599. func (gs *GLS) Uniform1f(location int32, v0 float32) {
  600. gs.gl.Call("uniform1f", gs.uniformMap[uint32(location)], v0)
  601. gs.checkError("Uniform1f")
  602. gs.stats.Unisets++
  603. }
  604. // Uniform2f sets the value of a vec2 uniform variable for the current program object.
  605. func (gs *GLS) Uniform2f(location int32, v0, v1 float32) {
  606. gs.gl.Call("uniform2f", gs.uniformMap[uint32(location)], v0, v1)
  607. gs.checkError("Uniform2f")
  608. gs.stats.Unisets++
  609. }
  610. // Uniform3f sets the value of a vec3 uniform variable for the current program object.
  611. func (gs *GLS) Uniform3f(location int32, v0, v1, v2 float32) {
  612. gs.gl.Call("uniform3f", gs.uniformMap[uint32(location)], v0, v1, v2)
  613. gs.checkError("Uniform3f")
  614. gs.stats.Unisets++
  615. }
  616. // Uniform4f sets the value of a vec4 uniform variable for the current program object.
  617. func (gs *GLS) Uniform4f(location int32, v0, v1, v2, v3 float32) {
  618. gs.gl.Call("uniform4f", gs.uniformMap[uint32(location)], v0, v1, v2, v3)
  619. gs.checkError("Uniform4f")
  620. gs.stats.Unisets++
  621. }
  622. //// UniformMatrix3fv sets the value of one or many 3x3 float matrices for the current program object.
  623. func (gs *GLS) UniformMatrix3fv(location int32, count int32, transpose bool, pm *float32) {
  624. data := (*[1 << 30]float32)(unsafe.Pointer(pm))[:9*count]
  625. dataTA := js.TypedArrayOf(data)
  626. gs.gl.Call("uniformMatrix3fv", gs.uniformMap[uint32(location)], transpose, dataTA)
  627. dataTA.Release()
  628. gs.checkError("UniformMatrix3fv")
  629. gs.stats.Unisets++
  630. }
  631. // UniformMatrix4fv sets the value of one or many 4x4 float matrices for the current program object.
  632. func (gs *GLS) UniformMatrix4fv(location int32, count int32, transpose bool, pm *float32) {
  633. data := (*[1 << 30]float32)(unsafe.Pointer(pm))[:16*count]
  634. dataTA := js.TypedArrayOf(data)
  635. gs.gl.Call("uniformMatrix4fv", gs.uniformMap[uint32(location)], transpose, dataTA)
  636. dataTA.Release()
  637. gs.checkError("UniformMatrix4fv")
  638. gs.stats.Unisets++
  639. }
  640. // Uniform1fv sets the value of one or many float uniform variables for the current program object.
  641. func (gs *GLS) Uniform1fv(location int32, count int32, v *float32) {
  642. data := (*[1 << 30]float32)(unsafe.Pointer(v))[:count]
  643. dataTA := js.TypedArrayOf(data)
  644. gs.gl.Call("uniform1fv", gs.uniformMap[uint32(location)], dataTA)
  645. dataTA.Release()
  646. gs.checkError("Uniform1fv")
  647. gs.stats.Unisets++
  648. }
  649. // Uniform2fv sets the value of one or many vec2 uniform variables for the current program object.
  650. func (gs *GLS) Uniform2fv(location int32, count int32, v *float32) {
  651. data := (*[1 << 30]float32)(unsafe.Pointer(v))[:2*count]
  652. dataTA := js.TypedArrayOf(data)
  653. gs.gl.Call("uniform2fv", gs.uniformMap[uint32(location)], dataTA)
  654. dataTA.Release()
  655. gs.checkError("Uniform2fv")
  656. gs.stats.Unisets++
  657. }
  658. // Uniform3fv sets the value of one or many vec3 uniform variables for the current program object.
  659. func (gs *GLS) Uniform3fv(location int32, count int32, v *float32) {
  660. data := (*[1 << 30]float32)(unsafe.Pointer(v))[:3*count]
  661. dataTA := js.TypedArrayOf(data)
  662. gs.gl.Call("uniform3fv", gs.uniformMap[uint32(location)], dataTA)
  663. dataTA.Release()
  664. gs.checkError("Uniform3fv")
  665. gs.stats.Unisets++
  666. }
  667. // Uniform4fv sets the value of one or many vec4 uniform variables for the current program object.
  668. func (gs *GLS) Uniform4fv(location int32, count int32, v *float32) {
  669. data := (*[1 << 30]float32)(unsafe.Pointer(v))[:4*count]
  670. dataTA := js.TypedArrayOf(data)
  671. gs.gl.Call("uniform4fv", gs.uniformMap[uint32(location)], dataTA)
  672. dataTA.Release()
  673. gs.checkError("Uniform4fv")
  674. gs.stats.Unisets++
  675. }
  676. // VertexAttribPointer defines an array of generic vertex attribute data.
  677. func (gs *GLS) VertexAttribPointer(index uint32, size int32, xtype uint32, normalized bool, stride int32, offset uint32) {
  678. gs.gl.Call("vertexAttribPointer", index, size, int(xtype), normalized, stride, offset)
  679. gs.checkError("VertexAttribPointer")
  680. }
  681. // Viewport sets the viewport.
  682. func (gs *GLS) Viewport(x, y, width, height int32) {
  683. gs.gl.Call("viewport", x, y, width, height)
  684. gs.checkError("Viewport")
  685. gs.viewportX = x
  686. gs.viewportY = y
  687. gs.viewportWidth = width
  688. gs.viewportHeight = height
  689. }
  690. // UseProgram sets the specified program as the current program.
  691. func (gs *GLS) UseProgram(prog *Program) {
  692. if prog.handle == 0 {
  693. panic("Invalid program")
  694. }
  695. gs.gl.Call("useProgram", gs.programMap[prog.handle])
  696. gs.checkError("UseProgram")
  697. gs.prog = prog
  698. // Inserts program in cache if not already there.
  699. if !gs.programs[prog] {
  700. gs.programs[prog] = true
  701. log.Debug("New Program activated. Total: %d", len(gs.programs))
  702. }
  703. }
  704. // checkError checks if there are any WebGL errors and panics if so.
  705. func (gs *GLS) checkError(name string) {
  706. if !gs.checkErrors {
  707. return
  708. }
  709. err := gs.gl.Call("getError")
  710. if err.Int() != NO_ERROR {
  711. panic(fmt.Sprintf("%s error: %v", name, err))
  712. }
  713. }