@margoengine/shader 0.0.0-stage → 1.0.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 (92) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1919 -2
  4. package/dist/attribute.d.ts +28 -0
  5. package/dist/black-hole.d.ts +246 -0
  6. package/dist/black-hole.js +445 -0
  7. package/dist/clock-BLGNexlt.js +52 -0
  8. package/dist/clock.d.ts +42 -0
  9. package/dist/compute-CXGG9qvI.js +360 -0
  10. package/dist/compute-texture.d.ts +100 -0
  11. package/dist/compute-texture.js +131 -0
  12. package/dist/compute-volume.d.ts +57 -0
  13. package/dist/compute-volume.js +130 -0
  14. package/dist/compute.d.ts +200 -0
  15. package/dist/control-CFjkGPBk.js +201 -0
  16. package/dist/control.d.ts +93 -0
  17. package/dist/deform.d.ts +111 -0
  18. package/dist/deform.js +89 -0
  19. package/dist/display-BrJM_Qkd.js +5 -0
  20. package/dist/editor-model.d.ts +192 -0
  21. package/dist/editor-pass.d.ts +71 -0
  22. package/dist/editor-pass.js +216 -0
  23. package/dist/editor.d.ts +150 -0
  24. package/dist/editor.js +1071 -0
  25. package/dist/effects.d.ts +150 -0
  26. package/dist/effects.js +87 -0
  27. package/dist/errors-CsQwshHA.js +7 -0
  28. package/dist/glsl/emit.d.ts +43 -0
  29. package/dist/glsl/host.d.ts +55 -0
  30. package/dist/glsl/material.d.ts +173 -0
  31. package/dist/glsl/parser.d.ts +76 -0
  32. package/dist/glsl/preprocess.d.ts +9 -0
  33. package/dist/glsl/presets.d.ts +33 -0
  34. package/dist/glsl/program.d.ts +210 -0
  35. package/dist/glsl/scene.d.ts +9 -0
  36. package/dist/glsl-material.d.ts +82 -0
  37. package/dist/glsl-material.js +288 -0
  38. package/dist/glsl-pass.d.ts +93 -0
  39. package/dist/glsl-pass.js +186 -0
  40. package/dist/glsl.d.ts +94 -0
  41. package/dist/glsl.js +170 -0
  42. package/dist/gpu-DQJk2xAv.js +55 -0
  43. package/dist/graph.d.ts +82 -0
  44. package/dist/graph.js +365 -0
  45. package/dist/index.d.ts +8 -0
  46. package/dist/index.js +103 -0
  47. package/dist/lighting-JU9yQPfM.js +77 -0
  48. package/dist/lighting.d.ts +62 -0
  49. package/dist/lighting.js +1 -0
  50. package/dist/material-C6bpqlnE.js +1184 -0
  51. package/dist/material-CZ8V1FeA.js +299 -0
  52. package/dist/material.d.ts +79 -0
  53. package/dist/node-B6yoIC9V.js +590 -0
  54. package/dist/node.d.ts +433 -0
  55. package/dist/noise.d.ts +38 -0
  56. package/dist/noise.js +107 -0
  57. package/dist/own-stack-D1XnXrrz.js +11 -0
  58. package/dist/own-stack.d.ts +12 -0
  59. package/dist/pass.d.ts +109 -0
  60. package/dist/pass.js +239 -0
  61. package/dist/post-DLpZ110L.js +113 -0
  62. package/dist/post.d.ts +69 -0
  63. package/dist/preprocess-CWGWlBWQ.js +1195 -0
  64. package/dist/raw-0AYej3Ak.js +1500 -0
  65. package/dist/raw.d.ts +744 -0
  66. package/dist/raw.js +1 -0
  67. package/dist/readback.d.ts +42 -0
  68. package/dist/readback.js +85 -0
  69. package/dist/scope.d.ts +21 -0
  70. package/dist/serialization.d.ts +11 -0
  71. package/dist/serialization.js +192 -0
  72. package/dist/shadertoy/sound.d.ts +48 -0
  73. package/dist/shadertoy.d.ts +249 -0
  74. package/dist/shadertoy.js +909 -0
  75. package/dist/shared/caster.d.ts +159 -0
  76. package/dist/shared/fog.d.ts +33 -0
  77. package/dist/shared/fractal.d.ts +9 -0
  78. package/dist/shared/msaa.d.ts +30 -0
  79. package/dist/shared/stack.d.ts +22 -0
  80. package/dist/sound-CF_dzP41.js +78 -0
  81. package/dist/storage-format-DWaiSxmq.js +25 -0
  82. package/dist/storage-format.d.ts +7 -0
  83. package/dist/subgroups.d.ts +97 -0
  84. package/dist/subgroups.js +95 -0
  85. package/dist/texture-BawKO_h9.js +62 -0
  86. package/dist/texture-array.d.ts +7 -0
  87. package/dist/texture-array.js +11 -0
  88. package/dist/texture.d.ts +16 -0
  89. package/dist/utils.d.ts +106 -0
  90. package/dist/utils.js +90 -0
  91. package/dist/view-rect-Bm1ac-TK.js +16 -0
  92. package/package.json +149 -4
