sgl-3d 0.2.0

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
use super::*;
use crate::settings::ShadowQuality;
use glam::camera;

// Surfaces in a camera view leave specular image-based lighting to the
// reflection stages (as AMD's SSSR sample's scene input does) but keep
// diffuse image-based lighting and emission; capture faces shade with
// environment specular. A nonemissive pure metal has no diffuse term and,
// with the direct light behind it, positive radiance in the camera view would
// be leaked specular image-based lighting. The capture metal, dielectric and
// emissive controls keep a disconnected or black draw from passing.
#[test]
fn camera_surfaces_exclude_specular_ibl_and_keep_diffuse_and_emission() {
    use crate::asset::{CpuMesh, Vertex};
    use crate::settings::Settings;
    use crate::{Camera, DirectionalLight, FrameInput, HemisphereLight, Scene};
    use glam::Vec3;
    let Some((device, queue)) = crate::test_support::device() else {
        return;
    };
    let size = [960u32, 540];
    let mut world = crate::test_support::cube();
    world.meshes = vec![CpuMesh {
        vertices: [[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5]]
            .map(|[x, y]| Vertex {
                tangent: [0.; 4],
                lightmap_uv: [0.; 2],
                lightmap_bounds: [0., 0., 1., 1.],
                position: [x, y, 0.],
                normal: [0., 0., -1.],
                uv: [0.5; 2],
                color: [1.; 4],
            })
            .to_vec(),
        indices: vec![0, 2, 1, 0, 3, 2],
        material: 0,
        deformation: Default::default(),
    }];
    world.materials[0].base = [0.5, 0.5, 0.5, 1.];
    world.materials[0].roughness = 0.3;
    let mut scene = Scene::new(&device, &queue);
    let (world, _) = crate::test_support::add_static(&device, &queue, &mut scene, world);
    // A uniform sky of radiance 1 at every roughness (binary16 0x3c00).
    let environment = scene
        .add_environment(
            &device,
            &queue,
            &crate::test_support::environment([255; 4], &0x3c00u16.to_le_bytes()),
        )
        .unwrap();
    let view = camera::rh::view::look_at_mat4(Vec3::new(0., 0., -2.), Vec3::ZERO, Vec3::Y);
    let mut input = FrameInput::new(Camera {
        view,
        projection: camera::rh::proj::directx::orthographic(-1., 1., -1., 1., 10., 0.1),
        eye: Vec3::new(0., 0., -2.),
    });
    // The sun shines from +Z, behind the plane's -Z normal; a hemisphere
    // fill and the sky light it.
    input.directional_lights[0] = Some(DirectionalLight {
        direction: Vec3::new(-0.2, -0.5, -1.),
        color: [1., 0.8, 0.6],
        illuminance: 3.,
        shadow: None,
        ..Default::default()
    });
    input.hemisphere_light = HemisphereLight {
        sky_color: [0.5, 0.6, 0.8],
        ground_color: [0.2, 0.2, 0.25],
        intensity: 1.,
    };
    input.reflection_environment = input.diffuse_environment;
    input.environment = Some(environment);
    let settings = Settings::default();
    let mut renderer = Renderer::for_test(&device, &queue, size, &settings);
    let target = |label, format| crate::view::targets::target(&device, label, size, format);
    let mut observations = Vec::new();
    let mut metal = Vec::new();
    for (name, capture, metallic, emission) in [
        ("camera metal", false, 1., [0.; 3]),
        ("capture metal", true, 1., [0.; 3]),
        ("camera diffuse", false, 0., [0.; 3]),
        ("camera emission", false, 1., [0.25, 0.5, 1.]),
    ] {
        let mut values = scene.material(world.materials[0]).unwrap();
        values.metallic = metallic;
        values.emission[..3].copy_from_slice(&emission);
        scene
            .set_material(&queue, world.materials[0], values)
            .unwrap();
        let color = target("lit surface", crate::shading::gbuffer::COLOR);
        let motion = target("motion", crate::shading::gbuffer::MOTION);
        let depth = target("depth", crate::shading::gbuffer::DEPTH);
        renderer.prepare_test_frame(&device, &queue, &mut scene, &input, &settings);
        if capture {
            renderer.test_view_as_capture(&queue);
        }
        let mut encoder = device.create_command_encoder(&Default::default());
        {
            let attachment = crate::view::targets::attachment;
            let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("camera and capture surfaces"),
                color_attachments: &[attachment(&color), attachment(&motion)],
                depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
                    view: &depth,
                    depth_ops: Some(wgpu::Operations {
                        load: wgpu::LoadOp::Clear(0.),
                        store: wgpu::StoreOp::Store,
                    }),
                    stencil_ops: None,
                }),
                ..Default::default()
            });
            renderer.draw_static((&device, &queue), &scene, &mut pass);
        }
        queue.submit([encoder.finish()]);
        let bytes = crate::test_support::read(&device, &queue, color.texture(), 8);
        let half = crate::test_support::half;
        let mut mean = [0.; 3];
        for y in 238..302 {
            for x in 448..512 {
                let p = &bytes[(y * 960 + x) * 8..];
                assert_eq!(half(&p[6..]), 1., "fixture was not rasterized");
                for c in 0..3 {
                    let value = half(&p[c * 2..]);
                    assert!(value.is_finite() && value >= 0., "invalid {name} radiance");
                    match name {
                        "camera metal" => metal.push(value),
                        "camera emission" => {
                            // The same metal plus emission, at half precision.
                            let added = value - metal[((y - 238) * 64 + x - 448) * 3 + c];
                            assert!(
                                (added - emission[c]).abs() <= emission[c] / 512.,
                                "emission was altered: {added} != {}",
                                emission[c]
                            )
                        }
                        _ => assert!(value > 0., "missing {name} environment lighting"),
                    }
                    mean[c] += value / 4096.;
                }
            }
        }
        observations.push((name, mean));
    }
    // A metal keeps only multiple scattering here, which uses irradiance: at
    // roughness 0.3 its GGX directional albedo exceeds 0.9, so multiple
    // scattering is under a tenth of the diffuse control's 0.5 albedo.
    // Leaked single-scattering specular (F0 0.5) would match it.
    for c in 0..3 {
        assert!(
            observations[0].1[c] < 0.2 * observations[2].1[c],
            "specular IBL leaked into the camera's surfaces: {observations:?}"
        );
    }
}

