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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! The scene layer: [`Scene`], the retained content a game adds, edits and
//! removes (shaders, materials, models, instances, lights, decals, environments,
//! baked lighting, probes, the irradiance volume, the dynamic GI volume's
//! placement and transient geometry), with the GPU buffers that mirror it
//! and the ray-query structures built from its geometry. Nothing here
//! depends on a camera, an output size or a quality setting, but the
//! hardware path's acceleration structures, which a frame asks the scene to
//! build while hardware ray tracing is in effect, nearest its camera first
//! (`rays::acceleration`).
pub(crate) mod candidates;
mod decal_atlas;
pub(crate) mod decals;
pub(crate) mod deformation;
pub(crate) mod dynamic_gi;
pub(crate) mod environments;
pub(crate) mod error;
pub(crate) mod geometry;
pub(crate) mod instances;
pub(crate) mod irradiance_volume;
mod lattice;
pub(crate) mod lights;
pub(crate) mod lod;
pub(crate) mod lookup_tables;
pub(crate) mod materials;
pub(crate) mod mesh_ranges;
pub(crate) mod models;
pub(crate) mod objects;
pub(crate) mod origin;
pub(crate) mod prepared;
pub(crate) mod probe_grid;
pub(crate) mod probes;
pub(crate) mod ranges;
pub(crate) mod ray_class;
pub(crate) mod rays;
pub(crate) mod shaders;
pub(crate) mod shadow_clusters;
mod slots;
pub(crate) mod static_edits;
pub(crate) mod static_lighting;
mod textures;
pub(crate) mod transient;

pub use error::SceneError;

/// How a model's shaders may change what it casts (`Scene::caster_shading`).
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct CasterShading {
    /// One of them reads the frame's time.
    pub reads_time: bool,
    /// The latest of their materials' parameter revisions.
    pub parameters: u64,
}
pub use prepared::PreparedModel;

use crate::content::identity::{Identity, MaterialId, ModelId};
use crate::content::instance::Mobility;
use std::sync::atomic::{AtomicU64, Ordering};

/// Retained content and the GPU buffers mirroring it, which a
/// [`Renderer`](crate::Renderer) renders. A scene starts empty; the game
/// adds, edits and removes content between frames, and each addition
/// returns the content's identity. Content only: rendering settings are
/// `settings::Settings` and each frame's camera and look are a
/// [`FrameInput`](crate::FrameInput). All dimensions are metres, Y-up.
pub struct Scene {
    pub(crate) shaders: shaders::Shaders,
    pub(crate) materials: materials::Materials,
    pub(crate) models: models::Models,
    pub(crate) instances: instances::Instances,
    /// Each instance's draw candidates, with the sets and level chains they
    /// name, which the GPU draw lists cull.
    pub(crate) candidates: candidates::Candidates,
    pub(crate) lights: lights::Lights,
    pub(crate) decals: decals::Decals,
    pub(crate) environments: environments::Environments,
    /// Lit group 0's lookup tables (`lookup_tables`).
    pub(crate) lookup_tables: wgpu::TextureView,
    pub(crate) rays: rays::SceneRays,
    /// What shadow casters draw from vertex and index buffers.
    pub(crate) geometry: geometry::GeometryBuffers,
    /// The ray source's instance entries and instance BVHs.
    pub(crate) ray_instances: rays::instances::RayInstances,
    /// The hardware path's acceleration structures, while hardware ray
    /// tracing is in effect on a device that has it; none until a frame
    /// builds them and after one runs with the setting off.
    acceleration: Option<rays::acceleration::AccelerationStructures>,
    /// Group 1: the object records and the ray buffers.
    pub(crate) scene_group: wgpu::BindGroup,
    /// The buffers `scene_group` binds: objects, ray source, ray instances.
    bound: [wgpu::Buffer; 4],
    scene_layout: wgpu::BindGroupLayout,
    pub(crate) static_lighting: static_lighting::StaticLighting,
    baked_specular_probes: Option<probes::UploadedProbes>,
    /// The irradiance volume (`Scene::set_irradiance_volume`).
    pub(crate) irradiance_cells: irradiance_volume::IrradianceCells,
    /// The dynamic GI volume's placement (`Scene::set_dynamic_gi_volume`).
    pub(crate) dynamic_gi: Option<dynamic_gi::InstalledVolume>,
    /// This scene among every scene created: a renderer given another scene
    /// restarts its history.
    pub(crate) id: u64,
    /// Changes whenever a resource renderers bind in group 0 is replaced
    /// (lights, decals, the decal atlas, lightmap, irradiance atlas,
    /// specular probes, the irradiance volume's texture).
    pub(crate) resources: u64,
    /// Changes with every edit to the content the dynamic GI volume's rays
    /// see and light (`Scene::edited`): each public edit that changes
    /// something, but the transient effects' and a deforming instance's
    /// pose and deformation, which the portable path's rays do not see
    /// (`deformation_edits`). A converged volume pauses while it holds
    /// still.
    pub(crate) edits: u64,
    /// Changes with every edit of a capture-visible deforming instance's
    /// pose or deformation, which rays see while the hardware path traces
    /// them, so the dynamic GI volume's pause counts them then.
    pub(crate) deformation_edits: u64,
    /// Caller-authored glow, heat and mist geometry.
    pub(crate) transient: transient::Transient,
    /// The world bounds static edits touched since the last submitted frame.
    pub(crate) static_edits: static_edits::StaticEdits,
    /// The deformations the frame being rendered writes (the deform stage).
    pub(crate) deformations: Vec<crate::shading::deformation::DeformDispatch>,
    /// Where the render origin lies in the frame the scene was created in
    /// (`origin`).
    origin: glam::DVec3,
}

