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