1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 WebGLTestUtils = (function() {
8 * Wrapped logging function.
9 * @param {string} msg The message to log.
11 var log = function(msg) {
12 if (window.console && window.console.log) {
13 window.console.log(msg);
18 * Wrapped logging function.
19 * @param {string} msg The message to log.
21 var error = function(msg) {
23 if (window.console.error) {
24 window.console.error(msg);
26 else if (window.console.log) {
27 window.console.log(msg);
33 * Turn off all logging.
35 var loggingOff = function() {
37 error = function() {};
41 * Converts a WebGL enum to a string
42 * @param {!WebGLContext} gl The WebGLContext to use.
43 * @param {number} value The enum value.
44 * @return {string} The enum as a string.
46 var glEnumToString = function(gl, value) {
52 return "0x" + value.toString(16);
58 * Returns the last compiler/linker error.
59 * @return {string} The last compiler/linker error.
61 var getLastError = function() {
66 * Whether a haystack ends with a needle.
67 * @param {string} haystack String to search
68 * @param {string} needle String to search for.
69 * @param {boolean} True if haystack ends with needle.
71 var endsWith = function(haystack, needle) {
72 return haystack.substr(haystack.length - needle.length) === needle;
76 * Whether a haystack starts with a needle.
77 * @param {string} haystack String to search
78 * @param {string} needle String to search for.
79 * @param {boolean} True if haystack starts with needle.
81 var startsWith = function(haystack, needle) {
82 return haystack.substr(0, needle.length) === needle;
86 * A vertex shader for a single texture.
89 var simpleTextureVertexShader = [
90 'attribute vec4 vPosition;',
91 'attribute vec2 texCoord0;',
92 'varying vec2 texCoord;',
94 ' gl_Position = vPosition;',
95 ' texCoord = texCoord0;',
99 * A fragment shader for a single texture.
102 var simpleTextureFragmentShader = [
103 'precision mediump float;',
104 'uniform sampler2D tex;',
105 'varying vec2 texCoord;',
107 ' gl_FragData[0] = texture2D(tex, texCoord);',
111 * Creates a simple texture vertex shader.
112 * @param {!WebGLContext} gl The WebGLContext to use.
113 * @return {!WebGLShader}
115 var setupSimpleTextureVertexShader = function(gl) {
116 return loadShader(gl, simpleTextureVertexShader, gl.VERTEX_SHADER);
120 * Creates a simple texture fragment shader.
121 * @param {!WebGLContext} gl The WebGLContext to use.
122 * @return {!WebGLShader}
124 var setupSimpleTextureFragmentShader = function(gl) {
126 gl, simpleTextureFragmentShader, gl.FRAGMENT_SHADER);
130 * Creates a program, attaches shaders, binds attrib locations, links the
131 * program and calls useProgram.
132 * @param {!Array.<!WebGLShader>} shaders The shaders to attach .
133 * @param {!Array.<string>} opt_attribs The attribs names.
134 * @param {!Array.<number>} opt_locations The locations for the attribs.
136 var setupProgram = function(gl, shaders, opt_attribs, opt_locations) {
137 var program = gl.createProgram();
138 for (var ii = 0; ii < shaders.length; ++ii) {
139 gl.attachShader(program, shaders[ii]);
142 for (var ii = 0; ii < opt_attribs.length; ++ii) {
143 gl.bindAttribLocation(
145 opt_locations ? opt_locations[ii] : ii,
149 gl.linkProgram(program);
151 // Check the link status
152 var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
154 // something went wrong with the link
155 lastError = gl.getProgramInfoLog (program);
156 error("Error in program linking:" + lastError);
158 gl.deleteProgram(program);
162 gl.useProgram(program);
167 * Creates a simple texture program.
168 * @param {!WebGLContext} gl The WebGLContext to use.
169 * @param {number} opt_positionLocation The attrib location for position.
170 * @param {number} opt_texcoordLocation The attrib location for texture coords.
171 * @return {WebGLProgram}
173 var setupSimpleTextureProgram = function(
174 gl, opt_positionLocation, opt_texcoordLocation) {
175 opt_positionLocation = opt_positionLocation || 0;
176 opt_texcoordLocation = opt_texcoordLocation || 1;
177 var vs = setupSimpleTextureVertexShader(gl);
178 var fs = setupSimpleTextureFragmentShader(gl);
182 var program = setupProgram(
185 ['vPosition', 'texCoord0'],
186 [opt_positionLocation, opt_texcoordLocation]);
191 gl.useProgram(program);
196 * Creates buffers for a textured unit quad and attaches them to vertex attribs.
197 * @param {!WebGLContext} gl The WebGLContext to use.
198 * @param {number} opt_positionLocation The attrib location for position.
199 * @param {number} opt_texcoordLocation The attrib location for texture coords.
200 * @return {!Array.<WebGLBuffer>} The buffer objects that were
203 var setupUnitQuad = function(gl, opt_positionLocation, opt_texcoordLocation) {
204 opt_positionLocation = opt_positionLocation || 0;
205 opt_texcoordLocation = opt_texcoordLocation || 1;
208 var vertexObject = gl.createBuffer();
209 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
210 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
216 1.0, -1.0, 0.0]), gl.STATIC_DRAW);
217 gl.enableVertexAttribArray(opt_positionLocation);
218 gl.vertexAttribPointer(opt_positionLocation, 3, gl.FLOAT, false, 0, 0);
219 objects.push(vertexObject);
221 var vertexObject = gl.createBuffer();
222 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
223 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
229 1.0, 0.0]), gl.STATIC_DRAW);
230 gl.enableVertexAttribArray(opt_texcoordLocation);
231 gl.vertexAttribPointer(opt_texcoordLocation, 2, gl.FLOAT, false, 0, 0);
232 objects.push(vertexObject);
237 * Creates a program and buffers for rendering a textured quad.
238 * @param {!WebGLContext} gl The WebGLContext to use.
239 * @param {number} opt_positionLocation The attrib location for position.
240 * @param {number} opt_texcoordLocation The attrib location for texture coords.
241 * @return {!WebGLProgram}
243 var setupTexturedQuad = function(
244 gl, opt_positionLocation, opt_texcoordLocation) {
245 var program = setupSimpleTextureProgram(
246 gl, opt_positionLocation, opt_texcoordLocation);
247 setupUnitQuad(gl, opt_positionLocation, opt_texcoordLocation);
252 * Creates a unit quad with only positions of a given rez
253 * @param {!WebGLContext} gl The WebGLContext to use.
254 * @param {number} gridRez The resolution of the mesh grid.
255 * @param {number} opt_positionLocation The attrib location for position.
257 var setupQuad = function (
258 gl, gridRes, opt_positionLocation, opt_flipOddTriangles) {
259 var positionLocation = opt_positionLocation || 0;
262 var vertsAcross = gridRes + 1;
263 var numVerts = vertsAcross * vertsAcross;
264 var positions = new Float32Array(numVerts * 3);
265 var indices = new Uint16Array(6 * gridRes * gridRes);
269 for (var yy = 0; yy <= gridRes; ++yy) {
270 for (var xx = 0; xx <= gridRes; ++xx) {
271 positions[poffset + 0] = -1 + 2 * xx / gridRes;
272 positions[poffset + 1] = -1 + 2 * yy / gridRes;
273 positions[poffset + 2] = 0;
280 for (var yy = 0; yy < gridRes; ++yy) {
281 var index = yy * vertsAcross;
282 for (var xx = 0; xx < gridRes; ++xx) {
283 indices[tbase + 0] = index + 0;
284 indices[tbase + 1] = index + 1;
285 indices[tbase + 2] = index + vertsAcross;
286 indices[tbase + 3] = index + vertsAcross;
287 indices[tbase + 4] = index + 1;
288 indices[tbase + 5] = index + vertsAcross + 1;
290 if (opt_flipOddTriangles) {
291 indices[tbase + 4] = index + vertsAcross + 1;
292 indices[tbase + 5] = index + 1;
300 var buf = gl.createBuffer();
301 gl.bindBuffer(gl.ARRAY_BUFFER, buf);
302 gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
303 gl.enableVertexAttribArray(positionLocation);
304 gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0);
307 var buf = gl.createBuffer();
308 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buf);
309 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
316 * Fills the given texture with a solid color
317 * @param {!WebGLContext} gl The WebGLContext to use.
318 * @param {!WebGLTexture} tex The texture to fill.
319 * @param {number} width The width of the texture to create.
320 * @param {number} height The height of the texture to create.
321 * @param {!Array.<number>} color The color to fill with. A 4 element array
322 * where each element is in the range 0 to 255.
323 * @param {number} opt_level The level of the texture to fill. Default = 0.
325 var fillTexture = function(gl, tex, width, height, color, opt_level) {
326 opt_level = opt_level || 0;
327 var numPixels = width * height;
328 var size = numPixels * 4;
329 var buf = new Uint8Array(size);
330 for (var ii = 0; ii < numPixels; ++ii) {
332 buf[off + 0] = color[0];
333 buf[off + 1] = color[1];
334 buf[off + 2] = color[2];
335 buf[off + 3] = color[3];
337 gl.bindTexture(gl.TEXTURE_2D, tex);
339 gl.TEXTURE_2D, opt_level, gl.RGBA, width, height, 0,
340 gl.RGBA, gl.UNSIGNED_BYTE, buf);
344 * Creates a textures and fills it with a solid color
345 * @param {!WebGLContext} gl The WebGLContext to use.
346 * @param {number} width The width of the texture to create.
347 * @param {number} height The height of the texture to create.
348 * @param {!Array.<number>} color The color to fill with. A 4 element array
349 * where each element is in the range 0 to 255.
350 * @return {!WebGLTexture}
352 var createColoredTexture = function(gl, width, height, color) {
353 var tex = gl.createTexture();
354 fillTexture(gl, tex, width, height, color);
359 * Draws a previously setup quad.
360 * @param {!WebGLContext} gl The WebGLContext to use.
361 * @param {!Array.<number>} opt_color The color to fill clear with before
362 * drawing. A 4 element array where each element is in the range 0 to
363 * 255. Default [255, 255, 255, 255]
365 var drawQuad = function(gl, opt_color) {
366 opt_color = opt_color || [255, 255, 255, 255];
372 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
373 gl.drawArrays(gl.TRIANGLES, 0, 6);
377 * Checks that a portion of a canvas is 1 color.
378 * @param {!WebGLContext} gl The WebGLContext to use.
379 * @param {number} x left corner of region to check.
380 * @param {number} y bottom corner of region to check.
381 * @param {number} width width of region to check.
382 * @param {number} height width of region to check.
383 * @param {!Array.<number>} color The color to fill clear with before drawing. A
384 * 4 element array where each element is in the range 0 to 255.
385 * @param {string} msg Message to associate with success. Eg ("should be red").
386 * @param {number} errorRange Optional. Acceptable error in
387 * color checking. 0 by default.
389 var checkCanvasRect = function(gl, x, y, width, height, color, msg, errorRange) {
390 errorRange = errorRange || 0;
391 var buf = new Uint8Array(width * height * 4);
392 gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
393 for (var i = 0; i < width * height; ++i) {
395 for (var j = 0; j < color.length; ++j) {
396 if (Math.abs(buf[offset + j] - color[j]) > errorRange) {
398 var was = buf[offset + 0].toString();
399 for (j = 1; j < color.length; ++j) {
400 was += "," + buf[offset + j];
402 debug('at (' + (i % width) + ', ' + Math.floor(i / width) +
403 ') expected: ' + color + ' was ' + was);
412 * Checks that an entire canvas is 1 color.
413 * @param {!WebGLContext} gl The WebGLContext to use.
414 * @param {!Array.<number>} color The color to fill clear with before drawing. A
415 * 4 element array where each element is in the range 0 to 255.
416 * @param {string} msg Message to associate with success. Eg ("should be red").
417 * @param {number} errorRange Optional. Acceptable error in
418 * color checking. 0 by default.
420 var checkCanvas = function(gl, color, msg, errorRange) {
421 checkCanvasRect(gl, 0, 0, gl.canvas.width, gl.canvas.height, color, msg, errorRange);
425 * Loads a texture, calls callback when finished.
426 * @param {!WebGLContext} gl The WebGLContext to use.
427 * @param {string} url URL of image to load
428 * @param {function(!Image): void} callback Function that gets called after
430 * @return {!WebGLTexture} The created texture.
432 var loadTexture = function(gl, url, callback) {
433 var texture = gl.createTexture();
434 gl.bindTexture(gl.TEXTURE_2D, texture);
435 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
436 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
437 var image = new Image();
438 image.onload = function() {
439 gl.bindTexture(gl.TEXTURE_2D, texture);
440 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
441 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
449 * Creates a webgl context.
450 * @param {!Canvas} opt_canvas The canvas tag to get context from. If one is not
451 * passed in one will be created.
452 * @return {!WebGLContext} The created context.
454 var create3DContext = function(opt_canvas, opt_attributes) {
455 opt_canvas = opt_canvas || document.createElement("canvas");
458 context = opt_canvas.getContext("webgl", opt_attributes);
462 context = opt_canvas.getContext("experimental-webgl", opt_attributes);
466 testFailed("Unable to fetch WebGL rendering context for Canvas");
472 * Gets a GLError value as a string.
473 * @param {!WebGLContext} gl The WebGLContext to use.
474 * @param {number} err The webgl error as retrieved from gl.getError().
475 * @return {string} the error as a string.
477 var getGLErrorAsString = function(gl, err) {
478 if (err === gl.NO_ERROR) {
481 for (var name in gl) {
482 if (gl[name] === err) {
486 return err.toString();
490 * Wraps a WebGL function with a function that throws an exception if there is
492 * @param {!WebGLContext} gl The WebGLContext to use.
493 * @param {string} fname Name of function to wrap.
494 * @return {function} The wrapped function.
496 var createGLErrorWrapper = function(context, fname) {
498 var rv = context[fname].apply(context, arguments);
499 var err = context.getError();
501 throw "GL error " + getGLErrorAsString(err) + " in " + fname;
507 * Creates a WebGL context where all functions are wrapped to throw an exception
508 * if there is an error.
509 * @param {!Canvas} canvas The HTML canvas to get a context from.
510 * @return {!Object} The wrapped context.
512 function create3DContextWithWrapperThatThrowsOnGLError(canvas) {
513 var context = create3DContext(canvas);
515 for (var i in context) {
517 if (typeof context[i] == 'function') {
518 wrap[i] = createGLErrorWrapper(context, i);
520 wrap[i] = context[i];
523 error("createContextWrapperThatThrowsOnGLError: Error accessing " + i);
526 wrap.getError = function() {
527 return context.getError();
533 * Tests that an evaluated expression generates a specific GL error.
534 * @param {!WebGLContext} gl The WebGLContext to use.
535 * @param {number} glError The expected gl error.
536 * @param {string} evalSTr The string to evaluate.
538 var shouldGenerateGLError = function(gl, glError, evalStr) {
546 testFailed(evalStr + " threw exception " + exception);
548 var err = gl.getError();
549 if (err != glError) {
550 testFailed(evalStr + " expected: " + getGLErrorAsString(gl, glError) + ". Was " + getGLErrorAsString(gl, err) + ".");
552 testPassed(evalStr + " was expected value: " + getGLErrorAsString(gl, glError) + ".");
558 * Tests that the first error GL returns is the specified error.
559 * @param {!WebGLContext} gl The WebGLContext to use.
560 * @param {number} glError The expected gl error.
561 * @param {string} opt_msg
563 var glErrorShouldBe = function(gl, glError, opt_msg) {
564 opt_msg = opt_msg || "";
565 var err = gl.getError();
566 if (err != glError) {
567 testFailed("getError expected: " + getGLErrorAsString(gl, glError) +
568 ". Was " + getGLErrorAsString(gl, err) + " : " + opt_msg);
570 testPassed("getError was expected value: " +
571 getGLErrorAsString(gl, glError) + " : " + opt_msg);
576 * Links a WebGL program, throws if there are errors.
577 * @param {!WebGLContext} gl The WebGLContext to use.
578 * @param {!WebGLProgram} program The WebGLProgram to link.
579 * @param {function(string): void) opt_errorCallback callback for errors.
581 var linkProgram = function(gl, program, opt_errorCallback) {
583 gl.linkProgram(program);
585 // Check the link status
586 var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
588 // something went wrong with the link
589 var error = gl.getProgramInfoLog (program);
591 testFailed("Error in program linking:" + error);
593 gl.deleteProgram(program);
598 * Sets up WebGL with shaders.
599 * @param {string} canvasName The id of the canvas.
600 * @param {string} vshader The id of the script tag that contains the vertex
602 * @param {string} fshader The id of the script tag that contains the fragment
604 * @param {!Array.<string>} attribs An array of attrib names used to bind
605 * attribs to the ordinal of the name in this array.
606 * @param {!Array.<number>} opt_clearColor The color to cla
607 * @return {!WebGLContext} The created WebGLContext.
609 var setupWebGLWithShaders = function(
610 canvasName, vshader, fshader, attribs) {
611 var canvas = document.getElementById(canvasName);
612 var gl = create3DContext(canvas);
614 testFailed("No WebGL context found");
617 // create our shaders
618 var vertexShader = loadShaderFromScript(gl, vshader);
619 var fragmentShader = loadShaderFromScript(gl, fshader);
621 if (!vertexShader || !fragmentShader) {
625 // Create the program object
626 program = gl.createProgram();
632 // Attach our two shaders to the program
633 gl.attachShader (program, vertexShader);
634 gl.attachShader (program, fragmentShader);
637 for (var i in attribs) {
638 gl.bindAttribLocation (program, i, attribs[i]);
641 linkProgram(gl, program);
643 gl.useProgram(program);
645 gl.clearColor(0,0,0,1);
648 gl.enable(gl.DEPTH_TEST);
650 gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
652 gl.program = program;
657 * Loads text from an external file. This function is synchronous.
658 * @param {string} url The url of the external file.
659 * @param {!function(bool, string): void} callback that is sent a bool for
660 * success and the string.
662 var loadTextFileAsync = function(url, callback) {
663 log ("loading: " + url);
664 var error = 'loadTextFileSynchronous failed to load url "' + url + '"';
666 if (window.XMLHttpRequest) {
667 request = new XMLHttpRequest();
668 if (request.overrideMimeType) {
669 request.overrideMimeType('text/plain');
672 throw 'XMLHttpRequest is disabled';
675 request.open('GET', url, true);
676 request.onreadystatechange = function() {
677 if (request.readyState == 4) {
679 // HTTP reports success with a 200 status. The file protocol reports
680 // success with zero. HTTP does not use zero as a status code (they
682 // https://developer.mozilla.org/En/Using_XMLHttpRequest
683 var success = request.status == 200 || request.status == 0;
685 text = request.responseText;
687 log("loaded: " + url);
688 callback(success, text);
693 log("failed to load: " + url);
699 * Recursively loads a file as a list. Each line is parsed for a relative
700 * path. If the file ends in .txt the contents of that file is inserted in
703 * @param {string} url The url of the external file.
704 * @param {!function(bool, Array<string>): void} callback that is sent a bool
705 * for success and the array of strings.
707 var getFileListAsync = function(url, callback) {
710 var getFileListImpl = function(url, callback) {
712 if (url.substr(url.length - 4) == '.txt') {
713 loadTextFileAsync(url, function() {
714 return function(success, text) {
719 var lines = text.split('\n');
721 var lastSlash = url.lastIndexOf('/');
722 if (lastSlash >= 0) {
723 prefix = url.substr(0, lastSlash + 1);
728 for (var ii = 0; ii < lines.length; ++ii) {
729 var str = lines[ii].replace(/^\s\s*/, '').replace(/\s\s*$/, '');
730 if (str.length > 4 &&
733 str.substr(0, 2) != "//") {
734 var names = str.split(/ +/);
735 new_url = prefix + str;
736 if (names.length == 1) {
737 new_url = prefix + str;
739 getFileListImpl(new_url, function(index) {
740 return function(success, new_files) {
741 log("got files: " + new_files.length);
743 files[index] = new_files;
751 for (var jj = 0; jj < names.length; ++jj) {
752 s += p + prefix + names[jj];
761 function finish(success) {
766 log("count: " + count);
768 callback(!fail, files);
776 callback(true, files);
780 getFileListImpl(url, function(success, files) {
784 function flatten(files) {
785 for (var ii = 0; ii < files.length; ++ii) {
786 var value = files[ii];
787 if (typeof(value) == "string") {
794 callback(success, flat);
799 * Gets a file from a file/URL
800 * @param {string} file the URL of the file to get.
801 * @return {string} The contents of the file.
803 var readFile = function(file) {
804 var xhr = new XMLHttpRequest();
805 xhr.open("GET", file, false);
807 return xhr.responseText.replace(/\r/g, "");
810 var readFileList = function(url) {
812 if (url.substr(url.length - 4) == '.txt') {
813 var lines = readFile(url).split('\n');
815 var lastSlash = url.lastIndexOf('/');
816 if (lastSlash >= 0) {
817 prefix = url.substr(0, lastSlash + 1);
819 for (var ii = 0; ii < lines.length; ++ii) {
820 var str = lines[ii].replace(/^\s\s*/, '').replace(/\s\s*$/, '');
821 if (str.length > 4 &&
824 str.substr(0, 2) != "//") {
825 var names = str.split(/ +/);
826 if (names.length == 1) {
827 new_url = prefix + str;
828 files = files.concat(readFileList(new_url));
832 for (var jj = 0; jj < names.length; ++jj) {
833 s += p + prefix + names[jj];
848 * @param {!WebGLContext} gl The WebGLContext to use.
849 * @param {string} shaderSource The shader source.
850 * @param {number} shaderType The type of shader.
851 * @param {function(string): void) opt_errorCallback callback for errors.
852 * @return {!WebGLShader} The created shader.
854 var loadShader = function(gl, shaderSource, shaderType, opt_errorCallback) {
855 var errFn = opt_errorCallback || error;
856 // Create the shader object
857 var shader = gl.createShader(shaderType);
858 if (shader == null) {
859 errFn("*** Error: unable to create shader '"+shaderSource+"'");
863 // Load the shader source
864 gl.shaderSource(shader, shaderSource);
865 var err = gl.getError();
866 if (err != gl.NO_ERROR) {
867 errFn("*** Error loading shader '" + shader + "':" + glEnumToString(gl, err));
871 // Compile the shader
872 gl.compileShader(shader);
874 // Check the compile status
875 var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
877 // Something went wrong during compilation; get the error
878 lastError = gl.getShaderInfoLog(shader);
879 errFn("*** Error compiling shader '" + shader + "':" + lastError);
880 gl.deleteShader(shader);
888 * Loads a shader from a URL.
889 * @param {!WebGLContext} gl The WebGLContext to use.
890 * @param {file} file The URL of the shader source.
891 * @param {number} type The type of shader.
892 * @param {function(string): void) opt_errorCallback callback for errors.
893 * @return {!WebGLShader} The created shader.
895 var loadShaderFromFile = function(gl, file, type, opt_errorCallback) {
896 var shaderSource = readFile(file);
897 return loadShader(gl, shaderSource, type, opt_errorCallback);
901 * Loads a shader from a script tag.
902 * @param {!WebGLContext} gl The WebGLContext to use.
903 * @param {string} scriptId The id of the script tag.
904 * @param {number} opt_shaderType The type of shader. If not passed in it will
905 * be derived from the type of the script tag.
906 * @param {function(string): void) opt_errorCallback callback for errors.
907 * @return {!WebGLShader} The created shader.
909 var loadShaderFromScript = function(
910 gl, scriptId, opt_shaderType, opt_errorCallback) {
911 var shaderSource = "";
913 var shaderScript = document.getElementById(scriptId);
915 throw("*** Error: unknown script element" + scriptId);
917 shaderSource = shaderScript.text;
919 if (!opt_shaderType) {
920 if (shaderScript.type == "x-shader/x-vertex") {
921 shaderType = gl.VERTEX_SHADER;
922 } else if (shaderScript.type == "x-shader/x-fragment") {
923 shaderType = gl.FRAGMENT_SHADER;
924 } else if (shaderType != gl.VERTEX_SHADER && shaderType != gl.FRAGMENT_SHADER) {
925 throw("*** Error: unknown shader type");
931 gl, shaderSource, opt_shaderType ? opt_shaderType : shaderType,
935 var loadStandardProgram = function(gl) {
936 var program = gl.createProgram();
937 gl.attachShader(program, loadStandardVertexShader(gl));
938 gl.attachShader(program, loadStandardFragmentShader(gl));
939 linkProgram(gl, program);
944 * Loads shaders from files, creates a program, attaches the shaders and links.
945 * @param {!WebGLContext} gl The WebGLContext to use.
946 * @param {string} vertexShaderPath The URL of the vertex shader.
947 * @param {string} fragmentShaderPath The URL of the fragment shader.
948 * @param {function(string): void) opt_errorCallback callback for errors.
949 * @return {!WebGLProgram} The created program.
951 var loadProgramFromFile = function(
952 gl, vertexShaderPath, fragmentShaderPath, opt_errorCallback) {
953 var program = gl.createProgram();
957 gl, vertexShaderPath, gl.VERTEX_SHADER, opt_errorCallback));
961 gl, fragmentShaderPath, gl.FRAGMENT_SHADER, opt_errorCallback));
962 linkProgram(gl, program, opt_errorCallback);
967 * Loads shaders from script tags, creates a program, attaches the shaders and
969 * @param {!WebGLContext} gl The WebGLContext to use.
970 * @param {string} vertexScriptId The id of the script tag that contains the
972 * @param {string} fragmentScriptId The id of the script tag that contains the
974 * @param {function(string): void) opt_errorCallback callback for errors.
975 * @return {!WebGLProgram} The created program.
977 var loadProgramFromScript = function loadProgramFromScript(
978 gl, vertexScriptId, fragmentScriptId, opt_errorCallback) {
979 var program = gl.createProgram();
982 loadShaderFromScript(
983 gl, vertexScriptId, gl.VERTEX_SHADER, opt_errorCallback));
986 loadShaderFromScript(
987 gl, fragmentScriptId, gl.FRAGMENT_SHADER, opt_errorCallback));
988 linkProgram(gl, program, opt_errorCallback);
993 * Loads shaders from source, creates a program, attaches the shaders and
995 * @param {!WebGLContext} gl The WebGLContext to use.
996 * @param {string} vertexShader The vertex shader.
997 * @param {string} fragmentShader The fragment shader.
998 * @param {function(string): void) opt_errorCallback callback for errors.
999 * @return {!WebGLProgram} The created program.
1001 var loadProgram = function(
1002 gl, vertexShader, fragmentShader, opt_errorCallback) {
1003 var program = gl.createProgram();
1007 gl, vertexShader, gl.VERTEX_SHADER, opt_errorCallback));
1011 gl, fragmentShader, gl.FRAGMENT_SHADER, opt_errorCallback));
1012 linkProgram(gl, program, opt_errorCallback);
1017 var getBasePath = function() {
1019 var expectedBase = "webgl-test-utils.js";
1020 var scripts = document.getElementsByTagName('script');
1021 for (var script, i = 0; script = scripts[i]; i++) {
1022 var src = script.src;
1024 if (src.substr(l - expectedBase.length) == expectedBase) {
1025 basePath = src.substr(0, l - expectedBase.length);
1032 var loadStandardVertexShader = function(gl) {
1033 return loadShaderFromFile(
1034 gl, getBasePath() + "vertexShader.vert", gl.VERTEX_SHADER);
1037 var loadStandardFragmentShader = function(gl) {
1038 return loadShaderFromFile(
1039 gl, getBasePath() + "fragmentShader.frag", gl.FRAGMENT_SHADER);
1043 * Loads an image asynchronously.
1044 * @param {string} url URL of image to load.
1045 * @param {!function(!Element): void} callback Function to call
1046 * with loaded image.
1048 var loadImageAsync = function(url, callback) {
1049 var img = document.createElement('img');
1050 img.onload = function() {
1057 * Loads an array of images.
1058 * @param {!Array.<string>} urls URLs of images to load.
1059 * @param {!function(!{string, img}): void} callback. Callback
1060 * that gets passed map of urls to img tags.
1062 var loadImagesAsync = function(urls, callback) {
1065 function countDown() {
1071 function imageLoaded(url) {
1072 return function(img) {
1077 for (var ii = 0; ii < urls.length; ++ii) {
1079 loadImageAsync(urls[ii], imageLoaded(urls[ii]));
1084 var getUrlArguments = function() {
1087 var s = window.location.href;
1088 var q = s.indexOf("?");
1089 var e = s.indexOf("#");
1093 var query = s.substring(q + 1, e);
1094 var pairs = query.split("&");
1095 for (var ii = 0; ii < pairs.length; ++ii) {
1096 var keyValue = pairs[ii].split("=");
1097 var key = keyValue[0];
1098 var value = decodeURIComponent(keyValue[1]);
1102 throw "could not parse url";
1108 create3DContext: create3DContext,
1109 create3DContextWithWrapperThatThrowsOnGLError:
1110 create3DContextWithWrapperThatThrowsOnGLError,
1111 checkCanvas: checkCanvas,
1112 checkCanvasRect: checkCanvasRect,
1113 createColoredTexture: createColoredTexture,
1116 getFileListAsync: getFileListAsync,
1117 getLastError: getLastError,
1118 getUrlArguments: getUrlArguments,
1119 glEnumToString: glEnumToString,
1120 glErrorShouldBe: glErrorShouldBe,
1121 fillTexture: fillTexture,
1122 loadImageAsync: loadImageAsync,
1123 loadImagesAsync: loadImagesAsync,
1124 loadProgram: loadProgram,
1125 loadProgramFromFile: loadProgramFromFile,
1126 loadProgramFromScript: loadProgramFromScript,
1127 loadShader: loadShader,
1128 loadShaderFromFile: loadShaderFromFile,
1129 loadShaderFromScript: loadShaderFromScript,
1130 loadStandardProgram: loadStandardProgram,
1131 loadStandardVertexShader: loadStandardVertexShader,
1132 loadStandardFragmentShader: loadStandardFragmentShader,
1133 loadTextFileAsync: loadTextFileAsync,
1134 loadTexture: loadTexture,
1136 loggingOff: loggingOff,
1138 setupProgram: setupProgram,
1139 setupQuad: setupQuad,
1140 setupSimpleTextureFragmentShader: setupSimpleTextureFragmentShader,
1141 setupSimpleTextureProgram: setupSimpleTextureProgram,
1142 setupSimpleTextureVertexShader: setupSimpleTextureVertexShader,
1143 setupTexturedQuad: setupTexturedQuad,
1144 setupUnitQuad: setupUnitQuad,
1145 setupWebGLWithShaders: setupWebGLWithShaders,
1146 startsWith: startsWith,
1147 shouldGenerateGLError: shouldGenerateGLError,
1149 readFileList: readFileList,