/
context_desktop.go
502 lines (452 loc) · 12.5 KB
/
context_desktop.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright 2014 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build darwin freebsd linux windows
// +build !js
// +build !android
// +build !ios
package opengl
import (
"errors"
"fmt"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl/gl"
"github.com/hajimehoshi/ebiten/internal/mainthread"
)
type (
textureNative uint32
framebufferNative uint32
shader uint32
program uint32
buffer uint32
)
var InvalidTexture textureNative
type (
uniformLocation int32
attribLocation int32
)
type programID uint32
const (
invalidTexture = 0
invalidFramebuffer = (1 << 32) - 1
)
func getProgramID(p program) programID {
return programID(p)
}
const (
vertexShader = shaderType(gl.VERTEX_SHADER)
fragmentShader = shaderType(gl.FRAGMENT_SHADER)
arrayBuffer = bufferType(gl.ARRAY_BUFFER)
elementArrayBuffer = bufferType(gl.ELEMENT_ARRAY_BUFFER)
dynamicDraw = bufferUsage(gl.DYNAMIC_DRAW)
short = dataType(gl.SHORT)
float = dataType(gl.FLOAT)
zero = operation(gl.ZERO)
one = operation(gl.ONE)
srcAlpha = operation(gl.SRC_ALPHA)
dstAlpha = operation(gl.DST_ALPHA)
oneMinusSrcAlpha = operation(gl.ONE_MINUS_SRC_ALPHA)
oneMinusDstAlpha = operation(gl.ONE_MINUS_DST_ALPHA)
)
type contextImpl struct {
init bool
}
func (c *context) reset() error {
if err := mainthread.Run(func() error {
if c.init {
return nil
}
// Note that this initialization must be done after Loop is called.
if err := gl.Init(); err != nil {
return fmt.Errorf("opengl: initializing error %v", err)
}
c.init = true
return nil
}); err != nil {
return err
}
c.locationCache = newLocationCache()
c.lastTexture = invalidTexture
c.lastFramebuffer = invalidFramebuffer
c.lastViewportWidth = 0
c.lastViewportHeight = 0
c.lastCompositeMode = graphics.CompositeModeUnknown
_ = mainthread.Run(func() error {
gl.Enable(gl.BLEND)
return nil
})
c.blendFunc(graphics.CompositeModeSourceOver)
_ = mainthread.Run(func() error {
f := int32(0)
gl.GetIntegerv(gl.FRAMEBUFFER_BINDING, &f)
c.screenFramebuffer = framebufferNative(f)
return nil
})
return nil
}
func (c *context) blendFunc(mode graphics.CompositeMode) {
_ = mainthread.Run(func() error {
if c.lastCompositeMode == mode {
return nil
}
c.lastCompositeMode = mode
s, d := mode.Operations()
s2, d2 := convertOperation(s), convertOperation(d)
gl.BlendFunc(uint32(s2), uint32(d2))
return nil
})
}
func (c *context) newTexture(width, height int) (textureNative, error) {
var texture textureNative
if err := mainthread.Run(func() error {
var t uint32
gl.GenTextures(1, &t)
// TODO: Use gl.IsTexture
if t <= 0 {
return errors.New("opengl: creating texture failed")
}
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
texture = textureNative(t)
return nil
}); err != nil {
return 0, err
}
c.bindTexture(texture)
_ = mainthread.Run(func() error {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
// If data is nil, this just allocates memory and the content is undefined.
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
return nil
})
return texture, nil
}
func (c *context) bindFramebufferImpl(f framebufferNative) {
_ = mainthread.Run(func() error {
gl.BindFramebufferEXT(gl.FRAMEBUFFER, uint32(f))
return nil
})
}
func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) {
var pixels []byte
_ = mainthread.Run(func() error {
gl.Flush()
return nil
})
c.bindFramebuffer(f.native)
if err := mainthread.Run(func() error {
pixels = make([]byte, 4*width*height)
gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels))
return nil
}); err != nil {
return nil, err
}
return pixels, nil
}
func (c *context) bindTextureImpl(t textureNative) {
_ = mainthread.Run(func() error {
gl.BindTexture(gl.TEXTURE_2D, uint32(t))
return nil
})
}
func (c *context) deleteTexture(t textureNative) {
_ = mainthread.Run(func() error {
tt := uint32(t)
if !gl.IsTexture(tt) {
return nil
}
if c.lastTexture == t {
c.lastTexture = invalidTexture
}
gl.DeleteTextures(1, &tt)
return nil
})
}
func (c *context) isTexture(t textureNative) bool {
r := false
_ = mainthread.Run(func() error {
r = gl.IsTexture(uint32(t))
return nil
})
return r
}
func (c *context) texSubImage2D(t textureNative, p []byte, x, y, width, height int) {
c.bindTexture(t)
_ = mainthread.Run(func() error {
gl.TexSubImage2D(gl.TEXTURE_2D, 0, int32(x), int32(y), int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))
return nil
})
}
func (c *context) newFramebuffer(texture textureNative) (framebufferNative, error) {
var framebuffer framebufferNative
var f uint32
if err := mainthread.Run(func() error {
gl.GenFramebuffersEXT(1, &f)
// TODO: Use gl.IsFramebuffer
if f <= 0 {
return errors.New("opengl: creating framebuffer failed: gl.IsFramebuffer returns false")
}
return nil
}); err != nil {
return 0, err
}
c.bindFramebuffer(framebufferNative(f))
if err := mainthread.Run(func() error {
gl.FramebufferTexture2DEXT(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, uint32(texture), 0)
s := gl.CheckFramebufferStatusEXT(gl.FRAMEBUFFER)
if s != gl.FRAMEBUFFER_COMPLETE {
if s != 0 {
return fmt.Errorf("opengl: creating framebuffer failed: %v", s)
}
if e := gl.GetError(); e != gl.NO_ERROR {
return fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e)
}
return fmt.Errorf("opengl: creating framebuffer failed: unknown error")
}
framebuffer = framebufferNative(f)
return nil
}); err != nil {
return 0, err
}
return framebuffer, nil
}
func (c *context) setViewportImpl(width, height int) {
_ = mainthread.Run(func() error {
gl.Viewport(0, 0, int32(width), int32(height))
return nil
})
}
func (c *context) deleteFramebuffer(f framebufferNative) {
_ = mainthread.Run(func() error {
ff := uint32(f)
if !gl.IsFramebufferEXT(ff) {
return nil
}
if c.lastFramebuffer == f {
c.lastFramebuffer = invalidFramebuffer
c.lastViewportWidth = 0
c.lastViewportHeight = 0
}
gl.DeleteFramebuffersEXT(1, &ff)
return nil
})
}
func (c *context) newShader(shaderType shaderType, source string) (shader, error) {
var sh shader
if err := mainthread.Run(func() error {
s := gl.CreateShader(uint32(shaderType))
if s == 0 {
return fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
}
cSources, free := gl.Strs(source + "\x00")
gl.ShaderSource(uint32(s), 1, cSources, nil)
free()
gl.CompileShader(s)
var v int32
gl.GetShaderiv(s, gl.COMPILE_STATUS, &v)
if v == gl.FALSE {
log := []byte{}
gl.GetShaderiv(uint32(s), gl.INFO_LOG_LENGTH, &v)
if v != 0 {
log = make([]byte, int(v))
gl.GetShaderInfoLog(uint32(s), v, nil, (*uint8)(gl.Ptr(log)))
}
return fmt.Errorf("opengl: shader compile failed: %s", log)
}
sh = shader(s)
return nil
}); err != nil {
return 0, err
}
return sh, nil
}
func (c *context) deleteShader(s shader) {
_ = mainthread.Run(func() error {
gl.DeleteShader(uint32(s))
return nil
})
}
func (c *context) newProgram(shaders []shader, attributes []string) (program, error) {
var pr program
if err := mainthread.Run(func() error {
p := gl.CreateProgram()
if p == 0 {
return errors.New("opengl: glCreateProgram failed")
}
for _, shader := range shaders {
gl.AttachShader(p, uint32(shader))
}
for i, name := range attributes {
l, free := gl.Strs(name + "\x00")
gl.BindAttribLocation(p, uint32(i), *l)
free()
}
gl.LinkProgram(p)
var v int32
gl.GetProgramiv(p, gl.LINK_STATUS, &v)
if v == gl.FALSE {
return errors.New("opengl: program error")
}
pr = program(p)
return nil
}); err != nil {
return 0, err
}
return pr, nil
}
func (c *context) useProgram(p program) {
_ = mainthread.Run(func() error {
gl.UseProgram(uint32(p))
return nil
})
}
func (c *context) deleteProgram(p program) {
_ = mainthread.Run(func() error {
if !gl.IsProgram(uint32(p)) {
return nil
}
gl.DeleteProgram(uint32(p))
return nil
})
}
func (c *context) getUniformLocationImpl(p program, location string) uniformLocation {
l, free := gl.Strs(location + "\x00")
uniform := uniformLocation(gl.GetUniformLocation(uint32(p), *l))
free()
if uniform == -1 {
panic("opengl: invalid uniform location: " + location)
}
return uniform
}
func (c *context) uniformInt(p program, location string, v int) {
_ = mainthread.Run(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location))
gl.Uniform1i(l, int32(v))
return nil
})
}
func (c *context) uniformFloat(p program, location string, v float32) {
_ = mainthread.Run(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location))
gl.Uniform1f(l, v)
return nil
})
}
func (c *context) uniformFloats(p program, location string, v []float32) {
_ = mainthread.Run(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location))
switch len(v) {
case 2:
gl.Uniform2fv(l, 1, (*float32)(gl.Ptr(v)))
case 4:
gl.Uniform4fv(l, 1, (*float32)(gl.Ptr(v)))
case 16:
gl.UniformMatrix4fv(l, 1, false, (*float32)(gl.Ptr(v)))
default:
panic(fmt.Sprintf("opengl: invalid uniform floats num: %d", len(v)))
}
return nil
})
}
func (c *context) vertexAttribPointer(p program, index int, size int, dataType dataType, stride int, offset int) {
_ = mainthread.Run(func() error {
gl.VertexAttribPointer(uint32(index), int32(size), uint32(dataType), false, int32(stride), gl.PtrOffset(offset))
return nil
})
}
func (c *context) enableVertexAttribArray(p program, index int) {
_ = mainthread.Run(func() error {
gl.EnableVertexAttribArray(uint32(index))
return nil
})
}
func (c *context) disableVertexAttribArray(p program, index int) {
_ = mainthread.Run(func() error {
gl.DisableVertexAttribArray(uint32(index))
return nil
})
}
func (c *context) newArrayBuffer(size int) buffer {
var bf buffer
_ = mainthread.Run(func() error {
var b uint32
gl.GenBuffers(1, &b)
gl.BindBuffer(uint32(arrayBuffer), b)
gl.BufferData(uint32(arrayBuffer), size, nil, uint32(dynamicDraw))
bf = buffer(b)
return nil
})
return bf
}
func (c *context) newElementArrayBuffer(size int) buffer {
var bf buffer
_ = mainthread.Run(func() error {
var b uint32
gl.GenBuffers(1, &b)
gl.BindBuffer(uint32(elementArrayBuffer), b)
gl.BufferData(uint32(elementArrayBuffer), size, nil, uint32(dynamicDraw))
bf = buffer(b)
return nil
})
return bf
}
func (c *context) bindBuffer(bufferType bufferType, b buffer) {
_ = mainthread.Run(func() error {
gl.BindBuffer(uint32(bufferType), uint32(b))
return nil
})
}
func (c *context) arrayBufferSubData(data []float32) {
_ = mainthread.Run(func() error {
gl.BufferSubData(uint32(arrayBuffer), 0, len(data)*4, gl.Ptr(data))
return nil
})
}
func (c *context) elementArrayBufferSubData(data []uint16) {
_ = mainthread.Run(func() error {
gl.BufferSubData(uint32(elementArrayBuffer), 0, len(data)*2, gl.Ptr(data))
return nil
})
}
func (c *context) deleteBuffer(b buffer) {
_ = mainthread.Run(func() error {
bb := uint32(b)
gl.DeleteBuffers(1, &bb)
return nil
})
}
func (c *context) drawElements(len int, offsetInBytes int) {
_ = mainthread.Run(func() error {
gl.DrawElements(gl.TRIANGLES, int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(offsetInBytes))
return nil
})
}
func (c *context) maxTextureSizeImpl() int {
size := 0
_ = mainthread.Run(func() error {
s := int32(0)
gl.GetIntegerv(gl.MAX_TEXTURE_SIZE, &s)
size = int(s)
return nil
})
return size
}
func (c *context) flush() {
_ = mainthread.Run(func() error {
gl.Flush()
return nil
})
}