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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
//! A home of [RenderingBundle] with it's rendering plugins system and all types directly related to it.

use crate::{
    mtl::Material,
    rendy::{
        factory::Factory,
        graph::{
            render::{RenderGroupBuilder, RenderPassNodeBuilder, SubpassBuilder},
            GraphBuilder, ImageId, NodeId,
        },
        hal,
        wsi::Surface,
    },
    system::{GraphCreator, MeshProcessorSystem, RenderingSystem, TextureProcessorSystem},
    types::Backend,
    SpriteSheet,
};
use amethyst_assets::Processor;
use amethyst_core::{
    ecs::{DispatcherBuilder, World},
    SystemBundle,
};
use amethyst_error::{format_err, Error};
use std::collections::HashMap;

/// A bundle of systems used for rendering using `Rendy` render graph.
///
/// Provides a mechanism for registering rendering plugins.
/// By itself doesn't render anything, you must use `with_plugin` method
/// to define a set of functionalities you want to use.
///
/// If you need much more control, or you need to deal directly with the render pipeline,
/// it's possible to define a `RenderGraphCreator` as show by the
/// `renderable_custom` example.
#[derive(Debug)]
pub struct RenderingBundle<B: Backend> {
    plugins: Vec<Box<dyn RenderPlugin<B>>>,
}

impl<B: Backend> RenderingBundle<B> {
    /// Create empty `RenderingBundle`. You must register a plugin using
    /// [`with_plugin`] in order to actually display anything.
    pub fn new() -> Self {
        Self {
            plugins: Vec::new(),
        }
    }

    /// Register a [`RenderPlugin`].
    ///
    /// If you want the non-consuming version of this method, see [`add_plugin`].
    pub fn with_plugin(mut self, plugin: impl RenderPlugin<B> + 'static) -> Self {
        self.add_plugin(plugin);
        self
    }

    /// Register a [`RenderPlugin`].
    pub fn add_plugin(&mut self, plugin: impl RenderPlugin<B> + 'static) {
        self.plugins.push(Box::new(plugin));
    }

    fn into_graph_creator(self) -> PluggableRenderGraphCreator<B> {
        PluggableRenderGraphCreator {
            plugins: self.plugins,
        }
    }
}

impl<'a, 'b, B: Backend> SystemBundle<'a, 'b> for RenderingBundle<B> {
    fn build(
        mut self,
        world: &mut World,
        builder: &mut DispatcherBuilder<'a, 'b>,
    ) -> Result<(), Error> {
        builder.add(MeshProcessorSystem::<B>::default(), "mesh_processor", &[]);
        builder.add(
            TextureProcessorSystem::<B>::default(),
            "texture_processor",
            &[],
        );
        builder.add(Processor::<Material>::new(), "material_processor", &[]);
        builder.add(
            Processor::<SpriteSheet>::new(),
            "sprite_sheet_processor",
            &[],
        );

        // make sure that all renderer-specific systems run after game code
        builder.add_barrier();

        for plugin in &mut self.plugins {
            plugin.on_build(world, builder)?;
        }

        builder.add_thread_local(RenderingSystem::<B, _>::new(self.into_graph_creator()));
        Ok(())
    }
}

struct PluggableRenderGraphCreator<B: Backend> {
    plugins: Vec<Box<dyn RenderPlugin<B>>>,
}

impl<B: Backend> GraphCreator<B> for PluggableRenderGraphCreator<B> {
    fn rebuild(&mut self, world: &World) -> bool {
        let mut rebuild = false;
        for plugin in self.plugins.iter_mut() {
            rebuild = plugin.should_rebuild(world) || rebuild;
        }
        rebuild
    }

    fn builder(&mut self, factory: &mut Factory<B>, world: &World) -> GraphBuilder<B, World> {
        if self.plugins.is_empty() {
            log::warn!("RenderingBundle is configured to display nothing. Use `with_plugin` to add functionality.");
        }

        let mut plan = RenderPlan::new();
        for plugin in self.plugins.iter_mut() {
            plugin.on_plan(&mut plan, factory, world).unwrap();
        }
        plan.build(factory).unwrap()
    }
}

