crcbl-render 0.1.0

Crucible renderer: the 3D frame on crcbl-frame — meshes, materials, lights, effects
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
//! The compute pass that reads the reflective shadow map into every probe row.
//!
//! ```text
//!  crate::rsm's two maps' targets ─┐
//!  the captured visibility maps ───┼─▶ probe_gather.slang ─▶ crate::probe's rows
//!  ProbeVolume::position, as data ─┤     one workgroup per probe,
//!  the punctual producer rows ─────┘     walking every producer
//! ```
//!
//! The irradiance probes' raster updater, second half.
//! [`crate::rsm`] is the pass that draws the map and
//! [`crcbl_shaders::probe_gather`] owns the parameter block and the two numbers
//! this side and the shader have to agree on.
//!
//! # A layout of its own, which is where the writable rows belong
//!
//! The mesh layout binds the probe table **read-only** and is at WebGPU's
//! guaranteed storage-buffer ceiling with no headroom — `crate::forward`'s
//! `PROBE_TABLE_BINDING` carries both facts and names this pass as where the
//! writable binding goes. So this is a bind group layout of its own, four
//! entries of which are images and two of which are buffers, and the table's
//! device-local memory is what makes the write legal at all — see [`crate::probe`].
//!
//! # One dispatch walks the sun's map and every punctual face
//!
//! `probe_gather.slang` ends in a plain store, so a second dispatch over the
//! same rows would erase the first's rather than add to it. Walking every
//! producer inside one dispatch is what keeps that store a store — and it pays
//! the shader's `groupshared` reduction once rather than once per producer, and
//! leaves a row a function of one dispatch, which is `docs/backlog.md`'s survey
//! constraint C2.
//!
//! # The probe positions arrive as data
//!
//! `probe_octahedral.slang`'s rule exactly:
//! [`ProbeVolume::position`](crcbl_shaders::probe::ProbeVolume::position) owns
//! the clipmap arithmetic that turns a level and a cell into a place, so the
//! host evaluates it once for every row and the shader reads a table. A second
//! transcription of it in Slang would be a second thing that can drift from the
//! volume it has to agree with, and `a_position_table_walks_the_volume_in_row_order`
//! is what holds this one to the module.

use crcbl_hal::{
    BindGroupEntry, BindGroupHandle, BindGroupLayoutDesc, BindGroupLayoutEntry,
    BindGroupLayoutHandle, BindingFlags, BindingKind, BindingResource, BufferDesc, BufferHandle,
    BufferUsage, ComputePipelineHandle, Device, HalError, ImageViewHandle, ImageViewType,
    MemoryLocation, PipelineLayoutDesc, PipelineLayoutHandle, ResourceState, SampleType,
    ShaderStages, check_portable_storage_buffers,
};
use crcbl_shaders::probe::ProbeVolume;
use crcbl_shaders::probe_gather::{
    GATHER_PARAMS_SIZE, GATHER_WORKGROUP_SIZE, GatherParams, PRODUCER_STRIDE, PunctualProducer,
};

use crate::draw_gen::{bound, compute_pipeline, storage, uniform};
use crate::graph::{BufferId, ImageId, RenderGraph};
use crate::ssao::cached_group;

/// Bytes one probe's position occupies in the table the shader reads: a
/// `float4`, because that is what a `StructuredBuffer<float4>` element is.
const POSITION_STRIDE: u64 = 16;

/// Rows the producer table holds: one per light tile of the shadow atlas, which
/// is the most punctual faces a frame can draw.
///
/// **Sized for the atlas rather than for the frame**, because a buffer is
/// created once and a frame's producer count moves with the lights the selection
/// happened to admit — see [`GatherParams::producers`], which is what bounds the
/// shader's loop.
const PRODUCER_ROWS: usize = crate::shadow::LIGHT_TILES;

/// How large a [`ProbeGather`] is and what it writes into.
#[derive(Clone, Copy, Debug)]
pub(crate) struct ProbeGatherDesc<'a> {
    /// Debug name; every resource is named after it.
    pub(crate) label: Option<&'a str>,
    /// The volume whose rows this fills — read for its probe count and for
    /// where each of those probes stands.
    pub(crate) volume: ProbeVolume,
    /// The probe table's ring, one buffer per frame in flight, in the order
    /// [`ProbeTable::buffer`](crate::probe::ProbeTable::buffer) hands them out.
    /// Its length is the ring this builds over.
    pub(crate) probes: &'a [BufferHandle],
}

