rusting_engine 2.6.0

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
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
//! 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 std::sync::Arc;

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_primitive_mesh, procedural_sphere_mesh, AlphaMode, AssetServer,
    Handle, LodGroupAsset, LodLevel, LodMetric, MaterialAsset, MeshAsset,
    MorphTargets, PrimitiveShape, SkinWeights, TextureAsset, TextureColorSpace,
    TextureSampler,
};
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,
    /// Night-street lights on a grid 3 m up: alternating point lights and
    /// spot lights facing down. The first 8 point lights and 4 spots ask
    /// for shadows; the frame shadows one light at most.
    pub lights: usize,
    /// City of this many static buildings in blocks of 5 by 5 lots, with
    /// the meshes, materials and textures of
    /// [`render_benchmark_city_assets`]. The camera then walks a square of
    /// streets ([`render_benchmark_city_camera`]) instead of orbiting.
    pub city: usize,
    /// Effects round the centre: [`RENDER_BENCHMARK_EFFECTS`] GPU
    /// particles in alpha and additive emitters, see-through spheres, and
    /// skinned characters bending on the GPU.
    pub effects: bool,
    /// A [`super::LightProbeGrid`] of [`RENDER_BENCHMARK_PROBES`] probes
    /// over the whole scene; the High profile relights it every frame.
    pub probes: bool,
    /// Rain of [`RENDER_BENCHMARK_RAIN`] CPU particles from emitters with
    /// `blockers` on, falling onto a hilly heightfield under roofs that
    /// are [`super::ParticleBlocker`]s.
    pub rain: bool,
}

/// CPU rain particles, emitters, and roofs of the rain mode.
pub const RENDER_BENCHMARK_RAIN: [usize; 3] = [50_000, 16, 16];
/// Height samples along each side of the rain mode's heightfield.
pub const RENDER_BENCHMARK_RAIN_GROUND: usize = 257;

/// Probes along each axis of the extras' light probe grid: the renderer's
/// cap of 64.
pub const RENDER_BENCHMARK_PROBES: [u32; 3] = [4, 4, 4];

/// GPU particles, blended instances and skinned characters of the effects
/// mode, and the emitters the particles share.
pub const RENDER_BENCHMARK_EFFECTS: [usize; 3] = [50_000, 2_000, 300];
/// Emitters sharing the effects mode's particles.
pub const RENDER_BENCHMARK_EFFECT_EMITTERS: usize = 20;

/// Building meshes (each with a LOD group), materials and 1024x1024
/// textures of a city of `buildings`: 1000, 200 and 64 from 10,000
/// buildings, fewer below.
#[must_use]
pub fn render_benchmark_city_assets(buildings: usize) -> [usize; 3] {
    [
        (buildings / 10).clamp(1, 1000),
        (buildings / 50).clamp(1, 200),
        (buildings / 156).clamp(1, 64),
    ]
}

/// Edge of each city texture in texels.
pub const RENDER_BENCHMARK_CITY_TEXTURE: u32 = 1024;
/// Distance at which a building swaps its cylinder for a box.
const CITY_LOD_DISTANCE: f32 = 60.0;
/// Lots on each side of a block, the lot and street widths in metres.
const CITY_BLOCK: usize = 5;
const CITY_LOT: f32 = 4.0;
const CITY_STREET: f32 = 8.0;

/// Name of building `index` of the city.
#[must_use]
pub fn render_benchmark_building(index: usize) -> String {
    format!("Benchmark Building {index}")
}

/// Blocks per side of the city's square, and its half width.
fn city_layout(buildings: usize) -> (usize, f32) {
    let blocks = buildings.div_ceil(CITY_BLOCK * CITY_BLOCK);
    let side = (blocks as f32).sqrt().ceil().max(1.0) as usize;
    (side, side as f32 * city_pitch() / 2.0)
}

fn city_pitch() -> f32 {
    CITY_BLOCK as f32 * CITY_LOT + CITY_STREET
}

