rusting_engine 2.4.1

Vulkan 3D game engine with GPU-accelerated physics for massive physics-heavy scenes
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
//! Fixed render benchmark: one scene and one camera path.
//!
//! The scene is a pure function of nothing: the same ECS entities, bit for
//! bit, on every machine. It exercises every stage the vertical slice uses:
//! a shadowed sun, point lights past the Eco budget, PBR materials, instanced
//! cubes and spheres, and GPU physics bodies falling onto a fixed ground. The
//! camera orbits the scene once in [`RENDER_BENCHMARK_FRAMES`] frames, moving
//! in and out so culling and shadow distance see changing work.

use std::f32::consts::TAU;

use bevy_ecs::prelude::{Entity, World};

use super::physics_benchmark::grid;
use super::{
    Camera, DirectionalLight, GpuCondition, GpuEventMode, GpuEventPayload,
    GpuPhysicsClassWatches, GpuPhysicsRule, MeshRenderer, Name, ObjectClasses,
    PhysicsBenchmark, PhysicsBody, PointLight, RigidBody, RigidBodyKind,
    SimulationClass, Visibility,
};
use crate::assets::{
    procedural_sphere_mesh, AssetServer, Handle, MaterialAsset, MeshAsset,
};
use crate::Transform;

/// Frames in one pass of the camera path: ten seconds at 60 FPS.
pub const RENDER_BENCHMARK_FRAMES: u32 = 600;
/// GPU physics bodies in the benchmark scene.
pub const RENDER_BENCHMARK_BODIES: usize = 1_000;
/// Point lights in the benchmark scene.
pub const RENDER_BENCHMARK_POINT_LIGHTS: usize = 24;
/// Pillars, each topped by a sphere.
pub const RENDER_BENCHMARK_PILLARS: usize = 40;
/// Class of the benchmark's dynamic bodies.
pub const RENDER_BENCHMARK_BODY_CLASS: &str = "benchmark_bodies";

/// Spawns the benchmark scene into `world` and returns its active camera.
/// Move the camera with [`render_benchmark_camera`] each frame. Add
/// `HybridPhysicsPlugin` first to also watch body landings.
pub fn spawn_render_benchmark(world: &mut World) -> Entity {
    let (cube, sphere, materials) = {
        let mut assets =
            world.get_resource_or_insert_with(AssetServer::default);
        let sphere = assets.meshes.insert(procedural_sphere_mesh(16));
        let materials: Vec<_> = (0..5)
            .map(|index| {
                let t = index as f32 / 4.0;
                assets.materials.insert(MaterialAsset {
                    base_color: [0.9 - 0.5 * t, 0.5, 0.2 + 0.6 * t, 1.0],
                    metallic: t,
                    roughness: 0.9 - 0.8 * t,
                    ..MaterialAsset::default()
                })
            })
            .collect();
        (assets.fallback_mesh, sphere, materials)
    };
    let mut spawn_mesh =
        |name: String, transform, mesh: Handle<MeshAsset>, material| {
            world
                .spawn((
                    Name(name),
                    transform,
                    MeshRenderer {
                        mesh,
                        material,
                        cast_shadows: true,
                        receive_shadows: true,
                    },
                    Visibility::default(),
                ))
                .id()
        };

    let ground = spawn_mesh(
        "Benchmark Ground".into(),
        Transform {
            position: [0.0, -0.5, 0.0],
            scale: [80.0, 1.0, 80.0],
            ..Transform::default()
        },
        cube,
        materials[0],
    );
    // A 7 x 7 grid, 8 m apart, without the centre 3 x 3 where bodies fall.
    let pillars = (0..49)
        .map(|cell| grid(cell, 7, 8.0))
        .filter(|[x, z]| x.abs() > 8.0 || z.abs() > 8.0);
    for (index, [x, z]) in pillars.enumerate() {
        let material = materials[index % materials.len()];
        spawn_mesh(
            format!("Benchmark Pillar {index}"),
            Transform {
                position: [x, 3.0, z],
                scale: [1.0, 6.0, 1.0],
                ..Transform::default()
            },
            cube,
            material,
        );
        spawn_mesh(
            format!("Benchmark Sphere {index}"),
            Transform {
                position: [x, 7.0, z],
                scale: [2.0; 3],
                ..Transform::default()
            },
            sphere,
            material,
        );
    }
    let bodies = PhysicsBenchmark::Mixed.bodies(RENDER_BENCHMARK_BODIES);
    let mut body_entities = Vec::with_capacity(bodies.len());
    for (index, body) in bodies.into_iter().enumerate() {
        let material = materials[index % materials.len()];
        let entity = spawn_mesh(
            format!("Benchmark Body {index}"),
            body.transform,
            cube,
            material,
        );
        body_entities.push((entity, body.solver));
    }

    let physics = |solver, kind| {
        (
            PhysicsBody {
                simulation: SimulationClass::Gpu,
                solver,
                custom_shader: None,
            },
            RigidBody {
                kind,
                ..RigidBody::default()
            },
            super::Collider::default(),
        )
    };
    world
        .entity_mut(ground)
        .insert(physics(super::PhysicsSolver::Full, RigidBodyKind::Fixed));
    for (entity, solver) in body_entities {
        world.entity_mut(entity).insert((
            physics(solver, RigidBodyKind::Dynamic),
            ObjectClasses::new([RENDER_BENCHMARK_BODY_CLASS]),
        ));
    }
    // Landing events give the benchmark event readback traffic.
    if let Some(mut watches) =
        world.get_resource_mut::<GpuPhysicsClassWatches>()
    {
        watches.add(
            RENDER_BENCHMARK_BODY_CLASS,
            GpuPhysicsRule::new(
                "benchmark_body_landed",
                GpuCondition::position_y().less_than(1.0),
            )
            .mode(GpuEventMode::OnEnter)
            .payload(GpuEventPayload::Position),
        );
    }

    world.spawn((
        Name("Benchmark Sun".into()),
        Transform {
            rotation: crate::engine::rotation_facing([-0.4, -1.0, -0.3]),
            ..Transform::default()
        },
        DirectionalLight {
            illuminance: 20_000.0,
            ..DirectionalLight::default()
        },
    ));
    for index in 0..RENDER_BENCHMARK_POINT_LIGHTS {
        let angle = TAU * index as f32 / RENDER_BENCHMARK_POINT_LIGHTS as f32;
        let mut color = [0.3; 3];
        color[index % 3] = 1.0;
        world.spawn((
            Name(format!("Benchmark Light {index}")),
            Transform::new([12.0 * angle.cos(), 3.0, 12.0 * angle.sin()]),
            PointLight {
                color,
                intensity: 400.0,
                range: 10.0,
                shadows: false,
                radius: 0.0,
            },
        ));
    }
    world
        .spawn((
            Name("Benchmark Camera".into()),
            render_benchmark_camera(0),
            Camera {
                active: true,
                ..Camera::default()
            },
        ))
        .id()
}

