diff options
Diffstat (limited to 'dom/vr/test/reftest/webgl-util.js')
-rw-r--r-- | dom/vr/test/reftest/webgl-util.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/dom/vr/test/reftest/webgl-util.js b/dom/vr/test/reftest/webgl-util.js new file mode 100644 index 0000000000..42f1c5ccd5 --- /dev/null +++ b/dom/vr/test/reftest/webgl-util.js @@ -0,0 +1,61 @@ +WebGLUtil = (function() { + // --------------------------------------------------------------------------- + // WebGL helpers + + // Returns a valid shader, or null on errors. + function createShaderById(gl, id) { + var elem = document.getElementById(id); + if (!elem) { + throw new Error( + "Failed to create shader from non-existent id '" + id + "'." + ); + } + + var src = elem.innerHTML.trim(); + + var shader; + if (elem.type == "x-shader/x-fragment") { + shader = gl.createShader(gl.FRAGMENT_SHADER); + } else if (elem.type == "x-shader/x-vertex") { + shader = gl.createShader(gl.VERTEX_SHADER); + } else { + throw new Error( + "Bad MIME type for shader '" + id + "': " + elem.type + "." + ); + } + + gl.shaderSource(shader, src); + gl.compileShader(shader); + + return shader; + } + + function createProgramByIds(gl, vsId, fsId) { + var vs = createShaderById(gl, vsId); + var fs = createShaderById(gl, fsId); + if (!vs || !fs) { + return null; + } + + var prog = gl.createProgram(); + gl.attachShader(prog, vs); + gl.attachShader(prog, fs); + gl.linkProgram(prog); + + if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) { + var str = "Shader program linking failed:"; + str += "\nShader program info log:\n" + gl.getProgramInfoLog(prog); + str += "\n\nVert shader log:\n" + gl.getShaderInfoLog(vs); + str += "\n\nFrag shader log:\n" + gl.getShaderInfoLog(fs); + console.error(str); + return null; + } + + return prog; + } + + return { + createShaderById, + createProgramByIds, + }; +})(); |