Skip to content

Commit b6ead2b

Browse files
superdumprobtfm
andauthored
Use EntityHashMap<Entity, T> for render world entity storage for better performance (#9903)
# Objective - Improve rendering performance, particularly by avoiding the large system commands costs of using the ECS in the way that the render world does. ## Solution - Define `EntityHasher` that calculates a hash from the `Entity.to_bits()` by `i | (i.wrapping_mul(0x517cc1b727220a95) << 32)`. `0x517cc1b727220a95` is something like `u64::MAX / N` for N that gives a value close to π and that works well for hashing. Thanks for @SkiFire13 for the suggestion and to @nicopap for alternative suggestions and discussion. This approach comes from `rustc-hash` (a.k.a. `FxHasher`) with some tweaks for the case of hashing an `Entity`. `FxHasher` and `SeaHasher` were also tested but were significantly slower. - Define `EntityHashMap` type that uses the `EntityHashser` - Use `EntityHashMap<Entity, T>` for render world entity storage, including: - `RenderMaterialInstances` - contains the `AssetId<M>` of the material associated with the entity. Also for 2D. - `RenderMeshInstances` - contains mesh transforms, flags and properties about mesh entities. Also for 2D. - `SkinIndices` and `MorphIndices` - contains the skin and morph index for an entity, respectively - `ExtractedSprites` - `ExtractedUiNodes` ## Benchmarks All benchmarks have been conducted on an M1 Max connected to AC power. The tests are run for 1500 frames. The 1000th frame is captured for comparison to check for visual regressions. There were none. ### 2D Meshes `bevymark --benchmark --waves 160 --per-wave 1000 --mode mesh2d` #### `--ordered-z` This test spawns the 2D meshes with z incrementing back to front, which is the ideal arrangement allocation order as it matches the sorted render order which means lookups have a high cache hit rate. <img width="1112" alt="Screenshot 2023-09-27 at 07 50 45" src="https://github.com/bevyengine/bevy/assets/302146/e140bc98-7091-4a3b-8ae1-ab75d16d2ccb"> -39.1% median frame time. #### Random This test spawns the 2D meshes with random z. This not only makes the batching and transparent 2D pass lookups get a lot of cache misses, it also currently means that the meshes are almost certain to not be batchable. <img width="1108" alt="Screenshot 2023-09-27 at 07 51 28" src="https://github.com/bevyengine/bevy/assets/302146/29c2e813-645a-43ce-982a-55df4bf7d8c4"> -7.2% median frame time. ### 3D Meshes `many_cubes --benchmark` <img width="1112" alt="Screenshot 2023-09-27 at 07 51 57" src="https://github.com/bevyengine/bevy/assets/302146/1a729673-3254-4e2a-9072-55e27c69f0fc"> -7.7% median frame time. ### Sprites **NOTE: On `main` sprites are using `SparseSet<Entity, T>`!** `bevymark --benchmark --waves 160 --per-wave 1000 --mode sprite` #### `--ordered-z` This test spawns the sprites with z incrementing back to front, which is the ideal arrangement allocation order as it matches the sorted render order which means lookups have a high cache hit rate. <img width="1116" alt="Screenshot 2023-09-27 at 07 52 31" src="https://github.com/bevyengine/bevy/assets/302146/bc8eab90-e375-4d31-b5cd-f55f6f59ab67"> +13.0% median frame time. #### Random This test spawns the sprites with random z. This makes the batching and transparent 2D pass lookups get a lot of cache misses. <img width="1109" alt="Screenshot 2023-09-27 at 07 53 01" src="https://github.com/bevyengine/bevy/assets/302146/22073f5d-99a7-49b0-9584-d3ac3eac3033"> +0.6% median frame time. ### UI **NOTE: On `main` UI is using `SparseSet<Entity, T>`!** `many_buttons` <img width="1111" alt="Screenshot 2023-09-27 at 07 53 26" src="https://github.com/bevyengine/bevy/assets/302146/66afd56d-cbe4-49e7-8b64-2f28f6043d85"> +15.1% median frame time. ## Alternatives - Cart originally suggested trying out `SparseSet<Entity, T>` and indeed that is slightly faster under ideal conditions. However, `PassHashMap<Entity, T>` has better worst case performance when data is randomly distributed, rather than in sorted render order, and does not have the worst case memory usage that `SparseSet`'s dense `Vec<usize>` that maps from the `Entity` index to sparse index into `Vec<T>`. This dense `Vec` has to be as large as the largest Entity index used with the `SparseSet`. - I also tested `PassHashMap<u32, T>`, intending to use `Entity.index()` as the key, but this proved to sometimes be slower and mostly no different. - The only outstanding approach that has not been implemented and tested is to _not_ clear the render world of its entities each frame. That has its own problems, though they could perhaps be solved. - Performance-wise, if the entities and their component data were not cleared, then they would incur table moves on spawn, and should not thereafter, rather just their component data would be overwritten. Ideally we would have a neat way of either updating data in-place via `&mut T` queries, or inserting components if not present. This would likely be quite cumbersome to have to remember to do everywhere, but perhaps it only needs to be done in the more performance-sensitive systems. - The main problem to solve however is that we want to both maintain a mapping between main world entities and render world entities, be able to run the render app and world in parallel with the main app and world for pipelined rendering, and at the same time be able to spawn entities in the render world in such a way that those Entity ids do not collide with those spawned in the main world. This is potentially quite solvable, but could well be a lot of ECS work to do it in a way that makes sense. --- ## Changelog - Changed: Component data for entities to be drawn are no longer stored on entities in the render world. Instead, data is stored in a `EntityHashMap<Entity, T>` in various resources. This brings significant performance benefits due to the way the render app clears entities every frame. Resources of most interest are `RenderMeshInstances` and `RenderMaterialInstances`, and their 2D counterparts. ## Migration Guide Previously the render app extracted mesh entities and their component data from the main world and stored them as entities and components in the render world. Now they are extracted into essentially `EntityHashMap<Entity, T>` where `T` are structs containing an appropriate group of data. This means that while extract set systems will continue to run extract queries against the main world they will store their data in hash maps. Also, systems in later sets will either need to look up entities in the available resources such as `RenderMeshInstances`, or maintain their own `EntityHashMap<Entity, T>` for their own data. Before: ```rust fn queue_custom( material_meshes: Query<(Entity, &MeshTransforms, &Handle<Mesh>), With<InstanceMaterialData>>, ) { ... for (entity, mesh_transforms, mesh_handle) in &material_meshes { ... } } ``` After: ```rust fn queue_custom( render_mesh_instances: Res<RenderMeshInstances>, instance_entities: Query<Entity, With<InstanceMaterialData>>, ) { ... for entity in &instance_entities { let Some(mesh_instance) = render_mesh_instances.get(&entity) else { continue; }; // The mesh handle in `AssetId<Mesh>` form, and the `MeshTransforms` can now // be found in `mesh_instance` which is a `RenderMeshInstance` ... } } ``` --------- Co-authored-by: robtfm <[email protected]>
1 parent 35d3213 commit b6ead2b

File tree

17 files changed

+584
-324
lines changed

17 files changed

+584
-324
lines changed

crates/bevy_ecs/src/entity/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::{
4444
storage::{SparseSetIndex, TableId, TableRow},
4545
};
4646
use serde::{Deserialize, Serialize};
47-
use std::{convert::TryFrom, fmt, mem, sync::atomic::Ordering};
47+
use std::{convert::TryFrom, fmt, hash::Hash, mem, sync::atomic::Ordering};
4848

4949
#[cfg(target_has_atomic = "64")]
5050
use std::sync::atomic::AtomicI64 as AtomicIdCursor;
@@ -115,12 +115,19 @@ type IdCursor = isize;
115115
/// [`EntityCommands`]: crate::system::EntityCommands
116116
/// [`Query::get`]: crate::system::Query::get
117117
/// [`World`]: crate::world::World
118-
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
118+
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
119119
pub struct Entity {
120120
generation: u32,
121121
index: u32,
122122
}
123123

124+
impl Hash for Entity {
125+
#[inline]
126+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
127+
self.to_bits().hash(state);
128+
}
129+
}
130+
124131
pub(crate) enum AllocAtWithoutReplacement {
125132
Exists(EntityLocation),
126133
DidNotExist,

crates/bevy_pbr/src/material.rs

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
render, AlphaMode, DrawMesh, DrawPrepass, EnvironmentMapLight, MeshPipeline, MeshPipelineKey,
3-
MeshTransforms, PrepassPipelinePlugin, PrepassPlugin, ScreenSpaceAmbientOcclusionSettings,
3+
PrepassPipelinePlugin, PrepassPlugin, RenderMeshInstances, ScreenSpaceAmbientOcclusionSettings,
44
SetMeshBindGroup, SetMeshViewBindGroup, Shadow,
55
};
66
use bevy_app::{App, Plugin};
@@ -14,10 +14,7 @@ use bevy_core_pipeline::{
1414
use bevy_derive::{Deref, DerefMut};
1515
use bevy_ecs::{
1616
prelude::*,
17-
system::{
18-
lifetimeless::{Read, SRes},
19-
SystemParamItem,
20-
},
17+
system::{lifetimeless::SRes, SystemParamItem},
2118
};
2219
use bevy_render::{
2320
mesh::{Mesh, MeshVertexBufferLayout},
@@ -37,7 +34,7 @@ use bevy_render::{
3734
view::{ExtractedView, Msaa, ViewVisibility, VisibleEntities},
3835
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
3936
};
40-
use bevy_utils::{tracing::error, HashMap, HashSet};
37+
use bevy_utils::{tracing::error, EntityHashMap, HashMap, HashSet};
4138
use std::hash::Hash;
4239
use std::marker::PhantomData;
4340

@@ -190,6 +187,7 @@ where
190187
.add_render_command::<AlphaMask3d, DrawMaterial<M>>()
191188
.init_resource::<ExtractedMaterials<M>>()
192189
.init_resource::<RenderMaterials<M>>()
190+
.init_resource::<RenderMaterialInstances<M>>()
193191
.init_resource::<SpecializedMeshPipelines<MaterialPipeline<M>>>()
194192
.add_systems(
195193
ExtractSchedule,
@@ -226,26 +224,6 @@ where
226224
}
227225
}
228226

229-
fn extract_material_meshes<M: Material>(
230-
mut commands: Commands,
231-
mut previous_len: Local<usize>,
232-
query: Extract<Query<(Entity, &ViewVisibility, &Handle<M>)>>,
233-
) {
234-
let mut values = Vec::with_capacity(*previous_len);
235-
for (entity, view_visibility, material) in &query {
236-
if view_visibility.get() {
237-
// NOTE: MaterialBindGroupId is inserted here to avoid a table move. Upcoming changes
238-
// to use SparseSet for render world entity storage will do this automatically.
239-
values.push((
240-
entity,
241-
(material.clone_weak(), MaterialBindGroupId::default()),
242-
));
243-
}
244-
}
245-
*previous_len = values.len();
246-
commands.insert_or_spawn_batch(values);
247-
}
248-
249227
/// A key uniquely identifying a specialized [`MaterialPipeline`].
250228
pub struct MaterialPipelineKey<M: Material> {
251229
pub mesh_key: MeshPipelineKey,
@@ -368,24 +346,53 @@ type DrawMaterial<M> = (
368346
/// Sets the bind group for a given [`Material`] at the configured `I` index.
369347
pub struct SetMaterialBindGroup<M: Material, const I: usize>(PhantomData<M>);
370348
impl<P: PhaseItem, M: Material, const I: usize> RenderCommand<P> for SetMaterialBindGroup<M, I> {
371-
type Param = SRes<RenderMaterials<M>>;
349+
type Param = (SRes<RenderMaterials<M>>, SRes<RenderMaterialInstances<M>>);
372350
type ViewWorldQuery = ();
373-
type ItemWorldQuery = Read<Handle<M>>;
351+
type ItemWorldQuery = ();
374352

375353
#[inline]
376354
fn render<'w>(
377-
_item: &P,
355+
item: &P,
378356
_view: (),
379-
material_handle: &'_ Handle<M>,
380-
materials: SystemParamItem<'w, '_, Self::Param>,
357+
_item_query: (),
358+
(materials, material_instances): SystemParamItem<'w, '_, Self::Param>,
381359
pass: &mut TrackedRenderPass<'w>,
382360
) -> RenderCommandResult {
383-
let material = materials.into_inner().get(&material_handle.id()).unwrap();
361+
let materials = materials.into_inner();
362+
let material_instances = material_instances.into_inner();
363+
364+
let Some(material_asset_id) = material_instances.get(&item.entity()) else {
365+
return RenderCommandResult::Failure;
366+
};
367+
let Some(material) = materials.get(material_asset_id) else {
368+
return RenderCommandResult::Failure;
369+
};
384370
pass.set_bind_group(I, &material.bind_group, &[]);
385371
RenderCommandResult::Success
386372
}
387373
}
388374

375+
#[derive(Resource, Deref, DerefMut)]
376+
pub struct RenderMaterialInstances<M: Material>(EntityHashMap<Entity, AssetId<M>>);
377+
378+
impl<M: Material> Default for RenderMaterialInstances<M> {
379+
fn default() -> Self {
380+
Self(Default::default())
381+
}
382+
}
383+
384+
fn extract_material_meshes<M: Material>(
385+
mut material_instances: ResMut<RenderMaterialInstances<M>>,
386+
query: Extract<Query<(Entity, &ViewVisibility, &Handle<M>)>>,
387+
) {
388+
material_instances.clear();
389+
for (entity, view_visibility, handle) in &query {
390+
if view_visibility.get() {
391+
material_instances.insert(entity, handle.id());
392+
}
393+
}
394+
}
395+
389396
const fn alpha_mode_pipeline_key(alpha_mode: AlphaMode) -> MeshPipelineKey {
390397
match alpha_mode {
391398
// Premultiplied and Add share the same pipeline key
@@ -424,12 +431,8 @@ pub fn queue_material_meshes<M: Material>(
424431
msaa: Res<Msaa>,
425432
render_meshes: Res<RenderAssets<Mesh>>,
426433
render_materials: Res<RenderMaterials<M>>,
427-
mut material_meshes: Query<(
428-
&Handle<M>,
429-
&mut MaterialBindGroupId,
430-
&Handle<Mesh>,
431-
&MeshTransforms,
432-
)>,
434+
mut render_mesh_instances: ResMut<RenderMeshInstances>,
435+
render_material_instances: Res<RenderMaterialInstances<M>>,
433436
images: Res<RenderAssets<Image>>,
434437
mut views: Query<(
435438
&ExtractedView,
@@ -493,15 +496,16 @@ pub fn queue_material_meshes<M: Material>(
493496
}
494497
let rangefinder = view.rangefinder3d();
495498
for visible_entity in &visible_entities.entities {
496-
let Ok((material_handle, mut material_bind_group_id, mesh_handle, mesh_transforms)) =
497-
material_meshes.get_mut(*visible_entity)
498-
else {
499+
let Some(material_asset_id) = render_material_instances.get(visible_entity) else {
500+
continue;
501+
};
502+
let Some(mesh_instance) = render_mesh_instances.get_mut(visible_entity) else {
499503
continue;
500504
};
501-
let Some(mesh) = render_meshes.get(mesh_handle) else {
505+
let Some(mesh) = render_meshes.get(mesh_instance.mesh_asset_id) else {
502506
continue;
503507
};
504-
let Some(material) = render_materials.get(&material_handle.id()) else {
508+
let Some(material) = render_materials.get(material_asset_id) else {
505509
continue;
506510
};
507511
let mut mesh_key = view_key;
@@ -530,9 +534,10 @@ pub fn queue_material_meshes<M: Material>(
530534
}
531535
};
532536

533-
*material_bind_group_id = material.get_bind_group_id();
537+
mesh_instance.material_bind_group_id = material.get_bind_group_id();
534538

535-
let distance = rangefinder.distance_translation(&mesh_transforms.transform.translation)
539+
let distance = rangefinder
540+
.distance_translation(&mesh_instance.transforms.transform.translation)
536541
+ material.properties.depth_bias;
537542
match material.properties.alpha_mode {
538543
AlphaMode::Opaque => {

crates/bevy_pbr/src/prepass/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ use bevy_utils::tracing::error;
4747
use crate::{
4848
prepare_materials, setup_morph_and_skinning_defs, AlphaMode, DrawMesh, Material,
4949
MaterialPipeline, MaterialPipelineKey, MeshLayouts, MeshPipeline, MeshPipelineKey,
50-
MeshTransforms, RenderMaterials, SetMaterialBindGroup, SetMeshBindGroup,
50+
RenderMaterialInstances, RenderMaterials, RenderMeshInstances, SetMaterialBindGroup,
51+
SetMeshBindGroup,
5152
};
5253

5354
use std::{hash::Hash, marker::PhantomData};
@@ -758,8 +759,9 @@ pub fn queue_prepass_material_meshes<M: Material>(
758759
pipeline_cache: Res<PipelineCache>,
759760
msaa: Res<Msaa>,
760761
render_meshes: Res<RenderAssets<Mesh>>,
762+
render_mesh_instances: Res<RenderMeshInstances>,
761763
render_materials: Res<RenderMaterials<M>>,
762-
material_meshes: Query<(&Handle<M>, &Handle<Mesh>, &MeshTransforms)>,
764+
render_material_instances: Res<RenderMaterialInstances<M>>,
763765
mut views: Query<(
764766
&ExtractedView,
765767
&VisibleEntities,
@@ -804,16 +806,16 @@ pub fn queue_prepass_material_meshes<M: Material>(
804806
let rangefinder = view.rangefinder3d();
805807

806808
for visible_entity in &visible_entities.entities {
807-
let Ok((material_handle, mesh_handle, mesh_transforms)) =
808-
material_meshes.get(*visible_entity)
809-
else {
809+
let Some(material_asset_id) = render_material_instances.get(visible_entity) else {
810810
continue;
811811
};
812-
813-
let (Some(material), Some(mesh)) = (
814-
render_materials.get(&material_handle.id()),
815-
render_meshes.get(mesh_handle),
816-
) else {
812+
let Some(mesh_instance) = render_mesh_instances.get(visible_entity) else {
813+
continue;
814+
};
815+
let Some(material) = render_materials.get(material_asset_id) else {
816+
continue;
817+
};
818+
let Some(mesh) = render_meshes.get(mesh_instance.mesh_asset_id) else {
817819
continue;
818820
};
819821

@@ -849,7 +851,8 @@ pub fn queue_prepass_material_meshes<M: Material>(
849851
}
850852
};
851853

852-
let distance = rangefinder.distance_translation(&mesh_transforms.transform.translation)
854+
let distance = rangefinder
855+
.distance_translation(&mesh_instance.transforms.transform.translation)
853856
+ material.properties.depth_bias;
854857
match alpha_mode {
855858
AlphaMode::Opaque => {

crates/bevy_pbr/src/render/light.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ use crate::{
33
CascadeShadowConfig, Cascades, CascadesVisibleEntities, Clusters, CubemapVisibleEntities,
44
DirectionalLight, DirectionalLightShadowMap, DrawPrepass, EnvironmentMapLight,
55
GlobalVisiblePointLights, Material, MaterialPipelineKey, MeshPipeline, MeshPipelineKey,
6-
NotShadowCaster, PointLight, PointLightShadowMap, PrepassPipeline, RenderMaterials, SpotLight,
7-
VisiblePointLights,
6+
PointLight, PointLightShadowMap, PrepassPipeline, RenderMaterialInstances, RenderMaterials,
7+
RenderMeshInstances, SpotLight, VisiblePointLights,
88
};
9-
use bevy_asset::Handle;
109
use bevy_core_pipeline::core_3d::Transparent3d;
1110
use bevy_ecs::prelude::*;
1211
use bevy_math::{Mat4, UVec3, UVec4, Vec2, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles};
@@ -1553,9 +1552,10 @@ pub fn prepare_clusters(
15531552
pub fn queue_shadows<M: Material>(
15541553
shadow_draw_functions: Res<DrawFunctions<Shadow>>,
15551554
prepass_pipeline: Res<PrepassPipeline<M>>,
1556-
casting_meshes: Query<(&Handle<Mesh>, &Handle<M>), Without<NotShadowCaster>>,
15571555
render_meshes: Res<RenderAssets<Mesh>>,
1556+
render_mesh_instances: Res<RenderMeshInstances>,
15581557
render_materials: Res<RenderMaterials<M>>,
1558+
render_material_instances: Res<RenderMaterialInstances<M>>,
15591559
mut pipelines: ResMut<SpecializedMeshPipelines<PrepassPipeline<M>>>,
15601560
pipeline_cache: Res<PipelineCache>,
15611561
view_lights: Query<(Entity, &ViewLightEntities)>,
@@ -1598,15 +1598,22 @@ pub fn queue_shadows<M: Material>(
15981598
// NOTE: Lights with shadow mapping disabled will have no visible entities
15991599
// so no meshes will be queued
16001600
for entity in visible_entities.iter().copied() {
1601-
let Ok((mesh_handle, material_handle)) = casting_meshes.get(entity) else {
1601+
let Some(mesh_instance) = render_mesh_instances.get(&entity) else {
16021602
continue;
16031603
};
1604-
let Some(mesh) = render_meshes.get(mesh_handle) else {
1604+
if !mesh_instance.shadow_caster {
1605+
continue;
1606+
}
1607+
let Some(material_asset_id) = render_material_instances.get(&entity) else {
16051608
continue;
16061609
};
1607-
let Some(material) = render_materials.get(&material_handle.id()) else {
1610+
let Some(material) = render_materials.get(material_asset_id) else {
16081611
continue;
16091612
};
1613+
let Some(mesh) = render_meshes.get(mesh_instance.mesh_asset_id) else {
1614+
continue;
1615+
};
1616+
16101617
let mut mesh_key =
16111618
MeshPipelineKey::from_primitive_topology(mesh.primitive_topology)
16121619
| MeshPipelineKey::DEPTH_PREPASS;

0 commit comments

Comments
 (0)