/// Basic building block of rendering in [RenderingBundle].
///
/// Can be used to register rendering-related systems to the dispatcher,
/// building render graph by registering render targets, adding [RenderableAction]s to them
/// and signalling when the graph has to be rebuild.
pub trait RenderPlugin<B: Backend>: std::fmt::Debug {
    /// Hook for adding systems and bundles to the dispatcher.
    fn on_build<'a, 'b>(
        &mut self,
        _world: &mut World,
        _builder: &mut DispatcherBuilder<'a, 'b>,
    ) -> Result<(), Error> {
        Ok(())
    }

    /// Hook for providing triggers to rebuild the render graph.
    fn should_rebuild(&mut self, _world: &World) -> bool {
        false
    }

    /// Hook for extending the rendering plan.
    fn on_plan(
        &mut self,
        plan: &mut RenderPlan<B>,
        factory: &mut Factory<B>,
        world: &World,
    ) -> Result<(), Error>;
}

/// Builder of a rendering plan for specified target.
#[derive(Debug)]
pub struct RenderPlan<B: Backend> {
    targets: HashMap<Target, TargetPlan<B>>,
    roots: Vec<Target>,
}

impl<B: Backend> RenderPlan<B> {
    fn new() -> Self {
        Self {
            targets: Default::default(),
            roots: vec![],
        }
    }

    /// Mark render target as root. Root render targets are always
    /// evaluated, even if nothing depends on them.
    pub fn add_root(&mut self, target: Target) {
        if !self.roots.contains(&target) {
            self.roots.push(target);
        }
    }

    /// Define a render target with predefined set of outputs.
    pub fn define_pass(
        &mut self,
        target: Target,
        outputs: TargetPlanOutputs<B>,
    ) -> Result<(), Error> {
        let target_plan = self
            .targets
            .entry(target)
            .or_insert_with(|| TargetPlan::new(target));

        target_plan.set_outputs(outputs)?;

        Ok(())
    }

    /// Extend the rendering plan of a render target. Target can be defined in other plugins.
    /// The closure is evaluated only if the target contributes to the rendering result, e.g.
    /// is rendered to a window or is a dependency of other evaluated target.
    pub fn extend_target(
        &mut self,
        target: Target,
        closure: impl FnOnce(&mut TargetPlanContext<'_, B>) -> Result<(), Error> + 'static,
    ) {
        let target_plan = self
            .targets
            .entry(target)
            .or_insert_with(|| TargetPlan::new(target));
        target_plan.add_extension(Box::new(closure));
    }

    fn build(self, factory: &Factory<B>) -> Result<GraphBuilder<B, World>, Error> {
        let mut ctx = PlanContext {
            target_metadata: self
                .targets
                .iter()
                .filter_map(|(k, t)| unsafe { t.metadata(factory.physical()) }.map(|m| (*k, m)))
                .collect(),
            targets: self.targets,
            passes: Default::default(),
            outputs: Default::default(),
            graph_builder: GraphBuilder::new(),
        };

        for target in self.roots {
            ctx.evaluate_target(target)?;
        }

        Ok(ctx.graph_builder)
    }
}

#[derive(Debug)]
enum EvaluationState {
    Evaluating,
    Built(NodeId),
}

impl EvaluationState {
    fn node(&self) -> Option<NodeId> {
        match self {
            EvaluationState::Built(node) => Some(*node),
            _ => None,
        }
    }

    fn is_built(&self) -> bool {
        self.node().is_some()
    }
}

/// Metadata for a planned render target.
/// Defines effective size and layer count that target's renderpass will operate on.
#[derive(Debug, Clone, Copy)]
pub struct TargetMetadata {
    width: u32,
    height: u32,
    layers: u16,
}

#[derive(Debug)]
struct PlanContext<B: Backend> {
    targets: HashMap<Target, TargetPlan<B>>,
    target_metadata: HashMap<Target, TargetMetadata>,
    passes: HashMap<Target, EvaluationState>,
    outputs: HashMap<TargetImage, ImageId>,
    graph_builder: GraphBuilder<B, World>,
}

impl<B: Backend> PlanContext<B> {
    pub fn mark_evaluating(&mut self, target: Target) -> Result<(), Error> {
        match self.passes.get(&target) {
            None => {},
            Some(EvaluationState::Evaluating) => return Err(format_err!("Trying to evaluate {:?} render plan that is already evaluating. Circular dependency detected.", target)),
            // this case is not a soft runtime error, as this should never be allowed by the API.
            Some(EvaluationState::Built(_)) => panic!("Trying to reevaluate a render plan for {:?}.", target),
        };
        self.passes.insert(target, EvaluationState::Evaluating);
        Ok(())
    }

