<commit_before>// Copyright (c) 2011 Hewlett-Packard Development Company, L.P. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var WebVfx = WebVfx || {};


////////

// canvas - canvas HTML element
// shaderSource - fragment shader source code
// uniforms - optional hash mapping uniform name to initial value
WebVfx.Shader = function (canvas, shaderSource, uniforms) {
    this.gl = canvas.getContext("experimental-webgl");
    if (!this.gl)
        throw "This browswer does not have WebGL enabled.";
    this.program = this.compileProgram(shaderSource);
    this.uniforms = this.extractUniforms();
    // Set default values if specified
    if (uniforms) {
        this.gl.useProgram(this.program);
        for (var name in uniforms)
            this.uniforms[name].setValue(uniforms[name]);
    }

    this.buildQuad();
}

// Return the script text from a <script type="x-shader/x-fragment">
// element of the given ID.
WebVfx.Shader.loadShader = function (shaderId) {
    var shaderScript = document.getElementById(shaderId);
    if (!shaderScript || shaderScript.type != "x-shader/x-fragment") {
        throw "Could not find shader script ID " + shaderId +
                    " of type x-shader/x-fragment";
    }
    return shaderScript.text;
}

// Update an existing uniforms value
WebVfx.Shader.prototype.updateUniform = function (name, value) {
    var uniform = this.uniforms[name];
    if (!uniform) {
        console.warn("Invalid uniform name " + name);
        return;
    }
    this.gl.useProgram(this.program);
    uniform.setValue(value);
}

WebVfx.Shader.VERTEX_SHADER_SOURCE = [
    "attribute vec2 vertex;",
    "varying vec2 texCoord;",
    "void main() {",
    "    texCoord = vertex;",
    "    gl_Position = vec4(vertex * 2.0 - 1.0, 0.0, 1.0);",
    "}"].join("\n");

WebVfx.Shader.prototype.compileProgram = function (shaderSource) {
    var gl = this.gl;
    var program = gl.createProgram();

    var vs = this.compileSource(gl.VERTEX_SHADER,
                                WebVfx.Shader.VERTEX_SHADER_SOURCE);
    var fs = this.compileSource(gl.FRAGMENT_SHADER, shaderSource);
    gl.attachShader(program, vs);
    gl.attachShader(program, fs);
    gl.linkProgram(program);
    if (!gl.getProgramParameter(program, gl.LINK_STATUS))
        throw 'link error: ' + gl.getProgramInfoLog(program);

    gl.deleteShader(vs);
    gl.deleteShader(fs);

    return program;
}

WebVfx.Shader.prototype.compileSource = function (type, shaderSource) {
    var gl = this.gl;
    var shader = gl.createShader(type);
    gl.shaderSource(shader, shaderSource);
    gl.compileShader(shader);
    if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
        throw 'compile error: ' + gl.getShaderInfoLog(shader);
    return shader;
}

// Extract uniform declarations from program
WebVfx.Shader.prototype.extractUniforms = function () {
    var gl = this.gl;
    var textureCount = 0;
    var uniforms = {};

    gl.useProgram(this.program);

    var uniformCount = gl.getProgramParameter(this.program, gl.ACTIVE_UNIFORMS);
    for (var u = 0; u < uniformCount; u++) {
        var info = gl.getActiveUniform(this.program, u);
        var location = gl.getUniformLocation(this.program, info.name);

        if (info.type == gl.SAMPLER_2D) {
            uniforms[info.name] = new WebVfx.Texture(gl, textureCount++,
                                                     location);
        }
        else
            uniforms[info.name] = new WebVfx.Uniform(gl, info.type, location);
    }
    return uniforms;
}

WebVfx.Shader.prototype.buildQuad = function () {
    var gl = this.gl;

    this.vertexBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
    gl.bufferData(gl.ARRAY_BUFFER,
                  new Float32Array([ 0, 0, 0, 1, 1, 0, 1, 1 ]),
                  gl.STATIC_DRAW);

    this.vertexAttribute = gl.getAttribLocation(this.program, 'vertex');
    gl.enableVertexAttribArray(this.vertexAttribute);
}

WebVfx.Shader.prototype.render = function () {
    var gl = this.gl;

    // Should use gl.drawingBufferWidth/Height but they aren't implemented
    gl.viewport(0, 0, gl.canvas.clientWidth, gl.canvas.clientHeight);

    gl.useProgram(this.program);
    gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
    gl.vertexAttribPointer(this.vertexAttribute, 2, gl.FLOAT, false, 0, 0);
    gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}


////////

