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
//! BLASes (the architecture's Hardware ray tracing, *Structures*): one for
//! each model that does not deform and whose class the form traces
//! (`RayClass::traced`), one geometry per mesh over the packed vertices'
//! positions and the mesh's indices where they lie in the ray source,
//! every geometry opaque but, under the candidate form, a masked mesh's, as
//! Bevy b56fc29 builds one per mesh from its mesh allocator's slices
//! (`crates/bevy_solari/src/scene/blas.rs` 55–102, 144–171) and Wicked
//! Engine one per mesh LOD (`wiScene_Components.cpp`,
//! `CreateRaytracingRenderData`); and one for each deforming instance over
//! its deformed positions, built whole again after each deform. A model's
//! BLAS is pending until a frame that builds the structures, which builds
//! the pending ones nearest the camera first under Bevy's budget of
//! vertices (`MOST_VERTICES_PER_FRAME`), the first always, and a replaced
//! or re-pended model's outside it; a built one is then compacted through
//! Bevy's queue under the same budget (`blas.rs` 104–142). A BLAS is built
//! for its geometry and its geometries' opacity under the form in effect,
//! so a material edit that moves a mesh between masked and not re-pends a
//! model's under the candidate form, and a deforming instance's is made
//! again for the opacity of the form in effect, as Wicked Engine rebuilds a
//! BLAS whose materials' opacity changed (`wiScene.cpp` 4232–4247).
use super::{allocated, distance};
use crate::content::identity::{Identity, InstanceId, ModelId};
use crate::scene::instances::{Instance, Instances};
use crate::scene::models::{Model, Models};
use crate::scene::ray_class::RayClass;
use crate::scene::static_edits::posed_bounds;
use crate::shading::RayQueryForm;
use crate::shading::deformation::DEFORMED_POSITION_WORDS;
pub(super) use build::BlasBuild;
use build::{Built, GeometryStart, built_non_opaque, create, fits, model_build, non_opaque, sizes};
use glam::Vec3;
use std::collections::{HashMap, VecDeque};

/// Bevy's `MAX_COMPACTION_VERTICES_PER_FRAME` (`blas.rs` 19–21), which
/// serves the builds too: once a frame has built, or compacted, this many
/// vertices' worth of models, it builds, or compacts, no further one, so
/// the first is always taken whatever its size. A game that turns hardware
/// ray tracing on, or installs a world, ramps its structures in over a few
/// frames, an RD-2 improvement on Bevy, which builds every mesh in the frame
/// it arrives.
pub(super) const MOST_VERTICES_PER_FRAME: u32 = 400_000;

/// A model's BLAS, for its geometry `geometry` (`Model::geometry`).
enum ModelBlas {
    /// Built with the geometries `non_opaque` names not opaque.
    Built {
        blas: wgpu::Blas,
        geometry: u64,
        non_opaque: Vec<bool>,
        #[cfg(any(test, feature = "diagnostics"))]
        triangles: u64,
    },
    /// The device could not hold it: its limits or its memory.
    LeftOut { geometry: u64 },
}

impl ModelBlas {
    /// Whether it is `model`'s as `form` builds it: of its geometry and,
    /// built, its geometries' opacity. The device's limits do not depend on
    /// the opacity.
    fn current(&self, model: &Model, form: RayQueryForm) -> bool {
        match self {
            Self::Built {
                geometry,
                non_opaque: built,
                ..
            } => *geometry == model.geometry && built.iter().copied().eq(non_opaque(model, form)),
            Self::LeftOut { geometry } => *geometry == model.geometry,
        }
    }
}

/// A deforming instance's BLAS, for its model's geometry `geometry`, as of
/// its deformation `revision` (`InstanceDeformation::revision`), built with
/// the geometries `non_opaque` names not opaque.
enum DeformedBlas {
    Built {
        blas: wgpu::Blas,
        geometry: u64,
        revision: u64,
        non_opaque: Vec<bool>,
        #[cfg(any(test, feature = "diagnostics"))]
        triangles: u64,
    },
    LeftOut {
        geometry: u64,
    },
}

/// A built model BLAS waiting to be compacted (Bevy's queue entry): its
/// model's current BLAS, since committing a model's BLAS drops the queue's
/// older entries for it. A re-pend keeps the model's geometry, and wgpu
/// refuses to prepare one BLAS's compaction twice.
struct Compaction {
    model: ModelId,
    vertices: u32,
    started: bool,
}

/// A model whose BLAS is pending: its distance from the camera (its
/// instances' nearest), its vertices, and whether it was replaced, so that
/// it has a BLAS of its old geometry.
#[derive(Clone, Copy)]
struct Pending {
    id: ModelId,
    nearest: f32,
    vertices: u32,
    replaced: bool,
}

