Skip to content

Instantly share code, notes, and snippets.

@michael-nischt
Last active October 5, 2015 15:58
Show Gist options
  • Save michael-nischt/2831189 to your computer and use it in GitHub Desktop.
Save michael-nischt/2831189 to your computer and use it in GitHub Desktop.
WebGL shader utilities
/*
* Copyright (c) 2011 Michael Nischt
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the project's author nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
function getProgramAttributes(gl, program, attributes) {
attributes = attributes || {};
var info, i, count = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
for (i = 0; i < count; i += 1) {
info = gl.getActiveAttrib(program, i);
attributes[info.name] = gl.getAttribLocation(program, info.name);
}
return attributes;
}
function getProgramUniforms(gl, program, uniforms) {
uniforms = uniforms || {};
var info, i, count = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (i = 0; i < count; i += 1) {
info = gl.getActiveUniform(program, i);
uniforms[info.name] = gl.getUniformLocation(program, info.name);
}
return uniforms;
}
function getProgramParameters(gl, program, parameters) {
parameters = parameters || {};
parameters.uniforms = getProgramUniforms(gl, program);
parameters.attributes = getProgramAttributes(gl, program);
return parameters;
}
function createShader(gl, str, shaderType) {
var shader = gl.createShader(shaderType);
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw ("GLSL compile error:\n" + gl.getShaderInfoLog(shader));
}
return shader;
}
function createVertexShader(gl, str) {
return createShader(gl, str, gl.VERTEX_SHADER);
}
function createFragmentShader(gl, str) {
return createShader(gl, str, gl.FRAGMENT_SHADER);
}
function createProgram(gl, vertexShader, fragmentShader) {
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw ("GLSL link error:\n" + gl.getProgramInfoLog(program));
}
return program;
}
function createProgramFromSource(gl, vs, fs) {
vs = GL.createVertexShader(gl, vs);
fs = GL.createFragmentShader(gl, fs);
return GL.createProgram(gl, vs, fs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment