sgl-3d 0.4.2

Caller-driven 3D rendering for SGL games, native and in the browser (WebGPU)
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
//! Volume paths (`scene_volume_path`, D-41) observed at real boundaries:
//! frames rendered, submitted and finished through `Renderer::render`, and
//! their composite read back. The fixture shader
//! (`test_support::VOLUME_SHADER`) writes its path's length, its bound and
//! its side into an unlit blended box's emission, which the composite holds
//! as written over black, showing one side of each box and hiding the other.
//! Each oracle is the test's own closed boxes, intersected on the CPU with
//! each pixel's ray through the test's camera, at pixels whose ray crosses
//! every face it meets clear of the faces' edges.
use super::shader_tests::{Harness, SIZE, frame, ray, settings, texel, wall};
use crate::asset::{self, Vertex};
use crate::settings::Settings;
use crate::test_support::{self, VOLUME_SHADER, VolumeShaderParams};
use crate::{
    AlphaMode, Camera, InstanceId, InstanceState, MaterialId, Mobility, ModelMesh, PreparedModel,
    Scene, ShaderId, ShaderSource,
};
use glam::{DVec3, Mat4, Quat, Vec3};

/// The contract's bounds (shader_contract.wgsl), as the fixture writes them.
const NONE: f32 = 0.;
const EXIT: f32 = 1.;
const OPAQUE: f32 = 2.;
const HIDDEN: f32 = 3.;
const ENTRY: f32 = 4.;
const EYE: f32 = 5.;

/// How far from a face's edges a pixel's ray must cross it to be checked.
const MARGIN: f64 = 0.03;

/// A closed box, its centre and half extents in its mesh's units, at a
/// rigid pose.
#[derive(Clone, Copy)]
struct Cuboid {
    center: Vec3,
    half: Vec3,
    pose: Mat4,
}

impl Cuboid {
    fn new(center: [f32; 3], half: [f32; 3]) -> Self {
        Self {
            center: Vec3::from_array(center),
            half: Vec3::from_array(half),
            pose: Mat4::IDENTITY,
        }
    }

    /// Its twelve triangles, wound counter-clockwise seen from outside.
    fn mesh(self) -> (Vec<Vertex>, Vec<u32>) {
        let cube = test_support::cube();
        let mesh = &cube.meshes[0];
        let vertices = mesh
            .vertices
            .iter()
            .map(|vertex| Vertex {
                position: (self.center + Vec3::from_array(vertex.position) * 2. * self.half)
                    .to_array(),
                ..*vertex
            })
            .collect();
        (vertices, mesh.indices.clone())
    }

    /// The box moved along `by` in its mesh's units, as the fixture's
    /// vertex function moves it before the pose.
    fn moved(self, by: Vec3) -> Self {
        Self {
            center: self.center + by,
            ..self
        }
    }

    /// Where `ray` (a world origin and unit direction) enters and leaves
    /// it, as distances along the ray from its origin, where it crosses it
    /// with every face it meets crossed clear of that face's edges by
    /// `margin`; none where it misses it or passes nearer an edge.
    fn crossing(self, (origin, direction): (DVec3, DVec3), margin: f64) -> Option<(f64, f64)> {
        let local = self.pose.as_dmat4().inverse();
        let origin = local.transform_point3(origin);
        let direction = local.transform_vector3(direction);
        let (center, half) = (self.center.as_dvec3(), self.half.as_dvec3());
        let (mut near, mut far) = (f64::NEG_INFINITY, f64::INFINITY);
        for axis in 0..3 {
            if direction[axis].abs() < 1e-12 {
                if (origin[axis] - center[axis]).abs() >= half[axis] {
                    return None;
                }
                continue;
            }
            let a = (center[axis] - half[axis] - origin[axis]) / direction[axis];
            let b = (center[axis] + half[axis] - origin[axis]) / direction[axis];
            near = near.max(a.min(b));
            far = far.min(a.max(b));
        }
        if near >= far || far <= 0. {
            return None;
        }
        // A point on a face lies at its half extent along one axis alone,
        // and within the others' by the margin.
        let clear = |t: f64| {
            let point = origin + direction * t - center;
            (0..3)
                .filter(|&axis| point[axis].abs() > half[axis] - margin)
                .count()
                <= 1
        };
        (clear(far) && (near <= 0. || clear(near))).then_some((near, far))
    }

