bevy_render/renderer/
graph_runner.rs

1use bevy_ecs::{prelude::Entity, world::World};
2#[cfg(feature = "trace")]
3use bevy_utils::tracing::info_span;
4use bevy_utils::HashMap;
5
6use smallvec::{smallvec, SmallVec};
7use std::{borrow::Cow, collections::VecDeque};
8use thiserror::Error;
9
10use crate::{
11    diagnostic::internal::{DiagnosticsRecorder, RenderDiagnosticsMutex},
12    render_graph::{
13        Edge, InternedRenderLabel, InternedRenderSubGraph, NodeRunError, NodeState, RenderGraph,
14        RenderGraphContext, SlotLabel, SlotType, SlotValue,
15    },
16    renderer::{RenderContext, RenderDevice},
17};
18
19/// The [`RenderGraphRunner`] is responsible for executing a [`RenderGraph`].
20///
21/// It will run all nodes in the graph sequentially in the correct order (defined by the edges).
22/// Each [`Node`](crate::render_graph::node::Node) can run any arbitrary code, but will generally
23/// either send directly a [`CommandBuffer`] or a task that will asynchronously generate a [`CommandBuffer`]
24///
25/// After running the graph, the [`RenderGraphRunner`] will execute in parallel all the tasks to get
26/// an ordered list of [`CommandBuffer`]s to execute. These [`CommandBuffer`] will be submitted to the GPU
27/// sequentially in the order that the tasks were submitted. (which is the order of the [`RenderGraph`])
28///
29/// [`CommandBuffer`]: wgpu::CommandBuffer
30pub(crate) struct RenderGraphRunner;
31
32#[derive(Error, Debug)]
33pub enum RenderGraphRunnerError {
34    #[error(transparent)]
35    NodeRunError(#[from] NodeRunError),
36    #[error("node output slot not set (index {slot_index}, name {slot_name})")]
37    EmptyNodeOutputSlot {
38        type_name: &'static str,
39        slot_index: usize,
40        slot_name: Cow<'static, str>,
41    },
42    #[error("graph '{sub_graph:?}' could not be run because slot '{slot_name}' at index {slot_index} has no value")]
43    MissingInput {
44        slot_index: usize,
45        slot_name: Cow<'static, str>,
46        sub_graph: Option<InternedRenderSubGraph>,
47    },
48    #[error("attempted to use the wrong type for input slot")]
49    MismatchedInputSlotType {
50        slot_index: usize,
51        label: SlotLabel,
52        expected: SlotType,
53        actual: SlotType,
54    },
55    #[error(
56        "node (name: '{node_name:?}') has {slot_count} input slots, but was provided {value_count} values"
57    )]
58    MismatchedInputCount {
59        node_name: InternedRenderLabel,
60        slot_count: usize,
61        value_count: usize,
62    },
63}
64
65impl RenderGraphRunner {
66    pub fn run(
67        graph: &RenderGraph,
68        render_device: RenderDevice,
69        mut diagnostics_recorder: Option<DiagnosticsRecorder>,
70        queue: &wgpu::Queue,
71        adapter: &wgpu::Adapter,
72        world: &World,
73        finalizer: impl FnOnce(&mut wgpu::CommandEncoder),
74    ) -> Result<Option<DiagnosticsRecorder>, RenderGraphRunnerError> {
75        if let Some(recorder) = &mut diagnostics_recorder {
76            recorder.begin_frame();
77        }
78
79        let mut render_context =
80            RenderContext::new(render_device, adapter.get_info(), diagnostics_recorder);
81        Self::run_graph(graph, None, &mut render_context, world, &[], None)?;
82        finalizer(render_context.command_encoder());
83
84        let (render_device, mut diagnostics_recorder) = {
85            #[cfg(feature = "trace")]
86            let _span = info_span!("submit_graph_commands").entered();
87
88            let (commands, render_device, diagnostics_recorder) = render_context.finish();
89            queue.submit(commands);
90
91            (render_device, diagnostics_recorder)
92        };
93
94        if let Some(recorder) = &mut diagnostics_recorder {
95            let render_diagnostics_mutex = world.resource::<RenderDiagnosticsMutex>().0.clone();
96            recorder.finish_frame(&render_device, move |diagnostics| {
97                *render_diagnostics_mutex.lock().expect("lock poisoned") = Some(diagnostics);
98            });
99        }
100
101        Ok(diagnostics_recorder)
102    }
103
104    /// Runs the [`RenderGraph`] and all its sub-graphs sequentially, making sure that all nodes are
105    /// run in the correct order. (a node only runs when all its dependencies have finished running)
106    fn run_graph<'w>(
107        graph: &RenderGraph,
108        sub_graph: Option<InternedRenderSubGraph>,
109        render_context: &mut RenderContext<'w>,
110        world: &'w World,
111        inputs: &[SlotValue],
112        view_entity: Option<Entity>,
113    ) -> Result<(), RenderGraphRunnerError> {
114        let mut node_outputs: HashMap<InternedRenderLabel, SmallVec<[SlotValue; 4]>> =
115            HashMap::default();
116        #[cfg(feature = "trace")]
117        let span = if let Some(label) = &sub_graph {
118            info_span!("run_graph", name = format!("{label:?}"))
119        } else {
120            info_span!("run_graph", name = "main_graph")
121        };
122        #[cfg(feature = "trace")]
123        let _guard = span.enter();
124
125        // Queue up nodes without inputs, which can be run immediately
126        let mut node_queue: VecDeque<&NodeState> = graph
127            .iter_nodes()
128            .filter(|node| node.input_slots.is_empty())
129            .collect();
130
131        // pass inputs into the graph
132        if let Some(input_node) = graph.get_input_node() {
133            let mut input_values: SmallVec<[SlotValue; 4]> = SmallVec::new();
134            for (i, input_slot) in input_node.input_slots.iter().enumerate() {
135                if let Some(input_value) = inputs.get(i) {
136                    if input_slot.slot_type != input_value.slot_type() {
137                        return Err(RenderGraphRunnerError::MismatchedInputSlotType {
138                            slot_index: i,
139                            actual: input_value.slot_type(),
140                            expected: input_slot.slot_type,
141                            label: input_slot.name.clone().into(),
142                        });
143                    }
144                    input_values.push(input_value.clone());
145                } else {
146                    return Err(RenderGraphRunnerError::MissingInput {
147                        slot_index: i,
148                        slot_name: input_slot.name.clone(),
149                        sub_graph,
150                    });
151                }
152            }
153
154            node_outputs.insert(input_node.label, input_values);
155
156            for (_, node_state) in graph
157                .iter_node_outputs(input_node.label)
158                .expect("node exists")
159            {
160                node_queue.push_front(node_state);
161            }
162        }
163
164        'handle_node: while let Some(node_state) = node_queue.pop_back() {
165            // skip nodes that are already processed
166            if node_outputs.contains_key(&node_state.label) {
167                continue;
168            }
169
170            let mut slot_indices_and_inputs: SmallVec<[(usize, SlotValue); 4]> = SmallVec::new();
171            // check if all dependencies have finished running
172            for (edge, input_node) in graph
173                .iter_node_inputs(node_state.label)
174                .expect("node is in graph")
175            {
176                match edge {
177                    Edge::SlotEdge {
178                        output_index,
179                        input_index,
180                        ..
181                    } => {
182                        if let Some(outputs) = node_outputs.get(&input_node.label) {
183                            slot_indices_and_inputs
184                                .push((*input_index, outputs[*output_index].clone()));
185                        } else {
186                            node_queue.push_front(node_state);
187                            continue 'handle_node;
188                        }
189                    }
190                    Edge::NodeEdge { .. } => {
191                        if !node_outputs.contains_key(&input_node.label) {
192                            node_queue.push_front(node_state);
193                            continue 'handle_node;
194                        }
195                    }
196                }
197            }
198
199            // construct final sorted input list
200            slot_indices_and_inputs.sort_by_key(|(index, _)| *index);
201            let inputs: SmallVec<[SlotValue; 4]> = slot_indices_and_inputs
202                .into_iter()
203                .map(|(_, value)| value)
204                .collect();
205
206            if inputs.len() != node_state.input_slots.len() {
207                return Err(RenderGraphRunnerError::MismatchedInputCount {
208                    node_name: node_state.label,
209                    slot_count: node_state.input_slots.len(),
210                    value_count: inputs.len(),
211                });
212            }
213
214            let mut outputs: SmallVec<[Option<SlotValue>; 4]> =
215                smallvec![None; node_state.output_slots.len()];
216            {
217                let mut context = RenderGraphContext::new(graph, node_state, &inputs, &mut outputs);
218                if let Some(view_entity) = view_entity {
219                    context.set_view_entity(view_entity);
220                }
221
222                {
223                    #[cfg(feature = "trace")]
224                    let _span = info_span!("node", name = node_state.type_name).entered();
225
226                    node_state.node.run(&mut context, render_context, world)?;
227                }
228
229                for run_sub_graph in context.finish() {
230                    let sub_graph = graph
231                        .get_sub_graph(run_sub_graph.sub_graph)
232                        .expect("sub graph exists because it was validated when queued.");
233                    Self::run_graph(
234                        sub_graph,
235                        Some(run_sub_graph.sub_graph),
236                        render_context,
237                        world,
238                        &run_sub_graph.inputs,
239                        run_sub_graph.view_entity,
240                    )?;
241                }
242            }
243
244            let mut values: SmallVec<[SlotValue; 4]> = SmallVec::new();
245            for (i, output) in outputs.into_iter().enumerate() {
246                if let Some(value) = output {
247                    values.push(value);
248                } else {
249                    let empty_slot = node_state.output_slots.get_slot(i).unwrap();
250                    return Err(RenderGraphRunnerError::EmptyNodeOutputSlot {
251                        type_name: node_state.type_name,
252                        slot_index: i,
253                        slot_name: empty_slot.name.clone(),
254                    });
255                }
256            }
257            node_outputs.insert(node_state.label, values);
258
259            for (_, node_state) in graph
260                .iter_node_outputs(node_state.label)
261                .expect("node exists")
262            {
263                node_queue.push_front(node_state);
264            }
265        }
266
267        Ok(())
268    }
269}