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

[Merged by Bors] - Add depth and normal prepass #6284

Closed
wants to merge 111 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
111 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
f31a04d
clean up
IceSentry Nov 24, 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
9be7e5e
Merge branch 'main' into depth-prepass
IceSentry Dec 8, 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
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
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
e20e15f
use depth buffer if present
IceSentry Dec 25, 2022
7b2fbbc
enable prepass by default
IceSentry Dec 25, 2022
005dcb8
extract prepass to module and standardize naming
IceSentry Dec 25, 2022
a54b73e
register type in core_pipeline
IceSentry Dec 25, 2022
fab7dc3
fix FragmentInput
IceSentry 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
715ca65
fix unwrap on empty frag shader in prepass
IceSentry Jan 2, 2023
35093ed
docs
IceSentry Jan 2, 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
8e3c28b
remove needless viewport
IceSentry Jan 15, 2023
4fdb631
fix ci
IceSentry Jan 15, 2023
4113981
Merge branch 'main' into depth-prepass
IceSentry Jan 15, 2023
37dc019
fix ci again
IceSentry Jan 15, 2023
4c93b78
reintroduce viewport
IceSentry Jan 16, 2023
40a6ed5
Merge branch 'main' into depth-prepass
IceSentry Jan 16, 2023
30c0c8b
fix merge
IceSentry Jan 16, 2023
59cdf6f
Update crates/bevy_pbr/src/render/pbr_functions.wgsl
IceSentry Jan 18, 2023
66e4ab4
Update crates/bevy_pbr/src/render/pbr_functions.wgsl
IceSentry Jan 18, 2023
55839ea
fix fallback with multisamples
IceSentry Jan 18, 2023
30393b0
wording
IceSentry Jan 18, 2023
048b08c
fix
IceSentry Jan 18, 2023
b002ef6
spelling
IceSentry Jan 18, 2023
522f49b
don't create or bind prepass textures with webgl
IceSentry Jan 19, 2023
0dd8d89
Merge branch 'main' into depth-prepass
IceSentry Jan 19, 2023
ed86541
don't log warning
IceSentry Jan 19, 2023
69a1da5
Merge branch 'main' into depth-prepass
IceSentry Jan 19, 2023
8a2b381
Apply suggestions from code review
IceSentry Jan 19, 2023
678aec7
Merge branch 'main' into depth-prepass
IceSentry Jan 19, 2023
9dc998e
add backticks
IceSentry Jan 19, 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
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1220,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 = false


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

@group(1) @binding(0)
var<uniform> show_depth: f32;
@group(1) @binding(1)
var<uniform> show_normal: f32;

@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> {
if show_depth == 1.0 {
let depth = prepass_depth(frag_coord, sample_index);
return vec4(depth, depth, depth, 1.0);
} else if show_normal == 1.0 {
let normal = prepass_normal(frag_coord, sample_index);
return vec4(normal, 1.0);
} else {
// transparent
return vec4(0.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 @@ -88,8 +100,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
IceSentry marked this conversation as resolved.
Show resolved Hide resolved
} else {
// NOTE: 0.0 is the far 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 @@ -253,7 +268,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 @@ -262,34 +277,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;
IceSentry marked this conversation as resolved.
Show resolved Hide resolved
}

// 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