    /// Whether `ray` passes within `margin` of it.
    fn near(self, ray: (DVec3, DVec3), margin: f64) -> bool {
        let grown = Self {
            half: self.half + Vec3::splat(margin as f32),
            ..self
        };
        grown.crossing(ray, -1.).is_some()
    }
}

/// A camera at `eye` looking along -Z.
fn camera_at(eye: Vec3) -> Camera {
    Camera {
        view: Mat4::from_translation(-eye),
        projection: crate::perspective(1., 1., 0.1),
        eye,
    }
}

/// Unlit black: the composite holds a fixture box's emission as written.
fn black() -> asset::Material {
    asset::Material {
        base: [0., 0., 0., 1.],
        unlit: true,
        double_sided: true,
        ..Default::default()
    }
}

/// `VOLUME_SHADER` added to `scene`.
fn add_volume_shader(scene: &mut Scene) -> ShaderId {
    scene
        .add_shader(ShaderSource {
            wgsl: VOLUME_SHADER.into(),
            label: "volume shader".into(),
        })
        .unwrap_or_else(|error| panic!("{error}"))
}

/// A blended material through `VOLUME_SHADER` with `params`, whose
/// vertices move at most `bound`.
fn volume(
    harness: &mut Harness,
    shader: ShaderId,
    params: VolumeShaderParams,
    bound: f32,
) -> MaterialId {
    let material = harness.shaded(
        asset::Material {
            alpha: AlphaMode::Blend {
                receives_screen_space_reflections: false,
                keeps_specular: false,
            },
            ..black()
        },
        shader,
        bound,
    );
    harness
        .scene
        .set_shader_parameters(&harness.queue, material, bytemuck::bytes_of(&params))
        .unwrap();
    material
}

/// An instance of `cuboid` drawn with `material`, its shader data `shown`
/// (its front where x is positive, its back where y is).
fn place_box(
    harness: &mut Harness,
    cuboid: Cuboid,
    material: MaterialId,
    shown: [f32; 2],
) -> InstanceId {
    let (device, queue) = (&harness.device, &harness.queue);
    let (vertices, indices) = cuboid.mesh();
    let mesh = ModelMesh {
        vertices,
        indices,
        material,
        deformation: Default::default(),
    };
    let prepared = PreparedModel::with_shader_data(vec![mesh], vec![Vec::new()]).unwrap();
    let model = harness.scene.add_model(device, queue, prepared).unwrap();
    let state = InstanceState {
        pose: cuboid.pose,
        ..InstanceState::new(model)
    };
    let id = harness
        .scene
        .add_instance(device, queue, state, Mobility::Static)
        .unwrap();
    harness
        .scene
        .set_instance_shader_data(queue, id, [shown[0], shown[1], 0., 0.])
        .unwrap();
    id
}

/// An opaque unlit black wall facing the camera at depth `z`.
fn place_wall(harness: &mut Harness, z: f32) -> InstanceId {
    let material = harness.material(black());
    harness.place(wall([-40., 40.], [-40., 40.], z), material, [0.; 4])
}

/// What a pixel's fixture fragment should write: its path's length, its
/// bound and whether it is the front.
type Expected = Option<(f64, f32, bool)>;

/// Asserts the path the composite holds at each pixel `expected` gives one
/// for: its length within 1 % and the composite's half-float precision, its
/// bound and its side exactly. Returns how many pixels it checked.
fn check(composite: &[[f32; 4]], label: &str, expected: impl Fn([u32; 2]) -> Expected) -> usize {
    let mut checked = 0;
    for y in 0..SIZE[1] {
        for x in 0..SIZE[0] {
            let pixel = [x, y];
            let Some((length, bound, front)) = expected(pixel) else {
                continue;
            };
            let [observed, observed_bound, side, _] = texel(composite, pixel);
            let tolerance = 0.01 * length + 2e-3;
            assert!(
                (f64::from(observed) - length).abs() <= tolerance
                    && observed_bound == bound
                    && side == f32::from(u8::from(front)),
                "{label} {pixel:?}: length {observed}, bound {observed_bound}, front {side}; \
                 expected {length}, {bound}, {front}"
            );
            checked += 1;
        }
    }
    checked
}

