rlx_core/render/scenes/particles/resources.rs
1//! The attractor's GPU resources: the three uniform blocks, the three resource
2//! holders, and their bind-group helpers (Plan 0061 Phase 6).
3//!
4//! This is the `wgpu` half of `particles/` — buffers, layouts, pipelines and the
5//! bind groups that wire them together. The scene that drives them, its `Scene`
6//! impl and the `encode_*` passes stay in `mod.rs`; the ODE math it draws is in
7//! [`family`], which imports no `wgpu` at all.
8
9// Hot-path panic-denial pragma (Plan 0002 Phase 2; render/ is scanned by the
10// hygiene guard).
11#![deny(
12 clippy::unwrap_used,
13 clippy::expect_used,
14 clippy::indexing_slicing,
15 clippy::panic,
16 clippy::unreachable
17)]
18
19// A continuation of one module split across four files, so it needs the names
20// `particles/mod.rs` has in scope.
21use super::*;
22
23/// Compute step uniform (per frame): the attractor coefficients, the fixed
24/// sub-step `dt`, the selected family, and the active particle count.
25///
26/// The same layout drives the one-shot **jitter** dispatch (ADR-0066), where
27/// `family` is [`JITTER_MODE`], `coeffs.xyz` is the kick's half-extent and `salt`
28/// is the reseed counter. One struct and one pipeline rather than a second of
29/// each: the jitter reads and writes the same storage buffer through the same
30/// bind-group layout, so only the uniform's contents differ.
31///
32/// **192 bytes**, for every family including the four that ignore the trailing
33/// fields — negligible in
34/// bandwidth, and noted because it is a struct four families share. ADR-0075
35/// predicted 144 for the Plan 0062 shape; the extra 16 is the alignment padding
36/// [`step_index`](Self::step_index) forces, because the scalar block ahead of the
37/// `vec4` table has to round up to a multiple of 16 and it was already exactly
38/// full. **The bind-group layout gains no binding at either step**, so the
39/// collision surface ADR-0058 reasons about does not change shape.
40#[repr(C)]
41#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
42pub(super) struct StepUniform {
43 pub(super) coeffs: [f32; 4],
44 pub(super) dt: f32,
45 pub(super) family: u32,
46 pub(super) count: u32,
47 /// Which reseed this is, for the jitter dispatch. Zero (and unread) on a
48 /// stepping dispatch — it was the struct's explicit padding word.
49 pub(super) salt: u32,
50 /// The monotonic fixed-step counter the IFS draws its map choice from
51 /// (ADR-0075). Zero (and unread) on every other family, and on the jitter
52 /// dispatch — which keeps its own `salt` rather than sharing this.
53 pub(super) step_index: u32,
54 /// The reciprocal of the fixed-point set's floored diameter
55 /// ([`ifs::skeleton_scale`], ADR-0088) — the scale the step shader's IFS arm
56 /// normalises a raw nearest-point distance by. Zero (and unread) on the four
57 /// map families and on the jitter dispatch, exactly as the affine table is.
58 ///
59 /// **It costs no bytes.** It takes the first of the three explicit padding
60 /// words the `vec4` table's alignment had already paid for, so the struct
61 /// stays 192 and the bind-group layout gains no binding.
62 pub(super) root_recip: f32,
63 /// The rest of that padding. Explicit, because the `vec4` table below is
64 /// 16-byte aligned and the scalars above are five words. `bytemuck::Pod`
65 /// requires no implicit padding, so these words must be named.
66 pub(super) _pad: [u32; 2],
67 /// The IFS's resolved affine table — [`IfsPacked`] laid out flat. Zeroed for
68 /// the four map families, which never read it.
69 pub(super) linear: [[f32; 4]; ifs::MAPS],
70 pub(super) translate: [[f32; 4]; 2],
71 pub(super) cumulative_p: [f32; 4],
72 /// The four respawn targets (ADR-0087), two `(x, y)` per row exactly as
73 /// `translate` is packed.
74 pub(super) fixed: [[f32; 4]; 2],
75}
76
77impl StepUniform {
78 /// The IFS half of the uniform, as the four map families and the jitter
79 /// dispatch write it: all zeros, and unread.
80 pub(super) const NO_IFS: IfsPacked = IfsPacked::ZERO;
81
82 /// Assemble one slot. The IFS payload is spread across three fields, so a
83 /// constructor is what keeps the three call sites from disagreeing about it.
84 pub(super) fn new(
85 coeffs: [f32; 4],
86 family: u32,
87 count: u32,
88 salt: u32,
89 step_index: u32,
90 packed: IfsPacked,
91 ) -> Self {
92 Self {
93 coeffs,
94 dt: FIXED_STEP,
95 family,
96 count,
97 salt,
98 step_index,
99 root_recip: packed.root_recip,
100 _pad: [0; 2],
101 linear: packed.linear,
102 translate: packed.translate,
103 cumulative_p: packed.cumulative_p,
104 fixed: packed.fixed,
105 }
106 }
107}
108
109/// Draw uniform (per frame). `v`: x aspect, y point half-size, z hue offset, w
110/// spin. `w`: x world scale, y projection dim (2 or 3), z z-centre (3D),
111/// w [`deposit_scale`] (ADR-0065) times [`brightness_factor`] (ADR-0080).
112/// `u`: x hue_spread, y hue_center, z palette_mix, w saturation (ADR-0021).
113/// `x`: x zoom, yz pan (view transform, ADR-0018), w the streak flag
114/// (ADR-0069) — non-zero exactly when [`AttractorFamily::is_continuous`].
115/// `bh`/`bv`: the 3D projection basis's two axis selectors (ADR-0068) — the axis
116/// the spin rotates `x` against, and the vertical. Read only on the 3D branch.
117/// `d`: x `perspective`, y `depth_fade`, z `depth_hue`, w the family's
118/// **inverse** depth half-extent (ADR-0076) — `0` for a 2D family, which is what
119/// makes every depth cue the identity there without a shader branch.
120/// `ctr`: xyz the world centre subtracted before projection (Plan 0062), w unused.
121/// `ch`: the two per-particle colour channels at two routes each — x `map_tint`,
122/// y `map_hue` (ADR-0087), z `root_tint`, w `root_hue` (ADR-0088). All four
123/// default to `0`, which is the arithmetic identity on every route.
124///
125/// **The row swapped rather than grew** at Plan 0074 Phase 3: `age_tint` and
126/// `age_hue` held z and w until the age channel was retired. The two halves are
127/// *not* the same shape — `map_*` is centred, `root_*` is anchored at `0` — for
128/// the reason in ADR-0088's Anchoring section.
129///
130/// `em`: the emergence ramp (ADR-0087) — x the per-step brightness increment, y
131/// the floor. `(1/emergence, 0)` on the IFS and `(0, 1)` everywhere else,
132/// because every other family's `age` is identically zero and a bare `age·rate`
133/// would black them out rather than leave them alone. **z and w are free** since
134/// the retirement: z carried `1/churn_max_lifetime()`, which only the age colour
135/// channel read.
136#[repr(C)]
137#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
138pub(super) struct DrawUniform {
139 pub(super) v: [f32; 4],
140 pub(super) w: [f32; 4],
141 pub(super) u: [f32; 4],
142 pub(super) x: [f32; 4],
143 pub(super) bh: [f32; 4],
144 pub(super) bv: [f32; 4],
145 pub(super) d: [f32; 4],
146 pub(super) ctr: [f32; 4],
147 pub(super) ch: [f32; 4],
148 pub(super) em: [f32; 4],
149}
150
151/// Decay uniform (per frame): x is the per-frame trail retention factor.
152#[repr(C)]
153#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
154pub(super) struct DecayUniform {
155 pub(super) k: [f32; 4],
156 /// The ADR-0048 feedback transform, exactly as
157 /// [`feedback::Transform::pack`](crate::render::feedback::Transform::pack)
158 /// returns it. Read by the decay pass; the present pass declares only `k` over
159 /// the same buffer, which is legal — a uniform binding may be wider than the
160 /// struct a shader lays over it — and deliberate, because the present's
161 /// bind-group layout *shape* is what the WARP adapter is sensitive to.
162 pub(super) xf: [f32; 4],
163 pub(super) tr: [f32; 4],
164 pub(super) wp: [f32; 4],
165}
166
167/// The GPU-side state, built lazily on first render (see the module docs), split
168/// along the axis that actually varies (Plan 0029 Phase 1): everything in
169/// [`PipelineResources`] is built once and survives every size change; only
170/// [`FieldResources`] is rebuilt when the accumulation grid changes.
171pub(super) struct Resources {
172 pub(super) pipelines: PipelineResources,
173 pub(super) grid: FieldResources,
174}
175
176/// The grid-**independent** GPU state: the four shader modules, every pipeline,
177/// the particle storage buffer, the uniform buffers, and the LUT textures. None
178/// of it references the accumulation field, so a size change must not touch it —
179/// recompiling four WGSL modules and rebuilding four pipelines inside `render` is
180/// a multi-hundred-millisecond stall, and the standalone forwards every
181/// `WindowEvent::Resized`, so a live drag paid it per frame (Plan 0029 Phase 1).
182pub(super) struct PipelineResources {
183 pub(super) compute_pipeline: wgpu::ComputePipeline,
184 pub(super) draw_pipeline: wgpu::RenderPipeline,
185 pub(super) decay_pipeline: wgpu::RenderPipeline,
186 pub(super) present_pipeline: wgpu::RenderPipeline,
187 pub(super) particles: wgpu::Buffer,
188 pub(super) step_uniform: wgpu::Buffer,
189 /// Byte stride between two slots of `step_uniform`, rounded up to the
190 /// adapter's dynamic-offset alignment.
191 ///
192 /// Separate slots rather than one written repeatedly, because a frame encodes
193 /// `pending_steps` step dispatches against one binding: folding the jitter into
194 /// the step slot would apply it once per sub-step, making the disturbance a
195 /// function of the frame's timing and breaking determinism. [`STEP_SLOTS`] has
196 /// the same argument for the sub-steps themselves.
197 pub(super) step_stride: u32,
198 pub(super) draw_uniform: wgpu::Buffer,
199 pub(super) decay_uniform: wgpu::Buffer,
200 pub(super) compute_bg: wgpu::BindGroup,
201 pub(super) draw_bg: wgpu::BindGroup,
202 /// The shared gradient LUT pair (A/B) the draw vertex shader samples +
203 /// crossfades (ADR-0021); uploaded from the scene's baked palette on the first
204 /// frame after a build and on a preset switch. It lives here rather than in
205 /// [`FieldResources`] because it outlives a grid change, so a resize does not
206 /// re-upload the palette.
207 pub(super) luts: palette::LutPair,
208 /// Kept so a grid change can rebuild [`FieldResources`]' four bind groups
209 /// without recreating a layout, a sampler, or any pipeline.
210 pub(super) decay_layout: wgpu::BindGroupLayout,
211 pub(super) present_layout: wgpu::BindGroupLayout,
212 pub(super) field_sampler: wgpu::Sampler,
213 /// How many particles the buffer above holds — the active tier's **ceiling**
214 /// ([`attractor_particles_live_ceiling`](crate::render::TierConfig::attractor_particles_live_ceiling),
215 /// or the offline one on a headless render path), fixed for the life of these
216 /// resources.
217 ///
218 /// The dispatch, the instance draw and the step uniform all take the
219 /// **active** count instead — `round(budget * density)` (ADR-0069) over a
220 /// budget that is itself a density against the render target (ADR-0140) — so
221 /// this is two clamps above what any frame actually draws. It survives as the
222 /// allocation bound: the draw clamps its instance range to it, so neither a
223 /// preset's `density` nor a resize can fetch a vertex past the end of the
224 /// buffer.
225 pub(super) count: u32,
226}
227
228/// The grid-**dependent** GPU state: the accumulation field and the four bind
229/// groups that reference its two texture views. The only block a size change
230/// rebuilds — a texture pair plus four bind groups, none of which compiles a
231/// shader (Plan 0029 Phase 1).
232pub(super) struct FieldResources {
233 /// Two-texture accumulation the trails ping-pong between (ADR-0012 reuse).
234 pub(super) field: PingPongField,
235 /// Decay/present bind groups reading texture A / texture B — selected by the
236 /// field's read side each frame so nothing is rebuilt on the hot path.
237 pub(super) decay_bg_a: wgpu::BindGroup,
238 pub(super) decay_bg_b: wgpu::BindGroup,
239 pub(super) present_bg_a: wgpu::BindGroup,
240 pub(super) present_bg_b: wgpu::BindGroup,
241 /// The accumulation grid this block was built for; `render` compares the
242 /// requested grid against it and rebuilds only this block on a difference.
243 pub(super) trail_w: u32,
244 pub(super) trail_h: u32,
245}
246
247impl Resources {
248 pub(super) fn build(
249 device: &wgpu::Device,
250 surface_format: wgpu::TextureFormat,
251 trail_w: u32,
252 trail_h: u32,
253 count: u32,
254 ) -> Self {
255 let pipelines = PipelineResources::build(device, surface_format, count);
256 let grid = FieldResources::build(device, &pipelines, trail_w, trail_h);
257 Self { pipelines, grid }
258 }
259
260 /// Re-allocate the accumulation field at a new grid, reusing every pipeline,
261 /// buffer, and texture that does not depend on it. The rebuilt field is
262 /// undefined, so the caller re-flags the clear (and the seed upload, which
263 /// keeps a capture reproducible from the same starting scatter).
264 pub(super) fn rebuild_grid(&mut self, device: &wgpu::Device, trail_w: u32, trail_h: u32) {
265 self.grid = FieldResources::build(device, &self.pipelines, trail_w, trail_h);
266 }
267}
268
269/// The draw pass's instance attributes, with **explicit byte offsets into
270/// [`Particle`]**.
271///
272/// **Spelled out rather than built by `vertex_attr_array!`, and that is the
273/// whole point of this constant.** That macro lays its attributes out
274/// *consecutively* — which was correct while the struct was `pos`, `seed`,
275/// `prev` and one trailing pad, and stopped being correct the moment ADR-0087
276/// put `age` and `map` past that pad. A fourth macro entry would have fetched
277/// the padding word at offset 28 and fed the draw someone else's bytes, silently
278/// and with no compile error. `the_particle_layout_carries_three_channels`
279/// measures these offsets against the struct so the two cannot drift.
280pub(super) const PARTICLE_ATTRIBUTES: &[wgpu::VertexAttribute] = &[
281 wgpu::VertexAttribute {
282 format: wgpu::VertexFormat::Float32x3,
283 offset: 0,
284 shader_location: 0, // pos (z = 0 for 2D families)
285 },
286 wgpu::VertexAttribute {
287 format: wgpu::VertexFormat::Float32,
288 offset: 12,
289 shader_location: 1, // seed
290 },
291 wgpu::VertexAttribute {
292 format: wgpu::VertexFormat::Float32x3,
293 offset: 16,
294 shader_location: 2, // prev (ADR-0069)
295 },
296 wgpu::VertexAttribute {
297 format: wgpu::VertexFormat::Float32,
298 offset: 36,
299 shader_location: 3, // map (ADR-0087) — 36, NOT 28, which is `_pad`
300 },
301 wgpu::VertexAttribute {
302 format: wgpu::VertexFormat::Float32,
303 offset: 32,
304 shader_location: 4, // age (ADR-0087)
305 },
306 wgpu::VertexAttribute {
307 format: wgpu::VertexFormat::Float32,
308 offset: 40,
309 shader_location: 5, // root (ADR-0088) — 40, the first spare word
310 },
311];
312
313impl PipelineResources {
314 pub(super) fn build(
315 device: &wgpu::Device,
316 surface_format: wgpu::TextureFormat,
317 count: u32,
318 ) -> Self {
319 // The shared bit-mixer, concatenated in — the same WGSL the tonemap's
320 // dither compiles (Plan 0082 Phase 1), so a particle's reseed kick and a
321 // display-write LSB cannot drift apart on what the hash is.
322 let step_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
323 label: Some("attractor-step-shader"),
324 source: wgpu::ShaderSource::Wgsl(format!("{}{STEP_SHADER}", gpu::HASH_WGSL).into()),
325 });
326 let draw_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
327 label: Some("attractor-draw-shader"),
328 source: wgpu::ShaderSource::Wgsl(DRAW_SHADER.into()),
329 });
330 // ADR-0048's transform, concatenated in: the same WGSL the engine trails
331 // stage compiles, so the two accumulation sinks cannot drift apart on what
332 // `fb_rotate` means.
333 let decay_shader = gpu::fullscreen_shader(
334 device,
335 "attractor-decay-shader",
336 gpu::FULLSCREEN_VS_UV_FLIPPED,
337 &format!("{}{DECAY_SHADER}", crate::render::feedback::TRANSFORM_WGSL),
338 );
339 let present_shader = gpu::fullscreen_shader(
340 device,
341 "attractor-present-shader",
342 gpu::FULLSCREEN_VS_UV_FLIPPED,
343 PRESENT_SHADER,
344 );
345
346 // Particle storage buffer: written by the compute step (STORAGE), read by
347 // the draw pass as an instance vertex buffer (VERTEX), seeded once from
348 // the CPU (COPY_DST). One buffer, two roles — no CPU round-trip.
349 //
350 // `COPY_SRC` is there for [`read_particles`], the reseed test's readback
351 // (Plan 0057 Phase 3). Carried unconditionally rather than behind
352 // `cfg(test)` so the test exercises the buffer the app actually allocates;
353 // a usage flag costs nothing that is not used, and a test running against a
354 // differently-configured resource is a test of something else.
355 let particles = device.create_buffer(&wgpu::BufferDescriptor {
356 label: Some("attractor-particles"),
357 size: (count as usize * std::mem::size_of::<Particle>()) as u64,
358 usage: wgpu::BufferUsages::STORAGE
359 | wgpu::BufferUsages::VERTEX
360 | wgpu::BufferUsages::COPY_DST
361 | wgpu::BufferUsages::COPY_SRC,
362 mapped_at_creation: false,
363 });
364 // [`STEP_SLOTS`] slots in ONE buffer, selected per dispatch by a dynamic
365 // offset.
366 //
367 // **Not two buffers behind two bind groups**, which is what this was first
368 // written as and which does not survive the software adapter: a second bind
369 // group sharing a live pipeline's layout gets aliased on WARP, so the step
370 // dispatch read the *jitter* slot — all zeros, so `count = 0`, so every
371 // invocation returned and the cloud never moved. It rendered a plausible
372 // static box, moved the golden baseline, and dropped three presets to
373 // ~0.000 in `animation`. One layout and one bind group has no aliasing
374 // surface to get wrong.
375 let step_stride = uniform_stride(device);
376 let step_uniform = gpu::uniform_buffer(
377 device,
378 "attractor-step-uniform",
379 (step_stride * STEP_SLOTS) as usize,
380 );
381 let draw_uniform =
382 gpu::uniform_buffer(device, "attractor-draw-uniform", size_of::<DrawUniform>());
383 let decay_uniform =
384 gpu::uniform_buffer(device, "attractor-decay-uniform", size_of::<DecayUniform>());
385
386 // --- compute: read_write storage + step uniform ---
387 let compute_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
388 label: Some("attractor-compute-layout"),
389 entries: &[
390 storage_entry(0),
391 wgpu::BindGroupLayoutEntry {
392 binding: 1,
393 visibility: wgpu::ShaderStages::COMPUTE,
394 ty: wgpu::BindingType::Buffer {
395 ty: wgpu::BufferBindingType::Uniform,
396 // The sub-step slots and the jitter slot, one dispatch each.
397 has_dynamic_offset: true,
398 min_binding_size: wgpu::BufferSize::new(size_of::<StepUniform>() as u64),
399 },
400 count: None,
401 },
402 ],
403 });
404 let compute_bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
405 label: Some("attractor-compute-bg"),
406 layout: &compute_layout,
407 entries: &[
408 wgpu::BindGroupEntry {
409 binding: 0,
410 resource: particles.as_entire_binding(),
411 },
412 wgpu::BindGroupEntry {
413 binding: 1,
414 // A window the size of one `StepUniform`, not the whole
415 // buffer: the dynamic offset slides it between the two slots.
416 resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
417 buffer: &step_uniform,
418 offset: 0,
419 size: wgpu::BufferSize::new(size_of::<StepUniform>() as u64),
420 }),
421 },
422 ],
423 });
424 let compute_pipeline_layout =
425 device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
426 label: Some("attractor-compute-pipeline-layout"),
427 bind_group_layouts: &[Some(&compute_layout)],
428 immediate_size: 0,
429 });
430 let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
431 label: Some("attractor-compute-pipeline"),
432 layout: Some(&compute_pipeline_layout),
433 module: &step_shader,
434 entry_point: Some("main"),
435 compilation_options: Default::default(),
436 cache: None,
437 });
438
439 // Shared gradient LUTs (ADR-0021): two 256×1 textures (A/B) + a repeat
440 // sampler, bound to the draw pass and sampled per-particle in the vertex
441 // shader (so VERTEX visibility).
442 let luts = palette::LutPair::new(device, "attractor");
443
444 // --- draw: the particle buffer as an instance vertex buffer, additively
445 // into the trail field (float target so the accumulation has headroom) ---
446 let lut_vertex_texture = |binding: u32| wgpu::BindGroupLayoutEntry {
447 binding,
448 visibility: wgpu::ShaderStages::VERTEX,
449 ty: wgpu::BindingType::Texture {
450 sample_type: wgpu::TextureSampleType::Float { filterable: true },
451 view_dimension: wgpu::TextureViewDimension::D2,
452 multisampled: false,
453 },
454 count: None,
455 };
456 let draw_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
457 label: Some("attractor-draw-layout"),
458 entries: &[
459 gpu::uniform(0, wgpu::ShaderStages::VERTEX),
460 lut_vertex_texture(1),
461 lut_vertex_texture(2),
462 wgpu::BindGroupLayoutEntry {
463 binding: 3,
464 visibility: wgpu::ShaderStages::VERTEX,
465 ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
466 count: None,
467 },
468 ],
469 });
470 let draw_bg = device.create_bind_group(&wgpu::BindGroupDescriptor {
471 label: Some("attractor-draw-bg"),
472 layout: &draw_layout,
473 entries: &{
474 let [lut_a, lut_b, lut_sampler] = luts.bind_entries(1, 2, 3);
475 [
476 wgpu::BindGroupEntry {
477 binding: 0,
478 resource: draw_uniform.as_entire_binding(),
479 },
480 lut_a,
481 lut_b,
482 lut_sampler,
483 ]
484 },
485 });
486 let draw_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
487 label: Some("attractor-draw-pipeline-layout"),
488 bind_group_layouts: &[Some(&draw_layout)],
489 immediate_size: 0,
490 });
491 let draw_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
492 label: Some("attractor-draw-pipeline"),
493 layout: Some(&draw_pipeline_layout),
494 vertex: wgpu::VertexState {
495 module: &draw_shader,
496 entry_point: Some("vs_main"),
497 compilation_options: Default::default(),
498 buffers: &[Some(wgpu::VertexBufferLayout {
499 array_stride: std::mem::size_of::<Particle>() as u64,
500 step_mode: wgpu::VertexStepMode::Instance,
501 attributes: PARTICLE_ATTRIBUTES,
502 })],
503 },
504 fragment: Some(wgpu::FragmentState {
505 module: &draw_shader,
506 entry_point: Some("fs_main"),
507 compilation_options: Default::default(),
508 targets: &[Some(wgpu::ColorTargetState {
509 format: PingPongField::FORMAT,
510 // Additive: overlapping points bloom brighter (the dense look).
511 blend: Some(wgpu::BlendState {
512 color: wgpu::BlendComponent {
513 src_factor: wgpu::BlendFactor::One,
514 dst_factor: wgpu::BlendFactor::One,
515 operation: wgpu::BlendOperation::Add,
516 },
517 alpha: wgpu::BlendComponent::OVER,
518 }),
519 write_mask: wgpu::ColorWrites::ALL,
520 })],
521 }),
522 primitive: wgpu::PrimitiveState::default(),
523 depth_stencil: None,
524 multisample: wgpu::MultisampleState::default(),
525 multiview_mask: None,
526 cache: None,
527 });
528
529 // --- decay + present: fullscreen samples of the accumulation field ---
530 // The layouts, the sampler and both pipelines are grid-independent; only
531 // the bind groups that name the field's views are not, and those live in
532 // `FieldResources`.
533 let field_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
534 label: Some("attractor-sampler"),
535 address_mode_u: wgpu::AddressMode::ClampToEdge,
536 address_mode_v: wgpu::AddressMode::ClampToEdge,
537 address_mode_w: wgpu::AddressMode::ClampToEdge,
538 mag_filter: wgpu::FilterMode::Linear,
539 min_filter: wgpu::FilterMode::Linear,
540 ..Default::default()
541 });
542
543 let decay_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
544 label: Some("attractor-decay-layout"),
545 entries: &[
546 gpu::texture(0, true),
547 gpu::sampler(1),
548 gpu::uniform(2, wgpu::ShaderStages::FRAGMENT),
549 ],
550 });
551 let decay_pipeline = gpu::fullscreen_pipeline(
552 device,
553 &decay_shader,
554 &[&decay_layout],
555 PingPongField::FORMAT,
556 // The decay pass overwrites the trail field with the faded previous frame.
557 wgpu::BlendState::REPLACE,
558 "attractor-decay",
559 );
560
561 // Texture, sampler, uniform, **sampler again** — the fourth entry is the
562 // same sampler a second time, and it is there to make this layout a shape
563 // nothing else in the crate has. `occlude` (ADR-0085) needed a uniform in a
564 // pass that had none; see `PRESENT_SHADER` for the measurement that says a
565 // colliding shape silently mis-renders on WARP.
566 let present_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
567 label: Some("attractor-present-layout"),
568 entries: &[
569 gpu::texture(0, true),
570 gpu::sampler(1),
571 gpu::uniform(2, wgpu::ShaderStages::FRAGMENT),
572 gpu::sampler(3),
573 ],
574 });
575 let present_pipeline = gpu::fullscreen_pipeline(
576 device,
577 &present_shader,
578 &[&present_layout],
579 surface_format,
580 // Premultiplied-alpha OVER the backdrop (ADR-0026): the accumulation is
581 // emissive, so `c` adds over the atmosphere and the present's alpha
582 // (accumulated luminance) reveals bg_* in the cloud's empty space. Over
583 // the default black backdrop this equals the prior opaque present.
584 wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING,
585 "attractor-present",
586 );
587
588 Self {
589 compute_pipeline,
590 draw_pipeline,
591 decay_pipeline,
592 present_pipeline,
593 particles,
594 step_uniform,
595 step_stride,
596 draw_uniform,
597 decay_uniform,
598 compute_bg,
599 draw_bg,
600 luts,
601 decay_layout,
602 present_layout,
603 field_sampler,
604 count,
605 }
606 }
607}
608
609impl FieldResources {
610 /// Allocate the accumulation field at `trail_w`x`trail_h` and bind its two
611 /// views into the four decay/present groups, reusing `pipelines`' layouts,
612 /// sampler and decay uniform. No shader, pipeline, particle or LUT resource
613 /// is created here — that is the whole point of the split.
614 pub(super) fn build(
615 device: &wgpu::Device,
616 pipelines: &PipelineResources,
617 trail_w: u32,
618 trail_h: u32,
619 ) -> Self {
620 let field = PingPongField::new(device, trail_w, trail_h);
621 let decay_bg_a = blit_bind_group(
622 device,
623 &pipelines.decay_layout,
624 "attractor-decay-bg-a",
625 field.view_a(),
626 &pipelines.field_sampler,
627 Some(&pipelines.decay_uniform),
628 false,
629 );
630 let decay_bg_b = blit_bind_group(
631 device,
632 &pipelines.decay_layout,
633 "attractor-decay-bg-b",
634 field.view_b(),
635 &pipelines.field_sampler,
636 Some(&pipelines.decay_uniform),
637 false,
638 );
639 let present_bg_a = blit_bind_group(
640 device,
641 &pipelines.present_layout,
642 "attractor-present-bg-a",
643 field.view_a(),
644 &pipelines.field_sampler,
645 Some(&pipelines.decay_uniform),
646 true,
647 );
648 let present_bg_b = blit_bind_group(
649 device,
650 &pipelines.present_layout,
651 "attractor-present-bg-b",
652 field.view_b(),
653 &pipelines.field_sampler,
654 Some(&pipelines.decay_uniform),
655 true,
656 );
657 Self {
658 field,
659 decay_bg_a,
660 decay_bg_b,
661 present_bg_a,
662 present_bg_b,
663 trail_w,
664 trail_h,
665 }
666 }
667
668 /// Clear both accumulation textures to black — run once after a (re)build so
669 /// the first decay pass reads a defined (empty) trail rather than garbage.
670 pub(super) fn clear_field(&self, encoder: &mut wgpu::CommandEncoder) {
671 for view in [self.field.view_a(), self.field.view_b()] {
672 gpu::color_pass(
673 encoder,
674 "attractor-clear-pass",
675 view,
676 wgpu::LoadOp::Clear(wgpu::Color::BLACK),
677 );
678 }
679 }
680}
681
682/// Byte stride between two dynamically-offset slots of a `StepUniform`, rounded
683/// up to the adapter's `min_uniform_buffer_offset_alignment` (256 on the default
684/// limits). Read from the device rather than hardcoded: a dynamic offset that is
685/// not a multiple of it is a validation error, and the limit is the adapter's to
686/// state.
687pub(super) fn uniform_stride(device: &wgpu::Device) -> u32 {
688 let align = device.limits().min_uniform_buffer_offset_alignment.max(1);
689 size_of::<StepUniform>().next_multiple_of(align as usize) as u32
690}
691
692pub(super) fn storage_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
693 wgpu::BindGroupLayoutEntry {
694 binding,
695 visibility: wgpu::ShaderStages::COMPUTE,
696 ty: wgpu::BindingType::Buffer {
697 ty: wgpu::BufferBindingType::Storage { read_only: false },
698 has_dynamic_offset: false,
699 min_binding_size: None,
700 },
701 count: None,
702 }
703}
704
705/// A texture(+sampler)[+uniform][+sampler] bind group for the decay/present
706/// fullscreen passes.
707///
708/// `uniform` is the decay buffer for both — the retention factor for decay, and
709/// `occlude` out of the same buffer's second component for present (ADR-0085).
710/// `repeat_sampler` binds the sampler a second time at binding 3 and is the
711/// **present** pass only: it is what makes that layout a fourth shape rather than
712/// a copy of `attractor-decay-layout`'s. See `PRESENT_SHADER`.
713pub(super) fn blit_bind_group(
714 device: &wgpu::Device,
715 layout: &wgpu::BindGroupLayout,
716 label: &str,
717 input: &wgpu::TextureView,
718 sampler: &wgpu::Sampler,
719 uniform: Option<&wgpu::Buffer>,
720 repeat_sampler: bool,
721) -> wgpu::BindGroup {
722 let mut entries = vec![
723 wgpu::BindGroupEntry {
724 binding: 0,
725 resource: wgpu::BindingResource::TextureView(input),
726 },
727 wgpu::BindGroupEntry {
728 binding: 1,
729 resource: wgpu::BindingResource::Sampler(sampler),
730 },
731 ];
732 if let Some(buf) = uniform {
733 entries.push(wgpu::BindGroupEntry {
734 binding: 2,
735 resource: buf.as_entire_binding(),
736 });
737 }
738 if repeat_sampler {
739 entries.push(wgpu::BindGroupEntry {
740 binding: 3,
741 resource: wgpu::BindingResource::Sampler(sampler),
742 });
743 }
744 device.create_bind_group(&wgpu::BindGroupDescriptor {
745 label: Some(label),
746 layout,
747 entries: &entries,
748 })
749}