Skip to content

Commit 614de30

Browse files
IceSentrycart
andauthored
Add RenderGraphApp to simplify adding render nodes (#8007)
# Objective - Adding a node to the render_graph can be quite verbose and error prone because there's a lot of moving parts to it. ## Solution - Encapsulate this in a simple utility method - Mostly intended for optional nodes that have specific ordering - Requires that the `Node` impl `FromWorld`, but every internal node is built using a new function taking a `&mut World` so it was essentially already `FromWorld` - Use it for the bloom, fxaa and taa, nodes. - The main nodes don't use it because they rely more on the order of many nodes being added --- ## Changelog - Impl `FromWorld` for `BloomNode`, `FxaaNode` and `TaaNode` - Added `RenderGraph::add_node_edges()` - Added `RenderGraph::sub_graph()` - Added `RenderGraph::sub_graph_mut()` - Added `RenderGraphApp`, `RenderGraphApp::add_render_graph_node`, `RenderGraphApp::add_render_graph_edges`, `RenderGraphApp::add_render_graph_edge` ## Notes ~~This was taken out of #7995 because it works on it's own. Once the linked PR is done, the new `add_node()` will be simplified a bit since the input/output params won't be necessary.~~ This feature will be useful in most of the upcoming render nodes so it's impact will be more relevant at that point. Partially fixes #7985 ## Future work * Add a way to automatically label nodes or at least make it part of the trait. This would remove one more field from the functions added in this PR * Use it in the main pass 2d/3d --------- Co-authored-by: Carter Anderson <mcanders1@gmail.com>
1 parent 5c7abb0 commit 614de30

File tree

12 files changed

+318
-199
lines changed

12 files changed

+318
-199
lines changed

crates/bevy_core_pipeline/src/bloom/mod.rs

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use bevy_render::{
1616
ComponentUniforms, DynamicUniformIndex, ExtractComponentPlugin, UniformComponentPlugin,
1717
},
1818
prelude::Color,
19-
render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext},
19+
render_graph::{Node, NodeRunError, RenderGraphApp, RenderGraphContext},
2020
render_resource::*,
2121
renderer::{RenderContext, RenderDevice},
2222
texture::{CachedTexture, TextureCache},
@@ -71,45 +71,27 @@ impl Plugin for BloomPlugin {
7171
prepare_upsampling_pipeline.in_set(RenderSet::Prepare),
7272
queue_bloom_bind_groups.in_set(RenderSet::Queue),
7373
),
74+
)
75+
// Add bloom to the 3d render graph
76+
.add_render_graph_node::<BloomNode>(core_3d::graph::NAME, core_3d::graph::node::BLOOM)
77+
.add_render_graph_edges(
78+
core_3d::graph::NAME,
79+
&[
80+
core_3d::graph::node::END_MAIN_PASS,
81+
core_3d::graph::node::BLOOM,
82+
core_3d::graph::node::TONEMAPPING,
83+
],
84+
)
85+
// Add bloom to the 2d render graph
86+
.add_render_graph_node::<BloomNode>(core_2d::graph::NAME, core_2d::graph::node::BLOOM)
87+
.add_render_graph_edges(
88+
core_2d::graph::NAME,
89+
&[
90+
core_2d::graph::node::MAIN_PASS,
91+
core_2d::graph::node::BLOOM,
92+
core_2d::graph::node::TONEMAPPING,
93+
],
7494
);
75-
76-
// Add bloom to the 3d render graph
77-
{
78-
let bloom_node = BloomNode::new(&mut render_app.world);
79-
let mut graph = render_app.world.resource_mut::<RenderGraph>();
80-
let draw_3d_graph = graph
81-
.get_sub_graph_mut(crate::core_3d::graph::NAME)
82-
.unwrap();
83-
draw_3d_graph.add_node(core_3d::graph::node::BLOOM, bloom_node);
84-
// MAIN_PASS -> BLOOM -> TONEMAPPING
85-
draw_3d_graph.add_node_edge(
86-
crate::core_3d::graph::node::END_MAIN_PASS,
87-
core_3d::graph::node::BLOOM,
88-
);
89-
draw_3d_graph.add_node_edge(
90-
core_3d::graph::node::BLOOM,
91-
crate::core_3d::graph::node::TONEMAPPING,
92-
);
93-
}
94-
95-
// Add bloom to the 2d render graph
96-
{
97-
let bloom_node = BloomNode::new(&mut render_app.world);
98-
let mut graph = render_app.world.resource_mut::<RenderGraph>();
99-
let draw_2d_graph = graph
100-
.get_sub_graph_mut(crate::core_2d::graph::NAME)
101-
.unwrap();
102-
draw_2d_graph.add_node(core_2d::graph::node::BLOOM, bloom_node);
103-
// MAIN_PASS -> BLOOM -> TONEMAPPING
104-
draw_2d_graph.add_node_edge(
105-
crate::core_2d::graph::node::MAIN_PASS,
106-
core_2d::graph::node::BLOOM,
107-
);
108-
draw_2d_graph.add_node_edge(
109-
core_2d::graph::node::BLOOM,
110-
crate::core_2d::graph::node::TONEMAPPING,
111-
);
112-
}
11395
}
11496
}
11597

