forked from phoboslab/jsmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgl.js
277 lines (218 loc) · 7.15 KB
/
webgl.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
JSMpeg.Renderer.WebGL = (function(){ "use strict";
var WebGLRenderer = function(options) {
this.canvas = options.canvas || document.createElement('canvas');
this.width = this.canvas.width;
this.height = this.canvas.height;
this.enabled = true;
this.hasTextureData = {};
var contextCreateOptions = {
preserveDrawingBuffer: !!options.preserveDrawingBuffer,
alpha: false,
depth: false,
stencil: false,
antialias: false,
premultipliedAlpha: false
};
this.gl =
this.canvas.getContext('webgl', contextCreateOptions) ||
this.canvas.getContext('experimental-webgl', contextCreateOptions);
if (!this.gl) {
throw new Error('Failed to get WebGL Context');
}
var gl = this.gl;
var vertexAttr = null;
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
// Init buffers
this.vertexBuffer = gl.createBuffer();
var vertexCoords = new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]);
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertexCoords, gl.STATIC_DRAW);
// Setup the main YCrCbToRGBA shader
this.program = this.createProgram(
WebGLRenderer.SHADER.VERTEX_IDENTITY,
WebGLRenderer.SHADER.FRAGMENT_YCRCB_TO_RGBA
);
vertexAttr = gl.getAttribLocation(this.program, 'vertex');
gl.enableVertexAttribArray(vertexAttr);
gl.vertexAttribPointer(vertexAttr, 2, gl.FLOAT, false, 0, 0);
this.textureY = this.createTexture(0, 'textureY');
this.textureCb = this.createTexture(1, 'textureCb');
this.textureCr = this.createTexture(2, 'textureCr');
// Setup the loading animation shader
this.loadingProgram = this.createProgram(
WebGLRenderer.SHADER.VERTEX_IDENTITY,
WebGLRenderer.SHADER.FRAGMENT_LOADING
);
vertexAttr = gl.getAttribLocation(this.loadingProgram, 'vertex');
gl.enableVertexAttribArray(vertexAttr);
gl.vertexAttribPointer(vertexAttr, 2, gl.FLOAT, false, 0, 0);
this.shouldCreateUnclampedViews = !this.allowsClampedTextureData();
};
WebGLRenderer.prototype.destroy = function() {
var gl = this.gl;
this.deleteTexture(gl.TEXTURE0, this.textureY);
this.deleteTexture(gl.TEXTURE1, this.textureCb);
this.deleteTexture(gl.TEXTURE2, this.textureCr);
gl.useProgram(null);
gl.deleteProgram(this.program);
gl.deleteProgram(this.loadingProgram);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.deleteBuffer(this.vertexBuffer);
gl.getExtension('WEBGL_lose_context').loseContext();
this.canvas.remove();
};
WebGLRenderer.prototype.resize = function(width, height) {
this.width = width|0;
this.height = height|0;
this.canvas.width = this.width;
this.canvas.height = this.height;
this.gl.useProgram(this.program);
var codedWidth = ((this.width + 15) >> 4) << 4;
this.gl.viewport(0, 0, codedWidth, this.height);
};
WebGLRenderer.prototype.createTexture = function(index, name) {
var gl = this.gl;
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
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.uniform1i(gl.getUniformLocation(this.program, name), index);
return texture;
};
WebGLRenderer.prototype.createProgram = function(vsh, fsh) {
var gl = this.gl;
var program = gl.createProgram();
gl.attachShader(program, this.compileShader(gl.VERTEX_SHADER, vsh));
gl.attachShader(program, this.compileShader(gl.FRAGMENT_SHADER, fsh));
gl.linkProgram(program);
gl.useProgram(program);
return program;
};
WebGLRenderer.prototype.compileShader = function(type, source) {
var gl = this.gl;
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw new Error(gl.getShaderInfoLog(shader));
}
return shader;
};
WebGLRenderer.prototype.allowsClampedTextureData = function() {
var gl = this.gl;
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(
gl.TEXTURE_2D, 0, gl.LUMINANCE, 1, 1, 0,
gl.LUMINANCE, gl.UNSIGNED_BYTE, new Uint8ClampedArray([0])
);
return (gl.getError() === 0);
};
WebGLRenderer.prototype.renderProgress = function(progress) {
var gl = this.gl;
gl.useProgram(this.loadingProgram);
var loc = gl.getUniformLocation(this.loadingProgram, 'progress');
gl.uniform1f(loc, progress);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};
WebGLRenderer.prototype.render = function(y, cb, cr, isClampedArray) {
if (!this.enabled) {
return;
}
var gl = this.gl;
var w = ((this.width + 15) >> 4) << 4,
h = this.height,
w2 = w >> 1,
h2 = h >> 1;
// In some browsers WebGL doesn't like Uint8ClampedArrays (this is a bug
// and should be fixed soon-ish), so we have to create a Uint8Array view
// for each plane.
if (isClampedArray && this.shouldCreateUnclampedViews) {
y = new Uint8Array(y.buffer),
cb = new Uint8Array(cb.buffer),
cr = new Uint8Array(cr.buffer);
}
gl.useProgram(this.program);
this.updateTexture(gl.TEXTURE0, this.textureY, w, h, y);
this.updateTexture(gl.TEXTURE1, this.textureCb, w2, h2, cb);
this.updateTexture(gl.TEXTURE2, this.textureCr, w2, h2, cr);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};
WebGLRenderer.prototype.updateTexture = function(unit, texture, w, h, data) {
var gl = this.gl;
gl.activeTexture(unit);
gl.bindTexture(gl.TEXTURE_2D, texture);
if (this.hasTextureData[unit]) {
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, w, h, gl.LUMINANCE, gl.UNSIGNED_BYTE, data);
}
else {
this.hasTextureData[unit] = true;
gl.texImage2D(
gl.TEXTURE_2D, 0, gl.LUMINANCE, w, h, 0,
gl.LUMINANCE, gl.UNSIGNED_BYTE, data
);
}
};
WebGLRenderer.prototype.deleteTexture = function(unit, texture) {
var gl = this.gl;
gl.activeTexture(unit);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteTexture(texture);
};
WebGLRenderer.IsSupported = function() {
try {
if (!window.WebGLRenderingContext) {
return false;
}
var canvas = document.createElement('canvas');
return !!(
canvas.getContext('webgl') ||
canvas.getContext('experimental-webgl')
);
}
catch (err) {
return false;
}
};
WebGLRenderer.SHADER = {
FRAGMENT_YCRCB_TO_RGBA: [
'precision mediump float;',
'uniform sampler2D textureY;',
'uniform sampler2D textureCb;',
'uniform sampler2D textureCr;',
'varying vec2 texCoord;',
'mat4 rec601 = mat4(',
'1.16438, 0.00000, 1.59603, -0.87079,',
'1.16438, -0.39176, -0.81297, 0.52959,',
'1.16438, 2.01723, 0.00000, -1.08139,',
'0, 0, 0, 1',
');',
'void main() {',
'float y = texture2D(textureY, texCoord).r;',
'float cb = texture2D(textureCb, texCoord).r;',
'float cr = texture2D(textureCr, texCoord).r;',
'gl_FragColor = vec4(y, cr, cb, 1.0) * rec601;',
'}'
].join('\n'),
FRAGMENT_LOADING: [
'precision mediump float;',
'uniform float progress;',
'varying vec2 texCoord;',
'void main() {',
'float c = ceil(progress-(1.0-texCoord.y));',
'gl_FragColor = vec4(c,c,c,1);',
'}'
].join('\n'),
VERTEX_IDENTITY: [
'attribute vec2 vertex;',
'varying vec2 texCoord;',
'void main() {',
'texCoord = vertex;',
'gl_Position = vec4((vertex * 2.0 - 1.0) * vec2(1, -1), 0.0, 1.0);',
'}'
].join('\n')
};
return WebGLRenderer;
})();