From 69bc1b2ff10a5bb0815fe25b9c269f075b220eef Mon Sep 17 00:00:00 2001 From: Zoey Date: Thu, 24 Mar 2022 02:11:52 -0700 Subject: [PATCH 1/4] bevy_render: Reserve only entities from the last frame --- crates/bevy_render/src/lib.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index 5d7127c0ce7b1..e39dbf352c247 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -107,6 +107,10 @@ pub struct RenderApp; #[derive(Default)] struct ScratchRenderWorld(World); +/// The number of entities used in the render world in the previous frame. +#[derive(Default)] +struct ReserveCount(u32); + impl Plugin for RenderPlugin { /// Initializes the renderer, sets up the [`RenderStage`](RenderStage) and creates the rendering sub-app. fn build(&self, app: &mut App) { @@ -176,7 +180,8 @@ impl Plugin for RenderPlugin { .insert_resource(adapter_info) .insert_resource(pipeline_cache) .insert_resource(asset_server) - .init_resource::(); + .init_resource::() + .init_resource::(); app.add_sub_app(RenderApp, render_app, move |app_world, render_app| { #[cfg(feature = "trace")] @@ -187,13 +192,10 @@ impl Plugin for RenderPlugin { bevy_utils::tracing::info_span!("stage", name = "reserve_and_flush") .entered(); - // reserve all existing app entities for use in render_app + // reserve the number of entities used in the previous frame for use in render_app // they can only be spawned using `get_or_spawn()` - let meta_len = app_world.entities().meta_len(); - render_app - .world - .entities() - .reserve_entities(meta_len as u32); + let count = render_app.world.resource::().0; + render_app.world.entities().reserve_entities(count); // flushing as "invalid" ensures that app world entities aren't added as "empty archetype" entities by default // these entities cannot be accessed without spawning directly onto them @@ -274,6 +276,9 @@ impl Plugin for RenderPlugin { .unwrap(); cleanup.run(&mut render_app.world); + render_app.world.resource_mut::().0 = + render_app.world.entities().meta.len() as u32; + render_app.world.clear_entities(); } }); From 32d78339c86bf48a5d13307e69558e6d4d981dde Mon Sep 17 00:00:00 2001 From: Zoey Date: Thu, 24 Mar 2022 10:47:06 -0700 Subject: [PATCH 2/4] Rename `ReserveCount` to be more descriptive Co-authored-by: Alice Cecile --- crates/bevy_render/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index e39dbf352c247..30dbf45c6fc9d 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -109,7 +109,7 @@ struct ScratchRenderWorld(World); /// The number of entities used in the render world in the previous frame. #[derive(Default)] -struct ReserveCount(u32); +struct NumberOfRenderingEntitiesToReserve(u32); impl Plugin for RenderPlugin { /// Initializes the renderer, sets up the [`RenderStage`](RenderStage) and creates the rendering sub-app. From c48db70e00b824d653c5531cb1d12bc1829efe0c Mon Sep 17 00:00:00 2001 From: Zoey Date: Thu, 24 Mar 2022 10:49:08 -0700 Subject: [PATCH 3/4] Fix missing renamed `NumberOfRenderingEntitiesToReserve` --- crates/bevy_render/src/lib.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index 30dbf45c6fc9d..029a1a0dcf5b0 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -181,7 +181,7 @@ impl Plugin for RenderPlugin { .insert_resource(pipeline_cache) .insert_resource(asset_server) .init_resource::() - .init_resource::(); + .init_resource::(); app.add_sub_app(RenderApp, render_app, move |app_world, render_app| { #[cfg(feature = "trace")] @@ -194,7 +194,10 @@ impl Plugin for RenderPlugin { // reserve the number of entities used in the previous frame for use in render_app // they can only be spawned using `get_or_spawn()` - let count = render_app.world.resource::().0; + let count = render_app + .world + .resource::() + .0; render_app.world.entities().reserve_entities(count); // flushing as "invalid" ensures that app world entities aren't added as "empty archetype" entities by default @@ -276,8 +279,10 @@ impl Plugin for RenderPlugin { .unwrap(); cleanup.run(&mut render_app.world); - render_app.world.resource_mut::().0 = - render_app.world.entities().meta.len() as u32; + render_app + .world + .resource_mut::() + .0 = render_app.world.entities().meta.len() as u32; render_app.world.clear_entities(); } From 07fceecbfd8ddffb9fa4b3168c367c5360e6ac6e Mon Sep 17 00:00:00 2001 From: Zoey Date: Fri, 13 May 2022 13:37:46 -0700 Subject: [PATCH 4/4] Fix access error --- crates/bevy_render/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index 029a1a0dcf5b0..b968abd308c23 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -282,7 +282,7 @@ impl Plugin for RenderPlugin { render_app .world .resource_mut::() - .0 = render_app.world.entities().meta.len() as u32; + .0 = render_app.world.entities().meta_len() as u32; render_app.world.clear_entities(); }