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
563
564
565
566
567
568
569
570
571
572
573
574
//! Models: each one's meshes (their positions and indices in the scene's
//! geometry buffers, culling hierarchy, local-light shadow caster clusters,
//! levels of detail), its range of the ray source and a deforming model's
//! deformation, with the `Scene` operations that add, replace and remove
//! them and add a loaded asset whole.
use super::deformation::{InstanceDeformation, ModelDeformation};
use super::geometry::{Elements, GeometryBuffers, GeometryRange};
use super::materials::Materials;
use super::mesh_ranges::MeshRanges;
use super::prepared::{PreparedMesh, PreparedModel};
use super::ray_class::RayClass;
use super::rays::{RayMeshWords, RayModel, SceneRays};
use super::shadow_clusters::{MeshClusters, PosedClusters};
use super::slots::Slots;
use super::static_edits::posed_bounds;
use super::{Scene, SceneError};
use crate::asset::Asset;
use crate::content::identity::{Identity, MaterialId, ModelId};
use crate::content::model::{AssetIds, ModelMesh};
use crate::counters::{BuildStep, step};
use crate::lod::MeshLod;
use glam::Vec3;
use std::ops::Range;

pub(crate) struct Mesh {
    /// Its vertices' positions, which shadow casters read, and its indices.
    pub positions: GeometryRange,
    pub indices: GeometryRange,
    pub count: u32,
    pub material: MaterialId,
    pub ranges: MeshRanges,
    pub lods: Vec<MeshLod>,
    /// Every vertex has an authored tangent frame.
    pub tangents: bool,
    /// None without triangles.
    pub clusters: Option<MeshClusters>,
}

pub(crate) struct Model {
    pub bounds: [Vec3; 2],
    pub meshes: Vec<Mesh>,
    /// Changes when its geometry is replaced.
    pub geometry: u64,
    pub ray: RayModel,
    ray_range: Range<u32>,
    /// Where each mesh's vertices and indices lie in the ray source.
    pub ray_meshes: Vec<RayMeshWords>,
    /// What rays see of it, from its meshes' alpha modes, kept through its
    /// materials' use lists (`Models::classify_users`).
    pub ray_class: RayClass,
    /// Which of its meshes are masked, kept with `ray_class`.
    pub ray_masked: Vec<bool>,
    /// What deforms it; none when it is rigid.
    pub deformation: Option<ModelDeformation>,
    /// Instances showing it.
    pub instances: u32,
    /// Levels of detail naming one of its meshes.
    pub lod_uses: u32,
}

/// A prepared mesh's ranges of the geometry buffers, placed and not yet
/// written.
struct PlacedMesh {
    positions: GeometryRange,
    indices: GeometryRange,
    clusters: Option<GeometryRange>,
}

impl PlacedMesh {
    /// Places `mesh`'s positions, indices and caster clusters' indices in
    /// `geometry`, or nothing when one does not fit.
    fn place(
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        geometry: &mut GeometryBuffers,
        mesh: &PreparedMesh,
    ) -> Result<Self, SceneError> {
        let mut placed = Self {
            positions: GeometryRange::EMPTY,
            indices: GeometryRange::EMPTY,
            clusters: None,
        };
        match placed.fill(device, queue, geometry, mesh) {
            Ok(()) => Ok(placed),
            Err(error) => {
                placed.free(geometry);
                Err(error)
            }
        }
    }

    fn fill(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        geometry: &mut GeometryBuffers,
        mesh: &PreparedMesh,
    ) -> Result<(), SceneError> {
        self.positions =
            geometry.place(device, queue, Elements::Positions, mesh.positions.len())?;
        self.indices = geometry.place(device, queue, Elements::Indices, mesh.indices.len())?;
        if let Some(clustered) = &mesh.clusters {
            let count = clustered.indices.len();
            self.clusters = Some(geometry.place(device, queue, Elements::Indices, count)?);
        }
        Ok(())
    }

    /// Copies `mesh`'s geometry to its ranges.
    fn write(&self, queue: &wgpu::Queue, geometry: &GeometryBuffers, mesh: &PreparedMesh) {
        geometry.write(queue, self.positions, bytemuck::cast_slice(&mesh.positions));
        geometry.write(queue, self.indices, bytemuck::cast_slice(&mesh.indices));
        if let (Some(range), Some(clustered)) = (self.clusters, &mesh.clusters) {
            geometry.write(queue, range, bytemuck::cast_slice(&clustered.indices));
        }
    }

    fn free(&self, geometry: &mut GeometryBuffers) {
        geometry.free(self.positions);
        geometry.free(self.indices);
        if let Some(clusters) = self.clusters {
            geometry.free(clusters);
        }
    }
}