/// The gather's pipeline, its per-frame parameter blocks and the probe-position
/// table it reads.
#[derive(Debug)]
pub(crate) struct ProbeGather {
    layout: BindGroupLayoutHandle,
    pipeline_layout: PipelineLayoutHandle,
    pipeline: ComputePipelineHandle,
    /// One parameter block per frame in flight, host-uploaded and rewritten
    /// every frame — the sun moves, and so does the cascade the map covers.
    params: Vec<BufferHandle>,
    /// This frame's rows, in the ring's own order.
    probes: Vec<BufferHandle>,
    /// Where each probe stands, evaluated once from the volume.
    positions: BufferHandle,
    /// One producer table per frame in flight, host-uploaded and rewritten every
    /// frame: the lights move, and so does which of them holds an atlas tile.
    producers: Vec<BufferHandle>,
    /// One cached bind group per frame — see [`cached_group`] for why a group
    /// naming a graph transient cannot be built where the others are.
    groups: Vec<Option<(Vec<ImageViewHandle>, BindGroupHandle)>>,
    /// Rows the volume holds, which is also the workgroup count.
    rows: u32,
}

/// The three targets one of [`crate::rsm`]'s passes wrote, as the graph knows
/// them.
#[derive(Clone, Copy, Debug)]
pub(crate) struct RsmTargets {
    /// `RsmOutput::albedo`.
    pub(crate) albedo: ImageId,
    /// `RsmOutput::normal`.
    pub(crate) normal: ImageId,
    /// `RsmOutput::world`.
    pub(crate) world: ImageId,
}

/// Both maps [`crate::rsm`] describes, as the graph knows them.
#[derive(Clone, Copy, Debug)]
pub(crate) struct RsmImages {
    /// The sun's near cascade, drawn once at [`crate::rsm::extent`].
    pub(crate) sun: RsmTargets,
    /// Every shadowed punctual face, drawn one tile each at
    /// [`crate::rsm::punctual_extent`] — see [`crate::rsm::punctual_tile`].
    pub(crate) punctual: RsmTargets,
}

impl ProbeGather {
    /// One pass, which is what [`crate::forward::ForwardRenderer::MAX_PASSES`]
    /// counts it as.
    pub(crate) const PASSES: u32 = 1;

    /// Builds the pipeline, the parameter ring and the probe-position table.
    ///
    /// # Errors
    ///
    /// [`HalError::InvalidDescriptor`] for a ring of no frames or a volume with
    /// no probes — neither is a gather, and both would reach the device as a
    /// zero-sized buffer or an empty dispatch. [`HalError`] from any seam call;
    /// a failure part-way through releases what it had already created.
    pub(crate) fn new(device: &dyn Device, desc: &ProbeGatherDesc<'_>) -> Result<Self, HalError> {
        let rows = desc.volume.total();
        if desc.probes.is_empty() || rows == 0 {
            return Err(HalError::InvalidDescriptor(
                "a probe gather needs at least one frame in flight and one probe".to_string(),
            ));
        }
        let label = desc.label.unwrap_or("probe gather");
        let mut rollback = Rollback::default();
        let result = Self::build(device, desc, label, rows, &mut rollback);
        if result.is_err() {
            rollback.run(device);
        }
        result
    }