    fn evaluate_target(&mut self, target: Target) -> Result<(), Error> {
        // prevent evaluation of roots that were accessed recursively or undefined
        if let Some(pass) = self.targets.remove(&target) {
            pass.evaluate(self)?;
        }
        Ok(())
    }

    fn submit_pass(
        &mut self,
        target: Target,
        pass: RenderPassNodeBuilder<B, World>,
    ) -> Result<(), Error> {
        match self.passes.get(&target) {
            None => {}
            Some(EvaluationState::Evaluating) => {}
            // this case is not a soft runtime error, as this should never be allowed by the API.
            Some(EvaluationState::Built(_)) => panic!(
                "Trying to resubmit a render pass for {:?}. This is a RenderingBundle bug.",
                target
            ),
        };
        let node = self.graph_builder.add_node(pass);
        self.passes.insert(target, EvaluationState::Built(node));
        Ok(())
    }

    fn get_pass_node_raw(&self, target: Target) -> Option<NodeId> {
        self.passes.get(&target).and_then(|p| p.node())
    }

    pub fn get_node(&mut self, target: Target) -> Result<NodeId, Error> {
        match self.get_pass_node_raw(target) {
            Some(node) => Ok(node),
            None => {
                self.evaluate_target(target)?;
                Ok(self
                    .passes
                    .get(&target)
                    .and_then(|p| p.node())
                    .expect("Just built"))
            }
        }
    }

    pub fn target_metadata(&self, target: Target) -> Option<TargetMetadata> {
        self.target_metadata.get(&target).copied()
    }

    fn get_image(&mut self, image_ref: TargetImage) -> Result<ImageId, Error> {
        self.try_get_image(image_ref)?.ok_or_else(|| {
            format_err!(
                "Output image {:?} is not registered by the target.",
                image_ref
            )
        })
    }

    fn try_get_image(&mut self, image_ref: TargetImage) -> Result<Option<ImageId>, Error> {
        if !self
            .passes
            .get(&image_ref.target())
            .map_or(false, |t| t.is_built())
        {
            self.evaluate_target(image_ref.target())?;
        }
        Ok(self.outputs.get(&image_ref).cloned())
    }

    fn register_output(&mut self, output: TargetImage, image: ImageId) -> Result<(), Error> {
        if self.outputs.contains_key(&output) {
            return Err(format_err!(
                "Trying to register already registered output image {:?}",
                output
            ));
        }
        self.outputs.insert(output, image);
        Ok(())
    }

    pub fn graph(&mut self) -> &mut GraphBuilder<B, World> {
        &mut self.graph_builder
    }

    pub fn create_image(&mut self, options: ImageOptions) -> ImageId {
        self.graph_builder
            .create_image(options.kind, options.levels, options.format, options.clear)
    }
}

/// A planning context focused on specific render target.
#[derive(Debug)]
pub struct TargetPlanContext<'a, B: Backend> {
    plan_context: &'a mut PlanContext<B>,
    key: Target,
    colors: usize,
    depth: bool,
    actions: Vec<(i32, RenderableAction<B>)>,
    deps: Vec<NodeId>,
}

impl<'a, B: Backend> TargetPlanContext<'a, B> {
    /// Add new action to render target in defined order.
    pub fn add(&mut self, order: impl Into<i32>, action: impl IntoAction<B>) -> Result<(), Error> {
        let action = action.into();

        if self.colors != action.colors() {
            return Err(format_err!(
                "Trying to add render action with {} colors to target {:?} that expects {} colors.",
                action.colors(),
                self.key,
                self.colors,
            ));
        }
        if self.depth != action.depth() {
            return Err(format_err!(
                "Trying to add render action with depth '{}' to target {:?} that expects depth '{}'.",
                action.depth(),
                self.key,
                self.depth,
            ));
        }

        self.actions.push((order.into(), action));
        Ok(())
    }

    /// Get number of color outputs of current render target.
    pub fn colors(&self) -> usize {
        self.colors
    }

    /// Check if current render target has a depth output.
    pub fn depth(&self) -> bool {
        self.depth
    }