// Defects: a capture shades without the scene's lights (its list empty or
// unbound), or takes a baked light where an atlas chart's bake already holds
// it. The oracle is the captured radiance of a scene whose only light is the
// scene light: a black sky and no frame lights, so without the light the
// capture holds nothing but the black floor.
#[test]
fn captures_shade_scene_lights_by_the_ownership_rule() {
    use crate::asset::{CpuMesh, Vertex};
    use crate::baked_specular_probe::SpecularProbeTexels;
    use crate::settings::Settings;
    use crate::{Camera, FrameInput, Light, LightShape, Scene};
    use glam::{Mat4, Vec3};
    let Some((device, queue)) = crate::test_support::device() else {
        return;
    };
    let mut floor = crate::test_support::cube();
    floor.meshes = vec![CpuMesh {
        vertices: [[-2., -2.], [2., -2.], [2., 2.], [-2., 2.]]
            .map(|[x, z]| Vertex {
                tangent: [0.; 4],
                lightmap_uv: [0.5; 2],
                lightmap_bounds: [0., 0., 1., 1.],
                position: [x, -1., z],
                normal: [0., 1., 0.],
                uv: [0.5; 2],
                color: [1.; 4],
            })
            .to_vec(),
        indices: vec![0, 2, 1, 0, 3, 2],
        material: 0,
        deformation: Default::default(),
    }];
    floor.materials[0].metallic = 0.;
    let mut scene = Scene::new(&device, &queue);
    crate::test_support::add_static(&device, &queue, &mut scene, floor);
    // A black atlas whose chart covers the floor.
    scene
        .set_static_irradiance_atlas(
            &device,
            &queue,
            &crate::static_lighting::IrradianceAtlas {
                size: [1, 1],
                irradiance: vec![[0.; 3]],
                back_irradiance: vec![[0.; 3]],
                directionality: Vec::new(),
                back_directionality: Vec::new(),
            },
        )
        .unwrap();
    let input = FrameInput::new(Camera {
        view: Mat4::IDENTITY,
        projection: Mat4::IDENTITY,
        eye: Vec3::ZERO,
    });
    let settings = Settings::default();
    let mut renderer = Renderer::for_test(&device, &queue, [64, 64], &settings);
    let mut captured = |scene: &mut Scene| -> f32 {
        let radiance = renderer
            .capture_specular_probe(&device, &queue, scene, &input, &settings, Vec3::ZERO, 64)
            .unwrap();
        let SpecularProbeTexels::Rgba16Float(texels) = radiance.texels else {
            unreachable!("captures return RGBA16F")
        };
        texels
            .chunks_exact(4)
            .map(|texel| {
                (0..3)
                    .map(|c| crate::test_support::half(&texel[c].to_le_bytes()))
                    .sum::<f32>()
            })
            .sum()
    };
    let dark = captured(&mut scene);
    let light = Light {
        position: Vec3::new(0., -0.5, 0.),
        shape: LightShape::Point {
            radius: LightShape::DEFAULT_RADIUS,
        },
        color: [1.; 3],
        intensity: 4.,
        range: 3.,
        baked: false,
        specular: 1.,
        casts_shadow: false,
        ..Default::default()
    };
    let id = scene.add_light(&device, &queue, light).unwrap();
    let live = captured(&mut scene);
    assert!(
        live > dark + 1.,
        "a live light did not reach the capture: {live} vs {dark}"
    );
    scene
        .set_light(
            &queue,
            id,
            Light {
                baked: true,
                ..light
            },
        )
        .unwrap();
    assert_eq!(
        captured(&mut scene),
        dark,
        "a baked light lit a capture's surface that a chart covers"
    );
}