/// Group 1 (`shading::bind::scene`) over `objects`, `source`, `instances`
/// and the instances' `shader_data`.
fn scene_group(
    device: &wgpu::Device,
    layout: &wgpu::BindGroupLayout,
    [objects, source, instances, shader_data]: &[wgpu::Buffer; 4],
) -> wgpu::BindGroup {
    use crate::shading::bind::group1;
    device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("scene objects and rays"),
        layout,
        entries: &[
            wgpu::BindGroupEntry {
                binding: group1::OBJECTS,
                resource: objects.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: group1::SCENE_SOURCE,
                resource: source.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: group1::SCENE_INSTANCES,
                resource: instances.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: group1::OBJECT_SHADER_DATA,
                resource: shader_data.as_entire_binding(),
            },
        ],
    })
}

impl Scene {
    /// An empty scene.
    pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
        let scene_layout = crate::shading::bind::scene(device);
        let instances = instances::Instances::new(device);
        let rays = rays::SceneRays::new(device);
        let ray_instances = rays::instances::RayInstances::new(device);
        let bound = Self::buffers(&instances, &rays, &ray_instances);
        Self {
            shaders: shaders::Shaders::default(),
            materials: materials::Materials::new(device, queue),
            models: models::Models::default(),
            candidates: candidates::Candidates::new(&device.limits()),
            lights: lights::Lights::new(device),
            decals: decals::Decals::new(device, queue),
            environments: environments::Environments::new(device, queue),
            lookup_tables: lookup_tables::lookup_tables(device, queue),
            ray_instances,
            acceleration: None,
            scene_group: scene_group(device, &scene_layout, &bound),
            bound,
            scene_layout,
            instances,
            rays,
            geometry: geometry::GeometryBuffers::new(device),
            static_lighting: static_lighting::StaticLighting::empty(device, queue),
            baked_specular_probes: None,
            irradiance_cells: irradiance_volume::IrradianceCells::new(device),
            dynamic_gi: None,
            id: next_generation(),
            resources: next_generation(),
            edits: 0,
            deformation_edits: 0,
            transient: transient::Transient::new(device),
            static_edits: static_edits::StaticEdits::default(),
            deformations: Vec::new(),
            origin: glam::DVec3::ZERO,
        }
    }

    /// Records an edit to content the dynamic GI volume sees (`edits`).
    pub(crate) fn edited(&mut self) {
        self.edits = self.edits.wrapping_add(1);
    }

    /// Records an edit of a capture-visible deforming instance
    /// (`deformation_edits`).
    pub(crate) fn deformation_edited(&mut self) {
        self.deformation_edits = self.deformation_edits.wrapping_add(1);
    }

    fn buffers(
        instances: &instances::Instances,
        rays: &rays::SceneRays,
        ray_instances: &rays::instances::RayInstances,
    ) -> [wgpu::Buffer; 4] {
        [
            instances.objects.buffer().clone(),
            rays.source().clone(),
            ray_instances.buffer().clone(),
            instances.objects.shader_data().clone(),
        ]
    }

    /// Rebuilds group 1 after content growth replaced a buffer it binds.
    fn refresh_scene_group(&mut self, device: &wgpu::Device) {
        let buffers = Self::buffers(&self.instances, &self.rays, &self.ray_instances);
        if buffers != self.bound {
            self.scene_group = scene_group(device, &self.scene_layout, &buffers);
            self.bound = buffers;
        }
    }

    /// A model an instance or level of detail draws, which lives while it
    /// does.
    pub(crate) fn drawn_model(&self, model: ModelId) -> &models::Model {
        self.models.slots.get(model).expect("a drawn model lives")
    }

    /// A material a mesh draws with, which lives while the mesh does.
    pub(crate) fn drawn_material(&self, material: MaterialId) -> &materials::Material {
        self.materials
            .slots
            .get(material)
            .expect("a mesh's material lives")
    }

    /// The farthest the shaders of `model`'s meshes' materials move a
    /// vertex, in its meshes' units (`MaterialShader::displacement_bound`).
    pub(crate) fn displacement_of(&self, model: &models::Model) -> f32 {
        model
            .meshes
            .iter()
            .map(|mesh| self.drawn_material(mesh.material).displacement_bound())
            .fold(0., f32::max)
    }

    /// How the shaders of `model`'s meshes' materials may change what it
    /// casts in a local light's shadow (`Material::shader_casts`): None
    /// where none may; else whether one reads the frame's time and the
    /// latest of their parameter revisions (`Material::parameters`).
    pub(crate) fn caster_shading(&self, model: &models::Model) -> Option<CasterShading> {
        model
            .meshes
            .iter()
            .map(|mesh| self.drawn_material(mesh.material))
            .filter(|material| material.shader_casts())
            .map(|material| {
                let shader = material.values.shader.expect("a shader casts");
                CasterShading {
                    reads_time: self
                        .shaders
                        .get(shader.shader)
                        .is_ok_and(|shader| shader.reads_time),
                    parameters: material.parameters,
                }
            })
            .reduce(|a, b| CasterShading {
                reads_time: a.reads_time || b.reads_time,
                parameters: a.parameters.max(b.parameters),
            })
    }

    /// `instance`'s bounds in its model's space as a shadow caster: as it
    /// shows `model` (its deformation's), grown by what its materials'
    /// shaders may move its vertices (`displacement_of`).
    pub(crate) fn caster_bounds(
        &self,
        instance: &instances::Instance,
        model: &models::Model,
    ) -> [glam::Vec3; 2] {
        static_edits::grown(instance.bounds(model), self.displacement_of(model))
    }

    /// `model`'s bounds in its space, grown by what its materials' shaders
    /// may move its vertices (`displacement_of`).
    pub(crate) fn shaded_bounds(&self, model: &models::Model) -> [glam::Vec3; 2] {
        static_edits::grown(model.bounds, self.displacement_of(model))
    }

    /// Before a frame: uploads a decal atlas packed since the last one, the
    /// draw candidates, sets and chains edits changed and the shader
    /// parameter blocks the last submitted frame changed, rewrites records
    /// whose motion the last submitted frame ended, chooses the deformations
    /// the frame writes and shows, and orders the mist from `eye`.
    pub(crate) fn prepare_frame(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        eye: glam::Vec3,
    ) {
        self.upload_decals(device, queue);
        self.candidates.upload(device, queue);
        self.materials.prepare_frame(queue);
        self.instances
            .prepare_frame(queue, &self.models, &mut self.deformations);
        self.transient.sort_mist(queue, eye);
    }

    /// Before a traced frame: uploads the frame's visibility mask and the
    /// instance entries set since the last traced frame, and builds the
    /// instance BVHs over the capture-visible instances whose model has
    /// triangles and does not deform: the moving one every traced frame and
    /// the static one after a static edit or a change of the instances it
    /// covers. Where the frame traces in `hardware` (after
    /// `prepare_acceleration_structures`), they cover only the instances
    /// its TLAS does not hold (predicate instances under the baseline form,
    /// pending and left out; the architecture's Hardware ray tracing,
    /// *Portable coverage*), and
    /// none whose model rays pass through whole (`RayClass::None`). The
    /// portable path sees no deforming instance, as Bevy 9d12036's
    /// ray-traced scene leaves out meshes with joint attributes
    /// (crates/bevy_solari/src/scene/blas.rs, `is_mesh_raytracing_compatible`).
    pub(crate) fn update_rays(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        visibility_mask: u32,
        hardware: bool,
    ) {
        self.rays.set_visibility_mask(queue, visibility_mask);
        let tlas = self.acceleration.as_ref().filter(|_| hardware);
        let mut covered = Vec::new();
        let mut moving = Vec::new();
        for (id, instance) in self.instances.slots.iter() {
            if !instance.state.capture_visible || instance.deformation.is_some() {
                continue;
            }
            let model = self.drawn_model(instance.state.model);
            if model.ray.bvh_root == 0 {
                continue;
            }
            if let Some(tlas) = tlas
                && (tlas.holds(id.index()) || model.ray_class == ray_class::RayClass::None)
            {
                continue;
            }
            match instance.mobility {
                Mobility::Static => covered.push(id.index() as u32),
                Mobility::Moving => moving.push(rays::instances::bounded(
                    id.index(),
                    model.bounds,
                    instance.state.pose,
                )),
            }
        }
        let edits = self.static_edits.edits();
        let mut statics = Vec::new();
        let rebuild_statics = self.ray_instances.statics_stale(edits, &covered);
        if rebuild_statics {
            statics.extend(covered.iter().map(|&index| {
                let instance = self
                    .instances
                    .slots
                    .at(index as usize)
                    .expect("a covered instance lives");
                let model = self.drawn_model(instance.state.model);
                rays::instances::bounded(index as usize, model.bounds, instance.state.pose)
            }));
        }
        self.ray_instances.update(
            device,
            queue,
            &self.rays,
            rebuild_statics.then_some(rays::instances::StaticBuild {
                instances: statics.as_mut_slice(),
                edits,
                covered,
            }),
            &mut moving,
        );
    }

    /// Before a frame that builds the hardware path's acceleration
    /// structures for the query form `form`, seen from `eye`, and before
    /// `update_rays`, which covers what its TLAS does not hold: chooses its
    /// builds and sets the TLAS (`rays::acceleration`), creating the
    /// structures on the first such frame; none when the device's memory
    /// cannot hold them. The frame records the builds with
    /// `encode_acceleration_structures` after its deform pass, and
    /// `finish_frame` commits them.
    pub(crate) fn prepare_acceleration_structures(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        eye: glam::Vec3,
        form: crate::shading::RayQueryForm,
    ) -> Option<rays::acceleration::RayTracingStats> {
        if self.acceleration.is_none() {
            self.acceleration = rays::acceleration::AccelerationStructures::new(device);
        }
        let acceleration = self.acceleration.as_mut()?;
        Some(acceleration.prepare(
            device,
            queue,
            (&self.models, &self.instances),
            (self.ray_instances.capacity(), eye, form),
        ))
    }

    /// Records the frame's acceleration-structure builds, which
    /// `prepare_acceleration_structures` chose, into `encoder`.
    pub(crate) fn encode_acceleration_structures(&self, encoder: &mut wgpu::CommandEncoder) {
        if let Some(acceleration) = &self.acceleration {
            acceleration.encode(encoder, self.rays.source());
        }
    }

    /// A frame that keeps the hardware path's acceleration structures but
    /// does not build them, tracing no rays: it records no builds.
    pub(crate) fn skip_acceleration_structures(&mut self) {
        if let Some(acceleration) = &mut self.acceleration {
            acceleration.skip_frame();
        }
    }

    /// Frees the hardware path's acceleration structures: a frame runs with
    /// hardware ray tracing off. A frame that turns it on again builds them
    /// anew.
    pub(crate) fn free_acceleration_structures(&mut self) {
        self.acceleration = None;
    }

    /// The hardware path's acceleration structures.
    pub(crate) fn acceleration_structures(
        &self,
    ) -> Option<&rays::acceleration::AccelerationStructures> {
        self.acceleration.as_ref()
    }

    /// Upload caller-generated additive geometry, growing the retained buffer when necessary.
    /// Returns `DeviceLimit` without replacing the geometry if it cannot fit.
    pub fn update_effects(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        vertices: &[crate::effects::Glow],
    ) -> Result<(), SceneError> {
        self.transient.update_glow(device, queue, vertices)
    }
    /// Replace the bounded heat triangle list; empty clears it. Invalid submissions
    /// return an error and retain the previous list. Camera-path only, before bloom/HUD.
    pub fn update_heat_distortion(
        &mut self,
        queue: &wgpu::Queue,
        vertices: &[crate::heat_distortion::HeatDistortion],
    ) -> Result<(), SceneError> {
        self.transient.update_heat(queue, vertices)
    }
    /// Replace the fog volumes (`FogVolume`), which add medium to the
    /// frame's fog where they lie; empty clears them. An invalid volume
    /// fails and keeps the previous ones, as does content exceeding device limits.
    pub fn update_fog_volumes(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        volumes: &[crate::FogVolume],
    ) -> Result<(), SceneError> {
        self.transient.update_fog_volumes(device, queue, volumes)
    }
    /// Replace the ground mist's world positions; empty clears it.
    /// Returns `DeviceLimit` without replacing the positions if they cannot fit.
    pub fn update_mist(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        positions: &[[f32; 3]],
    ) -> Result<(), SceneError> {
        self.transient.update_mist(device, queue, positions)
    }
    /// The installed specular probe collection.
    pub(crate) fn specular_probes(&self) -> Option<&probes::UploadedProbes> {
        self.baked_specular_probes.as_ref()
    }
    /// Commits a submitted frame: each moving instance's pose, and each
    /// instance's shader data and material's shader parameters, become the
    /// ones its motion is measured from, and its static edits are no longer
    /// pending. `Renderer::finish_frame` calls it.
    pub(crate) fn finish_frame(&mut self) {
        self.instances.finish_frame();
        self.materials.finish_frame();
        self.static_edits.finish();
        if let Some(acceleration) = &mut self.acceleration {
            acceleration.finish_frame();
        }
    }
}

