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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
//! Shared GPU readback and caller-generated geometry for renderer fixtures.
pub(crate) fn read(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    texture: &wgpu::Texture,
    bpp: u32,
) -> Vec<u8> {
    let size = texture.size();
    let row = (size.width * bpp).div_ceil(256) * 256;
    // A 3D texture's slices, and a 2D array's layers, follow one another.
    let slices = match texture.dimension() {
        wgpu::TextureDimension::D1 => 1,
        _ => size.depth_or_array_layers,
    };
    let buffer = device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("frame evidence"),
        size: u64::from(row * size.height * slices),
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    let mut encoder = device.create_command_encoder(&Default::default());
    encoder.copy_texture_to_buffer(
        texture.as_image_copy(),
        wgpu::TexelCopyBufferInfo {
            buffer: &buffer,
            layout: wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(row),
                rows_per_image: Some(size.height),
            },
        },
        size,
    );
    queue.submit([encoder.finish()]);
    let (tx, rx) = std::sync::mpsc::channel();
    buffer.map_async(wgpu::MapMode::Read, .., move |r| {
        tx.send(r).unwrap();
    });
    device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
    rx.recv().unwrap().unwrap();
    let mapped = buffer.get_mapped_range(..).unwrap();
    let mut pixels = Vec::new();
    for line in mapped.chunks(row as usize) {
        pixels.extend_from_slice(&line[..(size.width * bpp) as usize]);
    }
    pixels
}
/// The words of `buffer`, which can be copied from.
pub(crate) fn read_words(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    buffer: &wgpu::Buffer,
) -> Vec<u32> {
    let readback = device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("buffer readback"),
        size: buffer.size(),
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    let mut encoder = device.create_command_encoder(&Default::default());
    encoder.copy_buffer_to_buffer(buffer, 0, &readback, 0, buffer.size());
    queue.submit([encoder.finish()]);
    readback.map_async(wgpu::MapMode::Read, .., |result| result.unwrap());
    device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
    bytemuck::cast_slice(&readback.get_mapped_range(..).unwrap()).to_vec()
}

/// A unit vector's octahedral coordinates (Cigolle et al. 2014, "A Survey of
/// Efficient Representations for Independent Unit Vectors", section 3.1),
/// signed, as the G-buffer stores normals.
pub(crate) fn octahedral(v: glam::Vec3) -> [f32; 2] {
    let n = v / v.abs().element_sum();
    if n.z >= 0. {
        [n.x, n.y]
    } else {
        [
            (1. - n.y.abs()) * n.x.signum(),
            (1. - n.x.abs()) * n.y.signum(),
        ]
    }
}