    /// Retrieve an image produced by other render target.
    ///
    /// Results in an error if such image doesn't exist or
    /// retreiving it would result in a dependency cycle.
    pub fn get_image(&mut self, image: TargetImage) -> Result<ImageId, Error> {
        self.plan_context.get_image(image).map(|i| {
            let node = self
                .plan_context
                .get_pass_node_raw(image.target())
                .expect("Image without target node");
            self.add_dep(node);
            i
        })
    }
    /// Retrieve an image produced by other render target.
    /// Returns `None` when such image isn't registered.
    ///
    /// Results in an error if retreiving it would result in a dependency cycle.
    pub fn try_get_image(&mut self, image: TargetImage) -> Result<Option<ImageId>, Error> {
        self.plan_context.try_get_image(image).map(|i| {
            i.map(|i| {
                let node = self
                    .plan_context
                    .get_pass_node_raw(image.target())
                    .expect("Image without target node");
                self.add_dep(node);
                i
            })
        })
    }

    /// Add explicit dependency on another node.
    ///
    /// This is done automatically when you use `get_image`.
    pub fn add_dep(&mut self, node: NodeId) {
        if !self.deps.contains(&node) {
            self.deps.push(node);
        }
    }

    /// Access underlying rendy's GraphBuilder directly.
    /// This is useful for adding custom rendering nodes
    /// that are not just standard graphics render passes,
    /// e.g. for compute dispatch.
    pub fn graph(&mut self) -> &mut GraphBuilder<B, World> {
        self.plan_context.graph()
    }

    /// Retrieve render target metadata, e.g. size.
    pub fn target_metadata(&self, target: Target) -> Option<TargetMetadata> {
        self.plan_context.target_metadata(target)
    }

    /// Access computed NodeId of render target.
    pub fn get_node(&mut self, target: Target) -> Result<NodeId, Error> {
        self.plan_context.get_node(target)
    }
}

/// An identifier for output image of specific render target.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum TargetImage {
    /// Select target color output with given index.
    Color(Target, usize),
    /// Select target depth output.
    Depth(Target),
}

impl TargetImage {
    /// Retrieve target identifier for this image
    pub fn target(&self) -> Target {
        match self {
            TargetImage::Color(target, _) => *target,
            TargetImage::Depth(target) => *target,
        }
    }
}

/// Set of options required to create an image node in render graph.
#[derive(Debug, Clone, PartialEq)]
pub struct ImageOptions {
    /// Image kind and size
    pub kind: hal::image::Kind,
    /// Number of mipmap levels
    pub levels: hal::image::Level,
    /// Image format
    pub format: hal::format::Format,
    /// Clear operation performed once per frame.
    pub clear: Option<hal::command::ClearValue>,
}

/// Definition of render target color output image.
#[derive(Debug)]
pub enum OutputColor<B: Backend> {
    /// Render to image with specified options
    Image(ImageOptions),
    /// Render directly to a window surface.
    Surface(Surface<B>, Option<hal::command::ClearValue>),
}

/// Definition for set of outputs for a given render target.
#[derive(Debug)]
pub struct TargetPlanOutputs<B: Backend> {
    /// List of target color outputs with options
    pub colors: Vec<OutputColor<B>>,
    /// Settings for optional depth output
    pub depth: Option<ImageOptions>,
}

#[derive(derivative::Derivative)]
#[derivative(Debug(bound = ""))]
struct TargetPlan<B: Backend> {
    key: Target,
    #[derivative(Debug = "ignore")]
    extensions: Vec<Box<dyn FnOnce(&mut TargetPlanContext<'_, B>) -> Result<(), Error> + 'static>>,
    outputs: Option<TargetPlanOutputs<B>>,
}

impl<B: Backend> TargetPlan<B> {
    fn new(key: Target) -> Self {
        Self {
            key,
            extensions: vec![],
            outputs: None,
        }
    }

