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

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

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

require('./style-code');

const vertexPrelude = fs.readFileSync(path.join(inputPath, '_prelude.vertex.glsl'));
const fragmentPrelude = fs.readFileSync(path.join(inputPath, '_prelude.fragment.glsl'));

writeIfModified(path.join(outputPath, 'preludes.hpp'), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.

#pragma once

namespace mbgl {
namespace shaders {

extern const char* vertexPrelude;
extern const char* fragmentPrelude;

} // namespace shaders
} // namespace mbgl
`);

writeIfModified(path.join(outputPath, 'preludes.cpp'), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.

#include <mbgl/shaders/preludes.hpp>

namespace mbgl {
namespace shaders {

const char* vertexPrelude = R"MBGL_SHADER(
${vertexPrelude}
)MBGL_SHADER";
const char* fragmentPrelude = R"MBGL_SHADER(
${fragmentPrelude}
)MBGL_SHADER";

} // namespace shaders
} // namespace mbgl
`);

[
    'circle',
    'collision_box',
    'debug',
    'extrusion_texture',
    'fill',
    'fill_extrusion',
    'fill_extrusion_pattern',
    'fill_outline',
    'fill_outline_pattern',
    'fill_pattern',
    'line',
    'line_pattern',
    'line_sdf',
    'raster',
    'symbol_icon',
    'symbol_sdf'
].forEach(function (shaderName) {
    const re = /#pragma mapbox: ([\w]+) ([\w]+) ([\w]+) ([\w]+)/g;
    const fragmentPragmas = new Set();

    let fragmentSource = fs.readFileSync(path.join(inputPath, shaderName + '.fragment.glsl'), 'utf8');
    let vertexSource = fs.readFileSync(path.join(inputPath, shaderName + '.vertex.glsl'), 'utf8');

    fragmentSource = fragmentSource.replace(re, (match, operation, precision, type, name) => {
        fragmentPragmas.add(name);
        return operation === "define" ?
            `varying ${precision} ${type} ${name};` :
            ``;
    });

    vertexSource = vertexSource.replace(re, (match, operation, precision, type, name) => {
        const a_type = type === "float" ? "vec2" : "vec4";
        if (fragmentPragmas.has(name)) {
            return operation === "define" ?
                `uniform lowp float a_${name}_t;\nattribute ${precision} ${a_type} a_${name};\nvarying ${precision} ${type} ${name};` :
                `${name} = unpack_mix_${a_type}(a_${name}, a_${name}_t);`;
        } else {
            return operation === "define" ?
                `uniform lowp float a_${name}_t;\nattribute ${precision} ${a_type} a_${name};` :
                `${precision} ${type} ${name} = unpack_mix_${a_type}(a_${name}, a_${name}_t);`;
        }
    });

    writeIfModified(path.join(outputPath, `${shaderName}.hpp`), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.

#pragma once

namespace mbgl {
namespace shaders {

class ${shaderName} {
public:
    static const char* name;
    static const char* vertexSource;
    static const char* fragmentSource;
};

} // namespace shaders
} // namespace mbgl
`);

    writeIfModified(path.join(outputPath, `${shaderName}.cpp`), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.

#include <mbgl/shaders/${shaderName}.hpp>

namespace mbgl {
namespace shaders {

const char* ${shaderName}::name = "${shaderName}";
const char* ${shaderName}::vertexSource = R"MBGL_SHADER(
${vertexSource}
)MBGL_SHADER";
const char* ${shaderName}::fragmentSource = R"MBGL_SHADER(
${fragmentSource}
)MBGL_SHADER";

} // namespace shaders
} // namespace mbgl
`);
});