/// Runs `observation` in compute under the frame's ray-hit lit group 0 and
/// the scene's group 1, and reads back its `words` vec4 outputs.
#[allow(clippy::too_many_arguments)]
pub(crate) fn observe_ray_hits(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    renderer: &mut crate::Renderer,
    scene: &mut crate::Scene,
    input: &crate::FrameInput,
    settings: &crate::settings::Settings,
    observation: &str,
    outputs: usize,
) -> Vec<[f32; 4]> {
    let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("ray hit observation"),
        source: wgpu::ShaderSource::Wgsl(
            format!("{}\n{}", crate::shading::lit_compute_library(), observation).into(),
        ),
    });
    let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: None,
        entries: &[wgpu::BindGroupLayoutEntry {
            binding: 0,
            visibility: wgpu::ShaderStages::COMPUTE,
            ty: wgpu::BindingType::Buffer {
                ty: wgpu::BufferBindingType::Storage { read_only: false },
                has_dynamic_offset: false,
                min_binding_size: None,
            },
            count: None,
        }],
    });
    let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
        label: None,
        layout: Some(
            &device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: None,
                bind_group_layouts: &[
                    Some(renderer.test_lit_layout()),
                    Some(scene.scene_layout()),
                    None,
                    Some(&layout),
                ],
                immediate_size: 0,
            }),
        ),
        module: &shader,
        entry_point: Some("observe"),
        compilation_options: Default::default(),
        cache: None,
    });
    let bytes = (outputs * 16) as u64;
    let output = device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: bytes,
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        mapped_at_creation: false,
    });
    let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &layout,
        entries: &[wgpu::BindGroupEntry {
            binding: 0,
            resource: output.as_entire_binding(),
        }],
    });
    renderer.prepare_test_frame(device, queue, scene, input, settings);
    scene.update_rays(device, queue, input.visibility_mask, false);
    let mut encoder = device.create_command_encoder(&Default::default());
    {
        let mut pass = encoder.begin_compute_pass(&Default::default());
        pass.set_pipeline(&pipeline);
        pass.set_bind_group(0, renderer.test_ray_hit_lit(), &[]);
        pass.set_bind_group(1, &scene.scene_group, &[]);
        pass.set_bind_group(3, &group, &[]);
        pass.dispatch_workgroups(1, 1, 1);
    }
    queue.submit([encoder.finish()]);
    scene.finish_frame();
    let words = read_words(device, queue, &output);
    bytemuck::cast_slice::<u32, [f32; 4]>(&words).to_vec()
}

