reze-engine 0.43.0 → 0.50.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 (135) hide show
  1. package/README.md +40 -438
  2. package/dist/camera.d.ts +3 -0
  3. package/dist/camera.d.ts.map +1 -1
  4. package/dist/camera.js +33 -8
  5. package/dist/engine.d.ts +841 -53
  6. package/dist/engine.d.ts.map +1 -1
  7. package/dist/engine.js +3498 -451
  8. package/dist/graph/registry.d.ts +2 -2
  9. package/dist/graph/registry.d.ts.map +1 -1
  10. package/dist/graph/registry.js +1 -1
  11. package/dist/graph/slots.d.ts +0 -1
  12. package/dist/graph/slots.d.ts.map +1 -1
  13. package/dist/graph/slots.js +37 -9
  14. package/dist/hdr.d.ts +18 -0
  15. package/dist/hdr.d.ts.map +1 -0
  16. package/dist/hdr.js +162 -0
  17. package/dist/ibl.d.ts +19 -0
  18. package/dist/ibl.d.ts.map +1 -0
  19. package/dist/ibl.js +113 -0
  20. package/dist/ik-solver.d.ts +2 -1
  21. package/dist/ik-solver.d.ts.map +1 -1
  22. package/dist/index.d.ts +5 -1
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +10 -0
  25. package/dist/math.d.ts +20 -1
  26. package/dist/math.d.ts.map +1 -1
  27. package/dist/math.js +23 -16
  28. package/dist/midi-loader.d.ts +10 -0
  29. package/dist/midi-loader.d.ts.map +1 -0
  30. package/dist/midi-loader.js +247 -0
  31. package/dist/model.d.ts +2 -13
  32. package/dist/model.d.ts.map +1 -1
  33. package/dist/param-track.d.ts +48 -0
  34. package/dist/param-track.d.ts.map +1 -0
  35. package/dist/param-track.js +80 -0
  36. package/dist/physics/types.d.ts.map +1 -1
  37. package/dist/physics/types.js +3 -0
  38. package/dist/reflection.d.ts +27 -0
  39. package/dist/reflection.d.ts.map +1 -0
  40. package/dist/reflection.js +93 -0
  41. package/dist/shaders/anchor-table.d.ts +56 -0
  42. package/dist/shaders/anchor-table.d.ts.map +1 -0
  43. package/dist/shaders/anchor-table.js +128 -0
  44. package/dist/shaders/cast-api.d.ts +2 -0
  45. package/dist/shaders/cast-api.d.ts.map +1 -0
  46. package/dist/shaders/cast-api.js +121 -0
  47. package/dist/shaders/cast-layout.d.ts +21 -0
  48. package/dist/shaders/cast-layout.d.ts.map +1 -0
  49. package/dist/shaders/cast-layout.js +20 -0
  50. package/dist/shaders/lights.d.ts +79 -0
  51. package/dist/shaders/lights.d.ts.map +1 -0
  52. package/dist/shaders/lights.js +269 -0
  53. package/dist/shaders/lyrics-api.d.ts +39 -0
  54. package/dist/shaders/lyrics-api.d.ts.map +1 -0
  55. package/dist/shaders/lyrics-api.js +187 -0
  56. package/dist/shaders/materials/common.d.ts +2 -4
  57. package/dist/shaders/materials/common.d.ts.map +1 -1
  58. package/dist/shaders/materials/common.js +87 -35
  59. package/dist/shaders/midi-api.d.ts +10 -0
  60. package/dist/shaders/midi-api.d.ts.map +1 -0
  61. package/dist/shaders/midi-api.js +114 -0
  62. package/dist/shaders/passes/composite.d.ts +28 -22
  63. package/dist/shaders/passes/composite.d.ts.map +1 -1
  64. package/dist/shaders/passes/composite.js +165 -138
  65. package/dist/shaders/passes/cull.d.ts +2 -0
  66. package/dist/shaders/passes/cull.d.ts.map +1 -0
  67. package/dist/shaders/passes/cull.js +138 -0
  68. package/dist/shaders/passes/field-blit.d.ts +26 -0
  69. package/dist/shaders/passes/field-blit.d.ts.map +1 -0
  70. package/dist/shaders/passes/field-blit.js +65 -0
  71. package/dist/shaders/passes/grid.d.ts +31 -0
  72. package/dist/shaders/passes/grid.d.ts.map +1 -0
  73. package/dist/shaders/passes/grid.js +169 -0
  74. package/dist/shaders/passes/ground.d.ts +13 -1
  75. package/dist/shaders/passes/ground.d.ts.map +1 -1
  76. package/dist/shaders/passes/ground.js +170 -25
  77. package/dist/shaders/passes/hosted-api.d.ts +57 -0
  78. package/dist/shaders/passes/hosted-api.d.ts.map +1 -0
  79. package/dist/shaders/passes/hosted-api.js +166 -0
  80. package/dist/shaders/passes/id-debug.d.ts +28 -0
  81. package/dist/shaders/passes/id-debug.d.ts.map +1 -0
  82. package/dist/shaders/passes/id-debug.js +74 -0
  83. package/dist/shaders/passes/particles.d.ts +10 -2
  84. package/dist/shaders/passes/particles.d.ts.map +1 -1
  85. package/dist/shaders/passes/particles.js +37 -109
  86. package/dist/shaders/passes/scene-contract.d.ts +128 -0
  87. package/dist/shaders/passes/scene-contract.d.ts.map +1 -0
  88. package/dist/shaders/passes/scene-contract.js +207 -0
  89. package/dist/shaders/passes/sim.d.ts +34 -0
  90. package/dist/shaders/passes/sim.d.ts.map +1 -0
  91. package/dist/shaders/passes/sim.js +169 -0
  92. package/dist/shaders/passes/trails.d.ts +14 -2
  93. package/dist/shaders/passes/trails.d.ts.map +1 -1
  94. package/dist/shaders/passes/trails.js +55 -90
  95. package/dist/shaders/score-api.d.ts +10 -0
  96. package/dist/shaders/score-api.d.ts.map +1 -0
  97. package/dist/shaders/score-api.js +114 -0
  98. package/dist/shadow-cascades.d.ts +45 -0
  99. package/dist/shadow-cascades.d.ts.map +1 -0
  100. package/dist/shadow-cascades.js +70 -0
  101. package/dist/vmd-loader.d.ts +3 -2
  102. package/dist/vmd-loader.d.ts.map +1 -1
  103. package/package.json +1 -1
  104. package/src/camera.ts +31 -8
  105. package/src/engine.ts +4015 -556
  106. package/src/graph/registry.ts +2 -2
  107. package/src/graph/slots.ts +37 -9
  108. package/src/hdr.ts +156 -0
  109. package/src/ibl.ts +115 -0
  110. package/src/ik-solver.ts +1 -1
  111. package/src/index.ts +12 -0
  112. package/src/math.ts +23 -17
  113. package/src/midi-loader.ts +246 -0
  114. package/src/model.ts +3 -3
  115. package/src/param-track.ts +83 -0
  116. package/src/physics/types.ts +4 -1
  117. package/src/reflection.ts +94 -0
  118. package/src/shaders/anchor-table.ts +147 -0
  119. package/src/shaders/cast-api.ts +123 -0
  120. package/src/shaders/cast-layout.ts +20 -0
  121. package/src/shaders/lights.ts +280 -0
  122. package/src/shaders/lyrics-api.ts +202 -0
  123. package/src/shaders/materials/common.ts +89 -35
  124. package/src/shaders/midi-api.ts +116 -0
  125. package/src/shaders/passes/composite.ts +182 -139
  126. package/src/shaders/passes/cull.ts +139 -0
  127. package/src/shaders/passes/grid.ts +178 -0
  128. package/src/shaders/passes/ground.ts +172 -25
  129. package/src/shaders/passes/hosted-api.ts +171 -0
  130. package/src/shaders/passes/id-debug.ts +75 -0
  131. package/src/shaders/passes/particles.ts +54 -112
  132. package/src/shaders/passes/scene-contract.ts +266 -0
  133. package/src/shaders/passes/trails.ts +77 -93
  134. package/src/shadow-cascades.ts +97 -0
  135. package/src/vmd-loader.ts +2 -2
