bevy/crates/bevy_render/src/render_graph/app.rs
IceSentry 614de3019c
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 https://github.com/bevyengine/bevy/pull/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>
2023-04-04 00:50:22 +00:00

74 lines
2.4 KiB
Rust

use bevy_app::App;
use bevy_ecs::world::FromWorld;
use super::{Node, RenderGraph};
/// Adds common [`RenderGraph`] operations to [`App`].
pub trait RenderGraphApp {
/// Add a [`Node`] to the [`RenderGraph`]:
/// * Create the [`Node`] using the [`FromWorld`] implementation
/// * Add it to the graph
fn add_render_graph_node<T: Node + FromWorld>(
&mut self,
sub_graph_name: &'static str,
node_name: &'static str,
) -> &mut Self;
/// Automatically add the required node edges based on the given ordering
fn add_render_graph_edges(
&mut self,
sub_graph_name: &'static str,
edges: &[&'static str],
) -> &mut Self;
/// Add node edge to the specified graph
fn add_render_graph_edge(
&mut self,
sub_graph_name: &'static str,
output_edge: &'static str,
input_edge: &'static str,
) -> &mut Self;
}
impl RenderGraphApp for App {
fn add_render_graph_node<T: Node + FromWorld>(
&mut self,
sub_graph_name: &'static str,
node_name: &'static str,
) -> &mut Self {
let node = T::from_world(&mut self.world);
let mut render_graph = self.world.get_resource_mut::<RenderGraph>().expect(
"RenderGraph not found. Make sure you are using add_render_graph_node on the RenderApp",
);
let graph = render_graph.sub_graph_mut(sub_graph_name);
graph.add_node(node_name, node);
self
}
fn add_render_graph_edges(
&mut self,
sub_graph_name: &'static str,
edges: &[&'static str],
) -> &mut Self {
let mut render_graph = self.world.get_resource_mut::<RenderGraph>().expect(
"RenderGraph not found. Make sure you are using add_render_graph_node on the RenderApp",
);
let graph = render_graph.sub_graph_mut(sub_graph_name);
graph.add_node_edges(edges);
self
}
fn add_render_graph_edge(
&mut self,
sub_graph_name: &'static str,
output_edge: &'static str,
input_edge: &'static str,
) -> &mut Self {
let mut render_graph = self.world.get_resource_mut::<RenderGraph>().expect(
"RenderGraph not found. Make sure you are using add_render_graph_node on the RenderApp",
);
let graph = render_graph.sub_graph_mut(sub_graph_name);
graph.add_node_edge(output_edge, input_edge);
self
}
}