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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use super::{NodeId, NodeState, RenderGraph, RenderGraphError};
use bevy_utils::HashMap;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum StagerError {
    // This might have to be `:` tagged at the end.
    #[error("encountered a `RenderGraphError`")]
    RenderGraphError(#[from] RenderGraphError),
}

#[derive(Default, Debug, Eq, PartialEq)]
pub struct Stage {
    pub jobs: Vec<OrderedJob>,
}

#[derive(Default, Debug, Eq, PartialEq)]
pub struct OrderedJob {
    pub nodes: Vec<NodeId>,
}

#[derive(Default, Debug)]
pub struct StageBorrow<'a> {
    pub jobs: Vec<OrderedJobBorrow<'a>>,
}

#[derive(Default, Debug)]
pub struct OrderedJobBorrow<'a> {
    pub node_states: Vec<&'a mut NodeState>,
}

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
struct NodeIndices {
    stage: usize,
    job: usize,
    node: usize,
}

#[derive(Default, Debug)]
pub struct Stages {
    stages: Vec<Stage>,
    /// a collection of node indices that are used to efficiently borrow render graph nodes
    node_indices: HashMap<NodeId, NodeIndices>,
}

impl Stages {
    pub fn new(stages: Vec<Stage>) -> Self {
        let mut node_indices = HashMap::default();
        for (stage_index, stage) in stages.iter().enumerate() {
            for (job_index, job) in stage.jobs.iter().enumerate() {
                for (node_index, node) in job.nodes.iter().enumerate() {
                    node_indices.insert(
                        *node,
                        NodeIndices {
                            stage: stage_index,
                            job: job_index,
                            node: node_index,
                        },
                    );
                }
            }
        }
        Stages {
            stages,
            node_indices,
        }
    }

    pub fn borrow<'a>(&self, render_graph: &'a mut RenderGraph) -> Vec<StageBorrow<'a>> {
        // unfortunately borrowing render graph nodes in a specific order takes a little bit of
        // gymnastics
        let mut stage_borrows = Vec::with_capacity(self.stages.len());

        let mut node_borrows = Vec::new();
        for node in render_graph.iter_nodes_mut() {
            let indices = self.node_indices.get(&node.id).unwrap();
            node_borrows.push((node, indices));
        }

        node_borrows.sort_by_key(|(_node, indices)| <&NodeIndices>::clone(indices));
        let mut last_stage = usize::MAX;
        let mut last_job = usize::MAX;
        for (node, indices) in node_borrows.drain(..) {
            if last_stage != indices.stage {
                stage_borrows.push(StageBorrow::default());
                last_job = usize::MAX;
            }

            let stage = &mut stage_borrows[indices.stage];
            if last_job != indices.job {
                stage.jobs.push(OrderedJobBorrow::default());
            }

            let job = &mut stage.jobs[indices.job];
            job.node_states.push(node);

            last_stage = indices.stage;
            last_job = indices.job;
        }

        stage_borrows
    }
}

/// Produces a collection of `Stages`, which are sets of OrderedJobs that must be run before moving
/// on to the next stage
pub trait RenderGraphStager {
    fn get_stages(&mut self, render_graph: &RenderGraph) -> Result<Stages, RenderGraphError>;
}

// TODO: remove this
/// This scheduler ignores dependencies and puts everything in one stage. It shouldn't be used for
/// anything :)
#[derive(Debug, Default)]
pub struct LinearStager;

impl RenderGraphStager for LinearStager {
    fn get_stages(&mut self, render_graph: &RenderGraph) -> Result<Stages, RenderGraphError> {
        let mut stage = Stage::default();
        let mut job = OrderedJob::default();
        for node in render_graph.iter_nodes() {
            job.nodes.push(node.id);
        }

        stage.jobs.push(job);

        Ok(Stages::new(vec![stage]))
    }
}

#[derive(Debug, Copy, Clone)]
/// Determines the grouping strategy used when constructing graph stages
pub enum JobGrouping {
    /// Default to adding the current node to a new job in its assigned stage. This results
    /// in a "loose" pack that is easier to parallelize but has more jobs
    Loose,
    /// Default to adding the current node into the first job in its assigned stage. This results
    /// in a "tight" pack that is harder to parallelize but results in fewer jobs
    Tight,
}

