gl_gomobile.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //go:build (android || ios || mobile) && (!js || !wasm || !test_web_driver)
  2. // +build android ios mobile
  3. // +build !js !wasm !test_web_driver
  4. package gl
  5. import (
  6. "encoding/binary"
  7. "fmt"
  8. "math"
  9. "fyne.io/fyne/v2/internal/driver/mobile/gl"
  10. )
  11. const (
  12. arrayBuffer = gl.ArrayBuffer
  13. bitColorBuffer = gl.ColorBufferBit
  14. bitDepthBuffer = gl.DepthBufferBit
  15. clampToEdge = gl.ClampToEdge
  16. colorFormatRGBA = gl.RGBA
  17. colorFormatR = singleChannelColorFormat
  18. compileStatus = gl.CompileStatus
  19. constantAlpha = gl.ConstantAlpha
  20. float = gl.Float
  21. fragmentShader = gl.FragmentShader
  22. front = gl.Front
  23. glFalse = gl.False
  24. linkStatus = gl.LinkStatus
  25. one = gl.One
  26. oneMinusConstantAlpha = gl.OneMinusConstantAlpha
  27. oneMinusSrcAlpha = gl.OneMinusSrcAlpha
  28. scissorTest = gl.ScissorTest
  29. srcAlpha = gl.SrcAlpha
  30. staticDraw = gl.StaticDraw
  31. texture0 = gl.Texture0
  32. texture2D = gl.Texture2D
  33. textureMinFilter = gl.TextureMinFilter
  34. textureMagFilter = gl.TextureMagFilter
  35. textureWrapS = gl.TextureWrapS
  36. textureWrapT = gl.TextureWrapT
  37. triangles = gl.Triangles
  38. triangleStrip = gl.TriangleStrip
  39. unpackAlignment = gl.UnpackAlignment
  40. unsignedByte = gl.UnsignedByte
  41. vertexShader = gl.VertexShader
  42. )
  43. type (
  44. // Attribute represents a GL attribute
  45. Attribute gl.Attrib
  46. // Buffer represents a GL buffer
  47. Buffer gl.Buffer
  48. // Program represents a compiled GL program
  49. Program gl.Program
  50. // Shader represents a GL shader
  51. Shader gl.Shader
  52. // Uniform represents a GL uniform
  53. Uniform gl.Uniform
  54. )
  55. var noBuffer = Buffer{}
  56. var noShader = Shader{}
  57. var textureFilterToGL = []int32{gl.Linear, gl.Nearest}
  58. func (p *painter) glctx() gl.Context {
  59. return p.contextProvider.Context().(gl.Context)
  60. }
  61. func (p *painter) Init() {
  62. p.ctx = &mobileContext{glContext: p.contextProvider.Context().(gl.Context)}
  63. p.glctx().Disable(gl.DepthTest)
  64. p.glctx().Enable(gl.Blend)
  65. p.program = p.createProgram("simple_es")
  66. p.singleChannelProgram = p.createProgram("single_channel_es")
  67. p.lineProgram = p.createProgram("line_es")
  68. }
  69. // f32Bytes returns the byte representation of float32 values in the given byte
  70. // order. byteOrder must be either binary.BigEndian or binary.LittleEndian.
  71. func f32Bytes(byteOrder binary.ByteOrder, values ...float32) []byte {
  72. le := false
  73. switch byteOrder {
  74. case binary.BigEndian:
  75. case binary.LittleEndian:
  76. le = true
  77. default:
  78. panic(fmt.Sprintf("invalid byte order %v", byteOrder))
  79. }
  80. b := make([]byte, 4*len(values))
  81. for i, v := range values {
  82. u := math.Float32bits(v)
  83. if le {
  84. b[4*i+0] = byte(u >> 0)
  85. b[4*i+1] = byte(u >> 8)
  86. b[4*i+2] = byte(u >> 16)
  87. b[4*i+3] = byte(u >> 24)
  88. } else {
  89. b[4*i+0] = byte(u >> 24)
  90. b[4*i+1] = byte(u >> 16)
  91. b[4*i+2] = byte(u >> 8)
  92. b[4*i+3] = byte(u >> 0)
  93. }
  94. }
  95. return b
  96. }
  97. type mobileContext struct {
  98. glContext gl.Context
  99. }
  100. var _ context = (*mobileContext)(nil)
  101. func (c *mobileContext) ActiveTexture(textureUnit uint32) {
  102. c.glContext.ActiveTexture(gl.Enum(textureUnit))
  103. }
  104. func (c *mobileContext) AttachShader(program Program, shader Shader) {
  105. c.glContext.AttachShader(gl.Program(program), gl.Shader(shader))
  106. }
  107. func (c *mobileContext) BindBuffer(target uint32, buf Buffer) {
  108. c.glContext.BindBuffer(gl.Enum(target), gl.Buffer(buf))
  109. }
  110. func (c *mobileContext) BindTexture(target uint32, texture Texture) {
  111. c.glContext.BindTexture(gl.Enum(target), gl.Texture(texture))
  112. }
  113. func (c *mobileContext) BlendColor(r, g, b, a float32) {
  114. c.glContext.BlendColor(r, g, b, a)
  115. }
  116. func (c *mobileContext) BlendFunc(srcFactor, destFactor uint32) {
  117. c.glContext.BlendFunc(gl.Enum(srcFactor), gl.Enum(destFactor))
  118. }
  119. func (c *mobileContext) BufferData(target uint32, points []float32, usage uint32) {
  120. data := f32Bytes(binary.LittleEndian, points...)
  121. c.glContext.BufferData(gl.Enum(target), data, gl.Enum(usage))
  122. }
  123. func (c *mobileContext) Clear(mask uint32) {
  124. c.glContext.Clear(gl.Enum(mask))
  125. }
  126. func (c *mobileContext) ClearColor(r, g, b, a float32) {
  127. c.glContext.ClearColor(r, g, b, a)
  128. }
  129. func (c *mobileContext) CompileShader(shader Shader) {
  130. c.glContext.CompileShader(gl.Shader(shader))
  131. }
  132. func (c *mobileContext) CreateBuffer() Buffer {
  133. return Buffer(c.glContext.CreateBuffer())
  134. }
  135. func (c *mobileContext) CreateProgram() Program {
  136. return Program(c.glContext.CreateProgram())
  137. }
  138. func (c *mobileContext) CreateShader(typ uint32) Shader {
  139. return Shader(c.glContext.CreateShader(gl.Enum(typ)))
  140. }
  141. func (c *mobileContext) CreateTexture() (texture Texture) {
  142. return Texture(c.glContext.CreateTexture())
  143. }
  144. func (c *mobileContext) DeleteBuffer(buffer Buffer) {
  145. c.glContext.DeleteBuffer(gl.Buffer(buffer))
  146. }
  147. func (c *mobileContext) DeleteTexture(texture Texture) {
  148. c.glContext.DeleteTexture(gl.Texture(texture))
  149. }
  150. func (c *mobileContext) Disable(capability uint32) {
  151. c.glContext.Disable(gl.Enum(capability))
  152. }
  153. func (c *mobileContext) DrawArrays(mode uint32, first, count int) {
  154. c.glContext.DrawArrays(gl.Enum(mode), first, count)
  155. }
  156. func (c *mobileContext) Enable(capability uint32) {
  157. c.glContext.Enable(gl.Enum(capability))
  158. }
  159. func (c *mobileContext) EnableVertexAttribArray(attribute Attribute) {
  160. c.glContext.EnableVertexAttribArray(gl.Attrib(attribute))
  161. }
  162. func (c *mobileContext) GetAttribLocation(program Program, name string) Attribute {
  163. return Attribute(c.glContext.GetAttribLocation(gl.Program(program), name))
  164. }
  165. func (c *mobileContext) GetError() uint32 {
  166. return uint32(c.glContext.GetError())
  167. }
  168. func (c *mobileContext) GetProgrami(program Program, param uint32) int {
  169. return c.glContext.GetProgrami(gl.Program(program), gl.Enum(param))
  170. }
  171. func (c *mobileContext) GetProgramInfoLog(program Program) string {
  172. return c.glContext.GetProgramInfoLog(gl.Program(program))
  173. }
  174. func (c *mobileContext) GetShaderi(shader Shader, param uint32) int {
  175. return c.glContext.GetShaderi(gl.Shader(shader), gl.Enum(param))
  176. }
  177. func (c *mobileContext) GetShaderInfoLog(shader Shader) string {
  178. return c.glContext.GetShaderInfoLog(gl.Shader(shader))
  179. }
  180. func (c *mobileContext) GetUniformLocation(program Program, name string) Uniform {
  181. return Uniform(c.glContext.GetUniformLocation(gl.Program(program), name))
  182. }
  183. func (c *mobileContext) LinkProgram(program Program) {
  184. c.glContext.LinkProgram(gl.Program(program))
  185. }
  186. func (c *mobileContext) PixelStorei(pname uint32, param int32) {
  187. c.glContext.PixelStorei(gl.Enum(pname), param)
  188. }
  189. func (c *mobileContext) ReadBuffer(_ uint32) {
  190. }
  191. func (c *mobileContext) ReadPixels(x, y, width, height int, colorFormat, typ uint32, pixels []uint8) {
  192. c.glContext.ReadPixels(pixels, x, y, width, height, gl.Enum(colorFormat), gl.Enum(typ))
  193. }
  194. func (c *mobileContext) Scissor(x, y, w, h int32) {
  195. c.glContext.Scissor(x, y, w, h)
  196. }
  197. func (c *mobileContext) ShaderSource(shader Shader, source string) {
  198. c.glContext.ShaderSource(gl.Shader(shader), source)
  199. }
  200. func (c *mobileContext) TexImage2D(target uint32, level, width, height int, colorFormat, typ uint32, data []uint8) {
  201. c.glContext.TexImage2D(
  202. gl.Enum(target),
  203. level,
  204. int(colorFormat),
  205. width,
  206. height,
  207. gl.Enum(colorFormat),
  208. gl.Enum(typ),
  209. data,
  210. )
  211. }
  212. func (c *mobileContext) TexParameteri(target, param uint32, value int32) {
  213. c.glContext.TexParameteri(gl.Enum(target), gl.Enum(param), int(value))
  214. }
  215. func (c *mobileContext) Uniform1f(uniform Uniform, v float32) {
  216. c.glContext.Uniform1f(gl.Uniform(uniform), v)
  217. }
  218. func (c *mobileContext) Uniform4f(uniform Uniform, v0, v1, v2, v3 float32) {
  219. c.glContext.Uniform4f(gl.Uniform(uniform), v0, v1, v2, v3)
  220. }
  221. func (c *mobileContext) UseProgram(program Program) {
  222. c.glContext.UseProgram(gl.Program(program))
  223. }
  224. func (c *mobileContext) VertexAttribPointerWithOffset(attribute Attribute, size int, typ uint32, normalized bool, stride, offset int) {
  225. c.glContext.VertexAttribPointer(gl.Attrib(attribute), size, gl.Enum(typ), normalized, stride, offset)
  226. }
  227. func (c *mobileContext) Viewport(x, y, width, height int) {
  228. c.glContext.Viewport(x, y, width, height)
  229. }