/// Width, height and depth of building mesh `index`.
fn city_building_size(index: usize) -> [f32; 3] {
    let hash = (index as u32 ^ 0x9e37_79b9).wrapping_mul(2_654_435_761);
    let unit = |shift: u32| ((hash >> shift) & 255) as f32 / 255.0;
    [
        2.0 + 1.5 * unit(0),
        4.0 + 36.0 * unit(8),
        2.0 + 1.5 * unit(16),
    ]
}

/// Centre of building `index`'s lot on the ground.
fn city_lot(index: usize, side: usize, half: f32) -> [f32; 2] {
    let lots = CITY_BLOCK * CITY_BLOCK;
    let (block, lot) = (index / lots, index % lots);
    let axis = |block: usize, lot: usize| {
        -half
            + CITY_STREET
            + city_pitch() * block as f32
            + CITY_LOT * (lot as f32 + 0.5)
    };
    [
        axis(block % side, lot % CITY_BLOCK),
        axis(block / side, lot / CITY_BLOCK),
    ]
}

/// `mesh` with its positions scaled by `size`.
fn scaled_mesh(mesh: &MeshAsset, size: [f32; 3]) -> MeshAsset {
    let mut mesh = mesh.clone();
    for vertex in &mut mesh.vertices {
        let normal = [0, 1, 2].map(|axis| vertex.normal[axis] / size[axis]);
        let length = normal.iter().map(|n| n * n).sum::<f32>().sqrt();
        for axis in 0..3 {
            vertex.position[axis] *= size[axis];
            vertex.normal[axis] = normal[axis] / length.max(f32::EPSILON);
        }
    }
    mesh
}

/// Facade texture `index`: a grid of windows, some lit.
fn city_texture(index: usize) -> TextureAsset {
    let edge = RENDER_BENCHMARK_CITY_TEXTURE as usize;
    let mut rgba8 = Vec::with_capacity(edge * edge * 4);
    for y in 0..edge {
        for x in 0..edge {
            let window = x % 64 > 16 && y % 64 > 24;
            let lit = (index * 37 + x / 64 * 13 + y / 64 * 7).is_multiple_of(5);
            let value = match (window, lit) {
                (false, _) => 110u8,
                (true, false) => 40,
                (true, true) => 230,
            };
            rgba8.extend_from_slice(&[
                value,
                value,
                value.saturating_sub(30),
                255,
            ]);
        }
    }
    TextureAsset {
        size: [RENDER_BENCHMARK_CITY_TEXTURE; 2],
        rgba8,
        color_space: TextureColorSpace::Srgb,
        sampler: TextureSampler::default(),
    }
}

fn spawn_city(world: &mut World, buildings: usize) {
    if buildings == 0 {
        return;
    }
    let [meshes, materials, textures] = render_benchmark_city_assets(buildings);
    let (meshes, materials) = {
        let mut assets =
            world.get_resource_or_insert_with(AssetServer::default);
        let textures: Vec<_> = (0..textures)
            .map(|index| assets.textures.insert(city_texture(index)))
            .collect();
        let materials: Vec<_> = (0..materials)
            .map(|index| {
                let tint = (index % 4) as f32 / 3.0;
                assets.materials.insert(MaterialAsset {
                    name: format!("Benchmark City {index}"),
                    base_color: [0.6 + 0.4 * tint, 0.7, 1.0 - 0.4 * tint, 1.0],
                    roughness: 0.7,
                    base_color_texture: Some(textures[index % textures.len()]),
                    ..MaterialAsset::default()
                })
            })
            .collect();
        let tower = procedural_primitive_mesh(PrimitiveShape::Cylinder);
        let block = procedural_primitive_mesh(PrimitiveShape::Cube);
        let meshes: Vec<_> = (0..meshes)
            .map(|index| {
                let size = city_building_size(index);
                let near = assets.meshes.insert(scaled_mesh(&tower, size));
                let far = assets.meshes.insert(scaled_mesh(&block, size));
                assets.lod_groups.insert(LodGroupAsset {
                    metric: LodMetric::Distance,
                    levels: vec![
                        LodLevel {
                            mesh: near,
                            until: CITY_LOD_DISTANCE,
                        },
                        LodLevel {
                            mesh: far,
                            until: f32::INFINITY,
                        },
                    ],
                });
                near
            })
            .collect();
        (meshes, materials)
    };
    let (side, half) = city_layout(buildings);
    for index in 0..buildings {
        let [x, z] = city_lot(index, side, half);
        let height = city_building_size(index % meshes.len())[1];
        world.spawn((
            Name(render_benchmark_building(index)),
            Transform {
                position: [x, height / 2.0, z],
                ..Transform::default()
            },
            MeshRenderer {
                mesh: meshes[index % meshes.len()],
                material: materials[index % materials.len()],
                cast_shadows: true,
                receive_shadows: true,
            },
            Visibility::default(),
        ));
    }
}

