content/canvas/test/webgl-mochitest/webgl-util.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 WebGLUtil = (function() {
michael@0 2 // ---------------------------------------------------------------------------
michael@0 3 // Error handling (for obvious failures, such as invalid element ids)
michael@0 4
michael@0 5 function defaultErrorFunc(str) {
michael@0 6 console.log('Error: ' + str);
michael@0 7 }
michael@0 8
michael@0 9 var gErrorFunc = defaultErrorFunc;
michael@0 10 function setErrorFunc(func) {
michael@0 11 gErrorFunc = func;
michael@0 12 }
michael@0 13
michael@0 14 function error(str) {
michael@0 15 gErrorFunc(str);
michael@0 16 }
michael@0 17
michael@0 18 // ---------------------------------------------------------------------------
michael@0 19 // Warning handling (for failures that may be intentional)
michael@0 20
michael@0 21 function defaultWarningFunc(str) {
michael@0 22 console.log('Warning: ' + str);
michael@0 23 }
michael@0 24
michael@0 25 var gWarningFunc = defaultWarningFunc;
michael@0 26 function setWarningFunc(func) {
michael@0 27 gWarningFunc = func;
michael@0 28 }
michael@0 29
michael@0 30 function warning(str) {
michael@0 31 gWarningFunc(str);
michael@0 32 }
michael@0 33
michael@0 34 // ---------------------------------------------------------------------------
michael@0 35 // WebGL helpers
michael@0 36
michael@0 37 function getWebGL(canvasId, requireConformant) {
michael@0 38 // `requireConformant` will default to falsey if it is not supplied.
michael@0 39
michael@0 40 var canvas = document.getElementById(canvasId);
michael@0 41
michael@0 42 var gl = null;
michael@0 43 try {
michael@0 44 gl = canvas.getContext('webgl');
michael@0 45 } catch(e) {}
michael@0 46
michael@0 47 if (!gl && !requireConformant) {
michael@0 48 try {
michael@0 49 gl = canvas.getContext('experimental-webgl');
michael@0 50 } catch(e) {}
michael@0 51 }
michael@0 52
michael@0 53 if (!gl) {
michael@0 54 error('WebGL context could not be retrieved from \'' + canvasId + '\'.');
michael@0 55 return null;
michael@0 56 }
michael@0 57
michael@0 58 return gl;
michael@0 59 }
michael@0 60
michael@0 61 function getContentFromElem(elem) {
michael@0 62 var str = "";
michael@0 63 var k = elem.firstChild;
michael@0 64 while (k) {
michael@0 65 if (k.nodeType == 3)
michael@0 66 str += k.textContent;
michael@0 67
michael@0 68 k = k.nextSibling;
michael@0 69 }
michael@0 70
michael@0 71 return str;
michael@0 72 }
michael@0 73
michael@0 74 // Returns a valid shader, or null on errors.
michael@0 75 function createShaderById(gl, id) {
michael@0 76 var elem = document.getElementById(id);
michael@0 77 if (!elem) {
michael@0 78 error('Failed to create shader from non-existent id \'' + id + '\'.');
michael@0 79 return null;
michael@0 80 }
michael@0 81
michael@0 82 var src = getContentFromElem(elem);
michael@0 83
michael@0 84 var shader;
michael@0 85 if (elem.type == "x-shader/x-fragment") {
michael@0 86 shader = gl.createShader(gl.FRAGMENT_SHADER);
michael@0 87 } else if (elem.type == "x-shader/x-vertex") {
michael@0 88 shader = gl.createShader(gl.VERTEX_SHADER);
michael@0 89 } else {
michael@0 90 error('Bad MIME type for shader \'' + id + '\': ' + elem.type + '.');
michael@0 91 return null;
michael@0 92 }
michael@0 93
michael@0 94 gl.shaderSource(shader, src);
michael@0 95 gl.compileShader(shader);
michael@0 96
michael@0 97 return shader;
michael@0 98 }
michael@0 99
michael@0 100 function createProgramByIds(gl, vsId, fsId) {
michael@0 101 var vs = createShaderById(gl, vsId);
michael@0 102 var fs = createShaderById(gl, fsId);
michael@0 103 if (!vs || !fs)
michael@0 104 return null;
michael@0 105
michael@0 106 var prog = gl.createProgram();
michael@0 107 gl.attachShader(prog, vs);
michael@0 108 gl.attachShader(prog, fs);
michael@0 109 gl.linkProgram(prog);
michael@0 110
michael@0 111 if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
michael@0 112 var str = "Shader program linking failed:";
michael@0 113 str += "\nShader program info log:\n" + gl.getProgramInfoLog(prog);
michael@0 114 str += "\n\nVert shader log:\n" + gl.getShaderInfoLog(vs);
michael@0 115 str += "\n\nFrag shader log:\n" + gl.getShaderInfoLog(fs);
michael@0 116 warning(str);
michael@0 117 return null;
michael@0 118 }
michael@0 119
michael@0 120 return prog;
michael@0 121 }
michael@0 122
michael@0 123 return {
michael@0 124 setErrorFunc: setErrorFunc,
michael@0 125 setWarningFunc: setWarningFunc,
michael@0 126
michael@0 127 getWebGL: getWebGL,
michael@0 128 createShaderById: createShaderById,
michael@0 129 createProgramByIds: createProgramByIds,
michael@0 130 };
michael@0 131 })();

mercurial