/// Runs `observation`, WGSL after the surface library that reads its cases
/// at group 0 binding 3 and writes its `outputs` vectors at binding 4, from
/// its entry point `observe` over `workgroups` workgroups, under lit group
/// 0's `scene` lights, lookup tables and environment sampler, a zeroed frame
/// and no shadows, and reads the vectors back.
pub(crate) fn observe_surface(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    scene: &crate::Scene,
    cases: &[u8],
    observation: &str,
    workgroups: u32,
    outputs: usize,
) -> Vec<[f32; 4]> {
    use crate::shading::bind::group0;
    let source = format!(
        "{}\n{}",
        crate::shading::compose(&[
            &crate::shading::BIND_LIT,
            &crate::shading::SURFACE,
            &crate::shading::SHADOW_MASK_NONE,
            &crate::shading::tiers::LIT_BASIC,
        ]),
        observation
    );
    let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("surface observations"),
        source: wgpu::ShaderSource::Wgsl(source.into()),
    });
    let storage = |read_only| wgpu::BindingType::Buffer {
        ty: wgpu::BufferBindingType::Storage { read_only },
        has_dynamic_offset: false,
        min_binding_size: None,
    };
    let array = |sample_type| wgpu::BindingType::Texture {
        sample_type,
        view_dimension: wgpu::TextureViewDimension::D2Array,
        multisampled: false,
    };
    // Every binding an observation may read, whether or not it does.
    let entries = [
        (3, storage(true)),
        (4, storage(false)),
        (group0::LIGHTS, storage(true)),
        (group0::LOCAL_SHADOWS, storage(true)),
        (
            group0::FRAME,
            wgpu::BindingType::Buffer {
                ty: wgpu::BufferBindingType::Uniform,
                has_dynamic_offset: false,
                min_binding_size: None,
            },
        ),
        (
            group0::LOOKUP_TABLES,
            array(wgpu::TextureSampleType::Float { filterable: true }),
        ),
        (
            group0::ENVIRONMENT_SAMPLER,
            wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
        ),
        (
            group0::LOCAL_SHADOW_ATLAS,
            array(wgpu::TextureSampleType::Depth),
        ),
        (
            group0::SHADOW_SAMPLER,
            wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Comparison),
        ),
    ];
    let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some("surface observations"),
        entries: &entries.map(|(binding, ty)| wgpu::BindGroupLayoutEntry {
            binding,
            visibility: wgpu::ShaderStages::COMPUTE,
            ty,
            count: None,
        }),
    });
    let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
        label: None,
        layout: Some(
            &device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: None,
                bind_group_layouts: &[Some(&layout)],
                immediate_size: 0,
            }),
        ),
        module: &shader,
        entry_point: Some("observe"),
        compilation_options: Default::default(),
        cache: None,
    });
    let input = crate::scene::buffer(device, "observed cases", cases, wgpu::BufferUsages::STORAGE);
    let output = device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: (outputs * 16) as u64,
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        mapped_at_creation: false,
    });
    let shadows = crate::scene::buffer(
        device,
        "no local shadows",
        bytemuck::bytes_of(&crate::shading::lights::LocalShadowRecord::NONE),
        wgpu::BufferUsages::STORAGE,
    );
    let frame = crate::scene::buffer(
        device,
        "zero frame",
        bytemuck::bytes_of(
            &<crate::shading::uniforms::FrameUniform as bytemuck::Zeroable>::zeroed(),
        ),
        wgpu::BufferUsages::UNIFORM,
    );
    let atlas = device
        .create_texture(&wgpu::TextureDescriptor {
            label: None,
            size: wgpu::Extent3d {
                width: 1,
                height: 1,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Depth32Float,
            usage: wgpu::TextureUsages::TEXTURE_BINDING,
            view_formats: &[],
        })
        .create_view(&wgpu::TextureViewDescriptor {
            dimension: Some(wgpu::TextureViewDimension::D2Array),
            ..Default::default()
        });
    let shadow_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
        compare: Some(wgpu::CompareFunction::GreaterEqual),
        ..Default::default()
    });
    let resources = [
        input.as_entire_binding(),
        output.as_entire_binding(),
        scene.lights.buffer().as_entire_binding(),
        shadows.as_entire_binding(),
        frame.as_entire_binding(),
        wgpu::BindingResource::TextureView(&scene.lookup_tables),
        wgpu::BindingResource::Sampler(&scene.environments.sampler),
        wgpu::BindingResource::TextureView(&atlas),
        wgpu::BindingResource::Sampler(&shadow_sampler),
    ];
    let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &layout,
        entries: &entries
            .iter()
            .zip(resources)
            .map(|(&(binding, _), resource)| wgpu::BindGroupEntry { binding, resource })
            .collect::<Vec<_>>(),
    });
    let mut encoder = device.create_command_encoder(&Default::default());
    {
        let mut pass = encoder.begin_compute_pass(&Default::default());
        pass.set_pipeline(&pipeline);
        pass.set_bind_group(0, &group, &[]);
        pass.dispatch_workgroups(workgroups, 1, 1);
    }
    queue.submit([encoder.finish()]);
    bytemuck::cast_slice::<u32, [f32; 4]>(&read_words(device, queue, &output)).to_vec()
}

/// The directional albedo seen at cosine `nv` of GGX at perceptual roughness
/// `rough` with height-correlated Smith visibility and Schlick's Fresnel,
/// `f0` at normal and `f90` at grazing incidence: the single scattering the
/// split sum stands for, integrated in f64 by importance sampling the GGX
/// distribution of normals over a 2^16-point Hammersley set (within 1e-5 of
/// a 2^20-point one).
pub(crate) fn ggx_albedo(nv: f64, rough: f64, f0: f64, f90: f64) -> f64 {
    use std::f64::consts::PI;
    const SAMPLES: u32 = 1 << 16;
    let a2 = rough.powi(4);
    let view = glam::DVec3::new((1. - nv * nv).max(0.).sqrt(), 0., nv);
    let mut sum = 0.;
    for i in 0..SAMPLES {
        let u = (i as f64 + 0.5) / SAMPLES as f64;
        let v = i.reverse_bits() as f64 / 2f64.powi(32);
        let cos_theta = ((1. - v) / (1. + (a2 - 1.) * v)).sqrt();
        let sin_theta = (1. - cos_theta * cos_theta).sqrt();
        let phi = 2. * PI * u;
        let h = glam::DVec3::new(sin_theta * phi.cos(), sin_theta * phi.sin(), cos_theta);
        let vh = view.dot(h);
        let l = h * 2. * vh - view;
        if l.z <= 0. || vh <= 0. {
            continue;
        }
        let nl = l.z;
        let visibility =
            0.5 / (nl * (nv * nv * (1. - a2) + a2).sqrt() + nv * (nl * nl * (1. - a2) + a2).sqrt());
        let fresnel = f0 + (f90 - f0) * (1. - vh).powi(5);
        // f N.L / pdf, the pdf of l being D (n.h) / (4 v.h).
        sum += fresnel * visibility * 4. * vh * nl / h.z;
    }
    sum / SAMPLES as f64
}

