<commit_before>// Provides a convenient wrapper for WebGL shaders. A few uniforms and attributes,
// prefixed with `gl_`, are automatically added to all shader sources to make
// simple shaders easier to write.
// 
// Example usage:
// 
//     var shader = new Shader('\
//         void main() {\
//             gl_Position = gl_ModelViewProjectionMatrix\
//                           * vec4(gl_Vertex, 1.0);\
//         }\
//     ', '\
//         uniform vec3 color;\
//         void main() {\
//             gl_FragColor = vec4(color, 1.0);\
//         }\
//     ');
// 
//     shader.uniforms({
//         color: [1, 0, 0]
//     }).draw(mesh);

// ### new Shader(vertexSource, fragmentSource)
// 
// Compiles a shader program using the provided vertex and fragment shaders.
Shader = function(vertexSource, fragmentSource) {
    // Headers are prepended to the sources to provide some automatic functionality.
    var header = '\
        uniform mat4 gl_ModelViewMatrix;\
        uniform mat4 gl_ProjectionMatrix;\
        uniform mat4 gl_ModelViewProjectionMatrix;\
    ';
    var vertexHeader = '\
        attribute vec3 gl_Vertex;\
        attribute vec2 gl_TexCoord;\
        attribute vec3 gl_Normal;\
    ' + header;
    var fragmentHeader = '\
        precision highp float;\
    ' + header;

    // The `gl_` prefix must be substituted for something else to avoid compile errors,
    // since it's a reserved prefix. This prefixes all reserved names with `_`.
    function regexMap(regex, text, callback) {
        while ((result = regex.exec(text)) != null) {
            callback(result);
        }
    }
    function fix(header, source) {
        source = header + source;
        regexMap(/\bgl_\w+\b/g, header, function(result) {
            source = source.replace(new RegExp(result, 'g'), '_' + result);
        });
        return source;
    }
    vertexSource = fix(vertexHeader, vertexSource);
    fragmentSource = fix(fragmentHeader, fragmentSource);

    // Compile and link errors are thrown as strings, which are later caught and passed
    // to `fail()`.
    function compileSource(type, source) {
        var shader = gl.createShader(type);
        gl.shaderSource(shader, source);
        gl.compileShader(shader);
        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            throw 'compile error: ' + gl.getShaderInfoLog(shader);
        }<commit_msg>added regex to insert glsl header after extensions<commit_after>// Provides a convenient wrapper for WebGL shaders. A few uniforms and attributes,
// prefixed with `gl_`, are automatically added to all shader sources to make
// simple shaders easier to write.
// 
// Example usage:
// 
//     var shader = new Shader('\
//         void main() {\
//             gl_Position = gl_ModelViewProjectionMatrix\
//                           * vec4(gl_Vertex, 1.0);\
//         }\
//     ', '\
//         uniform vec3 color;\
//         void main() {\
//             gl_FragColor = vec4(color, 1.0);\
//         }\
//     ');
// 
//     shader.uniforms({
//         color: [1, 0, 0]
//     }).draw(mesh);

// ### new Shader(vertexSource, fragmentSource)
// 
// Compiles a shader program using the provided vertex and fragment shaders.
Shader = function(vertexSource, fragmentSource) {
    // Headers are prepended to the sources to provide some automatic functionality.
    var header = '\
        uniform mat4 gl_ModelViewMatrix;\
        uniform mat4 gl_ProjectionMatrix;\
        uniform mat4 gl_ModelViewProjectionMatrix;\
    ';
    var vertexHeader = '\
        attribute vec3 gl_Vertex;\
        attribute vec2 gl_TexCoord;\
        attribute vec3 gl_Normal;\
    ' + header;
    var fragmentHeader = '\
        precision highp float;\
    ' + header;

    // The `gl_` prefix must be substituted for something else to avoid compile errors,
    // since it's a reserved prefix. This prefixes all reserved names with `_`.
    function regexMap(regex, text, callback) {
        while ((result = regex.exec(text)) != null) {
            callback(result);
        }
    }
    // Insert the header after any extensions, since those must come first.
    function fix(header, source) {
        var match = /^((\s*\/\/.*\n|\s*#extension.*\n)+)[^]*$/.exec(source);
        source = match ? match[1] + header + source.substr(match[1].length) : header + source;
        regexMap(/\bgl_\w+\b/g, header, function(result) {
            source = source.replace(new RegExp(result, 'g'), '_' + result);
        });
        return source;
    }
    vertexSource = fix(vertexHeader, vertexSource);
    fragmentSource = fix(fragmentHeader, fragmentSource);

    // Compile and link errors are thrown as strings, which are later caught and passed
    // to `fail()`.
    function compileSource(type, source) {
        var shader = gl.createShader(type);
        gl.shaderSource(shader, source);
        gl.compileShader(shader);
        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            throw 'compile error: ' + gl.getShaderInfoLog(shader);
        }
{'fraction_non_alphanumeric': 0.12329038652130822, 'fraction_numerical': 0.006739345887016849, 'mean_word_length': 2.2099236641221376, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '174754', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1396, 'n_words': 425}