@margoengine/shader 0.0.0-stage → 0.1.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.
Files changed (91) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1771 -3
  4. package/dist/black-hole.d.ts +246 -0
  5. package/dist/black-hole.js +445 -0
  6. package/dist/clock-BLGNexlt.js +52 -0
  7. package/dist/clock.d.ts +42 -0
  8. package/dist/compute-D-BghbXj.js +354 -0
  9. package/dist/compute-texture.d.ts +100 -0
  10. package/dist/compute-texture.js +131 -0
  11. package/dist/compute-volume.d.ts +57 -0
  12. package/dist/compute-volume.js +130 -0
  13. package/dist/compute.d.ts +194 -0
  14. package/dist/control-DvjfV7R0.js +201 -0
  15. package/dist/control.d.ts +93 -0
  16. package/dist/deform.d.ts +111 -0
  17. package/dist/deform.js +89 -0
  18. package/dist/display-BrJM_Qkd.js +5 -0
  19. package/dist/editor-model.d.ts +192 -0
  20. package/dist/editor-pass.d.ts +71 -0
  21. package/dist/editor-pass.js +216 -0
  22. package/dist/editor.d.ts +150 -0
  23. package/dist/editor.js +1071 -0
  24. package/dist/effects.d.ts +150 -0
  25. package/dist/effects.js +87 -0
  26. package/dist/errors-CsQwshHA.js +7 -0
  27. package/dist/glsl/emit.d.ts +43 -0
  28. package/dist/glsl/host.d.ts +55 -0
  29. package/dist/glsl/material.d.ts +173 -0
  30. package/dist/glsl/parser.d.ts +76 -0
  31. package/dist/glsl/preprocess.d.ts +9 -0
  32. package/dist/glsl/presets.d.ts +33 -0
  33. package/dist/glsl/program.d.ts +210 -0
  34. package/dist/glsl/scene.d.ts +9 -0
  35. package/dist/glsl-material.d.ts +82 -0
  36. package/dist/glsl-material.js +288 -0
  37. package/dist/glsl-pass.d.ts +93 -0
  38. package/dist/glsl-pass.js +186 -0
  39. package/dist/glsl.d.ts +94 -0
  40. package/dist/glsl.js +170 -0
  41. package/dist/gpu-DQ5Jmggz.js +31 -0
  42. package/dist/graph.d.ts +82 -0
  43. package/dist/graph.js +365 -0
  44. package/dist/index.d.ts +7 -0
  45. package/dist/index.js +8 -0
  46. package/dist/lighting-Dx8_RsBz.js +77 -0
  47. package/dist/lighting.d.ts +62 -0
  48. package/dist/lighting.js +1 -0
  49. package/dist/material-CU7p-Q8u.js +1184 -0
  50. package/dist/material-REzUrXtu.js +275 -0
  51. package/dist/material.d.ts +74 -0
  52. package/dist/node-DzKv9CF2.js +590 -0
  53. package/dist/node.d.ts +427 -0
  54. package/dist/noise.d.ts +38 -0
  55. package/dist/noise.js +107 -0
  56. package/dist/own-stack-D1XnXrrz.js +11 -0
  57. package/dist/own-stack.d.ts +12 -0
  58. package/dist/pass.d.ts +108 -0
  59. package/dist/pass.js +236 -0
  60. package/dist/post-DC62ZleD.js +113 -0
  61. package/dist/post.d.ts +69 -0
  62. package/dist/preprocess-CWGWlBWQ.js +1195 -0
  63. package/dist/raw-D0W8U3Qa.js +1351 -0
  64. package/dist/raw.d.ts +634 -0
  65. package/dist/raw.js +1 -0
  66. package/dist/readback.d.ts +42 -0
  67. package/dist/readback.js +84 -0
  68. package/dist/scope.d.ts +21 -0
  69. package/dist/serialization.d.ts +11 -0
  70. package/dist/serialization.js +192 -0
  71. package/dist/shadertoy/sound.d.ts +48 -0
  72. package/dist/shadertoy.d.ts +249 -0
  73. package/dist/shadertoy.js +909 -0
  74. package/dist/shared/caster.d.ts +152 -0
  75. package/dist/shared/fog.d.ts +33 -0
  76. package/dist/shared/fractal.d.ts +9 -0
  77. package/dist/shared/msaa.d.ts +25 -0
  78. package/dist/shared/stack.d.ts +22 -0
  79. package/dist/sound-CF_dzP41.js +78 -0
  80. package/dist/storage-format-DWaiSxmq.js +25 -0
  81. package/dist/storage-format.d.ts +7 -0
  82. package/dist/subgroups.d.ts +97 -0
  83. package/dist/subgroups.js +95 -0
  84. package/dist/texture-D6vy0bam.js +62 -0
  85. package/dist/texture-array.d.ts +7 -0
  86. package/dist/texture-array.js +11 -0
  87. package/dist/texture.d.ts +16 -0
  88. package/dist/utils.d.ts +106 -0
  89. package/dist/utils.js +90 -0
  90. package/dist/view-rect-Bm1ac-TK.js +16 -0
  91. package/package.json +151 -6