/// Name of skinned character `index` of the effects mode.
#[must_use]
pub fn render_benchmark_character(index: usize) -> String {
    format!("Benchmark Character {index}")
}

fn spawn_effects(world: &mut World) {
    let [particles, blended, characters] = RENDER_BENCHMARK_EFFECTS;
    world.insert_resource(super::GpuSkinning);
    let (sphere, glass, body, skin) = {
        let mut assets =
            world.get_resource_or_insert_with(AssetServer::default);
        let sphere = assets.meshes.insert(procedural_sphere_mesh(8));
        let glass: Vec<_> = (0..4)
            .map(|index| {
                let mut color = [0.5, 0.7, 0.9, 0.35];
                color[index % 3] = 1.0;
                assets.materials.insert(MaterialAsset {
                    name: format!("Benchmark Glass {index}"),
                    alpha_mode: AlphaMode::Blend,
                    base_color: color,
                    roughness: 0.1,
                    ..MaterialAsset::default()
                })
            })
            .collect();
        // A 1.8 m capsule-like column bent at the hips: the lower half
        // follows the hip joint, the upper half blends into the spine.
        let body = scaled_mesh(
            &procedural_primitive_mesh(PrimitiveShape::Cylinder),
            [0.4, 1.8, 0.4],
        );
        let weights = SkinWeights {
            joints: vec![[0, 1, 0, 0]; body.vertices.len()],
            weights: body
                .vertices
                .iter()
                .map(|vertex| {
                    let up = (vertex.position[1] / 0.9).clamp(0.0, 1.0);
                    [1.0 - up, up, 0.0, 0.0]
                })
                .collect(),
        };
        let body = assets.meshes.insert(body);
        // Stands in for the `.rskin` file glTF import cooks.
        assets.deformers.cooked.insert(
            body.key(),
            Arc::new((Some(weights), MorphTargets::default())),
        );
        let skin = assets.materials.insert(MaterialAsset {
            name: "Benchmark Character".into(),
            base_color: [0.8, 0.5, 0.4, 1.0],
            roughness: 0.6,
            ..MaterialAsset::default()
        });
        (sphere, glass, body, skin)
    };
    let per_emitter = (particles / RENDER_BENCHMARK_EFFECT_EMITTERS) as u32;
    for index in 0..RENDER_BENCHMARK_EFFECT_EMITTERS {
        let angle =
            TAU * index as f32 / RENDER_BENCHMARK_EFFECT_EMITTERS as f32;
        let additive = index % 2 == 1;
        world.spawn((
            Name(format!("Benchmark Emitter {index}")),
            Transform {
                position: [10.0 * angle.sin(), 1.0, 10.0 * angle.cos()],
                ..Transform::default()
            },
            super::ParticleEmitter {
                // Full from the first frame and kept full: lifetime times
                // rate is the cap.
                rate: per_emitter as f32 / 2.5,
                max_particles: per_emitter,
                prewarm: true,
                lifetime: [2.0, 3.0],
                speed: [1.0, 3.0],
                size: [0.1, 0.2],
                spread: 0.5,
                gravity: 1.0,
                emissive: if additive { 2.0 } else { 1.0 },
                blend: if additive {
                    super::ParticleBlend::Additive
                } else {
                    super::ParticleBlend::Alpha
                },
                simulation: super::ParticleSimulation::Gpu,
                ..super::ParticleEmitter::default()
            },
            super::ParticleSystem::default(),
        ));
    }
    let side = (blended as f32).sqrt().ceil() as usize;
    for index in 0..blended {
        let [x, z] = grid(index, side, 0.5);
        world.spawn((
            Name(format!("Benchmark Glass {index}")),
            Transform {
                position: [x, 6.0 + 0.5 * (index % 3) as f32, z],
                scale: [0.3; 3],
                ..Transform::default()
            },
            MeshRenderer {
                mesh: sphere,
                material: glass[index % glass.len()],
                cast_shadows: false,
                receive_shadows: true,
            },
            Visibility::default(),
        ));
    }
    let bend = |angle: f32, time: f32| super::Keyframe {
        time,
        value: vec![0.0, 0.0, angle],
    };
    let animation = super::Animation {
        clips: vec![super::AnimationClip {
            name: "bend".into(),
            duration: 1.0,
            repeat: super::TweenRepeat::Loop,
            tracks: vec![super::AnimationTrack {
                target: "Hip/Spine".into(),
                property: super::AnimationProperty::Rotation,
                keys: vec![bend(-0.4, 0.0), bend(0.4, 0.5), bend(-0.4, 1.0)],
                ..super::AnimationTrack::default()
            }],
            ..super::AnimationClip::default()
        }],
        autoplay: "bend".into(),
        ..super::Animation::default()
    };
    let side = (characters as f32).sqrt().ceil() as usize;
    for index in 0..characters {
        let [x, z] = grid(index, side, 1.2);
        let character = world
            .spawn((
                Name(render_benchmark_character(index)),
                Transform {
                    position: [x, 0.9, z],
                    ..Transform::default()
                },
                MeshRenderer {
                    mesh: body,
                    material: skin,
                    cast_shadows: true,
                    receive_shadows: true,
                },
                Visibility::default(),
                super::Skin {
                    joints: vec!["Hip".into(), "Hip/Spine".into()],
                    inverse_bind: Vec::new(),
                },
                animation.clone(),
            ))
            .id();
        let hip = world.spawn((Name("Hip".into()), Transform::default())).id();
        let spine = world
            .spawn((Name("Spine".into()), Transform::default()))
            .id();
        for (child, parent) in [(hip, character), (spine, hip)] {
            super::hierarchy::set_parent(world, child, parent)
                .expect("fresh entities have no parent");
        }
    }
}