// Plausible defects: the exit layer not drawn, drawn from the front faces or
// culled by the material's side, so a front fragment measures to the opaque
// surface behind its volume, or to its own face; a length in view depth
// rather than along the ray, or in device depth; the layers drawn without
// the material's vertex function, so they stay where the mesh rests while
// the blended draw moves. The oracle is a thin box turned about Y in front
// of a wall, intersected with each pixel's ray: its front fragments report
// the box's own along-ray thickness, bounded by its exit, with the wall at
// 9 m and again at 15 m; then, with the vertex function moving the box 1.5
// m along its turned x, the displaced box's thickness, at pixels among
// which some lie off the box at rest.
#[test]
fn a_front_fragment_measures_to_its_exit_whatever_lies_behind() {
    let Some(mut harness) = Harness::new(settings()) else {
        return;
    };
    if !harness.renderer.volume_paths_in_effect(&harness.settings) {
        eprintln!("skipping: the Basic tier measures no volume path");
        return;
    }
    let shader = add_volume_shader(&mut harness.scene);
    let still = VolumeShaderParams {
        direction: [1., 0., 0.],
        lift: 0.,
    };
    let material = volume(&mut harness, shader, still, 2.);
    let thin = Cuboid {
        pose: Mat4::from_translation(Vec3::new(0., 0., -5.)) * Mat4::from_rotation_y(0.5),
        ..Cuboid::new([0., 0., 0.], [1.2, 1.2, 0.3])
    };
    place_box(&mut harness, thin, material, [1., 0.]);
    let camera = camera_at(Vec3::ZERO);
    let thickness = |cuboid: Cuboid| {
        move |pixel| {
            cuboid
                .crossing(ray(&camera, pixel), MARGIN)
                .map(|(near, far)| (far - near, EXIT, true))
        }
    };
    let mut wall = place_wall(&mut harness, -9.);
    for depth in [9., 15.] {
        if depth != 9. {
            harness.scene.remove_instance(wall).unwrap();
            wall = place_wall(&mut harness, -depth);
        }
        harness.frame(&frame(camera, 0.));
        let checked = check(
            &harness.composite(),
            &format!("the wall at {depth} m"),
            thickness(thin),
        );
        assert!(checked >= 60, "the wall at {depth} m: {checked} pixels");
    }
    let lift = 1.5;
    harness
        .scene
        .set_shader_parameters(
            &harness.queue,
            material,
            bytemuck::bytes_of(&VolumeShaderParams { lift, ..still }),
        )
        .unwrap();
    harness.frame(&frame(camera, 0.));
    let moved = thin.moved(Vec3::X * lift);
    let checked = check(&harness.composite(), "displaced", thickness(moved));
    let off_rest = (0..SIZE[0] * SIZE[1])
        .map(|index| [index % SIZE[0], index / SIZE[0]])
        .filter(|&pixel| {
            let ray = ray(&camera, pixel);
            moved.crossing(ray, MARGIN).is_some() && !thin.near(ray, MARGIN)
        })
        .count();
    assert!(
        checked >= 60 && off_rest >= 20,
        "displaced: {checked} pixels, {off_rest} off the box at rest"
    );
}

