-
Notifications
You must be signed in to change notification settings - Fork 60
/
WebGLFrameSink.js
525 lines (457 loc) · 16.4 KB
/
WebGLFrameSink.js
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
Copyright (c) 2014-2024 Brooke Vibber <bvibber@pobox.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function() {
"use strict";
var FrameSink = require('./FrameSink.js'),
shaders = require('../build/shaders.js');
/**
* Warning: canvas must not have been used for 2d drawing prior!
*
* @param {HTMLCanvasElement} canvas - HTML canvas element to attach to
* @constructor
*/
function WebGLFrameSink(canvas) {
var self = this,
gl = WebGLFrameSink.contextForCanvas(canvas),
debug = false; // swap this to enable more error checks, which can slow down rendering
if (gl === null) {
throw new Error('WebGL unavailable');
}
// GL!
function checkError() {
if (debug) {
err = gl.getError();
if (err !== 0) {
throw new Error("GL error " + err);
}
}
}
function compileShader(type, source) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
var err = gl.getShaderInfoLog(shader);
gl.deleteShader(shader);
throw new Error('GL shader compilation for ' + type + ' failed: ' + err);
}
return shader;
}
var program,
unpackProgram,
err;
// In the world of GL there are no rectangles.
// There are only triangles.
// THERE IS NO SPOON.
var rectangle = new Float32Array([
// First triangle (top left, clockwise)
-1.0, -1.0,
+1.0, -1.0,
-1.0, +1.0,
// Second triangle (bottom right, clockwise)
-1.0, +1.0,
+1.0, -1.0,
+1.0, +1.0
]);
var textures = {};
var framebuffers = {};
var stripes = {};
var buf, positionLocation, unpackPositionLocation;
var unpackTexturePositionBuffer, unpackTexturePositionLocation;
var stripeLocation, unpackTextureLocation;
var lumaPositionBuffer, lumaPositionLocation;
var chromaPositionBuffer, chromaPositionLocation;
function createOrReuseTexture(name, formatUpdate) {
if (!textures[name] || formatUpdate) {
textures[name] = gl.createTexture();
}
return textures[name];
}
function uploadTexture(name, formatUpdate, width, height, data) {
var create = !textures[name] || formatUpdate;
var texture = createOrReuseTexture(name, formatUpdate);
gl.activeTexture(gl.TEXTURE0);
if (WebGLFrameSink.stripe) {
var uploadTemp = !textures[name + '_temp'] || formatUpdate;
var tempTexture = createOrReuseTexture(name + '_temp', formatUpdate);
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
if (uploadTemp) {
// new texture
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);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texImage2D(
gl.TEXTURE_2D,
0, // mip level
gl.RGBA, // internal format
width / 4,
height,
0, // border
gl.RGBA, // format
gl.UNSIGNED_BYTE, // type
data // data!
);
} else {
// update texture
gl.texSubImage2D(
gl.TEXTURE_2D,
0, // mip level
0, // x offset
0, // y offset
width / 4,
height,
gl.RGBA, // format
gl.UNSIGNED_BYTE, // type
data // data!
);
}
var stripeTexture = textures[name + '_stripe'];
var uploadStripe = !stripeTexture || formatUpdate;
if (uploadStripe) {
stripeTexture = createOrReuseTexture(name + '_stripe', formatUpdate);
}
gl.bindTexture(gl.TEXTURE_2D, stripeTexture);
if (uploadStripe) {
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);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texImage2D(
gl.TEXTURE_2D,
0, // mip level
gl.RGBA, // internal format
width,
1,
0, // border
gl.RGBA, // format
gl.UNSIGNED_BYTE, //type
buildStripe(width, 1) // data!
);
}
} else {
gl.bindTexture(gl.TEXTURE_2D, texture);
if (create) {
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);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texImage2D(
gl.TEXTURE_2D,
0, // mip level
gl.ALPHA, // internal format
width,
height,
0, // border
gl.ALPHA, // format
gl.UNSIGNED_BYTE, //type
data // data!
);
} else {
gl.texSubImage2D(
gl.TEXTURE_2D,
0, // mip level
0, // x
0, // y
width,
height,
gl.ALPHA, // internal format
gl.UNSIGNED_BYTE, //type
data // data!
);
}
}
}
function unpackTexture(name, formatUpdate, width, height) {
var texture = textures[name];
// Upload to a temporary RGBA texture, then unpack it.
// This is faster than CPU-side swizzling in ANGLE on Windows.
gl.useProgram(unpackProgram);
var fb = framebuffers[name];
if (!fb || formatUpdate) {
// Create a framebuffer and an empty target size
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
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);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texImage2D(
gl.TEXTURE_2D,
0, // mip level
gl.RGBA, // internal format
width,
height,
0, // border
gl.RGBA, // format
gl.UNSIGNED_BYTE, //type
null // data!
);
fb = framebuffers[name] = gl.createFramebuffer();
}
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
var tempTexture = textures[name + '_temp'];
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
gl.uniform1i(unpackTextureLocation, 1);
var stripeTexture = textures[name + '_stripe'];
gl.activeTexture(gl.TEXTURE2);
gl.bindTexture(gl.TEXTURE_2D, stripeTexture);
gl.uniform1i(stripeLocation, 2);
// Rectangle geometry
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
// Set up the texture geometry...
gl.bindBuffer(gl.ARRAY_BUFFER, unpackTexturePositionBuffer);
gl.enableVertexAttribArray(unpackTexturePositionLocation);
gl.vertexAttribPointer(unpackTexturePositionLocation, 2, gl.FLOAT, false, 0, 0);
// Draw into the target texture...
gl.viewport(0, 0, width, height);
gl.drawArrays(gl.TRIANGLES, 0, rectangle.length / 2);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
function attachTexture(name, register, index) {
gl.activeTexture(register);
gl.bindTexture(gl.TEXTURE_2D, textures[name]);
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);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.uniform1i(gl.getUniformLocation(program, name), index);
}
function buildStripe(width) {
if (stripes[width]) {
return stripes[width];
}
var len = width,
out = new Uint32Array(len);
for (var i = 0; i < len; i += 4) {
out[i ] = 0x000000ff;
out[i + 1] = 0x0000ff00;
out[i + 2] = 0x00ff0000;
out[i + 3] = 0xff000000;
}
return stripes[width] = new Uint8Array(out.buffer);
}
function initProgram(vertexShaderSource, fragmentShaderSource) {
var vertexShader = compileShader(gl.VERTEX_SHADER, vertexShaderSource);
var fragmentShader = compileShader(gl.FRAGMENT_SHADER, fragmentShaderSource);
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
var err = gl.getProgramInfoLog(program);
gl.deleteProgram(program);
throw new Error('GL program linking failed: ' + err);
}
return program;
}
function init() {
if (WebGLFrameSink.stripe) {
unpackProgram = initProgram(shaders.vertexStripe, shaders.fragmentStripe);
unpackPositionLocation = gl.getAttribLocation(unpackProgram, 'aPosition');
unpackTexturePositionBuffer = gl.createBuffer();
var textureRectangle = new Float32Array([
0, 0,
1, 0,
0, 1,
0, 1,
1, 0,
1, 1
]);
gl.bindBuffer(gl.ARRAY_BUFFER, unpackTexturePositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, textureRectangle, gl.STATIC_DRAW);
unpackTexturePositionLocation = gl.getAttribLocation(unpackProgram, 'aTexturePosition');
stripeLocation = gl.getUniformLocation(unpackProgram, 'uStripe');
unpackTextureLocation = gl.getUniformLocation(unpackProgram, 'uTexture');
}
program = initProgram(shaders.vertex, shaders.fragment);
buf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, rectangle, gl.STATIC_DRAW);
positionLocation = gl.getAttribLocation(program, 'aPosition');
lumaPositionBuffer = gl.createBuffer();
lumaPositionLocation = gl.getAttribLocation(program, 'aLumaPosition');
chromaPositionBuffer = gl.createBuffer();
chromaPositionLocation = gl.getAttribLocation(program, 'aChromaPosition');
}
/**
* Actually draw a frame.
* @param {YUVFrame} buffer - YUV frame buffer object
*/
self.drawFrame = function(buffer) {
var format = buffer.format;
var formatUpdate = (!program || canvas.width !== format.displayWidth || canvas.height !== format.displayHeight);
if (formatUpdate) {
// Keep the canvas at the right size...
canvas.width = format.displayWidth;
canvas.height = format.displayHeight;
self.clear();
}
if (!program) {
init();
}
if (formatUpdate) {
var setupTexturePosition = function(buffer, location, texWidth) {
// Warning: assumes that the stride for Cb and Cr is the same size in output pixels
var textureX0 = format.cropLeft / texWidth;
var textureX1 = (format.cropLeft + format.cropWidth) / texWidth;
var textureY0 = (format.cropTop + format.cropHeight) / format.height;
var textureY1 = format.cropTop / format.height;
var textureRectangle = new Float32Array([
textureX0, textureY0,
textureX1, textureY0,
textureX0, textureY1,
textureX0, textureY1,
textureX1, textureY0,
textureX1, textureY1
]);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, textureRectangle, gl.STATIC_DRAW);
};
setupTexturePosition(
lumaPositionBuffer,
lumaPositionLocation,
buffer.y.stride);
setupTexturePosition(
chromaPositionBuffer,
chromaPositionLocation,
buffer.u.stride * format.width / format.chromaWidth);
}
// Create or update the textures...
uploadTexture('uTextureY', formatUpdate, buffer.y.stride, format.height, buffer.y.bytes);
uploadTexture('uTextureCb', formatUpdate, buffer.u.stride, format.chromaHeight, buffer.u.bytes);
uploadTexture('uTextureCr', formatUpdate, buffer.v.stride, format.chromaHeight, buffer.v.bytes);
if (WebGLFrameSink.stripe) {
// Unpack the textures after upload to avoid blocking on GPU
unpackTexture('uTextureY', formatUpdate, buffer.y.stride, format.height);
unpackTexture('uTextureCb', formatUpdate, buffer.u.stride, format.chromaHeight);
unpackTexture('uTextureCr', formatUpdate, buffer.v.stride, format.chromaHeight);
}
// Set up the rectangle and draw it
gl.useProgram(program);
gl.viewport(0, 0, canvas.width, canvas.height);
attachTexture('uTextureY', gl.TEXTURE0, 0);
attachTexture('uTextureCb', gl.TEXTURE1, 1);
attachTexture('uTextureCr', gl.TEXTURE2, 2);
// Set up geometry
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
// Set up the texture geometry...
gl.bindBuffer(gl.ARRAY_BUFFER, lumaPositionBuffer);
gl.enableVertexAttribArray(lumaPositionLocation);
gl.vertexAttribPointer(lumaPositionLocation, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, chromaPositionBuffer);
gl.enableVertexAttribArray(chromaPositionLocation);
gl.vertexAttribPointer(chromaPositionLocation, 2, gl.FLOAT, false, 0, 0);
// Aaaaand draw stuff.
gl.drawArrays(gl.TRIANGLES, 0, rectangle.length / 2);
};
self.clear = function() {
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0.0, 0.0, 0.0, 0.0);
gl.clear(gl.COLOR_BUFFER_BIT);
};
self.clear();
return self;
}
// Optional performance hack for Windows; luminance and alpha textures are
// ssllooww to upload on some machines, so we pack into RGBA and unpack in
// the shaders.
//
// Some browsers / GPUs seem to have no problem with this, others have
// a huge impact in CPU doing the texture uploads.
//
// For instance on macOS 12.2 on a MacBook Pro 2018 with AMD GPU it
// can real down at high res. This is partially compensated for by
// improving the upload-vs-update behavior for the alpha textures.
//
// Currently keeping it off as of April 2022, but leaving it in so it
// can be enabled if desired.
WebGLFrameSink.stripe = false;
WebGLFrameSink.contextForCanvas = function(canvas) {
var options = {
// Don't trigger discrete GPU in multi-GPU systems
preferLowPowerToHighPerformance: true,
powerPreference: 'low-power',
// Don't try to use software GL rendering!
failIfMajorPerformanceCaveat: true,
// In case we need to capture the resulting output.
preserveDrawingBuffer: true
};
return canvas.getContext('webgl', options) || canvas.getContext('experimental-webgl', options);
};
/**
* Static function to check if WebGL will be available with appropriate features.
*
* @returns {boolean} - true if available
*/
WebGLFrameSink.isAvailable = function() {
var canvas = document.createElement('canvas'),
gl;
canvas.width = 1;
canvas.height = 1;
try {
gl = WebGLFrameSink.contextForCanvas(canvas);
} catch (e) {
return false;
}
if (gl) {
var register = gl.TEXTURE0,
width = 4,
height = 4,
texture = gl.createTexture(),
data = new Uint8Array(width * height),
texWidth = WebGLFrameSink.stripe ? (width / 4) : width,
format = WebGLFrameSink.stripe ? gl.RGBA : gl.ALPHA,
filter = WebGLFrameSink.stripe ? gl.NEAREST : gl.LINEAR;
gl.activeTexture(register);
gl.bindTexture(gl.TEXTURE_2D, texture);
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);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
gl.texImage2D(
gl.TEXTURE_2D,
0, // mip level
format, // internal format
texWidth,
height,
0, // border
format, // format
gl.UNSIGNED_BYTE, //type
data // data!
);
var err = gl.getError();
if (err) {
// Doesn't support alpha textures?
return false;
} else {
return true;
}
} else {
return false;
}
};
WebGLFrameSink.prototype = Object.create(FrameSink.prototype);
module.exports = WebGLFrameSink;
})();