fn spawn_rain(world: &mut World) {
    let [particles, emitters, roofs] = RENDER_BENCHMARK_RAIN;
    let samples = RENDER_BENCHMARK_RAIN_GROUND;
    let spacing = 64.0 / (samples - 1) as f32;
    let ground = super::Heightfield {
        heights: (0..samples)
            .map(|row| {
                (0..samples)
                    .map(|column| {
                        let [x, z] =
                            [column, row].map(|at| at as f32 * spacing);
                        0.3 + 0.3 * (x * 0.3).sin() * (z * 0.2).cos()
                    })
                    .collect()
            })
            .collect(),
        spacing,
        ..super::Heightfield::default()
    };
    let (cube, ground_mesh, ground_material, roof_material) = {
        let mut assets =
            world.get_resource_or_insert_with(AssetServer::default);
        let ground_mesh = assets.meshes.insert(ground.mesh());
        let ground_material = assets.materials.insert(MaterialAsset {
            name: "Benchmark Wet Ground".into(),
            base_color: [0.25, 0.3, 0.2, 1.0],
            roughness: 0.3,
            ..MaterialAsset::default()
        });
        let roof_material = assets.materials.insert(MaterialAsset {
            name: "Benchmark Roof".into(),
            base_color: [0.5, 0.2, 0.15, 1.0],
            roughness: 0.8,
            ..MaterialAsset::default()
        });
        (
            assets.fallback_mesh,
            ground_mesh,
            ground_material,
            roof_material,
        )
    };
    world.spawn((
        Name("Benchmark Rain Ground".into()),
        Transform::default(),
        MeshRenderer {
            mesh: ground_mesh,
            material: ground_material,
            cast_shadows: false,
            receive_shadows: true,
        },
        Visibility::default(),
        ground,
    ));
    let side = (emitters as f32).sqrt().ceil() as usize;
    let per_emitter = (particles / emitters) as u32;
    for index in 0..emitters {
        let [x, z] = grid(index, side, 14.0);
        world.spawn((
            Name(format!("Benchmark Rain {index}")),
            Transform {
                position: [x, 20.0, z],
                ..Transform::default()
            },
            super::ParticleEmitter {
                // About 1.5 s to fall 20 m, so the cap stays nearly full.
                rate: per_emitter as f32 / 1.5,
                max_particles: per_emitter,
                shape: super::EmitterShape::Box,
                shape_size: [7.0, 0.5, 7.0],
                lifetime: [3.0, 3.0],
                speed: [4.0, 6.0],
                size: [0.02, 0.04],
                direction: [0.0, -1.0, 0.0],
                spread: 0.05,
                gravity: 9.8,
                color_over_life: vec![super::ColorKey {
                    t: 0.0,
                    color: [0.7, 0.8, 1.0, 0.5],
                }],
                facing: super::ParticleFacing::Velocity,
                stretch: 0.03,
                blockers: true,
                ..super::ParticleEmitter::default()
            },
            super::ParticleSystem::default(),
        ));
    }
    let side = (roofs as f32).sqrt().ceil() as usize;
    for index in 0..roofs {
        let [x, z] = grid(index, side, 14.0);
        world.spawn((
            Name(format!("Benchmark Roof {index}")),
            Transform {
                position: [x + 2.0, 4.0, z + 2.0],
                scale: [5.0, 0.3, 5.0],
                ..Transform::default()
            },
            MeshRenderer {
                mesh: cube,
                material: roof_material,
                cast_shadows: true,
                receive_shadows: true,
            },
            Visibility::default(),
            super::ParticleBlocker { extents: [0.5; 3] },
        ));
    }
}