/// The pending models a frame builds: every replaced one, outside the
/// budget, so an edited chunk never leaves the TLAS; then the others nearest
/// the camera first while the frame has built fewer than
/// `MOST_VERTICES_PER_FRAME` vertices of them, so the first is always taken.
/// Equally near models go by index, so every run chooses alike.
fn admit(mut pending: Vec<Pending>) -> Vec<ModelId> {
    pending.sort_by(|a, b| {
        b.replaced
            .cmp(&a.replaced)
            .then(a.nearest.total_cmp(&b.nearest))
            .then(a.id.index().cmp(&b.id.index()))
    });
    let mut vertices = 0u32;
    let mut admitted = Vec::new();
    for pending in pending {
        if !pending.replaced {
            if vertices >= MOST_VERTICES_PER_FRAME {
                break;
            }
            vertices = vertices.saturating_add(pending.vertices);
        }
        admitted.push(pending.id);
    }
    admitted
}

/// The BLASes of a scene's models and deforming instances.
#[derive(Default)]
pub(super) struct Blases {
    models: HashMap<ModelId, ModelBlas>,
    deformed: HashMap<InstanceId, DeformedBlas>,
    compaction: VecDeque<Compaction>,
    /// The model BLASes the frame being prepared builds.
    building: HashMap<ModelId, wgpu::Blas>,
}

impl Blases {
    /// Drops the BLASes of removed models and instances, of models whose
    /// class `form` no longer traces (`RayClass::traced`) and of instances
    /// whose model rays pass through.
    pub fn forget_removed(&mut self, models: &Models, instances: &Instances, form: RayQueryForm) {
        self.models.retain(|&id, _| {
            models
                .slots
                .get(id)
                .is_some_and(|model| model.ray_class.traced(form) && model.deformation.is_none())
        });
        self.deformed.retain(|&id, _| {
            instances.slots.get(id).is_some_and(|instance| {
                instance.deformation.is_some()
                    && models
                        .slots
                        .get(instance.state.model)
                        .is_some_and(|model| model.ray_class != RayClass::None)
            })
        });
        self.building.clear();
    }

    /// Bevy's compaction (`blas.rs` 104–142): each built model BLAS in the
    /// queue is prepared for compaction once, and compacted through `queue`
    /// once ready, until the frame has compacted the budget's vertices or
    /// looked at every queued BLAS once; the TLAS takes a compacted BLAS at
    /// its next build. A BLAS the device's memory cannot hold compacted stays
    /// as it was built.
    pub fn compact(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
        let queued = self.compaction.len();
        let mut looked = 0;
        let mut vertices = 0;
        while !self.compaction.is_empty() && vertices < MOST_VERTICES_PER_FRAME && looked < queued {
            looked += 1;
            let mut next = self.compaction.pop_front().expect("a queued BLAS");
            let Some(ModelBlas::Built { blas, .. }) = self.models.get_mut(&next.model) else {
                continue;
            };
            if !next.started {
                blas.prepare_compaction_async(|_| {});
            }
            if blas.ready_for_compaction() {
                if let Some(compacted) = allocated(device, || queue.compact_blas(blas)) {
                    *blas = compacted;
                    crate::counters::blas_compaction(next.vertices);
                    vertices += next.vertices;
                }
                continue;
            }
            next.started = true;
            self.compaction.push_back(next);
        }
    }

    /// The model BLASes the frame builds under `form`, of the models of the
    /// instances `traced` whose BLAS is pending (`admit`): never built, or
    /// built for another geometry or opacity, which re-pends it. A model
    /// the device cannot hold is left out.
    pub fn choose_models(
        &mut self,
        device: &wgpu::Device,
        limits: &wgpu::Limits,
        models: &Models,
        instances: &Instances,
        traced: &[InstanceId],
        (eye, form): (Vec3, RayQueryForm),
    ) -> Vec<BlasBuild> {
        let mut pending: HashMap<ModelId, Pending> = HashMap::new();
        for &id in traced {
            let instance = instances.slots.get(id).expect("a live instance");
            let model_id = instance.state.model;
            let model = models
                .slots
                .get(model_id)
                .expect("an instance's model lives");
            let built = self.models.get(&model_id);
            if built.is_some_and(|blas| blas.current(model, form)) {
                continue;
            }
            let nearest = distance(eye, posed_bounds(model.bounds, instance.state.pose));
            pending
                .entry(model_id)
                .and_modify(|pending| pending.nearest = pending.nearest.min(nearest))
                .or_insert(Pending {
                    id: model_id,
                    nearest,
                    vertices: model.ray_meshes.iter().map(|mesh| mesh.vertex_count).sum(),
                    // A model left out goes back under the budget; a
                    // replaced or re-pended one is built outside it.
                    replaced: matches!(built, Some(ModelBlas::Built { .. })),
                });
        }
        let mut builds = Vec::new();
        let mut held = Vec::with_capacity(pending.len());
        for pending in pending.into_values() {
            let model = models.slots.get(pending.id).expect("a pending model lives");
            if fits(&model.ray_meshes, limits) {
                held.push(pending);
            } else {
                self.leave_out(pending.id, model);
            }
        }
        for id in admit(held) {
            let model = models.slots.get(id).expect("a pending model lives");
            match model_build(device, id, model, form) {
                Some(build) => {
                    self.building.insert(id, build.blas.clone());
                    builds.push(build);
                }
                None => self.leave_out(id, model),
            }
        }
        builds
    }