// Plausible defects: the layers drawn without the opaque depth's test, so an
// exit hidden behind an opaque surface inside the volume bounds the path; a
// missing layer reported as an exit at the opaque depth (EXIT where the
// contract says OPAQUE). The oracle is an opaque cube inside a thick box:
// front fragments over the cube report the distance from the box's face to
// the cube's along each pixel's ray, bounded by the opaque surface; those
// beside it the box's thickness, bounded by its exit.
#[test]
fn an_opaque_surface_inside_the_volume_bounds_its_path() {
    let Some(mut harness) = Harness::new(settings()) else {
        return;
    };
    if !harness.renderer.volume_paths_in_effect(&harness.settings) {
        eprintln!("skipping: the Basic tier measures no volume path");
        return;
    }
    let shader = add_volume_shader(&mut harness.scene);
    let material = volume(&mut harness, shader, VolumeShaderParams::default(), 0.);
    let outer = Cuboid::new([0., 0., -6.], [2., 2., 2.]);
    let inner = Cuboid::new([0.2, -0.1, -6.], [0.7, 0.7, 0.7]);
    place_box(&mut harness, outer, material, [1., 0.]);
    let opaque = harness.material(black());
    let (vertices, indices) = inner.mesh();
    let (device, queue) = (&harness.device, &harness.queue);
    let model = harness
        .scene
        .add_model(
            device,
            queue,
            PreparedModel::new(vec![ModelMesh {
                vertices,
                indices,
                material: opaque,
                deformation: Default::default(),
            }])
            .unwrap(),
        )
        .unwrap();
    harness
        .scene
        .add_instance(device, queue, InstanceState::new(model), Mobility::Static)
        .unwrap();
    let camera = camera_at(Vec3::ZERO);
    harness.frame(&frame(camera, 0.));
    let composite = harness.composite();
    let at_inner = check(&composite, "over the opaque cube", |pixel| {
        let ray = ray(&camera, pixel);
        let (entry, _) = outer.crossing(ray, MARGIN)?;
        let (inside, _) = inner.crossing(ray, MARGIN)?;
        Some((inside - entry, OPAQUE, true))
    });
    let beside = check(&composite, "beside the opaque cube", |pixel| {
        let ray = ray(&camera, pixel);
        let (entry, exit) = outer.crossing(ray, MARGIN)?;
        (!inner.near(ray, MARGIN)).then_some((exit - entry, EXIT, true))
    });
    assert!(
        at_inner >= 20 && beside >= 40,
        "{at_inner} pixels over the cube, {beside} beside it"
    );
}

// Plausible defects: the second exit layer not peeled behind the first (it
// holds the first's faces again, or nothing), so a volume behind another's
// far side measures to the opaque surface or reports the other's exit; a
// third crossing given the opaque bound unmarked. The oracle is three boxes
// along the camera's axis, the two nearer hidden: the far box's front
// fragments report its own thickness, bounded by an exit, behind the middle
// box alone (the second exit layer's) and beside both (the exit layer's),
// and, behind both nearer boxes, the distance to the wall behind it,
// marked hidden.
#[test]
fn a_volume_behind_another_measures_its_own_exit() {
    let Some(mut harness) = Harness::new(settings()) else {
        return;
    };
    if !harness.renderer.volume_paths_in_effect(&harness.settings) {
        eprintln!("skipping: the Basic tier measures no volume path");
        return;
    }
    let shader = add_volume_shader(&mut harness.scene);
    let material = volume(&mut harness, shader, VolumeShaderParams::default(), 0.);
    let near = Cuboid::new([0.1, 0., -3.], [0.5, 0.5, 0.25]);
    let middle = Cuboid::new([0., 0.1, -5.], [1.4, 1.4, 0.3]);
    let far = Cuboid::new([0., 0., -8.], [3.5, 3.5, 0.5]);
    place_box(&mut harness, near, material, [0., 0.]);
    place_box(&mut harness, middle, material, [0., 0.]);
    place_box(&mut harness, far, material, [1., 0.]);
    place_wall(&mut harness, -12.);
    let camera = camera_at(Vec3::ZERO);
    harness.frame(&frame(camera, 0.));
    let composite = harness.composite();
    let counted =
        [(true, true), (false, true), (false, false)].map(|(behind_near, behind_middle)| {
            let label =
                format!("behind the near box {behind_near}, the middle box {behind_middle}");
            check(&composite, &label, |pixel| {
                let ray = ray(&camera, pixel);
                let crosses = |cuboid: Cuboid| {
                    if cuboid.crossing(ray, MARGIN).is_some() {
                        Some(true)
                    } else if cuboid.near(ray, MARGIN) {
                        None
                    } else {
                        Some(false)
                    }
                };
                if crosses(near)? != behind_near || crosses(middle)? != behind_middle {
                    return None;
                }
                let (entry, exit) = far.crossing(ray, MARGIN)?;
                if behind_near {
                    // To the wall at z = -12 along the ray.
                    let to_wall = (-12. - ray.0.z) / ray.1.z;
                    Some((to_wall - entry, HIDDEN, true))
                } else {
                    Some((exit - entry, EXIT, true))
                }
            })
        });
    assert!(
        counted.iter().all(|&count| count >= 15),
        "pixels behind both nearer boxes, the middle alone and neither: {counted:?}"
    );
}

