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

Keep track of when a texture is first cleared #10325

Merged
merged 41 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
857b05e
Keep track of when a texture is first cleared
JMS55 Oct 31, 2023
e60f66c
Add missing import
JMS55 Oct 31, 2023
b8aad9c
Merge commit 'cbadc31d19e1d40841969ac503d49b3fff030f57' into target_i…
JMS55 Nov 10, 2023
104742e
Don't ignore depth in prepass
JMS55 Nov 10, 2023
5c0e9a6
WIP
JMS55 Nov 10, 2023
a76f74c
Add prepass support
JMS55 Nov 10, 2023
a6266e1
Add texture_attachment module
JMS55 Nov 21, 2023
e48b7b1
Switch shadow view to new API
JMS55 Nov 21, 2023
f875497
Depth target attachment
JMS55 Nov 21, 2023
52c429d
Make ColorAttachment hold CachedTexture directly
JMS55 Nov 21, 2023
66aeff0
WIP
JMS55 Nov 21, 2023
d89d118
WIP
JMS55 Nov 22, 2023
50dc55f
WIP
JMS55 Nov 22, 2023
283105a
Fixes
JMS55 Nov 22, 2023
bd36f9f
Merge commit '8067e46049f222d37ac394745805bad98979980f' into target_i…
JMS55 Dec 29, 2023
2874843
Fix rebase
JMS55 Dec 29, 2023
63e8032
Small fix
JMS55 Dec 29, 2023
4e61072
WIP MSAA fix
JMS55 Dec 29, 2023
badd572
Fixes
JMS55 Dec 29, 2023
7e7b9a7
Fix post processing
JMS55 Dec 29, 2023
dceab7a
Address PR feedback
JMS55 Dec 29, 2023
1ced9ff
Fix examples
JMS55 Dec 29, 2023
88714b2
Fix depth attachment for niche use cases
JMS55 Dec 29, 2023
6916691
Update crates/bevy_render/src/texture/texture_attachment.rs
JMS55 Dec 30, 2023
1c29415
Update crates/bevy_render/src/texture/texture_attachment.rs
JMS55 Dec 30, 2023
de5a84f
Fix webgl
JMS55 Dec 30, 2023
68e55db
Merge branch 'target_is_first_write' of https://github.com/JMS55/bevy…
JMS55 Dec 30, 2023
9753667
Fix webgl2 for deferred
JMS55 Dec 30, 2023
c7f6221
Merge commit '786abbf3f5e5be4b89c6b53d2d03162079f8e1f4' into target_i…
JMS55 Dec 30, 2023
3117ade
Fix doc links
JMS55 Dec 30, 2023
d00dd51
Clippy lint
JMS55 Dec 30, 2023
aa6169a
Fix missing import in example
JMS55 Dec 30, 2023
ec9fd69
Add clear color back to prelude
JMS55 Dec 30, 2023
a464324
Fix more examples
JMS55 Dec 30, 2023
15f8ecc
Fix more examples
JMS55 Dec 30, 2023
8c12fad
Update crates/bevy_core_pipeline/src/core_2d/camera_2d.rs
JMS55 Dec 30, 2023
bcba6f9
Appease clippy
JMS55 Dec 30, 2023
7d896c7
Merge branch 'target_is_first_write' of https://github.com/JMS55/bevy…
JMS55 Dec 30, 2023
f3f4f14
Fix broken test (the other one this time)
JMS55 Dec 31, 2023
f5c5806
More fixes
JMS55 Dec 31, 2023
c6063c5
Fix doc link
JMS55 Dec 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 4 additions & 19 deletions crates/bevy_core_pipeline/src/core_3d/main_opaque_pass_3d_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ impl ViewNode for MainOpaquePass3dNode {
&'static Camera3d,
&'static ViewTarget,
&'static ViewDepthTexture,
Option<&'static DepthPrepass>,
Option<&'static NormalPrepass>,
Option<&'static MotionVectorPrepass>,
Option<&'static DeferredPrepass>,
Option<&'static SkyboxPipelineId>,
Option<&'static SkyboxBindGroup>,
&'static ViewUniformOffset,
Expand All @@ -51,24 +47,19 @@ impl ViewNode for MainOpaquePass3dNode {
camera_3d,
target,
depth,
depth_prepass,
normal_prepass,
motion_vector_prepass,
deferred_prepass,
skybox_pipeline,
skybox_bind_group,
view_uniform_offset,
): QueryItem<Self::ViewQuery>,
world: &World,
) -> Result<(), NodeRunError> {
let load = if deferred_prepass.is_none() {
let load = if target.is_first_write() {
match camera_3d.clear_color {
ClearColorConfig::Default => LoadOp::Clear(world.resource::<ClearColor>().0.into()),
ClearColorConfig::Custom(color) => LoadOp::Clear(color.into()),
ClearColorConfig::None => LoadOp::Load,
}
} else {
// If the deferred lighting pass has run, don't clear again in this pass.
LoadOp::Load
};

Expand All @@ -89,17 +80,11 @@ impl ViewNode for MainOpaquePass3dNode {
view: &depth.view,
// NOTE: The opaque main pass loads the depth buffer and possibly overwrites it
depth_ops: Some(Operations {
load: if depth_prepass.is_some()
|| normal_prepass.is_some()
|| motion_vector_prepass.is_some()
|| deferred_prepass.is_some()
{
// if any prepass runs, it will generate a depth buffer so we should use it,
// even if only the normal_prepass is used.
Camera3dDepthLoadOp::Load
} else {
load: if depth.is_first_write() {
// NOTE: 0.0 is the far plane due to bevy's use of reverse-z projections.
camera_3d.depth_load_op.clone()
} else {
Camera3dDepthLoadOp::Load
}
.into(),
store: true,
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,10 @@ pub fn prepare_core_3d_depth_textures(
})
.clone();

commands.entity(entity).insert(ViewDepthTexture {
texture: cached_texture.texture,
view: cached_texture.default_view,
});
commands.entity(entity).insert(ViewDepthTexture::new(
cached_texture.texture,
cached_texture.default_view,
));
}
}

Expand Down
12 changes: 3 additions & 9 deletions crates/bevy_core_pipeline/src/deferred/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ impl ViewNode for DeferredGBufferPrepassNode {
&'static ViewDepthTexture,
&'static ViewPrepassTextures,
&'static Camera3d,
Option<&'static DepthPrepass>,
Option<&'static NormalPrepass>,
Option<&'static MotionVectorPrepass>,
);
Expand All @@ -52,7 +51,6 @@ impl ViewNode for DeferredGBufferPrepassNode {
view_depth_texture,
view_prepass_textures,
camera_3d,
depth_prepass,
normal_prepass,
motion_vector_prepass,
): QueryItem<Self::ViewQuery>,
Expand Down Expand Up @@ -154,15 +152,11 @@ impl ViewNode for DeferredGBufferPrepassNode {
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &view_depth_texture.view,
depth_ops: Some(Operations {
load: if depth_prepass.is_some()
|| normal_prepass.is_some()
|| motion_vector_prepass.is_some()
{
// If any prepass runs, it will generate a depth buffer so we should use it.
Camera3dDepthLoadOp::Load
} else {
load: if view_depth_texture.is_first_write() {
// NOTE: 0.0 is the far plane due to bevy's use of reverse-z projections.
camera_3d.depth_load_op.clone()
} else {
Camera3dDepthLoadOp::Load
}
.into(),
store: true,
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_core_pipeline/src/prepass/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ impl ViewNode for PrepassNode {
) -> Result<(), NodeRunError> {
let view_entity = graph.view_entity();

let _ = view_depth_texture.is_first_write();
JMS55 marked this conversation as resolved.
Show resolved Hide resolved

let mut color_attachments = vec![
view_prepass_textures
.normal
Expand Down
23 changes: 13 additions & 10 deletions crates/bevy_pbr/src/deferred/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,21 @@ impl ViewNode for DeferredOpaquePass3dPbrLightingNode {
&BindGroupEntries::single(deferred_lighting_pass_id_binding),
);

let load = if target.is_first_write() {
match camera_3d.clear_color {
ClearColorConfig::Default => LoadOp::Clear(world.resource::<ClearColor>().0.into()),
ClearColorConfig::Custom(color) => LoadOp::Clear(color.into()),
ClearColorConfig::None => LoadOp::Load,
}
} else {
LoadOp::Load
};

let mut render_pass = render_context.begin_tracked_render_pass(RenderPassDescriptor {
label: Some("deferred_lighting_pass"),
color_attachments: &[Some(target.get_color_attachment(Operations {
load: match camera_3d.clear_color {
ClearColorConfig::Default => {
LoadOp::Clear(world.resource::<ClearColor>().0.into())
}
ClearColorConfig::Custom(color) => LoadOp::Clear(color.into()),
ClearColorConfig::None => LoadOp::Load,
},
store: true,
}))],
color_attachments: &[Some(
target.get_color_attachment(Operations { load, store: true }),
)],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &deferred_lighting_id_depth_texture.texture.default_view,
depth_ops: Some(Operations {
Expand Down
21 changes: 21 additions & 0 deletions crates/bevy_render/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ pub struct ViewTarget {
main_texture: Arc<AtomicUsize>,
out_texture: TextureView,
out_texture_format: TextureFormat,
first_write: Arc<AtomicBool>,
}

pub struct PostProcessWrite<'a> {
Expand All @@ -201,6 +202,10 @@ pub struct PostProcessWrite<'a> {
impl ViewTarget {
pub const TEXTURE_FORMAT_HDR: TextureFormat = TextureFormat::Rgba16Float;

pub fn is_first_write(&self) -> bool {
self.first_write.fetch_and(false, Ordering::SeqCst)
}

/// Retrieve this target's color attachment. This will use [`Self::sampled_main_texture_view`] and resolve to [`Self::main_texture`] if
/// the target has sampling enabled. Otherwise it will use [`Self::main_texture`] directly.
pub fn get_color_attachment(&self, ops: Operations<Color>) -> RenderPassColorAttachment {
Expand Down Expand Up @@ -342,6 +347,21 @@ impl ViewTarget {
pub struct ViewDepthTexture {
pub texture: Texture,
pub view: TextureView,
first_write: Arc<AtomicBool>,
}

impl ViewDepthTexture {
pub fn new(texture: Texture, view: TextureView) -> Self {
Self {
texture,
view,
first_write: Arc::new(AtomicBool::new(true)),
}
}

pub fn is_first_write(&self) -> bool {
self.first_write.fetch_and(false, Ordering::SeqCst)
}
}

pub fn prepare_view_uniforms(
Expand Down Expand Up @@ -511,6 +531,7 @@ fn prepare_view_targets(
main_texture: main_textures.main_texture.clone(),
out_texture: out_texture_view.clone(),
out_texture_format: out_texture_format.add_srgb_suffix(),
first_write: Arc::new(AtomicBool::new(true)),
});
}
}
Expand Down