    fn build(
        device: &dyn Device,
        desc: &ProbeGatherDesc<'_>,
        label: &str,
        rows: u32,
        rollback: &mut Rollback,
    ) -> Result<Self, HalError> {
        // **`probe_gather.slang`'s declaration order**, which is the only order
        // Metal and D3D12 agree about — see `crcbl_shaders::declaration_order`.
        //
        // Every image is `UnfilterableFloat`: the shader `Load`s all four and
        // binds no sampler, WebGPU checks the layout against the *view's* format
        // rather than against how the shader reads it, and two of these formats
        // — `Rgba32Float` and the maps' `Rg32Float` — are unfilterable without a
        // device feature. Declaring the filterable pair as unfilterable is
        // allowed and is the honest description of what this pass does with
        // them; the other way round is what made every 3D demo draw black on
        // Chromium.
        let sampled = |binding: u32, view_type: ImageViewType| BindGroupLayoutEntry {
            binding,
            visibility: ShaderStages::COMPUTE,
            kind: BindingKind::SampledImage {
                view_type,
                sample_type: SampleType::UnfilterableFloat,
            },
            count: 1,
            flags: BindingFlags::empty(),
        };
        let entries = [
            uniform(0),
            storage(1, true, size_of::<[f32; 4]>() as u32),
            sampled(2, ImageViewType::D2Array),
            sampled(3, ImageViewType::D2),
            sampled(4, ImageViewType::D2),
            sampled(5, ImageViewType::D2),
            storage(6, false, size_of::<[f32; 4]>() as u32),
            sampled(7, ImageViewType::D2),
            sampled(8, ImageViewType::D2),
            sampled(9, ImageViewType::D2),
            storage(
                10,
                true,
                crcbl_shaders::probe_gather::PRODUCER_STRIDE as u32,
            ),
        ];
        let layout_desc = BindGroupLayoutDesc {
            label: Some(label),
            entries: &entries,
        };
        check_portable_storage_buffers(Some(label), &[&layout_desc])?;
        let layout = device.create_bind_group_layout(&layout_desc)?;
        rollback.layouts.push(layout);
        let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDesc {
            label: Some(label),
            bind_group_layouts: &[layout],
            push_constants: None,
        })?;
        rollback.pipeline_layouts.push(pipeline_layout);
        let pipeline = compute_pipeline(
            device,
            label,
            &crcbl_shaders::PROBE_GATHER,
            pipeline_layout,
            GATHER_WORKGROUP_SIZE,
        )?;
        rollback.pipelines.push(pipeline);

        let positions = device.create_buffer(&BufferDesc {
            label: Some(&format!("{label} positions")),
            size: u64::from(rows) * POSITION_STRIDE,
            usage: BufferUsage::STORAGE,
            memory: MemoryLocation::HostUpload,
        })?;
        rollback.buffers.push(positions);
        device.write_buffer(positions, 0, &position_table(&desc.volume))?;

        let mut params = Vec::with_capacity(desc.probes.len());
        let mut producers = Vec::with_capacity(desc.probes.len());
        for frame in 0..desc.probes.len() {
            let block = device.create_buffer(&BufferDesc {
                label: Some(&format!("{label} params {frame}")),
                size: GATHER_PARAMS_SIZE as u64,
                usage: BufferUsage::UNIFORM,
                memory: MemoryLocation::HostUpload,
            })?;
            rollback.buffers.push(block);
            params.push(block);
            let table = device.create_buffer(&BufferDesc {
                label: Some(&format!("{label} producers {frame}")),
                size: (PRODUCER_ROWS * PRODUCER_STRIDE) as u64,
                usage: BufferUsage::STORAGE,
                memory: MemoryLocation::HostUpload,
            })?;
            rollback.buffers.push(table);
            producers.push(table);
        }