/// Optional additions to the benchmark scene, for horror-game sized loads:
/// CCTV screens, and a shelf of small identical props ("bears").
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct RenderBenchmarkExtras {
    /// Camera screens on a ring facing the centre, each with its own camera
    /// and material.
    pub screens: usize,
    /// Feed size of each screen.
    pub screen_size: [u32; 2],
    /// `CameraScreen::update_every` of each screen; 0 counts as 1.
    pub screen_every: u32,
    /// Static spheres sharing one mesh and material, in a grid 0.6 m apart.
    pub bears: usize,
    /// Bears whose transform changes every frame.
    pub bear_updates: usize,
    /// Removes the GPU physics bodies, leaving a scene that only renders.
    pub without_bodies: bool,
}

/// Name of bear `index` in [`spawn_render_benchmark_extras`].
#[must_use]
pub fn render_benchmark_bear(index: usize) -> String {
    format!("Benchmark Bear {index}")
}

/// Adds `extras` to a scene from [`spawn_render_benchmark`] and returns
/// the bears in index order.
pub fn spawn_render_benchmark_extras(
    world: &mut World,
    extras: RenderBenchmarkExtras,
) -> Vec<Entity> {
    let (cube, sphere, bear_material, screen_materials) = {
        let mut assets =
            world.get_resource_or_insert_with(AssetServer::default);
        let sphere = assets.meshes.insert(procedural_sphere_mesh(8));
        let bear = assets.materials.insert(MaterialAsset {
            base_color: [0.6, 0.4, 0.3, 1.0],
            roughness: 0.8,
            ..MaterialAsset::default()
        });
        let screens: Vec<_> = (0..extras.screens)
            .map(|index| {
                assets.materials.insert(MaterialAsset {
                    name: format!("Benchmark Screen {index}"),
                    base_color: [0.0, 0.0, 0.0, 1.0],
                    emissive: [1.0; 3],
                    ..MaterialAsset::default()
                })
            })
            .collect();
        (assets.fallback_mesh, sphere, bear, screens)
    };
    for (index, material) in screen_materials.into_iter().enumerate() {
        let angle = TAU * index as f32 / extras.screens as f32;
        let (sin, cos) = angle.sin_cos();
        let camera = format!("Benchmark Feed {index}");
        world.spawn((
            Name(camera.clone()),
            Transform {
                position: [6.0 * sin, 4.0, 6.0 * cos],
                rotation: crate::engine::rotation_facing([sin, -0.3, cos]),
                ..Transform::default()
            },
            Camera::default(),
        ));
        world.spawn((
            Name(format!("Benchmark Screen {index}")),
            Transform {
                position: [20.0 * sin, 4.0, 20.0 * cos],
                rotation: crate::engine::rotation_facing([-sin, 0.0, -cos]),
                scale: [3.2, 1.8, 0.1],
            },
            MeshRenderer {
                mesh: cube,
                material,
                cast_shadows: false,
                receive_shadows: false,
            },
            Visibility::default(),
            super::CameraScreen {
                camera,
                size: extras.screen_size,
                update_every: extras.screen_every.max(1),
                ..Default::default()
            },
        ));
    }
    if extras.without_bodies {
        let mut bodies = world.query::<(Entity, &ObjectClasses)>();
        let bodies: Vec<_> = bodies
            .iter(world)
            .filter(|(_, classes)| {
                classes.contains(RENDER_BENCHMARK_BODY_CLASS)
            })
            .map(|(entity, _)| entity)
            .collect();
        for body in bodies {
            world.despawn(body);
        }
    }
    let side = (extras.bears as f32).sqrt().ceil() as usize;
    (0..extras.bears)
        .map(|index| {
            world
                .spawn((
                    Name(render_benchmark_bear(index)),
                    bear_transform(index, side, 0),
                    MeshRenderer {
                        mesh: sphere,
                        material: bear_material,
                        cast_shadows: true,
                        receive_shadows: true,
                    },
                    Visibility::default(),
                ))
                .id()
        })
        .collect()
}

