Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor check_light_mesh_visibility for performance #1 #13905

Merged
merged 5 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ impl Plugin for PbrPlugin {
.after(TransformSystem::TransformPropagate)
.after(SimulationLightSystems::AssignLightsToClusters),
check_visibility::<WithLight>.in_set(VisibilitySystems::CheckVisibility),
check_light_mesh_visibility
(
check_dir_light_mesh_visibility,
check_point_light_mesh_visibility,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these systems need to be ordered relative to each other?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not be necessary. The main reason for splitting the system is that in subsequent PRs, we hope they can be automatically parallelized by the bevy's scheduler.

)
.in_set(SimulationLightSystems::CheckLightVisibility)
.after(VisibilitySystems::CalculateBounds)
.after(TransformSystem::TransformPropagate)
Expand Down
86 changes: 54 additions & 32 deletions crates/bevy_pbr/src/light/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,22 +638,23 @@ pub fn update_spot_light_frusta(
}
}

pub fn check_light_mesh_visibility(
visible_point_lights: Query<&VisibleClusterableObjects>,
mut point_lights: Query<(
&PointLight,
&GlobalTransform,
&CubemapFrusta,
&mut CubemapVisibleEntities,
Option<&RenderLayers>,
)>,
mut spot_lights: Query<(
&SpotLight,
&GlobalTransform,
&Frustum,
&mut VisibleEntities,
Option<&RenderLayers>,
)>,
fn shrink_entities(visible_entities: &mut Vec<Entity>) {
// Check that visible entities capacity() is no more than two times greater than len()
let capacity = visible_entities.capacity();
let reserved = capacity
.checked_div(visible_entities.len())
.map_or(0, |reserve| {
if reserve > 2 {
capacity / (reserve / 2)
} else {
capacity
}
});

visible_entities.shrink_to(reserved);
}

pub fn check_dir_light_mesh_visibility(
mut directional_lights: Query<
(
&DirectionalLight,
Expand Down Expand Up @@ -682,22 +683,6 @@ pub fn check_light_mesh_visibility(
>,
visible_entity_ranges: Option<Res<VisibleEntityRanges>>,
) {
fn shrink_entities(visible_entities: &mut Vec<Entity>) {
// Check that visible entities capacity() is no more than two times greater than len()
let capacity = visible_entities.capacity();
let reserved = capacity
.checked_div(visible_entities.len())
.map_or(0, |reserve| {
if reserve > 2 {
capacity / (reserve / 2)
} else {
capacity
}
});

visible_entities.shrink_to(reserved);
}

let visible_entity_ranges = visible_entity_ranges.as_deref();

// Directional lights
Expand Down Expand Up @@ -804,6 +789,43 @@ pub fn check_light_mesh_visibility(
.for_each(shrink_entities);
}
}
}

pub fn check_point_light_mesh_visibility(
visible_point_lights: Query<&VisibleClusterableObjects>,
mut point_lights: Query<(
&PointLight,
&GlobalTransform,
&CubemapFrusta,
&mut CubemapVisibleEntities,
Option<&RenderLayers>,
)>,
mut spot_lights: Query<(
&SpotLight,
&GlobalTransform,
&Frustum,
&mut VisibleEntities,
Option<&RenderLayers>,
)>,
mut visible_entity_query: Query<
(
Entity,
&InheritedVisibility,
&mut ViewVisibility,
Option<&RenderLayers>,
Option<&Aabb>,
Option<&GlobalTransform>,
Has<VisibilityRange>,
),
(
Without<NotShadowCaster>,
Without<DirectionalLight>,
With<Handle<Mesh>>,
),
>,
visible_entity_ranges: Option<Res<VisibleEntityRanges>>,
) {
let visible_entity_ranges = visible_entity_ranges.as_deref();

for visible_lights in &visible_point_lights {
for light_entity in visible_lights.entities.iter().copied() {
Expand Down