impl Mesh {
    /// Frees its ranges of `geometry`.
    fn free(&self, geometry: &mut GeometryBuffers) {
        geometry.free(self.positions);
        geometry.free(self.indices);
        if let Some(clusters) = &self.clusters {
            geometry.free(clusters.indices);
        }
    }
}

impl Model {
    /// Each mesh's caster clusters' and groups' world bounds at `pose`, in
    /// mesh order.
    pub fn caster_bounds(&self, pose: glam::Mat4) -> Vec<PosedClusters> {
        self.meshes
            .iter()
            .map(|mesh| {
                mesh.clusters
                    .as_ref()
                    .map_or_else(PosedClusters::default, |clusters| clusters.posed(pose))
            })
            .collect()
    }

    /// Whether it may be removed, or replaced (`replacing`).
    fn in_use(&self, replacing: bool) -> bool {
        self.lod_uses > 0 || (!replacing && self.instances > 0)
    }
}

/// A mesh with `tangents` drawn with a material of `anisotropy` strength.
fn validate_tangents(anisotropy: f32, tangents: bool) -> Result<(), SceneError> {
    if anisotropy > 0. && !tangents {
        return Err(SceneError::MissingAnisotropyTangents);
    }
    Ok(())
}

#[derive(Default)]
pub(crate) struct Models {
    pub slots: Slots<ModelId, Model>,
}

impl Models {
    pub fn get(&self, id: ModelId) -> Result<&Model, SceneError> {
        self.slots.get(id).ok_or(SceneError::UnknownModel)
    }

    pub fn get_mut(&mut self, id: ModelId) -> Result<&mut Model, SceneError> {
        self.slots.get_mut(id).ok_or(SceneError::UnknownModel)
    }

    /// Whether a model deforms.
    pub fn holds_deforming(&self) -> bool {
        self.slots
            .iter()
            .any(|(_, model)| model.deformation.is_some())
    }

    /// Whether `lod`'s mesh has authored tangent frames.
    pub fn lod_tangents(&self, lod: &MeshLod) -> bool {
        self.slots
            .get(lod.model)
            .expect("a level of detail's model lives")
            .meshes[lod.mesh]
            .tangents
    }

    /// Ends `model`'s uses of other content: its levels of detail's models
    /// and its meshes' materials.
    fn release(&mut self, materials: &mut Materials, model: ModelId, meshes: &[Mesh]) {
        for mesh in meshes {
            let material = materials
                .get_mut(mesh.material)
                .expect("a used material lives");
            let uses = material
                .users
                .get_mut(&model)
                .expect("a user of its material");
            *uses -= 1;
            if *uses == 0 {
                material.users.remove(&model);
            }
            material.untangented -= u32::from(!mesh.tangents);
        }
        for mesh in meshes {
            self.clear_lods(materials, mesh.material, &mesh.lods);
        }
    }

    /// Ends `lods`' uses of their models and of `material`'s tangent count.
    pub fn clear_lods(
        &mut self,
        materials: &mut Materials,
        material: MaterialId,
        lods: &[MeshLod],
    ) {
        for lod in lods {
            let untangented = !self.lod_tangents(lod);
            self.slots
                .get_mut(lod.model)
                .expect("a level of detail's model lives")
                .lod_uses -= 1;
            materials
                .get_mut(material)
                .expect("a used material lives")
                .untangented -= u32::from(untangented);
        }
    }

