/
ShaderProgram.js
414 lines (367 loc) · 13.2 KB
/
ShaderProgram.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
/**
* Copyright 2014 Google Inc. All Rights Reserved.
*
* 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.
*/
/* global Promise */
/**
* Create a WebGL shader program, which consists of a linked vertex shader and
* fragment shader. Shader source can be provided or modified at any time by
* calling setVertexShader or setFragmentShader as appropriate, but then the
* shaders must be manually linked to make this ShaderProgram use the new code.
* If vertex and fragment shader source are provided to the constructor,
* program compilation and linking is attempted immediately.
* @param {!WebGLRenderingContext} gl The WebGL context.
* @param {string=} opt_vertexSrc The GLSL ES vertex shader source code.
* @param {string=} opt_fragmentSrc The GLSL ES fragment shader source code.
* @constructor
*/
var ShaderProgram = function(gl, opt_vertexSrc, opt_fragmentSrc) {
// TODO: setters for uniforms? this would allow an easy soft fail for setting
// non-existent uniforms, but optional hard fail by Object.freeze (in strict
// mode).
/**
* The WebGL context.
* @private {!WebGLRenderingContext}
*/
this.gl_ = gl;
/**
* Collection of active attribute locations in this program object, keyed
* on attribute name. Unless new shaders are linked in this program and
* attribute locations change, use these rather than query for them.
* @type {!Object.<string, number>}
*/
this.attributes = {};
/**
* Collection of uniform locations and setters, keyed on uniform name. Raw
* uniform locations are at thisProgram.uniforms.uniformName.location, while
* thisProgram.uniforms.uniformName itself is a setter, used for setting
* scalars, vectors, or matrices, where appropriate.
*
* TODO(bckenny): create interface for these objects for documentation
* @type {Object.<function>}
*/
this.uniforms = {};
/**
* The vertex shader object.
* @private {!WebGLShader}
*/
this.vertexShader_ = gl.createShader(gl.VERTEX_SHADER);
/**
* The vertex shader has been successfully compiled.
* @private {boolean}
*/
this.vertexCompiled_ = false;
/**
* The fragment shader object.
* @private {!WebGLShader}
*/
this.fragmentShader_ = gl.createShader(gl.FRAGMENT_SHADER);
/**
* The fragment shader has been successfully compiled.
* @private {boolean}
*/
this.fragmentCompiled_ = false;
/**
* The raw WebGL program object.
* @type {!WebGLProgram}
*/
this.program = gl.createProgram();
/**
* The program has been successfully linked.
* @private {boolean}
*/
this.programLinked_ = false;
gl.attachShader(this.program, this.vertexShader_);
gl.attachShader(this.program, this.fragmentShader_);
// if shader sources have been provided, compile them now and attempt to link
if (opt_vertexSrc) {
this.setVertexShader(opt_vertexSrc);
}
if (opt_fragmentSrc) {
this.setFragmentShader(opt_fragmentSrc);
}
if (this.vertexCompiled_ && this.fragmentCompiled_) {
this.link();
}
};
/**
* Lookup table from uniform type to uniform setter function names. Keys are
* from OpenGL/WebGL spec.
* @enum {string}
* @private
*/
ShaderProgram.UNIFORM_SETTERS_ = {
0x1406: 'uniform1f', /* FLOAT */
0x8b50: 'uniform2f', /* FLOAT_VEC2 */
0x8b51: 'uniform3f', /* FLOAT_VEC3 */
0x8b52: 'uniform4f', /* FLOAT_VEC4 */
0x1404: 'uniform1i', /* INT */
0x8b53: 'uniform2i', /* INT_VEC2 */
0x8b54: 'uniform3i', /* INT_VEC3 */
0x8b55: 'uniform4i', /* INT_VEC4 */
0x8b56: 'uniform1i', /* BOOL */
0x8b57: 'uniform2i', /* BOOL_VEC2 */
0x8b58: 'uniform3i', /* BOOL_VEC3 */
0x8b59: 'uniform4i', /* BOOL_VEC4 */
0x8b5e: 'uniform1i', /* SAMPLER_2D */
0x8b60: 'uniform1i', /* SAMPLER_CUBE */
};
/**
* Lookup table from matrix uniform types to uniform setter function names.
* Matrices can only be supplied as arrays (setter functions ending with 'v'),
* so are special-cased here. Keys are from OpenGL/WebGL spec.
* @enum {string}
* @private
*/
ShaderProgram.UNIFORM_MATRIX_SETTERS_ = {
0x8b5a: 'uniformMatrix2fv', /* FLOAT_MAT2 */
0x8b5b: 'uniformMatrix3fv', /* FLOAT_MAT3 */
0x8b5c: 'uniformMatrix4fv', /* FLOAT_MAT4 */
};
/**
* Create a ShaderProgram from shader source at vertexUrl and fragmentUrl.
* Returns a promise that is fulfilled by returning the compiled and linked
* ShaderProgram.
* @param {string} vertexUrl URL for vertex shader source.
* @param {string} fragmentUrl URL for the fragment shader.
* @return {!Promise}
*/
ShaderProgram.fromXhr = function(gl, vertexUrl, fragmentUrl) {
var program = new ShaderProgram(gl);
return Promise.all([
ShaderProgram.promiseXhr_(vertexUrl)
.then(program.setVertexShader.bind(program)),
ShaderProgram.promiseXhr_(fragmentUrl)
.then(program.setFragmentShader.bind(program))
]).then(function() {
program.link();
return program;
});
};
/**
* Fetches a resource via XHR and returns a promise that will be fulfilled with
* its result.
* @param {string} url
* @return {!Promise}
* @private
*/
ShaderProgram.promiseXhr_ = function(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function(e) {
if (this.status === 200) {
resolve(this.responseText);
} else {
reject(this.statusText);
}
};
xhr.onerror = function(e) {
reject(this.statusText);
};
xhr.send();
});
};
/**
* Returns true if the shaders have succesfully compiled and have been linked.
* @return {boolean}
*/
ShaderProgram.prototype.isReady = function() {
return this.programLinked_;
};
/**
* Compiles a vertex or fragment shader from the supplied source code.
* @param {string} src
* @param {!WebGLShader} shader
* @return {boolean} Whether the shader compiled successfully.
* @private
*/
ShaderProgram.prototype.compileShader_ = function(src, shader) {
this.gl_.shaderSource(shader, src);
this.gl_.compileShader(shader);
var compileStatus = this.gl_.getShaderParameter(shader,
this.gl_.COMPILE_STATUS);
// invalidate current program
this.programLinked_ = false;
return compileStatus;
};
/**
* Sets the source of the fragment shader and attempts to compile it. If
* successful, the shader program will continue to operate as the previous
* program (if there was one) until link() is called and executed successfully.
* If compilation fails, an error is thrown with the compiler log within it.
* @param {string} src
* @throws {Error}
*/
ShaderProgram.prototype.setFragmentShader = function(src) {
this.fragmentCompiled_ = this.compileShader_(src, this.fragmentShader_);
if (!this.fragmentCompiled_) {
throw new Error('Fragment shader failed to compile. Log: ' +
this.getFragmentShaderInfoLog());
}
};
/**
* Sets the source of the vertex shader and attempts to compile it. If
* successful, the shader program will continue to operate as the previous
* program (if there was one) until link() is called and executed successfully.
* If compilation fails, an error is thrown with the compiler log within it.
* @param {string} src
* @throws {Error}
*/
ShaderProgram.prototype.setVertexShader = function(src) {
this.vertexCompiled_ = this.compileShader_(src, this.vertexShader_);
if (!this.vertexCompiled_) {
throw new Error('Vertex shader failed to compile. Log: ' +
this.getVertexShaderInfoLog());
}
};
/**
* Returns the contents of the information log for the currently attached
* fragment shader, if any.
* @return {string}
*/
ShaderProgram.prototype.getFragmentShaderInfoLog = function() {
return this.gl_.getShaderInfoLog(this.fragmentShader_);
};
/**
* Returns the contents of the information log for this program object, if any.
* @return {string}
*/
ShaderProgram.prototype.getProgramInfoLog = function() {
return this.gl_.getProgramInfoLog(this.program);
};
/**
* Returns the contents of the information log for the currently attached
* vertex shader, if any.
* @return {string}
*/
ShaderProgram.prototype.getVertexShaderInfoLog = function() {
return this.gl_.getShaderInfoLog(this.vertexShader_);
};
/**
* Enumerate all active attribute locations in this program object. Previous
* enumeration of attribute locations is discarded.
* @private
*/
ShaderProgram.prototype.initAttributes_ = function() {
var count = this.gl_.getProgramParameter(
this.program, this.gl_.ACTIVE_ATTRIBUTES);
// clear attribute locations and re-enumerate from currently linked program
this.attributes = {};
for (var i = 0; i < count; i++) {
var info = this.gl_.getActiveAttrib(this.program, i);
var loc = this.gl_.getAttribLocation(this.program, info.name);
this.attributes[info.name] = loc;
}
};
/**
* Creates a uniform setter function that can take dispatch to an array-based
* setter or one with an argument count based on the uniform type.
* @param {function(this:WebGLRenderingContext, ...[number])} set
* @param {function(this:WebGLRenderingContext, (Array.<number>|ArrayBufferView))} setVec
* @return {function(Array.<number>|ArrayBufferView|...[number])}
*/
ShaderProgram.createUniformSetter_ = function(set, setVec) {
return function setUniform() {
// TODO(bckenny): no ArrayBuffer.isView in Safari 7, but good elsewhere
if (Array.isArray(arguments[0]) || ArrayBuffer.isView(arguments[0])) {
setVec(arguments[0]);
} else {
set.apply(null, arguments);
}
};
};
/**
* Autogenerates setter methods for all active uniforms in this program object.
* When called, previously generated setter methods are discarded.
* @private
* @throws {Error} If uniform of unknown type is found.
*/
ShaderProgram.prototype.initUniforms_ = function() {
var gl = this.gl_;
this.uniforms = {};
// loop over current uniforms and create setter functions for them
var count = gl.getProgramParameter(this.program, gl.ACTIVE_UNIFORMS);
for (var i = 0; i < count; i++) {
var info = gl.getActiveUniform(this.program, i);
var name = info.name;
var location = gl.getUniformLocation(this.program, name);
// float, vec*, or sampler uniforms
if (ShaderProgram.UNIFORM_SETTERS_[info.type]) {
var setterMethod = ShaderProgram.UNIFORM_SETTERS_[info.type];
var set = gl[setterMethod].bind(gl, location);
var setVec = gl[setterMethod + 'v'].bind(gl, location);
// base is function that decides on type input what setter to call
// specialized setters are available at fn.set and fn.setVec
// and raw WebGLUniformLocation is on fn.location
this.uniforms[name] = ShaderProgram.createUniformSetter_(set, setVec);
this.uniforms[name].set = set;
this.uniforms[name].setVec = setVec;
this.uniforms[name].location = location;
// matrix uniform - matrix setters only accept arrays in setter
} else if (ShaderProgram.UNIFORM_MATRIX_SETTERS_[info.type]) {
var setterMatMethod = ShaderProgram.UNIFORM_MATRIX_SETTERS_[info.type];
this.uniforms[name] = gl[setterMatMethod].bind(gl, location, false);
this.uniforms[name].location = location;
} else {
// can't happen unless types are added to spec
throw new Error('Uniform ' + name + ' has unknown type ' + info.type);
}
}
};
/**
* Attempts to link the current vertex and fragment shaders. Throws an error if
* the vertex and/or fragment shaders aren't set and compiled, or if the
* shaders fail to link.
* @throws {Error}
*/
ShaderProgram.prototype.link = function() {
if (!this.vertexCompiled_) {
throw new Error('Current vertex shader has not been compiled');
}
if (!this.fragmentCompiled_) {
throw new Error('Current vertex shader has not been compiled');
}
this.gl_.linkProgram(this.program);
this.programLinked_ = this.gl_.getProgramParameter(this.program,
this.gl_.LINK_STATUS);
if (!this.programLinked_) {
throw new Error('Program failed to link. Log: ' +
this.getProgramInfoLog());
} else {
this.initAttributes_();
this.initUniforms_();
}
};
/**
* Installs this program object as part of the current rendering state. Does
* not check program status before attempting; if necessary, use isReady()
* first to check if program has successfully compiled and linked.
*/
ShaderProgram.prototype.use = function() {
this.gl_.useProgram(this.program);
};
/**
* Checks to see whether the executables contained in this program can execute
* given the current OpenGL state. The information generated by the validation
* process can be accessed via thisProgram.getProgramInfoLog().
* This function is typically useful only during application development and
* tends to be quite slow.
* @return {boolean} The validation status.
* @see http://www.khronos.org/opengles/sdk/2.0/docs/man/glValidateProgram.xml
*/
ShaderProgram.prototype.validateProgram = function() {
this.gl_.validateProgram(this.program);
return this.gl_.getProgramParameter(this.program, this.gl_.VALIDATE_STATUS);
};