#[derive(Debug)]
/// Produces Render Graph stages and jobs in a way that ensures node dependencies are respected.
pub struct DependentNodeStager {
    job_grouping: JobGrouping,
}

impl DependentNodeStager {
    pub fn loose_grouping() -> Self {
        DependentNodeStager {
            job_grouping: JobGrouping::Loose,
        }
    }

    pub fn tight_grouping() -> Self {
        DependentNodeStager {
            job_grouping: JobGrouping::Tight,
        }
    }
}

impl RenderGraphStager for DependentNodeStager {
    fn get_stages<'a>(&mut self, render_graph: &RenderGraph) -> Result<Stages, RenderGraphError> {
        // get all nodes without input. this intentionally includes nodes with no outputs
        let output_only_nodes = render_graph
            .iter_nodes()
            .filter(|node| node.input_slots.is_empty());
        let mut stages = vec![Stage::default()];
        let mut node_stages = HashMap::default();
        for output_only_node in output_only_nodes {
            // each "output only" node should start a new job on the first stage
            stage_node(
                render_graph,
                &mut stages,
                &mut node_stages,
                output_only_node,
                self.job_grouping,
            );
        }

        Ok(Stages::new(stages))
    }
}

fn stage_node(
    graph: &RenderGraph,
    stages: &mut Vec<Stage>,
    node_stages_and_jobs: &mut HashMap<NodeId, (usize, usize)>,
    node: &NodeState,
    job_grouping: JobGrouping,
) {
    // don't re-visit nodes or visit them before all of their parents have been visited
    if node_stages_and_jobs.contains_key(&node.id)
        || node
            .edges
            .input_edges
            .iter()
            .any(|e| !node_stages_and_jobs.contains_key(&e.get_output_node()))
    {
        return;
    }

    // by default assume we are creating a new job on a new stage
    let mut stage_index = 0;
    let mut job_index = match job_grouping {
        JobGrouping::Tight => Some(0),
        JobGrouping::Loose => None,
    };

    // check to see if the current node has a parent. if so, grab the parent with the highest stage
    if let Some((max_parent_stage, max_parent_job)) = node
        .edges
        .input_edges
        .iter()
        .map(|e| {
            node_stages_and_jobs
                .get(&e.get_output_node())
                .expect("Already checked that parents were visited.")
        })
        .max()
    {
        // count the number of parents that are in the highest stage
        let max_stage_parent_count = node
            .edges
            .input_edges
            .iter()
            .filter(|e| {
                let (max_stage, _) = node_stages_and_jobs
                    .get(&e.get_output_node())
                    .expect("Already checked that parents were visited.");
                max_stage == max_parent_stage
            })
            .count();

        // if the current node has more than one parent on the highest stage (aka requires
        // synchronization), then move it to the next stage and start a new job on that
        // stage
        if max_stage_parent_count > 1 {
            stage_index = max_parent_stage + 1;
        } else {
            stage_index = *max_parent_stage;
            job_index = Some(*max_parent_job);
        }
    }

    if stage_index == stages.len() {
        stages.push(Stage::default());
    }

    let stage = &mut stages[stage_index];

    let job_index = job_index.unwrap_or_else(|| stage.jobs.len());
    if job_index == stage.jobs.len() {
        stage.jobs.push(OrderedJob::default());
    }

    let job = &mut stage.jobs[job_index];
    job.nodes.push(node.id);

    node_stages_and_jobs.insert(node.id, (stage_index, job_index));

    for (_edge, node) in graph.iter_node_outputs(node.id).unwrap() {
        stage_node(graph, stages, node_stages_and_jobs, node, job_grouping);
    }
}

#[cfg(test)]
mod tests {
    use super::{DependentNodeStager, OrderedJob, RenderGraphStager, Stage};
    use crate::{
        render_graph::{Node, NodeId, RenderGraph, ResourceSlotInfo, ResourceSlots},
        renderer::{RenderContext, RenderResourceType},
    };
    use bevy_ecs::world::World;

    struct TestNode {
        inputs: Vec<ResourceSlotInfo>,
        outputs: Vec<ResourceSlotInfo>,
    }

    impl TestNode {
        pub fn new(inputs: usize, outputs: usize) -> Self {
            TestNode {
                inputs: (0..inputs)
                    .map(|i| ResourceSlotInfo {
                        name: format!("in_{}", i).into(),
                        resource_type: RenderResourceType::Texture,
                    })
                    .collect(),
                outputs: (0..outputs)
                    .map(|i| ResourceSlotInfo {
                        name: format!("out_{}", i).into(),
                        resource_type: RenderResourceType::Texture,
                    })
                    .collect(),
            }
        }
    }

