Skip to content

Commit 2c21d42

Browse files
authored
Make render graph slots optional for most cases (#8109)
# Objective - Currently, the render graph slots are only used to pass the view_entity around. This introduces significant boilerplate for very little value. Instead of using slots for this, make the view_entity part of the `RenderGraphContext`. This also means we won't need to have `IN_VIEW` on every node and and we'll be able to use the default impl of `Node::input()`. ## Solution - Add `view_entity: Option<Entity>` to the `RenderGraphContext` - Update all nodes to use this instead of entity slot input --- ## Changelog - Add optional `view_entity` to `RenderGraphContext` ## Migration Guide You can now get the view_entity directly from the `RenderGraphContext`. When implementing the Node: ```rust // 0.10 struct FooNode; impl FooNode { const IN_VIEW: &'static str = "view"; } impl Node for FooNode { fn input(&self) -> Vec<SlotInfo> { vec![SlotInfo::new(Self::IN_VIEW, SlotType::Entity)] } fn run( &self, graph: &mut RenderGraphContext, // ... ) -> Result<(), NodeRunError> { let view_entity = graph.get_input_entity(Self::IN_VIEW)?; // ... Ok(()) } } // 0.11 struct FooNode; impl Node for FooNode { fn run( &self, graph: &mut RenderGraphContext, // ... ) -> Result<(), NodeRunError> { let view_entity = graph.view_entity(); // ... Ok(()) } } ``` When adding the node to the graph, you don't need to specify a slot_edge for the view_entity. ```rust // 0.10 let mut graph = RenderGraph::default(); graph.add_node(FooNode::NAME, node); let input_node_id = draw_2d_graph.set_input(vec![SlotInfo::new( graph::input::VIEW_ENTITY, SlotType::Entity, )]); graph.add_slot_edge( input_node_id, graph::input::VIEW_ENTITY, FooNode::NAME, FooNode::IN_VIEW, ); // add_node_edge ... // 0.11 let mut graph = RenderGraph::default(); graph.add_node(FooNode::NAME, node); // add_node_edge ... ``` ## Notes This PR paired with #8007 will help reduce a lot of annoying boilerplate with the render nodes. Depending on which one gets merged first. It will require a bit of clean up work to make both compatible. I tagged this as a breaking change, because using the old system to get the view_entity will break things because it's not a node input slot anymore. ## Notes for reviewers A lot of the diffs are just removing the slots in every nodes and graph creation. The important part is mostly in the graph_runner/CameraDriverNode.
1 parent 353f2e0 commit 2c21d42

File tree

20 files changed

+63
-239
lines changed

20 files changed

+63
-239
lines changed

crates/bevy_core_pipeline/src/bloom/mod.rs

Lines changed: 2 additions & 20 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, SlotInfo, SlotType},
19+
render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext},
2020
render_resource::*,
2121
renderer::{RenderContext, RenderDevice},
2222
texture::{CachedTexture, TextureCache},
@@ -83,12 +83,6 @@ impl Plugin for BloomPlugin {
8383
.get_sub_graph_mut(crate::core_3d::graph::NAME)
8484
.unwrap();
8585
draw_3d_graph.add_node(core_3d::graph::node::BLOOM, bloom_node);
86-
draw_3d_graph.add_slot_edge(
87-
draw_3d_graph.input_node().id,
88-
crate::core_3d::graph::input::VIEW_ENTITY,
89-
core_3d::graph::node::BLOOM,
90-
BloomNode::IN_VIEW,
91-
);
9286
// MAIN_PASS -> BLOOM -> TONEMAPPING
9387
draw_3d_graph.add_node_edge(
9488
crate::core_3d::graph::node::MAIN_PASS,
@@ -108,12 +102,6 @@ impl Plugin for BloomPlugin {
108102
.get_sub_graph_mut(crate::core_2d::graph::NAME)
109103
.unwrap();
110104
draw_2d_graph.add_node(core_2d::graph::node::BLOOM, bloom_node);
111-
draw_2d_graph.add_slot_edge(
112-
draw_2d_graph.input_node().id,
113-
crate::core_2d::graph::input::VIEW_ENTITY,
114-
core_2d::graph::node::BLOOM,
115-
BloomNode::IN_VIEW,
116-
);
117105
// MAIN_PASS -> BLOOM -> TONEMAPPING
118106
draw_2d_graph.add_node_edge(
119107
crate::core_2d::graph::node::MAIN_PASS,
@@ -141,8 +129,6 @@ pub struct BloomNode {
141129
}
142130

143131
impl BloomNode {
144-
pub const IN_VIEW: &'static str = "view";
145-
146132
pub fn new(world: &mut World) -> Self {
147133
Self {
148134
view_query: QueryState::new(world),
@@ -151,10 +137,6 @@ impl BloomNode {
151137
}
152138

153139
impl Node for BloomNode {
154-
fn input(&self) -> Vec<SlotInfo> {
155-
vec![SlotInfo::new(Self::IN_VIEW, SlotType::Entity)]
156-
}
157-
158140
fn update(&mut self, world: &mut World) {
159141
self.view_query.update_archetypes(world);
160142
}
@@ -174,7 +156,7 @@ impl Node for BloomNode {
174156
let downsampling_pipeline_res = world.resource::<BloomDownsamplingPipeline>();
175157
let pipeline_cache = world.resource::<PipelineCache>();
176158
let uniforms = world.resource::<ComponentUniforms<BloomUniforms>>();
177-
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
159+
let view_entity = graph.view_entity();
178160
let Ok((
179161
camera,
180162
view_target,

crates/bevy_core_pipeline/src/core_2d/main_pass_2d_node.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
use bevy_ecs::prelude::*;
66
use bevy_render::{
77
camera::ExtractedCamera,
8-
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
8+
render_graph::{Node, NodeRunError, RenderGraphContext},
99
render_phase::RenderPhase,
1010
render_resource::{LoadOp, Operations, RenderPassDescriptor},
1111
renderer::RenderContext,
@@ -27,8 +27,6 @@ pub struct MainPass2dNode {
2727
}
2828

2929
impl MainPass2dNode {
30-
pub const IN_VIEW: &'static str = "view";
31-
3230
pub fn new(world: &mut World) -> Self {
3331
Self {
3432
query: world.query_filtered(),
@@ -37,10 +35,6 @@ impl MainPass2dNode {
3735
}
3836

3937
impl Node for MainPass2dNode {
40-
fn input(&self) -> Vec<SlotInfo> {
41-
vec![SlotInfo::new(MainPass2dNode::IN_VIEW, SlotType::Entity)]
42-
}
43-
4438
fn update(&mut self, world: &mut World) {
4539
self.query.update_archetypes(world);
4640
}
@@ -51,7 +45,7 @@ impl Node for MainPass2dNode {
5145
render_context: &mut RenderContext,
5246
world: &World,
5347
) -> Result<(), NodeRunError> {
54-
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
48+
let view_entity = graph.view_entity();
5549
let (camera, transparent_phase, target, camera_2d) =
5650
if let Ok(result) = self.query.get_manual(world, view_entity) {
5751
result

crates/bevy_core_pipeline/src/core_2d/mod.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use bevy_ecs::prelude::*;
2525
use bevy_render::{
2626
camera::Camera,
2727
extract_component::ExtractComponentPlugin,
28-
render_graph::{EmptyNode, RenderGraph, SlotInfo, SlotType},
28+
render_graph::{EmptyNode, RenderGraph},
2929
render_phase::{
3030
batch_phase_system, sort_phase_system, BatchedPhaseItem, CachedRenderPipelinePhaseItem,
3131
DrawFunctionId, DrawFunctions, PhaseItem, RenderPhase,
@@ -73,28 +73,6 @@ impl Plugin for Core2dPlugin {
7373
draw_2d_graph.add_node(graph::node::TONEMAPPING, tonemapping);
7474
draw_2d_graph.add_node(graph::node::END_MAIN_PASS_POST_PROCESSING, EmptyNode);
7575
draw_2d_graph.add_node(graph::node::UPSCALING, upscaling);
76-
let input_node_id = draw_2d_graph.set_input(vec![SlotInfo::new(
77-
graph::input::VIEW_ENTITY,
78-
SlotType::Entity,
79-
)]);
80-
draw_2d_graph.add_slot_edge(
81-
input_node_id,
82-
graph::input::VIEW_ENTITY,
83-
graph::node::MAIN_PASS,
84-
MainPass2dNode::IN_VIEW,
85-
);
86-
draw_2d_graph.add_slot_edge(
87-
input_node_id,
88-
graph::input::VIEW_ENTITY,
89-
graph::node::TONEMAPPING,
90-
TonemappingNode::IN_VIEW,
91-
);
92-
draw_2d_graph.add_slot_edge(
93-
input_node_id,
94-
graph::input::VIEW_ENTITY,
95-
graph::node::UPSCALING,
96-
UpscalingNode::IN_VIEW,
97-
);
9876
draw_2d_graph.add_node_edge(graph::node::MAIN_PASS, graph::node::TONEMAPPING);
9977
draw_2d_graph.add_node_edge(
10078
graph::node::TONEMAPPING,

crates/bevy_core_pipeline/src/core_3d/main_pass_3d_node.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
use bevy_ecs::prelude::*;
77
use bevy_render::{
88
camera::ExtractedCamera,
9-
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
9+
render_graph::{Node, NodeRunError, RenderGraphContext},
1010
render_phase::RenderPhase,
1111
render_resource::{LoadOp, Operations, RenderPassDepthStencilAttachment, RenderPassDescriptor},
1212
renderer::RenderContext,
@@ -35,8 +35,6 @@ pub struct MainPass3dNode {
3535
}
3636

3737
impl MainPass3dNode {
38-
pub const IN_VIEW: &'static str = "view";
39-
4038
pub fn new(world: &mut World) -> Self {
4139
Self {
4240
query: world.query_filtered(),
@@ -45,10 +43,6 @@ impl MainPass3dNode {
4543
}
4644

4745
impl Node for MainPass3dNode {
48-
fn input(&self) -> Vec<SlotInfo> {
49-
vec![SlotInfo::new(MainPass3dNode::IN_VIEW, SlotType::Entity)]
50-
}
51-
5246
fn update(&mut self, world: &mut World) {
5347
self.query.update_archetypes(world);
5448
}
@@ -59,7 +53,7 @@ impl Node for MainPass3dNode {
5953
render_context: &mut RenderContext,
6054
world: &World,
6155
) -> Result<(), NodeRunError> {
62-
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
56+
let view_entity = graph.view_entity();
6357
let Ok((
6458
camera,
6559
opaque_phase,

crates/bevy_core_pipeline/src/core_3d/mod.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use bevy_render::{
2929
camera::{Camera, ExtractedCamera},
3030
extract_component::ExtractComponentPlugin,
3131
prelude::Msaa,
32-
render_graph::{EmptyNode, RenderGraph, SlotInfo, SlotType},
32+
render_graph::{EmptyNode, RenderGraph},
3333
render_phase::{
3434
sort_phase_system, CachedRenderPipelinePhaseItem, DrawFunctionId, DrawFunctions, PhaseItem,
3535
RenderPhase,
@@ -94,34 +94,6 @@ impl Plugin for Core3dPlugin {
9494
draw_3d_graph.add_node(graph::node::END_MAIN_PASS_POST_PROCESSING, EmptyNode);
9595
draw_3d_graph.add_node(graph::node::UPSCALING, upscaling);
9696

97-
let input_node_id = draw_3d_graph.set_input(vec![SlotInfo::new(
98-
graph::input::VIEW_ENTITY,
99-
SlotType::Entity,
100-
)]);
101-
draw_3d_graph.add_slot_edge(
102-
input_node_id,
103-
graph::input::VIEW_ENTITY,
104-
graph::node::PREPASS,
105-
PrepassNode::IN_VIEW,
106-
);
107-
draw_3d_graph.add_slot_edge(
108-
input_node_id,
109-
graph::input::VIEW_ENTITY,
110-
graph::node::MAIN_PASS,
111-
MainPass3dNode::IN_VIEW,
112-
);
113-
draw_3d_graph.add_slot_edge(
114-
input_node_id,
115-
graph::input::VIEW_ENTITY,
116-
graph::node::TONEMAPPING,
117-
TonemappingNode::IN_VIEW,
118-
);
119-
draw_3d_graph.add_slot_edge(
120-
input_node_id,
121-
graph::input::VIEW_ENTITY,
122-
graph::node::UPSCALING,
123-
UpscalingNode::IN_VIEW,
124-
);
12597
draw_3d_graph.add_node_edge(graph::node::PREPASS, graph::node::MAIN_PASS);
12698
draw_3d_graph.add_node_edge(graph::node::MAIN_PASS, graph::node::TONEMAPPING);
12799
draw_3d_graph.add_node_edge(

crates/bevy_core_pipeline/src/fxaa/mod.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,6 @@ impl Plugin for FxaaPlugin {
9999

100100
graph.add_node(core_3d::graph::node::FXAA, fxaa_node);
101101

102-
graph.add_slot_edge(
103-
graph.input_node().id,
104-
core_3d::graph::input::VIEW_ENTITY,
105-
core_3d::graph::node::FXAA,
106-
FxaaNode::IN_VIEW,
107-
);
108-
109102
graph.add_node_edge(
110103
core_3d::graph::node::TONEMAPPING,
111104
core_3d::graph::node::FXAA,
@@ -122,13 +115,6 @@ impl Plugin for FxaaPlugin {
122115

123116
graph.add_node(core_2d::graph::node::FXAA, fxaa_node);
124117

125-
graph.add_slot_edge(
126-
graph.input_node().id,
127-
core_2d::graph::input::VIEW_ENTITY,
128-
core_2d::graph::node::FXAA,
129-
FxaaNode::IN_VIEW,
130-
);
131-
132118
graph.add_node_edge(
133119
core_2d::graph::node::TONEMAPPING,
134120
core_2d::graph::node::FXAA,

crates/bevy_core_pipeline/src/fxaa/node.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::fxaa::{CameraFxaaPipeline, Fxaa, FxaaPipeline};
44
use bevy_ecs::prelude::*;
55
use bevy_ecs::query::QueryState;
66
use bevy_render::{
7-
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
7+
render_graph::{Node, NodeRunError, RenderGraphContext},
88
render_resource::{
99
BindGroup, BindGroupDescriptor, BindGroupEntry, BindingResource, FilterMode, Operations,
1010
PipelineCache, RenderPassColorAttachment, RenderPassDescriptor, SamplerDescriptor,
@@ -28,8 +28,6 @@ pub struct FxaaNode {
2828
}
2929

3030
impl FxaaNode {
31-
pub const IN_VIEW: &'static str = "view";
32-
3331
pub fn new(world: &mut World) -> Self {
3432
Self {
3533
query: QueryState::new(world),
@@ -39,10 +37,6 @@ impl FxaaNode {
3937
}
4038

4139
impl Node for FxaaNode {
42-
fn input(&self) -> Vec<SlotInfo> {
43-
vec![SlotInfo::new(FxaaNode::IN_VIEW, SlotType::Entity)]
44-
}
45-
4640
fn update(&mut self, world: &mut World) {
4741
self.query.update_archetypes(world);
4842
}
@@ -53,7 +47,7 @@ impl Node for FxaaNode {
5347
render_context: &mut RenderContext,
5448
world: &World,
5549
) -> Result<(), NodeRunError> {
56-
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
50+
let view_entity = graph.view_entity();
5751
let pipeline_cache = world.resource::<PipelineCache>();
5852
let fxaa_pipeline = world.resource::<FxaaPipeline>();
5953

crates/bevy_core_pipeline/src/msaa_writeback.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy_app::{App, Plugin};
33
use bevy_ecs::prelude::*;
44
use bevy_render::{
55
camera::ExtractedCamera,
6-
render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext, SlotInfo, SlotType},
6+
render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext},
77
renderer::RenderContext,
88
view::{Msaa, ViewTarget},
99
Render, RenderSet,
@@ -28,7 +28,6 @@ impl Plugin for MsaaWritebackPlugin {
2828
let msaa_writeback_3d = MsaaWritebackNode::new(&mut render_app.world);
2929
let mut graph = render_app.world.resource_mut::<RenderGraph>();
3030
if let Some(core_2d) = graph.get_sub_graph_mut(crate::core_2d::graph::NAME) {
31-
let input_node = core_2d.input_node().id;
3231
core_2d.add_node(
3332
crate::core_2d::graph::node::MSAA_WRITEBACK,
3433
msaa_writeback_2d,
@@ -37,16 +36,9 @@ impl Plugin for MsaaWritebackPlugin {
3736
crate::core_2d::graph::node::MSAA_WRITEBACK,
3837
crate::core_2d::graph::node::MAIN_PASS,
3938
);
40-
core_2d.add_slot_edge(
41-
input_node,
42-
crate::core_2d::graph::input::VIEW_ENTITY,
43-
crate::core_2d::graph::node::MSAA_WRITEBACK,
44-
MsaaWritebackNode::IN_VIEW,
45-
);
4639
}
4740

4841
if let Some(core_3d) = graph.get_sub_graph_mut(crate::core_3d::graph::NAME) {
49-
let input_node = core_3d.input_node().id;
5042
core_3d.add_node(
5143
crate::core_3d::graph::node::MSAA_WRITEBACK,
5244
msaa_writeback_3d,
@@ -55,12 +47,6 @@ impl Plugin for MsaaWritebackPlugin {
5547
crate::core_3d::graph::node::MSAA_WRITEBACK,
5648
crate::core_3d::graph::node::MAIN_PASS,
5749
);
58-
core_3d.add_slot_edge(
59-
input_node,
60-
crate::core_3d::graph::input::VIEW_ENTITY,
61-
crate::core_3d::graph::node::MSAA_WRITEBACK,
62-
MsaaWritebackNode::IN_VIEW,
63-
);
6450
}
6551
}
6652
}
@@ -70,8 +56,6 @@ pub struct MsaaWritebackNode {
7056
}
7157

7258
impl MsaaWritebackNode {
73-
pub const IN_VIEW: &'static str = "view";
74-
7559
pub fn new(world: &mut World) -> Self {
7660
Self {
7761
cameras: world.query(),
@@ -80,9 +64,6 @@ impl MsaaWritebackNode {
8064
}
8165

8266
impl Node for MsaaWritebackNode {
83-
fn input(&self) -> Vec<SlotInfo> {
84-
vec![SlotInfo::new(Self::IN_VIEW, SlotType::Entity)]
85-
}
8667
fn update(&mut self, world: &mut World) {
8768
self.cameras.update_archetypes(world);
8869
}
@@ -92,7 +73,7 @@ impl Node for MsaaWritebackNode {
9273
render_context: &mut RenderContext,
9374
world: &World,
9475
) -> Result<(), NodeRunError> {
95-
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
76+
let view_entity = graph.view_entity();
9677
if let Ok((target, blit_pipeline_id)) = self.cameras.get_manual(world, view_entity) {
9778
let blit_pipeline = world.resource::<BlitPipeline>();
9879
let pipeline_cache = world.resource::<PipelineCache>();

0 commit comments

Comments
 (0)