From 8340a3bbd48eb053a2d55f0ce4173bca9ae1e672 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Mon, 9 Jan 2023 13:41:59 +0000 Subject: [PATCH] bevy_render: Run calculate_bounds in the end-of-update exclusive systems (#7127) # Objective - Avoid slower than necessary first frame after spawning many entities due to them not having `Aabb`s and so being marked visible - Avoids unnecessarily large system and VRAM allocations as a consequence ## Solution - I noticed when debugging the `many_cubes` stress test in Xcode that the `MeshUniform` binding was much larger than it needed to be. I realised that this was because initially, all mesh entities are marked as being visible because they don't have `Aabb`s because `calculate_bounds` is being run in `PostUpdate` and there are no system commands applications before executing the visibility check systems that need the `Aabb`s. The solution then is to run the `calculate_bounds` system just before the previous system commands are applied which is at the end of the `Update` stage. --- crates/bevy_pbr/src/lib.rs | 1 - crates/bevy_render/src/view/visibility/mod.rs | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/bevy_pbr/src/lib.rs b/crates/bevy_pbr/src/lib.rs index 366456b51d3fe..8366dbe673e1f 100644 --- a/crates/bevy_pbr/src/lib.rs +++ b/crates/bevy_pbr/src/lib.rs @@ -189,7 +189,6 @@ impl Plugin for PbrPlugin { check_light_mesh_visibility .label(SimulationLightSystems::CheckLightVisibility) .after(TransformSystem::TransformPropagate) - .after(VisibilitySystems::CalculateBounds) .after(SimulationLightSystems::UpdateLightFrusta) // NOTE: This MUST be scheduled AFTER the core renderer visibility check // because that resets entity ComputedVisibility for the first view diff --git a/crates/bevy_render/src/view/visibility/mod.rs b/crates/bevy_render/src/view/visibility/mod.rs index e4a57217c1bc1..f3381ac74c790 100644 --- a/crates/bevy_render/src/view/visibility/mod.rs +++ b/crates/bevy_render/src/view/visibility/mod.rs @@ -212,7 +212,7 @@ impl Plugin for VisibilityPlugin { app.add_system_to_stage( CoreStage::PostUpdate, - calculate_bounds.label(CalculateBounds), + calculate_bounds.label(CalculateBounds).before_commands(), ) .add_system_to_stage( CoreStage::PostUpdate, @@ -252,7 +252,6 @@ impl Plugin for VisibilityPlugin { CoreStage::PostUpdate, check_visibility .label(CheckVisibility) - .after(CalculateBounds) .after(UpdateOrthographicFrusta) .after(UpdatePerspectiveFrusta) .after(UpdateProjectionFrusta)