// Plausible defects: the entry layer not drawn, or drawn from the back
// faces, so a back fragment seen from inside its volume measures from the
// eye, or from its own face; an eye length in view depth rather than along
// the ray. The oracle is a box seen from outside, its back fragments
// alone shown, which report its along-ray thickness, bounded by its entry;
// and from inside, which report the distance from the eye to where each
// pixel's ray leaves it, bounded by the eye.
#[test]
fn a_back_fragment_measures_from_its_entry_or_the_eye() {
    let Some(mut harness) = Harness::new(settings()) else {
        return;
    };
    if !harness.renderer.volume_paths_in_effect(&harness.settings) {
        eprintln!("skipping: the Basic tier measures no volume path");
        return;
    }
    let shader = add_volume_shader(&mut harness.scene);
    let material = volume(&mut harness, shader, VolumeShaderParams::default(), 0.);
    let cuboid = Cuboid {
        pose: Mat4::from_translation(Vec3::new(0., 0., -5.)) * Mat4::from_rotation_y(0.3),
        ..Cuboid::new([0., 0., 0.], [2., 2., 2.])
    };
    place_box(&mut harness, cuboid, material, [0., 1.]);
    place_wall(&mut harness, -14.);
    let outside = camera_at(Vec3::ZERO);
    harness.frame(&frame(outside, 0.));
    let from_entry = check(&harness.composite(), "from outside", |pixel| {
        let (entry, exit) = cuboid.crossing(ray(&outside, pixel), MARGIN)?;
        Some((exit - entry, ENTRY, false))
    });
    let eye = Vec3::new(0.4, -0.3, -4.2);
    let inside = camera_at(eye);
    harness.frame(&frame(inside, 0.));
    let from_eye = check(&harness.composite(), "from inside", |pixel| {
        let ray = ray(&inside, pixel);
        let (_, exit) = cuboid.crossing(ray, MARGIN)?;
        Some(((ray.0 + ray.1 * exit).distance(eye.as_dvec3()), EYE, false))
    });
    assert!(
        from_entry >= 60 && from_eye >= 200,
        "{from_entry} pixels from outside, {from_eye} from inside"
    );
}

// Plausible defect: a back fragment measured from an entry that belongs to
// another volume in front of it, so its path spans that volume and the
// space between, which a shader that hides a back face behind an entry
// (the example's glass) erases in the other volume's silhouette. The
// oracle is the camera inside a large box with a smaller hidden box between
// it and the large box's far side: behind the small box the large box's
// back fragments report their segment hidden, with the distance from the
// eye to where each pixel's ray leaves the large box (an upper bound);
// beside it, that distance bounded by the eye.
#[test]
fn a_back_fragment_behind_another_exit_is_hidden() {
    let Some(mut harness) = Harness::new(settings()) else {
        return;
    };
    if !harness.renderer.volume_paths_in_effect(&harness.settings) {
        eprintln!("skipping: the Basic tier measures no volume path");
        return;
    }
    let shader = add_volume_shader(&mut harness.scene);
    let material = volume(&mut harness, shader, VolumeShaderParams::default(), 0.);
    let outer = Cuboid::new([0., 0., -3.], [3., 3., 4.]);
    let inner = Cuboid::new([0.1, -0.1, -3.5], [0.8, 0.8, 0.4]);
    place_box(&mut harness, outer, material, [0., 1.]);
    place_box(&mut harness, inner, material, [0., 0.]);
    place_wall(&mut harness, -12.);
    let eye = Vec3::new(0.2, -0.1, 0.);
    let camera = camera_at(eye);
    harness.frame(&frame(camera, 0.));
    let composite = harness.composite();
    let counted = [true, false].map(|behind| {
        check(
            &composite,
            &format!("behind the inner box {behind}"),
            |pixel| {
                let ray = ray(&camera, pixel);
                let crosses = if inner.crossing(ray, MARGIN).is_some() {
                    true
                } else if inner.near(ray, MARGIN) {
                    return None;
                } else {
                    false
                };
                if crosses != behind {
                    return None;
                }
                let (_, exit) = outer.crossing(ray, MARGIN)?;
                let from_eye = (ray.0 + ray.1 * exit).distance(eye.as_dvec3());
                Some((from_eye, if behind { HIDDEN } else { EYE }, false))
            },
        )
    });
    assert!(
        counted[0] >= 20 && counted[1] >= 100,
        "{} pixels behind the inner box, {} beside it",
        counted[0],
        counted[1]
    );
}