    // safety:
    // * `physical_device` must be created from same `Instance` as the `Surface` present in output
    unsafe fn metadata(&self, physical_device: &B::PhysicalDevice) -> Option<TargetMetadata> {
        self.outputs
            .as_ref()
            .map(|TargetPlanOutputs { colors, depth }| {
                use std::cmp::min;
                let mut framebuffer_width = u32::max_value();
                let mut framebuffer_height = u32::max_value();
                let mut framebuffer_layers = u16::max_value();

                for color in colors {
                    match color {
                        OutputColor::Surface(surface, _) => {
                            if let Some(extent) = surface.extent(physical_device) {
                                framebuffer_width = min(framebuffer_width, extent.width);
                                framebuffer_height = min(framebuffer_height, extent.height);
                                framebuffer_layers = min(framebuffer_layers, 1);
                            } else {
                                // Window was just closed, using size of 1 is the least bad option
                                // to default to. The output won't be used, things won't crash and
                                // graph is either going to be destroyed or rebuilt next frame.
                                framebuffer_width = min(framebuffer_width, 1);
                                framebuffer_height = min(framebuffer_height, 1);
                            }
                            framebuffer_layers = min(framebuffer_layers, 1);
                        }
                        OutputColor::Image(options) => {
                            let extent = options.kind.extent();
                            framebuffer_width = min(framebuffer_width, extent.width);
                            framebuffer_height = min(framebuffer_height, extent.height);
                            framebuffer_layers = min(framebuffer_layers, options.kind.num_layers());
                        }
                    };
                }
                if let Some(options) = depth {
                    let extent = options.kind.extent();
                    framebuffer_width = min(framebuffer_width, extent.width);
                    framebuffer_height = min(framebuffer_height, extent.height);
                    framebuffer_layers = min(framebuffer_layers, options.kind.num_layers());
                }
                TargetMetadata {
                    width: framebuffer_width,
                    height: framebuffer_height,
                    layers: framebuffer_layers,
                }
            })
    }

    fn set_outputs(&mut self, outputs: TargetPlanOutputs<B>) -> Result<(), Error> {
        if self.outputs.is_some() {
            return Err(format_err!("Target {:?} already defined.", self.key));
        }
        self.outputs.replace(outputs);
        Ok(())
    }

    fn add_extension(
        &mut self,
        extension: Box<dyn FnOnce(&mut TargetPlanContext<'_, B>) -> Result<(), Error> + 'static>,
    ) {
        self.extensions.push(extension);
    }

    fn evaluate(self, ctx: &mut PlanContext<B>) -> Result<(), Error> {
        if self.outputs.is_none() {
            return Err(format_err!(
                "Trying to evaluate not fully defined pass {:?}. Missing `define_pass` call.",
                self.key
            ));
        }
        let mut outputs = self.outputs.unwrap();

        ctx.mark_evaluating(self.key)?;

        let mut target_ctx = TargetPlanContext {
            plan_context: ctx,
            key: self.key,
            actions: vec![],
            colors: outputs.colors.len(),
            depth: outputs.depth.is_some(),
            deps: vec![],
        };

        for extension in self.extensions {
            extension(&mut target_ctx)?;
        }

        let TargetPlanContext {
            mut actions, deps, ..
        } = target_ctx;

        let mut subpass = SubpassBuilder::new();
        let mut pass = RenderPassNodeBuilder::new();

        actions.sort_by_key(|a| a.0);
        for action in actions.drain(..).map(|a| a.1) {
            match action {
                RenderableAction::RenderGroup(group) => {
                    subpass.add_dyn_group(group);
                }
            }
        }

        for (i, color) in outputs.colors.drain(..).enumerate() {
            match color {
                OutputColor::Surface(surface, clear) => {
                    subpass.add_color_surface();
                    pass.add_surface(surface, clear);
                }
                OutputColor::Image(opts) => {
                    let node = ctx.create_image(opts);
                    ctx.register_output(TargetImage::Color(self.key, i), node)?;
                    subpass.add_color(node);
                }
            }
        }

        if let Some(opts) = outputs.depth {
            let node = ctx.create_image(opts);
            ctx.register_output(TargetImage::Depth(self.key), node)?;
            subpass.set_depth_stencil(node);
        }

        for node in deps {
            subpass.add_dependency(node);
        }

        pass.add_subpass(subpass);
        ctx.submit_pass(self.key, pass)?;
        Ok(())
    }
}

/// An action that represents a single transformation to the
/// render graph, e.g. addition of single render group.
///
/// TODO: more actions needed for e.g. splitting pass into subpasses.
#[derive(Debug)]
pub enum RenderableAction<B: Backend> {
    /// Register single render group for evaluation during target rendering
    RenderGroup(Box<dyn RenderGroupBuilder<B, World>>),
}

impl<B: Backend> RenderableAction<B> {
    fn colors(&self) -> usize {
        match self {
            RenderableAction::RenderGroup(g) => g.colors(),
        }
    }

