forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
GLContextUtils.cpp
502 lines (419 loc) · 17.3 KB
/
GLContextUtils.cpp
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
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "GLContext.h"
#include "mozilla/Preferences.h"
#include "mozilla/Assertions.h"
#include <stdint.h>
using namespace mozilla::gfx;
namespace mozilla {
namespace gl {
static const char kTexBlit_VertShaderSource[] = "\
attribute vec2 aPosition; \n\
\n\
varying vec2 vTexCoord; \n\
\n\
void main(void) { \n\
vTexCoord = aPosition; \n\
vec2 vertPos = aPosition * 2.0 - 1.0; \n\
gl_Position = vec4(vertPos, 0.0, 1.0); \n\
} \n\
";
static const char kTex2DBlit_FragShaderSource[] = "\
#ifdef GL_FRAGMENT_PRECISION_HIGH \n\
precision highp float; \n\
#else \n\
precision mediump float; \n\
#endif \n\
\n\
uniform sampler2D uTexUnit; \n\
\n\
varying vec2 vTexCoord; \n\
\n\
void main(void) { \n\
gl_FragColor = texture2D(uTexUnit, vTexCoord); \n\
} \n\
";
static const char kTex2DRectBlit_FragShaderSource[] = "\
#ifdef GL_FRAGMENT_PRECISION_HIGH \n\
precision highp float; \n\
#else \n\
precision mediump float; \n\
#endif \n\
\n\
uniform sampler2D uTexUnit; \n\
uniform vec2 uTexCoordMult; \n\
\n\
varying vec2 vTexCoord; \n\
\n\
void main(void) { \n\
gl_FragColor = texture2DRect(uTexUnit, \n\
vTexCoord * uTexCoordMult); \n\
} \n\
";
// Allowed to be destructive of state we restore in functions below.
bool
GLContext::InitTexQuadProgram(GLenum target)
{
MOZ_ASSERT(target == LOCAL_GL_TEXTURE_2D ||
target == LOCAL_GL_TEXTURE_RECTANGLE_ARB);
bool success = false;
GLuint *programPtr;
GLuint *fragShaderPtr;
const char* fragShaderSource;
if (target == LOCAL_GL_TEXTURE_2D) {
programPtr = &mTex2DBlit_Program;
fragShaderPtr = &mTex2DBlit_FragShader;
fragShaderSource = kTex2DBlit_FragShaderSource;
} else {
programPtr = &mTex2DRectBlit_Program;
fragShaderPtr = &mTex2DRectBlit_FragShader;
fragShaderSource = kTex2DRectBlit_FragShaderSource;
}
GLuint& program = *programPtr;
GLuint& fragShader = *fragShaderPtr;
// Use do-while(false) to let us break on failure
do {
if (program) {
// Already have it...
success = true;
break;
}
if (!mTexBlit_Buffer) {
/* CCW tri-strip:
* 2---3
* | \ |
* 0---1
*/
GLfloat verts[] = {
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f
};
MOZ_ASSERT(!mTexBlit_Buffer);
fGenBuffers(1, &mTexBlit_Buffer);
fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mTexBlit_Buffer);
const size_t vertsSize = sizeof(verts);
// Make sure we have a sane size.
MOZ_ASSERT(vertsSize >= 3 * sizeof(GLfloat));
fBufferData(LOCAL_GL_ARRAY_BUFFER, vertsSize, verts, LOCAL_GL_STATIC_DRAW);
}
if (!mTexBlit_VertShader) {
const char* vertShaderSource = kTexBlit_VertShaderSource;
mTexBlit_VertShader = fCreateShader(LOCAL_GL_VERTEX_SHADER);
fShaderSource(mTexBlit_VertShader, 1, &vertShaderSource, nullptr);
fCompileShader(mTexBlit_VertShader);
}
MOZ_ASSERT(!fragShader);
fragShader = fCreateShader(LOCAL_GL_FRAGMENT_SHADER);
fShaderSource(fragShader, 1, &fragShaderSource, nullptr);
fCompileShader(fragShader);
program = fCreateProgram();
fAttachShader(program, mTexBlit_VertShader);
fAttachShader(program, fragShader);
fBindAttribLocation(program, 0, "aPosition");
fLinkProgram(program);
if (DebugMode()) {
GLint status = 0;
fGetShaderiv(mTexBlit_VertShader, LOCAL_GL_COMPILE_STATUS, &status);
if (status != LOCAL_GL_TRUE) {
NS_ERROR("Vert shader compilation failed.");
GLint length = 0;
fGetShaderiv(mTexBlit_VertShader, LOCAL_GL_INFO_LOG_LENGTH, &length);
if (!length) {
printf_stderr("No shader info log available.\n");
break;
}
nsAutoArrayPtr<char> buffer(new char[length]);
fGetShaderInfoLog(mTexBlit_VertShader, length, nullptr, buffer);
printf_stderr("Shader info log (%d bytes): %s\n", length, buffer.get());
break;
}
status = 0;
fGetShaderiv(fragShader, LOCAL_GL_COMPILE_STATUS, &status);
if (status != LOCAL_GL_TRUE) {
NS_ERROR("Frag shader compilation failed.");
GLint length = 0;
fGetShaderiv(fragShader, LOCAL_GL_INFO_LOG_LENGTH, &length);
if (!length) {
printf_stderr("No shader info log available.\n");
break;
}
nsAutoArrayPtr<char> buffer(new char[length]);
fGetShaderInfoLog(fragShader, length, nullptr, buffer);
printf_stderr("Shader info log (%d bytes): %s\n", length, buffer.get());
break;
}
}
GLint status = 0;
fGetProgramiv(program, LOCAL_GL_LINK_STATUS, &status);
if (status != LOCAL_GL_TRUE) {
if (DebugMode()) {
NS_ERROR("Linking blit program failed.");
GLint length = 0;
fGetProgramiv(program, LOCAL_GL_INFO_LOG_LENGTH, &length);
if (!length) {
printf_stderr("No program info log available.\n");
break;
}
nsAutoArrayPtr<char> buffer(new char[length]);
fGetProgramInfoLog(program, length, nullptr, buffer);
printf_stderr("Program info log (%d bytes): %s\n", length, buffer.get());
}
break;
}
MOZ_ASSERT(fGetAttribLocation(program, "aPosition") == 0);
GLint texUnitLoc = fGetUniformLocation(program, "uTexUnit");
MOZ_ASSERT(texUnitLoc != -1, "uniform not found");
// Set uniforms here:
fUseProgram(program);
fUniform1i(texUnitLoc, 0);
success = true;
} while (false);
if (!success) {
NS_ERROR("Creating program for texture blit failed!");
// Clean up:
DeleteTexBlitProgram();
return false;
}
fUseProgram(program);
fEnableVertexAttribArray(0);
fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mTexBlit_Buffer);
fVertexAttribPointer(0,
2,
LOCAL_GL_FLOAT,
false,
0,
nullptr);
return true;
}
bool
GLContext::UseTexQuadProgram(GLenum target, const gfxIntSize& srcSize)
{
if (!InitTexQuadProgram(target)) {
return false;
}
if (target == LOCAL_GL_TEXTURE_RECTANGLE_ARB) {
GLint texCoordMultLoc = fGetUniformLocation(mTex2DRectBlit_Program, "uTexCoordMult");
MOZ_ASSERT(texCoordMultLoc != -1, "uniform not found");
fUniform2f(texCoordMultLoc, srcSize.width, srcSize.height);
}
return true;
}
void
GLContext::DeleteTexBlitProgram()
{
if (mTexBlit_Buffer) {
fDeleteBuffers(1, &mTexBlit_Buffer);
mTexBlit_Buffer = 0;
}
if (mTexBlit_VertShader) {
fDeleteShader(mTexBlit_VertShader);
mTexBlit_VertShader = 0;
}
if (mTex2DBlit_FragShader) {
fDeleteShader(mTex2DBlit_FragShader);
mTex2DBlit_FragShader = 0;
}
if (mTex2DRectBlit_FragShader) {
fDeleteShader(mTex2DRectBlit_FragShader);
mTex2DRectBlit_FragShader = 0;
}
if (mTex2DBlit_Program) {
fDeleteProgram(mTex2DBlit_Program);
mTex2DBlit_Program = 0;
}
if (mTex2DRectBlit_Program) {
fDeleteProgram(mTex2DRectBlit_Program);
mTex2DRectBlit_Program = 0;
}
}
void
GLContext::BlitFramebufferToFramebuffer(GLuint srcFB, GLuint destFB,
const gfxIntSize& srcSize,
const gfxIntSize& destSize)
{
MOZ_ASSERT(!srcFB || fIsFramebuffer(srcFB));
MOZ_ASSERT(!destFB || fIsFramebuffer(destFB));
MOZ_ASSERT(IsExtensionSupported(EXT_framebuffer_blit) ||
IsExtensionSupported(ANGLE_framebuffer_blit));
ScopedBindFramebuffer boundFB(this);
ScopedGLState scissor(this, LOCAL_GL_SCISSOR_TEST, false);
BindReadFB(srcFB);
BindDrawFB(destFB);
fBlitFramebuffer(0, 0, srcSize.width, srcSize.height,
0, 0, destSize.width, destSize.height,
LOCAL_GL_COLOR_BUFFER_BIT,
LOCAL_GL_NEAREST);
}
void
GLContext::BlitFramebufferToFramebuffer(GLuint srcFB, GLuint destFB,
const gfxIntSize& srcSize,
const gfxIntSize& destSize,
const GLFormats& srcFormats)
{
MOZ_ASSERT(!srcFB || fIsFramebuffer(srcFB));
MOZ_ASSERT(!destFB || fIsFramebuffer(destFB));
if (IsExtensionSupported(EXT_framebuffer_blit) ||
IsExtensionSupported(ANGLE_framebuffer_blit))
{
BlitFramebufferToFramebuffer(srcFB, destFB,
srcSize, destSize);
return;
}
GLuint tex = CreateTextureForOffscreen(srcFormats, srcSize);
MOZ_ASSERT(tex);
BlitFramebufferToTexture(srcFB, tex, srcSize, srcSize);
BlitTextureToFramebuffer(tex, destFB, srcSize, destSize);
fDeleteTextures(1, &tex);
}
void
GLContext::BlitTextureToFramebuffer(GLuint srcTex, GLuint destFB,
const gfxIntSize& srcSize,
const gfxIntSize& destSize,
GLenum srcTarget)
{
MOZ_ASSERT(fIsTexture(srcTex));
MOZ_ASSERT(!destFB || fIsFramebuffer(destFB));
if (IsExtensionSupported(EXT_framebuffer_blit) ||
IsExtensionSupported(ANGLE_framebuffer_blit))
{
ScopedFramebufferForTexture srcWrapper(this, srcTex, srcTarget);
MOZ_ASSERT(srcWrapper.IsComplete());
BlitFramebufferToFramebuffer(srcWrapper.FB(), destFB,
srcSize, destSize);
return;
}
ScopedBindFramebuffer boundFB(this, destFB);
// UseTexQuadProgram initializes a shader that reads
// from texture unit 0.
ScopedBindTextureUnit boundTU(this, LOCAL_GL_TEXTURE0);
ScopedBindTexture boundTex(this, srcTex, srcTarget);
GLuint boundProgram = 0;
GetUIntegerv(LOCAL_GL_CURRENT_PROGRAM, &boundProgram);
GLuint boundBuffer = 0;
GetUIntegerv(LOCAL_GL_ARRAY_BUFFER_BINDING, &boundBuffer);
/*
* fGetVertexAttribiv takes:
* VERTEX_ATTRIB_ARRAY_ENABLED
* VERTEX_ATTRIB_ARRAY_SIZE,
* VERTEX_ATTRIB_ARRAY_STRIDE,
* VERTEX_ATTRIB_ARRAY_TYPE,
* VERTEX_ATTRIB_ARRAY_NORMALIZED,
* VERTEX_ATTRIB_ARRAY_BUFFER_BINDING,
* CURRENT_VERTEX_ATTRIB
*
* CURRENT_VERTEX_ATTRIB is vertex shader state. \o/
* Others appear to be vertex array state,
* or alternatively in the internal vertex array state
* for a buffer object.
*/
GLint attrib0_enabled = 0;
GLint attrib0_size = 0;
GLint attrib0_stride = 0;
GLint attrib0_type = 0;
GLint attrib0_normalized = 0;
GLint attrib0_bufferBinding = 0;
void* attrib0_pointer = nullptr;
fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_ENABLED, &attrib0_enabled);
fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_SIZE, &attrib0_size);
fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_STRIDE, &attrib0_stride);
fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_TYPE, &attrib0_type);
fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &attrib0_normalized);
fGetVertexAttribiv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &attrib0_bufferBinding);
fGetVertexAttribPointerv(0, LOCAL_GL_VERTEX_ATTRIB_ARRAY_POINTER, &attrib0_pointer);
// Note that uniform values are program state, so we don't need to rebind those.
ScopedGLState blend (this, LOCAL_GL_BLEND, false);
ScopedGLState cullFace (this, LOCAL_GL_CULL_FACE, false);
ScopedGLState depthTest (this, LOCAL_GL_DEPTH_TEST, false);
ScopedGLState dither (this, LOCAL_GL_DITHER, false);
ScopedGLState polyOffsFill(this, LOCAL_GL_POLYGON_OFFSET_FILL, false);
ScopedGLState sampleAToC (this, LOCAL_GL_SAMPLE_ALPHA_TO_COVERAGE, false);
ScopedGLState sampleCover (this, LOCAL_GL_SAMPLE_COVERAGE, false);
ScopedGLState scissor (this, LOCAL_GL_SCISSOR_TEST, false);
ScopedGLState stencil (this, LOCAL_GL_STENCIL_TEST, false);
realGLboolean colorMask[4];
fGetBooleanv(LOCAL_GL_COLOR_WRITEMASK, colorMask);
fColorMask(LOCAL_GL_TRUE,
LOCAL_GL_TRUE,
LOCAL_GL_TRUE,
LOCAL_GL_TRUE);
GLint viewport[4];
fGetIntegerv(LOCAL_GL_VIEWPORT, viewport);
fViewport(0, 0, destSize.width, destSize.height);
// Does destructive things to (only!) what we just saved above.
bool good = UseTexQuadProgram(srcTarget, srcSize);
if (!good) {
// We're up against the wall, so bail.
// This should really be MOZ_CRASH(why) or MOZ_RUNTIME_ASSERT(good).
printf_stderr("[%s:%d] Fatal Error: Failed to prepare to blit texture->framebuffer.\n",
__FILE__, __LINE__);
MOZ_CRASH();
}
fDrawArrays(LOCAL_GL_TRIANGLE_STRIP, 0, 4);
fViewport(viewport[0], viewport[1],
viewport[2], viewport[3]);
fColorMask(colorMask[0],
colorMask[1],
colorMask[2],
colorMask[3]);
if (attrib0_enabled)
fEnableVertexAttribArray(0);
fBindBuffer(LOCAL_GL_ARRAY_BUFFER, attrib0_bufferBinding);
fVertexAttribPointer(0,
attrib0_size,
attrib0_type,
attrib0_normalized,
attrib0_stride,
attrib0_pointer);
fBindBuffer(LOCAL_GL_ARRAY_BUFFER, boundBuffer);
fUseProgram(boundProgram);
}
void
GLContext::BlitFramebufferToTexture(GLuint srcFB, GLuint destTex,
const gfxIntSize& srcSize,
const gfxIntSize& destSize,
GLenum destTarget)
{
MOZ_ASSERT(!srcFB || fIsFramebuffer(srcFB));
MOZ_ASSERT(fIsTexture(destTex));
if (IsExtensionSupported(EXT_framebuffer_blit) ||
IsExtensionSupported(ANGLE_framebuffer_blit))
{
ScopedFramebufferForTexture destWrapper(this, destTex, destTarget);
BlitFramebufferToFramebuffer(srcFB, destWrapper.FB(),
srcSize, destSize);
return;
}
ScopedBindTexture autoTex(this, destTex, destTarget);
ScopedBindFramebuffer boundFB(this, srcFB);
ScopedGLState scissor(this, LOCAL_GL_SCISSOR_TEST, false);
fCopyTexSubImage2D(destTarget, 0,
0, 0,
0, 0,
srcSize.width, srcSize.height);
}
void
GLContext::BlitTextureToTexture(GLuint srcTex, GLuint destTex,
const gfxIntSize& srcSize,
const gfxIntSize& destSize,
GLenum srcTarget, GLenum destTarget)
{
MOZ_ASSERT(fIsTexture(srcTex));
MOZ_ASSERT(fIsTexture(destTex));
if (mTexBlit_UseDrawNotCopy) {
// Draw is texture->framebuffer
ScopedFramebufferForTexture destWrapper(this, destTex, destTarget);
BlitTextureToFramebuffer(srcTex, destWrapper.FB(),
srcSize, destSize, srcTarget);
return;
}
// Generally, just use the CopyTexSubImage path
ScopedFramebufferForTexture srcWrapper(this, srcTex, srcTarget);
BlitFramebufferToTexture(srcWrapper.FB(), destTex,
srcSize, destSize, destTarget);
}
} /* namespace gl */
} /* namespace mozilla */