crcbl-render 0.12.0

Crucible renderer: the 3D frame on crcbl-frame — meshes, materials, lights, effects
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
//! The cluster buffers `mesh_cluster.slang` reads: §3.5's meshlets, resident on
//! the GPU.
//!
//! Four storage buffers and a range per mesh. The **clusters** are
//! [`crcbl_shaders::meshlet::Meshlet`] records back to back in mesh order; the
//! **vertex** array is the original vertex indices, one run per cluster; the
//! **corner** array is `u8` corner indices packed four to a word; and the
//! **selection** array is one
//! [`crcbl_shaders::cluster_select::ClusterSelect`] per cluster — the two groups
//! topic 25's descent tests, and which level of a DAG the cluster's
//! vertices live in. Every one of those shapes is decided by the record, not
//! here — see those modules, which are where both sides of the packing are
//! written.
//!
//! # It receives clusters; it does not build them
//!
//! Topic 03 §3.5 makes the meshlet build a **bake
//! step**, and the layering makes it one whether or not it wants to be:
//! `crcbl_scene::meshlet::build_meshlets` is the builder, `crcbl-scene` pulls in
//! `gltf`, and a renderer that depended on the glTF importer to describe its own
//! geometry would have the direction backwards. So a caller hands this module
//! cooked clusters. [`crcbl_shaders::meshlet::cube_clusters`] and its pyramid
//! siblings are the cooked form of the meshes the forward pass has resident,
//! and `crcbl-scene`'s own test is what checks them against the real builder.
//!
//! # It is only built on the mesh-shader path
//!
//! A device on either indirect tail draws the same geometry out of the index
//! pool, so nothing reads these buffers and [`crate::forward`] does not create
//! them. That is why this is a pool of its own rather than three more fields on
//! [`MeshPool`](crate::mesh_pool::MeshPool): the geometry pools are every
//! device's, and these are one path's.

use crcbl_hal::{BufferDesc, BufferHandle, BufferUsage, Device, HalError, MemoryLocation};
use crcbl_shaders::cluster_select::{self, ClusterSelect};
use crcbl_shaders::meshlet::{self, MeshClusters};

/// Where one mesh's clusters live in [`ClusterPool::clusters`].
///
/// The two numbers a bucket's [`ClusterDrawConstants`] block carries, and the
/// x extent of the dispatch that draws it.
///
/// [`ClusterDrawConstants`]: crcbl_shaders::meshlet::ClusterDrawConstants
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ClusterRange {
    /// This mesh's first cluster.
    pub base: u32,
    /// How many clusters it has. Never zero for a mesh with triangles, which is
    /// what lets the mesh stage read cluster `base` unconditionally.
    pub count: u32,
}

/// Several meshes' cluster arrays laid end to end, before any of it is a
/// buffer.
///
/// Separate from [`ClusterPool::new`] so the arithmetic that shifts a mesh's
/// offsets is testable without a device — which is what a test of it has to be
/// to be a test at all: the seam takes bytes and hands back a handle, so a pool
/// that concatenated wrongly uploads exactly as many bytes as one that did not.
#[derive(Debug, Default, PartialEq)]
struct Concatenated {
    clusters: Vec<meshlet::Meshlet>,
    vertices: Vec<u32>,
    corners: Vec<u8>,
    ranges: Vec<ClusterRange>,
}

/// One mesh's clusters and, if it has a DAG, what each of them needs to descend
/// it.
///
/// A pair rather than a fourth array on [`MeshClusters`], because that type is
/// the *geometry* — it is what `crcbl_scene::meshlet::build_meshlets` produces
/// for a mesh with no hierarchy at all — and selection is
/// `crcbl_shaders::cluster_dag`'s. [`without_lod`](Self::without_lod) is what a
/// caller with only geometry passes, and it is not a special case downstream:
/// [`ClusterSelect::ALWAYS`] draws from every camera, so the pool uploads one
/// record per cluster either way.
#[derive(Clone, Debug, PartialEq)]
pub struct PooledMesh {
    /// The clusters, their vertex runs and their corners.
    pub clusters: MeshClusters,
    /// One [`ClusterSelect`] per cluster, parallel to
    /// [`clusters.clusters`](MeshClusters::clusters).
    pub selection: Vec<ClusterSelect>,
}

impl PooledMesh {
    /// A mesh that is a single level: every cluster drawn whenever the mesh is.
    #[must_use]
    pub fn without_lod(clusters: MeshClusters) -> Self {
        Self {
            selection: vec![ClusterSelect::ALWAYS; clusters.clusters.len()],
            clusters,
        }
    }
}