/// The words of `buffer`, a storage buffer that cannot be copied from, as
/// a compute pass reads them.
pub(crate) fn storage_words(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    buffer: &wgpu::Buffer,
) -> Vec<u32> {
    let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("storage readback"),
        source: wgpu::ShaderSource::Wgsl(
            r#"
@group(0) @binding(0) var<storage,read> source:array<u32>;
@group(0) @binding(1) var<storage,read_write> copy:array<u32>;
@compute @workgroup_size(64) fn copy_words(@builtin(global_invocation_id) id:vec3<u32>) {
 let index=id.x+id.y*65535u*64u;
 if index<arrayLength(&source) {
  copy[index]=source[index];
 }
}
"#
            .into(),
        ),
    });
    let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
        label: Some("storage readback"),
        layout: None,
        module: &module,
        entry_point: Some("copy_words"),
        compilation_options: Default::default(),
        cache: None,
    });
    let copy = device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("storage readback"),
        size: buffer.size(),
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        mapped_at_creation: false,
    });
    let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &pipeline.get_bind_group_layout(0),
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: buffer.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: copy.as_entire_binding(),
            },
        ],
    });
    let words = (buffer.size() / 4) as u32;
    let groups = words.div_ceil(64);
    let mut encoder = device.create_command_encoder(&Default::default());
    {
        let mut pass = encoder.begin_compute_pass(&Default::default());
        pass.set_pipeline(&pipeline);
        pass.set_bind_group(0, &group, &[]);
        pass.dispatch_workgroups(groups.min(65535), groups.div_ceil(65535), 1);
    }
    queue.submit([encoder.finish()]);
    read_words(device, queue, &copy)
}

#[path = "../tests/fixtures/ktx2_writer.rs"]
mod ktx2_writer;
pub(crate) use ktx2_writer::{bc7_mode_6, ktx2};

/// Binary16 of a nonnegative finite `value` below 65536, truncated; values
/// below binary16's normal range become zero.
pub(crate) fn to_half(value: f32) -> u16 {
    let bits = value.to_bits();
    let exponent = ((bits >> 23) & 0xff) as i32 - 127 + 15;
    if exponent <= 0 {
        return 0;
    }
    ((exponent as u16) << 10) | ((bits >> 13) & 0x3ff) as u16
}

/// A sampled RGBA16F texture of `size` holding `texels`, rows top to bottom.
pub(crate) fn hdr_texture(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    size: [u32; 2],
    texels: &[[f32; 4]],
) -> wgpu::TextureView {
    let texture = device.create_texture(&wgpu::TextureDescriptor {
        label: Some("fixture HDR input"),
        size: wgpu::Extent3d {
            width: size[0],
            height: size[1],
            depth_or_array_layers: 1,
        },
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format: crate::shading::gbuffer::COLOR,
        usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
        view_formats: &[],
    });
    let halves: Vec<u16> = texels.iter().flatten().map(|&v| to_half(v)).collect();
    queue.write_texture(
        texture.as_image_copy(),
        bytemuck::cast_slice(&halves),
        wgpu::TexelCopyBufferLayout {
            offset: 0,
            bytes_per_row: Some(size[0] * 8),
            rows_per_image: Some(size[1]),
        },
        texture.size(),
    );
    texture.create_view(&Default::default())
}

