Files
MapLibreParticleLab/scripts/generate-shaders.js
Molly Lloyd 692f5e0a9f Pack min + max into one attribute 💪
Some devices supported by Mapbox GL provide only 8 vertex attributes; this change packs existing attributes to get us just under that limit.

For properties using a composite function, pack the min and max values into a single attribute with two logical components instead of using two separate attributes and buffers. Special logic is included for color attributes, whose integer components must be packed into the available bits of floating-point attributes. (We don't have access to ivec types in GL ES 2.0.)

For source functions, continue to bind just a one-component attribute even though the GLSL type is vec2 (or vec4 for colors). The type-checking done by gl::Attribute is relaxed slightly to accommodate this.
2017-03-08 15:23:25 -08:00

135 lines
3.5 KiB
JavaScript
Executable File

#!/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',
'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) => {
const a_type = type === "float" ? "vec2" : "vec4";
return pragmas[operation]
.join("\n")
.replace(/\{type\}/g, type)
.replace(/\{a_type}/g, a_type)
.replace(/\{precision\}/g, precision)
.replace(/\{name\}/g, name);
});
}
function vertexSource() {
const source = fs.readFileSync(path.join(inputPath, shaderName + '.vertex.glsl'), 'utf8');
return applyPragmas(source, {
define: [
"uniform lowp float a_{name}_t;",
"attribute {precision} {a_type} a_{name};",
"varying {precision} {type} {name};"
],
initialize: [
"{name} = unpack_mix_{a_type}(a_{name}, a_{name}_t);"
]
});
}
function fragmentSource() {
const source = fs.readFileSync(path.join(inputPath, shaderName + '.fragment.glsl'), 'utf8');
return applyPragmas(source, {
define: [
"varying {precision} {type} {name};"
],
initialize: [
]
});
}
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
`);
});