// A probe capture has no camera: its directional shadow comes from cascades
// about its centre, each surface taking the first that holds it. Plausible
// defects: cascades fit to the frame's camera rather than the capture, a
// face selecting a cascade by its own view depth that does not hold the
// surface, or casters in front of a cascade clipped. The oracle is
// geometric (`floor_under_occluder`).
#[test]
fn a_capture_shadows_what_it_sees_from_cascades_about_its_centre() {
    let Some(gpu) = crate::test_support::device() else {
        return;
    };
    let (shadowed, lit) = floor_under_occluder(gpu, None, ShadowQuality::High);
    assert!(
        lit > 0.05 && shadowed < 0.01 * lit,
        "the floor below the capture is {shadowed} under the occluder and {lit} without"
    );
}

// Plausible defect: a capture at another shadow quality than the frames
// before it reallocates the frame's maps and draws its cascades into the
// new ones, while its lit groups bind the old ones, which hold the last
// frame's cascades about a camera far away. The oracle is geometric
// (`floor_under_occluder`), with the capture after a frame at the other
// quality, each way.
#[test]
fn a_capture_at_another_shadow_quality_shadows_from_its_own_maps() {
    for (frames, capture) in [
        (ShadowQuality::High, ShadowQuality::Low),
        (ShadowQuality::Low, ShadowQuality::High),
    ] {
        let Some(gpu) = crate::test_support::device() else {
            return;
        };
        let (shadowed, lit) = floor_under_occluder(gpu, Some(frames), capture);
        assert!(
            lit > 0.05 && shadowed < 0.01 * lit,
            "a {capture:?} capture after {frames:?} frames: the floor below it is {shadowed} \
             under the occluder and {lit} without"
        );
    }
}

