@ignifx/core 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/async-pipelines-CI5uT5iZ.js +26 -0
- package/dist/async-pipelines-CI5uT5iZ.js.map +1 -0
- package/dist/color-DrR6GFTd.js +351 -0
- package/dist/color-DrR6GFTd.js.map +1 -0
- package/dist/effect-task-CMbuO6Kb.js +205 -0
- package/dist/effect-task-CMbuO6Kb.js.map +1 -0
- package/dist/environment-C_JfuAjj.js +224 -0
- package/dist/environment-C_JfuAjj.js.map +1 -0
- package/dist/ignifx-error-R1ma_Lzv.js +544 -0
- package/dist/ignifx-error-R1ma_Lzv.js.map +1 -0
- package/dist/index.d.ts +3778 -1658
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6349 -5585
- package/dist/index.js.map +1 -1
- package/dist/instanced-mesh-instance-Cgx1H87p.js +701 -0
- package/dist/instanced-mesh-instance-Cgx1H87p.js.map +1 -0
- package/dist/material-plugin-CTaWZXEk.js +485 -0
- package/dist/material-plugin-CTaWZXEk.js.map +1 -0
- package/dist/math-utils-BcDnL-gi.js +277 -0
- package/dist/math-utils-BcDnL-gi.js.map +1 -0
- package/dist/mesh-CnK_PX46.js +811 -0
- package/dist/mesh-CnK_PX46.js.map +1 -0
- package/dist/post-effect-C42XJFmo.js +155 -0
- package/dist/post-effect-C42XJFmo.js.map +1 -0
- package/dist/post-effect-compiler-DTlkT1si.js +311 -0
- package/dist/post-effect-compiler-DTlkT1si.js.map +1 -0
- package/dist/shader-declaration-5q__jycG.js +165 -0
- package/dist/shader-declaration-5q__jycG.js.map +1 -0
- package/dist/shader-material-DssOqsvZ.js +1009 -0
- package/dist/shader-material-DssOqsvZ.js.map +1 -0
- package/dist/shader-material-Xk7Oc9UG.js +207 -0
- package/dist/shader-material-Xk7Oc9UG.js.map +1 -0
- package/dist/shader-pragma-CNAK4SIb.js +685 -0
- package/dist/shader-pragma-CNAK4SIb.js.map +1 -0
- package/dist/surface-shader-C2HtysNN.js +1397 -0
- package/dist/surface-shader-C2HtysNN.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,685 @@
|
|
|
1
|
+
import { o as CoreErrorCode, t as IgnifxError } from "./ignifx-error-R1ma_Lzv.js";
|
|
2
|
+
import { a as SHADER_BLEND_MODES, i as SHADER_ATTRIBUTE_NAMES, l as SHADER_TEXTURE_FALLBACKS, o as SHADER_CULL_MODES, r as LITE_SYSTEM_UNIFORM_NAMES, s as SHADER_INSTANCING_MODES, t as IGNIFX_UNIFORM_NAMES, u as SHADER_UNIFORM_TYPES } from "./shader-declaration-5q__jycG.js";
|
|
3
|
+
//#region src/render/shader-pragma.ts
|
|
4
|
+
/**
|
|
5
|
+
* Reads the `// @ignifx …` comment pragmas of a `.wgsl` file into a {@link ShaderDeclaration}
|
|
6
|
+
* (grammar: `docs/plan/2026-09-terrain-particles-shaders.md` §3.1; rationale: ADR-0024).
|
|
7
|
+
*
|
|
8
|
+
* Pure string work with no Babylon Lite import, so the build-time validator and the runtime loader
|
|
9
|
+
* can share it. Four rules the grammar itself does not show:
|
|
10
|
+
*
|
|
11
|
+
* - `@ignifx system` mixes Lite's names with ignifx's, and the WGSL reads them from different
|
|
12
|
+
* structs, so they are split into `declaration.system` and `declaration.ignifx`.
|
|
13
|
+
* - An omitted `attributes` directive means `position`; Lite refuses a shader material without it
|
|
14
|
+
* (`lib/material/shader/shader-material.js`, error 293).
|
|
15
|
+
* - A `color(…)` default keeps the authored sRGB numbers; `./shader-material.ts` converts.
|
|
16
|
+
* - `cull front` and `transmissive` on an opaque surface are refused here, with a line number,
|
|
17
|
+
* because Lite has no front-face culling and throws on the second combination (error 295).
|
|
18
|
+
*/
|
|
19
|
+
/** Matches an `// @ignifx …` line and captures the directive text after it. */
|
|
20
|
+
const PRAGMA_LINE = /^\s*\/\/\s*@ignifx\b(.*)$/;
|
|
21
|
+
/** Matches a WGSL identifier. */
|
|
22
|
+
const IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
23
|
+
/** Splits `name: type` at the colon, capturing the identifier and the rest of the line. */
|
|
24
|
+
const NAME_AND_TYPE = /^([A-Za-z_][A-Za-z0-9_]*)\s*:\s*(.*)$/;
|
|
25
|
+
/** Matches a leading `tooltip("…")` modifier, capturing the quoted text. */
|
|
26
|
+
const TOOLTIP_MODIFIER = /^tooltip\(\s*"((?:[^"\\]|\\.)*)"\s*\)\s*/;
|
|
27
|
+
/** Matches a leading `range(…)`, `step(…)`, or `color(…)`/`color` modifier. */
|
|
28
|
+
const CALL_MODIFIER = /^(range|step|color)\s*(?:\(([^)]*)\))?\s*/;
|
|
29
|
+
/** Matches a leading `color(…)` default value, capturing the arguments. */
|
|
30
|
+
const COLOR_DEFAULT = /^color\s*\(([^)]*)\)\s*/;
|
|
31
|
+
/** Matches a leading parenthesised numeric list, capturing the arguments. */
|
|
32
|
+
const TUPLE_DEFAULT = /^\(([^)]*)\)\s*/;
|
|
33
|
+
/** Matches a leading decimal number, with optional sign and exponent. */
|
|
34
|
+
const NUMBER_DEFAULT = /^([+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)\s*/;
|
|
35
|
+
/** How many elements each uniform type holds. */
|
|
36
|
+
const ELEMENT_COUNTS = Object.freeze({
|
|
37
|
+
f32: 1,
|
|
38
|
+
u32: 1,
|
|
39
|
+
i32: 1,
|
|
40
|
+
"vec2<f32>": 2,
|
|
41
|
+
"vec3<f32>": 3,
|
|
42
|
+
"vec4<f32>": 4,
|
|
43
|
+
"mat4x4<f32>": 16
|
|
44
|
+
});
|
|
45
|
+
/** The identity matrix a `mat4x4<f32>` uniform starts at when the file declares no default. */
|
|
46
|
+
const IDENTITY_MATRIX = Object.freeze([
|
|
47
|
+
1,
|
|
48
|
+
0,
|
|
49
|
+
0,
|
|
50
|
+
0,
|
|
51
|
+
0,
|
|
52
|
+
1,
|
|
53
|
+
0,
|
|
54
|
+
0,
|
|
55
|
+
0,
|
|
56
|
+
0,
|
|
57
|
+
1,
|
|
58
|
+
0,
|
|
59
|
+
0,
|
|
60
|
+
0,
|
|
61
|
+
0,
|
|
62
|
+
1
|
|
63
|
+
]);
|
|
64
|
+
/**
|
|
65
|
+
* Reads a `.wgsl` file's `// @ignifx` pragmas into the declaration the loader, the material
|
|
66
|
+
* builder, and the inspector all read.
|
|
67
|
+
*
|
|
68
|
+
* @param source - The whole file, as text.
|
|
69
|
+
* @param address - The asset address, so a failure names the file.
|
|
70
|
+
* @returns The declaration, with every default filled in.
|
|
71
|
+
* @throws IgnifxError with code `IGX-0719`, carrying the offending `line`, for an unknown directive,
|
|
72
|
+
* a malformed one, a duplicate binding name, an attribute or uniform type Babylon Lite does not
|
|
73
|
+
* have, or a combination it cannot express.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const declaration = parseShaderDeclaration("// @ignifx shader\n", "shaders/flat.wgsl");
|
|
78
|
+
* declaration.attributes; // ["position"]
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @public
|
|
82
|
+
*/
|
|
83
|
+
function parseShaderDeclaration(source, address) {
|
|
84
|
+
const draft = {
|
|
85
|
+
kind: null,
|
|
86
|
+
attributes: null,
|
|
87
|
+
system: [],
|
|
88
|
+
ignifx: [],
|
|
89
|
+
uniforms: [],
|
|
90
|
+
textures: [],
|
|
91
|
+
storage: [],
|
|
92
|
+
defines: [],
|
|
93
|
+
names: /* @__PURE__ */ new Set(),
|
|
94
|
+
blend: null,
|
|
95
|
+
cull: null,
|
|
96
|
+
depthWrite: null,
|
|
97
|
+
depthTest: null,
|
|
98
|
+
transmissive: false,
|
|
99
|
+
instancing: null
|
|
100
|
+
};
|
|
101
|
+
const lines = source.split("\n");
|
|
102
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
103
|
+
const matched = PRAGMA_LINE.exec(lines[index] ?? "");
|
|
104
|
+
if (matched === null) continue;
|
|
105
|
+
readDirective(draft, (matched[1] ?? "").trim(), address, index + 1);
|
|
106
|
+
}
|
|
107
|
+
return finish(draft, address);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Applies one `// @ignifx …` line to the draft.
|
|
111
|
+
*
|
|
112
|
+
* @param draft - The declaration being built.
|
|
113
|
+
* @param text - The directive text, already trimmed.
|
|
114
|
+
* @param address - The asset address, for diagnostics.
|
|
115
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
116
|
+
*/
|
|
117
|
+
function readDirective(draft, text, address, line) {
|
|
118
|
+
if (text.length === 0) throw pragmaError(address, line, "an @ignifx comment declares no directive");
|
|
119
|
+
const separator = text.search(/\s/);
|
|
120
|
+
const directive = separator < 0 ? text : text.slice(0, separator);
|
|
121
|
+
const rest = separator < 0 ? "" : text.slice(separator + 1).trim();
|
|
122
|
+
switch (directive) {
|
|
123
|
+
case "shader":
|
|
124
|
+
case "surface":
|
|
125
|
+
case "post":
|
|
126
|
+
readKind(draft, directive, rest, address, line);
|
|
127
|
+
return;
|
|
128
|
+
case "attributes":
|
|
129
|
+
readAttributes(draft, rest, address, line);
|
|
130
|
+
return;
|
|
131
|
+
case "system":
|
|
132
|
+
readSystem(draft, rest, address, line);
|
|
133
|
+
return;
|
|
134
|
+
case "uniform":
|
|
135
|
+
draft.uniforms.push(readUniform(draft, rest, address, line));
|
|
136
|
+
return;
|
|
137
|
+
case "texture":
|
|
138
|
+
draft.textures.push(readTexture(draft, rest, address, line));
|
|
139
|
+
return;
|
|
140
|
+
case "storage":
|
|
141
|
+
draft.storage.push(readStorage(draft, rest, address, line));
|
|
142
|
+
return;
|
|
143
|
+
case "define":
|
|
144
|
+
draft.defines.push(readDefine(draft, rest, address, line));
|
|
145
|
+
return;
|
|
146
|
+
case "blend":
|
|
147
|
+
case "cull":
|
|
148
|
+
case "depthWrite":
|
|
149
|
+
case "depthTest":
|
|
150
|
+
case "transmissive":
|
|
151
|
+
case "instancing":
|
|
152
|
+
readPipeline(draft, text, address, line);
|
|
153
|
+
return;
|
|
154
|
+
default: throw pragmaError(address, line, `${JSON.stringify(directive)} is not an @ignifx directive`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Records the file's form, refusing a second one.
|
|
159
|
+
*
|
|
160
|
+
* @param draft - The declaration being built.
|
|
161
|
+
* @param kind - The declared form.
|
|
162
|
+
* @param rest - Whatever followed it, which must be nothing.
|
|
163
|
+
* @param address - The asset address, for diagnostics.
|
|
164
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
165
|
+
*/
|
|
166
|
+
function readKind(draft, kind, rest, address, line) {
|
|
167
|
+
if (rest.length > 0) throw pragmaError(address, line, `@ignifx ${kind} takes no arguments, but found ${JSON.stringify(rest)}`);
|
|
168
|
+
if (draft.kind !== null) throw pragmaError(address, line, `the file already declared @ignifx ${draft.kind}`);
|
|
169
|
+
draft.kind = kind;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Records the vertex attributes the vertex stage reads.
|
|
173
|
+
*
|
|
174
|
+
* @param draft - The declaration being built.
|
|
175
|
+
* @param rest - The comma-separated attribute names.
|
|
176
|
+
* @param address - The asset address, for diagnostics.
|
|
177
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
178
|
+
*/
|
|
179
|
+
function readAttributes(draft, rest, address, line) {
|
|
180
|
+
if (draft.attributes !== null) throw pragmaError(address, line, "the file already declared @ignifx attributes");
|
|
181
|
+
const names = splitList(rest, address, line, "attributes");
|
|
182
|
+
const attributes = [];
|
|
183
|
+
for (const name of names) {
|
|
184
|
+
const attribute = matchLiteral(SHADER_ATTRIBUTE_NAMES, name);
|
|
185
|
+
if (attribute === null) throw pragmaError(address, line, `${JSON.stringify(name)} is not a vertex attribute; Babylon Lite has ${SHADER_ATTRIBUTE_NAMES.join(", ")}`);
|
|
186
|
+
if (attributes.includes(attribute)) throw pragmaError(address, line, `the attribute ${attribute} is declared twice`);
|
|
187
|
+
attributes.push(attribute);
|
|
188
|
+
}
|
|
189
|
+
draft.attributes = attributes;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Records the system uniforms, splitting Babylon Lite's names from ignifx's own.
|
|
193
|
+
*
|
|
194
|
+
* @param draft - The declaration being built.
|
|
195
|
+
* @param rest - The comma-separated uniform names.
|
|
196
|
+
* @param address - The asset address, for diagnostics.
|
|
197
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
198
|
+
*/
|
|
199
|
+
function readSystem(draft, rest, address, line) {
|
|
200
|
+
const names = splitList(rest, address, line, "system");
|
|
201
|
+
for (const name of names) {
|
|
202
|
+
const lite = matchLiteral(LITE_SYSTEM_UNIFORM_NAMES, name);
|
|
203
|
+
if (lite !== null) {
|
|
204
|
+
claimName(draft, lite, address, line, "system uniform");
|
|
205
|
+
draft.system.push(lite);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const ignifx = matchLiteral(IGNIFX_UNIFORM_NAMES, name);
|
|
209
|
+
if (ignifx !== null) {
|
|
210
|
+
claimName(draft, ignifx, address, line, "system uniform");
|
|
211
|
+
draft.ignifx.push(ignifx);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
throw pragmaError(address, line, `${JSON.stringify(name)} is not a system uniform; Babylon Lite has ${LITE_SYSTEM_UNIFORM_NAMES.join(", ")} and ignifx adds ${IGNIFX_UNIFORM_NAMES.join(", ")}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Reads one `// @ignifx uniform name: type [= default] [modifiers]` line.
|
|
219
|
+
*
|
|
220
|
+
* @param draft - The declaration being built, for the name namespace.
|
|
221
|
+
* @param rest - The directive body.
|
|
222
|
+
* @param address - The asset address, for diagnostics.
|
|
223
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
224
|
+
* @returns The declaration.
|
|
225
|
+
*/
|
|
226
|
+
function readUniform(draft, rest, address, line) {
|
|
227
|
+
const split = NAME_AND_TYPE.exec(rest);
|
|
228
|
+
if (split === null) throw pragmaError(address, line, `@ignifx uniform expects "name: type", but found ${JSON.stringify(rest)}`);
|
|
229
|
+
const name = split[1] ?? "";
|
|
230
|
+
let tail = (split[2] ?? "").trim();
|
|
231
|
+
const typeSeparator = tail.search(/[\s=]/);
|
|
232
|
+
const typeName = typeSeparator < 0 ? tail : tail.slice(0, typeSeparator);
|
|
233
|
+
tail = (typeSeparator < 0 ? "" : tail.slice(typeName.length)).trim();
|
|
234
|
+
const type = matchLiteral(SHADER_UNIFORM_TYPES, typeName);
|
|
235
|
+
if (type === null) throw pragmaError(address, line, `${JSON.stringify(typeName)} is not a uniform type; Babylon Lite has ${SHADER_UNIFORM_TYPES.join(", ")}`);
|
|
236
|
+
claimName(draft, name, address, line, "uniform");
|
|
237
|
+
let defaultValue = null;
|
|
238
|
+
let isColor = false;
|
|
239
|
+
if (tail.startsWith("=")) {
|
|
240
|
+
tail = tail.slice(1).trim();
|
|
241
|
+
const parsed = readDefaultValue(tail, type, name, address, line);
|
|
242
|
+
defaultValue = parsed.value;
|
|
243
|
+
isColor = parsed.color;
|
|
244
|
+
tail = parsed.tail;
|
|
245
|
+
}
|
|
246
|
+
const modifiers = readModifiers(tail, name, type, address, line);
|
|
247
|
+
return {
|
|
248
|
+
name,
|
|
249
|
+
type,
|
|
250
|
+
defaultValue: defaultValue ?? implicitDefault(type),
|
|
251
|
+
color: isColor || modifiers.color,
|
|
252
|
+
range: modifiers.range,
|
|
253
|
+
step: modifiers.step,
|
|
254
|
+
tooltip: modifiers.tooltip
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Reads the `= …` half of a uniform declaration.
|
|
259
|
+
*
|
|
260
|
+
* @param text - The text after the `=`.
|
|
261
|
+
* @param type - The declared type, which fixes the element count.
|
|
262
|
+
* @param name - The uniform's name, for diagnostics.
|
|
263
|
+
* @param address - The asset address, for diagnostics.
|
|
264
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
265
|
+
* @returns The value, whether it was a colour, and the rest of the line.
|
|
266
|
+
*/
|
|
267
|
+
function readDefaultValue(text, type, name, address, line) {
|
|
268
|
+
const expected = ELEMENT_COUNTS[type];
|
|
269
|
+
const colorMatch = COLOR_DEFAULT.exec(text);
|
|
270
|
+
if (colorMatch !== null) {
|
|
271
|
+
const numbers = readNumbers(colorMatch[1] ?? "", name, address, line);
|
|
272
|
+
if (type !== "vec3<f32>" && type !== "vec4<f32>") throw pragmaError(address, line, `${name} is a ${type}, so color(…) does not apply to it`);
|
|
273
|
+
assertArity(numbers.length, expected, name, type, address, line);
|
|
274
|
+
return {
|
|
275
|
+
value: numbers,
|
|
276
|
+
color: true,
|
|
277
|
+
tail: text.slice(colorMatch[0].length).trim()
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
const tupleMatch = TUPLE_DEFAULT.exec(text);
|
|
281
|
+
if (tupleMatch !== null) {
|
|
282
|
+
const numbers = readNumbers(tupleMatch[1] ?? "", name, address, line);
|
|
283
|
+
assertArity(numbers.length, expected, name, type, address, line);
|
|
284
|
+
return {
|
|
285
|
+
value: numbers,
|
|
286
|
+
color: false,
|
|
287
|
+
tail: text.slice(tupleMatch[0].length).trim()
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
const numberMatch = NUMBER_DEFAULT.exec(text);
|
|
291
|
+
if (numberMatch !== null) {
|
|
292
|
+
assertArity(1, expected, name, type, address, line);
|
|
293
|
+
return {
|
|
294
|
+
value: Number(numberMatch[1]),
|
|
295
|
+
color: false,
|
|
296
|
+
tail: text.slice(numberMatch[0].length).trim()
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
throw pragmaError(address, line, `${name} declares no readable default value after "="`);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Reads the `range(…)`, `step(…)`, `color`, and `tooltip("…")` modifiers that close a uniform line.
|
|
303
|
+
*
|
|
304
|
+
* @param text - What is left of the line.
|
|
305
|
+
* @param name - The uniform's name, for diagnostics.
|
|
306
|
+
* @param type - The declared type, which decides whether `color` applies.
|
|
307
|
+
* @param address - The asset address, for diagnostics.
|
|
308
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
309
|
+
* @returns The hints.
|
|
310
|
+
*/
|
|
311
|
+
function readModifiers(text, name, type, address, line) {
|
|
312
|
+
let rest = text;
|
|
313
|
+
let color = false;
|
|
314
|
+
let range = null;
|
|
315
|
+
let step = null;
|
|
316
|
+
let tooltip = null;
|
|
317
|
+
while (rest.length > 0) {
|
|
318
|
+
const tooltipMatch = TOOLTIP_MODIFIER.exec(rest);
|
|
319
|
+
if (tooltipMatch !== null) {
|
|
320
|
+
if (tooltip !== null) throw pragmaError(address, line, `${name} declares tooltip twice`);
|
|
321
|
+
tooltip = (tooltipMatch[1] ?? "").replaceAll("\\\"", "\"");
|
|
322
|
+
rest = rest.slice(tooltipMatch[0].length);
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
const callMatch = CALL_MODIFIER.exec(rest);
|
|
326
|
+
if (callMatch === null) throw pragmaError(address, line, `${name} declares ${JSON.stringify(rest)}, which is not a uniform modifier`);
|
|
327
|
+
const modifier = callMatch[1] ?? "";
|
|
328
|
+
const argumentText = callMatch[2];
|
|
329
|
+
if (modifier === "color") {
|
|
330
|
+
if (argumentText !== void 0) throw pragmaError(address, line, `${name} declares color(…) after its default value`);
|
|
331
|
+
if (type !== "vec3<f32>" && type !== "vec4<f32>") throw pragmaError(address, line, `${name} is a ${type}, so color does not apply to it`);
|
|
332
|
+
color = true;
|
|
333
|
+
} else if (modifier === "range") {
|
|
334
|
+
if (range !== null) throw pragmaError(address, line, `${name} declares range twice`);
|
|
335
|
+
const bounds = readNumbers(argumentText ?? "", name, address, line);
|
|
336
|
+
const low = bounds[0];
|
|
337
|
+
const high = bounds[1];
|
|
338
|
+
if (bounds.length !== 2 || low === void 0 || high === void 0) throw pragmaError(address, line, `${name} declares range(…) with ${String(bounds.length)} bounds, not 2`);
|
|
339
|
+
range = [low, high];
|
|
340
|
+
} else {
|
|
341
|
+
if (step !== null) throw pragmaError(address, line, `${name} declares step twice`);
|
|
342
|
+
const increments = readNumbers(argumentText ?? "", name, address, line);
|
|
343
|
+
const increment = increments[0];
|
|
344
|
+
if (increments.length !== 1 || increment === void 0) throw pragmaError(address, line, `${name} declares step(…) with ${String(increments.length)} values, not 1`);
|
|
345
|
+
step = increment;
|
|
346
|
+
}
|
|
347
|
+
rest = rest.slice(callMatch[0].length);
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
color,
|
|
351
|
+
range,
|
|
352
|
+
step,
|
|
353
|
+
tooltip
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Reads one `// @ignifx texture name [srgb] [normal] [default white|black|transparent] [array]`
|
|
358
|
+
* line.
|
|
359
|
+
*
|
|
360
|
+
* @param draft - The declaration being built, for the name namespace.
|
|
361
|
+
* @param rest - The directive body.
|
|
362
|
+
* @param address - The asset address, for diagnostics.
|
|
363
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
364
|
+
* @returns The declaration.
|
|
365
|
+
*/
|
|
366
|
+
function readTexture(draft, rest, address, line) {
|
|
367
|
+
const tokens = rest.split(/\s+/).filter((token) => token.length > 0);
|
|
368
|
+
const name = tokens[0];
|
|
369
|
+
if (name === void 0) throw pragmaError(address, line, "@ignifx texture declares no name");
|
|
370
|
+
assertIdentifier(name, address, line, "texture");
|
|
371
|
+
claimName(draft, name, address, line, "texture");
|
|
372
|
+
claimName(draft, `${name}Sampler`, address, line, "texture sampler");
|
|
373
|
+
let srgb = false;
|
|
374
|
+
let normal = false;
|
|
375
|
+
let array = false;
|
|
376
|
+
let fallback = null;
|
|
377
|
+
for (let index = 1; index < tokens.length; index += 1) {
|
|
378
|
+
const token = tokens[index] ?? "";
|
|
379
|
+
if (token === "srgb") srgb = true;
|
|
380
|
+
else if (token === "normal") normal = true;
|
|
381
|
+
else if (token === "array") array = true;
|
|
382
|
+
else if (token === "default") {
|
|
383
|
+
index += 1;
|
|
384
|
+
const named = matchLiteral(SHADER_TEXTURE_FALLBACKS, tokens[index] ?? "");
|
|
385
|
+
if (named === null) throw pragmaError(address, line, `${name} declares default ${JSON.stringify(tokens[index] ?? "")}; expected ${SHADER_TEXTURE_FALLBACKS.join(", ")}`);
|
|
386
|
+
fallback = named;
|
|
387
|
+
} else throw pragmaError(address, line, `${JSON.stringify(token)} is not a texture modifier`);
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
name,
|
|
391
|
+
srgb,
|
|
392
|
+
normal,
|
|
393
|
+
fallback,
|
|
394
|
+
array
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Reads one `// @ignifx storage name: array<T>` line.
|
|
399
|
+
*
|
|
400
|
+
* @param draft - The declaration being built, for the name namespace.
|
|
401
|
+
* @param rest - The directive body.
|
|
402
|
+
* @param address - The asset address, for diagnostics.
|
|
403
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
404
|
+
* @returns The declaration.
|
|
405
|
+
*/
|
|
406
|
+
function readStorage(draft, rest, address, line) {
|
|
407
|
+
const split = NAME_AND_TYPE.exec(rest);
|
|
408
|
+
if (split === null) throw pragmaError(address, line, `@ignifx storage expects "name: type", but found ${JSON.stringify(rest)}`);
|
|
409
|
+
const name = split[1] ?? "";
|
|
410
|
+
const type = (split[2] ?? "").trim();
|
|
411
|
+
if (type.length === 0) throw pragmaError(address, line, `${name} declares no storage buffer type`);
|
|
412
|
+
claimName(draft, name, address, line, "storage buffer");
|
|
413
|
+
return {
|
|
414
|
+
name,
|
|
415
|
+
type
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Reads one `// @ignifx define NAME = value` line.
|
|
420
|
+
*
|
|
421
|
+
* @param draft - The declaration being built, for the name namespace.
|
|
422
|
+
* @param rest - The directive body.
|
|
423
|
+
* @param address - The asset address, for diagnostics.
|
|
424
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
425
|
+
* @returns The declaration.
|
|
426
|
+
*/
|
|
427
|
+
function readDefine(draft, rest, address, line) {
|
|
428
|
+
const equals = rest.indexOf("=");
|
|
429
|
+
if (equals < 0) throw pragmaError(address, line, `@ignifx define expects "NAME = value", but found ${JSON.stringify(rest)}`);
|
|
430
|
+
const name = rest.slice(0, equals).trim();
|
|
431
|
+
const text = rest.slice(equals + 1).trim();
|
|
432
|
+
assertIdentifier(name, address, line, "define");
|
|
433
|
+
claimName(draft, name, address, line, "define");
|
|
434
|
+
if (text === "true" || text === "false") return {
|
|
435
|
+
name,
|
|
436
|
+
value: text === "true"
|
|
437
|
+
};
|
|
438
|
+
const numberMatch = NUMBER_DEFAULT.exec(text);
|
|
439
|
+
if (numberMatch === null || numberMatch[0].trim().length !== text.length) throw pragmaError(address, line, `${name} declares ${JSON.stringify(text)}; a define is true, false, or a number`);
|
|
440
|
+
return {
|
|
441
|
+
name,
|
|
442
|
+
value: Number(numberMatch[1])
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Reads a pipeline-state line, merging it into whatever earlier lines declared.
|
|
447
|
+
*
|
|
448
|
+
* @param draft - The declaration being built.
|
|
449
|
+
* @param text - The whole directive text, starting at its first keyword.
|
|
450
|
+
* @param address - The asset address, for diagnostics.
|
|
451
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
452
|
+
*/
|
|
453
|
+
function readPipeline(draft, text, address, line) {
|
|
454
|
+
const tokens = text.split(/\s+/).filter((token) => token.length > 0);
|
|
455
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
456
|
+
const keyword = tokens[index] ?? "";
|
|
457
|
+
if (keyword === "transmissive") {
|
|
458
|
+
draft.transmissive = true;
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
index += 1;
|
|
462
|
+
const value = tokens[index] ?? "";
|
|
463
|
+
switch (keyword) {
|
|
464
|
+
case "blend":
|
|
465
|
+
draft.blend = readPipelineValue(SHADER_BLEND_MODES, draft.blend, keyword, value, address, line);
|
|
466
|
+
break;
|
|
467
|
+
case "cull": {
|
|
468
|
+
const cull = readPipelineValue(SHADER_CULL_MODES, draft.cull, keyword, value, address, line);
|
|
469
|
+
if (cull === "front") throw pragmaError(address, line, "Babylon Lite 1.27.0 has no front-face culling for a shader material; declare cull back or cull none and flip the winding");
|
|
470
|
+
draft.cull = cull;
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
case "depthWrite":
|
|
474
|
+
draft.depthWrite = readToggle(draft.depthWrite, keyword, value, address, line);
|
|
475
|
+
break;
|
|
476
|
+
case "depthTest":
|
|
477
|
+
draft.depthTest = readToggle(draft.depthTest, keyword, value, address, line);
|
|
478
|
+
break;
|
|
479
|
+
case "instancing":
|
|
480
|
+
draft.instancing = readPipelineValue(SHADER_INSTANCING_MODES, draft.instancing, keyword, value, address, line);
|
|
481
|
+
break;
|
|
482
|
+
default: throw pragmaError(address, line, `${JSON.stringify(keyword)} is not a pipeline keyword`);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Reads one pipeline keyword's value out of a fixed list, refusing a second declaration of it.
|
|
488
|
+
*
|
|
489
|
+
* @typeParam Value - The literal union the keyword accepts.
|
|
490
|
+
* @param allowed - The accepted values.
|
|
491
|
+
* @param current - What an earlier line declared, or `null`.
|
|
492
|
+
* @param keyword - The keyword, for diagnostics.
|
|
493
|
+
* @param value - The token that followed it.
|
|
494
|
+
* @param address - The asset address, for diagnostics.
|
|
495
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
496
|
+
* @returns The value.
|
|
497
|
+
*/
|
|
498
|
+
function readPipelineValue(allowed, current, keyword, value, address, line) {
|
|
499
|
+
if (current !== null) throw pragmaError(address, line, `${keyword} is declared twice`);
|
|
500
|
+
const matched = matchLiteral(allowed, value);
|
|
501
|
+
if (matched === null) throw pragmaError(address, line, `${keyword} ${JSON.stringify(value)} is not one of ${allowed.join(", ")}`);
|
|
502
|
+
return matched;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Reads an `on`/`off` toggle, refusing a second declaration of it.
|
|
506
|
+
*
|
|
507
|
+
* @param current - What an earlier line declared, or `null`.
|
|
508
|
+
* @param keyword - The keyword, for diagnostics.
|
|
509
|
+
* @param value - The token that followed it.
|
|
510
|
+
* @param address - The asset address, for diagnostics.
|
|
511
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
512
|
+
* @returns `true` for `on`, `false` for `off`.
|
|
513
|
+
*/
|
|
514
|
+
function readToggle(current, keyword, value, address, line) {
|
|
515
|
+
if (current !== null) throw pragmaError(address, line, `${keyword} is declared twice`);
|
|
516
|
+
if (value !== "on" && value !== "off") throw pragmaError(address, line, `${keyword} ${JSON.stringify(value)} is not on or off`);
|
|
517
|
+
return value === "on";
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Fills in the defaults and checks the whole-file rules.
|
|
521
|
+
*
|
|
522
|
+
* @param draft - The declaration the parser built.
|
|
523
|
+
* @param address - The asset address, for diagnostics.
|
|
524
|
+
* @returns The finished declaration.
|
|
525
|
+
*/
|
|
526
|
+
function finish(draft, address) {
|
|
527
|
+
const kind = draft.kind;
|
|
528
|
+
if (kind === null) throw pragmaError(address, 1, "the file declares no @ignifx shader, @ignifx surface, or @ignifx post directive");
|
|
529
|
+
const declared = draft.attributes;
|
|
530
|
+
if (declared !== null && kind === "shader" && !declared.includes("position")) throw pragmaError(address, 1, "an @ignifx shader file must declare the position attribute");
|
|
531
|
+
const attributes = declared ?? (kind === "shader" ? ["position"] : []);
|
|
532
|
+
const blend = draft.blend ?? "opaque";
|
|
533
|
+
if (draft.transmissive && blend === "opaque") throw pragmaError(address, 1, "transmissive needs a blended surface; declare blend alpha, additive, or premultiplied");
|
|
534
|
+
return {
|
|
535
|
+
kind,
|
|
536
|
+
attributes,
|
|
537
|
+
system: draft.system,
|
|
538
|
+
ignifx: draft.ignifx,
|
|
539
|
+
uniforms: draft.uniforms,
|
|
540
|
+
textures: draft.textures,
|
|
541
|
+
storage: draft.storage,
|
|
542
|
+
defines: draft.defines,
|
|
543
|
+
pipeline: {
|
|
544
|
+
blend,
|
|
545
|
+
cull: draft.cull ?? "back",
|
|
546
|
+
depthWrite: draft.depthWrite ?? blend === "opaque",
|
|
547
|
+
depthTest: draft.depthTest ?? true,
|
|
548
|
+
transmissive: draft.transmissive,
|
|
549
|
+
instancing: draft.instancing ?? "none"
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Splits a comma-separated directive body, refusing an empty entry.
|
|
555
|
+
*
|
|
556
|
+
* @param rest - The directive body.
|
|
557
|
+
* @param address - The asset address, for diagnostics.
|
|
558
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
559
|
+
* @param directive - The directive name, for diagnostics.
|
|
560
|
+
* @returns The trimmed entries.
|
|
561
|
+
*/
|
|
562
|
+
function splitList(rest, address, line, directive) {
|
|
563
|
+
if (rest.length === 0) throw pragmaError(address, line, `@ignifx ${directive} declares nothing`);
|
|
564
|
+
const parts = rest.split(",");
|
|
565
|
+
const names = [];
|
|
566
|
+
for (let index = 0; index < parts.length; index += 1) {
|
|
567
|
+
const name = (parts[index] ?? "").trim();
|
|
568
|
+
if (name.length === 0) throw pragmaError(address, line, `@ignifx ${directive} has an empty entry`);
|
|
569
|
+
names.push(name);
|
|
570
|
+
}
|
|
571
|
+
return names;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Reads a comma-separated list of numbers.
|
|
575
|
+
*
|
|
576
|
+
* @param text - The argument text, without the parentheses.
|
|
577
|
+
* @param name - The declaration's name, for diagnostics.
|
|
578
|
+
* @param address - The asset address, for diagnostics.
|
|
579
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
580
|
+
* @returns The numbers.
|
|
581
|
+
*/
|
|
582
|
+
function readNumbers(text, name, address, line) {
|
|
583
|
+
const parts = text.split(",");
|
|
584
|
+
const numbers = [];
|
|
585
|
+
for (let index = 0; index < parts.length; index += 1) {
|
|
586
|
+
const part = (parts[index] ?? "").trim();
|
|
587
|
+
const value = Number(part);
|
|
588
|
+
if (part.length === 0 || !Number.isFinite(value)) throw pragmaError(address, line, `${name} declares ${JSON.stringify(part)}, which is not a number`);
|
|
589
|
+
numbers.push(value);
|
|
590
|
+
}
|
|
591
|
+
return numbers;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Checks that a default value has as many numbers as its type needs.
|
|
595
|
+
*
|
|
596
|
+
* @param actual - How many numbers the file wrote.
|
|
597
|
+
* @param expected - How many the type holds.
|
|
598
|
+
* @param name - The uniform's name, for diagnostics.
|
|
599
|
+
* @param type - The uniform's type, for diagnostics.
|
|
600
|
+
* @param address - The asset address, for diagnostics.
|
|
601
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
602
|
+
*/
|
|
603
|
+
function assertArity(actual, expected, name, type, address, line) {
|
|
604
|
+
if (actual !== expected) throw pragmaError(address, line, `${name} is a ${type}, which takes ${String(expected)} numbers, but its default has ${String(actual)}`);
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* The value a uniform starts at when the file declares no default: zeros, or the identity matrix.
|
|
608
|
+
*
|
|
609
|
+
* @param type - The declared type.
|
|
610
|
+
* @returns The default.
|
|
611
|
+
*/
|
|
612
|
+
function implicitDefault(type) {
|
|
613
|
+
if (type === "mat4x4<f32>") return IDENTITY_MATRIX;
|
|
614
|
+
const count = ELEMENT_COUNTS[type];
|
|
615
|
+
if (count === 1) return 0;
|
|
616
|
+
const zeros = [];
|
|
617
|
+
for (let index = 0; index < count; index += 1) zeros.push(0);
|
|
618
|
+
return zeros;
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Claims one WGSL binding name, refusing a duplicate.
|
|
622
|
+
*
|
|
623
|
+
* @remarks
|
|
624
|
+
* Babylon Lite puts uniforms, samplers, `<name>Sampler` pairs, storage buffers, and defines in one
|
|
625
|
+
* namespace (`assertUniqueName`, `lib/material/shader/shader-material.js`), so the parser checks it
|
|
626
|
+
* here and reports the line instead of letting Lite throw without one.
|
|
627
|
+
*
|
|
628
|
+
* @param draft - The declaration being built.
|
|
629
|
+
* @param name - The name being claimed.
|
|
630
|
+
* @param address - The asset address, for diagnostics.
|
|
631
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
632
|
+
* @param what - What kind of binding is claiming it, for diagnostics.
|
|
633
|
+
*/
|
|
634
|
+
function claimName(draft, name, address, line, what) {
|
|
635
|
+
if (draft.names.has(name)) throw pragmaError(address, line, `${name} is already declared; the ${what} name must be unique in the file`);
|
|
636
|
+
draft.names.add(name);
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Checks that a name is a WGSL identifier.
|
|
640
|
+
*
|
|
641
|
+
* @param name - The candidate.
|
|
642
|
+
* @param address - The asset address, for diagnostics.
|
|
643
|
+
* @param line - The 1-based line number, for diagnostics.
|
|
644
|
+
* @param what - What kind of binding it names, for diagnostics.
|
|
645
|
+
*/
|
|
646
|
+
function assertIdentifier(name, address, line, what) {
|
|
647
|
+
if (!IDENTIFIER.test(name)) throw pragmaError(address, line, `${JSON.stringify(name)} is not a WGSL identifier for a ${what}`);
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Narrows a token to one of a fixed list of literals.
|
|
651
|
+
*
|
|
652
|
+
* @typeParam Value - The literal union.
|
|
653
|
+
* @param allowed - The accepted values.
|
|
654
|
+
* @param token - The candidate.
|
|
655
|
+
* @returns The matching literal, or `null`.
|
|
656
|
+
*/
|
|
657
|
+
function matchLiteral(allowed, token) {
|
|
658
|
+
for (let index = 0; index < allowed.length; index += 1) {
|
|
659
|
+
const candidate = allowed[index];
|
|
660
|
+
if (candidate !== void 0 && candidate === token) return candidate;
|
|
661
|
+
}
|
|
662
|
+
return null;
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Builds the `IGX-0719` failure a malformed pragma produces.
|
|
666
|
+
*
|
|
667
|
+
* @param address - The asset address.
|
|
668
|
+
* @param line - The 1-based line number.
|
|
669
|
+
* @param message - What is wrong.
|
|
670
|
+
* @returns The error to throw.
|
|
671
|
+
*/
|
|
672
|
+
function pragmaError(address, line, message) {
|
|
673
|
+
return new IgnifxError(CoreErrorCode.invalidShaderPragma, `${address} line ${String(line)}: ${message}.`, {
|
|
674
|
+
context: {
|
|
675
|
+
asset: address,
|
|
676
|
+
line,
|
|
677
|
+
message
|
|
678
|
+
},
|
|
679
|
+
hint: "The @ignifx pragma grammar is in skills/ignifx/references/formats/wgsl.md."
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
//#endregion
|
|
683
|
+
export { parseShaderDeclaration };
|
|
684
|
+
|
|
685
|
+
//# sourceMappingURL=shader-pragma-CNAK4SIb.js.map
|