@@ -0,0 +1,207 @@
1
+ /**
2
+ * The scene pass's attachment contract, in one place.
3
+ *
4
+ * Everything drawn INSIDE the scene pass — models, ground, outline hulls,
5
+ * particles, ribbons, and the transparent depth prepass — shares one set of
6
+ * attachments, and therefore one set of formats, blends and write masks. That
7
+ * agreement used to be restated at every pipeline that joins the pass and in
8
+ * every shader that writes to it, which is why adding an attachment was
9
+ * dangerous rather than tedious: a class left behind does not fail loudly, it
10
+ * fails as a validation error naming a pipeline that was never edited.
11
+ *
12
+ * So the contract is DATA here, and the pipelines ask for it by render class.
13
+ * Adding the id attachment (MRT) becomes one edit in this file plus a per-class
14
+ * decision about whether that class writes it — which is the shape of the
15
+ * question, and now the shape of the code.
16
+ *
17
+ * WHAT IS NOT HERE. Two passes look like they belong and do not:
18
+ * - the FIELD pass has its own pair of rgba16float targets, its own blend and
19
+ * no MSAA. It is a different pass with a different contract.
20
+ * - the gizmo and selection-edge pipelines draw to the SWAPCHAIN in their own
21
+ * unmultisampled pass, so the presentation format is the whole of their
22
+ * contract. The plan listed gizmo as a scene render class; the code says
23
+ * otherwise, and the code is right.
24
+ */
25
+ /**
26
+ * The id attachment's format: (material index, object index), one u16 each.
27
+ *
28
+ * Two 16-bit channels rather than one 32-bit: 32-bit formats are not
29
+ * multisamplable, and this attachment is multisampled with the rest of the
30
+ * pass. Uint targets take no blend at all per spec, which is exactly right —
31
+ * an averaged id is not an id.
32
+ */
33
+ export const SCENE_ID_FORMAT = "rg16uint";
34
+ /**
35
+ * Whether the scene pass carries the id attachment.
36
+ *
37
+ * Runtime rather than a compile-time constant, and mutable, because it is not
38
+ * only a decision — it is a CAPABILITY. Multisampled rg16uint has to be probed
39
+ * on the device (see the engine's init), and a device that cannot do it must
40
+ * leave this off. That forces the shaders to be assembled after the probe,
41
+ * which is why the two shader modules that gain an output stopped being
42
+ * module-level constants: a string baked at import cannot know what the device
43
+ * said.
44
+ *
45
+ * Set ONCE at init, before any pipeline or shader module is built. Nothing
46
+ * reads it per frame.
47
+ */
48
+ let mrtIds = false;
49
+ /** Called by the engine at init, after probing the device. */
50
+ export function setMrtIds(on) {
51
+ mrtIds = on;
52
+ }
53
+ export function mrtIdsEnabled() {
54
+ return mrtIds;
55
+ }
56
+ const ALPHA_OVER = {
57
+ color: { srcFactor: "src-alpha", dstFactor: "one-minus-src-alpha", operation: "add" },
58
+ alpha: { srcFactor: "one", dstFactor: "one-minus-src-alpha", operation: "add" },
59
+ };
60
+ /** Colour sums; alpha is not touched (src zero, dst one). */
61
+ const ADD_KEEP_ALPHA = {
62
+ color: { srcFactor: "one", dstFactor: "one", operation: "add" },
63
+ alpha: { srcFactor: "zero", dstFactor: "one", operation: "add" },
64
+ };
65
+ /** Both channels sum. rg8unorm clamps at 1, which is the saturation alpha-over
66
+ * would have reached anyway. */
67
+ const ADD_BOTH = {
68
+ color: { srcFactor: "one", dstFactor: "one", operation: "add" },
69
+ alpha: { srcFactor: "one", dstFactor: "one", operation: "add" },
70
+ };
71
+ /**
72
+ * OVER for something that arrives ALREADY premultiplied: take the source whole
73
+ * and let it displace the destination by its own coverage.
74
+ *
75
+ * The ground needs this and nothing else does. Every other class writes a
76
+ * straight colour and an alpha, and the src-alpha factor premultiplies it once
77
+ * on the way in. The ground cannot: its coverage is the SUM of a lit surface
78
+ * and a colourless shadow-catcher layer, so it has to weight its own colour by
79
+ * the surface's share before it gets here. Handed to the src-alpha blend, that
80
+ * weighting happened a second time.
81
+ */
82
+ const PREMULTIPLIED_OVER = {
83
+ color: { srcFactor: "one", dstFactor: "one-minus-src-alpha", operation: "add" },
84
+ alpha: { srcFactor: "one", dstFactor: "one-minus-src-alpha", operation: "add" },
85
+ };
86
+ /** Additive, premultiplied by the fragment's alpha as it writes. */
87
+ const ADD_PREMULTIPLIED = {
88
+ color: { srcFactor: "src-alpha", dstFactor: "one", operation: "add" },
89
+ alpha: { srcFactor: "zero", dstFactor: "one", operation: "add" },
90
+ };
91
+ /**
92
+ * Which classes actually WRITE an id, and so gain a fragment output for it.
93
+ *
94
+ * Everything else keeps its shader exactly as it is and takes the id target at
95
+ * writeMask 0 — legal, specified, and free. The alternative (leaving the target
96
+ * off those pipelines) is not available: every pipeline in a pass must agree
97
+ * with the pass's attachments.
98
+ *
99
+ * The ground is in because a mark placed by id needs the floor to have one.
100
+ * Transparent fabric writes ids too — dissolving a dress needs the dress's own
101
+ * pixels, and last write wins there, which is a documented choice rather than a
102
+ * consequence. Outline hulls, particles and ribbons are OUT: they are not
103
+ * things you would ever address by id, and a hull would overwrite the id of the
104
+ * body it traces.
105
+ */
106
+ const WRITES_ID = new Set(["material", "ground"]);
107
+ /** The blends each class writes its two attachments with. */
108
+ const BLENDS = {
109
+ material: [ALPHA_OVER, ALPHA_OVER],
110
+ // PREMULTIPLIED colour, alone among the classes — see the blend's own note.
111
+ // The aux is ordinary alpha-over: the ground writes its mask unweighted, like
112
+ // everything else, and coverage is what the blend applies.
113
+ ground: [PREMULTIPLIED_OVER, ALPHA_OVER],
114
+ outline: [ALPHA_OVER, ALPHA_OVER],
115
+ particle: [ALPHA_OVER, ALPHA_OVER],
116
+ "particle-additive": [ADD_KEEP_ALPHA, ADD_BOTH],
117
+ trail: [ADD_PREMULTIPLIED, ALPHA_OVER],
118
+ };
119
+ /**
120
+ * The colour targets a scene-pass pipeline of this class declares, in
121
+ * attachment order.
122
+ *
123
+ * Fresh objects every call, deliberately: a caller that mutated a shared
124
+ * descriptor would change every pipeline built after it, and the ones built
125
+ * before would keep the old value — a difference that only shows up as one
126
+ * pipeline blending unlike its neighbours.
127
+ */
128
+ export function sceneTargets(cls, formats) {
129
+ const targets = cls === "depth-prepass"
130
+ ? // Format only, and writeMask 0. Note the asymmetry this leans on, which
131
+ // is the same one the id target leans on below: a target the shader has
132
+ // no output for is legal at writeMask 0 (gpuweb#1918), while an output
133
+ // with no target is NOT governed (gpuweb#5341). This is the specified
134
+ // direction, and it is the only one this file ever uses.
135
+ [
136
+ { format: formats.hdr, writeMask: 0 },
137
+ { format: formats.aux, writeMask: 0 },
138
+ ]
139
+ : (() => {
140
+ const [color, aux] = BLENDS[cls];
141
+ return [
142
+ { format: formats.hdr, blend: { color: { ...color.color }, alpha: { ...color.alpha } } },
143
+ { format: formats.aux, blend: { color: { ...aux.color }, alpha: { ...aux.alpha } } },
144
+ ];
145
+ })();
146
+ if (mrtIds)
147
+ targets.push({ format: SCENE_ID_FORMAT, writeMask: WRITES_ID.has(cls) ? 0xf : 0 });
148
+ return targets;
149
+ }
150
+ /**
151
+ * The pass's colour attachments, in order — for the things that describe the
152
+ * pass itself rather than a draw within it.
153
+ *
154
+ * A render bundle declares the formats it will be replayed into and is rejected
155
+ * against a pass that does not match, so the bundle encoder has to move in
156
+ * lockstep with the targets above. It restated the list independently until
157
+ * this existed, which made it the one consumer an MRT change would have missed
158
+ * — a bundle is recorded once and replayed, so the failure would have arrived
159
+ * at replay, naming the bundle rather than the attachment that changed.
160
+ */
161
+ export function sceneColorFormats(formats) {
162
+ return mrtIds ? [formats.hdr, formats.aux, SCENE_ID_FORMAT] : [formats.hdr, formats.aux];
163
+ }
164
+ /**
165
+ * The fragment-output struct a scene-pass shader returns.
166
+ *
167
+ * Emitted rather than written out per file so that the attachment list has one
168
+ * author. The struct and the targets above have to agree on count and order,
169
+ * and they now disagree in one file rather than in five.
170
+ *
171
+ * Only the shaders that will GAIN an output take this today — the materials
172
+ * (hand-written and graph-generated alike, through COMMON_FS_OUT_WGSL) and the
173
+ * ground. Outline, particles and ribbons keep their own declarations on
174
+ * purpose: they never write the id, so they would take an `id: false` argument
175
+ * forever, and their structs carry comments about their own blend that belong
176
+ * where they are.
177
+ */
178
+ export function sceneFsOutWgsl(opts) {
179
+ const name = opts?.name ?? "FSOut";
180
+ const aux = opts?.aux ?? "mask";
181
+ // No @interpolate here, deliberately. The plan called for
182
+ // `@location(2) @interpolate(flat)`, and that attribute is only legal on a
183
+ // vertex OUTPUT or a fragment INPUT — a fragment output is neither, so it
184
+ // would not compile. It is also unnecessary: the id is read from the per-draw
185
+ // uniform, not carried across the triangle as a varying, so there is no
186
+ // interpolation to suppress. (A varying carrying it WOULD need flat, since an
187
+ // integer varying must be.)
188
+ //
189
+ // vec2u for rg16uint: the output type must be compatible with the format, and
190
+ // uint targets take no blend, which is what makes last-write-wins the rule.
191
+ const id = mrtIds ? ` @location(2) id: vec2u,\n` : "";
192
+ return `struct ${name} {
193
+ @location(0) color: vec4f,
194
+ @location(1) ${aux}: vec4f,
195
+ ${id}};
196
+ `;
197
+ }
198
+ /**
199
+ * The line a fragment shader assigns its id with, or nothing when ids are off.
200
+ *
201
+ * Emitted rather than written into each shader for the same reason as the
202
+ * struct: with ids off there must be no assignment either, and a shader cannot
203
+ * ask the device what it supports.
204
+ */
205
+ export function sceneIdWriteWgsl(out, material, object) {
206
+ return mrtIds ? ` ${out}.id = vec2u(${material}, ${object});\n` : "";
207
+ }
@@ -0,0 +1,34 @@
1
+ import { type CastLayout } from "./particles";
2
+ export declare const SIM_MAX = 1024;
3
+ /** rgba16float, ping-ponged. Two of these is the whole memory cost. */
4
+ export declare const SIM_FORMAT: GPUTextureFormat;
5
+ /** `// @sim 256` — the grid's resolution, in texels per side. */
6
+ export declare function parseSimSize(wgsl: string, max: number): number;
7
+ /** Whether this effect drives a grid at all. */
8
+ export declare function simEntryPoint(wgsl: string): boolean;
9
+ /**
10
+ * `rzSim(uv)`, for a shader that READS the grid rather than steps it.
11
+ *
12
+ * Always compiled in, even for an effect with no grid — it then samples a 1×1
13
+ * of zeroes. An accessor that exists only sometimes is one an author has to
14
+ * guard, and a missing function is a compile error rather than a blank result.
15
+ */
16
+ export declare function simReadApi(group: number, tex: number, samp: number, size: number): string;
17
+ /** `rzTime`/`rzDt`, for a module that has no clock of its own — the field pass.
18
+ * Same reason as above: a spliced simStep refers to them. */
19
+ export declare function simClockApi(timeExpr: string): string;
20
+ /**
21
+ * The step shader: one invocation per texel, once per frame.
22
+ *
23
+ * The previous grid is bound as a SAMPLED texture and the next as a storage
24
+ * texture, which is what makes the two different resources and the whole thing
25
+ * legal — a shader cannot read and write one texture coherently, and the
26
+ * ping-pong is not an optimisation but the only correct way to do this.
27
+ *
28
+ * Sampled rather than only fetched, because advection needs to read BETWEEN
29
+ * texels: a semi-Lagrangian step asks "what was at the place this parcel came
30
+ * from", and that place is almost never a texel centre. Point-sampling it is
31
+ * what turns smoke into a staircase.
32
+ */
33
+ export declare function buildSimShader(wgsl: string, size: number, _cast: CastLayout): string;
34
+ //# sourceMappingURL=sim.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sim.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/sim.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAA;AAgC7C,eAAO,MAAM,OAAO,OAAO,CAAA;AAC3B,uEAAuE;AACvE,eAAO,MAAM,UAAU,EAAE,gBAAgC,CAAA;AAEzD,iEAAiE;AACjE,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAM9D;AAED,gDAAgD;AAChD,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEnD;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CA6BzF;AAED;8DAC8D;AAC9D,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAKpD;AAWD;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,MAAM,CAwDpF"}
@@ -0,0 +1,169 @@
1
+ import { audioApi } from "../audio-api";
2
+ import { scoreApi } from "../score-api";
3
+ import { EFFECT_SCENE_API } from "./composite";
4
+ // A persistent grid an effect can step and read: the one thing an effect could
5
+ // not have before, which is MEMORY.
6
+ //
7
+ // Every other mount is a pure function of position and time. A field shader
8
+ // recomputes its noise from scratch every frame; a trail reads history the
9
+ // engine happens to keep. Neither can build on what it produced last frame, and
10
+ // a whole class of effect is nothing but that:
11
+ //
12
+ // Fog that a dancer walks through does not "look displaced near her" — it IS
13
+ // displaced, and stays displaced, and creeps back over seconds. Water does not
14
+ // look rippled where a foot landed; a ripple leaves and propagates and
15
+ // reflects off the far edge long after the foot is gone. Both are the same
16
+ // shape of thing — a grid, stepped by its own previous value.
17
+ //
18
+ // It is also the only way to get the LOOK of a fluid, not merely its motion.
19
+ // Advection compounds: each frame's field is the last one pushed sideways, and a
20
+ // few hundred frames of that stretches and folds a smooth field into filaments
21
+ // and vortex sheets finer than the grid storing them. No noise function
22
+ // produces that, at any octave count, because the structure is not a function of
23
+ // position at all — it is a function of history.
24
+ //
25
+ // The engine owns the ping-pong, the dispatch and the sampling. The author owns
26
+ // the KERNEL, which is the part that differs: advection for smoke, the wave
27
+ // equation for water, a decaying stamp for footprints. That split is the same
28
+ // one the particle mounts make, and for the same reason — the loop is identical
29
+ // in every such effect and the physics never is.
30
+ // A megatexel of rgba16float is 8MB, and two of those is the whole cost. What
31
+ // actually bounds this is the STEP: one invocation per texel per frame, each
32
+ // taking a dozen samples, so doubling the side quadruples the work.
33
+ export const SIM_MAX = 1024;
34
+ /** rgba16float, ping-ponged. Two of these is the whole memory cost. */
35
+ export const SIM_FORMAT = "rgba16float";
36
+ /** `// @sim 256` — the grid's resolution, in texels per side. */
37
+ export function parseSimSize(wgsl, max) {
38
+ const m = /^\s*\/\/\s*@sim\s+(\d+)\s*$/m.exec(wgsl);
39
+ if (!m)
40
+ return 0;
41
+ // Clamped rather than rejected, as the particle count is: an author asking for
42
+ // 4096 gets the most the engine will give and a scene that still runs.
43
+ return Math.max(8, Math.min(max, parseInt(m[1], 10)));
44
+ }
45
+ /** Whether this effect drives a grid at all. */
46
+ export function simEntryPoint(wgsl) {
47
+ return /\bfn\s+simStep\s*\(/.test(wgsl);
48
+ }
49
+ /**
50
+ * `rzSim(uv)`, for a shader that READS the grid rather than steps it.
51
+ *
52
+ * Always compiled in, even for an effect with no grid — it then samples a 1×1
53
+ * of zeroes. An accessor that exists only sometimes is one an author has to
54
+ * guard, and a missing function is a compile error rather than a blank result.
55
+ */
56
+ export function simReadApi(group, tex, samp, size) {
57
+ return /* wgsl */ `
58
+ const RZ_SIM_SIZE: f32 = ${size > 0 ? size : 1}.0;
59
+ @group(${group}) @binding(${tex}) var _rzSimTex: texture_2d<f32>;
60
+ @group(${group}) @binding(${samp}) var _rzSimSamp: sampler;
61
+
62
+ /**
63
+ * The grid, bilinearly sampled. uv is 0..1 across it, and what that MEANS in
64
+ * the world is the effect's own business — the engine deliberately does not
65
+ * impose a mapping, because a fog laid over a stage and a ripple field around a
66
+ * character want different ones and both are two lines of arithmetic.
67
+ */
68
+ fn rzSim(uv: vec2f) -> vec4f {
69
+ return textureSampleLevel(_rzSimTex, _rzSimSamp, clamp(uv, vec2f(0.0), vec2f(1.0)), 0.0);
70
+ }
71
+ fn rzSimSize() -> f32 { return RZ_SIM_SIZE; }
72
+ fn rzSimTexel() -> f32 { return 1.0 / RZ_SIM_SIZE; }
73
+
74
+ // ── The step-only half, defined here too and never called here ──
75
+ //
76
+ // One effect file is spliced into EVERY module it has a mount in, so a file
77
+ // with a grid compiles its simStep inside the field and particle shaders as
78
+ // well — where it is dead code that nothing calls, but still code that has to
79
+ // resolve. Leaving these out is a compile error on a function the author was
80
+ // right to write. rzSimPrev reads the current grid rather than a previous one,
81
+ // which is the honest answer outside the step: there is no previous frame here.
82
+ fn rzSimPrev(uv: vec2f) -> vec4f { return rzSim(uv); }
83
+ fn rzSimFrame() -> i32 { return 1; }
84
+ `;
85
+ }
86
+ /** `rzTime`/`rzDt`, for a module that has no clock of its own — the field pass.
87
+ * Same reason as above: a spliced simStep refers to them. */
88
+ export function simClockApi(timeExpr) {
89
+ return /* wgsl */ `
90
+ fn rzTime() -> f32 { return ${timeExpr}; }
91
+ fn rzDt() -> f32 { return 0.0; }
92
+ `;
93
+ }
94
+ const SIM_UNIFORMS = /* wgsl */ `
95
+ struct SimU {
96
+ time: f32,
97
+ dt: f32,
98
+ size: f32,
99
+ frame: f32,
100
+ }
101
+ `;
102
+ /**
103
+ * The step shader: one invocation per texel, once per frame.
104
+ *
105
+ * The previous grid is bound as a SAMPLED texture and the next as a storage
106
+ * texture, which is what makes the two different resources and the whole thing
107
+ * legal — a shader cannot read and write one texture coherently, and the
108
+ * ping-pong is not an optimisation but the only correct way to do this.
109
+ *
110
+ * Sampled rather than only fetched, because advection needs to read BETWEEN
111
+ * texels: a semi-Lagrangian step asks "what was at the place this parcel came
112
+ * from", and that place is almost never a texel centre. Point-sampling it is
113
+ * what turns smoke into a staircase.
114
+ */
115
+ export function buildSimShader(wgsl, size, _cast) {
116
+ return (SIM_UNIFORMS +
117
+ /* wgsl */ `
118
+ @group(0) @binding(0) var<uniform> su: SimU;
119
+ @group(0) @binding(1) var _rzSimPrevTex: texture_2d<f32>;
120
+ @group(0) @binding(2) var _rzSimSamp: sampler;
121
+ @group(0) @binding(3) var _rzSimOut: texture_storage_2d<${SIM_FORMAT}, write>;
122
+ @group(0) @binding(4) var<storage, read> _rzCast: array<vec4f>;
123
+ // The same view uniform the composite reads, so the scene API below is the real
124
+ // thing here rather than a stub — a kernel can ask where a bone is, and a
125
+ // foreground spliced into this module resolves its camera calls.
126
+ @group(0) @binding(6) var<uniform> viewU: array<vec4<f32>, 15>;
127
+
128
+ fn rzTime() -> f32 { return su.time; }
129
+ fn rzDt() -> f32 { return su.dt; }
130
+ /** Texels per side. */
131
+ fn rzSimSize() -> f32 { return su.size; }
132
+ /** One texel, in uv — the step a neighbour lookup takes. */
133
+ fn rzSimTexel() -> f32 { return 1.0 / su.size; }
134
+ /**
135
+ * Steps since this effect was installed. Zero on the very first one, which is
136
+ * the only chance to seed a grid: everything after it builds on what is there.
137
+ */
138
+ fn rzSimFrame() -> i32 { return i32(su.frame); }
139
+
140
+ /** The grid as it was LAST frame, bilinear. The only input a kernel really has. */
141
+ fn rzSimPrev(uv: vec2f) -> vec4f {
142
+ return textureSampleLevel(_rzSimPrevTex, _rzSimSamp, clamp(uv, vec2f(0.0), vec2f(1.0)), 0.0);
143
+ }
144
+ ` +
145
+ EFFECT_SCENE_API +
146
+ audioApi(0, 5) +
147
+ scoreApi(0, 7) +
148
+ // rzSim itself, so a kernel may read the grid it is writing — through the
149
+ // PREVIOUS frame's texture, which is the only version of it that exists
150
+ // while the current one is still being written.
151
+ /* wgsl */ `
152
+ fn rzSim(uv: vec2f) -> vec4f { return rzSimPrev(uv); }
153
+ ` +
154
+ "\n// ── user effect (setEffect) ──\n" +
155
+ wgsl +
156
+ /* wgsl */ `
157
+
158
+ @compute @workgroup_size(8, 8)
159
+ fn main(@builtin(global_invocation_id) gid: vec3u) {
160
+ let n = u32(${size});
161
+ if (gid.x >= n || gid.y >= n) { return; }
162
+ let xy = vec2i(i32(gid.x), i32(gid.y));
163
+ // The texel CENTRE, so a kernel that reads its own uv back through rzSimPrev
164
+ // lands on itself rather than a quarter-texel off.
165
+ let uv = (vec2f(f32(gid.x), f32(gid.y)) + vec2f(0.5)) / su.size;
166
+ textureStore(_rzSimOut, xy, simStep(uv, textureLoad(_rzSimPrevTex, xy, 0), su.dt));
167
+ }
168
+ `);
169
+ }
@@ -1,8 +1,11 @@
1
- export type TrailSource = {
1
+ type TrailSource = {
2
2
  /** The author's WGSL verbatim. */
3
3
  wgsl: string;
4
- /** Declared anchors with `trail`, in slot order. */
4
+ /** How many ribbons to draw — one per trailed anchor. */
5
5
  slots: number;
6
+ /** For each ribbon, the LOCAL anchor slot it belongs to. Identity when every
7
+ * anchor is trailed; otherwise it skips the untrailed ones. */
8
+ ribbonSlots: number[];
6
9
  /** Additive, like most glowing ribbons, or straight alpha. */
7
10
  blend: "alpha" | "additive";
8
11
  bloom: boolean;
@@ -43,5 +46,14 @@ export declare function buildTrailShader(src: TrailSource, cast: {
43
46
  samples: number;
44
47
  base: number;
45
48
  trailBase: number;
49
+ slots: number;
50
+ alias: number[];
51
+ /** Depth convention of the scene buffer this layer tests against. The
52
+ * occlusion compare below is MANUAL (the layer has no depth attachment),
53
+ * so it does not flip with the pipelines' depthCompare — it has to be
54
+ * emitted the right way round at build time. On a reversed-Z device,
55
+ * larger z is CLOSER; the unflipped test drew ribbons only when occluded. */
56
+ reversedZ: boolean;
46
57
  }): string;
58
+ export {};
47
59
  //# sourceMappingURL=trails.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"trails.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/trails.ts"],"names":[],"mappings":"AAkBA,MAAM,MAAM,WAAW,GAAG;IACxB,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAA;IACZ,oDAAoD;IACpD,KAAK,EAAE,MAAM,CAAA;IACb,8DAA8D;IAC9D,KAAK,EAAE,OAAO,GAAG,UAAU,CAAA;IAC3B,KAAK,EAAE,OAAO,CAAA;CACf,CAAA;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB,IAAI,CAAA;AAEnC,6EAA6E;AAC7E,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAKjF;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAsVvI"}
1
+ {"version":3,"file":"trails.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/trails.ts"],"names":[],"mappings":"AAwBA,KAAK,WAAW,GAAG;IACjB,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAA;IACZ,yDAAyD;IACzD,KAAK,EAAE,MAAM,CAAA;IACb;oEACgE;IAChE,WAAW,EAAE,MAAM,EAAE,CAAA;IACrB,8DAA8D;IAC9D,KAAK,EAAE,OAAO,GAAG,UAAU,CAAA;IAC3B,KAAK,EAAE,OAAO,CAAA;CACf,CAAA;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB,IAAI,CAAA;AAEnC,6EAA6E;AAC7E,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAKjF;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,WAAW,EAChB,IAAI,EAAE;IACJ,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAA;IACjB,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,EAAE,CAAA;IACf;;;;kFAI8E;IAC9E,SAAS,EAAE,OAAO,CAAA;CACnB,GACA,MAAM,CA6SR"}
@@ -1,4 +1,10 @@
1
+ import { RZ_LIGHT_STRUCT_WGSL } from "../lights";
1
2
  import { audioApi } from "../audio-api";
3
+ import { lyricsApi } from "../lyrics-api";
4
+ import { anchorAliasWgsl, ribbonSlotWgsl } from "../anchor-table";
5
+ import { midiApi } from "../midi-api";
6
+ import { CAST_API } from "../cast-api";
7
+ import { clockApi, EFFECT_MATH_API, PARTICLE_STRUCT_WGSL, trailSlotsApi, viewportApi } from "./hosted-api";
2
8
  /**
3
9
  * Sub-segments drawn between each pair of recorded samples.
4
10
  *
@@ -33,60 +39,21 @@ export function trailEntryPoints(wgsl) {
33
39
  * of vanishing edge-on where it turns.
34
40
  */
35
41
  export function buildTrailShader(src, cast) {
36
- return (`const RZ_SUBJECTS: i32 = ${cast.subjects};
37
- const RZ_SAMPLES: i32 = ${cast.samples};
38
- const RZ_SLOTS: i32 = ${src.slots};
39
- const RZ_TRAIL_SLOTS: i32 = ${src.slots};
42
+ return (CAST_API +
43
+ trailSlotsApi(src.slots) +
44
+ EFFECT_MATH_API +
45
+ PARTICLE_STRUCT_WGSL +
46
+ clockApi("tu.time", "0.0") +
47
+ viewportApi("cam.targetHeight") +
48
+ // RZ_SLOTS is this module's alone: how many ribbons to DRAW, which is the
49
+ // instance loop's bound. RZ_MAX_ANCHORS in CAST_API is the address space the
50
+ // accessors are bounded by, and the two being one number was the old trail
51
+ // bug — the comment that said so lived here, so it stays here.
52
+ `const RZ_SLOTS: i32 = ${src.slots};
53
+ ${ribbonSlotWgsl(src.ribbonSlots)}
54
+ ${anchorAliasWgsl(cast.alias)}
40
55
  const SUB: i32 = ${TRAIL_SUBDIVISIONS};
41
56
 
42
- // The same struct and helpers the particle modules define. The whole effect
43
- // file is spliced into EVERY module its mounts compile into, so an effect that
44
- // declares both trails and particles carries its particle functions through
45
- // this module as dead code — dead code that still has to type-check. Keeping
46
- // the two preludes' public surface identical is what makes one file, two
47
- // mounts work.
48
- struct Particle {
49
- pos: vec3f,
50
- age: f32,
51
- vel: vec3f,
52
- life: f32,
53
- size: f32,
54
- rot: f32,
55
- seed: f32,
56
- stretch: f32,
57
- }
58
- fn rzDt() -> f32 { return 0.0; }
59
- fn rzHash21(p: vec2f) -> f32 {
60
- var p3 = fract(vec3f(p.x, p.y, p.x) * 0.1031);
61
- p3 = p3 + dot(p3, p3.yzx + 33.33);
62
- return fract((p3.x + p3.y) * p3.z);
63
- }
64
- fn rzHash31(p: vec3f) -> f32 {
65
- var p3 = fract(p * 0.1031);
66
- p3 = p3 + dot(p3, p3.zyx + 31.32);
67
- return fract((p3.x + p3.y) * p3.z);
68
- }
69
- fn rzHash13(x: f32) -> vec3f {
70
- return vec3f(rzHash11(x), rzHash11(x + 17.13), rzHash11(x + 41.71));
71
- }
72
- fn rzValueNoise(p: vec3f) -> f32 {
73
- let i = floor(p);
74
- let f = fract(p);
75
- let u = f * f * (3.0 - 2.0 * f);
76
- let n000 = rzHash31(i);
77
- let n100 = rzHash31(i + vec3f(1.0, 0.0, 0.0));
78
- let n010 = rzHash31(i + vec3f(0.0, 1.0, 0.0));
79
- let n110 = rzHash31(i + vec3f(1.0, 1.0, 0.0));
80
- let n001 = rzHash31(i + vec3f(0.0, 0.0, 1.0));
81
- let n101 = rzHash31(i + vec3f(1.0, 0.0, 1.0));
82
- let n011 = rzHash31(i + vec3f(0.0, 1.0, 1.0));
83
- let n111 = rzHash31(i + vec3f(1.0, 1.0, 1.0));
84
- let x00 = mix(n000, n100, u.x);
85
- let x10 = mix(n010, n110, u.x);
86
- let x01 = mix(n001, n101, u.x);
87
- let x11 = mix(n011, n111, u.x);
88
- return mix(mix(x00, x10, u.y), mix(x01, x11, u.y), u.z);
89
- }
90
57
  // World units a sample pair must span to count as a FULL contribution — the
91
58
  // original's REF_SEG_PX, converted out of pixels.
92
59
  //
@@ -114,9 +81,13 @@ struct TrailU {
114
81
  @group(0) @binding(0) var<storage, read> _rzCast: array<vec4f>;
115
82
  @group(0) @binding(1) var<uniform> tu: TrailU;
116
83
  @group(0) @binding(2) var<uniform> cam: CameraU;
117
- @group(0) @binding(3) var sceneDepth: texture_depth_multisampled_2d;
84
+ // Binding 3 is GONE, and it had to go. Ribbons draw inside the scene pass now,
85
+ // and the depth buffer is that pass's render attachment — binding it for
86
+ // sampling in the same pass is a usage conflict WebGPU rejects outright:
87
+ // "includes writable usage and another usage in the same synchronization
88
+ // scope". The hardware depth test replaced what it was read for.
118
89
 
119
- fn rzTime() -> f32 { return tu.time; }
90
+ ${RZ_LIGHT_STRUCT_WGSL}
120
91
  fn rzCameraPos() -> vec3f { return cam.camPos; }
121
92
  fn rzCameraRight() -> vec3f { return vec3f(cam.view[0][0], cam.view[1][0], cam.view[2][0]); }
122
93
  fn rzCameraUp() -> vec3f { return vec3f(cam.view[0][1], cam.view[1][1], cam.view[2][1]); }
@@ -127,7 +98,6 @@ fn rzProject(p: vec3f) -> vec3f {
127
98
  let w = max(clip.w, 1e-4);
128
99
  return vec3f(clip.xy / w * 0.5 + 0.5, clip.w);
129
100
  }
130
- fn rzViewportHeight() -> f32 { return cam.targetHeight; }
131
101
  fn rzCamPos() -> vec3f { return cam.camPos; }
132
102
  fn rzSubjectCount() -> i32 {
133
103
  var n = 0;
@@ -136,16 +106,6 @@ fn rzSubjectCount() -> i32 {
136
106
  }
137
107
  return n;
138
108
  }
139
- fn rzTrailCount(subject: i32, slot: i32) -> i32 {
140
- if (subject < 0 || subject >= RZ_SUBJECTS || slot < 0 || slot >= RZ_SLOTS) { return 0; }
141
- return i32(_rzCast[${cast.base} + (slot * RZ_SUBJECTS + subject) * 3 + 2].w);
142
- }
143
- /** Sample i of a path: xyz where it was, w how many seconds ago. i = 0 is now. */
144
- fn rzTrail(subject: i32, slot: i32, i: i32) -> vec4f {
145
- let n = rzTrailCount(subject, slot);
146
- if (i < 0 || i >= n) { return vec4f(0.0); }
147
- return _rzCast[${cast.trailBase} + (slot * RZ_SUBJECTS + subject) * RZ_SAMPLES + i];
148
- }
149
109
  /** Catmull-Rom through four samples — passes through p1 and p2. */
150
110
  fn rzSpline(p0: vec3f, p1: vec3f, p2: vec3f, p3: vec3f, t: f32) -> vec3f {
151
111
  let t2 = t * t;
@@ -210,18 +170,10 @@ fn rzTurnRadius(a: vec3f, b: vec3f, c: vec3f) -> f32 {
210
170
  if (la < 1e-5 || lb < 1e-5 || cr < 1e-7) { return 1e6; }
211
171
  return (la * lb * length(ca)) / (2.0 * cr);
212
172
  }
213
- fn rzFalloff(d: f32, r: f32) -> f32 {
214
- let x = clamp(d / max(r, 1e-6), 0.0, 1.0);
215
- let f = 1.0 - x;
216
- return f * f * f;
217
- }
218
- fn rzHash11(x: f32) -> f32 {
219
- var p = fract(x * 0.1031);
220
- p = p * (p + 33.33);
221
- return fract(p * (p + p));
222
- }
223
173
  ` +
224
174
  audioApi(0, 4) +
175
+ midiApi(0, 5) +
176
+ lyricsApi(0, 6) +
225
177
  "\n// ── user effect ──\n" +
226
178
  src.wgsl +
227
179
  /* wgsl */ `
@@ -244,10 +196,14 @@ fn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VSOut
244
196
  let seg = k / SUB;
245
197
  let sub = k % SUB;
246
198
  let subject = i32(rest % u32(RZ_SUBJECTS));
247
- let slot = i32(rest / u32(RZ_SUBJECTS));
199
+ // Instance index counts RIBBONS; the cast buffer is addressed by anchor slot.
200
+ // One ribbon per trailed anchor, so these are the same number only when every
201
+ // anchor is trailed — _rzRibbonSlot is what bridges them.
202
+ let ribbon = i32(rest / u32(RZ_SUBJECTS));
203
+ let slot = _rzRibbonSlot(ribbon);
248
204
 
249
205
  let n = rzTrailCount(subject, slot);
250
- if (slot >= RZ_SLOTS || seg + 1 >= n) {
206
+ if (ribbon >= RZ_SLOTS || seg + 1 >= n) {
251
207
  out.clip = vec4f(0.0, 0.0, -2.0, 1.0);
252
208
  out.uv = vec2f(0.0);
253
209
  out.age = 0.0;
@@ -354,22 +310,31 @@ fn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VSOut
354
310
  return out;
355
311
  }
356
312
 
313
+ struct TrailFSOut {
314
+ @location(0) color: vec4f,
315
+ // The scene's aux target: (bloom mask, coverage). Writing mask 1 is what
316
+ // makes a ribbon BLOOM — it is light, and the gate is what says so.
317
+ @location(1) aux: vec4f,
318
+ }
319
+
357
320
  @fragment
358
- fn fs(in: VSOut) -> @location(0) vec4f {
359
- // Manual depth test against the scene's own buffer — the layer has no depth
360
- // attachment, and a per-fragment compare is exactly what the fullscreen
361
- // version did (segZ <= depth). position.z IS this fragment's depth.
362
- let sceneD = textureLoad(sceneDepth, vec2i(in.clip.xy), 0);
363
- if (in.clip.z > sceneD) { discard; }
321
+ fn fs(in: VSOut) -> TrailFSOut {
322
+ // No manual depth test any more. This drew into its own attachment-less
323
+ // layer and compared position.z against the scene's depth texture by hand,
324
+ // with the direction baked per depth convention. Inside the scene pass the
325
+ // hardware does it, correctly and for free, and the reversed-Z trap that
326
+ // needed its own regression test goes with it.
364
327
  let c = trailShade(in.uv.x, in.uv.y, in.age, in.weight, i32(in.slot));
365
328
  if (c.a <= 0.0) { discard; }
366
- // STRAIGHT colour, into a layer that blends with MAX — which is the
367
- // original's core-takes-the-max rule turned into a blend mode. Parallel
368
- // strands of a circling hand meet as max, so they can never sum into the
369
- // doubled bright dashes that additive drew along the path. The layer then
370
- // composites over the frame AFTER tone mapping, where the fullscreen ribbon
371
- // always ran — so these colours reach the screen verbatim.
372
- return vec4f(c.rgb, c.a);
329
+ var o: TrailFSOut;
330
+ // STRAIGHT colour into an ADDITIVE target, which reverses the old MAX rule
331
+ // deliberately: max existed so parallel strands could not double into bright
332
+ // dashes on a layer composited after tone mapping. In HDR before bloom,
333
+ // overlapping light SHOULD sum — that is what neon does — and the tone
334
+ // mapper is what keeps the sum from clipping.
335
+ o.color = vec4f(c.rgb, c.a);
336
+ o.aux = vec4f(${src.bloom ? "1.0" : "0.0"}, 1.0, 0.0, c.a);
337
+ return o;
373
338
  }
374
339
  `);
375
340
  }