summaryrefslogtreecommitdiff
path: root/bin/build-shaders.js
blob: 61d182b03f602660fd5420bd1227c1b5c702ddff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env node
'use strict';

var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');

try { var glsl = require('glsl-optimizer'); } catch(err) {}

module.exports = function(shader_type, prefix, suffix) {
    var name;
    var shaders = {};

    var shaderFiles = fs.readdirSync('src/shader');

    // Load shaders
    for (var i = 0; i < shaderFiles.length; i++) {
        var parts = shaderFiles[i].match(/^(.+)\.(vertex|fragment)\.glsl$/);
        if (parts) {
            name = parts[1];
            var type = parts[2];
            if (!(name in shaders)) {
                shaders[name] = {};
            }
            shaders[name][type] = fs.readFileSync(path.join('src/shader', shaderFiles[i]), 'utf8');
        }
    }

    var preamble = '';
    if (shader_type == 'gles2' || shader_type == 'gles3') {
        preamble = 'precision highp float;';
    } else {
        preamble = '#version 120';
    }

    for (var name in shaders) {
        shaders[name].vertex = preamble + '\n' + shaders[name].vertex;
        shaders[name].fragment = preamble + '\n' + shaders[name].fragment;
    }

    // Optimize shader
    if (glsl) {
        var target = shader_type == 'gles2' ? glsl.TARGET_OPENGLES20 : (shader_type == 'gles3' ? glsl.TARGET_OPENGLES30 : glsl.TARGET_OPENGL);

        var compiler = new glsl.Compiler(target);
        for (name in shaders) {
            var vertex_shader = new glsl.Shader(compiler, glsl.VERTEX_SHADER, shaders[name].vertex);
            if (vertex_shader.compiled()) {
                shaders[name].vertex = vertex_shader.output();
            } else {
                console.warn('failed to optimize %s vertex shader', name);
                process.exit(1);
            }
            vertex_shader.dispose();

            var fragment_shader = new glsl.Shader(compiler, glsl.FRAGMENT_SHADER, shaders[name].fragment);
            if (fragment_shader.compiled()) {
                shaders[name].fragment = fragment_shader.output();
            } else {
                console.warn('failed to optimize %s fragment shader', name);
                process.exit(1);
            }
            fragment_shader.dispose();
        }
        compiler.dispose();
    } else {
        console.warn('Not optimizing shaders');
    }


    // Save to file
    var lines = [];
    var consts = [];
    for (var name in shaders) {
        consts.push(name.toUpperCase() + '_SHADER');

        var line = '';
        line += '   {\n';
        line += '       ' + JSON.stringify(shaders[name].vertex) + ',\n';
        line += '       ' + JSON.stringify(shaders[name].fragment) + ',\n';
        line += '   }';
        lines.push(line);
    }

    var header = '// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n\n';
    header += '#ifndef LLMR_SHADER_SHADERS\n';
    header += '#define LLMR_SHADER_SHADERS\n';
    header += '\n';
    header += 'namespace llmr {\n';
    header += '\n';
    header += 'struct shader_source {\n';
    header += '    const char *vertex;\n';
    header += '    const char *fragment;\n';
    header += '};\n';
    header += '\n';
    header += 'enum {\n';
    consts.push('SHADER_COUNT');
    header += '    ' + consts.join(',\n    ') + '\n';
    header += '};\n';
    header += '\n';
    header += 'extern const shader_source shaders[SHADER_COUNT];\n';
    header += '\n';
    header += '}\n';
    header += '\n';
    header += '#endif\n';


    var code = '// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n';
    code += '#include <llmr/platform/gl.hpp>\n';
    code += prefix + '\n';
    code += '#include <llmr/shader/shaders.hpp>\n';
    code += '\n';
    code += 'using namespace llmr;\n';
    code += '\n';
    code += 'const shader_source llmr::shaders[SHADER_COUNT] = {\n';
    code += lines.join(',\n');
    code += '\n};\n';
    code += suffix + '\n';

    var header_path = path.join(process.argv[2], 'include/llmr/shader/shaders.hpp');
    mkdirp.sync(path.dirname(header_path));
    fs.writeFileSync(header_path, header);
    console.warn('wrote file ' + header_path);
    var file_path = path.join(process.argv[2], 'src/shader/shaders_' + shader_type + '.cpp');
    mkdirp.sync(path.dirname(file_path));
    fs.writeFileSync(file_path, code);
    console.warn('wrote file ' + file_path);

};

module.exports('gl', '#ifndef GL_ES_VERSION_2_0', '#endif');
module.exports('gles2', '#ifdef GL_ES_VERSION_2_0', '#endif');