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
//! Executes the [`RenderGraph`] current render graph.

use std::{borrow::Cow, error::Error};

use log::error;

use crate::{
    context::MapContext,
    render::{eventually::Eventually::Initialized, graph_runner::RenderGraphRunner, Renderer},
    tcs::system::System,
};

/// Updates the [`RenderGraph`] with all of its nodes and then runs it to render the entire frame.
#[derive(Default)]
pub struct GraphRunnerSystem;

impl System for GraphRunnerSystem {
    fn name(&self) -> Cow<'static, str> {
        "graph_runner".into()
    }

    fn run(
        &mut self,
        MapContext {
            world,
            renderer:
                Renderer {
                    device,
                    queue,
                    resources: state,
                    render_graph,
                    ..
                },
            ..
        }: &mut MapContext,
    ) {
        render_graph.update(state);

        if let Err(e) = RenderGraphRunner::run(render_graph, device, queue, state, world) {
            error!("Error running render graph:");
            {
                let mut src: &dyn Error = &e;
                loop {
                    error!("> {src}");
                    match src.source() {
                        Some(s) => src = s,
                        None => break,
                    }
                }
            }

            // TODO: Replace panic with a graceful exit in the event loop
            // if e.should_exit() { *control_flow = ControlFlow::Exit; }
            panic!("Error running render graph: {e:?}");
        }

        {
            let _span = tracing::info_span!("present_frames").entered();

            if let Initialized(render_target) = state.render_target.take() {
                if let Some(surface_texture) = render_target.take_surface_texture() {
                    surface_texture.present();
                }

                #[cfg(feature = "tracing-tracy")]
                tracing::event!(
                    tracing::Level::INFO,
                    message = "finished frame",
                    tracy.frame_mark = true
                );
            }
        }
    }
}