use bevy_app::App;
use bevy_ecs::world::FromWorld;
use bevy_log::warn;
use super::{IntoRenderNodeArray, Node, RenderGraph, RenderLabel, RenderSubGraph};
pub trait RenderGraphApp {
fn add_render_sub_graph(&mut self, sub_graph: impl RenderSubGraph) -> &mut Self;
fn add_render_graph_node<T: Node + FromWorld>(
&mut self,
sub_graph: impl RenderSubGraph,
node_label: impl RenderLabel,
) -> &mut Self;
fn add_render_graph_edges<const N: usize>(
&mut self,
sub_graph: impl RenderSubGraph,
edges: impl IntoRenderNodeArray<N>,
) -> &mut Self;
fn add_render_graph_edge(
&mut self,
sub_graph: impl RenderSubGraph,
output_node: impl RenderLabel,
input_node: impl RenderLabel,
) -> &mut Self;
}
impl RenderGraphApp for App {
fn add_render_sub_graph(&mut self, sub_graph: impl RenderSubGraph) -> &mut Self {
let mut render_graph = self.world.get_resource_mut::<RenderGraph>().expect(
"RenderGraph not found. Make sure you are using add_render_sub_graph on the RenderApp",
);
render_graph.add_sub_graph(sub_graph, RenderGraph::default());
self
}
fn add_render_graph_node<T: Node + FromWorld>(
&mut self,
sub_graph: impl RenderSubGraph,
node_label: impl RenderLabel,
) -> &mut Self {
let sub_graph = sub_graph.intern();
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",
);
if let Some(graph) = render_graph.get_sub_graph_mut(sub_graph) {
graph.add_node(node_label, node);
} else {
warn!(
"Tried adding a render graph node to {sub_graph:?} but the sub graph doesn't exist"
);
}
self
}
fn add_render_graph_edges<const N: usize>(
&mut self,
sub_graph: impl RenderSubGraph,
edges: impl IntoRenderNodeArray<N>,
) -> &mut Self {
let sub_graph = sub_graph.intern();
let mut render_graph = self.world.get_resource_mut::<RenderGraph>().expect(
"RenderGraph not found. Make sure you are using add_render_graph_edges on the RenderApp",
);
if let Some(graph) = render_graph.get_sub_graph_mut(sub_graph) {
graph.add_node_edges(edges);
} else {
warn!(
"Tried adding render graph edges to {sub_graph:?} but the sub graph doesn't exist"
);
}
self
}
fn add_render_graph_edge(
&mut self,
sub_graph: impl RenderSubGraph,
output_node: impl RenderLabel,
input_node: impl RenderLabel,
) -> &mut Self {
let sub_graph = sub_graph.intern();
let mut render_graph = self.world.get_resource_mut::<RenderGraph>().expect(
"RenderGraph not found. Make sure you are using add_render_graph_edge on the RenderApp",
);
if let Some(graph) = render_graph.get_sub_graph_mut(sub_graph) {
graph.add_node_edge(output_node, input_node);
} else {
warn!(
"Tried adding a render graph edge to {sub_graph:?} but the sub graph doesn't exist"
);
}
self
}
}