// Plausible defect: the layers' side selection swapped under a mirroring
// pose, whose winding reverses, so a mirrored volume's entry and exit
// layers trade faces and its fragments measure from or to their own side.
// The oracle is a box posed with a negative scale, intersected through its
// pose: its front fragments report its thickness bounded by its exit, and
// its back fragments the same thickness bounded by its entry, as an
// unmirrored box's do.
#[test]
fn a_mirrored_volume_keeps_its_sides() {
    let Some(mut harness) = Harness::new(settings()) else {
        return;
    };
    if !harness.renderer.volume_paths_in_effect(&harness.settings) {
        eprintln!("skipping: the Basic tier measures no volume path");
        return;
    }
    let shader = add_volume_shader(&mut harness.scene);
    let material = volume(&mut harness, shader, VolumeShaderParams::default(), 0.);
    let mirrored = Cuboid {
        pose: Mat4::from_translation(Vec3::new(0., 0., -5.))
            * Mat4::from_rotation_y(0.4)
            * Mat4::from_scale(Vec3::new(-1., 1., 1.)),
        ..Cuboid::new([0.3, 0., 0.], [1.2, 1., 0.5])
    };
    let id = place_box(&mut harness, mirrored, material, [1., 0.]);
    place_wall(&mut harness, -12.);
    let camera = camera_at(Vec3::ZERO);
    for (front, bound, shown) in [(true, EXIT, [1., 0.]), (false, ENTRY, [0., 1.])] {
        harness
            .scene
            .set_instance_shader_data(&harness.queue, id, [shown[0], shown[1], 0., 0.])
            .unwrap();
        harness.frame(&frame(camera, 0.));
        let checked = check(&harness.composite(), &format!("front {front}"), |pixel| {
            let (entry, exit) = mirrored.crossing(ray(&camera, pixel), MARGIN)?;
            Some((exit - entry, bound, front))
        });
        assert!(checked >= 60, "front {front}: {checked} pixels");
    }
}

// Plausible defects: the layers bound or measured where the binding tier's
// sampled textures are spent, or while the setting is off, so a shader
// reads a stale or garbage path where the contract promises none; the
// setting's resolution misreported. The oracle is the contract: on the
// Extended tier with `Settings::volume_paths` off, and on the Basic tier
// (a device of WebGPU's default limits) with it on, the box's front
// fragments report no path (VOLUME_NONE, length 0) and the renderer reports
// volume paths out of effect; on the Extended tier with it on, in effect.
#[test]
fn no_path_is_measured_on_the_basic_tier_or_with_the_setting_off() {
    let Some(adapter) = test_support::adapter() else {
        return;
    };
    let basic = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
        required_limits: wgpu::Limits::default(),
        ..Default::default()
    }))
    .unwrap();
    let off = Settings {
        volume_paths: false,
        ..settings()
    };
    for (label, device, settings) in [
        ("Extended, off", test_support::device(), off),
        ("Basic, on", Some(basic), settings()),
    ] {
        let Some(device) = device else {
            return;
        };
        let mut harness = Harness::on(device, settings);
        let extended =
            harness.renderer.binding_tier() == crate::graphics_device::BindingTier::Extended;
        assert_eq!(extended, label.starts_with("Extended"), "{label}: the tier");
        assert!(
            !harness.renderer.volume_paths_in_effect(&harness.settings),
            "{label}: volume paths in effect"
        );
        if extended {
            assert!(
                harness
                    .renderer
                    .volume_paths_in_effect(&super::shader_tests::settings()),
                "{label}: volume paths out of effect with the setting on"
            );
        }
        let shader = add_volume_shader(&mut harness.scene);
        let material = volume(&mut harness, shader, VolumeShaderParams::default(), 0.);
        let cuboid = Cuboid::new([0., 0., -5.], [1.5, 1.5, 0.5]);
        place_box(&mut harness, cuboid, material, [1., 0.]);
        place_wall(&mut harness, -9.);
        let camera = camera_at(Vec3::ZERO);
        harness.frame(&frame(camera, 0.));
        let checked = check(&harness.composite(), label, |pixel| {
            cuboid
                .crossing(ray(&camera, pixel), MARGIN)
                .map(|_| (0., NONE, true))
        });
        assert!(checked >= 60, "{label}: {checked} pixels");
    }
}

