Skip to content

Commit 48fc396

Browse files
tychedeliamockersf
authored andcommitted
Fix unecessary specialization checks for apps with many materials (#18410)
# Objective For materials that aren't being used or a visible entity doesn't have an instance of, we were unnecessarily constantly checking whether they needed specialization, saying yes (because the material had never been specialized for that entity), and failing to look up the material instance. ## Solution If an entity doesn't have an instance of the material, it can't possibly need specialization, so exit early before spending time doing the check. Fixes #18388.
1 parent 42b7487 commit 48fc396

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

crates/bevy_pbr/src/material.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,9 @@ pub fn specialize_material_meshes<M: Material>(
863863
.or_default();
864864

865865
for (_, visible_entity) in visible_entities.iter::<Mesh3d>() {
866+
let Some(material_asset_id) = render_material_instances.get(visible_entity) else {
867+
continue;
868+
};
866869
let entity_tick = entity_specialization_ticks.get(visible_entity).unwrap();
867870
let last_specialized_tick = view_specialized_material_pipeline_cache
868871
.get(visible_entity)
@@ -874,9 +877,6 @@ pub fn specialize_material_meshes<M: Material>(
874877
if !needs_specialization {
875878
continue;
876879
}
877-
let Some(material_asset_id) = render_material_instances.get(visible_entity) else {
878-
continue;
879-
};
880880
let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(*visible_entity)
881881
else {
882882
continue;

crates/bevy_pbr/src/prepass/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,9 @@ pub fn specialize_prepass_material_meshes<M>(
962962
.or_default();
963963

964964
for (_, visible_entity) in visible_entities.iter::<Mesh3d>() {
965+
let Some(material_asset_id) = render_material_instances.get(visible_entity) else {
966+
continue;
967+
};
965968
let entity_tick = entity_specialization_ticks.get(visible_entity).unwrap();
966969
let last_specialized_tick = view_specialized_material_pipeline_cache
967970
.get(visible_entity)
@@ -973,10 +976,6 @@ pub fn specialize_prepass_material_meshes<M>(
973976
if !needs_specialization {
974977
continue;
975978
}
976-
977-
let Some(material_asset_id) = render_material_instances.get(visible_entity) else {
978-
continue;
979-
};
980979
let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(*visible_entity)
981980
else {
982981
continue;

crates/bevy_pbr/src/render/light.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,9 @@ pub fn specialize_shadows<M: Material>(
18091809
.or_default();
18101810

18111811
for (_, visible_entity) in visible_entities.iter().copied() {
1812+
let Some(material_asset_id) = render_material_instances.get(&visible_entity) else {
1813+
continue;
1814+
};
18121815
let entity_tick = entity_specialization_ticks.get(&visible_entity).unwrap();
18131816
let last_specialized_tick = view_specialized_material_pipeline_cache
18141817
.get(&visible_entity)
@@ -1820,7 +1823,9 @@ pub fn specialize_shadows<M: Material>(
18201823
if !needs_specialization {
18211824
continue;
18221825
}
1823-
1826+
let Some(material) = render_materials.get(*material_asset_id) else {
1827+
continue;
1828+
};
18241829
let Some(mesh_instance) =
18251830
render_mesh_instances.render_mesh_queue_data(visible_entity)
18261831
else {
@@ -1832,12 +1837,6 @@ pub fn specialize_shadows<M: Material>(
18321837
{
18331838
continue;
18341839
}
1835-
let Some(material_asset_id) = render_material_instances.get(&visible_entity) else {
1836-
continue;
1837-
};
1838-
let Some(material) = render_materials.get(*material_asset_id) else {
1839-
continue;
1840-
};
18411840
let Some(material_bind_group) =
18421841
material_bind_group_allocator.get(material.binding.group)
18431842
else {

crates/bevy_sprite/src/mesh2d/material.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,9 @@ pub fn specialize_material2d_meshes<M: Material2d>(
698698
.or_default();
699699

700700
for (_, visible_entity) in visible_entities.iter::<Mesh2d>() {
701+
let Some(material_asset_id) = render_material_instances.get(visible_entity) else {
702+
continue;
703+
};
701704
let entity_tick = entity_specialization_ticks.get(visible_entity).unwrap();
702705
let last_specialized_tick = view_specialized_material_pipeline_cache
703706
.get(visible_entity)
@@ -709,10 +712,6 @@ pub fn specialize_material2d_meshes<M: Material2d>(
709712
if !needs_specialization {
710713
continue;
711714
}
712-
713-
let Some(material_asset_id) = render_material_instances.get(visible_entity) else {
714-
continue;
715-
};
716715
let Some(mesh_instance) = render_mesh_instances.get_mut(visible_entity) else {
717716
continue;
718717
};

0 commit comments

Comments
 (0)