pub(crate) fn half(bytes: &[u8]) -> f32 {
    let bits = u16::from_le_bytes([bytes[0], bytes[1]]);
    let sign = if bits & 0x8000 == 0 { 1.0 } else { -1.0 };
    let exponent = ((bits >> 10) & 31) as i32;
    let fraction = (bits & 1023) as f32;
    sign * if exponent == 0 {
        fraction * 2f32.powi(-24)
    } else if exponent == 31 {
        if fraction == 0. {
            f32::INFINITY
        } else {
            f32::NAN
        }
    } else {
        (1. + fraction / 1024.) * 2f32.powi(exponent - 15)
    }
}

/// Unit cube geometry owned by tests, independent of any consuming game's asset.
pub(crate) fn cube() -> crate::asset::Asset {
    use crate::asset::{Asset, CpuMesh, Material, Vertex};
    use glam::Vec3;
    let mut vertices = Vec::new();
    let mut indices = Vec::new();
    for (axis, u, v) in [
        (Vec3::X, Vec3::Y, Vec3::Z),
        (Vec3::Y, Vec3::Z, Vec3::X),
        (Vec3::Z, Vec3::X, Vec3::Y),
    ] {
        for sign in [-1., 1.] {
            let start = vertices.len() as u32;
            for (x, y) in [(-1., -1.), (1., -1.), (1., 1.), (-1., 1.)] {
                vertices.push(Vertex {
                    tangent: [0.0; 4],
                    lightmap_bounds: [0., 0., 1., 1.],
                    lightmap_uv: [0.; 2],
                    position: (axis * sign * 0.5 + u * x * 0.5 + v * y * sign * 0.5).to_array(),
                    normal: (axis * sign).to_array(),
                    uv: [(x + 1.) * 0.5, (y + 1.) * 0.5],
                    color: [1.; 4],
                });
            }
            indices.extend([0, 1, 2, 0, 2, 3].map(|index| start + index));
        }
    }
    Asset {
        meshes: vec![CpuMesh {
            vertices,
            indices,
            material: 0,
            deformation: Default::default(),
        }],
        materials: vec![Material {
            anisotropy_strength: 0.0,
            anisotropy_rotation: 0.0,
            anisotropy_texture: None,
            name: "fixture material".into(),
            visibility_group: 0,
            casts_directional_shadow: true,
            base: [0.78, 0.82, 0.85, 1.],
            emissive: [0.; 3],
            metallic: 0.55,
            roughness: 0.35,
            ior: 1.5,
            specular: 1.,
            specular_color: [1.; 3],
            clearcoat: 0.,
            coat_roughness: 0.,
            clearcoat_texture: None,
            coat_roughness_texture: None,
            coat_normal_texture: None,
            coat_normal_scale: 1.,
            iridescence: 0.,
            iridescence_ior: 1.3,
            iridescence_thickness: [100., 400.],
            iridescence_texture: None,
            iridescence_thickness_texture: None,
            sheen_color: [0.; 3],
            sheen_roughness: 0.,
            sheen_color_texture: None,
            sheen_roughness_texture: None,
            diffuse_transmission: 0.,
            diffuse_transmission_color: [1.; 3],
            diffuse_transmission_texture: None,
            diffuse_transmission_color_texture: None,
            base_texture: None,
            mr_texture: None,
            occlusion_texture: None,
            occlusion_strength: 1.,
            emissive_texture: None,
            normal_texture: None,
            normal_scale: 1.,
            normal_layers: None,
            bump_texture: None,
            bump_scale: 0.,
            transmission: 0.,
            transmission_texture: None,
            thickness: 0.,
            thickness_texture: None,
            attenuation_distance: f32::INFINITY,
            attenuation_color: [1.; 3],
            dispersion: 0.,
            wrap: [gltf::texture::WrappingMode::Repeat; 2],
            double_sided: true,
            unlit: false,
            emits_into_gi: true,
            alpha: crate::AlphaMode::Opaque,
        }],
        images: vec![],
        rig: Default::default(),
        ignored: Vec::new(),
    }
}