    /// Prepared `model` placed and written: a model which nothing uses yet
    /// and which does not use its materials yet. Placing it checks it
    /// against `materials` and the device, allocates its ranges and rebases
    /// the words that address them; on failure nothing stays placed.
    fn place(
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        (rays, geometry): (&mut SceneRays, &mut GeometryBuffers),
        materials: &Materials,
        mut model: PreparedModel,
    ) -> Result<Model, SceneError> {
        let (words, deformation, placed) = step(BuildStep::Place, || {
            // Geometry beyond the device's limits fails where it is placed:
            // the ray source and the geometry slabs refuse what they cannot
            // hold.
            let mut material_words = Vec::with_capacity(model.meshes.len());
            for mesh in &model.meshes {
                let material = materials.get(mesh.material)?;
                validate_tangents(material.values.anisotropy_strength, mesh.tangents)?;
                material_words.push(material.word());
            }
            if model
                .deformation
                .as_ref()
                .is_some_and(|deformation| !deformation.dispatchable(device))
            {
                return Err(SceneError::DeviceLimit);
            }
            let words = rays.place_model(device, queue, &mut model.rays, &material_words)?;
            let vertices: Vec<u32> = words.meshes.iter().map(|mesh| mesh.vertices).collect();
            let deformation =
                match model.deformation.take().map(|prepared| {
                    ModelDeformation::place(device, queue, rays, prepared, &vertices)
                }) {
                    Some(Ok(placed)) => Some(placed),
                    Some(Err(error)) => {
                        rays.free(words.range);
                        return Err(error);
                    }
                    None => None,
                };
            let mut placed = Vec::with_capacity(model.meshes.len());
            for mesh in &model.meshes {
                match PlacedMesh::place(device, queue, geometry, mesh) {
                    Ok(mesh) => placed.push(mesh),
                    Err(error) => {
                        for mesh in &placed {
                            mesh.free(geometry);
                        }
                        if let Some((deformation, _)) = deformation {
                            deformation.free(rays);
                        }
                        rays.free(words.range);
                        return Err(error);
                    }
                }
            }
            Ok((words, deformation, placed))
        })?;
        step(BuildStep::Write, || {
            rays.write(queue, words.range.start, model.rays.words());
            if let Some((deformation, words)) = &deformation {
                rays.write(queue, deformation.word(), words);
            }
            for (placed, mesh) in placed.iter().zip(&model.meshes) {
                placed.write(queue, geometry, mesh);
            }
        });
        let meshes = model
            .meshes
            .into_iter()
            .zip(placed)
            .map(|(mesh, placed)| Mesh {
                positions: placed.positions,
                indices: placed.indices,
                count: mesh.indices.len() as u32,
                material: mesh.material,
                ranges: mesh.ranges,
                lods: Vec::new(),
                tangents: mesh.tangents,
                clusters: mesh
                    .clusters
                    .zip(placed.clusters)
                    .map(|(clustered, indices)| MeshClusters {
                        indices,
                        clusters: clustered.clusters,
                        groups: clustered.groups,
                    }),
            })
            .collect();
        let mut built = Model {
            bounds: model.bounds,
            meshes,
            geometry: super::next_generation(),
            ray: words.ray,
            ray_range: words.range,
            ray_meshes: words.meshes,
            ray_class: RayClass::None,
            ray_masked: Vec::new(),
            deformation: deformation.map(|(deformation, _)| deformation),
            instances: 0,
            lod_uses: 0,
        };
        super::ray_class::classify(&mut built, materials);
        Ok(built)
    }

    /// Frees a model's words in the ray source and its meshes' ranges of the
    /// geometry buffers.
    fn free(
        rays: &mut SceneRays,
        geometry: &mut GeometryBuffers,
        (range, deformation, meshes): (Range<u32>, Option<ModelDeformation>, &[Mesh]),
    ) {
        rays.free(range);
        if let Some(deformation) = deformation {
            deformation.free(rays);
        }
        for mesh in meshes {
            mesh.free(geometry);
        }
    }

    /// `model`'s `meshes` now use their materials.
    fn take_materials(materials: &mut Materials, model: ModelId, meshes: &[Mesh]) {
        for mesh in meshes {
            let material = materials
                .get_mut(mesh.material)
                .expect("a validated material");
            *material.users.entry(model).or_default() += 1;
            material.untangented += u32::from(!mesh.tangents);
        }
    }
}

impl Scene {
    /// Adds a prepared model (`PreparedModel::new`): an ordered list of
    /// meshes, each drawn with a material already in the scene. It only
    /// places the model's ranges and copies its geometry to the queue.
    pub fn add_model(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        model: PreparedModel,
    ) -> Result<ModelId, SceneError> {
        self.edited();
        let model = Models::place(
            device,
            queue,
            (&mut self.rays, &mut self.geometry),
            &self.materials,
            model,
        );
        self.refresh_scene_group(device);
        let id = self.models.slots.insert(model?);
        let meshes = &self
            .models
            .slots
            .get(id)
            .expect("a model just added")
            .meshes;
        Models::take_materials(&mut self.materials, id, meshes);
        Ok(id)
    }