    fn depth(&self) -> bool {
        match self {
            RenderableAction::RenderGroup(g) => g.depth(),
        }
    }
}

/// Trait for easy conversion of various types into `RenderableAction` shell.
pub trait IntoAction<B: Backend> {
    /// Convert to `RenderableAction`.
    fn into(self) -> RenderableAction<B>;
}

impl<B: Backend, G: RenderGroupBuilder<B, World> + 'static> IntoAction<B> for G {
    fn into(self) -> RenderableAction<B> {
        RenderableAction::RenderGroup(Box::new(self))
    }
}

/// Collection of predefined constants for action ordering in the builtin targets.
/// Two actions with the same order will be applied in their insertion order.
/// The list is provided mostly as a comparison point. If you can't find the exact
/// ordering you need, provide custom `i32` that fits into the right place.
///
/// Modules that provide custom render plugins using their own orders should export
/// similar enum with ordering they have added.
#[derive(Debug)]
#[repr(i32)]
pub enum RenderOrder {
    /// register before all opaques
    BeforeOpaque = 90,
    /// register for rendering opaque objects
    Opaque = 100,
    /// register after rendering opaque objects
    AfterOpaque = 110,
    /// register before rendering transparent objects
    BeforeTransparent = 190,
    /// register for rendering transparent objects
    Transparent = 200,
    /// register after rendering transparent objects
    AfterTransparent = 210,
    /// register as post effect in linear color space
    LinearPostEffects = 300,
    /// register as tonemapping step
    ToneMap = 400,
    /// register as post effect in display color space
    DisplayPostEffects = 500,
    /// register as overlay on final render
    Overlay = 600,
}

impl Into<i32> for RenderOrder {
    fn into(self) -> i32 {
        self as i32
    }
}

/// An identifier for render target used in render plugins.
/// Predefined targets are part of default rendering flow
/// used by builtin amethyst render plugins, but the list
/// can be arbitrarily extended for custom usage in user
/// plugins using custom str identifiers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Target {
    /// Default render target for most operations.
    /// Usually the one that gets presented to the window.
    Main,
    /// Render target for shadow mapping.
    /// Builtin plugins use cascaded shadow maps.
    ShadowMap,
    /// Custom render target identifier.
    Custom(&'static str),
}

impl Default for Target {
    fn default() -> Target {
        Target::Main
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        rendy::{
            command::QueueId,
            graph::{
                render::{RenderGroup, RenderGroupDesc},
                GraphContext, NodeBuffer, NodeImage,
            },
        },
        types::{Backend, DefaultBackend},
    };
    use hal::{
        command::{ClearDepthStencil, ClearValue},
        format::Format,
    };

    #[derive(Debug)]
    struct TestGroup1;
    #[derive(Debug)]
    struct TestGroup2;

    impl<B: Backend, T> RenderGroupDesc<B, T> for TestGroup1 {
        fn build(
            self,
            ctx: &GraphContext<B>,
            factory: &mut Factory<B>,
            queue: QueueId,
            aux: &T,
            framebuffer_width: u32,
            framebuffer_height: u32,
            subpass: hal::pass::Subpass<'_, B>,
            buffers: Vec<NodeBuffer>,
            images: Vec<NodeImage>,
        ) -> Result<Box<dyn RenderGroup<B, T>>, failure::Error> {
            unimplemented!()
        }
    }
    impl<B: Backend, T> RenderGroupDesc<B, T> for TestGroup2 {
        fn build(
            self,
            ctx: &GraphContext<B>,
            factory: &mut Factory<B>,
            queue: QueueId,
            aux: &T,
            framebuffer_width: u32,
            framebuffer_height: u32,
            subpass: hal::pass::Subpass<'_, B>,
            buffers: Vec<NodeBuffer>,
            images: Vec<NodeImage>,
        ) -> Result<Box<dyn RenderGroup<B, T>>, failure::Error> {
            unimplemented!()
        }
    }

