/
shader-assembler.spec.ts
233 lines (187 loc) · 6.28 KB
/
shader-assembler.spec.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// luma.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import test from 'tape-promise/tape';
import {ShaderAssembler, PlatformInfo, glsl, picking, dirlight} from '@luma.gl/shadertools';
const platformInfo: PlatformInfo = {
type: 'webgl',
gpu: 'test-gpu',
shaderLanguage: 'glsl',
shaderLanguageVersion: 300,
features: new Set()
};
const vs = glsl`\
#version 300 es
in vec4 positions;
void main(void) {
gl_Position = positions;
}
`;
const fs = glsl`\
#version 300 es
precision highp float;
out vec4 fragmentColor;
void main(void) {
fragmentColor = vec4(1.0, 1.0, 1.0, 1.0);
}
`;
/*
const VS_300 = glsl`\
#version 300 es
in vec4 positions;
in vec2 uvs;
out vec2 vUV;
void main() {
vUV = uvs;
gl_Position = positions;
}
`;
const FS_300 = glsl`\
#version 300 es
precision highp float;
in vec2 vUV;
uniform sampler2D tex;
out vec4 fragColor;
void main() {
fragColor = texture(tex, vUV);
}
`;
*/
test('ShaderAssembler#hooks', t => {
const shaderAssembler = new ShaderAssembler();
const preHookShaders = shaderAssembler.assembleGLSLShaderPair({platformInfo, vs, fs});
shaderAssembler.addShaderHook('vs:LUMAGL_pickColor(inout vec4 color)');
shaderAssembler.addShaderHook('fs:LUMAGL_fragmentColor(inout vec4 color)', {
header: 'if (color.a == 0.0) discard;\n',
footer: 'color.a *= 1.2;\n'
});
const assemblyResults = shaderAssembler.assembleGLSLShaderPair({platformInfo, vs, fs});
t.ok(preHookShaders !== assemblyResults, 'Adding hooks changes hash');
const pickingInjection = {
...picking,
instance: undefined,
inject: {
'vs:LUMAGL_pickColor': 'picking_setPickingColor(color.rgb);',
'fs:LUMAGL_fragmentColor': {
injection: 'color = picking_filterColor(color);',
order: Number.POSITIVE_INFINITY
}
}
};
const noModuleProgram = shaderAssembler.assembleGLSLShaderPair({platformInfo, vs, fs});
t.ok(preHookShaders !== noModuleProgram, 'Adding hooks changes hash');
const noModuleVs = noModuleProgram.vs;
const noModuleFs = noModuleProgram.fs;
t.ok(noModuleVs.indexOf('LUMAGL_pickColor') > -1, 'hook function injected into vertex shader');
t.ok(
noModuleFs.indexOf('LUMAGL_fragmentColor') > -1,
'hook function injected into fragment shader'
);
t.ok(
noModuleVs.indexOf('picking_setPickingColor(color.rgb)') === -1,
'injection code not included in vertex shader without module'
);
t.ok(
noModuleFs.indexOf('color = picking_filterColor(color)') === -1,
'injection code not included in fragment shader without module'
);
const modulesProgram = shaderAssembler.assembleGLSLShaderPair({
platformInfo,
vs,
fs,
modules: [pickingInjection]
});
const modulesVs = modulesProgram.vs;
const modulesFs = modulesProgram.fs;
t.ok(modulesVs.indexOf('LUMAGL_pickColor') > -1, 'hook function injected into vertex shader');
t.ok(
modulesFs.indexOf('LUMAGL_fragmentColor') > -1,
'hook function injected into fragment shader'
);
t.ok(
modulesVs.indexOf('picking_setPickingColor(color.rgb)') > -1,
'injection code included in vertex shader with module'
);
t.ok(
modulesFs.indexOf('color = picking_filterColor(color)') > -1,
'injection code included in fragment shader with module'
);
t.ok(
modulesFs.indexOf('if (color.a == 0.0) discard;') > -1,
'hook header injected into fragment shader'
);
t.ok(
modulesFs.indexOf('color.a *= 1.2;') > modulesFs.indexOf('color = picking_filterColor(color)'),
'hook footer injected after injection code'
);
const injectedShaders = shaderAssembler.assembleGLSLShaderPair({
platformInfo,
vs,
fs,
inject: {
'vs:LUMAGL_pickColor': 'color *= 0.1;',
'fs:LUMAGL_fragmentColor': 'color += 0.1;'
}
});
const injectVs = injectedShaders.vs;
const injectFs = injectedShaders.fs;
t.ok(injectVs.indexOf('color *= 0.1') > -1, 'argument injection code included in shader hook');
t.ok(injectFs.indexOf('color += 0.1') > -1, 'argument injection code included in shader hook');
const injectDefineProgram1 = shaderAssembler.assembleGLSLShaderPair({
platformInfo,
vs,
fs,
inject: {
'vs:LUMAGL_pickColor': 'color *= 0.1;'
}
});
const injectDefineProgram2 = shaderAssembler.assembleGLSLShaderPair({
platformInfo,
vs,
fs,
defines: {
'vs:LUMAGL_pickColor': 'color *= 0.1;'
}
});
t.ok(injectDefineProgram1 !== injectDefineProgram2, 'Injects and defines hashed separately.');
t.end();
});
test('ShaderAssembler#defaultModules', t => {
const shaderAssembler = new ShaderAssembler();
const program = shaderAssembler.assembleGLSLShaderPair({platformInfo, vs, fs});
const preDefaultModuleProgram = shaderAssembler.assembleGLSLShaderPair({
platformInfo,
vs,
fs,
modules: [dirlight]
});
const preDefaultModuleSource = preDefaultModuleProgram.fs;
shaderAssembler.addDefaultModule(dirlight);
const defaultModuleProgram = shaderAssembler.assembleGLSLShaderPair({platformInfo, vs, fs});
const moduleProgram = shaderAssembler.assembleGLSLShaderPair({
platformInfo,
vs,
fs,
modules: [dirlight]
});
t.notDeepEqual(program, defaultModuleProgram, 'Program with new default module properly cached');
t.deepEqual(preDefaultModuleProgram.vs, defaultModuleProgram.vs);
t.equal(preDefaultModuleProgram.fs, defaultModuleProgram.fs, 'Default module injected correctly');
t.equal(
moduleProgram.vs,
defaultModuleProgram.vs,
'Program with new default module matches regular module'
);
shaderAssembler.removeDefaultModule(dirlight);
const noDefaultModuleProgram = shaderAssembler.assembleGLSLShaderPair({platformInfo, vs, fs});
t.ok(program.fs === noDefaultModuleProgram.fs, 'Default module was removed');
t.ok(moduleProgram.fs !== noDefaultModuleProgram.fs, 'Default module was removed');
// Reset program manager
shaderAssembler.addDefaultModule(dirlight);
const uncachedProgram = shaderAssembler.assembleGLSLShaderPair({platformInfo, vs, fs});
const defaultModuleSource = uncachedProgram.fs;
// TODO - this deep equal thing doesn't make sense due to getUniforms
t.notDeepEqual(defaultModuleProgram, uncachedProgram, 'Program is not cached');
t.deepEqual(preDefaultModuleSource, defaultModuleSource, 'Default modules create correct source');
t.end();
});