// Plausible defects: the volume layers drawn in frames that show no
// material whose shader reads its volume path, or while the setting is off,
// a cost D-41 rules out; or not drawn in one that shows one. The oracle is
// the requirement, observed in the GPU timings of the frames' passes: a
// frame facing a blended box whose shader reads its path times `volume
// layers` passes, the same scene seen the other way, facing a blended box
// whose shader reads none, times none, and so does the first view with the
// setting off.
#[test]
fn volume_layers_are_drawn_only_for_a_shader_that_reads_them() {
    let Some(gpu) = test_support::device_choosing(|adapter| {
        crate::graphics_device::features(adapter)
            | (adapter.features() & wgpu::Features::TIMESTAMP_QUERY)
    }) else {
        return;
    };
    let mut harness = Harness::on(gpu, settings());
    let Some(mut timing) = crate::timing::GpuTiming::new(&harness.device, &harness.queue) else {
        eprintln!("skipping: the adapter has no timestamp queries");
        return;
    };
    if !harness.renderer.volume_paths_in_effect(&harness.settings) {
        eprintln!("skipping: the Basic tier measures no volume path");
        return;
    }
    let reading = add_volume_shader(&mut harness.scene);
    let reading = volume(&mut harness, reading, VolumeShaderParams::default(), 0.);
    let other = test_support::add_test_shader(&mut harness.scene);
    let other = harness.shaded(
        asset::Material {
            alpha: AlphaMode::Blend {
                receives_screen_space_reflections: false,
                keeps_specular: false,
            },
            ..black()
        },
        other,
        0.,
    );
    place_box(
        &mut harness,
        Cuboid::new([0., 0., -5.], [1., 1., 1.]),
        reading,
        [1., 0.],
    );
    place_box(
        &mut harness,
        Cuboid::new([0., 0., 5.], [1., 1., 1.]),
        other,
        [1., 0.],
    );
    // Whether a frame facing `facing` with the setting `on` timed a `volume
    // layers` pass: its timings are read back once it completes, a resolve
    // and a map later, within a few polls.
    let mut drawn = |facing: Quat, on: bool| {
        let settings = Settings {
            volume_paths: on,
            ..harness.settings
        };
        let input = frame(
            Camera {
                view: Mat4::from_quat(facing),
                ..camera_at(Vec3::ZERO)
            },
            0.,
        );
        let (device, queue) = (&harness.device, &harness.queue);
        let _ = timing.begin_frame(device, queue);
        let mut encoder = device.create_command_encoder(&Default::default());
        harness.renderer.render(
            device,
            queue,
            &mut encoder,
            &mut harness.scene,
            &input,
            &settings,
            &harness.output,
            Some(&timing),
        );
        queue.submit([encoder.finish()]);
        timing.submitted(queue);
        harness.renderer.finish_frame(&mut harness.scene);
        for _ in 0..8 {
            device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
            if let Some(frame) = timing.begin_frame(device, queue).next_back() {
                return frame.passes.iter().any(|pass| pass.name == "volume layers");
            }
        }
        panic!("the frame's timings did not complete");
    };
    let away = Quat::from_rotation_y(std::f32::consts::PI);
    for (label, facing, on, expected) in [
        ("facing the reading box", Quat::IDENTITY, true, true),
        ("facing the other box", away, true, false),
        ("with the setting off", Quat::IDENTITY, false, false),
        ("facing the reading box again", Quat::IDENTITY, true, true),
    ] {
        assert_eq!(drawn(facing, on), expected, "{label}");
    }
}