    /// Leaves model `id`'s current geometry out of the TLAS.
    fn leave_out(&mut self, id: ModelId, model: &Model) {
        self.models.insert(
            id,
            ModelBlas::LeftOut {
                geometry: model.geometry,
            },
        );
    }

    /// The BLAS of model `id`'s current geometry as `form` builds it, built
    /// or building this frame.
    pub fn model(&self, models: &Models, id: ModelId, form: RayQueryForm) -> Option<&wgpu::Blas> {
        if let Some(blas) = self.building.get(&id) {
            return Some(blas);
        }
        let model = models.slots.get(id).expect("a live model");
        match self.models.get(&id) {
            Some(built @ ModelBlas::Built { blas, .. }) if built.current(model, form) => Some(blas),
            _ => None,
        }
    }

    /// Whether the device could not hold model `id`'s current geometry.
    pub fn left_out(&self, models: &Models, id: ModelId) -> bool {
        let geometry = models.slots.get(id).expect("a live model").geometry;
        matches!(self.models.get(&id), Some(ModelBlas::LeftOut { geometry: g }) if *g == geometry)
    }

    /// The BLAS of deforming instance `id` as the frame deforms it under
    /// `form`, adding its build to `builds` when its deformation changed
    /// since it was built: whole, with `PREFER_FAST_BUILD`, outside the
    /// budget, as Wicked Engine rebuilds its skinned meshes' BLASes every
    /// frame (`wiScene.cpp` 4246–4252); a new BLAS where its geometries'
    /// opacity changed, which a BLAS's build cannot. None when the device
    /// cannot hold it.
    pub fn deformed(
        &mut self,
        device: &wgpu::Device,
        limits: &wgpu::Limits,
        models: &Models,
        (id, instance): (InstanceId, &Instance),
        (builds, form): (&mut Vec<BlasBuild>, RayQueryForm),
    ) -> Option<wgpu::Blas> {
        let model = models
            .slots
            .get(instance.state.model)
            .expect("an instance's model lives");
        let deformation = instance.deformation.as_ref().expect("a deforming instance");
        let blas = match self.deformed.get(&id) {
            Some(DeformedBlas::LeftOut { geometry }) if *geometry == model.geometry => {
                return None;
            }
            Some(DeformedBlas::Built {
                blas,
                geometry,
                revision,
                non_opaque: built,
                ..
            }) if *geometry == model.geometry
                && built.iter().copied().eq(non_opaque(model, form)) =>
            {
                if *revision == deformation.revision {
                    return Some(blas.clone());
                }
                blas.clone()
            }
            _ => {
                let sizes = sizes(&model.ray_meshes, non_opaque(model, form));
                let created = fits(&model.ray_meshes, limits)
                    .then(|| {
                        create(
                            device,
                            "scene deformed BLAS",
                            &sizes,
                            wgpu::AccelerationStructureFlags::PREFER_FAST_BUILD,
                        )
                    })
                    .flatten();
                let Some(blas) = created else {
                    self.deformed.insert(
                        id,
                        DeformedBlas::LeftOut {
                            geometry: model.geometry,
                        },
                    );
                    return None;
                };
                blas
            }
        };
        let rig = model.deformation.as_ref().expect("a deforming model");
        let starts = model
            .ray_meshes
            .iter()
            .enumerate()
            .map(|(mesh, words)| {
                let positions = deformation.positions(rig, mesh);
                debug_assert_eq!(
                    positions % DEFORMED_POSITION_WORDS,
                    0,
                    "positions are aligned"
                );
                GeometryStart {
                    first_vertex: positions / DEFORMED_POSITION_WORDS,
                    first_index: words.indices,
                }
            })
            .collect();
        builds.push(BlasBuild {
            built: Built::Deformed {
                id,
                geometry: model.geometry,
                revision: deformation.revision,
            },
            blas: blas.clone(),
            sizes: sizes(&model.ray_meshes, non_opaque(model, form)),
            starts,
            stride: u64::from(DEFORMED_POSITION_WORDS) * 4,
        });
        Some(blas)
    }