    #[test]
    #[ignore] // CI can't run tests requiring actual backend
    fn main_pass_color_image_plan() {
        let config: rendy::factory::Config = Default::default();
        let (factory, families): (Factory<DefaultBackend>, _) =
            rendy::factory::init(config).unwrap();
        let mut plan = RenderPlan::<DefaultBackend>::new();

        plan.extend_target(Target::Main, |ctx| {
            ctx.add(RenderOrder::Transparent, TestGroup1.builder())?;
            ctx.add(RenderOrder::Opaque, TestGroup2.builder())?;
            Ok(())
        });

        let kind = crate::Kind::D2(1920, 1080, 1, 1);
        plan.add_root(Target::Main);
        plan.define_pass(
            Target::Main,
            TargetPlanOutputs {
                colors: vec![OutputColor::Image(ImageOptions {
                    kind,
                    levels: 1,
                    format: Format::Rgb8Unorm,
                    clear: None,
                })],
                depth: Some(ImageOptions {
                    kind,
                    levels: 1,
                    format: Format::D32Sfloat,
                    clear: Some(ClearValue::DepthStencil(ClearDepthStencil(1.0, 0))),
                }),
            },
        )
        .unwrap();

        let planned_graph = plan.build(&factory).unwrap();

        let mut manual_graph = GraphBuilder::<DefaultBackend, World>::new();
        let color = manual_graph.create_image(kind, 1, Format::Rgb8Unorm, None);
        let depth = manual_graph.create_image(
            kind,
            1,
            Format::D32Sfloat,
            Some(ClearValue::DepthStencil(ClearDepthStencil(1.0, 0))),
        );
        manual_graph.add_node(
            RenderPassNodeBuilder::new().with_subpass(
                SubpassBuilder::new()
                    .with_group(TestGroup2.builder())
                    .with_group(TestGroup1.builder())
                    .with_color(color)
                    .with_depth_stencil(depth),
            ),
        );

        assert_eq!(
            format!("{:?}", planned_graph),
            format!("{:?}", manual_graph)
        );
    }

    #[test]
    #[ignore] // CI can't run tests requiring actual backend
    fn main_pass_surface_plan() {
        use winit::{EventsLoop, WindowBuilder};

        let ev_loop = EventsLoop::new();
        let mut window_builder = WindowBuilder::new();
        window_builder.window.visible = false;
        let window = window_builder.build(&ev_loop).unwrap();

        let size = window
            .get_inner_size()
            .unwrap()
            .to_physical(window.get_hidpi_factor());
        let window_kind = crate::Kind::D2(size.width as u32, size.height as u32, 1, 1);

        let config: rendy::factory::Config = Default::default();
        let (mut factory, families): (Factory<DefaultBackend>, _) =
            rendy::factory::init(config).unwrap();
        let mut plan = RenderPlan::<DefaultBackend>::new();

        let surface1 = factory.create_surface(&window);
        let surface2 = factory.create_surface(&window);

        plan.extend_target(Target::Main, |ctx| {
            ctx.add(RenderOrder::Opaque, TestGroup2.builder())?;
            Ok(())
        });

        plan.add_root(Target::Main);
        plan.define_pass(
            Target::Main,
            TargetPlanOutputs {
                colors: vec![OutputColor::Surface(surface1, None)],
                depth: Some(ImageOptions {
                    kind: window_kind,
                    levels: 1,
                    format: Format::D32Sfloat,
                    clear: Some(ClearValue::DepthStencil(ClearDepthStencil(1.0, 0))),
                }),
            },
        )
        .unwrap();

        plan.extend_target(Target::Main, |ctx| {
            ctx.add(RenderOrder::Transparent, TestGroup1.builder())?;
            Ok(())
        });

        let planned_graph = plan.build(&factory).unwrap();

        let mut manual_graph = GraphBuilder::<DefaultBackend, World>::new();
        let depth = manual_graph.create_image(
            window_kind,
            1,
            Format::D32Sfloat,
            Some(ClearValue::DepthStencil(ClearDepthStencil(1.0, 0))),
        );
        manual_graph.add_node(
            RenderPassNodeBuilder::new()
                .with_subpass(
                    SubpassBuilder::new()
                        .with_group(TestGroup2.builder())
                        .with_group(TestGroup1.builder())
                        .with_color_surface()
                        .with_depth_stencil(depth),
                )
                .with_surface(surface2, None),
        );

        assert_eq!(
            format!("{:?}", planned_graph),
            format!("{:?}", manual_graph)
        );
    }
}