forked from weatherlayers/weatherlayers-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup-plugin-glsl-minify.js
More file actions
130 lines (111 loc) · 3.43 KB
/
rollup-plugin-glsl-minify.js
File metadata and controls
130 lines (111 loc) · 3.43 KB
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
// customized rollup-plugin-glsl
//
// changes:
// - mangleShader - mangle tokens
// - generateCode - export code and tokens
import { createFilter } from 'rollup-pluginutils';
import { GlslMinify } from 'webpack-glsl-minify/build/minify.js';
import { nodeReadFile, nodeDirname } from 'webpack-glsl-minify/build/node.js';
import { fileURLToPath } from 'node:url';
const modules = [
{
structName: 'bitmap2Uniforms',
uniformBufferName: 'bitmap2',
path: './src/deck/shaderlib/bitmap-module/bitmap-module.glsl',
},
{
structName: 'rasterUniforms',
uniformBufferName: 'raster',
path: './src/deck/shaderlib/raster-module/raster-module.glsl',
},
{
structName: 'paletteUniforms',
uniformBufferName: 'palette',
path: './src/deck/shaderlib/palette-module/palette-module.glsl',
},
{
structName: 'contourUniforms',
uniformBufferName: 'contour',
path: './src/deck/layers/contour-layer/contour-module.glsl',
},
{
structName: 'particleUniforms',
uniformBufferName: 'particle',
path: './src/deck/layers/particle-layer/particle-module.glsl',
},
];
const modulesGlsl = modules.map(module => `@include "${module.path}"`).join('\n');
async function minifyShader(code, id, minimize) {
const nomangle = [
// don't mangle WebGL2 functions
'texture',
'floatBitsToUint',
// don't mangle deck.gl names
'PI',
'EARTH_RADIUS',
'geometry',
'uv',
'DECKGL_FILTER_COLOR',
// don't mangle deck.gl shader modules
'layer',
'opacity',
'picking',
'isActive',
'isAttribute',
// don't mangle shader modules
...modules.map(module => [module.structName, module.uniformBufferName]).flat(),
];
const glsl = new GlslMinify({
preserveDefines: true,
preserveAll: !minimize,
nomangle: nomangle,
}, nodeReadFile, nodeDirname);
// minify shader modules in the same GlslMinify instance to get their tokens
await glsl.executeFile({ path: fileURLToPath(import.meta.url), contents: modulesGlsl });
// minify shader
const result = await glsl.executeFile({ path: id, contents: code });
const tokens = getTokens(glsl.tokens);
return { sourceCode: result.sourceCode, tokens };
}
// use instead of tokenMap.getUniforms, because it doesn't support uniforms in UBOs (missing variableType) and nomangle in UBOs (not renamed)
function getTokens(tokenMap) {
// filter only the tokens that have the type field set, or that have been renamed
const tokens = {};
for (const original in tokenMap.tokens) {
const token = tokenMap.tokens[original];
if (token.variableType || original !== token.variableName || tokenMap.options.nomangle.includes(original)) {
tokens[original] = token.variableName;
}
}
return tokens;
}
function generateCode(result) {
return `
// eslint-disable
export const sourceCode = ${JSON.stringify(result.sourceCode)};
export const tokens = ${JSON.stringify(result.tokens)};
`;
}
export default function glsl(userOptions = {}) {
const options = Object.assign({
include: [
'**/*.vs',
'**/*.fs',
'**/*.vert',
'**/*.frag',
'**/*.glsl',
],
}, userOptions);
const filter = createFilter(options.include, options.exclude);
return {
name: 'glsl-minify',
async transform(code, id) {
if (!filter(id)) return;
const result = await minifyShader(code, id, options.minimize);
return {
code: generateCode(result),
map: { mappings: '' }
};
},
};
}