gl_gomobile.go 8.1 KB

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