gls-browser.go 25 KB

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