/// Street-level camera at `frame` of the city path: once round a square of
/// streets in [`RENDER_BENCHMARK_FRAMES`] frames, facing the way it walks,
/// so buildings hide most of the city. The path is closed.
#[must_use]
pub fn render_benchmark_city_camera(buildings: usize, frame: u32) -> Transform {
    let (side, half) = city_layout(buildings);
    let street =
        |index: usize| -half + CITY_STREET / 2.0 + city_pitch() * index as f32;
    let near = street(side / 4);
    let far = street((side - 1 - side / 4).max(side / 4 + 1));
    let turn = (frame % RENDER_BENCHMARK_FRAMES) as f32
        / RENDER_BENCHMARK_FRAMES as f32
        * 4.0;
    let leg = (turn as usize).min(3);
    let along = near + (far - near) * (turn - leg as f32);
    let back = far + near - along;
    let ([x, z], direction) = match leg {
        0 => ([along, near], [1.0, 0.0]),
        1 => ([far, along], [0.0, 1.0]),
        2 => ([back, far], [-1.0, 0.0]),
        _ => ([near, back], [0.0, -1.0]),
    };
    Transform {
        position: [x, 1.7, z],
        rotation: crate::engine::rotation_facing([
            direction[0],
            -0.1,
            direction[1],
        ]),
        ..Transform::default()
    }
}

/// Point lights, then spots, that ask for shadows in the extra lights.
pub const RENDER_BENCHMARK_SHADOWED_LIGHTS: [usize; 2] = [8, 4];

