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

Temporal Antialiasing and Screenspace Ambient Occlusion #7070

Closed
wants to merge 248 commits into from
Closed
Show file tree
Hide file tree
Changes from 219 commits
Commits
Show all changes
248 commits
Select commit Hold shift + click to select a range
d2af71e
robswain depth prepass
IceSentry Oct 11, 2022
0ecce68
robtfm depth prepass
IceSentry Oct 11, 2022
8458d1f
fix update to main
IceSentry Oct 11, 2022
e6bd4a2
show_depth example
IceSentry Oct 11, 2022
3327950
custom prepass shaders per material
IceSentry Oct 13, 2022
9754bae
reorder some stuff
IceSentry Oct 13, 2022
48c16e4
Prepass naming/doc cleanup
JMS55 Oct 13, 2022
1e7cb75
Remove PrepassSettings from example/3d_scene.rs
JMS55 Oct 13, 2022
06fa29e
Add PrepassPlugin within MaterialPlugin
JMS55 Oct 13, 2022
c0d528a
Misc fixes
JMS55 Oct 13, 2022
0b9f67b
Merge pull request #1 from JMS55/depth-prepass
IceSentry Oct 13, 2022
793352e
prepass wip
IceSentry Oct 27, 2022
40591ab
Merge branch 'main' into depth-prepass
IceSentry Oct 27, 2022
a3f2121
fix merge
IceSentry Oct 27, 2022
6c60290
simplify configuration
IceSentry Oct 27, 2022
5e9de81
yeet match bool
IceSentry Oct 27, 2022
4a3be2d
extract prepass node
IceSentry Oct 27, 2022
c619e94
rename phases
IceSentry Oct 27, 2022
8763721
fix targets for alpha mask only
IceSentry Oct 28, 2022
f26c4f7
rename some things
IceSentry Oct 28, 2022
a64cb56
remove unnecessary comment
IceSentry Oct 28, 2022
98da061
pr fixes
IceSentry Oct 30, 2022
8112944
Merge branch 'main' into depth-prepass
IceSentry Oct 30, 2022
d637d5b
Update crates/bevy_core_pipeline/src/prepass/node.rs
IceSentry Nov 5, 2022
745586d
Update crates/bevy_pbr/src/render/prepass.rs
IceSentry Nov 5, 2022
c584835
Apply suggestions from code review
IceSentry Nov 5, 2022
3a10291
Merge branch 'main' into depth-prepass
IceSentry Nov 5, 2022
0b43b35
use match to unwrap textures
IceSentry Nov 5, 2022
d312da4
fix plugin
IceSentry Nov 5, 2022
b3e2415
fmt
IceSentry Nov 5, 2022
9a5511e
update normal mapping
IceSentry Nov 5, 2022
94c5ac3
more warn
IceSentry Nov 5, 2022
b6efa0e
depth format
IceSentry Nov 11, 2022
3de2d75
Merge branch 'main' into depth-prepass
IceSentry Nov 11, 2022
a238ca7
Merge branch 'main' into depth-prepass
IceSentry Nov 12, 2022
1ffe9b4
Merge branch 'main' into depth-prepass
IceSentry Nov 20, 2022
47b41d6
simplify code related to texture cache
IceSentry Nov 20, 2022
1f776b3
reduce nesting a bit with let else
IceSentry Nov 20, 2022
f5bf3f8
Apply suggestions from code review
IceSentry Nov 20, 2022
1754e29
docs and minor renames
IceSentry Nov 20, 2022
a40b3bd
docs
IceSentry Nov 20, 2022
400bb5f
create new layout_entries
IceSentry Nov 20, 2022
a0d722a
docs
IceSentry Nov 20, 2022
4426530
fix docs ci
IceSentry Nov 20, 2022
24080de
update normal prepass
IceSentry Nov 24, 2022
e41ca17
Merge branch 'main' into depth-prepass
IceSentry Nov 24, 2022
7d3effd
remove unwraps
IceSentry Nov 24, 2022
8dc5bac
fix shader_defs
IceSentry Nov 24, 2022
3d04bed
Start work on GTAO prefilter_depth
JMS55 Nov 24, 2022
cd5332f
Fill out more of prefilter_depth
JMS55 Nov 24, 2022
522e2c7
More AO depth WIP
JMS55 Nov 24, 2022
7791c45
screen_to_view_space_depth WIP
JMS55 Nov 24, 2022
57720eb
Finish prefilter_depth
JMS55 Nov 24, 2022
42a829f
AO: WIP render node
JMS55 Nov 24, 2022
8fa4855
More AO boilerplate setup
JMS55 Nov 24, 2022
c5d871c
Get AO prefilter depth pass running (but shader wrong)
JMS55 Nov 24, 2022
dfa464e
Fix prefilter depth shader bugs
JMS55 Nov 24, 2022
d6fa8c0
Fix depth conversions
JMS55 Nov 24, 2022
e5c65a6
Fix screen UV calc
JMS55 Nov 24, 2022
09b7d2b
Main pass WIP
JMS55 Nov 24, 2022
4814b8c
Misc doc
JMS55 Nov 24, 2022
9e86663
WIP bindings
JMS55 Nov 24, 2022
2291feb
Generate hilbert lut
JMS55 Nov 24, 2022
7928ce9
Misc cleanup
JMS55 Nov 24, 2022
f89593b
WIP main gtao pass cpu code
JMS55 Nov 24, 2022
9b7b0b8
Remove unused
JMS55 Nov 24, 2022
b4238a5
Misc fix
JMS55 Nov 24, 2022
d4d1560
Dispatch fix
JMS55 Nov 24, 2022
4d91d69
Noise loading
JMS55 Nov 24, 2022
3f470ff
WIP
JMS55 Nov 24, 2022
0d4e8d3
Misc
JMS55 Nov 24, 2022
dae5b0c
Cleanup
JMS55 Nov 24, 2022
cc5db9a
TODO: More GTAO main pass work
JMS55 Nov 24, 2022
908a962
Improve GTAO greatly
JMS55 Nov 24, 2022
6bec444
Fix normal loading
JMS55 Nov 24, 2022
1b67e63
Fix normal calculation
JMS55 Nov 24, 2022
2b981c8
Fix noise loading
JMS55 Nov 24, 2022
8a1332c
Fix minimum cos horizon calculation
JMS55 Nov 24, 2022
f6f55e6
Switch to using shader def values
JMS55 Nov 24, 2022
f31a04d
clean up
IceSentry Nov 24, 2022
a044410
Fix shader defs
JMS55 Nov 24, 2022
239d6d2
WIP: denoise
JMS55 Nov 24, 2022
c79e01f
Fix typo
JMS55 Nov 24, 2022
760764e
Add render graph code for denoise pass
JMS55 Nov 24, 2022
a5a98f8
Add explination
JMS55 Nov 25, 2022
eb2b8f7
Initial spatial denoiser
JMS55 Nov 26, 2022
3694f68
Fix denoiser
JMS55 Nov 26, 2022
e0f8788
Fix noise slightly
JMS55 Nov 26, 2022
a5ddafe
Add velocity prepass (no jitter yet)
JMS55 Nov 27, 2022
dfd7a53
Add PreviousViewProjection
JMS55 Nov 27, 2022
9bdbcf7
Fix prepass view bind group
JMS55 Nov 27, 2022
65e4d69
Add missing position calculation
JMS55 Nov 27, 2022
3343068
Fix PreviousViewProjection uniforms
JMS55 Nov 27, 2022
275b867
Register missing resource
JMS55 Nov 27, 2022
294ed2a
Add missing system to stage
JMS55 Nov 27, 2022
7ba676c
Add TAA
JMS55 Nov 27, 2022
42e9652
Remove uneeded shader
JMS55 Nov 27, 2022
a7b6236
Fix compile error
JMS55 Nov 27, 2022
3c97318
Fix another compile error
JMS55 Nov 27, 2022
0c798ec
Fix typo
JMS55 Nov 27, 2022
2ffc41b
Misc cleanup
JMS55 Nov 28, 2022
eb34a6a
Add temporal jitter
JMS55 Nov 28, 2022
0b0ddea
Remove TAAPlugin from default plugins
JMS55 Nov 28, 2022
ece616e
Doc typo fix
JMS55 Nov 28, 2022
6dc817b
Apply AO
JMS55 Nov 28, 2022
ebd53d8
Fix typo
JMS55 Nov 28, 2022
3c47933
Fix ambient occlusion incorrectly applying to specular occlusion
JMS55 Nov 29, 2022
ee21ec8
Misc refactor
JMS55 Nov 29, 2022
b24d75a
Fix SSAO application
JMS55 Nov 29, 2022
6e2d25f
Misc refactor
JMS55 Nov 29, 2022
4424a01
Fix SSAO application again
JMS55 Nov 29, 2022
03106fa
Add GTAO multibounce approximation
JMS55 Nov 29, 2022
71ad8ba
Add missing value
JMS55 Nov 30, 2022
e62e5b5
Apply GTAO multibounce properly
JMS55 Nov 30, 2022
bad2f9e
Add SSAO example
JMS55 Nov 30, 2022
70eb3f3
Fix SSAO example TAA toggle
JMS55 Nov 30, 2022
65d5004
SSAO example add moving sphere
JMS55 Dec 1, 2022
392a43e
Improve SSAO example
JMS55 Dec 1, 2022
d13a661
Fix typo
JMS55 Dec 1, 2022
6b44888
Add missing docs
JMS55 Dec 1, 2022
73aae30
Remove multisampled SSAO texture support
JMS55 Dec 1, 2022
f28df5c
Specalize GTAO pipeline based on settings
JMS55 Dec 1, 2022
fa4b87d
SSAO Renames
JMS55 Dec 1, 2022
b577807
WIP TAA example
JMS55 Dec 1, 2022
5e45fcb
Fix uv calculation
JMS55 Dec 1, 2022
e4e3332
Fix bug
JMS55 Dec 1, 2022
f55a7c2
SSAO rename
JMS55 Dec 1, 2022
0f60b26
Fix TemporalJitter never working
JMS55 Dec 3, 2022
86d5462
Partially fix bug
JMS55 Dec 5, 2022
71b5ff6
Fix min horizon calculation
JMS55 Dec 5, 2022
7dfaad7
Note TODO when GI is implemented
JMS55 Dec 5, 2022
6dd5272
Merge branch 'main' into depth-prepass
IceSentry Dec 6, 2022
c2f1698
Apply suggestions from code review
IceSentry Dec 6, 2022
cc419af
Merge remote-tracking branch 'origin/depth-prepass' into depth-prepass
IceSentry Dec 6, 2022
7c5a5eb
docs
IceSentry Dec 6, 2022
ebb249c
GTAO fixes?
JMS55 Dec 7, 2022
07cd44b
Fix view reconstruction
JMS55 Dec 7, 2022
9fcbc6b
Reduce temporal noise
JMS55 Dec 7, 2022
dd557d7
Improve performance by using depth mip chain
JMS55 Dec 7, 2022
36f8569
Add back min_cos_horizon
JMS55 Dec 7, 2022
ef491bd
Greatly improve speed with fast acos
JMS55 Dec 7, 2022
9be7e5e
Merge branch 'main' into depth-prepass
IceSentry Dec 8, 2022
3923ede
Reduce TAA flickering
JMS55 Dec 8, 2022
cbbee68
Add taa velocity dilation
JMS55 Dec 8, 2022
83f82a6
Camera jitter cleanup
JMS55 Dec 8, 2022
dade3a1
Remove TAA blit pass
JMS55 Dec 8, 2022
6f699a4
TAA pipeline specialization
JMS55 Dec 8, 2022
526de34
Fix TAA hdr shader defs
JMS55 Dec 8, 2022
fed969e
Default to SSAO high, add custom quality
JMS55 Dec 8, 2022
bb4d157
Fix SSAO example
JMS55 Dec 8, 2022
3b57a03
Misc doc fix
JMS55 Dec 8, 2022
9928498
Double buffer TAA textures
JMS55 Dec 9, 2022
82088fb
Fix karis weight
JMS55 Dec 9, 2022
f355dfc
Fix TAA anti flicker
JMS55 Dec 10, 2022
783e020
Add TAA references
JMS55 Dec 10, 2022
4949cab
Misc fix
JMS55 Dec 10, 2022
bc14ad2
Ensure TAA goes before bloom
JMS55 Dec 10, 2022
e100d65
Remove failed anti flicker
JMS55 Dec 10, 2022
54997e3
Misc fix
JMS55 Dec 10, 2022
49af70d
Add second SSAO denoise pass
JMS55 Dec 10, 2022
71703b4
Misc fix
JMS55 Dec 10, 2022
d588b7e
Use R16 textures for SSAO
JMS55 Dec 10, 2022
5b54ab6
Misc tweaks
JMS55 Dec 10, 2022
9b191ad
Fix denoise missing right visibility
JMS55 Dec 11, 2022
2a12bf0
Unroll GTAO side loop
JMS55 Dec 11, 2022
04c114e
Misc cleanup
JMS55 Dec 11, 2022
ade10a5
Revert "Add second SSAO denoise pass"
JMS55 Dec 11, 2022
d077237
address pr comments
IceSentry Dec 12, 2022
ef4b3e9
Merge branch 'main' into depth-prepass
IceSentry Dec 12, 2022
314dea0
docs
IceSentry Dec 12, 2022
8719d47
Add depth based disocclusion test for TAA
JMS55 Dec 15, 2022
3c17e29
Doc comment
JMS55 Dec 15, 2022
8f269ca
Add another TAA reference
JMS55 Dec 15, 2022
a6b120d
Add more TAA references
JMS55 Dec 18, 2022
42f9282
Make TAA settings field public
JMS55 Dec 18, 2022
b904fb7
Misc GTAO optimization
JMS55 Dec 21, 2022
f2a84cf
Fix SSAO shadow acne
JMS55 Dec 22, 2022
266ac9e
Scale SSAO radius based on sample distance
JMS55 Dec 23, 2022
e6ac9e9
TAA velocity based dissoclusion detection
JMS55 Dec 23, 2022
a0f8af4
Misc cleanup
JMS55 Dec 23, 2022
ad3ce40
Misc cleanup
JMS55 Dec 23, 2022
c7a673b
TAA velocity fixes
JMS55 Dec 24, 2022
c4a60fb
Fix double buffer velocity prepass
JMS55 Dec 24, 2022
e6c0d5d
Disable TAA history rejection by default
JMS55 Dec 24, 2022
7cceee8
SSAO temporal noise reduction
JMS55 Dec 24, 2022
0e8a133
Rename SSAO denoise function
JMS55 Dec 24, 2022
2dcadff
Reduce TAA color clamping when velocity is small
JMS55 Dec 24, 2022
4e8890f
Misc tweaks
JMS55 Dec 24, 2022
4ebf55c
Merge branch 'main' into depth-prepass
IceSentry Dec 24, 2022
433605d
use separate prepass components
IceSentry Dec 24, 2022
722b580
fix ci
IceSentry Dec 24, 2022
98d526d
Merge commit '722b58024274f4585839e0df0effb15e2aaf1fb3' into gtao
JMS55 Dec 25, 2022
b9a5f39
Rebase fixes
JMS55 Dec 25, 2022
645bee1
Misc rebase fixes
JMS55 Dec 25, 2022
1e5fb5a
derive default and don't run when not present
IceSentry Dec 25, 2022
324eecf
Merge branch 'main' into depth-prepass
IceSentry Dec 25, 2022
4999f28
Merge commit '324eecfb4e9ce115f3febf08e99c953f1d212159' into gtao
JMS55 Dec 25, 2022
e20e15f
use depth buffer if present
IceSentry Dec 25, 2022
7b2fbbc
enable prepass by default
IceSentry Dec 25, 2022
b0039a4
Merge commit '7b2fbbc2a7b9bb91a8ce93ed65dd189a0785f76d' into gtao
JMS55 Dec 25, 2022
89e1f43
Adapt to changes
JMS55 Dec 25, 2022
005dcb8
extract prepass to module and standardize naming
IceSentry Dec 25, 2022
301d664
Finish adapting to merge
JMS55 Dec 25, 2022
e34179e
Misc fix
JMS55 Dec 25, 2022
a54b73e
register type in core_pipeline
IceSentry Dec 25, 2022
430ad2d
Misc
JMS55 Dec 25, 2022
9ce26ec
Remove TAA huerisitics
JMS55 Dec 25, 2022
0884758
Merge commit 'a54b73e0b7bbd90489c26c95c3abd9d3d8723f07' into gtao
JMS55 Dec 25, 2022
52157df
Merge fix
JMS55 Dec 25, 2022
e74265c
Rebase fixes
JMS55 Dec 25, 2022
013d2ef
Fix prepass shaders
JMS55 Dec 25, 2022
908a892
More rebase fixes
JMS55 Dec 25, 2022
b38b58f
Apply extra TAA variance only to the luma channel
JMS55 Dec 25, 2022
fab7dc3
fix FragmentInput
IceSentry Dec 26, 2022
1fc3fa1
Rename ambient occlusion to diffuse occlusion
JMS55 Dec 26, 2022
59d9907
Fix prepass fragment input
JMS55 Dec 26, 2022
7948684
Add TODO
JMS55 Dec 26, 2022
e201fb4
Misc format
JMS55 Dec 26, 2022
e083a7d
better example
IceSentry Dec 26, 2022
b5d37d9
Merge branch 'main' into depth-prepass
IceSentry Dec 26, 2022
d42b611
Apply suggestions from code review
IceSentry Dec 30, 2022
684a061
Apply suggestions from code review
IceSentry Dec 30, 2022
c3fa4e0
Apply suggestions from code review
IceSentry Dec 30, 2022
d134000
Update crates/bevy_pbr/src/prepass/prepass.wgsl
IceSentry Dec 30, 2022
1cb5ed6
Merge branch 'main' into depth-prepass
IceSentry Dec 30, 2022
51c7eea
remove unnecessary comment
IceSentry Dec 30, 2022
f61053e
use pbr_functions import
IceSentry Dec 30, 2022
1e3b173
clean up example
IceSentry Dec 30, 2022
969f3ad
add TODOs
IceSentry Dec 30, 2022
2f83d4b
add comment
IceSentry Dec 30, 2022
134b27c
clean up pbr_prepass
IceSentry Dec 30, 2022
2b0f4f9
Apply suggestions from code review
IceSentry Dec 30, 2022
c157cae
Extract constants
JMS55 Jan 1, 2023
b6acf22
Remove newline
JMS55 Jan 1, 2023
715ca65
fix unwrap on empty frag shader in prepass
IceSentry Jan 2, 2023
35093ed
docs
IceSentry Jan 2, 2023
5c9bb20
Merge commit '35093ed2e81b737fadfa5b50c32a6e49245acdf0' into gtao
JMS55 Jan 8, 2023
826b19a
Fix merge
JMS55 Jan 8, 2023
d95cc01
Merge branch 'main' into depth-prepass
IceSentry Jan 10, 2023
8e1ffbc
Merge branch 'main' into depth-prepass
IceSentry Jan 11, 2023
d64286f
update to main
IceSentry Jan 11, 2023
c9159a5
add alpha_mask_phase items is_empty check
IceSentry Jan 11, 2023
c99f582
shorten comment
IceSentry Jan 11, 2023
27db3b8
fix ci
IceSentry Jan 11, 2023
fdbb4d2
Merge branch 'main' into depth-prepass
IceSentry Jan 12, 2023
a5ec2f5
Merge branch 'main' into depth-prepass
IceSentry Jan 13, 2023
0af0d67
Merge remote-tracking branch 'icesentry/depth-prepass' into gtao
JMS55 Jan 13, 2023
a3e5c1b
Fix merge
JMS55 Jan 13, 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
29 changes: 29 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,24 @@ category = "2D Rendering"
wasm = true

