gls-browser.go 25 KB

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