    /// Commits a submitted frame's `builds`: each model's BLAS is built and
    /// queued for compaction, each deforming instance's built as of its
    /// deformation.
    pub fn commit(&mut self, builds: Vec<BlasBuild>) {
        for build in builds {
            #[cfg(any(test, feature = "diagnostics"))]
            let triangles = build
                .sizes
                .iter()
                .map(|size| u64::from(size.index_count.unwrap_or(0) / 3))
                .sum();
            let non_opaque = built_non_opaque(&build.sizes);
            match build.built {
                Built::Model {
                    id,
                    geometry,
                    vertices,
                } => {
                    self.models.insert(
                        id,
                        ModelBlas::Built {
                            blas: build.blas,
                            geometry,
                            non_opaque,
                            #[cfg(any(test, feature = "diagnostics"))]
                            triangles,
                        },
                    );
                    self.compaction.retain(|queued| queued.model != id);
                    self.compaction.push_back(Compaction {
                        model: id,
                        vertices,
                        started: false,
                    });
                }
                Built::Deformed {
                    id,
                    geometry,
                    revision,
                } => {
                    self.deformed.insert(
                        id,
                        DeformedBlas::Built {
                            blas: build.blas,
                            geometry,
                            revision,
                            non_opaque,
                            #[cfg(any(test, feature = "diagnostics"))]
                            triangles,
                        },
                    );
                }
            }
        }
        self.building.clear();
    }

    /// The BLASes it holds and their triangles.
    #[cfg(any(test, feature = "diagnostics"))]
    pub fn held(&self) -> (u64, u64) {
        let models = self.models.values().filter_map(|blas| match blas {
            ModelBlas::Built { triangles, .. } => Some(*triangles),
            ModelBlas::LeftOut { .. } => None,
        });
        let deformed = self.deformed.values().filter_map(|blas| match blas {
            DeformedBlas::Built { triangles, .. } => Some(*triangles),
            DeformedBlas::LeftOut { .. } => None,
        });
        models
            .chain(deformed)
            .fold((0, 0), |(count, sum), triangles| {
                (count + 1, sum + triangles)
            })
    }
}

mod build;

#[cfg(test)]
mod tests {
    use super::{MOST_VERTICES_PER_FRAME, Pending, admit};
    use crate::content::identity::{Identity, ModelId};
    use wasm_bindgen_test::wasm_bindgen_test;

    fn pending(index: usize, nearest: f32, vertices: u32, replaced: bool) -> Pending {
        Pending {
            id: ModelId::issue(index, 1),
            nearest,
            vertices,
            replaced,
        }
    }

    // Plausible defects: pending models built farthest first; a first
    // model larger than the budget never taken, so its instances stay off
    // the TLAS for good; the budget ignored, so a world installed at once
    // builds in one frame; or a replaced model held back by the budget, so
    // an edited chunk drops out of a frame's rays. The oracle is the
    // architecture's rule over Bevy's budget loop (`blas.rs` 112–141),
    // which takes a model while the frame has built fewer vertices than the
    // budget.
    #[wasm_bindgen_test(unsupported = test)]
    fn pending_models_are_built_nearest_first_under_the_budget() {
        let budget = MOST_VERTICES_PER_FRAME;
        let ids = |admitted: Vec<ModelId>| -> Vec<usize> {
            admitted.into_iter().map(Identity::index).collect()
        };
        // The first is taken whatever its size.
        assert_eq!(ids(admit(vec![pending(0, 10., budget * 5, false)])), [0]);
        let fresh = [
            pending(1, 30., budget / 4, false),
            pending(2, 10., budget / 4 * 3, false),
            pending(3, 20., budget / 8, false),
            pending(4, 40., budget / 40, false),
        ];
        // Three quarters of the budget, then seven eighths, then past it,
        // after which none is taken.
        assert_eq!(ids(admit(fresh.to_vec())), [2, 3, 1]);
        // A replaced model is taken whatever the budget, and spends none.
        let mut edited = fresh.to_vec();
        edited.push(pending(5, 90., budget * 2, true));
        assert_eq!(ids(admit(edited)), [5, 2, 3, 1]);
    }
}