        Ok(Self {
            layout,
            pipeline_layout,
            pipeline,
            params,
            probes: desc.probes.to_vec(),
            positions,
            producers,
            groups: vec![None; desc.probes.len()],
            rows,
        })
    }

    /// Writes `frame`'s parameter block and its producer table.
    ///
    /// Called once per frame, before [`ProbeGather::add_pass`], against the same
    /// frame slot the probe table is bound from.
    ///
    /// `producers` is every punctual face the frame's punctual pass is about to
    /// draw, **in the order it draws them** — the shader reads a row's own tile
    /// rectangle rather than deriving one, so the two lists have to be the same
    /// list. `crate::forward`'s `punctual_faces` is the one place it is built.
    /// A `producers` longer than the table holds is truncated to it, which is
    /// the atlas's own bound and so cannot happen from a legal selection.
    ///
    /// # Errors
    ///
    /// [`HalError`] if either write failed.
    ///
    /// # Panics
    ///
    /// If `frame` is not a slot this was built with.
    pub(crate) fn begin_frame(
        &self,
        device: &dyn Device,
        frame: usize,
        sun_color: [f32; 3],
        texel_area: f32,
        producers: &[PunctualProducer],
    ) -> Result<(), HalError> {
        let producers = &producers[..producers.len().min(PRODUCER_ROWS)];
        let params = GatherParams {
            sun_color,
            texel_area,
            rsm_side: crcbl_shaders::probe_gather::RSM_SIDE,
            probes: self.rows,
            producers: u32::try_from(producers.len())
                .unwrap_or_else(|_| unreachable!("bounded by the atlas's light tiles")),
        };
        device.write_buffer(self.params[frame], 0, &params.to_bytes())?;
        // **Only the rows the block claims.** The rest of the table is whatever
        // an earlier frame left there, and the shader's loop stops at
        // `params.producers` — so a shorter frame reads none of it rather than
        // paying to zero a buffer nothing will look at.
        let mut bytes = Vec::with_capacity(producers.len() * PRODUCER_STRIDE);
        for producer in producers {
            bytes.extend_from_slice(&producer.to_bytes());
        }
        if bytes.is_empty() {
            return Ok(());
        }
        device.write_buffer(self.producers[frame], 0, &bytes)
    }

    /// Adds the gather to `graph`: one workgroup per probe, reading `images` and
    /// `visibility` and writing `table`.
    ///
    /// `table` is the graph's id for the same buffer `frame` names in the ring —
    /// declared [`ResourceState::ShaderReadWrite`] here, which is what makes the
    /// graph order this write against the reads the drawing passes declare on
    /// it.
    ///
    /// # Panics
    ///
    /// If `frame` is not a slot this was built with.
    pub(crate) fn add_pass<'a>(
        &'a mut self,
        graph: &mut RenderGraph<'a>,
        frame: usize,
        images: RsmImages,
        visibility: ImageViewHandle,
        table: BufferId,
    ) {
        let pipeline = self.pipeline;
        let pipeline_layout = self.pipeline_layout;
        let layout = self.layout;
        let params = self.params[frame];
        let positions = self.positions;
        let rows = self.rows;
        let probes = self.probes[frame];
        let producers = self.producers[frame];
        // Split off before the closure, so it borrows this slot of the cache
        // rather than the whole of `self` — `crate::ssao`'s add_passes does the
        // same and for the same reason.
        let cached = &mut self.groups[frame];
        graph
            .add_compute_pass("probe-gather")
            .read_image(images.sun.albedo)
            .read_image(images.sun.normal)
            .read_image(images.sun.world)
            .read_image(images.punctual.albedo)
            .read_image(images.punctual.normal)
            .read_image(images.punctual.world)
            .use_buffer(table, ResourceState::ShaderReadWrite)
            .execute(move |ctx| {
                let albedo = ctx.image_view(images.sun.albedo);
                let normal = ctx.image_view(images.sun.normal);
                let world = ctx.image_view(images.sun.world);
                let punctual_albedo = ctx.image_view(images.punctual.albedo);
                let punctual_normal = ctx.image_view(images.punctual.normal);
                let punctual_world = ctx.image_view(images.punctual.world);
                let device = ctx.device();
                let entries = [
                    bound(0, params),
                    bound(1, positions),
                    BindGroupEntry {
                        binding: 2,
                        array_index: 0,
                        resource: BindingResource::ImageView(visibility),
                    },
                    // Overwritten by `cached_group` with the realised views;
                    // written here so the list is a complete description of the
                    // layout rather than one with three holes in it.
                    BindGroupEntry {
                        binding: 3,
                        array_index: 0,
                        resource: BindingResource::ImageView(albedo),
                    },
                    BindGroupEntry {
                        binding: 4,
                        array_index: 0,
                        resource: BindingResource::ImageView(normal),
                    },
                    BindGroupEntry {
                        binding: 5,
                        array_index: 0,
                        resource: BindingResource::ImageView(world),
                    },
                    bound(6, probes),
                    BindGroupEntry {
                        binding: 7,
                        array_index: 0,
                        resource: BindingResource::ImageView(punctual_albedo),
                    },
                    BindGroupEntry {
                        binding: 8,
                        array_index: 0,
                        resource: BindingResource::ImageView(punctual_normal),
                    },
                    BindGroupEntry {
                        binding: 9,
                        array_index: 0,
                        resource: BindingResource::ImageView(punctual_world),
                    },
                    bound(10, producers),
                ];
                // **The visibility view is part of the key too.** It is the
                // placeholder until `capture_probe_visibility` has run and the
                // captured array afterwards, and a group keyed on the transients
                // alone would go on naming the placeholder for the rest of the
                // process.
                let Some(group) = cached_group(
                    cached,
                    device,
                    &[
                        (2, visibility),
                        (3, albedo),
                        (4, normal),
                        (5, world),
                        (7, punctual_albedo),
                        (8, punctual_normal),
                        (9, punctual_world),
                    ],
                    "probe gather",
                    layout,
                    &entries,
                ) else {
                    return;
                };
                let encoder = ctx.encoder();
                encoder.bind_compute_pipeline(pipeline);
                encoder.bind_group(0, group, &[], pipeline_layout);
                // **One workgroup per probe**, not `div_ceil` of anything: a
                // group is a probe and its threads stride the whole map. See
                // `crcbl_shaders::probe_gather::GATHER_WORKGROUP_SIZE`.
                encoder.dispatch(rows, 1, 1);
            });
    }

    /// Rewrites the position table for a volume that has scrolled.
    ///
    /// **The device must be idle**, which is why this is not a per-frame call:
    /// one buffer serves every frame in flight, so a write while a frame is
    /// reading it is the hazard the ring exists to avoid elsewhere.
    /// `ForwardRenderer::follow_probe_volume` is the caller and it has just
    /// waited, on the recapture's own `wait_idle`.
    ///
    /// # Errors
    ///
    /// [`HalError`] from the write.
    pub(crate) fn rewrite_positions(
        &self,
        device: &dyn Device,
        volume: &ProbeVolume,
    ) -> Result<(), HalError> {
        device.write_buffer(self.positions, 0, &position_table(volume))
    }

    /// Releases everything. The device must be idle.
    pub(crate) fn destroy(self, device: &dyn Device) {
        device.destroy_compute_pipeline(self.pipeline);
        device.destroy_pipeline_layout(self.pipeline_layout);
        for (_, group) in self.groups.into_iter().flatten() {
            device.destroy_bind_group(group);
        }
        device.destroy_bind_group_layout(self.layout);
        device.destroy_buffer(self.positions);
        for buffer in self.params.into_iter().chain(self.producers) {
            device.destroy_buffer(buffer);
        }
    }
}

