summaryrefslogtreecommitdiff
path: root/scripts/generate-shaders.js
blob: 59544d0b348dc8c7aacc7e55df98d25ec4029b39 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env node

require('flow-remove-types/register');

const path = require('path');
const outputPath = 'src/mbgl/programs/gl';
const zlib = require('zlib');

var shaders = require('../mapbox-gl-js/src/shaders');

require('./style-code');

let concatenated = '';
let offsets = {};

function basicMinify(src) {
    return src = src.trim() // strip whitespace at the start/end
        .replace(/\s*\/\/[^\n]*\n/g, '\n') // strip double-slash comments
        .replace(/\n+/g, '\n') // collapse multi line breaks
        .replace(/\n\s+/g, '\n') // strip identation
        .replace(/\s?([+-\/*=,])\s?/g, '$1') // strip whitespace around operators
        .replace(/([;\(\),\{\}])\n(?=[^#])/g, '$1'); // strip more line breaks
}

for (const key in shaders) {
    const vertex = concatenated.length;
    concatenated += basicMinify(shaders[key].vertexSource);
    concatenated += '\n\0';

    const fragment = concatenated.length;
    concatenated += basicMinify(shaders[key].fragmentSource);
    concatenated += '\n\0';

    offsets[key] = {
        vertex,
        fragment,
        originalKey: key,
        shaderName: key.replace(/[A-Z]+/g, (match) => `_${match.toLowerCase()}`),
        ShaderName: key.replace(/^[a-z]/g, (match) => match.toUpperCase())
    };
}

// Overrides: The symbolSDF shader is used for two underlying programs.
offsets.symbolSDFIcon = {
    vertex: offsets.symbolSDF.vertex,
    fragment: offsets.symbolSDF.fragment,
    originalKey: 'symbolSDF',
    shaderName: 'symbol_sdf_icon',
    ShaderName: 'SymbolSDFIcon',
};

offsets.symbolSDFText = {
    vertex: offsets.symbolSDF.vertex,
    fragment: offsets.symbolSDF.fragment,
    originalKey: 'symbolSDF',
    shaderName: 'symbol_sdf_text',
    ShaderName: 'SymbolSDFText',
};

delete offsets.symbolSDF;

const compressed = zlib.deflateSync(concatenated, {level: zlib.Z_BEST_COMPRESSION})
    .toString('hex')
    .match(/.{1,32}/g)
    .map(line => line.match(/.{1,2}/g).map(n => `0x${n}`).join(', '))
    .join(',\n    ')
    .trim();

function sourceOffset(key, type) {
    return `programs::gl::shaderSource() + ${offsets[key][type]}`
}

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

#pragma once

namespace mbgl {
namespace programs {
namespace gl {

const char* shaderSource();

} // namespace gl
} // namespace programs
} // namespace mbgl
`);

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

#include <mbgl/programs/gl/shader_source.hpp>
#include <mbgl/util/compression.hpp>

#include <cstdint>

namespace mbgl {
namespace programs {
namespace gl {

constexpr const uint8_t compressedShaderSource[] = {
    ${compressed}
};

const char* shaderSource() {
    static std::string decompressed = util::decompress(std::string(reinterpret_cast<const char*>(compressedShaderSource), sizeof(compressedShaderSource)));
    return decompressed.c_str();
};

} // namespace gl
} // namespace programs
} // namespace mbgl
`);

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

#pragma once

namespace mbgl {
namespace programs {
namespace gl {

extern const char* vertexShaderPrelude;
extern const char* fragmentShaderPrelude;

} // namespace gl
} // namespace programs
} // namespace mbgl
`);

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

#include <mbgl/programs/gl/preludes.hpp>
#include <mbgl/programs/gl/shader_source.hpp>

namespace mbgl {
namespace programs {
namespace gl {

const char* vertexShaderPrelude = ${sourceOffset('prelude', 'vertex')};
const char* fragmentShaderPrelude = ${sourceOffset('prelude', 'fragment')};

} // namespace gl
} // namespace programs
} // namespace mbgl
`);

for (const key in offsets) {
    if (key === 'prelude')
        continue;

    const { shaderName, ShaderName, originalKey } = offsets[key];

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

#include <mbgl/programs/${shaderName}_program.hpp>
#include <mbgl/programs/gl/shader_source.hpp>
#include <mbgl/gl/program.hpp>

namespace mbgl {
namespace gfx {

template <>
std::unique_ptr<Program<${ShaderName}Program>>
Context::createProgram<gl::Context>(const ProgramParameters& programParameters) {
    return gl::Program<${ShaderName}Program>::createProgram(
        reinterpret_cast<gl::Context&>(*this), programParameters, "${shaderName}",
        ${sourceOffset(key, 'vertex')}, ${sourceOffset(key, 'fragment')});
}

} // namespace gfx
} // namespace mbgl

// Uncompressed source of ${shaderName}.vertex.glsl:
/*
${shaders[originalKey].vertexSource}
*/

// Uncompressed source of ${shaderName}.fragment.glsl:
/*
${shaders[originalKey].fragmentSource}
*/

`);
}