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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! Block-compressed material images at real GPU boundaries: a KTX2 chain's
//! levels as the device samples them, and level 0 as rays decode it.
use crate::asset::{CompressedImage, CpuMesh, Image, Vertex};
use crate::scene::rays::Query;
use crate::test_support;
use crate::{AlphaMode, Scene, SceneError};
use wgpu::util::DeviceExt;

/// A device that samples BC formats, or `None` when the adapter has none.
fn bc_device() -> Option<(wgpu::Device, wgpu::Queue)> {
    let (device, queue) = test_support::device()?;
    if !device
        .features()
        .contains(wgpu::Features::TEXTURE_COMPRESSION_BC)
    {
        eprintln!("skipping: the adapter has no BC texture compression");
        return None;
    }
    Some((device, queue))
}

/// `count` BC7 blocks from a fixed seed, each of a mode 0..=7 drawn at
/// random with the rest of its bits random (every such block is valid).
fn random_bc7_blocks(count: usize, seed: &mut u64) -> Vec<u8> {
    let mut next = || {
        *seed = seed
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        *seed >> 11
    };
    (0..count)
        .flat_map(|_| {
            let mode = next() % 8;
            let bits = u128::from(next()) | u128::from(next()) << 53 | u128::from(next()) << 106;
            let block = (bits >> (mode + 1) << (mode + 1)) | 1 << mode;
            block.to_le_bytes()
        })
        .collect()
}

/// Level `level` of `image`, decoded on the CPU by bcdec and cropped to the
/// level's texels.
fn cpu_decode(image: &CompressedImage, level: usize) -> ([u32; 2], Vec<u8>) {
    let [width, height] = [image.width, image.height].map(|side| (side >> level).max(1));
    let blocks_wide = width.div_ceil(4) as usize;
    let padded_pitch = blocks_wide * 16;
    let mut padded = vec![0; padded_pitch * height.div_ceil(4) as usize * 4];
    for (index, block) in image.levels[level].chunks_exact(16).enumerate() {
        let at = (index / blocks_wide) * 4 * padded_pitch + (index % blocks_wide) * 16;
        bcdec_rs::bc7(block, &mut padded[at..], padded_pitch);
    }
    let texels = padded
        .chunks_exact(padded_pitch)
        .take(height as usize)
        .flat_map(|row| &row[..width as usize * 4])
        .copied()
        .collect();
    ([width, height], texels)
}

/// Every texel of `view`'s level `level` of `size` texels, as `textureLoad`
/// returns it.
fn gpu_texels(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    view: &wgpu::TextureView,
    level: u32,
    [width, height]: [u32; 2],
) -> Vec<[f32; 4]> {
    let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("compressed level readback"),
        source: wgpu::ShaderSource::Wgsl(
            "@group(0) @binding(0) var map:texture_2d<f32>;
@group(0) @binding(1) var<storage,read_write> texels:array<vec4<f32>>;
@group(0) @binding(2) var<uniform> pick:vec4<u32>;
@compute @workgroup_size(8,8) fn load(@builtin(global_invocation_id) id:vec3<u32>) {
 if id.x>=pick.y || id.y>=pick.z {return;}
 texels[id.y*pick.y+id.x]=textureLoad(map,id.xy,i32(pick.x));
}"
            .into(),
        ),
    });
    let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
        label: None,
        layout: None,
        module: &module,
        entry_point: Some("load"),
        compilation_options: Default::default(),
        cache: None,
    });
    let texels = device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: u64::from(width * height) * 16,
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        mapped_at_creation: false,
    });
    let pick = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
        label: None,
        contents: bytemuck::cast_slice(&[level, width, height, 0]),
        usage: wgpu::BufferUsages::UNIFORM,
    });
    let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &pipeline.get_bind_group_layout(0),
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: wgpu::BindingResource::TextureView(view),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: texels.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 2,
                resource: pick.as_entire_binding(),
            },
        ],
    });
    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(width.div_ceil(8), height.div_ceil(8), 1);
    }
    queue.submit([encoder.finish()]);
    test_support::read_words(device, queue, &texels)
        .chunks_exact(4)
        .map(|texel| std::array::from_fn(|channel| f32::from_bits(texel[channel])))
        .collect()
}

fn srgb_to_linear(byte: u8) -> f32 {
    let x = f32::from(byte) / 255.;
    if x <= 0.04045 {
        x / 12.92
    } else {
        ((x + 0.055) / 1.055).powf(2.4)
    }
}

