summaryrefslogtreecommitdiff
path: root/scripts/generate-shaders.js
blob: 7660a6e66e9d2ed5e5532364181a0b64a78d7e03 (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
#!/usr/bin/env node

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

var inputPath = 'mapbox-gl-js/shaders';
var outputPath = 'src/mbgl/shaders';

require('./style-code');

[
    'circle',
    'collision_box',
    'debug',
    'fill',
    'fill_outline',
    'fill_outline_pattern',
    'fill_pattern',
    'line',
    'line_pattern',
    'line_sdf',
    'raster',
    'symbol_icon',
    'symbol_sdf'
].forEach(function (shaderName) {
    function applyPragmas(source, pragmas) {
        return source.replace(/#pragma mapbox: ([\w]+) ([\w]+) ([\w]+) ([\w]+)/g, (match, operation, precision, type, name) => {
            return pragmas[operation]
                .join("\n")
                .replace(/\{type\}/g, type)
                .replace(/\{precision\}/g, precision)
                .replace(/\{name\}/g, name);
        });
    }

    function vertexSource() {
        var prelude = fs.readFileSync(path.join(inputPath, '_prelude.vertex.glsl'));
        var source = fs.readFileSync(path.join(inputPath, shaderName + '.vertex.glsl'), 'utf8');
        return prelude + applyPragmas(source, {
                define: [
                    "uniform lowp float a_{name}_t;",
                    "attribute {precision} {type} a_{name}_min;",
                    "attribute {precision} {type} a_{name}_max;",
                    "varying {precision} {type} {name};"
                ],
                initialize: [
                    "{name} = mix(a_{name}_min, a_{name}_max, a_{name}_t);"
                ]
            });
    }

    function fragmentSource() {
        var prelude = fs.readFileSync(path.join(inputPath, '_prelude.fragment.glsl'));
        var source = fs.readFileSync(path.join(inputPath, shaderName + '.fragment.glsl'), 'utf8');
        return prelude + applyPragmas(source, {
                define: [
                    "varying {precision} {type} {name};"
                ],
                initialize: [
                ]
            });
    }

    var content = "#pragma once\n" +
    "\n" +
    "// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n" +
    "\n" +
    "#include <mbgl/gl/gl.hpp>\n" +
    "\n" +
    "namespace mbgl {\n" +
    "namespace shaders {\n" +
    "\n" +
    "class " + shaderName + " {\n" +
    "public:\n" +
    "    static constexpr const char* name = \"" + shaderName + "\";\n" +
    "    static constexpr const char* vertexSource = R\"MBGL_SHADER(\n" + vertexSource() + ")MBGL_SHADER\";\n" +
    "    static constexpr const char* fragmentSource = R\"MBGL_SHADER(\n" + fragmentSource() + ")MBGL_SHADER\";\n" +
    "};\n" +
    "\n" +
    "} // namespace shaders\n" +
    "} // namespace mbgl\n";

    writeIfModified(path.join(outputPath, shaderName + '.hpp'), content);
});