reze-engine 0.54.7 → 0.54.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/engine.d.ts +44 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +235 -1
- package/dist/shaders/passes/cast-distance.d.ts +52 -0
- package/dist/shaders/passes/cast-distance.d.ts.map +1 -0
- package/dist/shaders/passes/cast-distance.js +269 -0
- package/dist/shaders/passes/composite.d.ts.map +1 -1
- package/dist/shaders/passes/composite.js +5 -0
- package/package.json +1 -1
- package/src/engine.ts +258 -1
- package/src/shaders/passes/cast-distance.ts +280 -0
- package/src/shaders/passes/composite.ts +5 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { EFFECT_SUBJECTS } from "../cast-layout"
|
|
2
|
+
|
|
3
|
+
// How far is this pixel from the cast? — the primitive behind every silhouette
|
|
4
|
+
// look, computed once and read by anyone.
|
|
5
|
+
//
|
|
6
|
+
// AN EFFECT CANNOT ANSWER THIS ITSELF, and that is the whole reason this pass
|
|
7
|
+
// exists. Distance to the nearest drawn pixel is not a function of the pixel;
|
|
8
|
+
// it is a function of every pixel around it. A shader with one pass can only go
|
|
9
|
+
// looking, and searching a disc costs O(radius^2) per pixel — measured on the
|
|
10
|
+
// first sticker-outline effect, a 9 pixel border cost 64 id samples on every
|
|
11
|
+
// background pixel and a 16 pixel one cost 96 to 128. The look everybody wants
|
|
12
|
+
// next is a wide one, and that is the direction the cost runs away in.
|
|
13
|
+
//
|
|
14
|
+
// A JUMP FLOOD costs log2(resolution) passes and answers for ANY distance. It
|
|
15
|
+
// is the same 37 million reads at 1080p whether the effect wants a 4 pixel
|
|
16
|
+
// border or a 400 pixel aura, and it is shared: two effects reading it pay for
|
|
17
|
+
// it once.
|
|
18
|
+
//
|
|
19
|
+
// NO CAP ON THE DISTANCE. A partial flood — stopping early to save a pass —
|
|
20
|
+
// would silently limit how far an effect could reach, and the engine has no
|
|
21
|
+
// business deciding that. The author writes the falloff and pays for the width
|
|
22
|
+
// they asked for in their own arithmetic, not in ours.
|
|
23
|
+
//
|
|
24
|
+
// FULL RESOLUTION, after trying not to be.
|
|
25
|
+
//
|
|
26
|
+
// Half res was tried twice and rejected on sight both times, and the reason is
|
|
27
|
+
// worth recording because the arithmetic looked fine. A half-res distance field
|
|
28
|
+
// really does place the edge to within a pixel — measured against an exact
|
|
29
|
+
// transform, 0.22 px mean and 0.68 px at the 99th percentile. What that average
|
|
30
|
+
// hides is that the error is not noise: it VARIES along the silhouette, so the
|
|
31
|
+
// border breathes in and out by a fraction of a pixel as the edge turns, and an
|
|
32
|
+
// edge that wobbles reads as jagged no matter how smoothly it is feathered.
|
|
33
|
+
// Seeding at the sub-texel centroid rather than the texel centre halved it
|
|
34
|
+
// (0.83 to 0.28 px mean on a curve) and it was still visible.
|
|
35
|
+
//
|
|
36
|
+
// So the field is the size of the frame and the seeds are exact. What that costs
|
|
37
|
+
// at 1080p is about 224 million texture reads a frame, against 47 million at
|
|
38
|
+
// half — real, and the honest price of an edge that does not crawl. It is still
|
|
39
|
+
// the same cost whatever width an effect asks for, which is the whole point: the
|
|
40
|
+
// per-pixel search this replaced cost 64 reads a pixel for a 9 px border and
|
|
41
|
+
// four times that for an 18 px one.
|
|
42
|
+
|
|
43
|
+
/** Whether any of this effect's source reads the field, and so whether the
|
|
44
|
+
* engine should spend the passes building it. Nothing else turns it on. */
|
|
45
|
+
export function castDistanceUsed(wgsl: string): boolean {
|
|
46
|
+
return /\brzCastDistance\s*\(/.test(wgsl)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Seed and ping-pong target: the nearest seed's texel coordinate, or (-1,-1).
|
|
50
|
+
* 32-bit because these are coordinates — a half would quantise them at 2048
|
|
51
|
+
* and put the seam of that error straight through a 4K frame. */
|
|
52
|
+
export const CAST_SEED_FORMAT: GPUTextureFormat = "rg32float"
|
|
53
|
+
/** The resolved distance, in FIELD texels. Sampled bilinearly, so it is filtered
|
|
54
|
+
* rather than loaded, and r32float is not filterable everywhere — r16float is,
|
|
55
|
+
* and half a texel of precision on a distance is nothing. */
|
|
56
|
+
export const CAST_DIST_FORMAT: GPUTextureFormat = "r16float"
|
|
57
|
+
|
|
58
|
+
/** Per-pixel MSAA coverage of the cast, written beside the seeds and read by the
|
|
59
|
+
* resolve to place the edge inside the seed pixel. */
|
|
60
|
+
export const CAST_COVERAGE_FORMAT: GPUTextureFormat = "r8unorm"
|
|
61
|
+
|
|
62
|
+
/** The field is the size of the frame. See the note above for what half cost. */
|
|
63
|
+
export const CAST_FIELD_DIV = 1
|
|
64
|
+
|
|
65
|
+
const FULLSCREEN_VS = /* wgsl */ `
|
|
66
|
+
struct VSOut { @builtin(position) pos: vec4f };
|
|
67
|
+
|
|
68
|
+
@vertex
|
|
69
|
+
fn vs(@builtin(vertex_index) i: u32) -> VSOut {
|
|
70
|
+
// One oversized triangle rather than two: no seam down the diagonal, and the
|
|
71
|
+
// rasteriser does not have to think about a shared edge.
|
|
72
|
+
var p = array<vec2f, 3>(vec2f(-1.0, -3.0), vec2f(-1.0, 1.0), vec2f(3.0, 1.0));
|
|
73
|
+
var o: VSOut;
|
|
74
|
+
o.pos = vec4f(p[i], 0.0, 1.0);
|
|
75
|
+
return o;
|
|
76
|
+
}
|
|
77
|
+
`
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Pass 1 — plant a seed on every pixel the CAST drew.
|
|
81
|
+
*
|
|
82
|
+
* The ground, a stage and a media plane all write ids exactly as she does, so
|
|
83
|
+
* seeding "anything drawn" would grow a border around the floor: a rectangle
|
|
84
|
+
* round the frame rather than a sticker. Only the subject ids seed.
|
|
85
|
+
*/
|
|
86
|
+
export function buildCastSeedShader(samples: number): string {
|
|
87
|
+
return (
|
|
88
|
+
/* wgsl */ `
|
|
89
|
+
const RZ_SUBJECTS: i32 = ${EFFECT_SUBJECTS};
|
|
90
|
+
|
|
91
|
+
const RZ_ID_SAMPLES: i32 = ${samples};
|
|
92
|
+
|
|
93
|
+
@group(0) @binding(0) var _rzIdTex: texture_multisampled_2d<u32>;
|
|
94
|
+
@group(0) @binding(1) var<storage, read> _rzCast: array<vec4f>;
|
|
95
|
+
|
|
96
|
+
// The two cast accessors this pass needs, spelled out rather than pulled in.
|
|
97
|
+
// CAST_API brings subjects, trails, anchors and their aliases with it, and a
|
|
98
|
+
// seed pass wants none of that — it asks one question about one id.
|
|
99
|
+
fn rzSubjectCount() -> i32 {
|
|
100
|
+
var n = 0;
|
|
101
|
+
for (var i = 0; i < RZ_SUBJECTS; i++) {
|
|
102
|
+
if (_rzCast[i * 3 + 2].w > 0.0) { n = i + 1; }
|
|
103
|
+
}
|
|
104
|
+
return n;
|
|
105
|
+
}
|
|
106
|
+
fn rzSubjectId(i: i32) -> u32 {
|
|
107
|
+
if (i < 0 || i >= rzSubjectCount()) { return 0u; }
|
|
108
|
+
return u32(_rzCast[i * 3 + 1].w);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
${FULLSCREEN_VS}
|
|
112
|
+
|
|
113
|
+
struct SeedOut {
|
|
114
|
+
/** Where the nearest cast pixel is, for the flood to carry. */
|
|
115
|
+
@location(0) seed: vec2f,
|
|
116
|
+
/** How much of this pixel she covers, 0..1, for the resolve to place the edge
|
|
117
|
+
* inside it. */
|
|
118
|
+
@location(1) coverage: f32,
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
@fragment
|
|
122
|
+
fn fs(@builtin(position) pos: vec4f) -> SeedOut {
|
|
123
|
+
// COVERAGE, NOT A YES OR NO — this is the whole reason the border used to sit
|
|
124
|
+
// badly against her.
|
|
125
|
+
//
|
|
126
|
+
// The scene draws at 4x MSAA and the frame you see is resolved, so her edge is
|
|
127
|
+
// smooth. The id attachment is deliberately NOT resolved (an averaged id
|
|
128
|
+
// belongs to nothing), so reading sample 0 gives a hard, binary, aliased
|
|
129
|
+
// silhouette — a different edge from the one she is drawn with. A border grown
|
|
130
|
+
// off that can never sit flush against her: it traces the staircase while she
|
|
131
|
+
// has none.
|
|
132
|
+
//
|
|
133
|
+
// Counting the samples recovers what MSAA already knew. Four samples give five
|
|
134
|
+
// levels of coverage, the resolve turns that into a sub-pixel zero crossing,
|
|
135
|
+
// and the border meets her where she actually ends.
|
|
136
|
+
let dim = vec2<i32>(textureDimensions(_rzIdTex));
|
|
137
|
+
let p = clamp(vec2<i32>(pos.xy), vec2<i32>(0), dim - vec2<i32>(1));
|
|
138
|
+
let n = rzSubjectCount();
|
|
139
|
+
var covered = 0.0;
|
|
140
|
+
for (var sample = 0; sample < RZ_ID_SAMPLES; sample++) {
|
|
141
|
+
let o = textureLoad(_rzIdTex, p, sample).y;
|
|
142
|
+
if (o == 0u) { continue; }
|
|
143
|
+
for (var i = 0; i < n; i++) {
|
|
144
|
+
if (o == rzSubjectId(i)) { covered += 1.0; break; }
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
var out: SeedOut;
|
|
148
|
+
out.coverage = covered / f32(RZ_ID_SAMPLES);
|
|
149
|
+
// Anything she touches at all seeds, including a pixel she barely clips: a
|
|
150
|
+
// sliver is where the sub-pixel edge lives, and dropping it would put the
|
|
151
|
+
// staircase straight back.
|
|
152
|
+
// -1 is the empty marker, and every step below tests for it before believing
|
|
153
|
+
// a candidate.
|
|
154
|
+
out.seed = select(vec2f(-1.0, -1.0), pos.xy, covered > 0.0);
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
`
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Pass 2 — one jump-flood step, run once per halving of the stride.
|
|
163
|
+
*
|
|
164
|
+
* Each texel asks its eight neighbours at the current stride what seed THEY
|
|
165
|
+
* know about, and keeps whichever is nearest to itself. Starting at half the
|
|
166
|
+
* field's width and halving to one, a seed reaches every texel that is nearer
|
|
167
|
+
* to it than to any other — which is the definition of the field.
|
|
168
|
+
*/
|
|
169
|
+
export function buildCastStepShader(): string {
|
|
170
|
+
return /* wgsl */ `
|
|
171
|
+
@group(0) @binding(0) var _rzPrev: texture_2d<f32>;
|
|
172
|
+
@group(0) @binding(1) var<uniform> _rzStep: vec4f; // (stride, _, _, _)
|
|
173
|
+
|
|
174
|
+
${FULLSCREEN_VS}
|
|
175
|
+
|
|
176
|
+
@fragment
|
|
177
|
+
fn fs(@builtin(position) pos: vec4f) -> @location(0) vec2f {
|
|
178
|
+
let me = pos.xy;
|
|
179
|
+
let dim = vec2<i32>(textureDimensions(_rzPrev));
|
|
180
|
+
let stride = i32(_rzStep.x);
|
|
181
|
+
var best = textureLoad(_rzPrev, vec2<i32>(me), 0).xy;
|
|
182
|
+
// Squared throughout: the comparison is the only thing that matters and a
|
|
183
|
+
// square root per candidate is nine of them per texel per pass.
|
|
184
|
+
var bestD = select(1.0e30, dot(best - me, best - me), best.x >= 0.0);
|
|
185
|
+
for (var dy = -1; dy <= 1; dy++) {
|
|
186
|
+
for (var dx = -1; dx <= 1; dx++) {
|
|
187
|
+
let p = clamp(vec2<i32>(me) + vec2<i32>(dx, dy) * stride, vec2<i32>(0), dim - vec2<i32>(1));
|
|
188
|
+
let c = textureLoad(_rzPrev, p, 0).xy;
|
|
189
|
+
if (c.x < 0.0) { continue; }
|
|
190
|
+
let d = dot(c - me, c - me);
|
|
191
|
+
if (d < bestD) { bestD = d; best = c; }
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return best;
|
|
195
|
+
}
|
|
196
|
+
`
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Pass 3 — turn the seed coordinates into a distance.
|
|
201
|
+
*
|
|
202
|
+
* Its own pass because the READ is bilinear. Interpolating coordinates across
|
|
203
|
+
* the boundary between two seeds averages two unrelated points and lands the
|
|
204
|
+
* result on neither; interpolating the DISTANCE is the smooth, honest thing,
|
|
205
|
+
* and it is what makes a half-res field cut a full-res edge.
|
|
206
|
+
*/
|
|
207
|
+
export function buildCastResolveShader(): string {
|
|
208
|
+
return /* wgsl */ `
|
|
209
|
+
@group(0) @binding(0) var _rzSeed: texture_2d<f32>;
|
|
210
|
+
@group(0) @binding(1) var _rzCoverage: texture_2d<f32>;
|
|
211
|
+
|
|
212
|
+
${FULLSCREEN_VS}
|
|
213
|
+
|
|
214
|
+
@fragment
|
|
215
|
+
fn fs(@builtin(position) pos: vec4f) -> @location(0) f32 {
|
|
216
|
+
let s = textureLoad(_rzSeed, vec2<i32>(pos.xy), 0).xy;
|
|
217
|
+
// No cast on screen at all: everything is unreachably far from her, which is
|
|
218
|
+
// the answer that makes an effect draw nothing rather than everything.
|
|
219
|
+
if (s.x < 0.0) { return 1.0e30; }
|
|
220
|
+
let c = textureLoad(_rzCoverage, vec2<i32>(s), 0).x;
|
|
221
|
+
// SIGNED, WITH THE ZERO CROSSING INSIDE THE SEED PIXEL.
|
|
222
|
+
//
|
|
223
|
+
// A fully covered seed means her true edge runs about half a pixel outside its
|
|
224
|
+
// centre, so the distance to the EDGE is half a pixel less than the distance to
|
|
225
|
+
// the centre. A half-covered seed has the edge through its centre and wants no
|
|
226
|
+
// correction. A barely covered one has the edge nearly a half-pixel further in.
|
|
227
|
+
// All three are (c - 0.5).
|
|
228
|
+
//
|
|
229
|
+
// It also makes the field NEGATIVE inside her, and that is what lets an effect
|
|
230
|
+
// fade where it meets her rather than stopping dead on a texel boundary — the
|
|
231
|
+
// difference between a border that is attached and one that is merely nearby.
|
|
232
|
+
return length(s - pos.xy) - (c - 0.5);
|
|
233
|
+
}
|
|
234
|
+
`
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* `rzCastDistance(uv)`, for the effects that read it.
|
|
239
|
+
*
|
|
240
|
+
* ALWAYS COMPILED IN, even when the pass is not running — it then samples a 1x1
|
|
241
|
+
* holding a very large number, so an effect keyed on it draws nothing instead of
|
|
242
|
+
* failing to compile. Same rule the grid's accessor follows, and the id
|
|
243
|
+
* accessors before it: an author should never have to guard a name.
|
|
244
|
+
*/
|
|
245
|
+
export function castDistanceApi(group: number, tex: number, samp: number, scale: number): string {
|
|
246
|
+
return /* wgsl */ `
|
|
247
|
+
@group(${group}) @binding(${tex}) var _rzCastDistTex: texture_2d<f32>;
|
|
248
|
+
@group(${group}) @binding(${samp}) var _rzCastDistSamp: sampler;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Distance from this point to the nearest pixel the CAST drew, in SCREEN pixels.
|
|
252
|
+
*
|
|
253
|
+
* ZERO ON HER, positive outside, and the crossing is sub-pixel — it comes from
|
|
254
|
+
* MSAA coverage, so it lands on the same edge she is drawn with rather than on
|
|
255
|
+
* the staircase a single sample sees. Fade an effect across zero and it attaches
|
|
256
|
+
* to her cleanly.
|
|
257
|
+
*
|
|
258
|
+
* IT IS NOT AN INTERIOR DEPTH. Every pixel she covers is a seed, so the nearest
|
|
259
|
+
* seed to a pixel deep inside her is itself: the answer there is 0, not "far
|
|
260
|
+
* in". What the coverage buys is half a pixel of sub-pixel placement at the
|
|
261
|
+
* boundary, which is exactly enough to anti-alias against her and no more — so
|
|
262
|
+
* the value inside her runs to -0.5 and no further. An effect that wants to
|
|
263
|
+
* reach INTO her wants a second flood seeded on the background, which this pass
|
|
264
|
+
* does not build. Guard on d >= 0 and fade across it.
|
|
265
|
+
*
|
|
266
|
+
* There is no ceiling. A pixel on the far side of the frame gets the real
|
|
267
|
+
* number. The ground, a stage and a media plane are not the cast and do not
|
|
268
|
+
* seed it.
|
|
269
|
+
*
|
|
270
|
+
* Screen pixels whatever the field's own resolution is, so an author writes the
|
|
271
|
+
* width they mean and never has to know how this is built. uv is the effect's
|
|
272
|
+
* own convention, origin bottom-left; the flip to texture rows happens here.
|
|
273
|
+
*/
|
|
274
|
+
fn rzCastDistance(uv: vec2f) -> f32 {
|
|
275
|
+
let flipped = vec2f(clamp(uv.x, 0.0, 1.0), 1.0 - clamp(uv.y, 0.0, 1.0));
|
|
276
|
+
let d = textureSampleLevel(_rzCastDistTex, _rzCastDistSamp, flipped, 0.0).x;
|
|
277
|
+
return d * ${scale > 0 ? scale : 1}.0;
|
|
278
|
+
}
|
|
279
|
+
`
|
|
280
|
+
}
|
|
@@ -5,6 +5,7 @@ import { clockApi, EFFECT_MATH_API, PARTICLE_STRUCT_WGSL, trailSlotsApi, viewpor
|
|
|
5
5
|
import { EFFECT_ANCHORS, EFFECT_SUBJECTS, EFFECT_TRAIL_BASE, EFFECT_TRAIL_SAMPLES } from "../cast-layout"
|
|
6
6
|
import { audioApi } from "../audio-api"
|
|
7
7
|
import { idApi } from "../id-api"
|
|
8
|
+
import { castDistanceApi, CAST_FIELD_DIV } from "./cast-distance"
|
|
8
9
|
import { lyricsApi, lyricsTextApi } from "../lyrics-api"
|
|
9
10
|
import { midiApi } from "../midi-api"
|
|
10
11
|
import { gridReadApi } from "./grid"
|
|
@@ -669,6 +670,10 @@ export function buildFieldShader(effect: CompositeEffectSource): string {
|
|
|
669
670
|
viewportApi("viewU[6].w") +
|
|
670
671
|
trailSlotsApi(effect.trailCount) +
|
|
671
672
|
idApi(effect.ids === true, 0, 23) +
|
|
673
|
+
// Distance to the cast, in SCREEN pixels: the field is half-res, so a field
|
|
674
|
+
// texel is CAST_FIELD_DIV of them and the accessor scales on the way out.
|
|
675
|
+
// An author writes the width they mean and never learns how it is built.
|
|
676
|
+
castDistanceApi(0, 26, 18, CAST_FIELD_DIV) +
|
|
672
677
|
"\n// ── user effect (setEffect) ──\n" +
|
|
673
678
|
effect.paramsDecl +
|
|
674
679
|
"\n" +
|