/// A mesh of one leaf of camera culling's range hierarchy at each of
/// `centres`, in order (`scene::mesh_ranges`): a leaf's worth of small
/// triangles about each centre, so culling keeps or drops each leaf whole.
pub(crate) fn leaf_clusters(centres: &[glam::Vec3]) -> crate::asset::CpuMesh {
    use crate::asset::{CpuMesh, Vertex};
    let mut mesh = CpuMesh {
        vertices: Vec::new(),
        indices: Vec::new(),
        material: 0,
        deformation: Default::default(),
    };
    for centre in centres {
        for _ in 0..crate::scene::mesh_ranges::INDICES_PER_LEAF / 3 {
            for offset in [[-0.1, -0.1], [0.1, -0.1], [0., 0.1]] {
                mesh.indices.push(mesh.vertices.len() as u32);
                mesh.vertices.push(Vertex {
                    tangent: [1., 0., 0., 1.],
                    lightmap_bounds: [0., 0., 1., 1.],
                    lightmap_uv: [0.; 2],
                    position: (*centre + glam::Vec3::new(offset[0], offset[1], 0.)).to_array(),
                    normal: [0., 0., 1.],
                    uv: [0.; 2],
                    color: [1.; 4],
                });
            }
        }
    }
    mesh
}

/// A white 16x16 base map cut out (alpha 0) over its left half, u < 0.5,
/// and opaque over its right half.
pub(crate) fn half_cut_out() -> image::RgbaImage {
    image::RgbaImage::from_fn(16, 16, |x, _| {
        image::Rgba([255, 255, 255, if x < 8 { 0 } else { 255 }])
    })
}

/// `asset` with its first material masked at `cutoff` over the base map
/// `half_cut_out`, which its meshes' UVs map.
pub(crate) fn masked(mut asset: crate::asset::Asset, cutoff: f32) -> crate::asset::Asset {
    asset.images = vec![crate::asset::Image::Rgba8(half_cut_out())];
    asset.materials[0].base_texture = Some(0);
    asset.materials[0].alpha = crate::AlphaMode::Mask { cutoff };
    asset
}

/// A uniform environment: a 4x2 panorama of `panorama` and a 336x64 PMREM
/// atlas (16-texel cube faces) whose RGBA16F texels repeat `rgba16`'s bytes.
pub(crate) fn environment(panorama: [u8; 4], rgba16: &[u8]) -> crate::environment::EnvironmentMap {
    crate::environment::EnvironmentMap {
        panorama: image::RgbaImage::from_pixel(4, 2, image::Rgba(panorama)),
        filtered: crate::environment::PmremAtlas {
            width: 336,
            height: 64,
            rgba16: rgba16.iter().copied().cycle().take(336 * 64 * 8).collect(),
        },
    }
}

/// `asset` added whole and placed at the identity pose as a static instance
/// every view shows.
pub(crate) fn add_static(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    scene: &mut crate::Scene,
    asset: crate::asset::Asset,
) -> (crate::AssetIds, crate::InstanceId) {
    let ids = scene.add_asset(device, queue, asset).unwrap();
    let instance = scene
        .add_instance(
            device,
            queue,
            crate::InstanceState {
                model: ids.model,
                pose: glam::Mat4::IDENTITY,
                visible: true,
                capture_visible: true,
            },
            crate::Mobility::Static,
        )
        .unwrap();
    (ids, instance)
}

/// A device with the renderer's limits and optional features, or `None`
/// after printing why. `SGL_REQUIRE_GPU` turns the skip into a failure.
pub(crate) fn device() -> Option<(wgpu::Device, wgpu::Queue)> {
    device_without(wgpu::Features::empty())
}