/// 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.lights as f32).sqrt().ceil() as usize;
    let spacing = 72.0 / side.max(1) as f32;
    for index in 0..extras.lights {
        let [x, z] = grid(index, side, spacing);
        let transform = Transform {
            position: [x, 3.0, z],
            rotation: crate::engine::rotation_facing([0.0, -1.0, 0.0]),
            ..Transform::default()
        };
        let mut color = [0.4; 3];
        color[index % 3] = 1.0;
        let name = Name(format!("Benchmark Street Light {index}"));
        let [points, spots] = RENDER_BENCHMARK_SHADOWED_LIGHTS;
        if index % 2 == 0 {
            world.spawn((
                name,
                transform,
                PointLight {
                    color,
                    intensity: 300.0,
                    range: 8.0,
                    shadows: index / 2 < points,
                    radius: 0.0,
                },
            ));
        } else {
            world.spawn((
                name,
                transform,
                super::SpotLight {
                    color,
                    intensity: 600.0,
                    range: 8.0,
                    shadows: index / 2 < spots,
                    ..super::SpotLight::default()
                },
            ));
        }
    }
    spawn_city(world, extras.city);
    if extras.effects {
        spawn_effects(world);
    }
    if extras.rain {
        spawn_rain(world);
    }
    if extras.probes {
        world.spawn((
            Name("Benchmark Light Probes".into()),
            Transform {
                position: [0.0, 6.0, 0.0],
                ..Transform::default()
            },
            super::LightProbeGrid {
                extents: [24.0, 6.0, 24.0],
                resolution: RENDER_BENCHMARK_PROBES,
                intensity: 1.0,
            },
        ));
    }
    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);
    }

    #[test]
    fn extra_lights_alternate_points_and_spots_with_few_shadows() {
        let mut world = World::new();
        spawn_render_benchmark(&mut world);
        spawn_render_benchmark_extras(
            &mut world,
            RenderBenchmarkExtras {
                lights: 301,
                ..RenderBenchmarkExtras::default()
            },
        );
        let points: Vec<_> =
            world.query::<&PointLight>().iter(&world).copied().collect();
        let spots: Vec<_> = world
            .query::<&super::super::SpotLight>()
            .iter(&world)
            .copied()
            .collect();
        assert_eq!(points.len(), RENDER_BENCHMARK_POINT_LIGHTS + 151);
        assert_eq!(spots.len(), 150);
        let shadowed = [
            points.iter().filter(|light| light.shadows).count(),
            spots.iter().filter(|light| light.shadows).count(),
        ];
        assert_eq!(shadowed, RENDER_BENCHMARK_SHADOWED_LIGHTS);
    }

    #[test]
    fn city_shares_lod_meshes_and_textures_and_its_streets_are_clear() {
        let buildings = 600;
        let mut world = World::new();
        spawn_render_benchmark_extras(
            &mut world,
            RenderBenchmarkExtras {
                city: buildings,
                ..RenderBenchmarkExtras::default()
            },
        );
        let [meshes, materials, textures] =
            render_benchmark_city_assets(buildings);
        assert_eq!([meshes, materials, textures], [60, 12, 3]);
        assert_eq!(render_benchmark_city_assets(10_000), [1000, 200, 64]);
        let assets = world.resource::<AssetServer>();
        assert_eq!(assets.lod_groups.len(), meshes);
        let city_materials = assets
            .materials
            .iter()
            .filter(|(_, material)| material.name.starts_with("Benchmark City"))
            .count();
        assert_eq!(city_materials, materials);
        let lots: Vec<_> = world
            .query::<(&Name, &Transform, &MeshRenderer)>()
            .iter(&world)
            .filter(|(name, ..)| name.0.starts_with("Benchmark Building"))
            .map(|(_, transform, renderer)| (transform.position, renderer.mesh))
            .collect();
        assert_eq!(lots.len(), buildings);
        let distinct: std::collections::HashSet<_> =
            lots.iter().map(|(_, mesh)| *mesh).collect();
        assert_eq!(distinct.len(), meshes);
        // The walk is closed, at eye height, and never inside a building.
        let first = render_benchmark_city_camera(buildings, 0);
        let last =
            render_benchmark_city_camera(buildings, RENDER_BENCHMARK_FRAMES);
        assert_eq!(first, last);
        for frame in (0..RENDER_BENCHMARK_FRAMES).step_by(5) {
            let eye = render_benchmark_city_camera(buildings, frame).position;
            assert_eq!(eye[1], 1.7);
            for (position, _) in &lots {
                let clear = (eye[0] - position[0]).abs() > CITY_LOT / 2.0
                    || (eye[2] - position[2]).abs() > CITY_LOT / 2.0;
                assert!(
                    clear,
                    "frame {frame} at {eye:?} is inside {position:?}"
                );
            }
        }
    }

    #[test]
    fn effects_fill_gpu_emitters_and_skin_animated_characters() {
        let mut app = super::super::App::new();
        app.add_plugin(super::super::HybridPhysicsPlugin).unwrap();
        app.add_plugin(super::super::RenderExtractPlugin).unwrap();
        spawn_render_benchmark_extras(
            app.world_mut(),
            RenderBenchmarkExtras {
                effects: true,
                ..RenderBenchmarkExtras::default()
            },
        );
        let [particles, blended, characters] = RENDER_BENCHMARK_EFFECTS;
        let world = app.world_mut();
        let emitters: Vec<_> = world
            .query::<&super::super::ParticleEmitter>()
            .iter(world)
            .map(|emitter| (emitter.max_particles, emitter.simulation))
            .collect();
        assert_eq!(emitters.len(), RENDER_BENCHMARK_EFFECT_EMITTERS);
        let total: u32 = emitters.iter().map(|(max, _)| max).sum();
        assert_eq!(total as usize, particles);
        assert!(emitters.iter().all(|(_, simulation)| {
            *simulation == super::super::ParticleSimulation::Gpu
        }));
        let materials: Vec<_> = world
            .query::<&MeshRenderer>()
            .iter(world)
            .map(|renderer| renderer.material)
            .collect();
        let assets = world.resource::<AssetServer>();
        let see_through = materials
            .iter()
            .filter(|&&material| {
                assets.materials.get(material).unwrap().alpha_mode
                    == AlphaMode::Blend
            })
            .count();
        assert_eq!(see_through, blended);
        // A few fixed ticks play the clip and bend the GPU-skinned copies.
        let tick = std::time::Duration::from_secs_f64(1.0 / 60.0);
        for _ in 0..10 {
            app.update(tick).unwrap();
        }
        let jobs = super::super::skin_jobs(app.world_mut());
        assert_eq!(jobs.len(), characters);
        let bent = jobs[0].matrices[1];
        assert_ne!(bent, jobs[0].matrices[0], "the spine bends off the hip");
    }

    #[test]
    fn probes_add_one_full_grid_over_the_scene() {
        let mut world = World::new();
        spawn_render_benchmark_extras(
            &mut world,
            RenderBenchmarkExtras {
                probes: true,
                ..RenderBenchmarkExtras::default()
            },
        );
        let grids: Vec<_> = world
            .query::<&super::super::LightProbeGrid>()
            .iter(&world)
            .map(|grid| grid.resolution)
            .collect();
        assert_eq!(grids, [RENDER_BENCHMARK_PROBES]);
        assert_eq!(RENDER_BENCHMARK_PROBES.iter().product::<u32>(), 64);
    }

    #[test]
    fn rain_comes_from_blocked_cpu_emitters_over_ground_and_roofs() {
        let mut world = World::new();
        spawn_render_benchmark_extras(
            &mut world,
            RenderBenchmarkExtras {
                rain: true,
                ..RenderBenchmarkExtras::default()
            },
        );
        let [particles, emitters, roofs] = RENDER_BENCHMARK_RAIN;
        let rain: Vec<_> = world
            .query::<&super::super::ParticleEmitter>()
            .iter(&world)
            .map(|emitter| {
                (emitter.blockers, emitter.simulation, emitter.max_particles)
            })
            .collect();
        assert_eq!(rain.len(), emitters);
        assert!(rain.iter().all(|&(blockers, simulation, _)| {
            blockers && simulation == super::super::ParticleSimulation::Cpu
        }));
        let cap: u32 = rain.iter().map(|(.., max)| max).sum();
        assert_eq!(cap as usize, particles);
        let blockers = world
            .query::<&super::super::ParticleBlocker>()
            .iter(&world)
            .count();
        assert_eq!(blockers, roofs);
        let ground = world
            .query::<&super::super::Heightfield>()
            .single(&world)
            .unwrap();
        assert_eq!(ground.heights.len(), RENDER_BENCHMARK_RAIN_GROUND);
    }
}