@@ -0,0 +1,590 @@
1
+ import { SRGBToLinear as e, hexColor as t } from "@margoengine/math/color";
2
+ //#region \0rolldown/runtime.js
3
+ var n = Object.defineProperty, r = (e, t) => {
4
+ let r = {};
5
+ for (var i in e) n(r, i, {
6
+ get: e[i],
7
+ enumerable: !0
8
+ });
9
+ return t || n(r, Symbol.toStringTag, { value: "Module" }), r;
10
+ }, i = {}, a = (e) => {
11
+ let { list: t, store: n, flush: r } = i;
12
+ i.list = i.store = i.flush = void 0;
13
+ try {
14
+ return e();
15
+ } finally {
16
+ i.list = t, i.store = n, i.flush = r;
17
+ }
18
+ }, o = /* @__PURE__ */ r({
19
+ Builder: () => Wt,
20
+ FIXED: () => 8,
21
+ Fn: () => Pt,
22
+ LEAF: () => 1,
23
+ MUTABLE: () => 32,
24
+ Node: () => u,
25
+ ONCE: () => 16,
26
+ OP: () => 2,
27
+ Program: () => Bt,
28
+ STMT: () => 4,
29
+ UniformNode: () => It,
30
+ abs: () => ge,
31
+ acos: () => k,
32
+ add: () => de,
33
+ asin: () => O,
34
+ atan: () => A,
35
+ atan2: () => xe,
36
+ bitangentWorld: () => ft,
37
+ bool: () => Be,
38
+ builtin: () => Q,
39
+ call: () => b,
40
+ cameraFar: () => ht,
41
+ cameraNear: () => mt,
42
+ cameraPosition: () => Tt,
43
+ cast: () => _,
44
+ ceil: () => ye,
45
+ clamp: () => q,
46
+ color: () => Qe,
47
+ constant: () => g,
48
+ cos: () => E,
49
+ cross: () => De,
50
+ degrees: () => V,
51
+ deltaTime: () => kt,
52
+ discard: () => Ie,
53
+ distance: () => ke,
54
+ div: () => me,
55
+ dot: () => Ae,
56
+ dpdx: () => H,
57
+ dpdy: () => Se,
58
+ exp: () => M,
59
+ exp2: () => N,
60
+ float: () => Le,
61
+ floor: () => ve,
62
+ fract: () => w,
63
+ fwidth: () => U,
64
+ hdrTarget: () => Ct,
65
+ instanceColor: () => xt,
66
+ instanceMatrix: () => bt,
67
+ instanceUV: () => St,
68
+ int: () => Re,
69
+ inverseSqrt: () => L,
70
+ isMatrix: () => p,
71
+ ivec2: () => We,
72
+ ivec3: () => Ge,
73
+ ivec4: () => Ke,
74
+ length: () => Oe,
75
+ linearColor: () => X,
76
+ linearDepth: () => pt,
77
+ log: () => P,
78
+ log2: () => F,
79
+ loop: () => Nt,
80
+ mat3: () => Xe,
81
+ mat4: () => Ze,
82
+ max: () => G,
83
+ min: () => W,
84
+ mix: () => Te,
85
+ mod: () => he,
86
+ modelMatrix: () => gt,
87
+ mul: () => pe,
88
+ negate: () => Ne,
89
+ normalLocal: () => ot,
90
+ normalView: () => lt,
91
+ normalWorld: () => st,
92
+ normalize: () => z,
93
+ not: () => Pe,
94
+ oneMinus: () => Me,
95
+ positionLocal: () => it,
96
+ positionView: () => ct,
97
+ positionWorld: () => at,
98
+ pow: () => K,
99
+ projectionMatrix: () => vt,
100
+ radians: () => B,
101
+ reflect: () => Ee,
102
+ refract: () => je,
103
+ resolution: () => At,
104
+ round: () => be,
105
+ sameEntries: () => Vt,
106
+ saturate: () => R,
107
+ scalarOf: () => d,
108
+ screenCoordinate: () => Et,
109
+ screenUV: () => Dt,
110
+ select: () => Fe,
111
+ sign: () => _e,
112
+ sin: () => T,
113
+ sizeOf: () => f,
114
+ smoothstep: () => we,
115
+ sqrt: () => I,
116
+ step: () => Ce,
117
+ sub: () => fe,
118
+ tan: () => D,
119
+ tangentLocal: () => ut,
120
+ tangentWorld: () => dt,
121
+ tanh: () => j,
122
+ time: () => Ot,
123
+ trunc: () => C,
124
+ uint: () => ze,
125
+ uniform: () => Lt,
126
+ unique: () => l,
127
+ uv: () => nt,
128
+ uvec2: () => qe,
129
+ uvec3: () => Je,
130
+ uvec4: () => Ye,
131
+ varying: () => jt,
132
+ vec2: () => Ve,
133
+ vec3: () => He,
134
+ vec4: () => Ue,
135
+ vecOf: () => m,
136
+ vertexColor: () => rt,
137
+ vertexIndex: () => yt,
138
+ viewDirection: () => wt,
139
+ viewMatrix: () => _t,
140
+ wgslFn: () => Ft
141
+ }), s = 1, c = 2, ee = 4, te = 8, ne = 16, re = 32, ie = 0, l = () => ie++, u = class {
142
+ constructor(e, t, n, r, i = 0) {
143
+ this.type = e, this.key = t, this.args = n, this.emit = r, this.flags = i;
144
+ }
145
+ }, ae = /^vec([234])(f|h|i|u|<bool>)$/, oe = /^mat([234])x\1f$/, d = (e) => oe.test(e) ? "f32" : ae.exec(e)?.[2] === "<bool>" ? "bool" : {
146
+ f: "f32",
147
+ h: "f16",
148
+ i: "i32",
149
+ u: "u32"
150
+ }[ae.exec(e)?.[2] ?? ""] ?? e, f = (e) => +(ae.exec(e)?.[1] ?? 1), p = (e) => oe.test(e), m = (e, t) => t === 1 ? e : e === "bool" ? `vec${t}<bool>` : `vec${t}${e === "f16" ? "h" : e[0]}`, h = (e) => {
151
+ throw TypeError(e);
152
+ };
153
+ function se(e, t) {
154
+ if (t === "bool") return String(!!e);
155
+ if (typeof e != "number" || !Number.isFinite(e)) return h(`Shader constant must be a finite number, got ${String(e)}`);
156
+ if (t === "f16") return e + "h";
157
+ if (t !== "f32") return (!Number.isInteger(e) || t === "u32" && e < 0) && h(`${e} is not a valid ${t} constant`), e + t[0];
158
+ let n = String(e);
159
+ return /[.e]/.test(n) ? n : n + ".0";
160
+ }
161
+ function g(e, t) {
162
+ let n = d(t), r = se(e, n);
163
+ return new u(t, "l" + r, [], () => t === n ? r : `${t}(${r})`, 1 | (r[0] === "-" ? 2 : 0));
164
+ }
165
+ function _(e, t, n = "value") {
166
+ return e instanceof u ? e.type === t ? e : f(e.type) === 1 && !p(e.type) && d(e.type) === d(t) && !p(t) ? ce(e, t) : h(`${n} must be ${t}, got ${e.type}`) : g(e, t);
167
+ }
168
+ var ce = (e, t) => new u(t, "s", [e], (e, n) => `${t}(${e.expr(n.args[0])})`), v = (e) => e instanceof u ? e : g(e, typeof e == "boolean" ? "bool" : "f32");
169
+ function y(e, t) {
170
+ let n = t.filter((e) => e instanceof u), r = n[0]?.type ?? "f32", i = n.reduce((e, t) => f(t.type) > f(e) || p(t.type) ? t.type : e, r);
171
+ return n.some((e) => d(e.type) !== d(i) || e.type !== i && f(e.type) !== 1) && h(`${e}(${t.map((e) => e instanceof u ? e.type : typeof e).join(", ")}): operand types do not match`), t.map((t, n) => _(t, i, `${e} argument ${n + 1}`));
172
+ }
173
+ function b(e, t, n, r = e) {
174
+ return new u(n, "c" + r, t, (e, t) => `${r}(${t.args.map((t) => e.expr(t)).join(", ")})`);
175
+ }
176
+ var x = (e, t = e) => (...n) => {
177
+ let r = y(e, n);
178
+ return b(e, r, r[0].type, t);
179
+ }, le = (e) => (...t) => {
180
+ let n = y(e, t);
181
+ return b(e, n, d(n[0].type), e);
182
+ }, ue = (e, t) => new u(t.type, "u" + e, [t], (t, n) => e + t.wrap(n.args[0]), 2);
183
+ function S(e, t, n, r) {
184
+ let i = v(t), a = v(n);
185
+ t instanceof u || (i = _(t, d(a.type))), n instanceof u || (a = _(n, d(i.type)));
186
+ let o, s = d(i.type);
187
+ return r || e === "&&" || e === "||" ? ([i, a] = y(e, [i, a]), o = m("bool", f(i.type)), !r && s !== "bool" && h(`${e} needs bool operands`)) : i.type === a.type ? o = i.type : s === d(a.type) && (f(i.type) === 1 && !p(i.type) || f(a.type) === 1 && !p(a.type)) ? o = f(i.type) === 1 && !p(i.type) ? a.type : i.type : e === "*" && p(i.type) && a.type === `vec${i.type[3]}f` ? o = a.type : e === "*" && p(a.type) && i.type === `vec${a.type[3]}f` && (o = i.type), !o || s === "bool" && !r && e !== "&&" && e !== "||" ? h(`${i.type} ${e} ${a.type}: operand types do not combine`) : new u(o, "o" + e, [i, a], (t, n) => `${t.wrap(n.args[0])} ${e} ${t.wrap(n.args[1])}`, 2);
188
+ }
189
+ var de = (e, t) => S("+", e, t, !1), fe = (e, t) => S("-", e, t, !1), pe = (e, t) => S("*", e, t, !1), me = (e, t) => S("/", e, t, !1), he = (e, t) => S("%", e, t, !1), ge = x("abs"), _e = x("sign"), ve = x("floor"), ye = x("ceil"), be = x("round"), C = x("trunc"), w = x("fract"), T = x("sin"), E = x("cos"), D = x("tan"), O = x("asin"), k = x("acos"), A = x("atan"), j = x("tanh"), xe = x("atan2"), M = x("exp"), N = x("exp2"), P = x("log"), F = x("log2"), I = x("sqrt"), L = x("inverseSqrt"), R = x("saturate"), z = x("normalize"), B = x("radians"), V = x("degrees"), H = x("dpdx"), Se = x("dpdy"), U = x("fwidth"), W = x("min"), G = x("max"), K = x("pow"), q = x("clamp"), Ce = x("step"), we = x("smoothstep"), Te = x("mix"), Ee = x("reflect"), De = x("cross"), Oe = le("length"), ke = le("distance"), Ae = le("dot"), je = (e, t, n) => b("refract", [
190
+ _(e, "vec3f"),
191
+ _(t, "vec3f"),
192
+ _(n, "f32")
193
+ ], "vec3f"), Me = (e) => S("-", 1, e, !1), Ne = (e) => ue("-", e), Pe = (e) => d(e.type) === "bool" ? ue("!", e) : h("not() needs bool");
194
+ function Fe(e, t, n) {
195
+ let [r, i] = y("select", [t, n]);
196
+ return b("select", [
197
+ i,
198
+ r,
199
+ _(e, f(e instanceof u ? e.type : "bool") === 1 ? "bool" : m("bool", f(r.type)), "select condition")
200
+ ], r.type);
201
+ }
202
+ function Ie(e, t) {
203
+ let n = v(t);
204
+ return new u(n.type, "d", [_(e, "bool", "discard condition"), n], (e, t) => {
205
+ e.stage.kind !== "fragment" && e.stage.kind !== "post" && h(`discard() is not available in ${e.stage.kind} nodes`), e.line(`if (${e.expr(t.args[0])}) { discard; }`);
206
+ let n = e.name();
207
+ return e.line(`let ${n} = ${e.expr(t.args[1])};`), n;
208
+ }, 4);
209
+ }
210
+ function J(e, t) {
211
+ return e instanceof u ? e.type === t ? e : ((p(t) || p(e.type) || f(e.type) !== 1 && f(e.type) !== f(t)) && h(`Cannot convert ${e.type} to ${t}`), new u(t, "v", [e], (e, n) => `${t}(${e.expr(n.args[0])})`)) : g(e, t);
212
+ }
213
+ function Y(e, t) {
214
+ let n = d(e), r = p(e) ? e[3] ** 2 : f(e);
215
+ if (t.length === 1 && !(p(e) && t[0] instanceof u && t[0].type === e)) return J(t[0], e);
216
+ let i = t.map((e) => {
217
+ let t = e instanceof u ? e : g(e, n);
218
+ return d(t.type) === n ? t : J(t, m(n, f(t.type)));
219
+ }), a = i.reduce((e, t) => e + (p(t.type) ? r : f(t.type)), 0);
220
+ return a !== r && h(`${e} needs ${r} components, got ${a}`), new u(e, "k", i, (t, n) => `${e}(${n.args.map((e) => t.expr(e)).join(", ")})`);
221
+ }
222
+ var Le = (e) => J(e, "f32"), Re = (e) => J(e, "i32"), ze = (e) => J(e, "u32"), Be = (e) => J(e, "bool"), Ve = (...e) => Y("vec2f", e), He = (...e) => Y("vec3f", e), Ue = (...e) => Y("vec4f", e), We = (...e) => Y("vec2i", e), Ge = (...e) => Y("vec3i", e), Ke = (...e) => Y("vec4i", e), qe = (...e) => Y("vec2u", e), Je = (...e) => Y("vec3u", e), Ye = (...e) => Y("vec4u", e), Xe = (...e) => Y("mat3x3f", e), Ze = (...e) => Y("mat4x4f", e);
223
+ function X(n) {
224
+ let r = typeof n == "number" ? n >= 0 && n <= 16777215 ? [
225
+ 16,
226
+ 8,
227
+ 0
228
+ ].map((e) => (n >> e & 255) / 255) : void 0 : typeof n == "string" ? t(n)?.slice(0, 3) : void 0;
229
+ return (r ? r.map(e) : Array.isArray(n) && n.length === 3 ? [...n] : h("Expected a #rrggbb or 0xrrggbb colour or [r, g, b]")).map((e) => Math.round(e * 1e6) / 1e6);
230
+ }
231
+ var Qe = (e) => Y("vec3f", X(e)), $e = (e, t) => {
232
+ let n = [...t].map((e) => "xyzwrgba".indexOf(e) % 4), r = f(e.type), i = m(d(e.type), t.length), a = n.map((e) => "xyzw"[e]).join("");
233
+ return p(e.type) && h(`.${t} is not valid on ${e.type}`), r === 1 ? n.some((e) => e) ? h(`.${t} is not valid on a scalar`) : t.length === 1 ? e : ce(e, i) : (n.some((e) => e >= r) && h(`.${t} is out of range for ${e.type}`), new u(i, "." + a, [e], (e, t) => {
234
+ let r = e.wrap(t.args[0]), i = e.program.canon(t.args[0]).key[0] === "b" && /^([\w.]+)\.([xyzw]{1,4})$/.exec(r);
235
+ return i ? `${i[1]}.${n.map((e) => i[2][e]).join("")}` : `${r}.${a}`;
236
+ }));
237
+ }, Z = u.prototype, et = (e, t) => t ? et(e, t - 1).flatMap((t) => [...e].map((e) => t + e)) : [""];
238
+ for (let e of ["xyzw", "rgba"]) for (let t = 1; t < 5; t++) for (let n of et(e, t)) Object.defineProperty(Z, n, { get() {
239
+ return $e(this, n);
240
+ } });
241
+ for (let [e, t] of [
242
+ ["add", "+"],
243
+ ["sub", "-"],
244
+ ["mul", "*"],
245
+ ["div", "/"],
246
+ ["mod", "%"],
247
+ ["and", "&&"],
248
+ ["or", "||"]
249
+ ]) Z[e] = function(e) {
250
+ return S(t, this, e, !1);
251
+ };
252
+ for (let [e, t] of [
253
+ ["lessThan", "<"],
254
+ ["lessThanEqual", "<="],
255
+ ["greaterThan", ">"],
256
+ ["greaterThanEqual", ">="],
257
+ ["equal", "=="],
258
+ ["notEqual", "!="]
259
+ ]) Z[e] = function(e) {
260
+ return S(t, this, e, !0);
261
+ };
262
+ var tt = {
263
+ abs: ge,
264
+ sign: _e,
265
+ floor: ve,
266
+ ceil: ye,
267
+ round: be,
268
+ trunc: C,
269
+ fract: w,
270
+ sin: T,
271
+ cos: E,
272
+ tan: D,
273
+ asin: O,
274
+ acos: k,
275
+ atan: A,
276
+ tanh: j,
277
+ exp: M,
278
+ exp2: N,
279
+ log: P,
280
+ log2: F,
281
+ sqrt: I,
282
+ inverseSqrt: L,
283
+ saturate: R,
284
+ normalize: z,
285
+ radians: B,
286
+ degrees: V,
287
+ dpdx: H,
288
+ dpdy: Se,
289
+ fwidth: U,
290
+ min: W,
291
+ max: G,
292
+ pow: K,
293
+ clamp: q,
294
+ step: Ce,
295
+ mix: Te,
296
+ reflect: Ee,
297
+ cross: De,
298
+ length: Oe,
299
+ distance: ke,
300
+ dot: Ae,
301
+ oneMinus: Me,
302
+ negate: Ne,
303
+ not: Pe,
304
+ toFloat: (e) => J(e, m("f32", f(e.type))),
305
+ toInt: (e) => J(e, m("i32", f(e.type))),
306
+ toUint: (e) => J(e, m("u32", f(e.type)))
307
+ };
308
+ for (let e in tt) Z[e] = function(...t) {
309
+ return tt[e](this, ...t);
310
+ };
311
+ Z.element = function(e) {
312
+ let t = p(this.type);
313
+ !t && f(this.type) === 1 && h("element() needs a vector or matrix");
314
+ let n = e instanceof u ? e : g(e, "i32");
315
+ return new u(t ? m("f32", +this.type[3]) : d(this.type), "[]", [this, n], (e, t) => `${e.wrap(t.args[0])}[${e.expr(t.args[1])}]`);
316
+ };
317
+ var Q = (e, t) => new u(t, "b" + e, [], (t) => t.builtin(e), 1), nt = () => Q("uv", "vec2f"), rt = () => Q("vertexColor", "vec4f"), it = /*@__PURE__*/ Q("positionLocal", "vec3f"), at = /*@__PURE__*/ Q("positionWorld", "vec3f"), ot = /*@__PURE__*/ Q("normalLocal", "vec3f"), st = /*@__PURE__*/ Q("normalWorld", "vec3f"), ct = /*@__PURE__*/ Q("positionView", "vec3f"), lt = /*@__PURE__*/ Q("normalView", "vec3f"), ut = /*@__PURE__*/ Q("tangentLocal", "vec3f"), dt = /*@__PURE__*/ Q("tangentWorld", "vec3f"), ft = /*@__PURE__*/ Q("bitangentWorld", "vec3f"), pt = /*@__PURE__*/ Q("linearDepth", "f32"), mt = /*@__PURE__*/ Q("cameraNear", "f32"), ht = /*@__PURE__*/ Q("cameraFar", "f32"), gt = /*@__PURE__*/ Q("modelMatrix", "mat4x4f"), _t = /*@__PURE__*/ Q("viewMatrix", "mat4x4f"), vt = /*@__PURE__*/ Q("projectionMatrix", "mat4x4f"), yt = /*@__PURE__*/ Q("vertexIndex", "u32"), bt = /*@__PURE__*/ Q("instanceMatrix", "mat4x4f"), xt = /*@__PURE__*/ Q("instanceColor", "vec4f"), St = /*@__PURE__*/ Q("instanceUV", "vec4f"), Ct = /*@__PURE__*/ Q("hdrTarget", "f32"), wt = /*@__PURE__*/ Q("viewDirection", "vec3f"), Tt = /*@__PURE__*/ Q("cameraPosition", "vec3f"), Et = /*@__PURE__*/ Q("screenCoordinate", "vec2f"), Dt = /*@__PURE__*/ Q("screenUV", "vec2f"), $ = (e, t) => new u(t, "f" + e, [], (n) => n.frame(e, t), 1), Ot = /*@__PURE__*/ $("time", "f32"), kt = /*@__PURE__*/ $("deltaTime", "f32"), At = /*@__PURE__*/ $("resolution", "vec2f");
318
+ function jt(e) {
319
+ return (d(e.type) === "bool" || p(e.type)) && h("varying() needs a numeric scalar or vector"), new u(e.type, "y", [e], (e, t) => e.stage.varying ? e.stage.varying(e, t.args[0]) : e.expr(t.args[0]), 1);
320
+ }
321
+ var Mt = (e) => new u(e, "p" + l(), [], (e, t) => e.param(t), 1);
322
+ function Nt(e, t, n) {
323
+ let r = v(t), i = Mt("i32"), o = Mt(r.type), s = _(a(() => n(i, o)), r.type, "loop body result"), c = e instanceof u ? e.type === "i32" ? e : J(e, "i32") : g(e, "i32");
324
+ return new u(r.type, "r" + l(), [
325
+ c,
326
+ r,
327
+ s
328
+ ], (e, t) => {
329
+ let n = e.expr(t.args[0]), r = e.expr(t.args[1]), a = e.name(), s = e.name();
330
+ e.line(`var ${a} = ${r};`), e.bind(i, s), e.bind(o, a), e.epoch++, e.push();
331
+ let c = e.expr(t.args[2]), ee = e.pop();
332
+ e.line(`for (var ${s} = 0i; ${s} < ${n}; ${s}++) {`);
333
+ for (let t of ee) e.line(" " + t);
334
+ return e.line(` ${a} = ${c};`), e.line("}"), a;
335
+ }, 4);
336
+ }
337
+ function Pt(e, t = "func") {
338
+ (!/^[a-zA-Z]\w*$/.test(t) || t.startsWith("nd") || t === "fn") && h("Fn names are WGSL identifiers not starting with nd");
339
+ let n = /* @__PURE__ */ new Map();
340
+ return (...r) => {
341
+ let i = r.map((e) => v(e)), o = i.map((e) => e.type).join(), s = n.get(o);
342
+ if (!s) {
343
+ let r = i.map((e) => Mt(e.type));
344
+ s = {
345
+ name: t,
346
+ id: l(),
347
+ params: r,
348
+ result: v(a(() => e(...r)))
349
+ }, n.set(o, s);
350
+ }
351
+ let c = s;
352
+ return new u(c.result.type, "n" + c.id, i, (e, t) => `${e.fn(c)}(${t.args.map((t) => e.expr(t)).join(", ")})`);
353
+ };
354
+ }
355
+ function Ft(e) {
356
+ let t = /fn\s+(\w+)\s*\(([^)]*)\)\s*->\s*([\w<>]+)/.exec(e) ?? h("wgslFn needs \"fn name(params) -> type\""), n = t[2].split(",").filter((e) => e.trim()).map((e) => e.split(":")[1]?.trim());
357
+ return (...r) => (r.length !== n.length && h(`${t[1]} takes ${n.length} arguments, got ${r.length}`), new u(t[3], "w" + e, r.map((e, r) => _(e, n[r], `${t[1]} argument ${r + 1}`)), (n, r) => (n.declare(e, e), `${t[1]}(${r.args.map((e) => n.expr(e)).join(", ")})`)));
358
+ }
359
+ var It = class extends u {
360
+ constructor(e, t, n) {
361
+ super(t, "u" + l(), [], (e, t) => e.uniform(t), 1), this.name = n, d(t) === "bool" && h("Uniforms cannot be bool; use u32"), n !== void 0 && (!/^[a-zA-Z]\w*$/.test(n) || /^(alias|break|case|const|const_assert|continue|continuing|default|diagnostic|discard|else|enable|false|fn|for|if|let|loop|override|requires|return|struct|switch|true|var|while|[fiu]32|f16|bool|vec\d\w*|mat\d\w*|array|atomic|ptr|sampler\w*|texture\w*)$/.test(n)) && h(`Uniform name ${n} is not a usable WGSL identifier`), this.data = [], this.value = e;
362
+ }
363
+ get value() {
364
+ return this.data.length === 1 ? this.data[0] : [...this.data];
365
+ }
366
+ set value(e) {
367
+ let t = p(this.type) ? this.type[3] ** 2 : f(this.type), n = typeof e == "number" && t === 1 ? [e] : typeof e == "string" || typeof e == "number" ? X(e) : Array.from(e), r = d(this.type) === "f32";
368
+ (n.length !== t || n.some((e) => !Number.isFinite(r ? Math.fround(e) : e))) && h(`Uniform ${this.name ?? ""} needs ${t} finite${r ? " float32" : ""} numbers`), this.data = n;
369
+ }
370
+ write(e, t) {
371
+ let n = d(this.type), r = p(this.type) ? +this.type[3] : 0;
372
+ this.data.forEach((i, a) => {
373
+ let o = t + (r ? Math.floor(a / r) * (r === 2 ? 8 : 16) + a % r * 4 : a * 4);
374
+ n === "f32" ? e.setFloat32(o, i, !0) : n === "i32" ? e.setInt32(o, i, !0) : e.setUint32(o, i, !0);
375
+ });
376
+ }
377
+ };
378
+ function Lt(e, t = {}) {
379
+ let n = typeof e == "string" ? 3 : typeof e == "number" ? 1 : e.length;
380
+ return new It(e, t.type ?? {
381
+ 1: "f32",
382
+ 2: "vec2f",
383
+ 3: "vec3f",
384
+ 4: "vec4f",
385
+ 9: "mat3x3f",
386
+ 16: "mat4x4f"
387
+ }[n] ?? h("Unsupported uniform value"), t.name);
388
+ }
389
+ var Rt = (e) => p(e) ? e[3] === "2" ? 8 : 16 : [
390
+ 4,
391
+ 8,
392
+ 16,
393
+ 16
394
+ ][f(e) - 1], zt = (e) => p(e) ? e[3] === "2" ? 16 : e[3] * 16 : f(e) * 4, Bt = class {
395
+ constructor(e = 0, t = !1) {
396
+ this.base = e, this.compute = t, this.members = [], this.resources = [], this.varyings = [], this.functions = /* @__PURE__ */ new Map(), this.declarations = /* @__PURE__ */ new Map(), this.canonical = /* @__PURE__ */ new Map(), this.canonOf = /* @__PURE__ */ new Map(), this.ids = /* @__PURE__ */ new Map(), this.refs = /* @__PURE__ */ new Map(), this.mutable = /* @__PURE__ */ new Map(), this.size = 16, this.counter = 0, this.buffers = /* @__PURE__ */ new WeakMap(), this.next = e;
397
+ }
398
+ stage(e, t) {
399
+ for (let e of t) this.count(e);
400
+ let n = new Wt(this, e), r = t.map((e) => n.expr(e));
401
+ return {
402
+ lines: n.pop(),
403
+ values: r
404
+ };
405
+ }
406
+ declare(e) {
407
+ let t = [];
408
+ for (let n of this.resources) n === this.block ? t.push(`struct NdUniforms {\n${this.members.map((e) => ` ${e.name}:${e.type},`).join("\n")}\n}\n@group(${e}) @binding(${n.binding}) var<uniform> ndu:NdUniforms;`) : t.push(n.resource.declare(n.name, e, n.binding, this.compute));
409
+ let n = [...this.declarations.values()], r = n.filter((e) => e.startsWith("enable "));
410
+ return [
411
+ ...r,
412
+ ...t,
413
+ ...n.filter((e) => !r.includes(e))
414
+ ].join("\n");
415
+ }
416
+ entries(e, t) {
417
+ return this.resources.flatMap((n) => (n === this.block ? [{ buffer: this.upload(e, t) }] : n.resource.entries(e, t)).map((e, t) => ({
418
+ binding: n.binding + t,
419
+ resource: e
420
+ })));
421
+ }
422
+ release(e) {
423
+ this.buffers.get(e)?.buffer.destroy(), this.buffers.delete(e);
424
+ }
425
+ upload(e, t) {
426
+ let n = this.buffers.get(e);
427
+ if (!n) {
428
+ let t = new ArrayBuffer(this.size);
429
+ n = {
430
+ buffer: e.createBuffer({
431
+ label: "shader:uniforms",
432
+ size: this.size,
433
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
434
+ }),
435
+ view: new DataView(t),
436
+ bytes: new Uint8Array(t)
437
+ }, this.buffers.set(e, n);
438
+ }
439
+ for (let e of this.members) {
440
+ if (e.node) {
441
+ e.node.write(n.view, e.offset);
442
+ continue;
443
+ }
444
+ let r = e.frame === "time" ? [t.time ?? 0] : e.frame === "deltaTime" ? [t.delta ?? 0] : e.frame === "resolution" ? [t.width, t.height] : t.frames?.[e.frame] ?? [];
445
+ for (let t = 0; t < r.length; t++) n.view.setFloat32(e.offset + t * 4, r[t], !0);
446
+ }
447
+ return e.queue.writeBuffer(n.buffer, 0, n.bytes), n.buffer;
448
+ }
449
+ name() {
450
+ return "nd" + this.counter++;
451
+ }
452
+ canon(e) {
453
+ let t = this.canonOf.get(e);
454
+ if (t) return t;
455
+ e instanceof u || h("Expected a shader node");
456
+ let n = e.type + e.key + "(" + e.args.map((e) => this.ids.get(this.canon(e))).join() + ")";
457
+ return t = this.canonical.get(n), t || (t = e, this.canonical.set(n, e), this.ids.set(e, this.ids.size)), this.canonOf.set(e, t), t;
458
+ }
459
+ count(e) {
460
+ let t = this.canon(e), n = (this.refs.get(t) ?? 0) + 1;
461
+ if (this.refs.set(t, n), n === 1) for (let e of t.args) this.count(e);
462
+ }
463
+ shared(e) {
464
+ return (this.refs.get(e) ?? 0) > 1;
465
+ }
466
+ volatile(e) {
467
+ let t = this.mutable.get(e);
468
+ return t === void 0 && (t = !(e.flags & 8) && (!!(e.flags & 32) || e.args.some((e) => this.volatile(this.canon(e)))), this.mutable.set(e, t)), t;
469
+ }
470
+ declareOnce(e, t) {
471
+ this.declarations.has(e) || (this.declarations.set(e, ""), this.declarations.set(e, t()));
472
+ }
473
+ resource(e) {
474
+ let t = this.resources.find((t) => t.resource === e);
475
+ return t || (t = {
476
+ resource: e,
477
+ name: e.prefix + this.resources.length,
478
+ binding: this.next
479
+ }, this.next += e.bindings, this.resources.push(t)), t.name;
480
+ }
481
+ member(e, t, n, r) {
482
+ let i = this.members.find((e) => n ? e.node === n : e.frame === r);
483
+ if (!i) {
484
+ this.block || (this.block = {
485
+ resource: {
486
+ prefix: "ndu",
487
+ bindings: 1,
488
+ declare: () => "",
489
+ entries: () => []
490
+ },
491
+ name: "ndu",
492
+ binding: this.next++
493
+ }, this.resources.push(this.block));
494
+ let a = t, o = 1;
495
+ for (; this.members.some((e) => e.name === a);) a = t + o++;
496
+ let s = Math.ceil((this.members.length ? this.members.at(-1).offset + zt(this.members.at(-1).type) : 0) / Rt(e)) * Rt(e);
497
+ i = {
498
+ name: a,
499
+ type: e,
500
+ offset: s,
501
+ node: n,
502
+ frame: r
503
+ }, this.members.push(i), this.size = Math.max(16, Math.ceil((s + zt(e)) / 16) * 16);
504
+ }
505
+ return "ndu." + i.name;
506
+ }
507
+ }, Vt = (e, t) => e.length === t.length && e.every((e, n) => {
508
+ let r = e.resource, i = t[n].resource;
509
+ return e.binding === t[n].binding && (r === i || !!r?.buffer && r.buffer === i?.buffer && r.offset === i.offset && r.size === i.size);
510
+ }), Ht = /^(?:vec[234][fhiu]?|mat[234]x[234][fh]?|f32|f16|i32|u32|bool|true|false|abs|acos|asin|atan|atan2|ceil|clamp|cos|cosh|cross|degrees|determinant|distance|dot|exp|exp2|floor|fract|inverseSqrt|length|log|log2|max|min|mix|normalize|pow|radians|reflect|round|saturate|sign|sin|sinh|smoothstep|sqrt|step|tan|tanh|transpose|trunc)$/, Ut = (e, t) => (e.match(/(?<![\w.])[A-Za-z_]\w*/g) ?? []).every((e) => Ht.test(e) || t.has(e)), Wt = class e {
511
+ constructor(e, t) {
512
+ this.program = e, this.stage = t, this.inFn = !1, this.epoch = 0, this.scopes = [[]], this.memo = /* @__PURE__ */ new Map(), this.params = /* @__PURE__ */ new Map(), this.consts = /* @__PURE__ */ new Set();
513
+ }
514
+ expr(e) {
515
+ let t = this.program.canon(e), n = this.memo.get(t);
516
+ if (n && this.scopes.includes(n.scope) && (n.epoch === this.epoch || !this.program.volatile(t))) return n.name;
517
+ n && t.flags & 16 && h("A value made inside an If, Loop or Switch body cannot be used outside it; assign it to a Var");
518
+ let r = t.emit(this, t), i = this.scopes.at(-1), a = this.epoch;
519
+ if (t.flags & 4) return this.memo.set(t, {
520
+ name: r,
521
+ scope: i,
522
+ epoch: a
523
+ }), r;
524
+ if (t.flags & 1 || !this.program.shared(t)) return r;
525
+ let o = this.program.name();
526
+ return (f(t.type) > 1 || p(t.type)) && Ut(r, this.consts) ? (this.consts.add(o), this.line(`const ${o}: ${t.type} = ${r};`)) : this.line(`let ${o} = ${r};`), this.memo.set(t, {
527
+ name: o,
528
+ scope: i,
529
+ epoch: a
530
+ }), o;
531
+ }
532
+ wrap(e) {
533
+ let t = this.expr(e);
534
+ return this.program.canon(e).flags & 2 && !/^[\w.]+$/.test(t) ? `(${t})` : t;
535
+ }
536
+ line(e) {
537
+ this.scopes.at(-1).push(e);
538
+ }
539
+ push() {
540
+ this.scopes.push([]);
541
+ }
542
+ pop() {
543
+ return this.scopes.pop();
544
+ }
545
+ name() {
546
+ return this.program.name();
547
+ }
548
+ bind(e, t) {
549
+ this.params.set(this.program.canon(e), t);
550
+ }
551
+ unbind(e) {
552
+ this.params.delete(this.program.canon(e));
553
+ }
554
+ param(e) {
555
+ return this.params.get(e) ?? h("A loop or Fn parameter or a Var was used outside its body");
556
+ }
557
+ local(e) {
558
+ return this.inFn ? h("Stage inputs such as uv() cannot be read inside Fn; pass them as arguments") : e;
559
+ }
560
+ builtin(e) {
561
+ let t = this.stage.builtins[e];
562
+ return t === void 0 && h(`${e} is not available in ${this.stage.kind} nodes`), typeof t == "string" ? this.local(t) : t(this);
563
+ }
564
+ resource(e) {
565
+ return this.program.resource(e);
566
+ }
567
+ uniform(e) {
568
+ return this.program.member(e.type, e.name ?? "u", e);
569
+ }
570
+ frame(e, t) {
571
+ return this.program.member(t, e, void 0, e);
572
+ }
573
+ declare(e, t) {
574
+ this.program.declareOnce(e, () => t);
575
+ }
576
+ fn(t) {
577
+ let n = "fn#" + t.id, r = this.program.functions, i = r.get(n);
578
+ if (i) return i;
579
+ i = t.name;
580
+ for (let e = 1; [...r.values()].includes(i); e++) i = t.name + "_" + e;
581
+ r.set(n, i);
582
+ let a = new e(this.program, this.stage);
583
+ return a.inFn = !0, t.params.forEach((e, t) => a.bind(e, "p" + t)), this.program.count(t.result), this.program.declareOnce(n, () => {
584
+ let e = a.expr(t.result), n = a.pop();
585
+ return `fn ${i}(${t.params.map((e, t) => `p${t}:${e.type}`).join(", ")}) -> ${t.result.type} {\n${n.map((e) => " " + e + "\n").join("")} return ${e};\n}`;
586
+ }), i;
587
+ }
588
+ };
589
+ //#endregion
590
+ export { We as $, ze as $t, E as A, Ee as At, M as B, _e as Bt, mt as C, Me as Ct, q as D, K as Dt, ye as E, at as Et, ke as F, R as Ft, U as G, Ce as Gt, Le as H, f as Ht, me as I, d as It, bt as J, ut as Jt, Ct as K, fe as Kt, Ae as L, Et as Lt, V as M, At as Mt, kt as N, be as Nt, Qe as O, vt as Ot, Ie as P, Vt as Pt, p as Q, C as Qt, H as R, Dt as Rt, ht as S, Pe as St, _ as T, ct as Tt, ve as U, we as Ut, N as V, T as Vt, w as W, I as Wt, Re as X, j as Xt, St as Y, dt as Yt, L as Z, Ot as Zt, xe as _, o as _t, re as a, Ye as an, P as at, Q as b, st as bt, c, He as cn, Xe as ct, It as d, rt as dn, W as dt, Lt as en, Ge as et, ge as f, yt as fn, Te as ft, A as g, i as gn, Ne as gt, O as h, Ft as hn, pe as ht, s as i, Je as in, pt as it, De as j, je as jt, g as k, B as kt, Bt as l, Ue as ln, Ze as lt, de as m, _t as mn, gt as mt, te as n, nt as nn, Oe as nt, u as o, jt as on, F as ot, k as p, wt as pn, he as pt, xt as q, D as qt, Pt as r, qe as rn, X as rt, ne as s, Ve as sn, Nt as st, Wt as t, l as tn, Ke as tt, ee as u, m as un, G as ut, ft as v, ot as vt, Tt as w, it as wt, b as x, z as xt, Be as y, lt as yt, Se as z, Fe as zt };