/// Where every probe of `volume` stands, one `float4` a row in the table's own
/// order: `x`-fastest within a level wrapped by that level's scroll offset, and
/// the levels one after another finest first.
///
/// [`ProbeVolume::position`](crcbl_shaders::probe::ProbeVolume::position) is the
/// one place that arithmetic is written and
/// [`ProbeVolume::row`](crcbl_shaders::probe::ProbeVolume::row) the one place
/// the addressing is — see the [module docs](self).
///
/// **Written by row rather than pushed in cell order**, because a scrolled
/// level's cell order is not its row order: `crcbl_render::probe_capture`'s
/// `probe_positions` is the same walk for the same reason, and the two have to
/// agree or the gather would light a probe whose visibility map is another
/// probe's.
fn position_table(volume: &ProbeVolume) -> Vec<u8> {
    let rows = volume.total() as usize;
    let mut bytes = vec![0u8; rows * POSITION_STRIDE as usize];
    for level in 0..volume.level_count() {
        for z in 0..volume.counts[2] {
            for y in 0..volume.counts[1] {
                for x in 0..volume.counts[0] {
                    let cell = [x, y, z];
                    let at = volume.row(level, cell) as usize * POSITION_STRIDE as usize;
                    let Some(slot) = bytes.get_mut(at..at + 3 * size_of::<f32>()) else {
                        continue;
                    };
                    for (lane, value) in volume.position(level, cell).into_iter().enumerate() {
                        slot[lane * 4..lane * 4 + 4].copy_from_slice(&value.to_le_bytes());
                    }
                    // The `w` lane a `float4` element carries stays the zero
                    // this started as; unread.
                }
            }
        }
    }
    bytes
}

/// What a failed [`ProbeGather::new`] has to give back, in the order it must.
#[derive(Default)]
struct Rollback {
    pipelines: Vec<ComputePipelineHandle>,
    pipeline_layouts: Vec<PipelineLayoutHandle>,
    layouts: Vec<BindGroupLayoutHandle>,
    buffers: Vec<BufferHandle>,
}