/// Lays `meshes`' three arrays end to end, moving each cluster's offsets by
/// what came before it.
///
/// # Errors
///
/// [`HalError::InvalidDescriptor`] when an offset outgrows the `u32` the record
/// holds, or when `meshes` cluster into nothing.
fn concatenate(meshes: &[MeshClusters]) -> Result<Concatenated, HalError> {
    let mut out = Concatenated::default();
    for mesh in meshes {
        let base = u32::try_from(out.clusters.len()).map_err(|_| {
            HalError::InvalidDescriptor(
                "more clusters than a u32 can name; the record's offsets are 32-bit".to_string(),
            )
        })?;
        // Every offset in this mesh moves by however much came before it.
        // Rebuilt through `Meshlet::new` rather than added into the existing
        // fields, because that is the constructor that refuses an offset a
        // `u32` cannot hold — and a wrapped one would point at the previous
        // mesh's corners.
        for cluster in &mesh.clusters {
            let shifted = meshlet::Meshlet::new(
                cluster.vertex_offset as usize + out.vertices.len(),
                cluster.vertex_count as usize,
                cluster.triangle_offset as usize + out.corners.len(),
                cluster.triangle_count as usize,
                cluster.bounds,
            )
            .map_err(|error| HalError::InvalidDescriptor(error.to_string()))?;
            out.clusters.push(shifted);
        }
        out.vertices.extend_from_slice(&mesh.vertices);
        out.corners.extend_from_slice(&mesh.corners);
        let count = u32::try_from(mesh.clusters.len())
            .unwrap_or_else(|_| unreachable!("the running total above would have refused first"));
        out.ranges.push(ClusterRange { base, count });
    }
    if out.clusters.is_empty() {
        return Err(HalError::InvalidDescriptor(
            "a cluster pool with no clusters has nothing for a mesh stage to read".to_string(),
        ));
    }
    Ok(out)
}

/// The four cluster buffers, and where each mesh's clusters are in them.
#[derive(Debug)]
pub struct ClusterPool {
    clusters: BufferHandle,
    vertices: BufferHandle,
    corners: BufferHandle,
    selection: BufferHandle,
    ranges: Vec<ClusterRange>,
    count: u32,
}

impl ClusterPool {
    /// Uploads `meshes`' clusters, concatenated in the order given, and returns
    /// the pool plus a range per mesh.
    ///
    /// The three buffers are `HostUpload` rather than device-local with a
    /// staging copy, on [`InstancePool`](crate::instance_pool::InstancePool)'s
    /// terms: they are written once at start-up and read by every frame, and a
    /// staging copy would buy a transfer submit and a barrier for bytes that
    /// never change. A bake cache that streams clusters is what would make them
    /// device-local, and that is §3.5's own later slice.
    ///
    /// # Errors
    ///
    /// [`HalError`] from any seam call, and
    /// [`HalError::InvalidDescriptor`] if `meshes` cluster into nothing — a
    /// pipeline whose cluster buffer is empty has a mesh stage that cannot
    /// safely read element zero, which is the read its out-of-range path makes.
    pub fn new(device: &dyn Device, label: &str, meshes: &[PooledMesh]) -> Result<Self, HalError> {
        let geometry: Vec<MeshClusters> = meshes.iter().map(|mesh| mesh.clusters.clone()).collect();
        let Concatenated {
            clusters,
            vertices,
            corners,
            ranges,
        } = concatenate(&geometry)?;

        // Parallel to the clusters by construction, and refused rather than
        // padded: a selection array short by one leaves a cluster reading its
        // neighbour's groups, which is a level chosen wrong rather than a crash.
        let mut selection = Vec::with_capacity(clusters.len());
        for mesh in meshes {
            if mesh.selection.len() != mesh.clusters.clusters.len() {
                return Err(HalError::InvalidDescriptor(format!(
                    "a mesh of {} clusters carries {} selection records",
                    mesh.clusters.clusters.len(),
                    mesh.selection.len()
                )));
            }
            selection.extend_from_slice(&mesh.selection);
        }

        let mut created: Vec<BufferHandle> = Vec::with_capacity(4);
        let mut upload = |name: &str, bytes: &[u8]| -> Result<BufferHandle, HalError> {
            let buffer = device.create_buffer(&BufferDesc {
                label: Some(&format!("{label} {name}")),
                size: bytes.len() as u64,
                usage: BufferUsage::STORAGE,
                memory: MemoryLocation::HostUpload,
            })?;
            created.push(buffer);
            device.write_buffer(buffer, 0, bytes)?;
            Ok(buffer)
        };
        // Each `?` can leave the ones before it created, so the whole sequence
        // is rolled back on the way out rather than leaked — the same shape
        // every other pool in this crate uses.
        let built = (|| {
            let cluster_buffer = upload("clusters", &meshlet::cluster_bytes(&clusters))?;
            let vertex_bytes: Vec<u8> = vertices.iter().flat_map(|v| v.to_le_bytes()).collect();
            let vertex_buffer = upload("cluster vertices", &vertex_bytes)?;
            let corner_buffer = upload("cluster corners", &meshlet::corner_bytes(&corners))?;
            let selection_buffer = upload(
                "cluster selection",
                &cluster_select::selection_bytes(&selection),
            )?;
            Ok::<_, HalError>((
                cluster_buffer,
                vertex_buffer,
                corner_buffer,
                selection_buffer,
            ))
        })();
        let count = u32::try_from(clusters.len())
            .unwrap_or_else(|_| unreachable!("`concatenate` refused a larger pool first"));
        match built {
            Ok((clusters, vertices, corners, selection)) => Ok(Self {
                clusters,
                vertices,
                corners,
                selection,
                ranges,
                count,
            }),
            Err(error) => {
                for buffer in created {
                    device.destroy_buffer(buffer);
                }
                Err(error)
            }
        }
    }

