/*
 * Copyright (c) 2009, Mozilla Corp
 * 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 <organization> 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 <copyright holder> ''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 <copyright holder> 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.
 */

// convenience function to create a shader from a <shader> tag's contents
function getShader(id)
{
    // use jQuery to grab the content of the shader and the type
    var shaderScriptType = $("#" + id).attr("type");
    var shaderScript = $("#" + id).text();

    if (!shaderScript || shaderScript.length == 0)
        return 0;

    var shader = 0;
    if (shaderScriptType == "x-shader/x-fragment") {
        shader = gl.createShader(gl.FRAGMENT_SHADER);
    } else if (shaderScriptType == "x-shader/x-vertex") {
        shader = gl.createShader(gl.VERTEX_SHADER);
    }

    if (shader == 0)
	return 0;

    gl.shaderSource(shader, shaderScript);
    gl.compileShader(shader);

    if (gl.getShaderParameter(shader, gl.COMPILE_STATUS) != 1) {
	var error = gl.getShaderInfoLog(shader);
	log("Error while compiling " + id + ":");
	log(shader);

	gl.deleteShader(shader);
        return 0;
    }

    return shader;
}

// Convenience function to create a program from all the passed-in
// shader ids.  We use the Javascript "arguments" builtin to access all
// the arguments passed to the function.
function getProgram() {
    var shaders = [];

    // first load and compile all the passed-in shaders.
    for (var i = 0; i < arguments.length; i++) {
	var shader = getShader(arguments[i]);
	if (shader == 0)
	    return 0;
	shaders.push(shader);
    }

    // then do the program object creation
    var program = gl.createProgram();
    if (program == 0)
	return 0;

    // attach all the shaders
    for (var i = 0; i < shaders.length; i++) {
	gl.attachShader(program, shaders[i]);
    }

    // link, and check for errors
    gl.linkProgram(program);

    var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
    if (!linked) {
	var error = gl.getProgramInfoLog(program);
	log("Error while linking: " + error);
	return 0;
    }

    return program;
}

