<commit_before>// Rendering modes

var GL = require('./gl.js');
var GLBuilders = require('./gl_builders.js');
var GLGeometry = require('./gl_geom.js').GLGeometry;
var shader_sources = require('./gl_shaders.js'); // built-in shaders

// Base

var RenderMode = {
    init: function (gl) {
        this.gl = gl;
        this.gl_program = this.makeGLProgram();
    },
    // state: {},
    // updateState: function (new_state) {
    //     this.state = this.state || {};
    //     if (new_state != null) {
    //         for (var k in new_state) {
    //             this.state[k] = this.new_state[k];
    //         }
    //     }
    //     return this.state;
    // },
    defines: {},
    buildPolygons: function(){}, // build functions are no-ops until overriden
    buildLines: function(){},
    buildPoints: function(){}
};

// TODO: allow mode programs to be recompiled
RenderMode.makeGLProgram = function ()
{
    // Add any custom defines to built-in mode defines
    var defines = {}; // create a new object to avoid mutating a prototype value that may be shared with other modes
    if (this.defines != null) {
        for (var d in this.defines) {
            defines[d] = this.defines[d];
        }
    }
    if (this.shaders != null && this.shaders.defines != null) {
        for (var d in this.shaders.defines) {
            defines[d] = this.shaders.defines[d];
        }
    }

    // Get any custom code transforms
    var transforms = (this.shaders && this.shaders.transforms);

    // Create shader from custom URLs
    if (this.shaders && this.shaders.vertex_url && this.shaders.fragment_url) {
        return new GL.Program.createProgramFromURLs(
            this.gl,
            this.shaders.vertex_url,
            this.shaders.fragment_url,
            { defines: defines, transforms: transforms }
        );
    }
    // Create shader from built-in source
    else {
        return new GL.Program(
            this.gl,
            shader_sources[this.vertex_shader_key],
            shader_sources[this.fragment_shader_key],
            { defines: defines, transforms: transforms }
        );
    }
};

RenderMode.updateUniforms = function ()
{
    // TODO: only update uniforms when changed
    if (this.uniforms != null) {
        for (var u in this.uniforms) {
            // Single float
            if (typeof this.uniforms[u] == 'number') {
                this.gl_program.uniform('1f', u, this.uniforms[u]);
            }
            else if (typeof this.uniforms[u] == 'object') {
                // float vectors (vec2, vec3, vec4)
                if (this.uniforms[u].length >= 2 && this.uniforms[u].length <= 4) {
                    this.gl_program.uniform(this.uniforms[u].length + 'fv', u, this.uniforms[u]);
                }
                // TODO: support arrays for more than 4 components
                // TODO: assume matrix for (typeof == Float32Array && length == 16)?
                // TODO: support non-float types? (int, texture sampler, etc.)
                // this.gl_program.uniform('1fv', u, this.uniforms[u]);
            }
        }
    }
};

RenderMode.update = function ()
{
    // Mode-specific animation
    if (typeof this.animation == 'function') {
        this.animation();
    }

    this.updateUniforms();
};


var Modes = {};
var ModeManager = {};

// Update built-in mode or create a new one
ModeManager.configureMode = function (name, settings)
{
    Modes[name] = Modes[name] || Object.create(Modes[settings.extends] || RenderMode);
    for (var s in settings) {
        Modes[name][s] = settings[s];
    }
    return Modes[name];
};


// Built-in rendering modes

/*** Plain polygons ***/

Modes.polygons = Object.create(RenderMode);

// Modes.polygons.init = function (gl)
// {
//     RenderMode.init.apply(this, arguments);
//     // this.state.colors = {};
// };

// Count uses of colors
// Modes.polygons.countColor = function (color)
// {g
//     var k = color.join(',');
//     if (this.state.colors[k] != null) {
//         this.state.colors[k]++;
//     }
//     else {
//         this.state.colors[k] = 1;
//     }
// };

Modes.polygons.vertex_shader_key = 'polygon_vertex';
Modes.polygons.fragment_shader_key = 'polygon_fragment';

Modes.polygons.uniforms = {
    // scale: 1.0
};

Modes.polygons.makeGLGeometry = function (vertex_data)
{
    var geom = new GLGeometry(this.gl, this.gl_program, vertex_data, [
        { name: 'a_position', size: 3, type: gl.FLOAT, normalized: false },
        { name: 'a_normal', size: 3, type: gl.FLOAT, normalized: false },
        { name: 'a_color', size: 3, type: gl.FLOAT, normalized: false },
        { name: 'a_layer', size: 1, type: gl.FLOAT, normalized: false }
    ]);
    geom.geometry_count = geom.vertex_count / 3;

    return geom;
};