    /// The cluster records, as `mesh_cluster.slang`'s binding 9.
    #[must_use]
    pub const fn clusters(&self) -> BufferHandle {
        self.clusters
    }

    /// The per-cluster vertex runs, as binding 10.
    #[must_use]
    pub const fn vertices(&self) -> BufferHandle {
        self.vertices
    }

    /// The packed corner words, as binding 11.
    #[must_use]
    pub const fn corners(&self) -> BufferHandle {
        self.corners
    }

    /// The per-cluster selection records, as binding 17.
    #[must_use]
    pub const fn selection(&self) -> BufferHandle {
        self.selection
    }

    /// How many clusters the pool holds in total — the length of the buffer a
    /// frame records its chosen cut into.
    #[must_use]
    pub const fn count(&self) -> u32 {
        self.count
    }

    /// Where mesh `index`'s clusters are, in the order [`ClusterPool::new`]
    /// received them.
    #[must_use]
    pub fn range(&self, index: usize) -> Option<ClusterRange> {
        self.ranges.get(index).copied()
    }

    /// Releases the four buffers. The device must be idle.
    pub fn destroy(self, device: &dyn Device) {
        for buffer in [self.clusters, self.vertices, self.corners, self.selection] {
            device.destroy_buffer(buffer);
        }
    }
}