// A KTX2 file of random BC7 blocks of every mode, Zstandard-supercompressed,
// with a full chain whose lower levels hold partial blocks, added to a scene
// as one material's sRGB base map, linear data map, or both. At every level
// the device samples, through each view a material uses, what bcdec decodes
// from that level's blocks.
#[test]
fn compressed_chain_samples_its_stored_levels() {
    let Some((device, queue)) = bc_device() else {
        return;
    };
    let [width, height] = [20, 12];
    let mut seed = 7;
    let levels: Vec<_> = (0..5)
        .map(|level| {
            let blocks = |side: u32| (side >> level).max(1).div_ceil(4) as usize;
            random_bc7_blocks(blocks(width) * blocks(height), &mut seed)
        })
        .collect();
    let file = test_support::ktx2(ktx2::Format::BC7_SRGB_BLOCK, [width, height], &levels, true);
    let image = CompressedImage::from_ktx2(&file).unwrap();
    assert_eq!(image.levels, levels, "the stored levels, level 0 first");
    for [color, data] in [[true, false], [false, true], [true, true]] {
        let mut material = test_support::cube().materials[0].clone();
        material.base_texture = color.then_some(0);
        material.mr_texture = data.then_some(0);
        let mut scene = Scene::new(&device, &queue);
        scene
            .add_materials(
                &device,
                &queue,
                &[material],
                &[Image::Compressed(image.clone())],
            )
            .unwrap();
        let texture = scene.materials.texture(0);
        assert_eq!(
            [texture.color.is_some(), texture.data.is_some()],
            [color, data]
        );
        for level in 0..levels.len() {
            let (size, expected) = cpu_decode(&image, level);
            if let Some(view) = &texture.data {
                let sampled: Vec<u8> = gpu_texels(&device, &queue, view, level as u32, size)
                    .iter()
                    .flatten()
                    .map(|&value| (value * 255.).round() as u8)
                    .collect();
                assert_eq!(sampled, expected, "level {level} as linear data");
            }
            let Some(view) = &texture.color else {
                continue;
            };
            let sampled = gpu_texels(&device, &queue, view, level as u32, size);
            for (texel, (sampled, bytes)) in
                sampled.iter().zip(expected.chunks_exact(4)).enumerate()
            {
                for channel in 0..4 {
                    let expected = if channel < 3 {
                        srgb_to_linear(bytes[channel])
                    } else {
                        f32::from(bytes[3]) / 255.
                    };
                    assert!(
                        (sampled[channel] - expected).abs() < 1e-3,
                        "level {level} texel {texel} channel {channel} as sRGB colour: {} vs {expected}",
                        sampled[channel]
                    );
                }
            }
        }
    }
}

// A BC7 image of a block for each mode and each value of the six bits after
// its mode bit (a partition of every mode that has them, and every rotation
// and index selection), the rest random, as one material's data map. A ray
// reads each texel of level 0 as the device samples it, from no more than
// the stored blocks: no decoded copy.
#[test]
fn rays_decode_every_bc7_mode_and_partition_as_level_0_samples() {
    let Some((device, queue)) = bc_device() else {
        return;
    };
    let [width, height] = [128, 64];
    let blocks: Vec<u8> = random_bc7_blocks((width / 4 * height / 4) as usize, &mut 11)
        .chunks_exact(16)
        .zip(0u128..)
        .flat_map(|(random, block)| {
            let mode = block % 8;
            let six_bits = block / 8 % 64;
            let random = u128::from_le_bytes(random.try_into().unwrap());
            let bits = (random >> (mode + 7) << (mode + 7)) | six_bits << (mode + 1) | 1 << mode;
            bits.to_le_bytes()
        })
        .collect();
    let image = CompressedImage {
        format: crate::asset::CompressedFormat::Bc7,
        width,
        height,
        levels: vec![blocks],
    };
    let mut material = test_support::cube().materials[0].clone();
    material.mr_texture = Some(0);
    let mut scene = Scene::new(&device, &queue);
    scene
        .add_materials(
            &device,
            &queue,
            &[material],
            &[Image::Compressed(image.clone())],
        )
        .unwrap();
    let texture = scene.materials.texture(0);
    let ray_words = (texture.ray.end - texture.ray.start) as usize;
    assert!(
        ray_words * 4 <= image.levels[0].len() + 16,
        "the ray image takes {ray_words} words for {} bytes of blocks",
        image.levels[0].len()
    );
    let sampled = gpu_texels(
        &device,
        &queue,
        texture.data.as_ref().unwrap(),
        0,
        [width, height],
    );
    let read = ray_texels(&device, &queue, &scene, texture.ray.start, [width, height]);
    let bytes = |texels: &[[f32; 4]]| -> Vec<[u8; 4]> {
        texels
            .iter()
            .map(|texel| texel.map(|value| (value * 255.).round() as u8))
            .collect()
    };
    for (texel, (read, sampled)) in bytes(&read).iter().zip(bytes(&sampled)).enumerate() {
        let [x, y] = [texel as u32 % width, texel as u32 / width];
        assert_eq!(
            *read,
            sampled,
            "texel ({x}, {y}): mode {}",
            (y / 4 * width / 4 + x / 4) % 8
        );
    }
}

