gl_es.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //go:build (gles || arm || arm64) && !android && !ios && !mobile && !darwin && !js && !wasm && !test_web_driver
  2. // +build gles arm arm64
  3. // +build !android
  4. // +build !ios
  5. // +build !mobile
  6. // +build !darwin
  7. // +build !js
  8. // +build !wasm
  9. // +build !test_web_driver
  10. package gl
  11. import (
  12. "strings"
  13. gl "github.com/go-gl/gl/v3.1/gles2"
  14. "fyne.io/fyne/v2"
  15. )
  16. const (
  17. arrayBuffer = gl.ARRAY_BUFFER
  18. bitColorBuffer = gl.COLOR_BUFFER_BIT
  19. bitDepthBuffer = gl.DEPTH_BUFFER_BIT
  20. clampToEdge = gl.CLAMP_TO_EDGE
  21. colorFormatRGBA = gl.RGBA
  22. compileStatus = gl.COMPILE_STATUS
  23. constantAlpha = gl.CONSTANT_ALPHA
  24. float = gl.FLOAT
  25. fragmentShader = gl.FRAGMENT_SHADER
  26. front = gl.FRONT
  27. glFalse = gl.FALSE
  28. linkStatus = gl.LINK_STATUS
  29. one = gl.ONE
  30. oneMinusConstantAlpha = gl.ONE_MINUS_CONSTANT_ALPHA
  31. oneMinusSrcAlpha = gl.ONE_MINUS_SRC_ALPHA
  32. scissorTest = gl.SCISSOR_TEST
  33. srcAlpha = gl.SRC_ALPHA
  34. staticDraw = gl.STATIC_DRAW
  35. texture0 = gl.TEXTURE0
  36. texture2D = gl.TEXTURE_2D
  37. textureMinFilter = gl.TEXTURE_MIN_FILTER
  38. textureMagFilter = gl.TEXTURE_MAG_FILTER
  39. textureWrapS = gl.TEXTURE_WRAP_S
  40. textureWrapT = gl.TEXTURE_WRAP_T
  41. triangles = gl.TRIANGLES
  42. triangleStrip = gl.TRIANGLE_STRIP
  43. unsignedByte = gl.UNSIGNED_BYTE
  44. vertexShader = gl.VERTEX_SHADER
  45. )
  46. const noBuffer = Buffer(0)
  47. const noShader = Shader(0)
  48. type (
  49. // Attribute represents a GL attribute
  50. Attribute int32
  51. // Buffer represents a GL buffer
  52. Buffer uint32
  53. // Program represents a compiled GL program
  54. Program uint32
  55. // Shader represents a GL shader
  56. Shader uint32
  57. // Uniform represents a GL uniform
  58. Uniform int32
  59. )
  60. var textureFilterToGL = []int32{gl.LINEAR, gl.NEAREST, gl.LINEAR}
  61. func (p *painter) Init() {
  62. p.ctx = &esContext{}
  63. err := gl.Init()
  64. if err != nil {
  65. fyne.LogError("failed to initialise OpenGL", err)
  66. return
  67. }
  68. gl.Disable(gl.DEPTH_TEST)
  69. gl.Enable(gl.BLEND)
  70. p.logError()
  71. p.program = p.createProgram("simple_es")
  72. p.lineProgram = p.createProgram("line_es")
  73. p.rectangleProgram = p.createProgram("rectangle_es")
  74. p.roundRectangleProgram = p.createProgram("round_rectangle_es")
  75. }
  76. type esContext struct{}
  77. var _ context = (*esContext)(nil)
  78. func (c *esContext) ActiveTexture(textureUnit uint32) {
  79. gl.ActiveTexture(textureUnit)
  80. }
  81. func (c *esContext) AttachShader(program Program, shader Shader) {
  82. gl.AttachShader(uint32(program), uint32(shader))
  83. }
  84. func (c *esContext) BindBuffer(target uint32, buf Buffer) {
  85. gl.BindBuffer(target, uint32(buf))
  86. }
  87. func (c *esContext) BindTexture(target uint32, texture Texture) {
  88. gl.BindTexture(target, uint32(texture))
  89. }
  90. func (c *esContext) BlendColor(r, g, b, a float32) {
  91. gl.BlendColor(r, g, b, a)
  92. }
  93. func (c *esContext) BlendFunc(srcFactor, destFactor uint32) {
  94. gl.BlendFunc(srcFactor, destFactor)
  95. }
  96. func (c *esContext) BufferData(target uint32, points []float32, usage uint32) {
  97. gl.BufferData(target, 4*len(points), gl.Ptr(points), usage)
  98. }
  99. func (c *esContext) Clear(mask uint32) {
  100. gl.Clear(mask)
  101. }
  102. func (c *esContext) ClearColor(r, g, b, a float32) {
  103. gl.ClearColor(r, g, b, a)
  104. }
  105. func (c *esContext) CompileShader(shader Shader) {
  106. gl.CompileShader(uint32(shader))
  107. }
  108. func (c *esContext) CreateBuffer() Buffer {
  109. var vbo uint32
  110. gl.GenBuffers(1, &vbo)
  111. return Buffer(vbo)
  112. }
  113. func (c *esContext) CreateProgram() Program {
  114. return Program(gl.CreateProgram())
  115. }
  116. func (c *esContext) CreateShader(typ uint32) Shader {
  117. return Shader(gl.CreateShader(typ))
  118. }
  119. func (c *esContext) CreateTexture() (texture Texture) {
  120. var tex uint32
  121. gl.GenTextures(1, &tex)
  122. return Texture(tex)
  123. }
  124. func (c *esContext) DeleteBuffer(buffer Buffer) {
  125. gl.DeleteBuffers(1, (*uint32)(&buffer))
  126. }
  127. func (c *esContext) DeleteTexture(texture Texture) {
  128. tex := uint32(texture)
  129. gl.DeleteTextures(1, &tex)
  130. }
  131. func (c *esContext) Disable(capability uint32) {
  132. gl.Disable(capability)
  133. }
  134. func (c *esContext) DrawArrays(mode uint32, first, count int) {
  135. gl.DrawArrays(mode, int32(first), int32(count))
  136. }
  137. func (c *esContext) Enable(capability uint32) {
  138. gl.Enable(capability)
  139. }
  140. func (c *esContext) EnableVertexAttribArray(attribute Attribute) {
  141. gl.EnableVertexAttribArray(uint32(attribute))
  142. }
  143. func (c *esContext) GetAttribLocation(program Program, name string) Attribute {
  144. return Attribute(gl.GetAttribLocation(uint32(program), gl.Str(name+"\x00")))
  145. }
  146. func (c *esContext) GetError() uint32 {
  147. return gl.GetError()
  148. }
  149. func (c *esContext) GetProgrami(program Program, param uint32) int {
  150. var value int32
  151. gl.GetProgramiv(uint32(program), param, &value)
  152. return int(value)
  153. }
  154. func (c *esContext) GetProgramInfoLog(program Program) string {
  155. var logLength int32
  156. gl.GetProgramiv(uint32(program), gl.INFO_LOG_LENGTH, &logLength)
  157. info := strings.Repeat("\x00", int(logLength+1))
  158. gl.GetProgramInfoLog(uint32(program), logLength, nil, gl.Str(info))
  159. return info
  160. }
  161. func (c *esContext) GetShaderi(shader Shader, param uint32) int {
  162. var value int32
  163. gl.GetShaderiv(uint32(shader), param, &value)
  164. return int(value)
  165. }
  166. func (c *esContext) GetShaderInfoLog(shader Shader) string {
  167. var logLength int32
  168. gl.GetShaderiv(uint32(shader), gl.INFO_LOG_LENGTH, &logLength)
  169. info := strings.Repeat("\x00", int(logLength+1))
  170. gl.GetShaderInfoLog(uint32(shader), logLength, nil, gl.Str(info))
  171. return info
  172. }
  173. func (c *esContext) GetUniformLocation(program Program, name string) Uniform {
  174. return Uniform(gl.GetUniformLocation(uint32(program), gl.Str(name+"\x00")))
  175. }
  176. func (c *esContext) LinkProgram(program Program) {
  177. gl.LinkProgram(uint32(program))
  178. }
  179. func (c *esContext) ReadBuffer(src uint32) {
  180. gl.ReadBuffer(src)
  181. }
  182. func (c *esContext) ReadPixels(x, y, width, height int, colorFormat, typ uint32, pixels []uint8) {
  183. gl.ReadPixels(int32(x), int32(y), int32(width), int32(height), colorFormat, typ, gl.Ptr(pixels))
  184. }
  185. func (c *esContext) Scissor(x, y, w, h int32) {
  186. gl.Scissor(x, y, w, h)
  187. }
  188. func (c *esContext) ShaderSource(shader Shader, source string) {
  189. csources, free := gl.Strs(source + "\x00")
  190. defer free()
  191. gl.ShaderSource(uint32(shader), 1, csources, nil)
  192. }
  193. func (c *esContext) TexImage2D(target uint32, level, width, height int, colorFormat, typ uint32, data []uint8) {
  194. gl.TexImage2D(
  195. target,
  196. int32(level),
  197. int32(colorFormat),
  198. int32(width),
  199. int32(height),
  200. 0,
  201. colorFormat,
  202. typ,
  203. gl.Ptr(data),
  204. )
  205. }
  206. func (c *esContext) TexParameteri(target, param uint32, value int32) {
  207. gl.TexParameteri(target, param, value)
  208. }
  209. func (c *esContext) Uniform1f(uniform Uniform, v float32) {
  210. gl.Uniform1f(int32(uniform), v)
  211. }
  212. func (c *esContext) Uniform2f(uniform Uniform, v0, v1 float32) {
  213. gl.Uniform2f(int32(uniform), v0, v1)
  214. }
  215. func (c *esContext) Uniform4f(uniform Uniform, v0, v1, v2, v3 float32) {
  216. gl.Uniform4f(int32(uniform), v0, v1, v2, v3)
  217. }
  218. func (c *esContext) UseProgram(program Program) {
  219. gl.UseProgram(uint32(program))
  220. }
  221. func (c *esContext) VertexAttribPointerWithOffset(attribute Attribute, size int, typ uint32, normalized bool, stride, offset int) {
  222. gl.VertexAttribPointerWithOffset(uint32(attribute), int32(size), typ, normalized, int32(stride), uintptr(offset))
  223. }
  224. func (c *esContext) Viewport(x, y, width, height int) {
  225. gl.Viewport(int32(x), int32(y), int32(width), int32(height))
  226. }