Skip to content

Commit

Permalink
Only extract added entities, stop nuking render world
Browse files Browse the repository at this point in the history
  • Loading branch information
Trashtalk217 committed Jul 8, 2024
1 parent 09d86bf commit b2c1414
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 deletions.
10 changes: 5 additions & 5 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub fn extract_lights(
&GlobalTransform,
&ViewVisibility,
&CubemapFrusta,
)>,
), Added<PointLight>>,
>,
spot_lights: Extract<
Query<(
Expand All @@ -190,7 +190,7 @@ pub fn extract_lights(
&GlobalTransform,
&ViewVisibility,
&Frustum,
)>,
), Added<SpotLight>>,
>,
directional_lights: Extract<
Query<
Expand All @@ -206,7 +206,7 @@ pub fn extract_lights(
Option<&RenderLayers>,
Option<&VolumetricLight>,
),
Without<SpotLight>,
(Without<SpotLight>, Added<DirectionalLight>),
>,
>,
mut previous_point_lights_len: Local<usize>,
Expand Down Expand Up @@ -531,8 +531,8 @@ pub fn prepare_lights(
Entity,
&ExtractedPointLight,
AnyOf<(&CubemapFrusta, &Frustum)>,
)>,
directional_lights: Query<(Entity, &ExtractedDirectionalLight)>,
), Added<ExtractedPointLight>>,
directional_lights: Query<(Entity, &ExtractedDirectionalLight), Added<ExtractedDirectionalLight>>,
mut live_shadow_mapping_lights: Local<EntityHashSet>,
) {
let views_iter = views.iter();
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
use bevy_asset::{AssetEvent, AssetId, Assets, Handle};
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{
prelude::*,
change_detection::DetectChanges,
component::Component,
entity::Entity,
Expand Down Expand Up @@ -842,7 +843,7 @@ pub fn extract_cameras(
Option<&RenderLayers>,
Option<&Projection>,
Has<GpuCulling>,
)>,
), Added<Camera>>,
>,
primary_window: Extract<Query<Entity, With<PrimaryWindow>>>,
gpu_preprocessing_support: Res<GpuPreprocessingSupport>,
Expand Down
15 changes: 9 additions & 6 deletions crates/bevy_render/src/extract_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use std::{marker::PhantomData, ops::Deref};

pub use bevy_render_macros::ExtractComponent;

#[derive(Component)]
pub struct MainWorldEntity(Entity);

/// Stores the index of a uniform inside of [`ComponentUniforms`].
#[derive(Component)]
pub struct DynamicUniformIndex<C: Component> {
Expand Down Expand Up @@ -209,32 +212,32 @@ impl<T: Asset> ExtractComponent for Handle<T> {
fn extract_components<C: ExtractComponent>(
mut commands: Commands,
mut previous_len: Local<usize>,
query: Extract<Query<(Entity, C::QueryData), C::QueryFilter>>,
query: Extract<Query<(Entity, C::QueryData), (C::QueryFilter, Added<C>)>>,
) {
let mut values = Vec::with_capacity(*previous_len);
for (entity, query_item) in &query {
if let Some(component) = C::extract_component(query_item) {
values.push((entity, component));
values.push((component, MainWorldEntity(entity)));
}
}
*previous_len = values.len();
commands.insert_or_spawn_batch(values);
commands.spawn_batch(values);
}

/// This system extracts all visible components of the corresponding [`ExtractComponent`] type.
fn extract_visible_components<C: ExtractComponent>(
mut commands: Commands,
mut previous_len: Local<usize>,
query: Extract<Query<(Entity, &ViewVisibility, C::QueryData), C::QueryFilter>>,
query: Extract<Query<(Entity, &ViewVisibility, C::QueryData), (C::QueryFilter, Added<C>)>>,
) {
let mut values = Vec::with_capacity(*previous_len);
for (entity, view_visibility, query_item) in &query {
if view_visibility.get() {
if let Some(component) = C::extract_component(query_item) {
values.push((entity, component));
values.push((component, MainWorldEntity(entity)));
}
}
}
*previous_len = values.len();
commands.insert_or_spawn_batch(values);
commands.spawn_batch(values);
}
21 changes: 16 additions & 5 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,7 @@ unsafe fn initialize_render_app(app: &mut App) {
render_system,
)
.in_set(RenderSet::Render),
World::clear_entities.in_set(RenderSet::Cleanup),
),
);
));

render_app.set_extract(|main_world, render_world| {
#[cfg(feature = "trace")]
Expand All @@ -479,7 +477,7 @@ unsafe fn initialize_render_app(app: &mut App) {
// reserve all existing main world entities for use in render_app
// they can only be spawned using `get_or_spawn()`
let total_count = main_world.entities().total_count();

/*
assert_eq!(
render_world.entities().len(),
0,
Expand All @@ -492,8 +490,21 @@ unsafe fn initialize_render_app(app: &mut App) {
.entities_mut()
.flush_and_reserve_invalid_assuming_no_entities(total_count);
}
*/
}

println!("{}", render_world.entities().len());
/*
if (render_world.entities().len() < 50) {
println!("Entities:");
for entity in render_world.iter_entities() {
print!("[");
for component_info in render_world.inspect_entity(entity.id()).take(5) {
print!("{:?}, ", component_info.name());
}
print!("] ");
}
}
*/
// run extract schedule
extract(main_world, render_world);
});
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/view/visibility/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{

use bevy_app::{App, Plugin, PostUpdate};
use bevy_ecs::{
prelude::*,
component::Component,
entity::Entity,
query::{Changed, With},
Expand Down Expand Up @@ -405,7 +406,7 @@ pub fn check_visibility_ranges(
/// render world and inserts them into [`RenderVisibilityRanges`].
pub fn extract_visibility_ranges(
mut render_visibility_ranges: ResMut<RenderVisibilityRanges>,
visibility_ranges_query: Extract<Query<(Entity, &VisibilityRange)>>,
visibility_ranges_query: Extract<Query<(Entity, &VisibilityRange), Added<VisibilityRange>>>,
changed_ranges_query: Extract<Query<Entity, Changed<VisibilityRange>>>,
) {
if changed_ranges_query.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/view/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn extract_windows(
mut extracted_windows: ResMut<ExtractedWindows>,
screenshot_manager: Extract<Res<ScreenshotManager>>,
mut closing: Extract<EventReader<WindowClosing>>,
windows: Extract<Query<(Entity, &Window, &RawHandleWrapper, Option<&PrimaryWindow>)>>,
windows: Extract<Query<(Entity, &Window, &RawHandleWrapper, Option<&PrimaryWindow>), Added<Window>>>,
mut removed: Extract<RemovedComponents<RawHandleWrapper>>,
mut window_surfaces: ResMut<WindowSurfaces>,
) {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ pub fn extract_default_ui_camera_view(
mut commands: Commands,
mut transparent_render_phases: ResMut<ViewSortedRenderPhases<TransparentUi>>,
ui_scale: Extract<Res<UiScale>>,
query: Extract<Query<(Entity, &Camera), Or<(With<Camera2d>, With<Camera3d>)>>>,
query: Extract<Query<(Entity, &Camera), (Or<(With<Camera2d>, With<Camera3d>)>, Added<Camera>)>>,
mut live_entities: Local<EntityHashSet>,
) {
live_entities.clear();
Expand Down

0 comments on commit b2c1414

Please sign in to comment.