gl_goxjs.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //go:build js || wasm || test_web_driver
  2. // +build js wasm test_web_driver
  3. package gl
  4. import (
  5. "encoding/binary"
  6. "github.com/fyne-io/gl-js"
  7. "golang.org/x/mobile/exp/f32"
  8. )
  9. const (
  10. arrayBuffer = gl.ARRAY_BUFFER
  11. bitColorBuffer = gl.COLOR_BUFFER_BIT
  12. bitDepthBuffer = gl.DEPTH_BUFFER_BIT
  13. clampToEdge = gl.CLAMP_TO_EDGE
  14. colorFormatRGBA = gl.RGBA
  15. colorFormatR = gl.LUMINANCE
  16. compileStatus = gl.COMPILE_STATUS
  17. constantAlpha = gl.CONSTANT_ALPHA
  18. float = gl.FLOAT
  19. fragmentShader = gl.FRAGMENT_SHADER
  20. front = gl.FRONT
  21. glFalse = gl.FALSE
  22. linkStatus = gl.LINK_STATUS
  23. one = gl.ONE
  24. oneMinusConstantAlpha = gl.ONE_MINUS_CONSTANT_ALPHA
  25. oneMinusSrcAlpha = gl.ONE_MINUS_SRC_ALPHA
  26. scissorTest = gl.SCISSOR_TEST
  27. srcAlpha = gl.SRC_ALPHA
  28. staticDraw = gl.STATIC_DRAW
  29. texture0 = gl.TEXTURE0
  30. texture2D = gl.TEXTURE_2D
  31. textureMinFilter = gl.TEXTURE_MIN_FILTER
  32. textureMagFilter = gl.TEXTURE_MAG_FILTER
  33. textureWrapS = gl.TEXTURE_WRAP_S
  34. textureWrapT = gl.TEXTURE_WRAP_T
  35. triangles = gl.TRIANGLES
  36. triangleStrip = gl.TRIANGLE_STRIP
  37. unpackAlignment = gl.UNPACK_ALIGNMENT
  38. unsignedByte = gl.UNSIGNED_BYTE
  39. vertexShader = gl.VERTEX_SHADER
  40. )
  41. type (
  42. // Attribute represents a GL attribute
  43. Attribute gl.Attrib
  44. // Buffer represents a GL buffer
  45. Buffer gl.Buffer
  46. // Program represents a compiled GL program
  47. Program gl.Program
  48. // Shader represents a GL shader
  49. Shader gl.Shader
  50. // Uniform represents a GL uniform
  51. Uniform gl.Uniform
  52. )
  53. var noBuffer = Buffer(gl.NoBuffer)
  54. var noShader = Shader(gl.NoShader)
  55. var textureFilterToGL = []int32{gl.LINEAR, gl.NEAREST}
  56. func (p *painter) Init() {
  57. p.ctx = &xjsContext{}
  58. gl.Disable(gl.DEPTH_TEST)
  59. gl.Enable(gl.BLEND)
  60. p.logError()
  61. p.program = p.createProgram("simple_es")
  62. p.singleChannelProgram = p.createProgram("single_channel_es")
  63. p.lineProgram = p.createProgram("line_es")
  64. }
  65. type xjsContext struct{}
  66. var _ context = (*xjsContext)(nil)
  67. func (c *xjsContext) ActiveTexture(textureUnit uint32) {
  68. gl.ActiveTexture(gl.Enum(textureUnit))
  69. }
  70. func (c *xjsContext) AttachShader(program Program, shader Shader) {
  71. gl.AttachShader(gl.Program(program), gl.Shader(shader))
  72. }
  73. func (c *xjsContext) BindBuffer(target uint32, buf Buffer) {
  74. gl.BindBuffer(gl.Enum(target), gl.Buffer(buf))
  75. }
  76. func (c *xjsContext) BindTexture(target uint32, texture Texture) {
  77. gl.BindTexture(gl.Enum(target), gl.Texture(texture))
  78. }
  79. func (c *xjsContext) BlendColor(r, g, b, a float32) {
  80. gl.BlendColor(r, g, b, a)
  81. }
  82. func (c *xjsContext) BlendFunc(srcFactor, destFactor uint32) {
  83. gl.BlendFunc(gl.Enum(srcFactor), gl.Enum(destFactor))
  84. }
  85. func (c *xjsContext) BufferData(target uint32, points []float32, usage uint32) {
  86. gl.BufferData(gl.Enum(target), f32.Bytes(binary.LittleEndian, points...), gl.Enum(usage))
  87. }
  88. func (c *xjsContext) Clear(mask uint32) {
  89. gl.Clear(gl.Enum(mask))
  90. }
  91. func (c *xjsContext) ClearColor(r, g, b, a float32) {
  92. gl.ClearColor(r, g, b, a)
  93. }
  94. func (c *xjsContext) CompileShader(shader Shader) {
  95. gl.CompileShader(gl.Shader(shader))
  96. }
  97. func (c *xjsContext) CreateBuffer() Buffer {
  98. return Buffer(gl.CreateBuffer())
  99. }
  100. func (c *xjsContext) CreateProgram() Program {
  101. return Program(gl.CreateProgram())
  102. }
  103. func (c *xjsContext) CreateShader(typ uint32) Shader {
  104. return Shader(gl.CreateShader(gl.Enum(typ)))
  105. }
  106. func (c *xjsContext) CreateTexture() (texture Texture) {
  107. return Texture(gl.CreateTexture())
  108. }
  109. func (c *xjsContext) DeleteBuffer(buffer Buffer) {
  110. gl.DeleteBuffer(gl.Buffer(buffer))
  111. }
  112. func (c *xjsContext) DeleteTexture(texture Texture) {
  113. gl.DeleteTexture(gl.Texture(texture))
  114. }
  115. func (c *xjsContext) Disable(capability uint32) {
  116. gl.Disable(gl.Enum(capability))
  117. }
  118. func (c *xjsContext) DrawArrays(mode uint32, first, count int) {
  119. gl.DrawArrays(gl.Enum(mode), first, count)
  120. }
  121. func (c *xjsContext) Enable(capability uint32) {
  122. gl.Enable(gl.Enum(capability))
  123. }
  124. func (c *xjsContext) EnableVertexAttribArray(attribute Attribute) {
  125. gl.EnableVertexAttribArray(gl.Attrib(attribute))
  126. }
  127. func (c *xjsContext) GetAttribLocation(program Program, name string) Attribute {
  128. return Attribute(gl.GetAttribLocation(gl.Program(program), name))
  129. }
  130. func (c *xjsContext) GetError() uint32 {
  131. return uint32(gl.GetError())
  132. }
  133. func (c *xjsContext) GetProgrami(program Program, param uint32) int {
  134. return gl.GetProgrami(gl.Program(program), gl.Enum(param))
  135. }
  136. func (c *xjsContext) GetProgramInfoLog(program Program) string {
  137. return gl.GetProgramInfoLog(gl.Program(program))
  138. }
  139. func (c *xjsContext) GetShaderi(shader Shader, param uint32) int {
  140. return gl.GetShaderi(gl.Shader(shader), gl.Enum(param))
  141. }
  142. func (c *xjsContext) GetShaderInfoLog(shader Shader) string {
  143. return gl.GetShaderInfoLog(gl.Shader(shader))
  144. }
  145. func (c *xjsContext) GetUniformLocation(program Program, name string) Uniform {
  146. return Uniform(gl.GetUniformLocation(gl.Program(program), name))
  147. }
  148. func (c *xjsContext) LinkProgram(program Program) {
  149. gl.LinkProgram(gl.Program(program))
  150. }
  151. func (c *xjsContext) PixelStorei(pname uint32, param int32) {
  152. gl.PixelStorei(gl.Enum(pname), param)
  153. }
  154. func (c *xjsContext) ReadBuffer(_ uint32) {
  155. }
  156. func (c *xjsContext) ReadPixels(x, y, width, height int, colorFormat, typ uint32, pixels []uint8) {
  157. gl.ReadPixels(pixels, x, y, width, height, gl.Enum(colorFormat), gl.Enum(typ))
  158. }
  159. func (c *xjsContext) ShaderSource(shader Shader, source string) {
  160. gl.ShaderSource(gl.Shader(shader), source)
  161. }
  162. func (c *xjsContext) Scissor(x, y, w, h int32) {
  163. gl.Scissor(x, y, w, h)
  164. }
  165. func (c *xjsContext) TexImage2D(target uint32, level, width, height int, colorFormat, typ uint32, data []uint8) {
  166. gl.TexImage2D(
  167. gl.Enum(target),
  168. level,
  169. width,
  170. height,
  171. gl.Enum(colorFormat),
  172. gl.Enum(typ),
  173. data,
  174. )
  175. }
  176. func (c *xjsContext) TexParameteri(target, param uint32, value int32) {
  177. gl.TexParameteri(gl.Enum(target), gl.Enum(param), int(value))
  178. }
  179. func (c *xjsContext) Uniform1f(uniform Uniform, v float32) {
  180. gl.Uniform1f(gl.Uniform(uniform), v)
  181. }
  182. func (c *xjsContext) Uniform4f(uniform Uniform, v0, v1, v2, v3 float32) {
  183. gl.Uniform4f(gl.Uniform(uniform), v0, v1, v2, v3)
  184. }
  185. func (c *xjsContext) UseProgram(program Program) {
  186. gl.UseProgram(gl.Program(program))
  187. }
  188. func (c *xjsContext) VertexAttribPointerWithOffset(attribute Attribute, size int, typ uint32, normalized bool, stride, offset int) {
  189. gl.VertexAttribPointer(gl.Attrib(attribute), size, gl.Enum(typ), normalized, stride, offset)
  190. }
  191. func (c *xjsContext) Viewport(x, y, width, height int) {
  192. gl.Viewport(x, y, width, height)
  193. }