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

Add RenderComponent trait for transient render world marker components #16011

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
6 changes: 5 additions & 1 deletion crates/bevy_core_pipeline/src/bloom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use settings::{
Bloom, BloomCompositeMode, BloomPrefilter, BloomPrefilterSettings, BloomSettings,
};

use crate::bloom::settings::UseBloom;
use crate::{
core_2d::graph::{Core2d, Node2d},
core_3d::graph::{Core3d, Node3d},
Expand All @@ -16,6 +17,7 @@ use bevy_app::{App, Plugin};
use bevy_asset::{load_internal_asset, Handle};
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_math::{ops, UVec2};
use bevy_render::render_component::RenderComponentPlugin;
use bevy_render::{
camera::ExtractedCamera,
diagnostic::RecordDiagnostics,
Expand Down Expand Up @@ -53,6 +55,7 @@ impl Plugin for BloomPlugin {
app.add_plugins((
ExtractComponentPlugin::<Bloom>::default(),
UniformComponentPlugin::<BloomUniforms>::default(),
RenderComponentPlugin::<UseBloom>::default(),
));

let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
Expand Down Expand Up @@ -107,6 +110,7 @@ impl ViewNode for BloomNode {
&'static UpsamplingPipelineIds,
&'static BloomDownsamplingPipelineIds,
);
type ViewFilter = With<UseBloom>;

// Atypically for a post-processing effect, we do not need to
// use a secondary texture normally provided by view_target.post_process_write(),
Expand Down Expand Up @@ -327,7 +331,7 @@ fn prepare_bloom_textures(
mut commands: Commands,
mut texture_cache: ResMut<TextureCache>,
render_device: Res<RenderDevice>,
views: Query<(Entity, &ExtractedCamera, &Bloom)>,
views: Query<(Entity, &ExtractedCamera, &Bloom), With<UseBloom>>,
) {
for (entity, camera, bloom) in &views {
if let Some(UVec2 {
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_core_pipeline/src/bloom/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::downsampling_pipeline::BloomUniforms;
use bevy_ecs::{prelude::Component, query::QueryItem, reflect::ReflectComponent};
use bevy_math::{AspectRatio, URect, UVec4, Vec4};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::render_component::RenderComponent;
use bevy_render::{extract_component::ExtractComponent, prelude::Camera};

/// Applies a bloom effect to an HDR-enabled 2d or 3d camera.
Expand Down Expand Up @@ -179,6 +180,9 @@ impl Default for Bloom {
}
}

#[derive(Component, RenderComponent)]
pub struct UseBloom;

/// Applies a threshold filter to the input image to extract the brightest
/// regions before blurring them and compositing back onto the original image.
/// These settings are useful when emulating the 1990s-2000s game look.
Expand Down Expand Up @@ -216,7 +220,7 @@ impl ExtractComponent for Bloom {
type QueryData = (&'static Self, &'static Camera);

type QueryFilter = ();
type Out = (Self, BloomUniforms);
type Out = (Self, BloomUniforms, UseBloom);

fn extract_component((bloom, camera): QueryItem<'_, Self::QueryData>) -> Option<Self::Out> {
match (
Expand Down Expand Up @@ -249,7 +253,7 @@ impl ExtractComponent for Bloom {
uv_offset: bloom.uv_offset,
};

Some((bloom.clone(), uniform))
Some((bloom.clone(), uniform, UseBloom))
}
_ => None,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy_app::prelude::*;
use bevy_asset::{load_internal_asset, Handle};
use bevy_ecs::{prelude::*, query::QueryItem};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::render_component::{RenderComponent, RenderComponentPlugin};
use bevy_render::{
extract_component::{ExtractComponent, ExtractComponentPlugin, UniformComponentPlugin},
prelude::Camera,
Expand Down Expand Up @@ -67,6 +68,9 @@ impl Default for ContrastAdaptiveSharpening {
}
}

#[derive(Component, RenderComponent)]
pub struct UseContrastAdaptiveSharpening;

#[derive(Component, Default, Reflect, Clone)]
#[reflect(Component, Default)]
pub struct DenoiseCas(bool);
Expand All @@ -82,7 +86,7 @@ pub struct CasUniform {
impl ExtractComponent for ContrastAdaptiveSharpening {
type QueryData = &'static Self;
type QueryFilter = With<Camera>;
type Out = (DenoiseCas, CasUniform);
type Out = (DenoiseCas, CasUniform, UseContrastAdaptiveSharpening);

fn extract_component(item: QueryItem<Self::QueryData>) -> Option<Self::Out> {
if !item.enabled || item.sharpening_strength == 0.0 {
Expand All @@ -94,6 +98,7 @@ impl ExtractComponent for ContrastAdaptiveSharpening {
// above 1.0 causes extreme artifacts and fireflies
sharpness: item.sharpening_strength.clamp(0.0, 1.0),
},
UseContrastAdaptiveSharpening,
))
}
}
Expand All @@ -117,6 +122,7 @@ impl Plugin for CasPlugin {
app.add_plugins((
ExtractComponentPlugin::<ContrastAdaptiveSharpening>::default(),
UniformComponentPlugin::<CasUniform>::default(),
RenderComponentPlugin::<UseContrastAdaptiveSharpening>::default(),
));

let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl ViewNode for MainOpaquePass2dNode {
&'static ViewTarget,
&'static ViewDepthTexture,
);
type ViewFilter = ();

fn run<'w>(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl ViewNode for MainTransparentPass2dNode {
&'static ViewTarget,
&'static ViewDepthTexture,
);
type ViewFilter = ();

fn run<'w>(
&self,
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_core_pipeline/src/core_2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ pub use camera_2d::*;
pub use main_opaque_pass_2d_node::*;
pub use main_transparent_pass_2d_node::*;

use self::graph::{Core2d, Node2d};
use crate::{tonemapping::TonemappingNode, upscaling::UpscalingNode};
use bevy_app::{App, Plugin};
use bevy_ecs::{entity::EntityHashSet, prelude::*};
use bevy_math::FloatOrd;
use bevy_render::camera::CameraActive;
use bevy_render::sync_world::MainEntity;
use bevy_render::{
camera::{Camera, ExtractedCamera},
Expand All @@ -63,8 +65,6 @@ use bevy_render::{
Extract, ExtractSchedule, Render, RenderApp, RenderSet,
};

use self::graph::{Core2d, Node2d};

pub const CORE_2D_DEPTH_FORMAT: TextureFormat = TextureFormat::Depth32Float;

pub struct Core2dPlugin;
Expand Down Expand Up @@ -403,7 +403,7 @@ pub fn prepare_core_2d_depth_textures(
render_device: Res<RenderDevice>,
transparent_2d_phases: Res<ViewSortedRenderPhases<Transparent2d>>,
opaque_2d_phases: Res<ViewBinnedRenderPhases<Opaque2d>>,
views_2d: Query<(Entity, &ExtractedCamera, &Msaa), (With<Camera2d>,)>,
views_2d: Query<(Entity, &ExtractedCamera, &Msaa), (With<Camera2d>, With<CameraActive>)>,
) {
let mut textures = HashMap::default();
for (view, camera, msaa) in &views_2d {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl ViewNode for MainOpaquePass3dNode {
Option<&'static SkyboxBindGroup>,
&'static ViewUniformOffset,
);
type ViewFilter = ();

fn run<'w>(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl ViewNode for MainTransmissivePass3dNode {
Option<&'static ViewTransmissionTexture>,
&'static ViewDepthTexture,
);
type ViewFilter = ();

fn run(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ impl ViewNode for MainTransparentPass3dNode {
&'static ViewTarget,
&'static ViewDepthTexture,
);
type ViewFilter = ();

fn run(
&self,
graph: &mut RenderGraphContext,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub use main_transparent_pass_3d_node::*;
use bevy_app::{App, Plugin, PostUpdate};
use bevy_ecs::{entity::EntityHashSet, prelude::*};
use bevy_math::FloatOrd;
use bevy_render::camera::CameraActive;
use bevy_render::sync_world::MainEntity;
use bevy_render::{
camera::{Camera, ExtractedCamera},
Expand Down Expand Up @@ -715,7 +716,7 @@ pub fn prepare_core_3d_transmission_textures(
alpha_mask_3d_phases: Res<ViewBinnedRenderPhases<AlphaMask3d>>,
transmissive_3d_phases: Res<ViewSortedRenderPhases<Transmissive3d>>,
transparent_3d_phases: Res<ViewSortedRenderPhases<Transparent3d>>,
views_3d: Query<(Entity, &ExtractedCamera, &Camera3d, &ExtractedView)>,
views_3d: Query<(Entity, &ExtractedCamera, &Camera3d, &ExtractedView), With<CameraActive>>,
) {
let mut textures = HashMap::default();
for (entity, camera, camera_3d, view) in &views_3d {
Expand Down
7 changes: 4 additions & 3 deletions crates/bevy_core_pipeline/src/deferred/copy_lighting_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use bevy_render::{
Render, RenderApp, RenderSet,
};

use super::DEFERRED_LIGHTING_PASS_ID_DEPTH_FORMAT;
use bevy_ecs::query::QueryItem;
use bevy_render::camera::CameraActive;
use bevy_render::{
render_graph::{NodeRunError, RenderGraphContext, ViewNode},
renderer::RenderContext,
};

use super::DEFERRED_LIGHTING_PASS_ID_DEPTH_FORMAT;

pub const COPY_DEFERRED_LIGHTING_ID_SHADER_HANDLE: Handle<Shader> =
Handle::weak_from_u128(5230948520734987);
pub struct CopyDeferredLightingIdPlugin;
Expand Down Expand Up @@ -65,6 +65,7 @@ impl ViewNode for CopyDeferredLightingIdNode {
&'static ViewPrepassTextures,
&'static DeferredLightingIdDepthTexture,
);
type ViewFilter = ();

fn run(
&self,
Expand Down Expand Up @@ -178,7 +179,7 @@ fn prepare_deferred_lighting_id_textures(
mut commands: Commands,
mut texture_cache: ResMut<TextureCache>,
render_device: Res<RenderDevice>,
views: Query<(Entity, &ExtractedCamera), With<DeferredPrepass>>,
views: Query<(Entity, &ExtractedCamera), (With<DeferredPrepass>, With<CameraActive>)>,
) {
for (entity, camera) in &views {
if let Some(UVec2 {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_core_pipeline/src/deferred/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl ViewNode for DeferredGBufferPrepassNode {
&'static ViewDepthTexture,
&'static ViewPrepassTextures,
);
type ViewFilter = ();

fn run<'w>(
&self,
Expand Down
50 changes: 22 additions & 28 deletions crates/bevy_core_pipeline/src/dof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
//!
//! [Depth of field]: https://en.wikipedia.org/wiki/Depth_of_field

use crate::{
core_3d::{
graph::{Core3d, Node3d},
Camera3d, DEPTH_TEXTURE_SAMPLING_SUPPORTED,
},
fullscreen_vertex_shader::fullscreen_shader_vertex_state,
};
use bevy_app::{App, Plugin};
use bevy_asset::{load_internal_asset, Handle};
use bevy_derive::{Deref, DerefMut};
Expand All @@ -28,6 +35,8 @@ use bevy_ecs::{
};
use bevy_math::ops;
use bevy_reflect::{prelude::ReflectDefault, Reflect};
use bevy_render::extract_component::{ExtractComponent, ExtractComponentPlugin};
use bevy_render::render_component::{RenderComponent, RenderComponentPlugin};
use bevy_render::{
camera::{PhysicalCameraParameters, Projection},
extract_component::{ComponentUniforms, DynamicUniformIndex, UniformComponentPlugin},
Expand All @@ -46,7 +55,6 @@ use bevy_render::{
TextureDescriptor, TextureDimension, TextureFormat, TextureSampleType, TextureUsages,
},
renderer::{RenderContext, RenderDevice},
sync_component::SyncComponentPlugin,
sync_world::RenderEntity,
texture::{BevyDefault, CachedTexture, TextureCache},
view::{
Expand All @@ -58,14 +66,6 @@ use bevy_render::{
use bevy_utils::{info_once, prelude::default, warn_once};
use smallvec::SmallVec;

use crate::{
core_3d::{
graph::{Core3d, Node3d},
Camera3d, DEPTH_TEXTURE_SAMPLING_SUPPORTED,
},
fullscreen_vertex_shader::fullscreen_shader_vertex_state,
};

const DOF_SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(2031861180739216043);

/// A plugin that adds support for the depth of field effect to Bevy.
Expand All @@ -75,7 +75,7 @@ pub struct DepthOfFieldPlugin;
/// simulating the focus of a camera lens.
///
/// [depth of field]: https://en.wikipedia.org/wiki/Depth_of_field
#[derive(Component, Clone, Copy, Reflect)]
#[derive(Component, ExtractComponent, Clone, Copy, Reflect)]
#[reflect(Component, Default)]
pub struct DepthOfField {
/// The appearance of the effect.
Expand Down Expand Up @@ -118,6 +118,9 @@ pub struct DepthOfField {
pub max_depth: f32,
}

#[derive(Component, RenderComponent)]
pub struct UseDepthOfField;

#[deprecated(since = "0.15.0", note = "Renamed to `DepthOfField`")]
pub type DepthOfFieldSettings = DepthOfField;

Expand Down Expand Up @@ -210,9 +213,12 @@ impl Plugin for DepthOfFieldPlugin {

app.register_type::<DepthOfField>();
app.register_type::<DepthOfFieldMode>();
app.add_plugins(UniformComponentPlugin::<DepthOfFieldUniform>::default());
app.add_plugins((
UniformComponentPlugin::<DepthOfFieldUniform>::default(),
RenderComponentPlugin::<UseDepthOfField>::default(),
));

app.add_plugins(SyncComponentPlugin::<DepthOfField>::default());
app.add_plugins(ExtractComponentPlugin::<DepthOfField>::default());

let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
Expand Down Expand Up @@ -339,6 +345,7 @@ impl ViewNode for DepthOfFieldNode {
Read<DynamicUniformIndex<DepthOfFieldUniform>>,
Option<Read<AuxiliaryDepthOfFieldTexture>>,
);
type ViewFilter = With<UseDepthOfField>;

fn run<'w>(
&self,
Expand Down Expand Up @@ -596,7 +603,7 @@ pub fn prepare_depth_of_field_view_bind_group_layouts(
/// need to set the appropriate flag to tell Bevy to make samplable depth
/// buffers.
pub fn configure_depth_of_field_view_targets(
mut view_targets: Query<&mut Camera3d, With<DepthOfField>>,
mut view_targets: Query<&mut Camera3d, With<UseDepthOfField>>,
) {
for mut camera_3d in view_targets.iter_mut() {
let mut depth_texture_usages = TextureUsages::from(camera_3d.depth_texture_usages);
Expand Down Expand Up @@ -823,30 +830,16 @@ fn extract_depth_of_field_settings(
}

for (entity, depth_of_field, projection) in query.iter_mut() {
let mut entity_commands = commands
.get_entity(entity)
.expect("Depth of field entity wasn't synced.");

// Depth of field is nonsensical without a perspective projection.
let Projection::Perspective(ref perspective_projection) = *projection else {
// TODO: needs better strategy for cleaning up
entity_commands.remove::<(
DepthOfField,
DepthOfFieldUniform,
// components added in prepare systems (because `DepthOfFieldNode` does not query extracted components)
DepthOfFieldPipelines,
AuxiliaryDepthOfFieldTexture,
ViewDepthOfFieldBindGroupLayouts,
)>();
continue;
};

let focal_length =
calculate_focal_length(depth_of_field.sensor_height, perspective_projection.fov);

// Convert `DepthOfField` to `DepthOfFieldUniform`.
entity_commands.insert((
*depth_of_field,
commands.entity(entity).insert((
DepthOfFieldUniform {
focal_distance: depth_of_field.focal_distance,
focal_length,
Expand All @@ -858,6 +851,7 @@ fn extract_depth_of_field_settings(
pad_b: 0,
pad_c: 0,
},
UseDepthOfField,
));
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_core_pipeline/src/fxaa/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl ViewNode for FxaaNode {
&'static CameraFxaaPipeline,
&'static Fxaa,
);
type ViewFilter = ();

fn run(
&self,
Expand Down
Loading