gls-browser.go 26 KB

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