Skip to content

Commit 91cd84f

Browse files
authored
Refactor check_light_mesh_visibility for performance #1 (bevyengine#13905)
# Objective - first part of bevyengine#13900 ## Solution - split `check_light_mesh_visibility `into `check_dir_light_mesh_visibility `and `check_point_light_mesh_visibility` for better review
1 parent 41ad4e9 commit 91cd84f

File tree

2 files changed

+58
-33
lines changed

2 files changed

+58
-33
lines changed

crates/bevy_pbr/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,10 @@ impl Plugin for PbrPlugin {
368368
.after(TransformSystem::TransformPropagate)
369369
.after(SimulationLightSystems::AssignLightsToClusters),
370370
check_visibility::<WithLight>.in_set(VisibilitySystems::CheckVisibility),
371-
check_light_mesh_visibility
371+
(
372+
check_dir_light_mesh_visibility,
373+
check_point_light_mesh_visibility,
374+
)
372375
.in_set(SimulationLightSystems::CheckLightVisibility)
373376
.after(VisibilitySystems::CalculateBounds)
374377
.after(TransformSystem::TransformPropagate)

crates/bevy_pbr/src/light/mod.rs

+54-32
Original file line numberDiff line numberDiff line change
@@ -638,22 +638,23 @@ pub fn update_spot_light_frusta(
638638
}
639639
}
640640

641-
pub fn check_light_mesh_visibility(
642-
visible_point_lights: Query<&VisibleClusterableObjects>,
643-
mut point_lights: Query<(
644-
&PointLight,
645-
&GlobalTransform,
646-
&CubemapFrusta,
647-
&mut CubemapVisibleEntities,
648-
Option<&RenderLayers>,
649-
)>,
650-
mut spot_lights: Query<(
651-
&SpotLight,
652-
&GlobalTransform,
653-
&Frustum,
654-
&mut VisibleEntities,
655-
Option<&RenderLayers>,
656-
)>,
641+
fn shrink_entities(visible_entities: &mut Vec<Entity>) {
642+
// Check that visible entities capacity() is no more than two times greater than len()
643+
let capacity = visible_entities.capacity();
644+
let reserved = capacity
645+
.checked_div(visible_entities.len())
646+
.map_or(0, |reserve| {
647+
if reserve > 2 {
648+
capacity / (reserve / 2)
649+
} else {
650+
capacity
651+
}
652+
});
653+
654+
visible_entities.shrink_to(reserved);
655+
}
656+
657+
pub fn check_dir_light_mesh_visibility(
657658
mut directional_lights: Query<
658659
(
659660
&DirectionalLight,
@@ -682,22 +683,6 @@ pub fn check_light_mesh_visibility(
682683
>,
683684
visible_entity_ranges: Option<Res<VisibleEntityRanges>>,
684685
) {
685-
fn shrink_entities(visible_entities: &mut Vec<Entity>) {
686-
// Check that visible entities capacity() is no more than two times greater than len()
687-
let capacity = visible_entities.capacity();
688-
let reserved = capacity
689-
.checked_div(visible_entities.len())
690-
.map_or(0, |reserve| {
691-
if reserve > 2 {
692-
capacity / (reserve / 2)
693-
} else {
694-
capacity
695-
}
696-
});
697-
698-
visible_entities.shrink_to(reserved);
699-
}
700-
701686
let visible_entity_ranges = visible_entity_ranges.as_deref();
702687

703688
// Directional lights
@@ -804,6 +789,43 @@ pub fn check_light_mesh_visibility(
804789
.for_each(shrink_entities);
805790
}
806791
}
792+
}
793+
794+
pub fn check_point_light_mesh_visibility(
795+
visible_point_lights: Query<&VisibleClusterableObjects>,
796+
mut point_lights: Query<(
797+
&PointLight,
798+
&GlobalTransform,
799+
&CubemapFrusta,
800+
&mut CubemapVisibleEntities,
801+
Option<&RenderLayers>,
802+
)>,
803+
mut spot_lights: Query<(
804+
&SpotLight,
805+
&GlobalTransform,
806+
&Frustum,
807+
&mut VisibleEntities,
808+
Option<&RenderLayers>,
809+
)>,
810+
mut visible_entity_query: Query<
811+
(
812+
Entity,
813+
&InheritedVisibility,
814+
&mut ViewVisibility,
815+
Option<&RenderLayers>,
816+
Option<&Aabb>,
817+
Option<&GlobalTransform>,
818+
Has<VisibilityRange>,
819+
),
820+
(
821+
Without<NotShadowCaster>,
822+
Without<DirectionalLight>,
823+
With<Handle<Mesh>>,
824+
),
825+
>,
826+
visible_entity_ranges: Option<Res<VisibleEntityRanges>>,
827+
) {
828+
let visible_entity_ranges = visible_entity_ranges.as_deref();
807829

808830
for visible_lights in &visible_point_lights {
809831
for light_entity in visible_lights.entities.iter().copied() {

0 commit comments

Comments
 (0)