/// `device`, without `features` of the renderer's optional ones.
pub(crate) fn device_without(features: wgpu::Features) -> Option<(wgpu::Device, wgpu::Queue)> {
    device_choosing(|adapter| crate::graphics_device::features(adapter) - features)
}

/// `device`, with FSR2's features where the adapter has them.
pub(crate) fn fsr2_device() -> Option<(wgpu::Device, wgpu::Queue)> {
    device_choosing(|adapter| {
        crate::graphics_device::features(adapter) | crate::graphics_device::fsr2_features(adapter)
    })
}

/// A device that traces rays in hardware, with the renderer's limits as
/// `limits` changes them and its optional features, or `None` after printing
/// that the adapter has no ray queries: a test then reports itself
/// unsupported, never passed.
#[allow(unsafe_code)]
pub(crate) fn ray_tracing_device(
    limits: impl FnOnce(wgpu::Limits) -> wgpu::Limits,
) -> Option<(wgpu::Device, wgpu::Queue)> {
    let adapter = adapter()?;
    let ray_tracing = crate::graphics_device::ray_tracing_features(&adapter);
    if ray_tracing.is_empty() {
        eprintln!("unsupported: the adapter has no hardware ray queries");
        return None;
    }
    Some(
        pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
            required_features: (adapter.features() & wgpu::Features::TEXTURE_COMPRESSION_BC)
                | crate::graphics_device::features(&adapter)
                | ray_tracing,
            required_limits: limits(crate::graphics_device::limits(&adapter)),
            // SAFETY: the tests accept wgpu's experimental ray queries.
            experimental_features: unsafe { wgpu::ExperimentalFeatures::enabled() },
            ..Default::default()
        }))
        .unwrap(),
    )
}

/// A device with the renderer's limits and the optional features `features`
/// chooses for the adapter, or `None` after printing why.
pub(crate) fn device_choosing(
    features: impl FnOnce(&wgpu::Adapter) -> wgpu::Features,
) -> Option<(wgpu::Device, wgpu::Queue)> {
    let adapter = adapter()?;
    Some(
        pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
            required_features: (adapter.features() & wgpu::Features::TEXTURE_COMPRESSION_BC)
                | features(&adapter),
            required_limits: crate::graphics_device::limits(&adapter),
            ..Default::default()
        }))
        .unwrap(),
    )
}

/// The default adapter, for a test that requests its own devices, or `None`
/// after printing why. `SGL_REQUIRE_GPU` turns the skip into a failure.
pub(crate) fn adapter() -> Option<wgpu::Adapter> {
    let required = std::env::var("SGL_REQUIRE_GPU").is_ok_and(|v| !v.is_empty() && v != "0");
    match pollster::block_on(wgpu::Instance::default().request_adapter(&Default::default())) {
        Ok(adapter) => Some(adapter),
        Err(error) => {
            assert!(
                !required,
                "GPU test cannot run but SGL_REQUIRE_GPU is set: {error}"
            );
            eprintln!("skipping GPU test: {error}");
            None
        }
    }
}

/// A game's shader for fixtures (`Scene::add_shader`): it moves every vertex
/// along its parameters' `direction` by their `lift`, plus `amplitude` times
/// the sine of `frequency` times the frame's time, plus the vertex's shader
/// data's x and the instance's; its surface function cuts out the fragments
/// at negative world x where `cutting` or the instance's data's z is
/// positive, and adds the scene depth behind the fragment, whether it is
/// available and the instance's data's y to its emission's red, green and
/// blue.
pub(crate) const TEST_SHADER: &str = r#"
struct ShaderParams {
 direction:vec3<f32>,
 lift:f32,
 amplitude:f32,
 frequency:f32,
 cutting:f32,
 unused:f32,
}
fn material_vertex(v:MaterialVertex,ctx:VertexContext,params:ShaderParams)->MaterialVertex {
 var out=v;
 let along=params.lift+params.amplitude*sin(params.frequency*ctx.time)+v.shader_data.x+ctx.instance.x;
 out.position+=params.direction*along;
 return out;
}
fn material_surface(s:MaterialSurface,ctx:SurfaceContext,params:ShaderParams)->MaterialSurface {
 var out=s;
 out.base_color.a=select(s.base_color.a,0.,(params.cutting>0. || ctx.instance.z>0.) && ctx.position.x<0.);
 out.emission=s.emission+vec3(scene_depth_behind(ctx),select(0.,1.,scene_depth_available()),ctx.instance.y);
 return out;
}
"#;

