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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
pub mod settings;

use bevy_utils::intern::Interned;
pub use settings::Settings;

use self::iter_utils::sorted;
use crate::dot::{font_tag, html_escape, DotGraph};
use bevy_render::render_graph::{Edge, RenderGraph, RenderLabel};
use iter_utils::EitherOrBoth;
use pretty_type_name::pretty_type_name_str;
use std::collections::HashMap;

/// Formats the render graph into a dot graph
pub fn render_graph_dot(graph: &RenderGraph, settings: &Settings) -> String {
    let mut dot = DotGraph::digraph("RenderGraph", &[("rankdir", "LR"), ("ranksep", "1.0")])
        .graph_attributes(&[("bgcolor", &settings.style.color_background)])
        .edge_attributes(&[
            ("fontname", &settings.style.fontname),
            ("fontcolor", &settings.style.color_text),
        ])
        .node_attributes(&[
            ("shape", "plaintext"),
            ("fontname", &settings.style.fontname),
            ("fontcolor", &settings.style.color_text),
        ]);

    build_dot_graph(&mut dot, None, graph, settings, 0);
    dot.finish()
}

fn build_dot_graph(
    dot: &mut DotGraph,
    graph_name: Option<&str>,
    graph: &RenderGraph,
    settings: &Settings,
    subgraph_nest_level: usize,
) {
    let fmt_label = |label: Interned<dyn RenderLabel>| format!("{:?}", label);

    let node_mapping: HashMap<_, _> = graph
        .iter_nodes()
        .map(|node| {
            (
                node.label,
                format!("{}{:?}", graph_name.unwrap_or_default(), node.label),
            )
        })
        .collect();

    // Convert to format fitting GraphViz node id requirements
    let node_id = |id: &Interned<dyn RenderLabel>| {
        format!("{}_{}", graph_name.unwrap_or_default(), &node_mapping[id])
    };

    let mut nodes: Vec<_> = graph.iter_nodes().collect();
    nodes.sort_by(|a, b| {
        a.type_name
            .cmp(b.type_name)
            .then_with(|| fmt_label(a.label).cmp(&fmt_label(b.label)))
    });

    let layer_style = &settings.style.layers[subgraph_nest_level % settings.style.layers.len()];
    let next_layer_style =
        &settings.style.layers[(subgraph_nest_level + 1) % settings.style.layers.len()];

    for (name, subgraph) in sorted(graph.iter_sub_graphs(), |(name, _)| format!("{:?}", name)) {
        let internal_name = format!("{}_{:?}", graph_name.unwrap_or_default(), name);
        let mut sub_dot = DotGraph::subgraph(
            &internal_name,
            &[("label", &format!("{:?}", name)), ("fontcolor", "red")],
        )
        .graph_attributes(&[
            ("style", "rounded,filled"),
            ("color", &next_layer_style.color_background),
            ("fontcolor", &next_layer_style.color_label),
        ]);
        build_dot_graph(
            &mut sub_dot,
            Some(&internal_name),
            subgraph,
            settings,
            subgraph_nest_level + 1,
        );
        dot.add_sub_graph(sub_dot);
    }

    for node in &nodes {
        let name = &fmt_label(node.label);
        let type_name = pretty_type_name_str(node.type_name);

        let inputs = node
            .input_slots
            .iter()
            .enumerate()
            .map(|(index, slot)| {
                format!(
                    "<TD PORT=\"{}\">{}: {}</TD>",
                    html_escape(&format!("in-{}", index)),
                    html_escape(&slot.name),
                    html_escape(&format!("{:?}", slot.slot_type))
                )
            })
            .collect::<Vec<_>>();

        let outputs = node
            .output_slots
            .iter()
            .enumerate()
            .map(|(index, slot)| {
                format!(
                    "<TD PORT=\"{}\">{}: {}</TD>",
                    html_escape(&format!("out-{}", index)),
                    html_escape(&slot.name),
                    html_escape(&format!("{:?}", slot.slot_type))
                )
            })
            .collect::<Vec<_>>();

        let slots = iter_utils::zip_longest(inputs.iter(), outputs.iter())
            .map(|pair| match pair {
                EitherOrBoth::Both(input, output) => format!("<TR>{}{}</TR>", input, output),
                EitherOrBoth::Left(input) => {
                    format!("<TR>{}<TD BORDER=\"0\">&nbsp;</TD></TR>", input)
                }
                EitherOrBoth::Right(output) => {
                    format!("<TR><TD BORDER=\"0\">&nbsp;</TD>{}</TR>", output)
                }
            })
            .collect::<String>();

        let label = format!(
            "<<TABLE STYLE=\"rounded\"><TR><TD PORT=\"title\" BORDER=\"0\" COLSPAN=\"2\">{}<BR/>{}</TD></TR>{}</TABLE>>",
            html_escape(name),
            font_tag(&type_name, &settings.style.color_typename, 10),
            slots,
        );

        dot.add_node(
            &node_id(&node.label),
            &[
                ("label", &label),
                ("color", &settings.style.color_node),
                ("fillcolor", &settings.style.color_node),
            ],
        );
    }

    for node in &nodes {
        for edge in node.edges.input_edges() {
            match edge {
                Edge::SlotEdge {
                    input_node,
                    input_index,
                    output_node,
                    output_index,
                } => {
                    dot.add_edge_with_ports(
                        &node_id(output_node),
                        Some(&format!("out-{}:e", output_index)),
                        &node_id(input_node),
                        Some(&format!("in-{}:w", input_index)),
                        &[("color", &layer_style.color_edge_slot)],
                    );
                }
                Edge::NodeEdge {
                    input_node,
                    output_node,
                } => {
                    dot.add_edge_with_ports(
                        &node_id(output_node),
                        Some("title:e"),
                        &node_id(input_node),
                        Some("title:w"),
                        &[("color", &layer_style.color_edge)],
                    );
                }
            }
        }
    }
}
mod iter_utils {
    use std::iter::Fuse;