    /// Replaces a model's whole geometry with a prepared model's, with any
    /// vertex and index counts, none included, and clears the levels of
    /// detail registered on it. A model that is a level of detail cannot be
    /// replaced.
    pub fn set_model(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        id: ModelId,
        model: PreparedModel,
    ) -> Result<(), SceneError> {
        self.edited();
        if self.models.get(id)?.in_use(true) {
            return Err(SceneError::ModelInUse);
        }
        if model.deformation.is_some() && self.instances.static_poses(id).next().is_some() {
            return Err(SceneError::DeformingModel);
        }
        let built = Models::place(
            device,
            queue,
            (&mut self.rays, &mut self.geometry),
            &self.materials,
            model,
        );
        // The moving instances showing it deform as the new geometry does,
        // from its bind pose.
        let posed = built.and_then(|built| {
            let meshes = self.candidate_meshes(id, &built);
            let deforms = Some(built.deformation.is_some());
            if !self.candidates_fit(&[(id, &meshes, deforms)]) {
                Models::free(
                    &mut self.rays,
                    &mut self.geometry,
                    (built.ray_range, built.deformation, &built.meshes),
                );
                return Err(SceneError::DeviceLimit);
            }
            let mut posed = Vec::new();
            if let Some(deformation) = &built.deformation {
                for (instance, _) in self
                    .instances
                    .slots
                    .iter()
                    .filter(|(_, i)| i.state.model == id)
                {
                    match InstanceDeformation::add(device, queue, &mut self.rays, deformation) {
                        Ok(added) => posed.push((instance, added)),
                        Err(error) => {
                            for (_, added) in posed {
                                InstanceDeformation::free(added, &mut self.rays);
                            }
                            Models::free(
                                &mut self.rays,
                                &mut self.geometry,
                                (built.ray_range, built.deformation, &built.meshes),
                            );
                            return Err(error);
                        }
                    }
                }
            }
            Ok((built, posed))
        });
        self.refresh_scene_group(device);
        let (built, posed) = posed?;
        for (_, instance) in self.instances.slots.iter_mut() {
            if instance.state.model == id
                && let Some(previous) = instance.deformation.take()
            {
                previous.free(&mut self.rays);
            }
        }
        for (instance, deformation) in posed {
            self.instances.slots.get_mut(instance).unwrap().deformation = Some(deformation);
        }
        Models::take_materials(&mut self.materials, id, &built.meshes);
        let model = self.models.get_mut(id).unwrap();
        // Replacing the geometry a static instance shows is a static edit.
        for pose in self.instances.static_poses(id) {
            self.static_edits.record(posed_bounds(model.bounds, pose));
            self.static_edits.record(posed_bounds(built.bounds, pose));
        }
        let previous = std::mem::replace(&mut model.meshes, built.meshes);
        model.bounds = built.bounds;
        model.geometry = built.geometry;
        model.ray = built.ray;
        model.ray_meshes = built.ray_meshes;
        model.ray_class = built.ray_class;
        model.ray_masked = built.ray_masked;
        let previous_range = std::mem::replace(&mut model.ray_range, built.ray_range);
        let previous_deformation = std::mem::replace(&mut model.deformation, built.deformation);
        self.models.release(&mut self.materials, id, &previous);
        Models::free(
            &mut self.rays,
            &mut self.geometry,
            (previous_range, previous_deformation, &previous),
        );
        // Its candidates name its new meshes; its alternatives are cleared.
        // Its instances' records say whether they deform now.
        self.candidates.remove_chains(id);
        self.place_candidates_of(&[id]);
        self.instances.write_of_model(queue, id);
        let model = self.models.get(id).unwrap();
        self.instances.pose_casters(model, id);
        // Rays see its instances' new geometry from their entries.
        for (instance, shown) in self.instances.slots.iter() {
            if shown.state.model == id {
                self.ray_instances
                    .set(instance.index(), model.ray, shown.state.pose);
            }
        }
        Ok(())
    }

    /// Removes a model no instance uses and that is no level of detail.
    pub fn remove_model(&mut self, id: ModelId) -> Result<(), SceneError> {
        self.edited();
        if self.models.get(id)?.in_use(false) {
            return Err(SceneError::ModelInUse);
        }
        let model = self.models.slots.remove(id).unwrap();
        self.candidates.remove_chains(id);
        self.models.release(&mut self.materials, id, &model.meshes);
        Models::free(
            &mut self.rays,
            &mut self.geometry,
            (model.ray_range, model.deformation, &model.meshes),
        );
        Ok(())
    }

    /// Adds a loaded asset whole: its materials and images, then its meshes,
    /// prepared here, as one model. Its own indices do not outlive this
    /// call. On failure nothing remains added.
    pub fn add_asset(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        asset: Asset,
    ) -> Result<AssetIds, SceneError> {
        self.edited();
        if asset
            .meshes
            .iter()
            .any(|mesh| mesh.material >= asset.materials.len())
        {
            return Err(SceneError::MissingMaterial);
        }
        let materials = self.add_materials(device, queue, &asset.materials, &asset.images)?;
        let meshes = asset
            .meshes
            .into_iter()
            .map(|mesh| ModelMesh {
                vertices: mesh.vertices,
                indices: mesh.indices,
                material: materials[mesh.material],
                deformation: mesh.deformation,
            })
            .collect();
        match PreparedModel::new(meshes).and_then(|model| self.add_model(device, queue, model)) {
            Ok(model) => Ok(AssetIds { materials, model }),
            Err(error) => {
                for &material in &materials {
                    self.remove_material(material)
                        .expect("a material just added has no users");
                }
                Err(error)
            }
        }
    }
}