/// The floor a capture records below it, under an occluder and without it,
/// at the shadow quality `capture`, after a frame at `frames` when given: a
/// capture looks down at a floor that an occluder high above it, beyond its
/// cascades' pancake, covers from a light shining down, while the frame's
/// camera is far away, so the floor it records should be dark under the
/// occluder and lit without it.
fn floor_under_occluder(
    (device, queue): (wgpu::Device, wgpu::Queue),
    frames: Option<ShadowQuality>,
    capture: ShadowQuality,
) -> (f32, f32) {
    use crate::asset::CpuMesh;
    use crate::settings::Settings;
    use crate::{Camera, DirectionalLight, DirectionalShadow, FrameInput, Scene};
    use glam::Vec3;
    // A white single-sided square facing +Y at height `y`.
    let square = |y: f32, half: f32| {
        let mut asset = crate::test_support::cube();
        let mut mesh: CpuMesh = asset.meshes.remove(0);
        // The cube's +Y face: vertices 12..16, indices 18..24.
        mesh.vertices = mesh.vertices[12..16]
            .iter()
            .map(|vertex| {
                let [x, _, z] = vertex.position;
                crate::asset::Vertex {
                    position: [x * 2. * half, y, z * 2. * half],
                    ..*vertex
                }
            })
            .collect();
        mesh.indices = vec![0, 1, 2, 0, 2, 3];
        asset.meshes = vec![mesh];
        asset.materials[0].base = [0.8, 0.8, 0.8, 1.];
        asset.materials[0].metallic = 0.;
        asset.materials[0].double_sided = false;
        asset
    };
    // The first cascade's cube reaches 2 m (Godot's first split, 0.1 of the
    // distance) from the capture's centre, 1 m up.
    let shadow = DirectionalShadow {
        distance: 20.,
        cascades: 3,
    };
    let mut scene = Scene::new(&device, &queue);
    crate::test_support::add_static(&device, &queue, &mut scene, square(0., 6.));
    // 10 m above the first cascade's near plane: its cube's top, 3 m up,
    // plus the pancake.
    let (_, occluder) = crate::test_support::add_static(
        &device,
        &queue,
        &mut scene,
        square(13. + crate::view::cascades::SHADOW_PANCAKE_SIZE, 3.),
    );
    let mut input = FrameInput::new(Camera {
        view: camera::rh::view::look_at_mat4(
            Vec3::new(1000., 0., 0.),
            Vec3::new(2000., 0., 0.),
            Vec3::Y,
        ),
        projection: crate::perspective(1., 1., 0.1),
        eye: Vec3::new(1000., 0., 0.),
    });
    input.directional_lights[0] = Some(DirectionalLight {
        direction: Vec3::NEG_Y,
        color: [1.; 3],
        illuminance: 1.,
        shadow: Some(shadow),
        ..Default::default()
    });
    let quality = |shadow_quality| Settings {
        shadow_quality,
        ..Settings::default()
    };
    let settings = quality(capture);
    let mut renderer = Renderer::for_test(
        &device,
        &queue,
        [64, 64],
        &quality(frames.unwrap_or(capture)),
    );
    if let Some(frames) = frames {
        let output = crate::view::targets::target(
            &device,
            "frame before the capture",
            [64, 64],
            crate::shading::gbuffer::COLOR,
        );
        let mut encoder = device.create_command_encoder(&Default::default());
        renderer.render(
            &device,
            &queue,
            &mut encoder,
            &mut scene,
            &input,
            &quality(frames),
            &output,
            None,
        );
        queue.submit([encoder.finish()]);
        renderer.finish_frame(&mut scene);
    }
    let face_size = 64;
    let mut floor = |scene: &mut Scene| {
        let radiance = renderer
            .capture_specular_probe(
                &device,
                &queue,
                scene,
                &input,
                &settings,
                Vec3::Y,
                face_size,
            )
            .unwrap();
        let crate::SpecularProbeTexels::Rgba16Float(texels) = radiance.texels else {
            unreachable!("captures return RGBA16F");
        };
        // The centre of the downward face (-Y, the fourth) at full detail.
        let size = face_size as usize;
        let texel = (3 * size * size + size / 2 * size + size / 2) * 4;
        crate::test_support::half(&texels[texel].to_le_bytes())
    };
    let shadowed = floor(&mut scene);
    let mut state = *scene.instance(occluder).unwrap();
    state.capture_visible = false;
    scene.set_instance(&queue, occluder, state).unwrap();
    (shadowed, floor(&mut scene))
}