    pub fn sorted<'a, T: 'a, U: Ord>(
        iter: impl IntoIterator<Item = T>,
        key: impl Fn(&T) -> U,
    ) -> impl IntoIterator<Item = T> + 'a {
        let mut vec: Vec<_> = iter.into_iter().collect();
        vec.sort_by_key(key);
        vec.into_iter()
    }

    pub enum EitherOrBoth<A, B> {
        Both(A, B),
        Left(A),
        Right(B),
    }

    #[derive(Clone, Debug)]
    pub struct ZipLongest<T, U> {
        a: Fuse<T>,
        b: Fuse<U>,
    }

    pub fn zip_longest<T, U>(a: T, b: U) -> ZipLongest<T, U>
    where
        T: Iterator,
        U: Iterator,
    {
        ZipLongest {
            a: a.fuse(),
            b: b.fuse(),
        }
    }

    impl<T, U> Iterator for ZipLongest<T, U>
    where
        T: Iterator,
        U: Iterator,
    {
        type Item = EitherOrBoth<T::Item, U::Item>;

        fn next(&mut self) -> Option<Self::Item> {
            match (self.a.next(), self.b.next()) {
                (None, None) => None,
                (Some(a), None) => Some(EitherOrBoth::Left(a)),
                (None, Some(b)) => Some(EitherOrBoth::Right(b)),
                (Some(a), Some(b)) => Some(EitherOrBoth::Both(a, b)),
            }
        }

        #[inline]
        fn size_hint(&self) -> (usize, Option<usize>) {
            max_size_hint(self.a.size_hint(), self.b.size_hint())
        }
    }

    fn max_size_hint(
        a: (usize, Option<usize>),
        b: (usize, Option<usize>),
    ) -> (usize, Option<usize>) {
        let (a_lower, a_upper) = a;
        let (b_lower, b_upper) = b;

        let lower = std::cmp::max(a_lower, b_lower);

        let upper = match (a_upper, b_upper) {
            (Some(x), Some(y)) => Some(std::cmp::max(x, y)),
            _ => None,
        };

        (lower, upper)
    }
}