@@ -0,0 +1,1195 @@
1
+ import { t as e } from "./errors-CsQwshHA.js";
2
+ //#region src/glsl/parser.ts
3
+ var t = /* @__PURE__ */ new Set(/* @__PURE__ */ "void.float.int.uint.bool.vec2.vec3.vec4.ivec2.ivec3.ivec4.uvec2.uvec3.uvec4.bvec2.bvec3.bvec4.mat2.mat3.mat4.mat2x3.mat2x4.mat3x2.mat3x4.mat4x2.mat4x3.sampler2D.samplerCube.sampler3D.sampler2DArray.isampler2D.isampler3D.isamplerCube.isampler2DArray.usampler2D.usampler3D.usamplerCube.usampler2DArray.sampler2DShadow.samplerCubeShadow.sampler2DArrayShadow".split(".")), n = {
4
+ mat2x2: "mat2",
5
+ mat3x3: "mat3",
6
+ mat4x4: "mat4"
7
+ }, r = /* @__PURE__ */ new Set([
8
+ "const",
9
+ "uniform",
10
+ "highp",
11
+ "mediump",
12
+ "lowp",
13
+ "in",
14
+ "out",
15
+ "inout",
16
+ "flat",
17
+ "smooth",
18
+ "centroid",
19
+ "invariant",
20
+ "attribute",
21
+ "varying"
22
+ ]), i = {
23
+ ",": 1,
24
+ "=": 2,
25
+ "+=": 2,
26
+ "-=": 2,
27
+ "*=": 2,
28
+ "/=": 2,
29
+ "%=": 2,
30
+ "<<=": 2,
31
+ ">>=": 2,
32
+ "&=": 2,
33
+ "|=": 2,
34
+ "^=": 2,
35
+ "?": 3,
36
+ "||": 4,
37
+ "^^": 5,
38
+ "&&": 6,
39
+ "|": 7,
40
+ "^": 8,
41
+ "&": 9,
42
+ "==": 10,
43
+ "!=": 10,
44
+ "<": 11,
45
+ "<=": 11,
46
+ ">": 11,
47
+ ">=": 11,
48
+ "<<": 12,
49
+ ">>": 12,
50
+ "+": 13,
51
+ "-": 13,
52
+ "*": 14,
53
+ "/": 14,
54
+ "%": 14
55
+ }, a = (e, t = "", n = []) => ({
56
+ op: e,
57
+ value: t,
58
+ args: n
59
+ });
60
+ function o(t, n = []) {
61
+ let r = [], i = /\s+|\/\/[^\n]*|\/\*[\s\S]*?\*\/|0[xX][\dA-Fa-f]+[uU]?|(?:\d+\.\d*|\.\d+|\d+)(?:[eE][+-]?\d+)?[fFuU]?|[A-Za-z_]\w*|<<=|>>=|&=|\|=|\^=|\+\+|--|\+=|-=|\*=|\/=|%=|==|!=|<=|>=|&&|\|\||\^\^|<<|>>|[{}()[\].,;?:+*/%<>=!&|^~-]/y;
62
+ for (let a = 0; a < t.length;) {
63
+ i.lastIndex = a;
64
+ let o = i.exec(t);
65
+ if (!o) throw e("failed", `GLSL character at ${a} is invalid; correct the transition source`);
66
+ if (a = i.lastIndex, !/^\s|^\/\//.test(o[0]) && !o[0].startsWith("/*") && (r.push(o[0]), n.push(o.index)), r.length > 32768) throw e("failed", "GLSL token limit exceeded; keep source below 32768 tokens");
67
+ }
68
+ return r;
69
+ }
70
+ var s = class {
71
+ constructor(e, n = "transition") {
72
+ this.source = e, this.subject = n, this.offsets = [], this.i = 0, this.depth = 0, this.known = new Set(t), this.globals = [], this.constants = [/* @__PURE__ */ new Map()], this.tokens = o(e, this.offsets);
73
+ }
74
+ peek() {
75
+ return this.tokens[this.i] ?? "";
76
+ }
77
+ take() {
78
+ let e = this.peek();
79
+ return e || this.fail("Unexpected end of source"), this.i++, e;
80
+ }
81
+ match(e) {
82
+ return this.peek() === e && (this.i++, !0);
83
+ }
84
+ expect(e) {
85
+ this.match(e) || this.fail(`Expected ${e}, found ${this.peek() || "end"}`);
86
+ }
87
+ fail(t, n = 0) {
88
+ let r = this.offsets[Math.max(0, Math.min(this.i - n, this.offsets.length - 1))] ?? 0, i = this.source.slice(0, r);
89
+ throw Object.assign(e("failed", `GLSL token ${this.i}: ${t}; correct the ${this.subject} source`), {
90
+ line: i.split("\n").length,
91
+ column: r - i.lastIndexOf("\n")
92
+ });
93
+ }
94
+ identifier() {
95
+ let e = this.take();
96
+ return /^[A-Za-z_]\w*$/.test(e) || this.fail("Expected an identifier", 1), e;
97
+ }
98
+ qualified() {
99
+ let e = [];
100
+ for (;;) if (r.has(this.peek())) e.push(this.take());
101
+ else if (this.match("layout")) for (this.expect("("); !this.match(")");) this.take();
102
+ else break;
103
+ let t = this.identifier(), i = n[t] ?? t;
104
+ return !this.known.has(i) && !n[t] && this.fail(`Unknown type ${i}`), {
105
+ type: this.peek() === "[" ? this.array(i) : i,
106
+ qualifier: e.join(" ")
107
+ };
108
+ }
109
+ array(e) {
110
+ for (; this.match("[");) {
111
+ if (this.match("]")) {
112
+ e += "[]";
113
+ continue;
114
+ }
115
+ let t = this.expression(2);
116
+ this.expect("]");
117
+ let n = this.fold(t);
118
+ n < 1 && this.fail("Array sizes must be positive integer constants"), e += `[${n}]`;
119
+ }
120
+ return e;
121
+ }
122
+ fold(e, t = 0) {
123
+ if (t > 64 && this.fail("Array dimensions must be integer constants"), e.op === "literal" && /^\d+[uU]?$/.test(e.value)) return parseInt(e.value, 10);
124
+ if (e.op === "name") for (let n = this.constants.length - 1; n >= 0; n--) {
125
+ let r = this.constants[n].get(e.value);
126
+ if (r) return this.fold(r, t + 1);
127
+ if (r === null) break;
128
+ }
129
+ if (e.op === "prefix" && e.value === "-" && e.args[0]) return -this.fold(e.args[0], t + 1);
130
+ if (e.op === "prefix" && e.value === "+" && e.args[0]) return this.fold(e.args[0], t + 1);
131
+ if (e.op === "binary" && e.args[0] && e.args[1]) {
132
+ let n = this.fold(e.args[0], t + 1), r = this.fold(e.args[1], t + 1);
133
+ switch (e.value) {
134
+ case "+": return n + r;
135
+ case "-": return n - r;
136
+ case "*": return n * r;
137
+ case "/":
138
+ if (r !== 0) return Math.trunc(n / r);
139
+ break;
140
+ case "%": if (r !== 0) return n % r;
141
+ }
142
+ }
143
+ return this.fail("Array dimensions must be integer constants");
144
+ }
145
+ number(e) {
146
+ if (/[.eE]/.test(e) && !/^0[xX]/.test(e)) return e;
147
+ let t = /[uU]$/.test(e) ? "u" : "", n = t ? e.slice(0, -1) : e;
148
+ return (/^0[xX]/.test(n) ? parseInt(n.slice(2), 16) : /^0[0-7]*$/.test(n) ? parseInt(n, 8) : /^[1-9]\d*$/.test(n) ? Number(n) : this.fail(`Integer literal ${e} is malformed`, 1)) + t;
149
+ }
150
+ declarations(e, t, n, r = !0) {
151
+ let i = [];
152
+ do {
153
+ let a = n ?? this.identifier();
154
+ n = void 0;
155
+ let o = this.array(e), s = this.match("=") ? this.expression(2) : void 0;
156
+ if (o.endsWith("[]")) {
157
+ let e = s?.op === "call" && s.args[0].op === "name" ? s.args[0].value : "";
158
+ (!s || e.replace(/\[\d+\]$/, "[]") !== o) && this.fail(`Unsized array ${a} needs an array constructor of its type`), o = e;
159
+ }
160
+ r && this.constants.at(-1).set(a, /\bconst\b/.test(t) && s ? s : null), i.push({
161
+ name: a,
162
+ type: o,
163
+ qualifier: t,
164
+ init: s
165
+ });
166
+ } while (this.match(","));
167
+ return this.expect(";"), i;
168
+ }
169
+ uniformBlock(e) {
170
+ let t = this.i, n = !1;
171
+ for (;;) {
172
+ let e = this.tokens[t];
173
+ if (e === "layout" && this.tokens[t + 1] === "(") {
174
+ for (; this.tokens[t] && this.tokens[t] !== ")";) t++;
175
+ t++;
176
+ } else if (e && r.has(e)) n ||= e === "uniform", t++;
177
+ else break;
178
+ }
179
+ let i = this.tokens[t] ?? "";
180
+ if (!n || this.tokens[t + 1] !== "{" || !/^[A-Za-z_]\w*$/.test(i) || this.known.has(i)) return !1;
181
+ this.i = t + 2;
182
+ let a = [];
183
+ for (; !this.match("}");) {
184
+ let e = this.qualified();
185
+ /sampler/.test(e.type) && this.fail("A uniform block holds no samplers"), a.push(...this.declarations(e.type, e.qualifier, void 0, !1));
186
+ }
187
+ return this.match(";") ? e.globals.push(...a.map((e) => ({
188
+ ...e,
189
+ qualifier: `uniform ${e.qualifier}`.trim()
190
+ }))) : (this.known.add(i), e.structs.set(i, a), e.globals.push(...this.declarations(i, "uniform"))), !0;
191
+ }
192
+ parse() {
193
+ let e = {
194
+ globals: this.globals,
195
+ functions: [],
196
+ structs: /* @__PURE__ */ new Map()
197
+ };
198
+ for (; this.peek();) {
199
+ if (this.match("precision")) {
200
+ this.take(), this.take(), this.expect(";");
201
+ continue;
202
+ }
203
+ if (this.match(";") || this.uniformBlock(e)) continue;
204
+ if (this.peek() === "invariant" && !this.known.has(this.tokens[this.i + 1]) && !r.has(this.tokens[this.i + 1])) {
205
+ this.take();
206
+ do
207
+ this.identifier();
208
+ while (this.match(","));
209
+ this.expect(";");
210
+ continue;
211
+ }
212
+ if (this.match("struct")) {
213
+ let t = this.identifier();
214
+ this.known.add(t), this.expect("{");
215
+ let n = [];
216
+ for (; !this.match("}");) {
217
+ let e = this.qualified();
218
+ n.push(...this.declarations(e.type, e.qualifier, void 0, !1));
219
+ }
220
+ e.structs.set(t, n), this.match(";") || e.globals.push(...this.declarations(t, ""));
221
+ continue;
222
+ }
223
+ let t = this.qualified(), n = this.identifier();
224
+ if (this.match("(")) {
225
+ let r = [];
226
+ if (this.constants.push(/* @__PURE__ */ new Map()), !this.match(")")) {
227
+ if (this.peek() === "void" && this.tokens[this.i + 1] === ")") this.take(), this.take();
228
+ else {
229
+ do {
230
+ let e = this.qualified(), t = [
231
+ ",",
232
+ ")",
233
+ "["
234
+ ].includes(this.peek()) ? `margo_unnamed${r.length}` : this.identifier();
235
+ r.push({
236
+ name: t,
237
+ type: this.array(e.type),
238
+ qualifier: e.qualifier
239
+ }), this.constants.at(-1).set(t, null);
240
+ } while (this.match(","));
241
+ this.expect(")");
242
+ }
243
+ }
244
+ let i = this.match(";") ? void 0 : this.block();
245
+ this.constants.pop(), e.functions.push({
246
+ name: n,
247
+ type: t.type,
248
+ params: r,
249
+ body: i
250
+ });
251
+ } else e.globals.push(...this.declarations(t.type, t.qualifier, n));
252
+ }
253
+ return e;
254
+ }
255
+ block() {
256
+ this.expect("{");
257
+ let e = [];
258
+ for (this.constants.push(/* @__PURE__ */ new Map()); !this.match("}");) e.push(this.statement());
259
+ return this.constants.pop(), e;
260
+ }
261
+ statement() {
262
+ ++this.depth > 128 && this.fail("Statement nesting exceeds 128");
263
+ try {
264
+ if (this.peek() === "{") return {
265
+ kind: "block",
266
+ body: this.block()
267
+ };
268
+ if (this.match(";")) return {
269
+ kind: "block",
270
+ body: []
271
+ };
272
+ if (this.match("precision")) return this.take(), this.take(), this.expect(";"), {
273
+ kind: "block",
274
+ body: []
275
+ };
276
+ if (this.match("if")) {
277
+ this.expect("(");
278
+ let e = this.expression();
279
+ return this.expect(")"), {
280
+ kind: "if",
281
+ expr: e,
282
+ body: [this.statement()],
283
+ other: this.match("else") ? [this.statement()] : void 0
284
+ };
285
+ }
286
+ if (this.match("for")) {
287
+ this.expect("("), this.constants.push(/* @__PURE__ */ new Map());
288
+ let e = this.match(";") ? void 0 : this.simple(), t = this.match(";") ? void 0 : this.expression();
289
+ t && this.expect(";");
290
+ let n = this.peek() === ")" ? void 0 : this.expression();
291
+ this.expect(")");
292
+ let r = [this.statement()];
293
+ return this.constants.pop(), {
294
+ kind: "for",
295
+ init: e,
296
+ expr: t,
297
+ update: n,
298
+ body: r
299
+ };
300
+ }
301
+ if (this.match("while")) {
302
+ this.expect("(");
303
+ let e = this.expression();
304
+ return this.expect(")"), {
305
+ kind: "while",
306
+ expr: e,
307
+ body: [this.statement()]
308
+ };
309
+ }
310
+ if (this.match("switch")) {
311
+ this.expect("(");
312
+ let e = this.expression();
313
+ this.expect(")"), this.expect("{"), this.constants.push(/* @__PURE__ */ new Map());
314
+ let t = [], n = /* @__PURE__ */ new Set();
315
+ for (; !this.match("}");) {
316
+ let e = this.match("case") ? this.fold(this.expression()) : this.match("default") ? null : void 0;
317
+ if (e === void 0) {
318
+ t.length || this.fail("A switch body starts with a case or default label"), t.at(-1).body.push(this.statement());
319
+ continue;
320
+ }
321
+ this.expect(":"), n.has(e) && this.fail(`Duplicate switch label ${e ?? "default"}`), n.add(e);
322
+ let r = t.at(-1);
323
+ r && !r.body.length ? r.labels.push(e) : t.push({
324
+ labels: [e],
325
+ body: []
326
+ });
327
+ }
328
+ return this.constants.pop(), t.length && !t.at(-1).body.length && this.fail("A switch label needs a statement after it"), {
329
+ kind: "switch",
330
+ expr: e,
331
+ cases: t
332
+ };
333
+ }
334
+ if (this.match("do")) {
335
+ let e = [this.statement()];
336
+ this.expect("while"), this.expect("(");
337
+ let t = this.expression();
338
+ return this.expect(")"), this.expect(";"), {
339
+ kind: "do",
340
+ body: e,
341
+ expr: t
342
+ };
343
+ }
344
+ for (let e of [
345
+ "return",
346
+ "break",
347
+ "continue",
348
+ "discard"
349
+ ]) if (this.match(e)) {
350
+ let t = e === "return" && this.peek() !== ";" ? this.expression() : void 0;
351
+ return this.expect(";"), {
352
+ kind: e,
353
+ expr: t
354
+ };
355
+ }
356
+ return this.simple();
357
+ } finally {
358
+ this.depth--;
359
+ }
360
+ }
361
+ simple() {
362
+ let e = this.peek();
363
+ if (r.has(e) || e === "layout" || (this.known.has(e) || Object.hasOwn(n, e)) && !this.constructs()) {
364
+ let e = this.qualified();
365
+ return {
366
+ kind: "decl",
367
+ decls: this.declarations(e.type, e.qualifier)
368
+ };
369
+ }
370
+ let t = this.expression();
371
+ return this.expect(";"), {
372
+ kind: "expr",
373
+ expr: t
374
+ };
375
+ }
376
+ constructs() {
377
+ if (this.tokens[this.i + 1] !== "[") return !1;
378
+ let e = this.i + 1, t = 0;
379
+ for (; e < this.tokens.length; e++) if (this.tokens[e] === "[") t++;
380
+ else if (this.tokens[e] === "]" && --t === 0) break;
381
+ return this.tokens[e + 1] === "(";
382
+ }
383
+ expression(e = 0) {
384
+ ++this.depth > 128 && this.fail("Expression nesting exceeds 128");
385
+ try {
386
+ let t = this.take(), r;
387
+ if (t === "(") r = this.expression(), this.expect(")");
388
+ else if ([
389
+ "+",
390
+ "-",
391
+ "!",
392
+ "~",
393
+ "++",
394
+ "--"
395
+ ].includes(t)) r = a("prefix", t, [this.expression(15)]);
396
+ else if (/^\.?\d/.test(t)) r = a("literal", this.number(t));
397
+ else if (t === "true" || t === "false") r = a("literal", t);
398
+ else if (this.known.has(t) && this.peek() === "[") {
399
+ let e = this.array(t);
400
+ this.expect("(");
401
+ let n = [];
402
+ if (!this.match(")")) {
403
+ do
404
+ n.push(this.expression(2));
405
+ while (this.match(","));
406
+ this.expect(")");
407
+ }
408
+ e.endsWith("[]") && (e = e.slice(0, -2) + `[${n.length}]`), n.length || this.fail("An array constructor needs its elements"), r = a("call", "", [a("name", e), ...n]);
409
+ } else /^[A-Za-z_]\w*$/.test(t) ? r = a("name", n[t] ?? t) : this.fail(`Unexpected expression token ${t}`, 1);
410
+ for (;;) {
411
+ if (this.match("(")) {
412
+ let e = [];
413
+ if (!this.match(")")) {
414
+ do
415
+ e.push(this.expression(2));
416
+ while (this.match(","));
417
+ this.expect(")");
418
+ }
419
+ r = a("call", "", [r, ...e]);
420
+ continue;
421
+ }
422
+ if (this.match("[")) {
423
+ let e = this.expression();
424
+ this.expect("]"), r = a("index", "", [r, e]);
425
+ continue;
426
+ }
427
+ if (this.match(".")) {
428
+ r = a("member", this.identifier(), [r]);
429
+ continue;
430
+ }
431
+ if (this.peek() === "++" || this.peek() === "--") {
432
+ r = a("postfix", this.take(), [r]);
433
+ continue;
434
+ }
435
+ let t = this.peek(), n = i[t];
436
+ if (n === void 0 || n < e) break;
437
+ if (this.take(), t === "?") {
438
+ let e = this.expression();
439
+ this.expect(":");
440
+ let t = this.expression(2);
441
+ r = a("conditional", "", [
442
+ r,
443
+ e,
444
+ t
445
+ ]);
446
+ continue;
447
+ }
448
+ let o = this.expression(n + (n === 2 ? 0 : 1));
449
+ r = a("binary", t, [r, o]);
450
+ }
451
+ return r;
452
+ } finally {
453
+ this.depth--;
454
+ }
455
+ }
456
+ }, c = (e) => e.startsWith("bvec") ? "bool" : e.startsWith("ivec") ? "int" : e.startsWith("uvec") ? "uint" : e.startsWith("vec") || e.startsWith("mat") ? "float" : e, l = (e) => /vec[234]$/.test(e) ? Number(e.at(-1)) : 1, u = (e, t) => t === 1 ? e : `${e === "float" ? "" : e === "int" ? "i" : e === "uint" ? "u" : "b"}vec${t}`, d = (e) => "user_" + e, f = (e) => {
457
+ let t = /^mat([234])(?:x([234]))?$/.exec(e);
458
+ return t ? [Number(t[1]), Number(t[2] ?? t[1])] : void 0;
459
+ }, p = (e, t) => e === t ? `mat${e}` : `mat${e}x${t}`, m = {
460
+ sampler2D: "texture_2d<f32>",
461
+ samplerCube: "texture_cube<f32>",
462
+ sampler3D: "texture_3d<f32>",
463
+ sampler2DArray: "texture_2d_array<f32>",
464
+ sampler2DShadow: "texture_depth_2d",
465
+ samplerCubeShadow: "texture_depth_cube",
466
+ sampler2DArrayShadow: "texture_depth_2d_array"
467
+ };
468
+ for (let e of ["i", "u"]) for (let [t, n] of [
469
+ ["2D", "2d"],
470
+ ["3D", "3d"],
471
+ ["Cube", "cube"],
472
+ ["2DArray", "2d_array"]
473
+ ]) m[`${e}sampler${t}`] = `texture_${n}<${e}32>`;
474
+ var h = (e) => e.replace(/^[iu]/, "").replace("Shadow", ""), g = (e) => e.endsWith("Shadow") ? "float" : /^[iu]/.test(e) ? e[0] + "vec4" : "vec4", _ = (e) => {
475
+ let t = /^(.*)\[([^\]]+)\]$/.exec(e);
476
+ if (t) return `array<${_(t[1])},${t[2]}>`;
477
+ let n = {
478
+ float: "f32",
479
+ int: "i32",
480
+ uint: "u32",
481
+ bool: "bool",
482
+ void: "void",
483
+ ...m
484
+ };
485
+ if (n[e]) return n[e];
486
+ let r = /^([ibu]?)vec([234])$/.exec(e);
487
+ if (r) return `vec${r[2]}<${r[1] === "i" ? "i32" : r[1] === "u" ? "u32" : r[1] === "b" ? "bool" : "f32"}>`;
488
+ let i = f(e);
489
+ return i ? `mat${i[0]}x${i[1]}<f32>` : d(e);
490
+ }, v = "\n", y = (e, t, n = []) => ({
491
+ code: e,
492
+ type: t,
493
+ before: n
494
+ }), b = class {
495
+ constructor(e, t, n = 100) {
496
+ this.program = e, this.host = t, this.version = n, this.scopes = [/* @__PURE__ */ new Map()], this.serial = 0, this.names = /* @__PURE__ */ new Map(), this.helpers = /* @__PURE__ */ new Set(), this.equalityNames = /* @__PURE__ */ new Map(), this.uniforms = t.uniforms, this.flipY = t.flipY, this.functions = e.functions.filter((e) => e.body), this.functions.forEach((e, t) => this.names.set(e, `user_function_${t}`));
497
+ }
498
+ fail(t) {
499
+ throw e("failed", `GLSL translation: ${t}; correct the source or supplied uniform values`);
500
+ }
501
+ temp() {
502
+ return `temporary_${this.serial++}`;
503
+ }
504
+ equality(e) {
505
+ let t = this.equalityNames.get(e);
506
+ if (t) return t;
507
+ let n = `aggregate_equal_${this.serial++}`;
508
+ this.equalityNames.set(e, n);
509
+ let r = (e, t, n) => [
510
+ "float",
511
+ "int",
512
+ "uint",
513
+ "bool"
514
+ ].includes(e) ? `(${t}==${n})` : `${this.equality(e)}(${t},${n})`, i;
515
+ if (/^[ibu]?vec[234]$/.test(e)) i = "all(a==b)";
516
+ else if (f(e)) i = Array.from({ length: f(e)[0] }, (e, t) => `all(a[${t}]==b[${t}])`).join("&&");
517
+ else if (/^(.*)\[(\d+)\]$/.test(e) && this.version === 300) {
518
+ let [, t, n] = /^(.*)\[(\d+)\]$/.exec(e);
519
+ i = Array.from({ length: Number(n) }, (e, n) => r(t, `a[${n}]`, `b[${n}]`)).join("&&");
520
+ } else {
521
+ let t = this.program.structs.get(e);
522
+ t || this.fail("GLSL ES 1.00 equality excludes arrays and samplers"), i = t.map((e) => r(e.type, `a.${d(e.name)}`, `b.${d(e.name)}`)).join("&&");
523
+ }
524
+ return this.helpers.add(`fn ${n}(a:${_(e)},b:${_(e)})->bool{return ${i};}`), n;
525
+ }
526
+ find(e) {
527
+ for (let t = this.scopes.length - 1; t >= 0; t--) {
528
+ let n = this.scopes[t].get(e);
529
+ if (n) return n;
530
+ }
531
+ }
532
+ define(e, t, n = d(e)) {
533
+ let r = this.scopes.at(-1);
534
+ return r.has(e) && this.fail(`Duplicate variable ${e}`), r.set(e, {
535
+ type: t,
536
+ code: n
537
+ }), n;
538
+ }
539
+ convert(e, t) {
540
+ if (t === e.type) return e;
541
+ if (c(t) === "bool" && c(e.type) !== "bool") return y(`(${e.code}!=${_(e.type)}(0))`, t, e.before);
542
+ if (c(e.type) === "bool" && c(t) !== "bool") return y(`select(${_(t)}(0),${_(t)}(1),${e.code})`, t, e.before);
543
+ if (l(t) > 1 && l(e.type) === 1 && c(t) !== c(e.type)) return this.convert(this.convert(e, c(t)), t);
544
+ if (c(t) === c(e.type) && l(t) > 1 && l(e.type) === 1 || l(t) === l(e.type) && [
545
+ "float",
546
+ "int",
547
+ "uint",
548
+ "bool"
549
+ ].includes(c(t)) && [
550
+ "float",
551
+ "int",
552
+ "uint",
553
+ "bool"
554
+ ].includes(c(e.type))) return y(`${_(t)}(${e.code})`, t, e.before);
555
+ this.fail(`Cannot convert ${e.type} to ${t}`);
556
+ }
557
+ join(e, t) {
558
+ let n = Math.max(l(e.type), l(t.type)), r = u(c(e.type) === "float" || c(t.type) === "float" ? "float" : c(e.type), n);
559
+ return [this.convert(e, r), this.convert(t, r)];
560
+ }
561
+ literal(e) {
562
+ let t = e.value;
563
+ return t === "true" || t === "false" ? y(t, "bool") : /[uU]$/.test(t) ? y(t.slice(0, -1) + "u", "uint") : /[.eEfF]/.test(t) ? (t = t.replace(/[fF]$/, ""), t.startsWith(".") && (t = "0" + t), t.endsWith(".") && (t += "0"), y(`f32(${t})`, "float")) : Number(t) > 2147483647 ? (Number(t) > 4294967295 && this.fail(`Integer literal ${t} does not fit 32 bits`), y(`bitcast<i32>(${t}u)`, "int")) : y(`i32(${t})`, "int");
564
+ }
565
+ assign(e, t, n = "=", r) {
566
+ [
567
+ "name",
568
+ "index",
569
+ "member"
570
+ ].includes(e.op) || this.fail("Assignment needs a variable, element, field or swizzle");
571
+ let i = r ?? this.expr(e), a = this.convert(t, i.type), o = [...i.before, ...a.before];
572
+ if (e.op === "member" && /^[ibu]?vec[234]$/.test(this.expr(e.args[0]).type) && /^[xyzwrgba stpq]+$/.test(e.value) && e.value.length > 1) {
573
+ let t = i.code.slice(0, -e.value.length - 1), r = e.value.replace(/[rgba]/g, (e) => "xyzw"["rgba".indexOf(e)]).replace(/[stpq]/g, (e) => "xyzw"["stpq".indexOf(e)]);
574
+ new Set(r).size !== r.length && this.fail("Repeated component in swizzle assignment");
575
+ let s = this.temp();
576
+ o.push(`let ${s}=${a.code};`);
577
+ for (let e = 0; e < r.length; e++) o.push(`${t}.${r[e]} ${n} ${s}[${e}];`);
578
+ } else o.push(`${i.code} ${n} ${a.code};`);
579
+ return o;
580
+ }
581
+ expr(e) {
582
+ if (e.op === "literal") return this.literal(e);
583
+ if (e.op === "name") {
584
+ let t = this.find(e.value);
585
+ return t || this.fail(`Unknown identifier ${e.value}`), y(t.code, t.type);
586
+ }
587
+ if (e.op === "index") {
588
+ let t = this.expr(e.args[0]), n = this.convert(this.expr(e.args[1]), "int"), r = /^(.*)\[([^\]]+)\]$/.exec(t.type);
589
+ if (r && Object.hasOwn(m, r[1])) {
590
+ let e = /^i32\((\d+)\)$/.exec(n.code)?.[1];
591
+ if (e !== void 0) return +e >= +r[2] && this.fail(`Sampler array index ${e} is outside its ${r[2]} elements`), y(`${t.code}__${e}`, r[1], t.before);
592
+ let i = this.temp();
593
+ return y(`margo_pick(${t.code},${r[2]},${i})`, r[1], [
594
+ ...t.before,
595
+ ...n.before,
596
+ `let ${i}=${n.code};`
597
+ ]);
598
+ }
599
+ let i = r ? r[1] : t.type.startsWith("mat") ? "vec" + t.type.at(-1) : c(t.type);
600
+ return y(`${t.code}[${n.code}]`, i, [...t.before, ...n.before]);
601
+ }
602
+ if (e.op === "member") {
603
+ let t = this.expr(e.args[0]), n = e.value, r = this.program.structs.get(t.type);
604
+ if (r) {
605
+ let e = r.find((e) => e.name === n);
606
+ return e || this.fail(`Unknown struct member ${n}`), y(`${t.code}.${d(n)}`, e.type, t.before);
607
+ }
608
+ let i = /^[xyzw]{1,4}$/.test(n) ? "xyzw" : /^[rgba]{1,4}$/.test(n) ? "rgba" : /^[stpq]{1,4}$/.test(n) ? "stpq" : "";
609
+ (!i || [...n].some((e) => i.indexOf(e) >= l(t.type))) && this.fail(`Invalid swizzle ${n}`);
610
+ let a = [...n].map((e) => "xyzw"[i.indexOf(e)]).join("");
611
+ return y(`(${t.code}).${a}`, u(c(t.type), n.length), t.before);
612
+ }
613
+ if (e.op === "prefix" && e.value === "-" && e.args[0].op === "literal" && e.args[0].value === "2147483648") return y("i32(-2147483648)", "int");
614
+ if (e.op === "prefix" || e.op === "postfix") {
615
+ let t = this.expr(e.args[0]);
616
+ if (e.value === "++" || e.value === "--") {
617
+ let n = this.temp(), r = y(`${_(c(t.type))}(1)`, c(t.type)), i = this.assign(e.args[0], this.arith(e.value[0], y(t.code, t.type), r), "=", {
618
+ ...t,
619
+ before: []
620
+ }), a = e.op === "postfix" ? [
621
+ ...t.before,
622
+ `let ${n}=${t.code};`,
623
+ ...i
624
+ ] : [...t.before, ...i];
625
+ return y(e.op === "postfix" ? n : t.code, t.type, a);
626
+ }
627
+ return e.value === "-" && t.type.startsWith("mat") ? y(`(${t.code}*(-1.0))`, t.type, t.before) : y(`(${e.value === "+" ? "" : e.value}${t.code})`, e.value === "!" ? "bool" : t.type, t.before);
628
+ }
629
+ if (e.op === "conditional") {
630
+ let t = this.expr(e.args[0]), n = this.expr(e.args[1]), r = this.convert(this.expr(e.args[2]), n.type), i = this.temp();
631
+ return y(i, n.type, [
632
+ ...t.before,
633
+ `var ${i}:${_(n.type)};`,
634
+ `if(${t.code}){${n.before.join("\n")}${i}=${n.code};}else{${r.before.join("\n")}${i}=${r.code};}`
635
+ ]);
636
+ }
637
+ if (e.op === "binary") {
638
+ let t = e.value;
639
+ if ([
640
+ "=",
641
+ "+=",
642
+ "-=",
643
+ "*=",
644
+ "/=",
645
+ "%=",
646
+ "<<=",
647
+ ">>=",
648
+ "&=",
649
+ "|=",
650
+ "^="
651
+ ].includes(t)) {
652
+ let n = this.expr(e.args[0]), r = this.expr(e.args[1]), i = t === "=" ? r : this.arith(t.slice(0, -1), y(n.code, n.type), r);
653
+ return y(n.code, n.type, this.assign(e.args[0], i, "=", n));
654
+ }
655
+ let n = this.expr(e.args[0]), r = this.expr(e.args[1]);
656
+ if (t === ",") return y(r.code, r.type, [
657
+ ...n.before,
658
+ ...n.type === "void" ? n.code ? [n.code + ";"] : [] : [`let ${this.temp()}=${n.code};`],
659
+ ...r.before
660
+ ]);
661
+ if (t === "&&" || t === "||") {
662
+ if (!r.before.length) return y(`(${n.code}${t}${r.code})`, "bool", n.before);
663
+ let e = this.temp();
664
+ return y(e, "bool", [
665
+ ...n.before,
666
+ `var ${e}=${n.code};`,
667
+ `if(${t === "||" ? "!" : ""}${e}){${r.before.join("\n")}${e}=${r.code};}`
668
+ ]);
669
+ }
670
+ if (t === "^^") return y(`(${n.code}!=${r.code})`, "bool", [...n.before, ...r.before]);
671
+ if (t === "==" || t === "!=") {
672
+ n.type !== r.type && this.fail("Equality operands must have matching types");
673
+ let e = [
674
+ "float",
675
+ "int",
676
+ "uint",
677
+ "bool"
678
+ ].includes(n.type) ? `(${n.code}==${r.code})` : `${this.equality(n.type)}(${n.code},${r.code})`;
679
+ return y(t === "!=" ? `!(${e})` : e, "bool", [...n.before, ...r.before]);
680
+ }
681
+ return this.arith(t, n, r);
682
+ }
683
+ if (e.op === "call") return this.call(e.args[0], e.args.slice(1));
684
+ this.fail(`Unknown expression ${e.op}`);
685
+ }
686
+ arith(e, t, n) {
687
+ let r = f(t.type), i = f(n.type);
688
+ if (e === "*" && (r || i)) return y(`(${t.code}*${n.code})`, r && i ? p(i[0], r[1]) : r && n.type.startsWith("vec") ? `vec${r[1]}` : i && t.type.startsWith("vec") ? `vec${i[0]}` : r ? t.type : n.type, [...t.before, ...n.before]);
689
+ if ((r || i) && [
690
+ "+",
691
+ "-",
692
+ "/"
693
+ ].includes(e)) {
694
+ let a = r ? t.type : n.type, [o, s] = r ?? i, c = this.temp(), l = this.temp(), u = (e, t, n) => e.type.startsWith("mat") ? `${t}[${n}]` : `vec${s}f(f32(${t}))`;
695
+ return y(`${_(a)}(${Array.from({ length: o }, (r, i) => `(${u(t, c, i)}${e}${u(n, l, i)})`).join(",")})`, a, [
696
+ ...t.before,
697
+ ...n.before,
698
+ `let ${c}=${t.code};`,
699
+ `let ${l}=${n.code};`
700
+ ]);
701
+ }
702
+ if (e === "<<" || e === ">>") {
703
+ let r = this.convert(n, u("uint", l(t.type)));
704
+ return y(`(${t.code}${e}${r.code})`, t.type, [...t.before, ...r.before]);
705
+ }
706
+ let [a, o] = this.join(t, n), s = [
707
+ "==",
708
+ "!=",
709
+ "<",
710
+ "<=",
711
+ ">",
712
+ ">="
713
+ ].includes(e);
714
+ return y(`(${a.code}${e}${o.code})`, s ? "bool" : a.type, [...a.before, ...o.before]);
715
+ }
716
+ call(e, t) {
717
+ if (e.op === "member" && e.value === "length" && !t.length) {
718
+ let t = this.expr(e.args[0]), n = /\[(\d+)\]$/.exec(t.type);
719
+ return n || this.fail("length() needs an array"), y(`i32(${n[1]})`, "int", t.before);
720
+ }
721
+ e.op !== "name" && this.fail("Function calls must use a named function");
722
+ let n = e.value, r = t.map((e) => this.expr(e)), i = r.flatMap((e) => e.before), a = r.map((e) => e.code), o = r[0] ?? y("", "void"), s = (e) => ((r.length !== 1 || o.type !== "vec2") && this.fail(`${n} needs one vec2 coordinate`), y(`sample(${this.host.colorReaders[e ? "getToColor" : "getFromColor"]},${this.flipY ? `vec2f(${o.code}.x,1.0-${o.code}.y)` : o.code})`, "vec4", i));
723
+ if (Object.hasOwn(this.host.colorReaders, n)) return s(n === "getToColor");
724
+ let d = /^(texture2D|texture)(Proj)?(Lod|Grad|Offset)?$/.exec(n);
725
+ if (d && Object.hasOwn(m, o.type)) return this.lookup(!!d[2], d[3] ?? "", r, i);
726
+ if (n === "texelFetch" || n === "textureSize") return this.texel(n, r, i);
727
+ let h = /^(.*)\[(\d+)\]$/.exec(n);
728
+ if (h) {
729
+ r.length !== Number(h[2]) && this.fail(`${n} needs ${h[2]} elements`);
730
+ let e = r.map((e) => this.convert(e, h[1]));
731
+ return y(`${_(n)}(${e.map((e) => e.code).join(",")})`, n, e.flatMap((e) => e.before));
732
+ }
733
+ if (this.program.structs.has(n)) return y(`${_(n)}(${a.join(",")})`, n, i);
734
+ if (/^(float|int|uint|bool|[ibu]?vec[234]|mat[234](x[234])?)$/.test(n)) {
735
+ let e = f(n);
736
+ if (e && r.length === 1 && f(o.type)) {
737
+ let [t, r] = e, [a, s] = f(o.type), c = this.temp();
738
+ return y(`${_(n)}(${Array.from({ length: t }, (e, t) => `vec${r}f(${Array.from({ length: r }, (e, n) => t < a && n < s ? `${c}[${t}][${n}]` : t === n ? "1.0" : "0.0").join(",")})`).join(",")})`, n, [...i, `let ${c}=${o.code};`]);
739
+ }
740
+ if (e && r.length === 1 && [
741
+ "float",
742
+ "int",
743
+ "uint"
744
+ ].includes(o.type)) {
745
+ let [t, r] = e, a = Array.from({ length: t }, (e, t) => `${_("vec" + r)}(${Array.from({ length: r }, (e, n) => n === t ? `f32(${o.code})` : "0.0").join(",")})`);
746
+ return y(`${_(n)}(${a.join(",")})`, n, i);
747
+ }
748
+ let t = e ? e[0] * e[1] : l(n), a = r.reduce((e, t) => e + this.components(t.type), 0);
749
+ if (!(e ? a === t && (r.every((t) => t.type === `vec${e[1]}`) || r.every((e) => l(e.type) === 1 && !f(e.type))) : !r.some((e) => f(e.type)) && (r.length === 1 || a === t)) && (r.length > 1 || !e && l(n) > 1)) {
750
+ a < t && this.fail(`${n} needs ${t} components`);
751
+ let e = [...i], o = [];
752
+ for (let t of r) {
753
+ let r = this.temp(), i = f(t.type);
754
+ e.push(`let ${r}=${t.code};`);
755
+ let a = i ? Array.from({ length: i[0] * i[1] }, (e, t) => `${r}[${Math.floor(t / i[1])}][${t % i[1]}]`) : l(t.type) > 1 ? Array.from({ length: l(t.type) }, (e, t) => `${r}.${"xyzw"[t]}`) : [r];
756
+ for (let e of a) o.push(this.convert(y(e, c(t.type)), c(n)).code);
757
+ }
758
+ return y(`${_(n)}(${o.slice(0, t).join(",")})`, n, e);
759
+ }
760
+ if (!n.startsWith("mat") && l(n) === 1 && r.length === 1 && (l(o.type) > 1 || /^mat/.test(o.type))) return this.convert(y(`(${o.code})${o.type.startsWith("mat") ? "[0].x" : ".x"}`, c(o.type), i), n);
761
+ if (l(n) > 1 && r.length === 1 && l(o.type) > l(n)) return y(`${_(n)}((${o.code}).${"xyzw".slice(0, l(n))})`, n, i);
762
+ let s = r.map((e) => c(e.type) === c(n) ? e : this.convert(e, u(c(n), l(e.type))));
763
+ return y(`${_(n)}(${s.map((e) => e.code).join(",")})`, n, s.flatMap((e) => e.before));
764
+ }
765
+ let g = this.functions.filter((e) => e.name === n && e.params.length === r.length), v = g.find((e) => e.params.every((e, t) => e.type === r[t].type)) ?? g.find((e) => e.params.every((e, t) => l(e.type) === l(r[t].type)));
766
+ if (v) {
767
+ let e = v.params.map((e, t) => this.convert(r[t], e.type)), n = e.flatMap((e) => e.before);
768
+ e.some((e) => e.code.startsWith("margo_pick(") || /sampler\w*\[/.test(e.type)) && this.fail(`${v.name} takes a sampler array element; index it with a constant`);
769
+ let i = `${this.names.get(v)}(${e.map((e) => this.host.sample && Object.hasOwn(m, e.type) ? `${e.code},${this.host.samplerOf?.(e.code) ?? e.code + "s"}${this.host.samplerMode ? "," + this.host.samplerMode(e.code) : ""}` : e.code).join(",")})`;
770
+ if (!v.params.some((e) => /\b(out|inout)\b/.test(e.qualifier))) return y(i, v.type, n);
771
+ let a = this.temp();
772
+ return n.push(`let ${a}=${i};`), v.params.forEach((e, i) => {
773
+ /\b(out|inout)\b/.test(e.qualifier) && n.push(...this.assign(t[i], y(`${a}.out_${i}`, e.type), "=", {
774
+ ...r[i],
775
+ before: []
776
+ }));
777
+ }), y(v.type === "void" ? "" : `${a}.result`, v.type, n);
778
+ }
779
+ if (n === "mix" && r.length === 3 && c(r[2].type) === "bool") {
780
+ let e = l(r[1].type) > l(o.type) ? r[1].type : o.type, [t, n] = [this.convert(o, e), this.convert(r[1], e)];
781
+ return y(`select(${t.code},${n.code},${r[2].code})`, e, [
782
+ ...t.before,
783
+ ...n.before,
784
+ ...r[2].before
785
+ ]);
786
+ }
787
+ let b = {
788
+ floatBitsToInt: "int",
789
+ floatBitsToUint: "uint",
790
+ intBitsToFloat: "float",
791
+ uintBitsToFloat: "float"
792
+ };
793
+ if (b[n] && r.length === 1) {
794
+ let e = u(b[n], l(o.type));
795
+ return y(`bitcast<${_(e)}>(${o.code})`, e, i);
796
+ }
797
+ if ((n === "isnan" || n === "isinf") && r.length === 1) {
798
+ let e = l(o.type), t = _(u("uint", e)), r = this.temp();
799
+ return y(`((${r}&${t}(0x7fffffffu))${n === "isnan" ? ">" : "=="}${t}(0x7f800000u))`, u("bool", e), [...i, `let ${r}=bitcast<${t}>(${o.code});`]);
800
+ }
801
+ if (n === "modf" && r.length === 2) {
802
+ let e = this.temp();
803
+ return y(`${e}.fract`, o.type, [
804
+ ...i,
805
+ `let ${e}=modf(${o.code});`,
806
+ ...this.assign(t[1], y(`${e}.whole`, o.type), "=", {
807
+ ...r[1],
808
+ before: []
809
+ })
810
+ ]);
811
+ }
812
+ let x = f(o.type);
813
+ if (n === "inverse" && this.version === 300 && r.length === 1 && x && x[0] === x[1]) return y(`${this.inverse(x[0])}(${o.code})`, o.type, i);
814
+ if (n === "outerProduct" && r.length === 2) {
815
+ let e = this.temp(), t = this.temp(), n = p(l(r[1].type), l(o.type));
816
+ return y(`${_(n)}(${Array.from({ length: l(r[1].type) }, (n, r) => `${e}*${t}[${r}]`).join(",")})`, n, [
817
+ ...i,
818
+ `let ${e}=${o.code};`,
819
+ `let ${t}=${a[1]};`
820
+ ]);
821
+ }
822
+ if (n === "transpose" && r.length === 1 && x) return y(`transpose(${o.code})`, p(x[1], x[0]), i);
823
+ let S = {
824
+ packSnorm2x16: ["pack2x16snorm", "uint"],
825
+ unpackSnorm2x16: ["unpack2x16snorm", "vec2"],
826
+ packUnorm2x16: ["pack2x16unorm", "uint"],
827
+ unpackUnorm2x16: ["unpack2x16unorm", "vec2"],
828
+ packHalf2x16: ["pack2x16float", "uint"],
829
+ unpackHalf2x16: ["unpack2x16float", "vec2"]
830
+ };
831
+ if (S[n] && r.length === 1) return y(`${S[n][0]}(${o.code})`, S[n][1], i);
832
+ if ((/* @__PURE__ */ "radians.degrees.sin.cos.tan.asin.acos.exp.log.exp2.log2.sqrt.inversesqrt.abs.sign.floor.ceil.fract.normalize.dFdx.dFdy.fwidth.trunc.round.roundEven.sinh.cosh.tanh.asinh.acosh.atanh".split(".")).includes(n)) return r.length !== 1 && this.fail(`${n} needs one argument`), y(`${n === "dFdy" && (this.host.yUp ?? this.flipY) ? "-" : ""}${{
833
+ inversesqrt: "inverseSqrt",
834
+ dFdx: "dpdx",
835
+ dFdy: "dpdy",
836
+ roundEven: "round"
837
+ }[n] ?? n}(${a.join(",")})`, o.type, i);
838
+ if (n === "atan") return y(`${r.length === 2 ? "atan2" : "atan"}(${a.join(",")})`, o.type, i);
839
+ if ([
840
+ "length",
841
+ "distance",
842
+ "dot",
843
+ "determinant"
844
+ ].includes(n)) return y(`${n}(${a.join(",")})`, "float", i);
845
+ if (["any", "all"].includes(n)) return y(`${n}(${a.join(",")})`, "bool", i);
846
+ if (n === "not") return y(`(!${o.code})`, o.type, i);
847
+ if ([
848
+ "cross",
849
+ "reflect",
850
+ "refract",
851
+ "transpose",
852
+ "faceforward"
853
+ ].includes(n)) return y(`${n === "faceforward" ? "faceForward" : n}(${a.join(",")})`, o.type, i);
854
+ let C = {
855
+ lessThan: "<",
856
+ lessThanEqual: "<=",
857
+ greaterThan: ">",
858
+ greaterThanEqual: ">=",
859
+ equal: "==",
860
+ notEqual: "!="
861
+ };
862
+ if (C[n]) return y(`(${a[0]}${C[n]}${a[1]})`, u("bool", l(o.type)), i);
863
+ if ([
864
+ "min",
865
+ "max",
866
+ "pow",
867
+ "step",
868
+ "mod",
869
+ "clamp",
870
+ "mix",
871
+ "smoothstep"
872
+ ].includes(n)) {
873
+ let e = [
874
+ "clamp",
875
+ "mix",
876
+ "smoothstep"
877
+ ].includes(n) ? 3 : 2;
878
+ r.length !== e && this.fail(`${n} needs ${e} arguments`);
879
+ let t = r.reduce((e, t) => l(t.type) > l(e) ? t.type : e, o.type), i = r.map((e) => this.convert(e, t)), a = i.map((e) => e.code), s = i.flatMap((e) => e.before);
880
+ if (n === "mod") {
881
+ let e = this.temp(), n = this.temp();
882
+ return y(`(${e}-${n}*floor(${e}/${n}))`, t, [
883
+ ...s,
884
+ `let ${e}=${a[0]};`,
885
+ `let ${n}=${a[1]};`
886
+ ]);
887
+ }
888
+ return y(`${n}(${a.join(",")})`, t, s);
889
+ }
890
+ if (n === "matrixCompMult" && x) return y(`${_(o.type)}(${Array.from({ length: x[0] }, (e, t) => `${a[0]}[${t}]*${a[1]}[${t}]`).join(",")})`, o.type, i);
891
+ this.fail(`Unknown function or overload ${n}`);
892
+ }
893
+ picked(e, t, n) {
894
+ let r = /^margo_pick\((\w+),(\d+),(\w+)\)$/.exec(e.code);
895
+ if (!r) return;
896
+ let i = Array.from({ length: +r[2] }, (n, i) => t(y(`${r[1]}__${i}`, e.type)));
897
+ return y(i.reduce((e, t, n) => n ? `select(${e},${t.code},${r[3]}==${n})` : t.code, ""), i[0].type, [...n, ...i.flatMap((e) => e.before)]);
898
+ }
899
+ lookup(e, t, n, r) {
900
+ let [i, a, ...o] = n, s = this.picked(i, (r) => this.lookup(e, t, [r, ...n.slice(1)], []), r);
901
+ if (s) return s;
902
+ let c = h(i.type), u = i.type.endsWith("Shadow"), d = ({
903
+ sampler2D: 2,
904
+ samplerCube: 3,
905
+ sampler3D: 3,
906
+ sampler2DArray: 3
907
+ }[c] ?? 2) + +!!u, f = t === "Grad" ? 2 : +!!t;
908
+ (!a || o.length < f || o.length > f + (t === "Lod" || t === "Grad" ? 0 : 1)) && this.fail(`texture${e ? "Proj" : ""}${t} has the wrong number of arguments`), e && c !== "sampler2D" && c !== "sampler3D" && this.fail("A projective lookup needs a 2D or 3D sampler"), e && u && l(a.type) !== 4 && this.fail("textureProj of a sampler2DShadow takes vec4 coordinates"), l(a.type) !== (e ? d + 1 : d) && !(e && c === "sampler2D" && l(a.type) === 4) && this.fail(`${i.type} lookups take vec${e ? d + 1 : d} coordinates`), /^[iu]samplerCube/.test(i.type) && this.fail(`${i.type} lookups need a cube texture of integers, which WGSL cannot read; use a 2D array`);
909
+ let p = e ? `(${a.code}.${d === 2 ? "xy" : "xyz"}/${a.code}.${l(a.type) === 4 ? "w" : "z"})` : a.code, m;
910
+ if (u) {
911
+ let e = this.temp(), t = d - 1;
912
+ r = [...r, `let ${e}=${p};`], m = `${e}.${"xyzw"[t]}`, p = t === 2 ? `${e}.xy` : `${e}.xyz`;
913
+ }
914
+ let _ = d - +!!u, v = (e) => this.flipY && c !== "samplerCube" && c !== "sampler3D" ? `vec${_}f(${e}.x,1.0-${e}.y${_ === 3 ? `,${e}.z` : ""})` : e, b = this.host.sample, x = m ? { compare: m } : {};
915
+ if (t === "Lod") x.lod = o[0].code;
916
+ else if (t === "Grad") {
917
+ let e = (e) => this.flipY && c !== "samplerCube" && c !== "sampler3D" ? `vec2f(${e.code}.x,-${e.code}.y)` : e.code;
918
+ x.ddx = e(o[0]), x.ddy = e(o[1]);
919
+ } else t === "Offset" && (x.offset = this.flipY ? `vec2i(${o[0].code}.x,-${o[0].code}.y)` : o[0].code);
920
+ return o.length > f && (x.bias = o[f].code), b ? y(b(i.code, i.type, v(p), x), g(i.type), r) : (i.type !== "sampler2D" && this.fail("Only sampler2D images can be read here"), t === "Offset" && (p = `(${p}+vec2f(${o[0].code})/vec2f(textureDimensions(${i.code})))`), y(`sample(${i.code},${v(p)})`, "vec4", r));
921
+ }
922
+ texel(e, t, n) {
923
+ let [r, i] = t;
924
+ (!r || !Object.hasOwn(m, r.type)) && this.fail(`${e} needs a sampler`);
925
+ let a = this.picked(r, (n) => this.texel(e, [n, ...t.slice(1)], []), n);
926
+ if (a) return a;
927
+ let o = h(r.type);
928
+ e === "texelFetch" && r.type.endsWith("Shadow") && this.fail("texelFetch takes no shadow sampler"), t.length !== (e === "textureSize" ? 2 : 3) && this.fail(`${e} has the wrong number of arguments`);
929
+ let s = this.convert(t.at(-1), "int"), c = this.temp(), l = this.temp();
930
+ if (n = [...n, ...s.before], e === "textureSize") {
931
+ let e = o === "sampler2D" || o === "samplerCube" ? "ivec2" : "ivec3", t = `textureDimensions(${r.code},${l})`;
932
+ return y(o === "sampler2DArray" ? `vec3<i32>(vec2<i32>(${t}),i32(textureNumLayers(${r.code})))` : `${_(e)}(${t})`, e, [...n, `let ${l}=${s.code};`]);
933
+ }
934
+ o === "samplerCube" && this.fail("texelFetch needs a 2D, 3D or array sampler");
935
+ let u = this.convert(i, o === "sampler2D" ? "ivec2" : "ivec3"), d = [
936
+ ...n,
937
+ ...u.before,
938
+ `let ${c}=${u.code};`,
939
+ `let ${l}=${s.code};`
940
+ ], f = this.flipY && o !== "sampler3D" ? `vec2i(${c}.x,i32(textureDimensions(${r.code},${l}).y)-1-${c}.y)` : `${c}.xy`;
941
+ return y(o === "sampler2DArray" ? `textureLoad(${r.code},${f},${c}.z,${l})` : o === "sampler3D" ? `textureLoad(${r.code},${c},${l})` : `textureLoad(${r.code},${f},${l})`, g(r.type), d);
942
+ }
943
+ inverse(e) {
944
+ let t = `margo_inverse${e}`, n = `mat${e}x${e}<f32>`;
945
+ if (e === 2) this.helpers.add(`fn ${t}(m:${n})->${n}{return ${n}(m[1][1],-m[0][1],-m[1][0],m[0][0])*(1.0/determinant(m));}`);
946
+ else {
947
+ let r = (t, n) => {
948
+ let r = [...Array(e).keys()].filter((e) => e !== t), i = [...Array(e).keys()].filter((e) => e !== n);
949
+ return `determinant(mat${e - 1}x${e - 1}<f32>(${r.map((t) => `vec${e - 1}f(${i.map((e) => `m[${t}][${e}]`).join(",")})`).join(",")}))`;
950
+ }, i = [];
951
+ for (let t = 0; t < e; t++) for (let n = 0; n < e; n++) i.push(`${(n + t) % 2 ? "-" : ""}${r(n, t)}`);
952
+ this.helpers.add(`fn ${t}(m:${n})->${n}{return ${n}(${i.join(",")})*(1.0/determinant(m));}`);
953
+ }
954
+ return t;
955
+ }
956
+ declarations(e, t = !1) {
957
+ return e.map((e) => {
958
+ let n = e.init ? this.convert(this.expr(e.init), e.type) : void 0, r = this.define(e.name, e.type);
959
+ if (/\buniform\b/.test(e.qualifier)) {
960
+ let t = this.uniforms[e.name];
961
+ t === void 0 && this.fail(`Supply uniform ${e.name}`), n = y(this.parameter(t, e.type), e.type);
962
+ }
963
+ let i = /\bconst\b|\buniform\b/.test(e.qualifier);
964
+ return t && n?.before.length && this.fail(`Global initializer ${e.name} is not constant`), [...n?.before ?? [], `${t ? i ? "const" : "var<private>" : i ? "let" : "var"} ${r}:${_(e.type)}${n ? "=" + n.code : ""};`].join("\n");
965
+ });
966
+ }
967
+ arrayCount(e) {
968
+ let t = Number(e);
969
+ return (!Number.isSafeInteger(t) || t < 1 || t > 16384) && this.fail("Uniform array length must be in 1..16384"), t;
970
+ }
971
+ components(e) {
972
+ let t = /^(.*)\[([^\]]+)\]$/.exec(e);
973
+ return t ? this.arrayCount(t[2]) * this.components(t[1]) : e.startsWith("mat") ? f(e)[0] * f(e)[1] : l(e);
974
+ }
975
+ parameter(e, t) {
976
+ let n = Array.isArray(e) ? e : [e];
977
+ n.length !== this.components(t) && this.fail(`Uniform ${t} needs ${this.components(t)} components`);
978
+ let r = 0, i = (e) => {
979
+ let t = /^(.*)\[([^\]]+)\]$/.exec(e);
980
+ if (t) return `${_(e)}(${Array.from({ length: this.arrayCount(t[2]) }, () => i(t[1])).join(",")})`;
981
+ let a = c(e), o = this.components(e), s = Array.from({ length: o }, () => {
982
+ let e = n[r++];
983
+ if (a === "bool") {
984
+ if (typeof e == "boolean" || e === 0 || e === 1) return String(!!e);
985
+ this.fail("Boolean uniforms need true, false, zero or one");
986
+ }
987
+ return (typeof e != "number" || !Number.isFinite(e) || a === "float" && !Number.isFinite(Math.fround(e))) && this.fail("Uniform values must fit finite 32-bit numbers"), (a === "int" || a === "uint") && (!Number.isSafeInteger(e) || e < (a === "uint" ? 0 : -2147483648) || e > (a === "uint" ? 4294967295 : 2147483647)) && this.fail("Integer uniform is outside 32-bit range"), `${_(a)}(${Object.is(e, -0) ? "-0.0" : e})`;
988
+ });
989
+ return o === 1 ? s[0] : `${_(e)}(${s.join(",")})`;
990
+ };
991
+ return i(t);
992
+ }
993
+ returnValue(e) {
994
+ let t = this.active, n = e ? this.convert(this.expr(e), t.type) : void 0, r = t.params.map((e, t) => /\b(out|inout)\b/.test(e.qualifier) ? this.find(e.name).code : void 0).filter((e) => e !== void 0);
995
+ return [...n?.before ?? [], `return${r.length ? " " + this.names.get(t) + "_result(" + [...n ? [n.code] : [], ...r].join(",") + ")" : n ? " " + n.code : ""};`];
996
+ }
997
+ body(e) {
998
+ this.scopes.push(/* @__PURE__ */ new Map());
999
+ try {
1000
+ return e.flatMap((e) => this.statement(e)).join("\n");
1001
+ } finally {
1002
+ this.scopes.pop();
1003
+ }
1004
+ }
1005
+ statement(e) {
1006
+ if (e.kind === "block") return ["{" + this.body(e.body ?? []) + "}"];
1007
+ if (e.kind === "decl") return this.declarations(e.decls);
1008
+ if (e.kind === "return") return this.returnValue(e.expr);
1009
+ if ([
1010
+ "break",
1011
+ "continue",
1012
+ "discard"
1013
+ ].includes(e.kind)) return [e.kind + ";"];
1014
+ if (e.kind === "expr") {
1015
+ let t = this.expr(e.expr);
1016
+ return [...t.before, ...t.code ? [t.type === "void" ? t.code + ";" : "_=" + t.code + ";"] : []];
1017
+ }
1018
+ if (e.kind === "if") {
1019
+ let t = this.expr(e.expr);
1020
+ return [...t.before, `if(${t.code}){${this.body(e.body ?? [])}}${e.other ? "else{" + this.body(e.other) + "}" : ""}`];
1021
+ }
1022
+ if (e.kind === "switch") return this.switch(e);
1023
+ if ([
1024
+ "for",
1025
+ "while",
1026
+ "do"
1027
+ ].includes(e.kind)) {
1028
+ this.scopes.push(/* @__PURE__ */ new Map());
1029
+ try {
1030
+ let t = e.init ? this.statement(e.init) : [], n = e.expr ? this.expr(e.expr) : void 0, r = e.update ? this.expr(e.update) : void 0, i = n ? [...n.before, `if(!(${n.code})){break;}`].join("\n") : "", a = r ? [...r.before, ...r.before.length === 0 && r.code ? [r.code + ";"] : []].join("\n") : "";
1031
+ return [
1032
+ "{",
1033
+ ...t,
1034
+ `loop{${e.kind === "do" ? "" : i}\n${this.body(e.body ?? [])}\ncontinuing{${e.kind === "do" ? `${n?.before.join("\n") ?? ""}\nbreak if !(${n?.code ?? "true"});` : a}}}`,
1035
+ "}"
1036
+ ];
1037
+ } finally {
1038
+ this.scopes.pop();
1039
+ }
1040
+ }
1041
+ this.fail(`Unknown statement ${e.kind}`);
1042
+ }
1043
+ switch(e) {
1044
+ let t = this.expr(e.expr);
1045
+ t.type !== "int" && t.type !== "uint" && this.fail("A switch selector is an int or uint scalar");
1046
+ let n = e.cases ?? [], r = (e) => [
1047
+ "break",
1048
+ "return",
1049
+ "continue",
1050
+ "discard"
1051
+ ].includes(e.at(-1)?.kind ?? ""), i = n.map((e, i) => {
1052
+ let a = [];
1053
+ for (let e = i; e < n.length && (a.push(...n[e].body), !r(n[e].body)); e++);
1054
+ return `case ${e.labels.map((e) => e === null ? "default" : t.type === "uint" ? `${e >>> 0}u` : `i32(${e | 0})`).join(",")}{${this.body(a)}}`;
1055
+ });
1056
+ return n.some((e) => e.labels.includes(null)) || i.push("default{}"), [...t.before, `switch(${t.code}){${i.join("\n")}}`];
1057
+ }
1058
+ emit() {
1059
+ let e = this.scopes[0];
1060
+ for (let [t, n] of Object.entries(this.host.names)) e.set(t, n);
1061
+ let t = [...this.program.structs].map(([e, t]) => `struct ${d(e)}{${t.map((e) => d(e.name) + ":" + _(e.type)).join(",")}}`).join("\n"), n = this.declarations(this.program.globals.filter((e) => e.name === "gl_FragCoord" || !Object.hasOwn(this.host.names, e.name)), !0).join("\n"), r = this.functions.filter((e) => e.params.some((e) => /\b(out|inout)\b/.test(e.qualifier))).map((e) => `struct ${this.names.get(e)}_result{${[...e.type === "void" ? [] : [`result:${_(e.type)}`], ...e.params.flatMap((e, t) => /\b(out|inout)\b/.test(e.qualifier) ? [`out_${t}:${_(e.type)}`] : [])].join(",")}}`).join("\n"), i = this.functions.map((e) => {
1062
+ this.active = e, this.scopes.push(/* @__PURE__ */ new Map());
1063
+ try {
1064
+ let t = e.params.map((e, t) => `argument_${t}:${_(e.type)}${this.host.sample && Object.hasOwn(m, e.type) ? `,argument_${t}s:sampler${e.type.endsWith("Shadow") ? "_comparison" : ""}${this.host.samplerMode ? `,argument_${t}m:u32` : ""}` : ""}`), n = e.params.map((e, t) => Object.hasOwn(m, e.type) ? (/\b(out|inout)\b/.test(e.qualifier) && this.fail("Samplers cannot be output parameters"), this.define(e.name, e.type, "argument_" + t), "") : `var ${this.define(e.name, e.type)}:${_(e.type)} = ${/\bout\b/.test(e.qualifier) ? _(e.type) + "()" : "argument_" + t};`), r = e.params.some((e) => /\b(out|inout)\b/.test(e.qualifier)) ? `${this.names.get(e)}_result` : e.type === "void" ? "" : _(e.type), i = e.body.flatMap((e) => this.statement(e));
1065
+ return e.type === "void" && e.body.at(-1)?.kind !== "return" && i.push(...this.returnValue()), `fn ${this.names.get(e)}(${t.join(",")})${r ? "->" + r : ""}{\n${n.join("\n")}\n${i.join("\n")}\n}`;
1066
+ } finally {
1067
+ this.scopes.pop();
1068
+ }
1069
+ }).join("\n"), a = this.host.entry, o = this.functions.find((e) => e.name === a.name && e.type === a.type && e.params.length === a.params.length && e.params.every((e, t) => e.type === a.params[t]));
1070
+ return o || this.fail(this.host.missingEntry), [
1071
+ t,
1072
+ n,
1073
+ r,
1074
+ this.host.prologue,
1075
+ [...this.helpers].join(v),
1076
+ i,
1077
+ this.host.epilogue(this.names.get(o))
1078
+ ].join(v);
1079
+ }
1080
+ };
1081
+ //#endregion
1082
+ //#region src/glsl/preprocess.ts
1083
+ function x(t, n = { version: 100 }) {
1084
+ n.version = /^\s*#\s*version\s+300\s+es\s*$/m.test(t.replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, "")) ? 300 : 100;
1085
+ let r = /* @__PURE__ */ new Map([["GL_ES", { body: "1" }], ["__VERSION__", { body: String(n.version) }]]), i = (t) => {
1086
+ let n = t;
1087
+ for (let t = 0; t < 64; t++) {
1088
+ let t = !1;
1089
+ n = n.replace(/\b[A-Za-z_]\w*\b/g, (e, n, i) => {
1090
+ let a = r.get(e);
1091
+ return !a || a.params || i[n - 1] === "#" ? e : (a.body !== e && (t = !0), a.body);
1092
+ });
1093
+ for (let [i, a] of r) {
1094
+ if (!a.params) continue;
1095
+ let r = RegExp("\\b" + i + "\\s*\\(", "g"), o;
1096
+ for (; o = r.exec(n);) {
1097
+ let s = 1, c = r.lastIndex, l = c, u = [];
1098
+ for (; c < n.length && s; c++) {
1099
+ let e = n[c];
1100
+ e === "(" ? s++ : e === ")" ? --s === 0 && u.push(n.slice(l, c)) : e === "," && s === 1 && (u.push(n.slice(l, c)), l = c + 1);
1101
+ }
1102
+ if (u.length === 1 && u[0].trim() === "" && a.params.length === 0 && (u.length = 0), s || u.length !== a.params.length) throw e("failed", `GLSL macro ${i} has invalid arguments; correct its invocation`);
1103
+ let d = a.body.replace(/\b[A-Za-z_]\w*\b/g, (e) => {
1104
+ let t = a.params.indexOf(e);
1105
+ return t < 0 ? e : u[t];
1106
+ });
1107
+ n = n.slice(0, o.index) + d + n.slice(c), r.lastIndex = o.index + d.length, t = !0;
1108
+ }
1109
+ }
1110
+ if (n.length > 262144) throw e("failed", "Expanded GLSL is too large; reduce macro expansion below 262144 code units");
1111
+ if (!t) return n;
1112
+ }
1113
+ throw e("failed", "GLSL macro expansion is recursive or too deep; reduce macro nesting below 64");
1114
+ }, a = (t) => {
1115
+ let n = t.args.map(a), r = n[0] ?? 0, i = n[1] ?? 0;
1116
+ if (t.op === "literal") return Number(t.value.replace(/[uU]$/, ""));
1117
+ if (t.op === "name") return 0;
1118
+ if (t.op === "conditional") return r ? n[1] : n[2];
1119
+ if (t.op === "prefix") return t.value === "!" ? +!r : t.value === "-" ? -r : t.value === "~" ? ~r : r;
1120
+ switch (t.value) {
1121
+ case "+": return r + i;
1122
+ case "-": return r - i;
1123
+ case "*": return r * i;
1124
+ case "/": return Math.trunc(r / i);
1125
+ case "%": return r % i;
1126
+ case "==": return +(r === i);
1127
+ case "!=": return +(r !== i);
1128
+ case "<": return +(r < i);
1129
+ case ">": return +(r > i);
1130
+ case "<=": return +(r <= i);
1131
+ case ">=": return +(r >= i);
1132
+ case "&&": return +(!!r && !!i);
1133
+ case "||": return +(!!r || !!i);
1134
+ case "&": return r & i;
1135
+ case "|": return r | i;
1136
+ case "^": return r ^ i;
1137
+ case "<<": return r << i;
1138
+ case ">>": return r >> i;
1139
+ default: throw e("failed", "Invalid GLSL preprocessor condition; use an integer constant expression");
1140
+ }
1141
+ }, o = (e) => {
1142
+ let t = new s("const int condition=" + i(e.replace(/\bdefined\s*(?:\(\s*([A-Za-z_]\w*)\s*\)|([A-Za-z_]\w*))/g, (e, t, n) => r.has(t ?? n) ? "1" : "0")) + ";").parse();
1143
+ return !!a(t.globals[0].init);
1144
+ }, c = [], l = !0, u = [];
1145
+ t = t.replace(/\/\*[\s\S]*?\*\//g, (e) => e.replace(/[^\n]/g, " ")).replace(/\/\/[^\n]*/g, "").replace(/\\\r?\n/g, "");
1146
+ for (let n of t.split(/\r?\n/)) {
1147
+ let t = /^\s*#\s*(\w+)\s*(.*)$/.exec(n);
1148
+ if (!t) {
1149
+ u.push(l ? i(n) : "");
1150
+ continue;
1151
+ }
1152
+ u.push("");
1153
+ let a = t[1], s = t[2].trim();
1154
+ if ([
1155
+ "if",
1156
+ "ifdef",
1157
+ "ifndef"
1158
+ ].includes(a)) {
1159
+ let e = l && (a === "if" ? o(s) : a === "ifdef" ? r.has(s) : !r.has(s));
1160
+ c.push({
1161
+ parent: l,
1162
+ taken: e,
1163
+ active: e
1164
+ }), l = e;
1165
+ } else if (a === "else" || a === "elif") {
1166
+ let t = c.at(-1);
1167
+ if (!t) throw e("failed", "GLSL conditional has no matching #if; correct the directives");
1168
+ t.active = t.parent && !t.taken && (a === "else" || o(s)), t.taken ||= t.active, l = t.active;
1169
+ } else if (a === "endif") {
1170
+ let t = c.pop();
1171
+ if (!t) throw e("failed", "GLSL #endif has no matching #if; correct the directives");
1172
+ l = t.parent;
1173
+ } else if (l) {
1174
+ if (a === "define") {
1175
+ let t = /^([A-Za-z_]\w*)(?:\(([^)]*)\))?\s*(.*)$/.exec(s);
1176
+ if (!t) throw e("failed", "Invalid GLSL #define; supply a name and replacement");
1177
+ r.set(t[1], {
1178
+ params: t[2] === void 0 ? void 0 : t[2].trim() ? t[2].split(",").map((e) => e.trim()) : [],
1179
+ body: t[3]
1180
+ });
1181
+ } else if (a === "undef") r.delete(s);
1182
+ else if (a === "version") {
1183
+ if (s !== "100" && !/^300\s+es$/.test(s)) throw e("unsupported", "GLSL import accepts GLSL ES 1.00 and 3.00; use #version 100 or #version 300 es, or remove the directive");
1184
+ } else if (![
1185
+ "line",
1186
+ "pragma",
1187
+ "extension"
1188
+ ].includes(a)) throw e("failed", `GLSL #${a} is invalid in this transition; correct its preprocessing directives`);
1189
+ }
1190
+ }
1191
+ if (c.length) throw e("failed", "Unclosed GLSL preprocessor conditional; add the matching #endif");
1192
+ return u.join("\n");
1193
+ }
1194
+ //#endregion
1195
+ export { b as n, s as r, x as t };