gls-browser.go 25 KB

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