1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
use super::{Edge, RenderGraphError, ResourceSlotInfo, ResourceSlots}; use crate::renderer::RenderContext; use bevy_ecs::{Commands, Resources, System, World}; use downcast_rs::{impl_downcast, Downcast}; use std::{borrow::Cow, fmt::Debug}; use uuid::Uuid; #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct NodeId(Uuid); impl NodeId { #[allow(clippy::new_without_default)] pub fn new() -> Self { NodeId(Uuid::new_v4()) } } pub trait Node: Downcast + Send + Sync + 'static { fn input(&self) -> &[ResourceSlotInfo] { &[] } fn output(&self) -> &[ResourceSlotInfo] { &[] } fn update( &mut self, world: &World, resources: &Resources, render_context: &mut dyn RenderContext, input: &ResourceSlots, output: &mut ResourceSlots, ); } impl_downcast!(Node); pub trait SystemNode: Node { fn get_system(&self, commands: &mut Commands) -> Box<dyn System>; } pub struct Edges { pub id: NodeId, pub input_edges: Vec<Edge>, pub output_edges: Vec<Edge>, } impl Edges { pub(crate) fn add_input_edge(&mut self, edge: Edge) -> Result<(), RenderGraphError> { if self.has_input_edge(&edge) { return Err(RenderGraphError::EdgeAlreadyExists(edge)); } self.input_edges.push(edge); Ok(()) } pub(crate) fn add_output_edge(&mut self, edge: Edge) -> Result<(), RenderGraphError> { if self.has_output_edge(&edge) { return Err(RenderGraphError::EdgeAlreadyExists(edge)); } self.output_edges.push(edge); Ok(()) } pub fn has_input_edge(&self, edge: &Edge) -> bool { self.input_edges.contains(edge) } pub fn has_output_edge(&self, edge: &Edge) -> bool { self.output_edges.contains(edge) } pub fn get_input_slot_edge(&self, index: usize) -> Result<&Edge, RenderGraphError> { self.input_edges .iter() .find(|e| { if let Edge::SlotEdge { input_index, .. } = e { *input_index == index } else { false } }) .ok_or_else(|| RenderGraphError::UnconnectedNodeInputSlot { input_slot: index, node: self.id, }) } pub fn get_output_slot_edge(&self, index: usize) -> Result<&Edge, RenderGraphError> { self.output_edges .iter() .find(|e| { if let Edge::SlotEdge { output_index, .. } = e { *output_index == index } else { false } }) .ok_or_else(|| RenderGraphError::UnconnectedNodeOutputSlot { output_slot: index, node: self.id, }) } } pub struct NodeState { pub id: NodeId, pub name: Option<Cow<'static, str>>, pub node: Box<dyn Node>, pub input_slots: ResourceSlots, pub output_slots: ResourceSlots, pub edges: Edges, } impl Debug for NodeState { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, "{:?} ({:?})", self.id, self.name) } } impl NodeState { pub fn new<T>(id: NodeId, node: T) -> Self where T: Node, { NodeState { id, name: None, input_slots: ResourceSlots::from(node.input()), output_slots: ResourceSlots::from(node.output()), node: Box::new(node), edges: Edges { id, input_edges: Vec::new(), output_edges: Vec::new(), }, } } pub fn node<T>(&self) -> Result<&T, RenderGraphError> where T: Node, { self.node .downcast_ref::<T>() .ok_or_else(|| RenderGraphError::WrongNodeType) } pub fn node_mut<T>(&mut self) -> Result<&mut T, RenderGraphError> where T: Node, { self.node .downcast_mut::<T>() .ok_or_else(|| RenderGraphError::WrongNodeType) } pub fn validate_output_slots(&self) -> Result<(), RenderGraphError> { for i in 0..self.output_slots.len() { self.edges.get_output_slot_edge(i)?; } Ok(()) } pub fn validate_input_slots(&self) -> Result<(), RenderGraphError> { for i in 0..self.input_slots.len() { self.edges.get_input_slot_edge(i)?; } Ok(()) } } #[derive(Debug, Clone, Eq, PartialEq)] pub enum NodeLabel { Id(NodeId), Name(Cow<'static, str>), } impl From<&NodeLabel> for NodeLabel { fn from(value: &NodeLabel) -> Self { value.clone() } } impl From<String> for NodeLabel { fn from(value: String) -> Self { NodeLabel::Name(value.into()) } } impl From<&'static str> for NodeLabel { fn from(value: &'static str) -> Self { NodeLabel::Name(value.into()) } } impl From<NodeId> for NodeLabel { fn from(value: NodeId) -> Self { NodeLabel::Id(value) } }