impl Rollback {
    fn run(self, device: &dyn Device) {
        for pipeline in self.pipelines {
            device.destroy_compute_pipeline(pipeline);
        }
        for layout in self.pipeline_layouts {
            device.destroy_pipeline_layout(layout);
        }
        for layout in self.layouts {
            device.destroy_bind_group_layout(layout);
        }
        for buffer in self.buffers {
            device.destroy_buffer(buffer);
        }
    }
}

// `Instance::create_device` is native-only: see the `crcbl_hal::device` module docs.
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;
    use crcbl_hal::null::{NullInstance, Recorder};
    use crcbl_hal::{DeviceDesc, Instance, QueueKind};

    /// A volume with an unequal count on every axis and two levels, so a table
    /// walked in the wrong order is a different set of numbers rather than the
    /// same one.
    fn volume() -> ProbeVolume {
        ProbeVolume {
            origin: [-1.0, 0.5, 2.0],
            inv_spacing: [0.5, 0.25, 1.0],
            counts: [2, 3, 4],
            levels: 2,
            steps: crcbl_shaders::probe::ProbeSteps::default(),
        }
    }

    /// One producer row, with every field distinct so a table written in the
    /// wrong order is a different set of bytes.
    fn producer() -> PunctualProducer {
        PunctualProducer {
            position: [1.0, 2.0, 3.0],
            radius: 4.0,
            color: [0.5, 0.25, 0.125],
            cos_outer: 0.5,
            axis: [0.0, -1.0, 0.0],
            cos_inner: 0.9,
            origin: [16, 32],
            side: 16,
            kind: crcbl_shaders::light::KIND_SPOT,
        }
    }

    fn read(bytes: &[u8], row: usize) -> [f32; 3] {
        let at = row * POSITION_STRIDE as usize;
        let mut out = [0.0f32; 3];
        for (axis, value) in out.iter_mut().enumerate() {
            let start = at + axis * 4;
            *value = f32::from_le_bytes(
                bytes[start..start + 4]
                    .try_into()
                    .expect("four bytes of a float"),
            );
        }
        out
    }

    /// **The table walks the volume in the table's own order**, and every entry
    /// is [`ProbeVolume::position`]'s answer rather than a second copy of the
    /// clipmap arithmetic.
    ///
    /// Compared against the module the volume is defined by, not against
    /// written-out numbers: what could go wrong here is the *order* — a level
    /// stride, a `z`-fastest walk, a scroll offset dropped — and a hand-written
    /// expectation would have to re-derive the same positions to catch it.
    ///
    /// **Swept over a scrolled volume too**, because that is where cell order
    /// and row order part company: a table written in cell order lights the
    /// probe that used to stand in each row.
    #[test]
    fn a_position_table_walks_the_volume_in_row_order() {
        let mut scrolled = volume();
        scrolled.steps[0] = [1, -1, 2];
        scrolled.steps[1] = [0, 1, -3];
        for volume in [volume(), scrolled] {
            let bytes = position_table(&volume);
            assert_eq!(
                bytes.len(),
                volume.total() as usize * POSITION_STRIDE as usize
            );
            for level in 0..volume.level_count() {
                for z in 0..volume.counts[2] {
                    for y in 0..volume.counts[1] {
                        for x in 0..volume.counts[0] {
                            let row = volume.row(level, [x, y, z]) as usize;
                            assert_eq!(
                                read(&bytes, row),
                                volume.position(level, [x, y, z]),
                                "row {row} is not probe ({x}, {y}, {z}) of level {level}"
                            );
                        }
                    }
                }
            }
        }
        let volume = volume();
        let bytes = position_table(&volume);
        // And the padding lane really is a lane rather than a fourth component
        // of the position — a table written as `float3`s would be shorter and
        // every row past the first would be wrong.
        assert_ne!(read(&bytes, 1), read(&bytes, 0));
    }

    /// A gather with nothing to gather into is refused by name rather than
    /// served as a zero-sized buffer no backend creates.
    #[test]
    fn a_gather_with_no_probes_is_refused() {
        let recorder = Recorder::new();
        let instance = NullInstance::gpu_driven().with_recorder(recorder.clone());
        let adapter = instance.adapters().remove(0);
        let device = instance
            .create_device(&DeviceDesc::for_adapter(adapter.id))
            .expect("the null backend always opens");
        let queue = device.queue(QueueKind::Graphics).expect("always present");
        let table = crate::probe::ProbeTable::new(
            device.as_ref(),
            queue,
            &crate::probe::ProbeTableDesc {
                label: Some("test"),
                capacity: 1,
                frames: 1,
            },
        )
        .expect("the null backend accepts every descriptor");
        let before = recorder.total_live_objects();
        let error = ProbeGather::new(
            device.as_ref(),
            &ProbeGatherDesc {
                label: Some("test"),
                volume: ProbeVolume::default(),
                probes: &[table.buffer(0)],
            },
        )
        .expect_err("a volume of no probes is a dispatch of no workgroups");
        assert!(
            matches!(&error, HalError::InvalidDescriptor(message)
                if message.contains("one probe")),
            "got: {error:?}"
        );
        assert_eq!(
            recorder.total_live_objects(),
            before,
            "a refusal creates nothing"
        );

        table.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// Everything created is destroyed.
    #[test]
    fn a_gather_leaks_nothing() {
        let recorder = Recorder::new();
        let instance = NullInstance::gpu_driven().with_recorder(recorder.clone());
        let adapter = instance.adapters().remove(0);
        let device = instance
            .create_device(&DeviceDesc::for_adapter(adapter.id))
            .expect("the null backend always opens");
        let queue = device.queue(QueueKind::Graphics).expect("always present");
        let volume = volume();
        let table = crate::probe::ProbeTable::new(
            device.as_ref(),
            queue,
            &crate::probe::ProbeTableDesc {
                label: Some("test"),
                capacity: volume.total(),
                frames: 2,
            },
        )
        .expect("the null backend accepts every descriptor");
        let before = recorder.total_live_objects();
        let gather = ProbeGather::new(
            device.as_ref(),
            &ProbeGatherDesc {
                label: Some("test"),
                volume,
                probes: &[table.buffer(0), table.buffer(1)],
            },
        )
        .expect("the null backend accepts every descriptor");
        assert!(recorder.total_live_objects() > before);
        gather
            .begin_frame(device.as_ref(), 0, [1.0, 1.0, 1.0], 0.25, &[producer()])
            .expect("a host-upload write always lands");
        gather.destroy(device.as_ref());
        assert_eq!(recorder.total_live_objects(), before);

        table.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// **A frame with more punctual faces than the atlas has light tiles writes
    /// only the tiles**, rather than running off the end of a buffer sized for
    /// them.
    ///
    /// The selection cannot hand out such a list — [`PRODUCER_ROWS`] is that
    /// atlas's own bound — so what this guards is the one place the two counts
    /// could drift apart. Shown red by removing the truncation, which makes the
    /// write overrun the buffer and the backend refuse it.
    #[test]
    fn a_producer_table_is_bounded_by_the_atlas_s_light_tiles() {
        let recorder = Recorder::new();
        let instance = NullInstance::gpu_driven().with_recorder(recorder.clone());
        let adapter = instance.adapters().remove(0);
        let device = instance
            .create_device(&DeviceDesc::for_adapter(adapter.id))
            .expect("the null backend always opens");
        let queue = device.queue(QueueKind::Graphics).expect("always present");
        let volume = volume();
        let table = crate::probe::ProbeTable::new(
            device.as_ref(),
            queue,
            &crate::probe::ProbeTableDesc {
                label: Some("test"),
                capacity: volume.total(),
                frames: 1,
            },
        )
        .expect("the null backend accepts every descriptor");
        let gather = ProbeGather::new(
            device.as_ref(),
            &ProbeGatherDesc {
                label: Some("test"),
                volume,
                probes: &[table.buffer(0)],
            },
        )
        .expect("the null backend accepts every descriptor");
        let too_many = vec![producer(); PRODUCER_ROWS + 3];
        gather
            .begin_frame(device.as_ref(), 0, [1.0; 3], 0.25, &too_many)
            .expect("the table takes the rows it has room for and no more");

        gather.destroy(device.as_ref());
        table.destroy(device.as_ref());
        recorder.assert_valid();
    }
}