Skip to content

Commit b2c1414

Browse files
committed
Only extract added entities, stop nuking render world
1 parent 09d86bf commit b2c1414

7 files changed

Lines changed: 36 additions & 20 deletions

File tree

crates/bevy_pbr/src/render/light.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub fn extract_lights(
181181
&GlobalTransform,
182182
&ViewVisibility,
183183
&CubemapFrusta,
184-
)>,
184+
), Added<PointLight>>,
185185
>,
186186
spot_lights: Extract<
187187
Query<(
@@ -190,7 +190,7 @@ pub fn extract_lights(
190190
&GlobalTransform,
191191
&ViewVisibility,
192192
&Frustum,
193-
)>,
193+
), Added<SpotLight>>,
194194
>,
195195
directional_lights: Extract<
196196
Query<
@@ -206,7 +206,7 @@ pub fn extract_lights(
206206
Option<&RenderLayers>,
207207
Option<&VolumetricLight>,
208208
),
209-
Without<SpotLight>,
209+
(Without<SpotLight>, Added<DirectionalLight>),
210210
>,
211211
>,
212212
mut previous_point_lights_len: Local<usize>,
@@ -531,8 +531,8 @@ pub fn prepare_lights(
531531
Entity,
532532
&ExtractedPointLight,
533533
AnyOf<(&CubemapFrusta, &Frustum)>,
534-
)>,
535-
directional_lights: Query<(Entity, &ExtractedDirectionalLight)>,
534+
), Added<ExtractedPointLight>>,
535+
directional_lights: Query<(Entity, &ExtractedDirectionalLight), Added<ExtractedDirectionalLight>>,
536536
mut live_shadow_mapping_lights: Local<EntityHashSet>,
537537
) {
538538
let views_iter = views.iter();

crates/bevy_render/src/camera/camera.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
use bevy_asset::{AssetEvent, AssetId, Assets, Handle};
1616
use bevy_derive::{Deref, DerefMut};
1717
use bevy_ecs::{
18+
prelude::*,
1819
change_detection::DetectChanges,
1920
component::Component,
2021
entity::Entity,
@@ -842,7 +843,7 @@ pub fn extract_cameras(
842843
Option<&RenderLayers>,
843844
Option<&Projection>,
844845
Has<GpuCulling>,
845-
)>,
846+
), Added<Camera>>,
846847
>,
847848
primary_window: Extract<Query<Entity, With<PrimaryWindow>>>,
848849
gpu_preprocessing_support: Res<GpuPreprocessingSupport>,

crates/bevy_render/src/extract_component.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ use std::{marker::PhantomData, ops::Deref};
1616

1717
pub use bevy_render_macros::ExtractComponent;
1818

19+
#[derive(Component)]
20+
pub struct MainWorldEntity(Entity);
21+
1922
/// Stores the index of a uniform inside of [`ComponentUniforms`].
2023
#[derive(Component)]
2124
pub struct DynamicUniformIndex<C: Component> {
@@ -209,32 +212,32 @@ impl<T: Asset> ExtractComponent for Handle<T> {
209212
fn extract_components<C: ExtractComponent>(
210213
mut commands: Commands,
211214
mut previous_len: Local<usize>,
212-
query: Extract<Query<(Entity, C::QueryData), C::QueryFilter>>,
215+
query: Extract<Query<(Entity, C::QueryData), (C::QueryFilter, Added<C>)>>,
213216
) {
214217
let mut values = Vec::with_capacity(*previous_len);
215218
for (entity, query_item) in &query {
216219
if let Some(component) = C::extract_component(query_item) {
217-
values.push((entity, component));
220+
values.push((component, MainWorldEntity(entity)));
218221
}
219222
}
220223
*previous_len = values.len();
221-
commands.insert_or_spawn_batch(values);
224+
commands.spawn_batch(values);
222225
}
223226

224227
/// This system extracts all visible components of the corresponding [`ExtractComponent`] type.
225228
fn extract_visible_components<C: ExtractComponent>(
226229
mut commands: Commands,
227230
mut previous_len: Local<usize>,
228-
query: Extract<Query<(Entity, &ViewVisibility, C::QueryData), C::QueryFilter>>,
231+
query: Extract<Query<(Entity, &ViewVisibility, C::QueryData), (C::QueryFilter, Added<C>)>>,
229232
) {
230233
let mut values = Vec::with_capacity(*previous_len);
231234
for (entity, view_visibility, query_item) in &query {
232235
if view_visibility.get() {
233236
if let Some(component) = C::extract_component(query_item) {
234-
values.push((entity, component));
237+
values.push((component, MainWorldEntity(entity)));
235238
}
236239
}
237240
}
238241
*previous_len = values.len();
239-
commands.insert_or_spawn_batch(values);
242+
commands.spawn_batch(values);
240243
}

crates/bevy_render/src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,7 @@ unsafe fn initialize_render_app(app: &mut App) {
463463
render_system,
464464
)
465465
.in_set(RenderSet::Render),
466-
World::clear_entities.in_set(RenderSet::Cleanup),
467-
),
468-
);
466+
));
469467

