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

Synchronize removed components with the render world #15582

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/auto_exposure/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy_ecs::prelude::*;
use bevy_render::{
render_resource::{StorageBuffer, UniformBuffer},
renderer::{RenderDevice, RenderQueue},
world_sync::RenderEntity,
sync_world::RenderEntity,
Extract,
};
use bevy_utils::{Entry, HashMap};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/core_2d/camera_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};
use bevy_ecs::prelude::*;
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::world_sync::SyncToRenderWorld;
use bevy_render::sync_world::SyncToRenderWorld;
use bevy_render::{
camera::{
Camera, CameraMainTextureUsages, CameraProjection, CameraRenderGraph,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/core_2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use bevy_render::{
renderer::RenderDevice,
texture::TextureCache,
view::{Msaa, ViewDepthTexture},
world_sync::RenderEntity,
sync_world::RenderEntity,
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/core_3d/camera_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use bevy_render::{
primitives::Frustum,
render_resource::{LoadOp, TextureUsages},
view::{ColorGrading, Msaa, VisibleEntities},
world_sync::SyncToRenderWorld,
sync_world::SyncToRenderWorld,
};
use bevy_transform::prelude::{GlobalTransform, Transform};
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ use bevy_render::{
renderer::RenderDevice,
texture::{BevyDefault, ColorAttachment, Image, TextureCache},
view::{ExtractedView, ViewDepthTexture, ViewTarget},
world_sync::RenderEntity,
sync_world::RenderEntity,
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_utils::{tracing::warn, HashMap};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/dof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use bevy_render::{
prepare_view_targets, ExtractedView, Msaa, ViewDepthTexture, ViewTarget, ViewUniform,
ViewUniformOffset, ViewUniforms,
},
world_sync::RenderEntity,
sync_world::RenderEntity,
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_utils::{info_once, prelude::default, warn_once};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/taa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use bevy_render::{
renderer::{RenderContext, RenderDevice},
texture::{BevyDefault, CachedTexture, TextureCache},
view::{ExtractedView, Msaa, ViewTarget},
world_sync::RenderEntity,
sync_world::RenderEntity,
ExtractSchedule, MainWorld, Render, RenderApp, RenderSet,
};
use bevy_utils::tracing::warn;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ use {
ShaderStages, ShaderType, VertexFormat,
},
renderer::RenderDevice,
world_sync::TemporaryRenderEntity,
sync_world::TemporaryRenderEntity,
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
},
bytemuck::cast_slice,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use bevy_render::{
mesh::Mesh3d,
primitives::{CascadesFrusta, CubemapFrusta, Frustum},
view::{InheritedVisibility, ViewVisibility, Visibility},
world_sync::SyncToRenderWorld,
sync_world::SyncToRenderWorld,
};
use bevy_transform::components::{GlobalTransform, Transform};

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/cluster/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use bevy_render::{
UniformBuffer,
},
renderer::{RenderDevice, RenderQueue},
world_sync::RenderEntity,
sync_world::RenderEntity,
Extract,
};
use bevy_utils::{hashbrown::HashSet, tracing::warn};
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ use bevy_render::{
render_asset::prepare_assets,
render_graph::RenderGraph,
render_resource::Shader,
sync_component::SyncComponentPlugin,
texture::{GpuImage, Image},
view::{check_visibility, VisibilitySystems},
ExtractSchedule, Render, RenderApp, RenderSet,
Expand Down Expand Up @@ -314,7 +315,6 @@ impl Plugin for PbrPlugin {
.register_type::<PointLight>()
.register_type::<PointLightShadowMap>()
.register_type::<SpotLight>()
.register_type::<DistanceFog>()
kristoff3r marked this conversation as resolved.
Show resolved Hide resolved
.register_type::<ShadowFilteringMethod>()
.init_resource::<AmbientLight>()
.init_resource::<GlobalVisibleClusterableObjects>()
Expand Down Expand Up @@ -346,6 +346,11 @@ impl Plugin for PbrPlugin {
VolumetricFogPlugin,
ScreenSpaceReflectionsPlugin,
))
.add_plugins((
SyncComponentPlugin::<DirectionalLight>::default(),
SyncComponentPlugin::<PointLight>::default(),
SyncComponentPlugin::<SpotLight>::default(),
))
.configure_sets(
PostUpdate,
(
Expand Down
5 changes: 2 additions & 3 deletions crates/bevy_pbr/src/light/directional_light.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_render::{view::Visibility, world_sync::SyncToRenderWorld};
use bevy_render::view::Visibility;

use super::*;

Expand Down Expand Up @@ -57,8 +57,7 @@ use super::*;
CascadeShadowConfig,
CascadesVisibleEntities,
Transform,
Visibility,
SyncToRenderWorld
Visibility
)]
pub struct DirectionalLight {
/// The color of the light.
Expand Down
10 changes: 2 additions & 8 deletions crates/bevy_pbr/src/light/point_light.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_render::{view::Visibility, world_sync::SyncToRenderWorld};
use bevy_render::view::Visibility;

use super::*;

Expand All @@ -21,13 +21,7 @@ use super::*;
/// Source: [Wikipedia](https://en.wikipedia.org/wiki/Lumen_(unit)#Lighting)
#[derive(Component, Debug, Clone, Copy, Reflect)]
#[reflect(Component, Default, Debug)]
#[require(
CubemapFrusta,
CubemapVisibleEntities,
Transform,
Visibility,
SyncToRenderWorld
)]
#[require(CubemapFrusta, CubemapVisibleEntities, Transform, Visibility)]
pub struct PointLight {
/// The color of this light source.
pub color: Color,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/light/spot_light.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_render::{view::Visibility, world_sync::SyncToRenderWorld};
use bevy_render::view::Visibility;

use super::*;

Expand All @@ -9,7 +9,7 @@ use super::*;
/// the transform, and can be specified with [`Transform::looking_at`](Transform::looking_at).
#[derive(Component, Debug, Clone, Copy, Reflect)]
#[reflect(Component, Default, Debug)]
#[require(Frustum, VisibleMeshEntities, Transform, Visibility, SyncToRenderWorld)]
#[require(Frustum, VisibleMeshEntities, Transform, Visibility)]
pub struct SpotLight {
/// The color of the light.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/light_probe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use bevy_render::{
settings::WgpuFeatures,
texture::{FallbackImage, GpuImage, Image},
view::ExtractedView,
world_sync::RenderEntity,
sync_world::RenderEntity,
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_transform::{components::Transform, prelude::GlobalTransform};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod prepass_bindings;
use bevy_render::{
mesh::{Mesh3d, MeshVertexBufferLayoutRef, RenderMesh},
render_resource::binding_types::uniform_buffer,
world_sync::RenderEntity,
sync_world::RenderEntity,
};
pub use prepass_bindings::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy_ecs::{
system::lifetimeless::Read,
};
use bevy_math::{ops, Mat4, UVec4, Vec2, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles};
use bevy_render::world_sync::RenderEntity;
use bevy_render::sync_world::RenderEntity;
use bevy_render::{
diagnostic::RecordDiagnostics,
mesh::RenderMesh,
Expand Down
19 changes: 13 additions & 6 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use bevy_render::{
},
render_resource::*,
renderer::{RenderDevice, RenderQueue},
sync_world::RenderEntity,
texture::{BevyDefault, DefaultImageSampler, ImageSampler, TextureFormatPixelInfo},
view::{
prepare_view_targets, GpuCulling, RenderVisibilityRanges, ViewTarget, ViewUniformOffset,
Expand Down Expand Up @@ -557,11 +558,11 @@ pub enum RenderMeshInstanceGpuQueue {
/// The version of [`RenderMeshInstanceGpuQueue`] that omits the
/// [`MeshCullingData`], so that we don't waste space when GPU
/// culling is disabled.
CpuCulling(Vec<(Entity, RenderMeshInstanceGpuBuilder)>),
CpuCulling(Vec<(RenderEntity, RenderMeshInstanceGpuBuilder)>),
/// The version of [`RenderMeshInstanceGpuQueue`] that contains the
/// [`MeshCullingData`], used when any view has GPU culling
/// enabled.
GpuCulling(Vec<(Entity, RenderMeshInstanceGpuBuilder, MeshCullingData)>),
GpuCulling(Vec<(RenderEntity, RenderMeshInstanceGpuBuilder, MeshCullingData)>),
kristoff3r marked this conversation as resolved.
Show resolved Hide resolved
}

/// The per-thread queues containing mesh instances, populated during the
Expand Down Expand Up @@ -739,7 +740,7 @@ impl RenderMeshInstanceGpuQueue {
/// Adds a new mesh to this queue.
fn push(
&mut self,
entity: Entity,
entity: RenderEntity,
instance_builder: RenderMeshInstanceGpuBuilder,
culling_data_builder: Option<MeshCullingData>,
) {
Expand Down Expand Up @@ -971,6 +972,7 @@ pub fn extract_meshes_for_gpu_building(
meshes_query: Extract<
Query<(
Entity,
&RenderEntity,
&ViewVisibility,
&GlobalTransform,
Option<&PreviousGlobalTransform>,
Expand Down Expand Up @@ -1005,6 +1007,7 @@ pub fn extract_meshes_for_gpu_building(
|queue,
(
entity,
render_entity,
view_visibility,
transform,
previous_transform,
Expand Down Expand Up @@ -1063,7 +1066,11 @@ pub fn extract_meshes_for_gpu_building(
previous_input_index,
};

queue.push(entity, gpu_mesh_instance_builder, gpu_mesh_culling_data);
queue.push(
*render_entity,
kristoff3r marked this conversation as resolved.
Show resolved Hide resolved
gpu_mesh_instance_builder,
gpu_mesh_culling_data,
);
},
);
}
Expand Down Expand Up @@ -1136,7 +1143,7 @@ pub fn collect_meshes_for_gpu_building(
RenderMeshInstanceGpuQueue::CpuCulling(ref mut queue) => {
for (entity, mesh_instance_builder) in queue.drain(..) {
mesh_instance_builder.add_to(
entity,
entity.id(),
&mut *render_mesh_instances,
current_input_buffer,
&mesh_allocator,
Expand All @@ -1146,7 +1153,7 @@ pub fn collect_meshes_for_gpu_building(
RenderMeshInstanceGpuQueue::GpuCulling(ref mut queue) => {
for (entity, mesh_instance_builder, mesh_culling_builder) in queue.drain(..) {
let instance_data_index = mesh_instance_builder.add_to(
entity,
entity.id(),
&mut *render_mesh_instances,
current_input_buffer,
&mesh_allocator,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/ssao/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use bevy_render::{
renderer::{RenderAdapter, RenderContext, RenderDevice, RenderQueue},
texture::{CachedTexture, TextureCache},
view::{Msaa, ViewUniform, ViewUniformOffset, ViewUniforms},
world_sync::RenderEntity,
sync_world::RenderEntity,
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_utils::{
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_pbr/src/volumetric_fog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use bevy_render::{
mesh::{Mesh, Meshable},
render_graph::{RenderGraphApp, ViewNodeRunner},
render_resource::{Shader, SpecializedRenderPipelines},
sync_component::SyncComponentPlugin,
texture::Image,
view::{InheritedVisibility, ViewVisibility, Visibility},
ExtractSchedule, Render, RenderApp, RenderSet,
Expand Down Expand Up @@ -231,6 +232,8 @@ impl Plugin for VolumetricFogPlugin {
app.register_type::<VolumetricFog>()
.register_type::<VolumetricLight>();

app.add_plugins(SyncComponentPlugin::<FogVolume>::default());

let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/volumetric_fog/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use bevy_render::{
renderer::{RenderContext, RenderDevice, RenderQueue},
texture::{BevyDefault as _, GpuImage, Image},
view::{ExtractedView, Msaa, ViewDepthTexture, ViewTarget, ViewUniformOffset},
world_sync::RenderEntity,
sync_world::RenderEntity,
Extract,
};
use bevy_transform::components::GlobalTransform;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
ColorGrading, ExtractedView, ExtractedWindows, GpuCulling, Msaa, RenderLayers, Visibility,
VisibleEntities,
},
world_sync::{RenderEntity, SyncToRenderWorld},
sync_world::{RenderEntity, SyncToRenderWorld},
Extract,
};
use bevy_asset::{AssetEvent, AssetId, Assets, Handle};
Expand Down
23 changes: 6 additions & 17 deletions crates/bevy_render/src/extract_component.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{
render_resource::{encase::internal::WriteInto, DynamicUniformBuffer, ShaderType},
renderer::{RenderDevice, RenderQueue},
sync_component::SyncComponentPlugin,
sync_world::RenderEntity,
view::ViewVisibility,
world_sync::{RenderEntity, SyncToRenderWorld},
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};
use bevy_app::{App, Plugin};
Expand All @@ -12,7 +13,6 @@ use bevy_ecs::{
prelude::*,
query::{QueryFilter, QueryItem, ReadOnlyQueryData},
system::lifetimeless::Read,
world::OnAdd,
};
use core::{marker::PhantomData, ops::Deref};

Expand Down Expand Up @@ -160,15 +160,6 @@ fn prepare_uniform_components<C>(
/// This plugin extracts the components into the render world for synced entities.
///
/// To do so, it sets up the [`ExtractSchedule`] step for the specified [`ExtractComponent`].
///
/// # Warning
///
/// Be careful when removing the [`ExtractComponent`] from an entity. When an [`ExtractComponent`]
/// is added to an entity, that entity is automatically synced with the render world (see also
/// [`WorldSyncPlugin`](crate::world_sync::WorldSyncPlugin)). When removing the entity in the main
/// world, the synced entity also gets removed. However, if only the [`ExtractComponent`] is removed
/// this *doesn't* happen, and the synced entity stays around with the old extracted data.
/// We recommend despawning the entire entity, instead of only removing [`ExtractComponent`].
kristoff3r marked this conversation as resolved.
Show resolved Hide resolved
pub struct ExtractComponentPlugin<C, F = ()> {
only_extract_visible: bool,
marker: PhantomData<fn() -> (C, F)>,
Expand All @@ -194,10 +185,8 @@ impl<C, F> ExtractComponentPlugin<C, F> {

impl<C: ExtractComponent> Plugin for ExtractComponentPlugin<C> {
fn build(&self, app: &mut App) {
// TODO: use required components
app.observe(|trigger: Trigger<OnAdd, C>, mut commands: Commands| {
commands.entity(trigger.entity()).insert(SyncToRenderWorld);
});
app.add_plugins(SyncComponentPlugin::<C>::default());

if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
if self.only_extract_visible {
render_app.add_systems(ExtractSchedule, extract_visible_components::<C>);
Expand All @@ -219,7 +208,7 @@ impl<T: Asset> ExtractComponent for Handle<T> {
}
}

/// This system extracts all components of the corresponding [`ExtractComponent`], for entities that are synced via [`SyncToRenderWorld`].
/// This system extracts all components of the corresponding [`ExtractComponent`], for entities that are synced via [`crate::sync_world::SyncToRenderWorld`].
fn extract_components<C: ExtractComponent>(
mut commands: Commands,
mut previous_len: Local<usize>,
Expand All @@ -235,7 +224,7 @@ fn extract_components<C: ExtractComponent>(
commands.insert_or_spawn_batch(values);
}

/// This system extracts all components of the corresponding [`ExtractComponent`], for entities that are visible and synced via [`SyncToRenderWorld`].
/// This system extracts all components of the corresponding [`ExtractComponent`], for entities that are visible and synced via [`crate::sync_world::SyncToRenderWorld`].
fn extract_visible_components<C: ExtractComponent>(
mut commands: Commands,
mut previous_len: Local<usize>,
Expand Down
Loading
Loading