aboutsummaryrefslogtreecommitdiff
path: root/stockton-skeleton/src/builders/renderpass.rs
blob: 7a0141252004ff26b3efc5eed8280bb4606806fa (plain)
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
use crate::types::*;

use std::iter::{empty, once};

use anyhow::Result;
use hal::pass::{Attachment, AttachmentLayout, AttachmentRef, SubpassDesc};

/// An attachment for a renderpass
#[derive(Debug, Clone)]
pub struct AttachmentSpec {
    /// The attachment description for gfx-hal
    pub attachment: Attachment,

    /// The layout that the image should be transitioned to while this pass is in use.
    pub used_layout: AttachmentLayout,
}

#[derive(Debug, Clone)]
pub struct RenderpassSpec {
    pub colors: Vec<AttachmentSpec>,
    pub depth: Option<AttachmentSpec>,
    pub inputs: Vec<AttachmentSpec>,
    pub resolves: Vec<AttachmentSpec>,
    pub preserves: Vec<Attachment>,
}

impl RenderpassSpec {
    pub fn build_renderpass(self, device: &mut DeviceT) -> Result<RenderPassT> {
        let mut next_offset = 0;

        let colors: Vec<AttachmentRef> = self
            .colors
            .iter()
            .enumerate()
            .map(|(i, a)| (next_offset + i, a.used_layout))
            .collect();
        next_offset = colors.len();

        let depth_stencil = self.depth.as_ref().map(|x| (next_offset, x.used_layout));
        if depth_stencil.is_some() {
            next_offset += 1;
        }

        let inputs: Vec<AttachmentRef> = self
            .inputs
            .iter()
            .enumerate()
            .map(|(i, a)| (next_offset + i, a.used_layout))
            .collect();
        next_offset += inputs.len();

        let resolves: Vec<AttachmentRef> = self
            .resolves
            .iter()
            .enumerate()
            .map(|(i, a)| (next_offset + i, a.used_layout))
            .collect();
        next_offset += resolves.len();

        let preserves: Vec<usize> = self
            .preserves
            .iter()
            .enumerate()
            .map(|(i, _a)| next_offset + i)
            .collect();

        let sp_desc = SubpassDesc {
            colors: colors.as_slice(),
            depth_stencil: depth_stencil.as_ref(),
            inputs: inputs.as_slice(),
            resolves: resolves.as_slice(),
            preserves: preserves.as_slice(),
        };

        let all_attachments = self
            .colors
            .into_iter()
            .map(|x| x.attachment)
            .chain(self.depth.into_iter().map(|x| x.attachment))
            .chain(self.inputs.into_iter().map(|x| x.attachment))
            .chain(self.resolves.into_iter().map(|x| x.attachment))
            .chain(self.preserves.into_iter());

        Ok(unsafe { device.create_render_pass(all_attachments, once(sp_desc), empty())? })
    }
}