/// Every texel of the image at `image_word` in `scene`'s ray source, of
/// `size` texels, as a ray reads it as data.
fn ray_texels(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    scene: &Scene,
    image_word: u32,
    [width, height]: [u32; 2],
) -> Vec<[f32; 4]> {
    let library = crate::shading::compose(&[&crate::shading::SCENE_RAYS]);
    let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("ray image readback"),
        source: wgpu::ShaderSource::Wgsl(
            format!(
                "{library}
@group(0) @binding(0) var<storage,read_write> texels:array<vec4<f32>>;
@group(0) @binding(1) var<uniform> pick:vec4<u32>;
@compute @workgroup_size(8,8) fn read_texels(@builtin(global_invocation_id) id:vec3<u32>) {{
 if id.x>=pick.y || id.y>=pick.z {{return;}}
 texels[id.y*pick.y+id.x]=scene_texel(pick.x,vec2<i32>(id.xy),vec2(0u),false);
}}"
            )
            .into(),
        ),
    });
    let io = 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,
            },
            wgpu::BindGroupLayoutEntry {
                binding: 1,
                visibility: wgpu::ShaderStages::COMPUTE,
                ty: wgpu::BindingType::Buffer {
                    ty: wgpu::BufferBindingType::Uniform,
                    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(&io), Some(&crate::shading::bind::scene(device))],
                immediate_size: 0,
            }),
        ),
        module: &module,
        entry_point: Some("read_texels"),
        compilation_options: Default::default(),
        cache: None,
    });
    let texels = device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: u64::from(width * height) * 16,
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        mapped_at_creation: false,
    });
    let pick = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
        label: None,
        contents: bytemuck::cast_slice(&[image_word, width, height, 0]),
        usage: wgpu::BufferUsages::UNIFORM,
    });
    let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: None,
        layout: &io,
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: texels.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: pick.as_entire_binding(),
            },
        ],
    });
    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.set_bind_group(1, &scene.scene_group, &[]);
        pass.dispatch_workgroups(width.div_ceil(8), height.div_ceil(8), 1);
    }
    queue.submit([encoder.finish()]);
    test_support::read_words(device, queue, &texels)
        .chunks_exact(4)
        .map(|texel| std::array::from_fn(|channel| f32::from_bits(texel[channel])))
        .collect()
}

// The KTX2 reader refuses what it does not read, each a valid file of one
// 2D BC7 image (which it reads) changed in one header field: an array, a
// cube, generated mips, another format, another supercompression.
#[test]
fn ktx2_files_other_than_one_bc7_chain_are_refused() {
    let levels = [random_bc7_blocks(1, &mut 5)];
    let bc7 = |zstd| test_support::ktx2(ktx2::Format::BC7_UNORM_BLOCK, [4; 2], &levels, zstd);
    assert!(CompressedImage::from_ktx2(&bc7(false)).is_ok());
    assert!(CompressedImage::from_ktx2(&bc7(true)).is_ok());
    let with = |field: usize, value: u32| {
        let mut file = bc7(false);
        file[field..field + 4].copy_from_slice(&value.to_le_bytes());
        file
    };
    // Header words: layerCount at byte 32, faceCount 36, levelCount 40 and
    // supercompressionScheme 44 (KTX 2.0 section 3); ZLIB is scheme 3.
    for (case, file) in [
        ("an array", with(32, 2)),
        ("a cube", with(36, 6)),
        ("generated mips", with(40, 0)),
        ("ZLIB supercompression", with(44, 3)),
        (
            "BC6H",
            test_support::ktx2(ktx2::Format::BC6H_UFLOAT_BLOCK, [4; 2], &levels, false),
        ),
    ] {
        assert!(CompressedImage::from_ktx2(&file).is_err(), "{case}");
    }
}

