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
//! 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()
}

/// 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 = "../examples/support/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,
            clearcoat: 0.,
            coat_roughness: 0.,
            base_texture: None,
            mr_texture: None,
            emissive_texture: None,
            normal_texture: None,
            normal_scale: 1.,
            normal_layers: None,
            bump_texture: None,
            bump_scale: 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(),
    }
}

/// 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.
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
        }
    }
}