    impl Node for TestNode {
        fn input(&self) -> &[ResourceSlotInfo] {
            &self.inputs
        }

        fn output(&self) -> &[ResourceSlotInfo] {
            &self.outputs
        }

        fn update(
            &mut self,
            _: &World,
            _: &mut dyn RenderContext,
            _: &ResourceSlots,
            _: &mut ResourceSlots,
        ) {
        }
    }

    #[test]
    fn test_render_graph_dependency_stager_loose() {
        let mut graph = RenderGraph::default();

        // Setup graph to look like this:
        //
        // A -> B -> C -> D
        //    /     /
        //  E      F -> G
        //
        // H -> I -> J

        let a_id = graph.add_node("A", TestNode::new(0, 1));
        let b_id = graph.add_node("B", TestNode::new(2, 1));
        let c_id = graph.add_node("C", TestNode::new(2, 1));
        let d_id = graph.add_node("D", TestNode::new(1, 0));
        let e_id = graph.add_node("E", TestNode::new(0, 1));
        let f_id = graph.add_node("F", TestNode::new(0, 2));
        let g_id = graph.add_node("G", TestNode::new(1, 0));
        let h_id = graph.add_node("H", TestNode::new(0, 1));
        let i_id = graph.add_node("I", TestNode::new(1, 1));
        let j_id = graph.add_node("J", TestNode::new(1, 0));

        graph.add_node_edge("A", "B").unwrap();
        graph.add_node_edge("B", "C").unwrap();
        graph.add_node_edge("C", "D").unwrap();
        graph.add_node_edge("E", "B").unwrap();
        graph.add_node_edge("F", "C").unwrap();
        graph.add_node_edge("F", "G").unwrap();
        graph.add_node_edge("H", "I").unwrap();
        graph.add_node_edge("I", "J").unwrap();

        let mut stager = DependentNodeStager::loose_grouping();
        let mut stages = stager.get_stages(&graph).unwrap();

        // Expected Stages:
        // (X indicates nodes that are not part of that stage)

        // Stage 1
        // A -> X -> X -> X
        //    /     /
        //  E      F -> G
        //
        // H -> I -> J

        // Stage 2
        // X -> B -> C -> D
        //    /     /
        //  X      X -> X
        //
        // X -> X -> X

        let mut expected_stages = vec![
            Stage {
                jobs: vec![
                    OrderedJob {
                        nodes: vec![f_id, g_id],
                    },
                    OrderedJob { nodes: vec![a_id] },
                    OrderedJob { nodes: vec![e_id] },
                    OrderedJob {
                        nodes: vec![h_id, i_id, j_id],
                    },
                ],
            },
            Stage {
                jobs: vec![OrderedJob {
                    nodes: vec![b_id, c_id, d_id],
                }],
            },
        ];

        // ensure job order lines up within stages (this can vary due to hash maps)
        // jobs within a stage are unordered conceptually so this is ok
        expected_stages
            .iter_mut()
            .for_each(|stage| stage.jobs.sort_by_key(|job| job.nodes[0]));

        stages
            .stages
            .iter_mut()
            .for_each(|stage| stage.jobs.sort_by_key(|job| job.nodes[0]));

        assert_eq!(
            stages.stages, expected_stages,
            "stages should be loosely grouped"
        );

        let mut borrowed = stages.borrow(&mut graph);
        // ensure job order lines up within stages (this can vary due to hash maps)
        // jobs within a stage are unordered conceptually so this is ok
        borrowed
            .iter_mut()
            .for_each(|stage| stage.jobs.sort_by_key(|job| job.node_states[0].id));

        assert_eq!(
            borrowed.len(),
            expected_stages.len(),
            "same number of stages"
        );
        for (stage_index, borrowed_stage) in borrowed.iter().enumerate() {
            assert_eq!(
                borrowed_stage.jobs.len(),
                stages.stages[stage_index].jobs.len(),
                "job length matches"
            );
            for (job_index, borrowed_job) in borrowed_stage.jobs.iter().enumerate() {
                assert_eq!(
                    borrowed_job.node_states.len(),
                    stages.stages[stage_index].jobs[job_index].nodes.len(),
                    "node length matches"
                );
                for (node_index, borrowed_node) in borrowed_job.node_states.iter().enumerate() {
                    assert_eq!(
                        borrowed_node.id,
                        stages.stages[stage_index].jobs[job_index].nodes[node_index]
                    );
                }
            }
        }
    }