// type - uniform type
// location - uniform location
WebVfx.Uniform = function (gl, type, location) {
    this.gl = gl;

    this.uniformLocation = location;

    switch (type) {
    case gl.FLOAT:
        this.uniformFunction = gl.uniform1f;
        break;
    case gl.INT:
    case gl.BOOL:
        this.uniformFunction = gl.uniform1i;<commit_msg>Split Renderer out of Shader.<commit_after>// Copyright (c) 2011 Hewlett-Packard Development Company, L.P. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var WebVfx = WebVfx || {};


////////

// canvas - canvas HTML element
WebVfx.Renderer = function (canvas) {
    this.gl = canvas.getContext("experimental-webgl");
    if (!this.gl)
        throw "This browswer does not have WebGL enabled.";
    this.buildQuad();
}

WebVfx.Renderer.prototype.buildQuad = function () {
    var gl = this.gl;

    this.vertexBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
    gl.bufferData(gl.ARRAY_BUFFER,
                  new Float32Array([ 0, 0, 0, 1, 1, 0, 1, 1 ]),
                  gl.STATIC_DRAW);
}

// shader - Shader to render
// target - optional RenderTarget, if not specified then render to canvas
WebVfx.Renderer.prototype.render = function (shader, target) {
    var gl = this.gl;

//XXX handle target
    // Should use gl.drawingBufferWidth/Height but they aren't implemented
    gl.viewport(0, 0, gl.canvas.clientWidth, gl.canvas.clientHeight);

    gl.useProgram(shader.program);
    gl.enableVertexAttribArray(shader.vertexAttribute);
    gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
    gl.disableVertexAttribArray(shader.vertexAttribute);
}

////////

// renderer - Renderer
// shaderSource - fragment shader source code
// uniforms - optional hash mapping uniform name to initial value
WebVfx.Shader = function (renderer, shaderSource, uniforms) {
    this.renderer = renderer;
    var gl = renderer.gl;
    this.program = this.compileProgram(shaderSource);
    this.uniforms = this.extractUniforms();
    // Set default values if specified
    if (uniforms) {
        gl.useProgram(this.program);
        for (var name in uniforms)
            this.uniforms[name].setValue(uniforms[name]);
    }
    this.vertexAttribute = gl.getAttribLocation(this.program, 'vertex');
    gl.vertexAttribPointer(this.vertexAttribute, 2, gl.FLOAT, false, 0, 0);
}

// Return the script text from a <script type="x-shader/x-fragment">
// element of the given ID.
WebVfx.Shader.loadShader = function (shaderId) {
    var shaderScript = document.getElementById(shaderId);
    if (!shaderScript || shaderScript.type != "x-shader/x-fragment") {
        throw "Could not find shader script ID " + shaderId +
                    " of type x-shader/x-fragment";
    }
    return shaderScript.text;
}

// Update an existing uniforms value
WebVfx.Shader.prototype.updateUniform = function (name, value) {
    var uniform = this.uniforms[name];
    if (!uniform) {
        console.warn("Invalid uniform name " + name);
        return;
    }
    this.renderer.gl.useProgram(this.program);
    uniform.setValue(value);
}

WebVfx.Shader.VERTEX_SHADER_SOURCE = [
    "attribute vec2 vertex;",
    "varying vec2 texCoord;",
    "void main() {",
    "    texCoord = vertex;",
    "    gl_Position = vec4(vertex * 2.0 - 1.0, 0.0, 1.0);",
    "}"].join("\n");

WebVfx.Shader.prototype.compileProgram = function (shaderSource) {
    var gl = this.renderer.gl;
    var program = gl.createProgram();

    var vs = this.compileSource(gl.VERTEX_SHADER,
                                WebVfx.Shader.VERTEX_SHADER_SOURCE);
    var fs = this.compileSource(gl.FRAGMENT_SHADER, shaderSource);
    gl.attachShader(program, vs);
    gl.attachShader(program, fs);
    gl.linkProgram(program);
    if (!gl.getProgramParameter(program, gl.LINK_STATUS))
        throw 'link error: ' + gl.getProgramInfoLog(program);

    gl.deleteShader(vs);
    gl.deleteShader(fs);

    return program;
}

WebVfx.Shader.prototype.compileSource = function (type, shaderSource) {
    var gl = this.renderer.gl;
    var shader = gl.createShader(type);
    gl.shaderSource(shader, shaderSource);
    gl.compileShader(shader);
    if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
        throw 'compile error: ' + gl.getShaderInfoLog(shader);
    return shader;
}

// Extract uniform declarations from program
WebVfx.Shader.prototype.extractUniforms = function () {
    var gl = this.renderer.gl;
    var textureCount = 0;
    var uniforms = {};

    gl.useProgram(this.program);

    var uniformCount = gl.getProgramParameter(this.program, gl.ACTIVE_UNIFORMS);
    for (var u = 0; u < uniformCount; u++) {
        var info = gl.getActiveUniform(this.program, u);
        var location = gl.getUniformLocation(this.program, info.name);

        if (info.type == gl.SAMPLER_2D) {
            uniforms[info.name] = new WebVfx.Texture(gl, textureCount++,
                                                     location);
        }
        else
            uniforms[info.name] = new WebVfx.Uniform(gl, info.type, location);
    }
    return uniforms;
}

////////

// type - uniform type
// location - uniform location
WebVfx.Uniform = function (gl, type, location) {
    this.gl = gl;

    this.uniformLocation = location;

    switch (type) {
    case gl.FLOAT:
        this.uniformFunction = gl.uniform1f;
        break;
    case gl.INT:
    case gl.BOOL:
        this.uniformFunction = gl.uniform1i;
{'fraction_non_alphanumeric': 0.12538839330460058, 'fraction_numerical': 0.00741705923624336, 'mean_word_length': 2.805491990846682, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '357401', 'n_tokens_mistral': 3136, 'n_tokens_neox': 3056, 'n_words': 729}