#[cfg(all(test, not(target_arch = "wasm32")))]
impl Scene {
    /// Group 1's layout.
    pub(crate) fn scene_layout(&self) -> &wgpu::BindGroupLayout {
        &self.scene_layout
    }

    /// The object records, as a storage binding.
    pub(crate) fn object_records(&self) -> wgpu::BindingResource<'_> {
        self.instances.objects.buffer().as_entire_binding()
    }
}

/// The GPU memory a scene's content holds (`Scene::diagnostic_resources`,
/// re-exported as `diagnostics::SceneResources`).
#[cfg(any(test, feature = "diagnostics"))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SceneResources {
    /// The ray source buffer's bytes, the bytes up to the last word content
    /// holds, and the bytes content holds.
    pub ray_source: u64,
    pub ray_source_used: u64,
    pub ray_source_live: u64,
    /// The object records', instance entries' and light records' buffers.
    pub object_records: u64,
    pub instance_entries: u64,
    pub light_records: u64,
    /// The geometry buffers' bytes (models' positions, indices and caster
    /// clusters' indices), the bytes their content holds, and how many
    /// buffers there are.
    pub geometry: u64,
    pub geometry_live: u64,
    pub geometry_buffers: u64,
    /// The hardware path's BLASes, its models' and deforming instances',
    /// and the triangles they hold. wgpu 30 reports no acceleration
    /// structure's size.
    pub blases: u64,
    pub blas_triangles: u64,
    /// The GPU draw lists' buffers: the draw candidates', the draw sets'
    /// and the level chains' bytes, and the bytes the sets' regions take
    /// in each GPU-built view's cluster list, which holds them once for
    /// each phase it culls and, a cascade's, once more for its paired
    /// regions.
    pub draw_candidates: u64,
    pub draw_sets: u64,
    pub level_chains: u64,
    pub cluster_list: u64,
}