/// A game's shader that reads its volume path, for fixtures: it moves every
/// vertex along its parameters' `direction` by their `lift`; its surface
/// function, through a helper of its own, writes its volume path's length,
/// its bound and whether the fragment is the material's front into its
/// emission's red, green and blue, and shows its front fragments where its
/// instance's data's x is positive and its back fragments where its y is,
/// the others hidden (alpha 0), so either side can be read alone.
pub(crate) const VOLUME_SHADER: &str = r#"
struct ShaderParams {
 direction:vec3<f32>,
 lift:f32,
}
fn material_vertex(v:MaterialVertex,ctx:VertexContext,params:ShaderParams)->MaterialVertex {
 var out=v;
 out.position+=params.direction*params.lift;
 return out;
}
fn volume_measured(ctx:SurfaceContext)->VolumePath {
 return scene_volume_path(ctx);
}
fn material_surface(s:MaterialSurface,ctx:SurfaceContext,params:ShaderParams)->MaterialSurface {
 var out=s;
 let path=volume_measured(ctx);
 let shown=select(ctx.instance.y,ctx.instance.x,ctx.front)>0.;
 out.base_color=vec4(0.,0.,0.,select(0.,1.,shown));
 out.emission=vec3(path.length,f32(path.bound),select(0.,1.,ctx.front));
 return out;
}
"#;

/// `VOLUME_SHADER`'s `ShaderParams`, mirrored.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct VolumeShaderParams {
    pub direction: [f32; 3],
    pub lift: f32,
}

/// `TEST_SHADER`'s `ShaderParams`, mirrored.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct TestShaderParams {
    pub direction: [f32; 3],
    pub lift: f32,
    pub amplitude: f32,
    pub frequency: f32,
    pub cutting: f32,
    pub unused: f32,
}

/// `TEST_SHADER` added to `scene`.
pub(crate) fn add_test_shader(scene: &mut crate::Scene) -> crate::ShaderId {
    scene
        .add_shader(crate::ShaderSource {
            wgsl: TEST_SHADER.into(),
            label: "test shader".into(),
        })
        .unwrap_or_else(|error| panic!("{error}"))
}

/// Draws `material` through `shader`, whose vertices move at most `bound`,
/// as a game names a material's shader: `Scene::set_material`.
pub(crate) fn set_shader(
    scene: &mut crate::Scene,
    queue: &wgpu::Queue,
    material: crate::MaterialId,
    (shader, bound): (crate::ShaderId, f32),
) {
    let values = scene.material(material).unwrap();
    scene
        .set_material(
            queue,
            material,
            crate::SurfaceMaterial {
                shader: Some(crate::MaterialShader {
                    shader,
                    displacement_bound: bound,
                }),
                ..values
            },
        )
        .unwrap_or_else(|error| panic!("{error}"));
}

/// Sets `material`'s `TEST_SHADER` parameters.
pub(crate) fn set_test_params(
    scene: &mut crate::Scene,
    queue: &wgpu::Queue,
    material: crate::MaterialId,
    params: TestShaderParams,
) {
    scene
        .set_shader_parameters(queue, material, bytemuck::bytes_of(&params))
        .unwrap();
}