// The scene refuses a chain it cannot upload: sides that are not whole
// blocks, more levels than a full chain, none, or a level of the wrong size.
#[test]
fn a_compressed_chain_the_device_cannot_upload_is_refused() {
    let Some((device, queue)) = bc_device() else {
        return;
    };
    let mut seed = 3;
    let mut chain = |[width, height]: [u32; 2], levels: usize| CompressedImage {
        format: crate::asset::CompressedFormat::Bc7,
        width,
        height,
        levels: (0..levels)
            .map(|level| {
                let blocks = |side: u32| (side >> level).max(1).div_ceil(4) as usize;
                random_bc7_blocks(blocks(width) * blocks(height), &mut seed)
            })
            .collect(),
    };
    let mut short_level = chain([8, 8], 2);
    short_level.levels[1].pop();
    let mut material = test_support::cube().materials[0].clone();
    material.base_texture = Some(0);
    let mut scene = Scene::new(&device, &queue);
    for image in [
        chain([6, 8], 1),
        chain([8, 8], 5),
        chain([8, 8], 0),
        short_level,
    ] {
        assert!(matches!(
            scene.add_materials(
                &device,
                &queue,
                &[material.clone()],
                &[Image::Compressed(image)]
            ),
            Err(SceneError::InvalidCompressedImage)
        ));
    }
    assert!(
        scene
            .add_materials(
                &device,
                &queue,
                &[material],
                &[Image::Compressed(chain([8, 4], 4))]
            )
            .is_ok(),
        "a full chain of a non-square image"
    );
}

// A masked material whose base map is a KTX2 BC7 image cut out texel by
// texel: one mode 6 block per 4x4 texels, its endpoints opaque and clear,
// each texel's index picking one. A ray at each texel's centre hits the
// quad exactly where that texel is opaque, as the portable traversal's
// acceptance predicate decodes the ray source's blocks.
#[test]
fn rays_pass_the_cut_out_texels_of_a_compressed_masked_material() {
    let Some((device, queue)) = bc_device() else {
        return;
    };
    const SIDE: u32 = 8;
    let opaque = |x: u32, y: u32| (x * 3 + y * 5) % 7 < 4;
    let blocks: Vec<u8> = (0..SIDE / 4)
        .flat_map(|block_y| (0..SIDE / 4).map(move |block_x| (block_x, block_y)))
        .flat_map(|(block_x, block_y)| {
            let texel = |i: u32| opaque(block_x * 4 + i % 4, block_y * 4 + i / 4);
            // The anchor texel takes endpoint 0.
            let first = texel(0);
            let alpha = |opaque: bool| if opaque { 127 } else { 0 };
            let indices = std::array::from_fn(|i| if texel(i as u32) == first { 0 } else { 15 });
            test_support::bc7_mode_6(
                [[100, 60, 20, alpha(first)], [100, 60, 20, alpha(!first)]],
                [u8::from(first), u8::from(!first)],
                indices,
            )
        })
        .collect();
    let file = test_support::ktx2(ktx2::Format::BC7_UNORM_BLOCK, [SIDE; 2], &[blocks], false);
    let corners = [[0., 0.], [1., 0.], [1., 1.], [0., 1.]];
    let mut asset = test_support::cube();
    asset.meshes = vec![CpuMesh {
        vertices: corners
            .map(|[x, y]| Vertex {
                position: [x, y, 0.],
                normal: [0., 0., 1.],
                uv: [x, y],
                color: [1.; 4],
                lightmap_uv: [0.; 2],
                lightmap_bounds: [0.; 4],
                tangent: [0.; 4],
            })
            .to_vec(),
        indices: vec![0, 1, 2, 0, 2, 3],
        material: 0,
        deformation: Default::default(),
    }];
    asset.images = vec![Image::Compressed(
        CompressedImage::from_ktx2(&file).unwrap(),
    )];
    asset.materials[0].base_texture = Some(0);
    asset.materials[0].alpha = AlphaMode::Mask { cutoff: 0.5 };
    asset.materials[0].double_sided = true;
    let mut scene = Scene::new(&device, &queue);
    test_support::add_static(&device, &queue, &mut scene, asset);
    scene.update_rays(&device, &queue, !0, false);
    let rays: Vec<[f32; 8]> = (0..SIDE * SIDE)
        .map(|i| {
            let centre = |t: u32| (t as f32 + 0.5) / SIDE as f32;
            [centre(i % SIDE), centre(i / SIDE), 1., 0., 0., 0., -1., 2.]
        })
        .collect();
    let input = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
        label: None,
        contents: bytemuck::cast_slice(&rays),
        usage: wgpu::BufferUsages::STORAGE,
    });
    let hits = device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: input.size(),
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        mapped_at_creation: false,
    });
    let query = Query::new(&device);
    let bindings = query.ray_bind_group(&device, &input, &hits);
    let mut encoder = device.create_command_encoder(&Default::default());
    query.trace(
        &mut encoder,
        &scene.scene_group,
        &bindings,
        rays.len() as u32,
    );
    queue.submit([encoder.finish()]);
    let words = test_support::read_words(&device, &queue, &hits);
    let hit: Vec<bool> = words.chunks_exact(8).map(|hit| hit[0] != 0).collect();
    let expected: Vec<bool> = (0..SIDE * SIDE)
        .map(|i| opaque(i % SIDE, i / SIDE))
        .collect();
    assert_eq!(hit, expected);
}