# 3D Rendering
[[example]]
name = "ssao"
path = "examples/3d/ssao.rs"

[package.metadata.example.ssao]
name = "Screen Space Ambient Occlusion"
description = "A scene showcasing screen space ambient occlusion"
category = "3D Rendering"

[[example]]
name = "taa"
path = "examples/3d/taa.rs"

[package.metadata.example.taa]
name = "Temporal Antialiasing"
description = "A scene showcasing temporal antialiasing"
category = "3D Rendering"

[[example]]
name = "3d_scene"
path = "examples/3d/3d_scene.rs"
Expand Down Expand Up @@ -1202,6 +1220,17 @@ description = "A shader and a material that uses it"
category = "Shaders"
wasm = true

[[example]]
name = "shader_prepass"
path = "examples/shader/shader_prepass.rs"

[package.metadata.example.shader_prepass]
name = "Material Prepass"
description = "A shader that uses the depth texture generated in a prepass"
category = "Shaders"
wasm = true


[[example]]
name = "shader_material_screenspace_texture"
path = "examples/shader/shader_material_screenspace_texture.rs"
Expand Down
13 changes: 13 additions & 0 deletions assets/shaders/show_depth.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import bevy_pbr::mesh_types
#import bevy_pbr::mesh_view_bindings
#import bevy_pbr::utils