470468
render_app.set_extract(|main_world, render_world| {
471469
#[cfg(feature = "trace")]
@@ -479,7 +477,7 @@ unsafe fn initialize_render_app(app: &mut App) {
479477
// reserve all existing main world entities for use in render_app
480478
// they can only be spawned using `get_or_spawn()`
481479
let total_count = main_world.entities().total_count();
482-
480+
/*
483481
assert_eq!(
484482
render_world.entities().len(),
485483
0,
@@ -492,8 +490,21 @@ unsafe fn initialize_render_app(app: &mut App) {
492490
.entities_mut()
493491
.flush_and_reserve_invalid_assuming_no_entities(total_count);
494492
}
493+
*/
495494
}
496-
495+
println!("{}", render_world.entities().len());
496+
/*
497+
if (render_world.entities().len() < 50) {
498+
println!("Entities:");
499+
for entity in render_world.iter_entities() {
500+
print!("[");
501+
for component_info in render_world.inspect_entity(entity.id()).take(5) {
502+
print!("{:?}, ", component_info.name());
503+
}
504+
print!("] ");
505+
}
506+
}
507+
*/
497508
// run extract schedule
498509
extract(main_world, render_world);
499510
});

crates/bevy_render/src/view/visibility/range.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88

99
use bevy_app::{App, Plugin, PostUpdate};
1010
use bevy_ecs::{
11+
prelude::*,
1112
component::Component,
1213
entity::Entity,
1314
query::{Changed, With},
@@ -405,7 +406,7 @@ pub fn check_visibility_ranges(
405406
/// render world and inserts them into [`RenderVisibilityRanges`].
406407
pub fn extract_visibility_ranges(
407408
mut render_visibility_ranges: ResMut<RenderVisibilityRanges>,
408-
visibility_ranges_query: Extract<Query<(Entity, &VisibilityRange)>>,
409+
visibility_ranges_query: Extract<Query<(Entity, &VisibilityRange), Added<VisibilityRange>>>,
409410
changed_ranges_query: Extract<Query<Entity, Changed<VisibilityRange>>>,
410411
) {
411412
if changed_ranges_query.is_empty() {

crates/bevy_render/src/view/window/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn extract_windows(
118118
mut extracted_windows: ResMut<ExtractedWindows>,
119119
screenshot_manager: Extract<Res<ScreenshotManager>>,
120120
mut closing: Extract<EventReader<WindowClosing>>,
121-
windows: Extract<Query<(Entity, &Window, &RawHandleWrapper, Option<&PrimaryWindow>)>>,
121+
windows: Extract<Query<(Entity, &Window, &RawHandleWrapper, Option<&PrimaryWindow>), Added<Window>>>,
122122
mut removed: Extract<RemovedComponents<RawHandleWrapper>>,
123123
mut window_surfaces: ResMut<WindowSurfaces>,
124124
) {

crates/bevy_ui/src/render/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ pub fn extract_default_ui_camera_view(
725725
mut commands: Commands,
726726
mut transparent_render_phases: ResMut<ViewSortedRenderPhases<TransparentUi>>,
727727
ui_scale: Extract<Res<UiScale>>,
728-
query: Extract<Query<(Entity, &Camera), Or<(With<Camera2d>, With<Camera3d>)>>>,
728+
query: Extract<Query<(Entity, &Camera), (Or<(With<Camera2d>, With<Camera3d>)>, Added<Camera>)>>,
729729
mut live_entities: Local<EntityHashSet>,
730730
) {
731731
live_entities.clear();

0 commit comments

Comments
 (0)