#[cfg(any(test, feature = "diagnostics"))]
impl Scene {
    /// The GPU memory the scene's content holds.
    pub fn diagnostic_resources(&self) -> SceneResources {
        let (ray_source_used, ray_source_live) = self.rays.words_in_use();
        let [geometry, geometry_live, geometry_buffers] = self.geometry.sizes();
        let (blases, blas_triangles) = self
            .acceleration
            .as_ref()
            .map_or((0, 0), |acceleration| acceleration.held());
        let [draw_candidates, draw_sets, level_chains] = self.candidates.bytes();
        SceneResources {
            ray_source: self.rays.source().size(),
            ray_source_used: ray_source_used * 4,
            ray_source_live: ray_source_live * 4,
            object_records: self.instances.objects.buffer().size(),
            instance_entries: self.ray_instances.buffer().size(),
            light_records: self.lights.buffer().size(),
            geometry,
            geometry_live,
            geometry_buffers,
            blases,
            blas_triangles,
            draw_candidates,
            draw_sets,
            level_chains,
            cluster_list: u64::from(self.candidates.sets.region_end())
                * std::mem::size_of::<crate::shading::vertex::DrawInstance>() as u64,
        }
    }
}

/// A value no other scene, resource or identity generation has.
pub(crate) fn next_generation() -> u64 {
    static NEXT: AtomicU64 = AtomicU64::new(1);
    NEXT.fetch_add(1, Ordering::Relaxed)
}

#[cfg_attr(any(test, feature = "diagnostics"), track_caller)]
pub(crate) fn buffer(
    device: &wgpu::Device,
    label: &str,
    contents: &[u8],
    usage: wgpu::BufferUsages,
) -> wgpu::Buffer {
    crate::counters::buffer_init(
        device,
        &wgpu::util::BufferInitDescriptor {
            label: Some(label),
            contents,
            usage,
        },
    )
}

#[cfg(all(test, not(target_arch = "wasm32")))]
#[path = "content_tests.rs"]
mod content_tests;

#[cfg(all(test, not(target_arch = "wasm32")))]
mod origin_tests;

#[cfg(all(test, not(target_arch = "wasm32")))]
mod streaming_tests;