@fragment
fn fragment(
@builtin(position) frag_coord: vec4<f32>,
@builtin(sample_index) sample_index: u32,
#import bevy_pbr::mesh_vertex_output
) -> @location(0) vec4<f32> {
let depth = prepass_depth(frag_coord, sample_index);
return vec4(depth, depth, depth, 1.0);
}
37 changes: 28 additions & 9 deletions crates/bevy_core_pipeline/src/core_3d/main_pass_3d_node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
clear_color::{ClearColor, ClearColorConfig},
core_3d::{AlphaMask3d, Camera3d, Opaque3d, Transparent3d},
prepass::{DepthPrepass, NormalPrepass},
};
use bevy_ecs::prelude::*;
use bevy_render::{
Expand All @@ -14,6 +15,8 @@ use bevy_render::{
#[cfg(feature = "trace")]
use bevy_utils::tracing::info_span;

use super::Camera3dDepthLoadOp;

pub struct MainPass3dNode {
query: QueryState<
(
Expand All @@ -24,6 +27,8 @@ pub struct MainPass3dNode {
&'static Camera3d,
&'static ViewTarget,
&'static ViewDepthTexture,
Option<&'static DepthPrepass>,
Option<&'static NormalPrepass>,
),
With<ExtractedView>,
>,
Expand Down Expand Up @@ -55,13 +60,20 @@ impl Node for MainPass3dNode {
world: &World,
) -> Result<(), NodeRunError> {
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
let (camera, opaque_phase, alpha_mask_phase, transparent_phase, camera_3d, target, depth) =
match self.query.get_manual(world, view_entity) {
Ok(query) => query,
Err(_) => {
return Ok(());
} // No window
};
let Ok((
camera,
opaque_phase,
alpha_mask_phase,
transparent_phase,
camera_3d,
target,
depth,
depth_prepass,
normal_prepass,
)) = self.query.get_manual(world, view_entity) else {
// No window
return Ok(());
};

// Always run opaque pass to ensure screen is cleared
{
Expand All @@ -87,8 +99,15 @@ impl Node for MainPass3dNode {
view: &depth.view,
// NOTE: The opaque main pass loads the depth buffer and possibly overwrites it
depth_ops: Some(Operations {
// NOTE: 0.0 is the far plane due to bevy's use of reverse-z projections.
load: camera_3d.depth_load_op.clone().into(),
load: if depth_prepass.is_some() || normal_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 {
// NOTE: 0.0 is the near plane due to bevy's use of reverse-z projections.
camera_3d.depth_load_op.clone()
}
.into(),
store: true,
}),
stencil_ops: None,
Expand Down
89 changes: 58 additions & 31 deletions crates/bevy_core_pipeline/src/core_3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod graph {
pub const VIEW_ENTITY: &str = "view_entity";
}
pub mod node {
pub const PREPASS: &str = "prepass";
pub const MAIN_PASS: &str = "main_pass";
pub const BLOOM: &str = "bloom";
pub const TONEMAPPING: &str = "tonemapping";
Expand Down Expand Up @@ -43,7 +44,11 @@ use bevy_render::{
};
use bevy_utils::{FloatOrd, HashMap};

use crate::{tonemapping::TonemappingNode, upscaling::UpscalingNode};
use crate::{
prepass::{node::PrepassNode, DepthPrepass},
tonemapping::TonemappingNode,
upscaling::UpscalingNode,
};

pub struct Core3dPlugin;

Expand All @@ -68,20 +73,29 @@ impl Plugin for Core3dPlugin {
.add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::<AlphaMask3d>)
.add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::<Transparent3d>);

let prepass_node = PrepassNode::new(&mut render_app.world);
let pass_node_3d = MainPass3dNode::new(&mut render_app.world);
let tonemapping = TonemappingNode::new(&mut render_app.world);
let upscaling = UpscalingNode::new(&mut render_app.world);
let mut graph = render_app.world.resource_mut::<RenderGraph>();

let mut draw_3d_graph = RenderGraph::default();
draw_3d_graph.add_node(graph::node::PREPASS, prepass_node);
draw_3d_graph.add_node(graph::node::MAIN_PASS, pass_node_3d);
draw_3d_graph.add_node(graph::node::TONEMAPPING, tonemapping);
draw_3d_graph.add_node(graph::node::END_MAIN_PASS_POST_PROCESSING, EmptyNode);
draw_3d_graph.add_node(graph::node::UPSCALING, upscaling);

let input_node_id = draw_3d_graph.set_input(vec![SlotInfo::new(
graph::input::VIEW_ENTITY,
SlotType::Entity,
)]);
draw_3d_graph.add_slot_edge(
input_node_id,
graph::input::VIEW_ENTITY,
graph::node::PREPASS,
PrepassNode::IN_VIEW,
);
draw_3d_graph.add_slot_edge(
input_node_id,
graph::input::VIEW_ENTITY,
Expand All @@ -100,6 +114,7 @@ impl Plugin for Core3dPlugin {
graph::node::UPSCALING,
UpscalingNode::IN_VIEW,
);
draw_3d_graph.add_node_edge(graph::node::PREPASS, graph::node::MAIN_PASS);
draw_3d_graph.add_node_edge(graph::node::MAIN_PASS, graph::node::TONEMAPPING);
draw_3d_graph.add_node_edge(
graph::node::TONEMAPPING,
Expand Down Expand Up @@ -259,7 +274,7 @@ pub fn prepare_core_3d_depth_textures(
msaa: Res<Msaa>,
render_device: Res<RenderDevice>,
views_3d: Query<
(Entity, &ExtractedCamera),
(Entity, &ExtractedCamera, Option<&DepthPrepass>),
(
With<RenderPhase<Opaque3d>>,
With<RenderPhase<AlphaMask3d>>,
Expand All @@ -268,34 +283,46 @@ pub fn prepare_core_3d_depth_textures(
>,
) {
let mut textures = HashMap::default();
for (entity, camera) in &views_3d {
if let Some(physical_target_size) = camera.physical_target_size {
let cached_texture = textures
.entry(camera.target.clone())
.or_insert_with(|| {
texture_cache.get(
&render_device,
TextureDescriptor {
label: Some("view_depth_texture"),
size: Extent3d {
depth_or_array_layers: 1,
width: physical_target_size.x,
height: physical_target_size.y,
},
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::Depth32Float, /* PERF: vulkan docs recommend using 24
* bit depth for better performance */
usage: TextureUsages::RENDER_ATTACHMENT,
},
)
})
.clone();
commands.entity(entity).insert(ViewDepthTexture {
texture: cached_texture.texture,
view: cached_texture.default_view,
});
}
for (entity, camera, depth_prepass) in &views_3d {
let Some(physical_target_size) = camera.physical_target_size else {
continue;
};

let cached_texture = textures
.entry(camera.target.clone())
.or_insert_with(|| {
// Default usage required to write to the depth texture
let mut usage = TextureUsages::RENDER_ATTACHMENT;
if depth_prepass.is_some() {
// Required to read the output of the prepass
usage |= TextureUsages::COPY_SRC;
}

// The size of the depth texture
let size = Extent3d {
depth_or_array_layers: 1,
width: physical_target_size.x,
height: physical_target_size.y,
};

let descriptor = TextureDescriptor {
label: Some("view_depth_texture"),
size,
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
// PERF: vulkan docs recommend using 24 bit depth for better performance
format: TextureFormat::Depth32Float,
usage,
};

texture_cache.get(&render_device, descriptor)
})
.clone();

commands.entity(entity).insert(ViewDepthTexture {
texture: cached_texture.texture,
view: cached_texture.default_view,
});
}
}
4 changes: 4 additions & 0 deletions crates/bevy_core_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod core_2d;
pub mod core_3d;
pub mod fullscreen_vertex_shader;
pub mod fxaa;
pub mod prepass;
pub mod tonemapping;
pub mod upscaling;

Expand All @@ -23,6 +24,7 @@ use crate::{
core_3d::Core3dPlugin,
fullscreen_vertex_shader::FULLSCREEN_SHADER_HANDLE,
fxaa::FxaaPlugin,
prepass::{DepthPrepass, NormalPrepass},
tonemapping::TonemappingPlugin,
upscaling::UpscalingPlugin,
};
Expand All @@ -44,6 +46,8 @@ impl Plugin for CorePipelinePlugin {

app.register_type::<ClearColor>()
.register_type::<ClearColorConfig>()
.register_type::<DepthPrepass>()
.register_type::<NormalPrepass>()
.init_resource::<ClearColor>()
.add_plugin(ExtractResourcePlugin::<ClearColor>::default())
.add_plugin(Core2dPlugin)
Expand Down
Loading