// `Instance::create_device` is native-only: see the `crcbl_hal::device` module docs.
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;
    use crcbl_hal::null::{NullInstance, Recorder};
    use crcbl_hal::{DeviceDesc, Instance, QueueKind};

    fn open() -> (Recorder, Box<dyn Device>) {
        let recorder = Recorder::new();
        let instance = NullInstance::gpu_driven().with_recorder(recorder.clone());
        let adapter = instance.adapters().remove(0);
        let device = instance
            .create_device(&DeviceDesc::for_adapter(adapter.id))
            .expect("the null backend always opens");
        let _ = device.queue(QueueKind::Graphics);
        (recorder, device)
    }

    /// **A second mesh's clusters address their own runs.** That is the whole
    /// job of concatenating three arrays: every offset in the second mesh moves
    /// by the length of the first's runs, and one that did not would draw the
    /// first mesh's corners under the second mesh's name — same triangle count,
    /// same buffer sizes, wrong geometry.
    ///
    /// Against [`concatenate`] rather than through a device, because a device
    /// cannot see it: the seam takes bytes and hands back a handle, so a pool
    /// that shifted nothing uploads exactly as many bytes as one that did.
    #[test]
    fn the_second_mesh_s_clusters_point_past_the_first_s_runs() {
        let cube = crcbl_shaders::meshlet::cube_clusters();
        let pyramid = crcbl_shaders::meshlet::pyramid_clusters();
        let laid_out = concatenate(&[cube.clone(), pyramid.clone()]).expect("both meshes fit");

        assert_eq!(
            laid_out.ranges,
            vec![
                ClusterRange {
                    base: 0,
                    count: u32::try_from(cube.clusters.len()).expect("small"),
                },
                ClusterRange {
                    base: u32::try_from(cube.clusters.len()).expect("small"),
                    count: u32::try_from(pyramid.clusters.len()).expect("small"),
                },
            ],
            "each mesh's clusters start where the last mesh's ended"
        );

        // The cooked records are mesh-relative — both meshes' first cluster
        // starts at offset zero — which is what makes the shift observable at
        // all rather than a no-op that happens to agree.
        assert_eq!(pyramid.clusters[0].vertex_offset, 0);
        assert_eq!(pyramid.clusters[0].triangle_offset, 0);
        assert!(
            !cube.vertices.is_empty() && !cube.corners.is_empty(),
            "the first mesh has to have runs for the second one's shift to be \
             anything but zero"
        );

        let shifted = laid_out.clusters[cube.clusters.len()];
        assert_eq!(
            shifted.vertex_offset,
            u32::try_from(cube.vertices.len()).expect("small"),
            "the pyramid's vertex run starts where the cube's ended"
        );
        assert_eq!(
            shifted.triangle_offset,
            u32::try_from(cube.corners.len()).expect("small"),
            "and so does its corner run"
        );
        assert_eq!(
            shifted.bounds, pyramid.clusters[0].bounds,
            "the bounds are carried through unchanged — only the offsets move"
        );
        assert_eq!(
            (laid_out.vertices.len(), laid_out.corners.len()),
            (
                cube.vertices.len() + pyramid.vertices.len(),
                cube.corners.len() + pyramid.corners.len()
            ),
            "and both runs hold every entry of both meshes"
        );
    }

    /// The pool uploads what [`concatenate`] produced, at the stride the shader
    /// reads — checked against the writes the recorder saw rather than against
    /// a host-side copy.
    #[test]
    fn the_pool_uploads_every_cluster_of_every_mesh() {
        let (recorder, device) = open();
        let cube = crcbl_shaders::meshlet::cube_clusters();
        let pyramid = crcbl_shaders::meshlet::pyramid_clusters();
        let pool = ClusterPool::new(
            device.as_ref(),
            "test",
            &[
                PooledMesh::without_lod(cube.clone()),
                PooledMesh::without_lod(pyramid.clone()),
            ],
        )
        .expect("the null backend accepts every descriptor");

        assert_eq!(pool.range(2), None, "there is no third mesh");
        let written = |buffer| {
            recorder
                .events()
                .into_iter()
                .find_map(|event| match event {
                    crcbl_hal::null::Event::BufferWritten {
                        buffer: written,
                        len,
                        ..
                    } if written == buffer => Some(len),
                    _ => None,
                })
                .expect("the buffer was written")
        };
        assert_eq!(
            written(pool.clusters()),
            (cube.clusters.len() + pyramid.clusters.len()) * crcbl_shaders::meshlet::MESHLET_STRIDE
        );
        assert_eq!(
            written(pool.vertices()),
            (cube.vertices.len() + pyramid.vertices.len()) * 4
        );
        assert_eq!(
            written(pool.corners()),
            (cube.corners.len() + pyramid.corners.len()).div_ceil(4) * 4,
            "corners pack four to a word, tail included"
        );
        assert_eq!(
            written(pool.selection()),
            (cube.clusters.len() + pyramid.clusters.len())
                * crcbl_shaders::cluster_select::CLUSTER_SELECT_STRIDE,
            "one selection record per cluster of every mesh"
        );
        assert_eq!(
            pool.count(),
            u32::try_from(cube.clusters.len() + pyramid.clusters.len()).expect("small")
        );

        pool.destroy(device.as_ref());
        recorder.assert_valid();
    }

    /// A pool with nothing in it is refused rather than created empty: the mesh
    /// stage reads cluster `cluster_base` on its out-of-range path, and an
    /// empty buffer makes that read out of bounds.
    #[test]
    fn a_pool_with_no_clusters_is_refused() {
        let (_, device) = open();
        let error = ClusterPool::new(device.as_ref(), "test", &[])
            .expect_err("an empty pool has nothing to draw");
        assert!(
            matches!(error, HalError::InvalidDescriptor(ref message) if message.contains("no clusters")),
            "{error}"
        );
    }

    /// Every buffer created is destroyed, which is the leak assertion the rest
    /// of this crate's pools carry.
    #[test]
    fn the_pool_leaks_nothing() {
        let (recorder, device) = open();
        let before = recorder.total_live_objects();
        let pool = ClusterPool::new(
            device.as_ref(),
            "test",
            &[PooledMesh::without_lod(
                crcbl_shaders::meshlet::cube_clusters(),
            )],
        )
        .expect("built");
        assert!(recorder.total_live_objects() > before);
        pool.destroy(device.as_ref());
        assert_eq!(recorder.total_live_objects(), before);
        recorder.assert_valid();
    }
}