    #[test]
    fn test_render_graph_dependency_stager_tight() {
        let mut graph = RenderGraph::default();

        // Setup graph to look like this:
        //
        // A -> B -> C -> D
        //    /     /
        //  E      F -> G
        //
        // H -> I -> J

        let _a_id = graph.add_node("A", TestNode::new(0, 1));
        let b_id = graph.add_node("B", TestNode::new(2, 1));
        let c_id = graph.add_node("C", TestNode::new(2, 1));
        let d_id = graph.add_node("D", TestNode::new(1, 0));
        let _e_id = graph.add_node("E", TestNode::new(0, 1));
        let f_id = graph.add_node("F", TestNode::new(0, 2));
        let g_id = graph.add_node("G", TestNode::new(1, 0));
        let h_id = graph.add_node("H", TestNode::new(0, 1));
        let i_id = graph.add_node("I", TestNode::new(1, 1));
        let j_id = graph.add_node("J", TestNode::new(1, 0));

        graph.add_node_edge("A", "B").unwrap();
        graph.add_node_edge("B", "C").unwrap();
        graph.add_node_edge("C", "D").unwrap();
        graph.add_node_edge("E", "B").unwrap();
        graph.add_node_edge("F", "C").unwrap();
        graph.add_node_edge("F", "G").unwrap();
        graph.add_node_edge("H", "I").unwrap();
        graph.add_node_edge("I", "J").unwrap();

        let mut stager = DependentNodeStager::tight_grouping();
        let mut stages = stager.get_stages(&graph).unwrap();

        // Expected Stages:
        // (X indicates nodes that are not part of that stage)

        // Stage 1
        // A -> X -> X -> X
        //    /     /
        //  E      F -> G
        //
        // H -> I -> J

        // Stage 2
        // X -> B -> C -> D
        //    /     /
        //  X      X -> X
        //
        // X -> X -> X

        assert_eq!(stages.stages[0].jobs.len(), 1, "expect exactly 1 job");

        let job = &stages.stages[0].jobs[0];

        assert_eq!(job.nodes.len(), 7, "expect exactly 7 nodes in the job");

        // its hard to test the exact order of this job's nodes because of hashing, so instead we'll
        // test the constraints that must hold true
        let index =
            |node_id: NodeId| -> usize { job.nodes.iter().position(|id| *id == node_id).unwrap() };

        assert!(index(f_id) < index(g_id));
        assert!(index(h_id) < index(i_id));
        assert!(index(i_id) < index(j_id));

        let expected_stage_1 = Stage {
            jobs: vec![OrderedJob {
                nodes: vec![b_id, c_id, d_id],
            }],
        };

        assert_eq!(stages.stages[1], expected_stage_1,);

        let mut borrowed = stages.borrow(&mut graph);
        // ensure job order lines up within stages (this can vary due to hash maps)
        // jobs within a stage are unordered conceptually so this is ok
        stages
            .stages
            .iter_mut()
            .for_each(|stage| stage.jobs.sort_by_key(|job| job.nodes[0]));
        borrowed
            .iter_mut()
            .for_each(|stage| stage.jobs.sort_by_key(|job| job.node_states[0].id));

        assert_eq!(borrowed.len(), 2, "same number of stages");
        for (stage_index, borrowed_stage) in borrowed.iter().enumerate() {
            assert_eq!(
                borrowed_stage.jobs.len(),
                stages.stages[stage_index].jobs.len(),
                "job length matches"
            );
            for (job_index, borrowed_job) in borrowed_stage.jobs.iter().enumerate() {
                assert_eq!(
                    borrowed_job.node_states.len(),
                    stages.stages[stage_index].jobs[job_index].nodes.len(),
                    "node length matches"
                );
                for (node_index, borrowed_node) in borrowed_job.node_states.iter().enumerate() {
                    assert_eq!(
                        borrowed_node.id,
                        stages.stages[stage_index].jobs[job_index].nodes[node_index]
                    );
                }
            }
        }
    }
}