gl_es.go 7.2 KB

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