Modes.polygons.buildPolygons = function (polygons, style, vertex_data)
{
    // Color and layer number are currently constant across vertices
    var vertex_constants = [
        style.color[0], style.color[1], style.color[2],
        style.layer_num
    ];
    // this.countColor(style.color);

    // Outlines have a slightly different set of constants, because the layer number is modified
    if (style.outline.color) {<commit_msg>stylesheet uniforms should be under 'shaders' block<commit_after>// Rendering modes

var GL = require('./gl.js');
var GLBuilders = require('./gl_builders.js');
var GLGeometry = require('./gl_geom.js').GLGeometry;
var shader_sources = require('./gl_shaders.js'); // built-in shaders

// Base

var RenderMode = {
    init: function (gl) {
        this.gl = gl;
        this.gl_program = this.makeGLProgram();
    },
    // state: {},
    // updateState: function (new_state) {
    //     this.state = this.state || {};
    //     if (new_state != null) {
    //         for (var k in new_state) {
    //             this.state[k] = this.new_state[k];
    //         }
    //     }
    //     return this.state;
    // },
    defines: {},
    buildPolygons: function(){}, // build functions are no-ops until overriden
    buildLines: function(){},
    buildPoints: function(){}
};

// TODO: allow mode programs to be recompiled
RenderMode.makeGLProgram = function ()
{
    // Add any custom defines to built-in mode defines
    var defines = {}; // create a new object to avoid mutating a prototype value that may be shared with other modes
    if (this.defines != null) {
        for (var d in this.defines) {
            defines[d] = this.defines[d];
        }
    }
    if (this.shaders != null && this.shaders.defines != null) {
        for (var d in this.shaders.defines) {
            defines[d] = this.shaders.defines[d];
        }
    }

    // Get any custom code transforms
    var transforms = (this.shaders && this.shaders.transforms);

    // Create shader from custom URLs
    if (this.shaders && this.shaders.vertex_url && this.shaders.fragment_url) {
        return new GL.Program.createProgramFromURLs(
            this.gl,
            this.shaders.vertex_url,
            this.shaders.fragment_url,
            { defines: defines, transforms: transforms }
        );
    }
    // Create shader from built-in source
    else {
        return new GL.Program(
            this.gl,
            shader_sources[this.vertex_shader_key],
            shader_sources[this.fragment_shader_key],
            { defines: defines, transforms: transforms }
        );
    }
};

RenderMode.updateUniforms = function ()
{
    // TODO: only update uniforms when changed
    if (this.shaders != null && this.shaders.uniforms != null) {
        for (var u in this.shaders.uniforms) {
            // Single float
            if (typeof this.shaders.uniforms[u] == 'number') {
                this.gl_program.uniform('1f', u, this.shaders.uniforms[u]);
            }
            else if (typeof this.shaders.uniforms[u] == 'object') {
                // float vectors (vec2, vec3, vec4)
                if (this.shaders.uniforms[u].length >= 2 && this.shaders.uniforms[u].length <= 4) {
                    this.gl_program.uniform(this.shaders.uniforms[u].length + 'fv', u, this.shaders.uniforms[u]);
                }
                // TODO: support arrays for more than 4 components
                // TODO: assume matrix for (typeof == Float32Array && length == 16)?
                // TODO: support non-float types? (int, texture sampler, etc.)
                // this.gl_program.uniform('1fv', u, this.shaders.uniforms[u]);
            }
        }
    }
};

RenderMode.update = function ()
{
    // Mode-specific animation
    if (typeof this.animation == 'function') {
        this.animation();
    }

    this.updateUniforms();
};


var Modes = {};
var ModeManager = {};

// Update built-in mode or create a new one
ModeManager.configureMode = function (name, settings)
{
    Modes[name] = Modes[name] || Object.create(Modes[settings.extends] || RenderMode);
    for (var s in settings) {
        Modes[name][s] = settings[s];
    }
    return Modes[name];
};


// Built-in rendering modes

/*** Plain polygons ***/

Modes.polygons = Object.create(RenderMode);

// Modes.polygons.init = function (gl)
// {
//     RenderMode.init.apply(this, arguments);
//     // this.state.colors = {};
// };

// Count uses of colors
// Modes.polygons.countColor = function (color)
// {g
//     var k = color.join(',');
//     if (this.state.colors[k] != null) {
//         this.state.colors[k]++;
//     }
//     else {
//         this.state.colors[k] = 1;
//     }
// };

Modes.polygons.vertex_shader_key = 'polygon_vertex';
Modes.polygons.fragment_shader_key = 'polygon_fragment';

// Modes.polygons.shaders.uniforms = {
//     // scale: 1.0
// };

Modes.polygons.makeGLGeometry = function (vertex_data)
{
    var geom = new GLGeometry(this.gl, this.gl_program, vertex_data, [
        { name: 'a_position', size: 3, type: gl.FLOAT, normalized: false },
        { name: 'a_normal', size: 3, type: gl.FLOAT, normalized: false },
        { name: 'a_color', size: 3, type: gl.FLOAT, normalized: false },
        { name: 'a_layer', size: 1, type: gl.FLOAT, normalized: false }
    ]);
    geom.geometry_count = geom.vertex_count / 3;

    return geom;
};

Modes.polygons.buildPolygons = function (polygons, style, vertex_data)
{
    // Color and layer number are currently constant across vertices
    var vertex_constants = [
        style.color[0], style.color[1], style.color[2],
        style.layer_num
    ];
    // this.countColor(style.color);

    // Outlines have a slightly different set of constants, because the layer number is modified
    if (style.outline.color) {
{'fraction_non_alphanumeric': 0.1438985136798258, 'fraction_numerical': 0.004354823440310518, 'mean_word_length': 2.2089914945321993, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3993047', 'n_tokens_mistral': 3344, 'n_tokens_neox': 3264, 'n_words': 790}