@@ -126,8 +108,8 @@ pub struct BloomNode {
126108
)>,
127109
}
128110

129-
impl BloomNode {
130-
pub fn new(world: &mut World) -> Self {
111+
impl FromWorld for BloomNode {
112+
fn from_world(world: &mut World) -> Self {
131113
Self {
132114
view_query: QueryState::new(world),
133115
}

crates/bevy_core_pipeline/src/contrast_adaptive_sharpening/mod.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bevy_reflect::{Reflect, TypeUuid};
66
use bevy_render::{
77
extract_component::{ExtractComponent, ExtractComponentPlugin, UniformComponentPlugin},
88
prelude::Camera,
9-
render_graph::RenderGraph,
9+
render_graph::RenderGraphApp,
1010
render_resource::*,
1111
renderer::RenderDevice,
1212
texture::BevyDefault,
@@ -114,47 +114,47 @@ impl Plugin for CASPlugin {
114114
render_app
115115
.init_resource::<CASPipeline>()
116116
.init_resource::<SpecializedRenderPipelines<CASPipeline>>()
117-
.add_systems(Render, prepare_cas_pipelines.in_set(RenderSet::Prepare));
118-
{
119-
let cas_node = CASNode::new(&mut render_app.world);
120-
let mut binding = render_app.world.resource_mut::<RenderGraph>();
121-
let graph = binding.get_sub_graph_mut(core_3d::graph::NAME).unwrap();
122-
123-
graph.add_node(core_3d::graph::node::CONTRAST_ADAPTIVE_SHARPENING, cas_node);
124-
125-
graph.add_node_edge(
117+
.add_systems(Render, prepare_cas_pipelines.in_set(RenderSet::Prepare))
118+
// 3d
119+
.add_render_graph_node::<CASNode>(
120+
core_3d::graph::NAME,
121+
core_3d::graph::node::CONTRAST_ADAPTIVE_SHARPENING,
122+
)
123+
.add_render_graph_edge(
124+
core_3d::graph::NAME,
126125
core_3d::graph::node::TONEMAPPING,
127126
core_3d::graph::node::CONTRAST_ADAPTIVE_SHARPENING,
128-
);
129-
graph.add_node_edge(
127+
)
128+
.add_render_graph_edge(
129+
core_3d::graph::NAME,
130130
core_3d::graph::node::FXAA,
131131
core_3d::graph::node::CONTRAST_ADAPTIVE_SHARPENING,
132-
);
133-
graph.add_node_edge(
132+
)
133+
.add_render_graph_edge(
134+
core_3d::graph::NAME,
134135
core_3d::graph::node::CONTRAST_ADAPTIVE_SHARPENING,
135136
core_3d::graph::node::END_MAIN_PASS_POST_PROCESSING,
136-
);
137-
}
138-
{
139-
let cas_node = CASNode::new(&mut render_app.world);
140-
let mut binding = render_app.world.resource_mut::<RenderGraph>();
141-
let graph = binding.get_sub_graph_mut(core_2d::graph::NAME).unwrap();
142-
143-
graph.add_node(core_2d::graph::node::CONTRAST_ADAPTIVE_SHARPENING, cas_node);
144-
145-
graph.add_node_edge(
137+
)
138+
// 2d
139+
.add_render_graph_node::<CASNode>(
140+
core_2d::graph::NAME,
141+
core_2d::graph::node::CONTRAST_ADAPTIVE_SHARPENING,
142+
)
143+
.add_render_graph_edge(
144+
core_2d::graph::NAME,
146145
core_2d::graph::node::TONEMAPPING,
147146
core_2d::graph::node::CONTRAST_ADAPTIVE_SHARPENING,
148-
);
149-
graph.add_node_edge(
147+
)
148+
.add_render_graph_edge(
149+
core_2d::graph::NAME,
150150
core_2d::graph::node::FXAA,
151151
core_2d::graph::node::CONTRAST_ADAPTIVE_SHARPENING,
152-
);
153-
graph.add_node_edge(
152+
)
153+
.add_render_graph_edge(
154+
core_2d::graph::NAME,
154155
core_2d::graph::node::CONTRAST_ADAPTIVE_SHARPENING,
155156
core_2d::graph::node::END_MAIN_PASS_POST_PROCESSING,
156157
);
157-
}
158158
}
159159
}
160160

crates/bevy_core_pipeline/src/contrast_adaptive_sharpening/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ pub struct CASNode {
2828
cached_bind_group: Mutex<Option<(BufferId, TextureViewId, BindGroup)>>,
2929
}
3030

31-
impl CASNode {
32-
pub fn new(world: &mut World) -> Self {
31+
impl FromWorld for CASNode {
32+
fn from_world(world: &mut World) -> Self {
3333
Self {
3434
query: QueryState::new(world),
3535
cached_bind_group: Mutex::new(None),

crates/bevy_core_pipeline/src/core_2d/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,14 @@ impl Plugin for Core2dPlugin {
7474
draw_2d_graph.add_node(graph::node::TONEMAPPING, tonemapping);
7575
draw_2d_graph.add_node(graph::node::END_MAIN_PASS_POST_PROCESSING, EmptyNode);
7676
draw_2d_graph.add_node(graph::node::UPSCALING, upscaling);
77-
draw_2d_graph.add_node_edge(graph::node::MAIN_PASS, graph::node::TONEMAPPING);
78-
draw_2d_graph.add_node_edge(
77+
78+
draw_2d_graph.add_node_edges(&[
79+
graph::node::MAIN_PASS,
7980
graph::node::TONEMAPPING,
8081
graph::node::END_MAIN_PASS_POST_PROCESSING,
81-
);
82-
draw_2d_graph.add_node_edge(
83-
graph::node::END_MAIN_PASS_POST_PROCESSING,
8482
graph::node::UPSCALING,
85-
);
83+
]);
84+
8685
graph.add_sub_graph(graph::NAME, draw_2d_graph);
8786
}
8887
}

crates/bevy_core_pipeline/src/core_3d/mod.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,17 @@ impl Plugin for Core3dPlugin {
106106
draw_3d_graph.add_node(graph::node::END_MAIN_PASS_POST_PROCESSING, EmptyNode);
107107
draw_3d_graph.add_node(graph::node::UPSCALING, upscaling);
108108

109-
draw_3d_graph.add_node_edge(graph::node::PREPASS, graph::node::START_MAIN_PASS);
110-
draw_3d_graph.add_node_edge(graph::node::START_MAIN_PASS, graph::node::MAIN_OPAQUE_PASS);
111-
draw_3d_graph.add_node_edge(
109+
draw_3d_graph.add_node_edges(&[
110+
graph::node::PREPASS,
111+
graph::node::START_MAIN_PASS,
112112
graph::node::MAIN_OPAQUE_PASS,
113113
graph::node::MAIN_TRANSPARENT_PASS,
114-
);
115-
draw_3d_graph.add_node_edge(
116-
graph::node::MAIN_TRANSPARENT_PASS,
117114
graph::node::END_MAIN_PASS,
118-
);
119-
draw_3d_graph.add_node_edge(graph::node::END_MAIN_PASS, graph::node::TONEMAPPING);
120-
draw_3d_graph.add_node_edge(
121115
graph::node::TONEMAPPING,
122116
graph::node::END_MAIN_PASS_POST_PROCESSING,
123-
);
124-
draw_3d_graph.add_node_edge(
125-
graph::node::END_MAIN_PASS_POST_PROCESSING,
126117
graph::node::UPSCALING,
127-
);
118+
]);
119+
128120
graph.add_sub_graph(graph::NAME, draw_3d_graph);
129121
}
130122
}

crates/bevy_core_pipeline/src/fxaa/mod.rs

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use bevy_reflect::{
99
use bevy_render::{
1010
extract_component::{ExtractComponent, ExtractComponentPlugin},
1111
prelude::Camera,
12-
render_graph::RenderGraph,
12+
render_graph::RenderGraphApp,
1313
render_resource::*,
1414
renderer::RenderDevice,
1515
texture::BevyDefault,
@@ -90,40 +90,25 @@ impl Plugin for FxaaPlugin {
9090
render_app
9191
.init_resource::<FxaaPipeline>()
9292
.init_resource::<SpecializedRenderPipelines<FxaaPipeline>>()
93-
.add_systems(Render, prepare_fxaa_pipelines.in_set(RenderSet::Prepare));
94-
95-
{
96-
let fxaa_node = FxaaNode::new(&mut render_app.world);
97-
let mut binding = render_app.world.resource_mut::<RenderGraph>();
98-
let graph = binding.get_sub_graph_mut(core_3d::graph::NAME).unwrap();
99-
100-
graph.add_node(core_3d::graph::node::FXAA, fxaa_node);
101-
102-
graph.add_node_edge(
103-
core_3d::graph::node::TONEMAPPING,
104-
core_3d::graph::node::FXAA,
105-
);
106-
graph.add_node_edge(
107-
core_3d::graph::node::FXAA,
108-
core_3d::graph::node::END_MAIN_PASS_POST_PROCESSING,
109-
);
110-
}
111-
{
112-
let fxaa_node = FxaaNode::new(&mut render_app.world);
113-
let mut binding = render_app.world.resource_mut::<RenderGraph>();
114-
let graph = binding.get_sub_graph_mut(core_2d::graph::NAME).unwrap();
115-
116-
graph.add_node(core_2d::graph::node::FXAA, fxaa_node);
117-
118-
graph.add_node_edge(
119-
core_2d::graph::node::TONEMAPPING,
120-
core_2d::graph::node::FXAA,
121-
);
122-
graph.add_node_edge(
123-
core_2d::graph::node::FXAA,
124-
core_2d::graph::node::END_MAIN_PASS_POST_PROCESSING,
93+
.add_systems(Render, prepare_fxaa_pipelines.in_set(RenderSet::Prepare))
94+
.add_render_graph_node::<FxaaNode>(core_3d::graph::NAME, core_3d::graph::node::FXAA)
95+
.add_render_graph_edges(
96+
core_3d::graph::NAME,
97+
&[
98+
core_3d::graph::node::TONEMAPPING,
99+
core_3d::graph::node::FXAA,
100+
core_3d::graph::node::END_MAIN_PASS_POST_PROCESSING,
101+
],
102+
)
103+
.add_render_graph_node::<FxaaNode>(core_2d::graph::NAME, core_2d::graph::node::FXAA)
104+
.add_render_graph_edges(
105+
core_2d::graph::NAME,
106+
&[
107+
core_2d::graph::node::TONEMAPPING,
108+
core_2d::graph::node::FXAA,
109+
core_2d::graph::node::END_MAIN_PASS_POST_PROCESSING,
110+
],
125111
);
126-
}
127112
}
128113
}
129114

crates/bevy_core_pipeline/src/fxaa/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub struct FxaaNode {
2727
cached_texture_bind_group: Mutex<Option<(TextureViewId, BindGroup)>>,
2828
}
2929

30-
impl FxaaNode {
31-
pub fn new(world: &mut World) -> Self {
30+
impl FromWorld for FxaaNode {
31+
fn from_world(world: &mut World) -> Self {
3232
Self {
3333
query: QueryState::new(world),
3434
cached_texture_bind_group: Mutex::new(None),

crates/bevy_core_pipeline/src/taa/mod.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
core_3d,
23
fullscreen_vertex_shader::fullscreen_shader_vertex_state,
34
prelude::Camera3d,
45
prepass::{DepthPrepass, MotionVectorPrepass, ViewPrepassTextures},
@@ -18,7 +19,7 @@ use bevy_reflect::{Reflect, TypeUuid};
1819
use bevy_render::{
1920
camera::{ExtractedCamera, TemporalJitter},
2021
prelude::{Camera, Projection},
21-
render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext},
22+
render_graph::{Node, NodeRunError, RenderGraphApp, RenderGraphContext},
2223
render_resource::{
2324
BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutDescriptor,
2425
BindGroupLayoutEntry, BindingResource, BindingType, CachedRenderPipelineId,
@@ -71,24 +72,17 @@ impl Plugin for TemporalAntiAliasPlugin {
7172
prepare_taa_history_textures.in_set(RenderSet::Prepare),
7273
prepare_taa_pipelines.in_set(RenderSet::Prepare),
7374
),
75+
)
76+
.add_render_graph_node::<TAANode>(core_3d::graph::NAME, draw_3d_graph::node::TAA)
77+
.add_render_graph_edges(
78+
core_3d::graph::NAME,
79+
&[
80+
core_3d::graph::node::END_MAIN_PASS,
81+
draw_3d_graph::node::TAA,
82+
core_3d::graph::node::BLOOM,
83+
core_3d::graph::node::TONEMAPPING,
84+
],
7485
);
75-
76-
let taa_node = TAANode::new(&mut render_app.world);
77-
let mut graph = render_app.world.resource_mut::<RenderGraph>();
78-
let draw_3d_graph = graph
79-
.get_sub_graph_mut(crate::core_3d::graph::NAME)
80-
.unwrap();
81-
draw_3d_graph.add_node(draw_3d_graph::node::TAA, taa_node);
82-
// MAIN_PASS -> TAA -> BLOOM -> TONEMAPPING
83-
draw_3d_graph.add_node_edge(
84-
crate::core_3d::graph::node::END_MAIN_PASS,
85-
draw_3d_graph::node::TAA,
86-
);
87-
draw_3d_graph.add_node_edge(draw_3d_graph::node::TAA, crate::core_3d::graph::node::BLOOM);
88-
draw_3d_graph.add_node_edge(
89-
draw_3d_graph::node::TAA,
90-
crate::core_3d::graph::node::TONEMAPPING,
91-
);
9286
}
9387
}
9488

@@ -168,8 +162,8 @@ struct TAANode {
168162
)>,
169163
}
170164

171-
impl TAANode {
172-
fn new(world: &mut World) -> Self {
165+
impl FromWorld for TAANode {
166+
fn from_world(world: &mut World) -> Self {
173167
Self {
174168
view_query: QueryState::new(world),
175169
}

0 commit comments

Comments
 (0)