// A probe capture shades with the scene's lights' shadows from the static
// layers it places itself: the frame's camera may never have seen those
// lights. Plausible defects: captures bound to no shadows or to the
// frame's atlas, a capture placing no lights, or a capture's static layer
// kept after a static edit removed its caster. The oracle is geometric: a
// capture looks down at a floor that an occluder between it and a casting
// point light covers, while the frame's camera is far away; the floor it
// records is dark under the occluder and lit once the occluder no longer
// casts.
#[test]
fn a_capture_shadows_scene_lights_from_static_layers_it_places() {
    use crate::asset::CpuMesh;
    use crate::settings::Settings;
    use crate::{Camera, FrameInput, Light, LightShape, Scene};
    use glam::Vec3;
    let Some((device, queue)) = crate::test_support::device() else {
        return;
    };
    // A white double-sided square facing +Y at height `y`.
    let square = |y: f32, half: f32| {
        let mut asset = crate::test_support::cube();
        let mut mesh: CpuMesh = asset.meshes.remove(0);
        // The cube's +Y face: vertices 12..16, indices 18..24.
        mesh.vertices = mesh.vertices[12..16]
            .iter()
            .map(|vertex| {
                let [x, _, z] = vertex.position;
                crate::asset::Vertex {
                    position: [x * 2. * half, y, z * 2. * half],
                    ..*vertex
                }
            })
            .collect();
        mesh.indices = vec![0, 1, 2, 0, 2, 3];
        asset.meshes = vec![mesh];
        asset.materials[0].base = [0.8, 0.8, 0.8, 1.];
        asset.materials[0].metallic = 0.;
        asset.materials[0].double_sided = true;
        asset
    };
    let mut scene = Scene::new(&device, &queue);
    crate::test_support::add_static(&device, &queue, &mut scene, square(0., 6.));
    let (_, occluder) =
        crate::test_support::add_static(&device, &queue, &mut scene, square(3., 1.));
    scene
        .add_light(
            &device,
            &queue,
            Light {
                position: Vec3::new(0., 5., 0.),
                shape: LightShape::Point {
                    radius: LightShape::DEFAULT_RADIUS,
                },
                color: [1.; 3],
                intensity: 20.,
                range: 10.,
                baked: false,
                specular: 1.,
                casts_shadow: true,
                ..Default::default()
            },
        )
        .unwrap();
    let input = FrameInput::new(Camera {
        view: camera::rh::view::look_at_mat4(
            Vec3::new(1000., 0., 0.),
            Vec3::new(2000., 0., 0.),
            Vec3::Y,
        ),
        projection: crate::perspective(1., 1., 0.1),
        eye: Vec3::new(1000., 0., 0.),
    });
    let settings = Settings::default();
    let mut renderer = Renderer::for_test(&device, &queue, [64, 64], &settings);
    let face_size = 64;
    let mut floor = |scene: &mut Scene| {
        let radiance = renderer
            .capture_specular_probe(
                &device,
                &queue,
                scene,
                &input,
                &settings,
                Vec3::Y,
                face_size,
            )
            .unwrap();
        let crate::SpecularProbeTexels::Rgba16Float(texels) = radiance.texels else {
            unreachable!("captures return RGBA16F");
        };
        // The centre of the downward face (-Y, the fourth) at full detail.
        let size = face_size as usize;
        let texel = (3 * size * size + size / 2 * size + size / 2) * 4;
        crate::test_support::half(&texels[texel].to_le_bytes())
    };
    let shadowed = floor(&mut scene);
    let mut state = *scene.instance(occluder).unwrap();
    state.capture_visible = false;
    scene.set_instance(&queue, occluder, state).unwrap();
    let lit = floor(&mut scene);
    assert!(
        lit > 0.05 && shadowed < 0.01 * lit,
        "the floor below the capture is {shadowed} under the occluder and {lit} without"
    );
}