/// Moves the first `extras.bear_updates` bears for `frame`: each one bobs,
/// so every frame changes that many transforms.
pub fn update_render_benchmark_bears(
    world: &mut World,
    bears: &[Entity],
    extras: RenderBenchmarkExtras,
    frame: u32,
) {
    let side = (bears.len() as f32).sqrt().ceil() as usize;
    for (index, &bear) in bears.iter().take(extras.bear_updates).enumerate() {
        if let Some(mut transform) = world.get_mut::<Transform>(bear) {
            *transform = bear_transform(index, side, frame);
        }
    }
}

fn bear_transform(index: usize, side: usize, frame: u32) -> Transform {
    let [x, z] = grid(index, side.max(1), 0.6);
    let bob = 0.05 * (frame as f32 * 0.2 + index as f32).sin();
    Transform {
        position: [x, 0.2 + bob, z],
        scale: [0.4; 3],
        ..Transform::default()
    }
}

/// Camera transform at `frame` of the benchmark path. The path is closed:
/// frame [`RENDER_BENCHMARK_FRAMES`] matches frame 0.
#[must_use]
pub fn render_benchmark_camera(frame: u32) -> Transform {
    let turn = (frame % RENDER_BENCHMARK_FRAMES) as f32
        / RENDER_BENCHMARK_FRAMES as f32;
    let angle = TAU * turn;
    // Two dives per orbit: from 40 m out to 16 m, low over the pillars.
    let reach = 28.0 + 12.0 * (2.0 * angle).cos();
    let height = 10.0 + 6.0 * (2.0 * angle).cos();
    let position = [reach * angle.sin(), height, reach * angle.cos()];
    let target = [0.0, 2.0, 0.0];
    Transform {
        position,
        rotation: crate::engine::rotation_facing(
            [0, 1, 2].map(|axis| target[axis] - position[axis]),
        ),
        ..Transform::default()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn camera_path_is_closed_repeatable_and_faces_the_scene() {
        assert_eq!(
            render_benchmark_camera(0),
            render_benchmark_camera(RENDER_BENCHMARK_FRAMES)
        );
        for frame in 0..RENDER_BENCHMARK_FRAMES {
            let camera = render_benchmark_camera(frame);
            assert_eq!(camera, render_benchmark_camera(frame));
            let [x, y, z] = camera.position;
            let reach = (x * x + z * z).sqrt();
            assert!((15.9..=40.1).contains(&reach), "{frame}: {reach}");
            assert!(y > 3.9, "{frame}: {y}");
            // Pitch looks down toward the target below the camera.
            assert!(camera.rotation[0] < 0.0, "{frame}");
        }
        let quarter = render_benchmark_camera(RENDER_BENCHMARK_FRAMES / 4);
        assert!(quarter.position[0] > 15.0, "{quarter:?}");
    }

    #[test]
    fn scene_spawns_the_same_fixed_content_every_time() {
        let counts = || {
            let mut world = World::new();
            let camera = spawn_render_benchmark(&mut world);
            assert!(world.get::<Camera>(camera).unwrap().active);
            let mut meshes =
                world.query::<(&Name, &Transform, &MeshRenderer)>();
            let layout: Vec<_> = meshes
                .iter(&world)
                .map(|(name, transform, _)| (name.0.clone(), *transform))
                .collect();
            let bodies = world.query::<&PhysicsBody>().iter(&world).count();
            let points = world.query::<&PointLight>().iter(&world).count();
            let suns = world.query::<&DirectionalLight>().iter(&world).count();
            (layout, bodies, points, suns)
        };
        let (layout, bodies, points, suns) = counts();
        assert_eq!(
            layout.len(),
            1 + 2 * RENDER_BENCHMARK_PILLARS + RENDER_BENCHMARK_BODIES
        );
        assert_eq!(bodies, 1 + RENDER_BENCHMARK_BODIES);
        assert_eq!((points, suns), (RENDER_BENCHMARK_POINT_LIGHTS, 1));
        assert_eq!(counts().0, layout);
    }
}