rendy_chain/
resource.rs

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
use std::{
    fmt::Debug,
    ops::{BitOr, BitOrAssign},
};

/// Trait to abstract of specific access flags.
pub trait AccessFlags: Copy + Debug + BitOr<Output = Self> + BitOrAssign + 'static {
    /// Get flags value with no flags set.
    fn empty() -> Self;

    /// Check if this access must be exclusive.
    ///
    /// Basically this checks if all flags are known read flags.
    fn exclusive(&self) -> bool;
}

impl AccessFlags for rendy_core::hal::buffer::Access {
    #[inline]
    fn empty() -> Self {
        Self::empty()
    }

    #[inline]
    fn exclusive(&self) -> bool {
        self.intersects(
            Self::SHADER_WRITE | Self::TRANSFER_WRITE | Self::HOST_WRITE | Self::MEMORY_WRITE,
        )
    }
}

impl AccessFlags for rendy_core::hal::image::Access {
    #[inline]
    fn empty() -> Self {
        Self::empty()
    }

    #[inline]
    fn exclusive(&self) -> bool {
        self.intersects(
            Self::SHADER_WRITE
                | Self::COLOR_ATTACHMENT_WRITE
                | Self::DEPTH_STENCIL_ATTACHMENT_WRITE
                | Self::TRANSFER_WRITE
                | Self::HOST_WRITE
                | Self::MEMORY_WRITE,
        )
    }
}

/// Trait to abstract of specific usage flags.
pub trait UsageFlags: Copy + Debug + BitOr<Output = Self> + BitOrAssign + 'static {}

impl UsageFlags for rendy_core::hal::buffer::Usage {}
impl UsageFlags for rendy_core::hal::image::Usage {}

/// Abstracts resource types that uses different usage flags and layouts types.
pub trait Resource: 'static {
    /// Access flags for resource type.
    type Access: AccessFlags;

    /// Usage flags type for the resource.
    type Usage: UsageFlags;

    /// Layout type for the resource.
    type Layout: Copy + Debug + 'static;

    /// Empty usage.
    fn no_usage() -> Self::Usage;

    /// Layout suitable for specified accesses.
    fn layout_for(access: Self::Access) -> Self::Layout;
}

/// Buffer resource type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Buffer;

impl Resource for Buffer {
    type Access = rendy_core::hal::buffer::Access;
    type Usage = rendy_core::hal::buffer::Usage;
    type Layout = ();

    fn no_usage() -> Self::Usage {
        rendy_core::hal::buffer::Usage::empty()
    }

    fn layout_for(_access: rendy_core::hal::buffer::Access) {}
}

/// Image resource type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Image;

impl Resource for Image {
    type Access = rendy_core::hal::image::Access;

    type Usage = rendy_core::hal::image::Usage;

    type Layout = rendy_core::hal::image::Layout;

    fn no_usage() -> Self::Usage {
        rendy_core::hal::image::Usage::empty()
    }

    fn layout_for(access: rendy_core::hal::image::Access) -> rendy_core::hal::image::Layout {
        let mut acc = None;
        if access.contains(rendy_core::hal::image::Access::INPUT_ATTACHMENT_READ) {
            acc = Some(common_layout(
                acc,
                rendy_core::hal::image::Layout::ShaderReadOnlyOptimal,
            ));
        }
        if access.contains(rendy_core::hal::image::Access::COLOR_ATTACHMENT_READ) {
            acc = Some(common_layout(
                acc,
                rendy_core::hal::image::Layout::ColorAttachmentOptimal,
            ));
        }
        if access.contains(rendy_core::hal::image::Access::COLOR_ATTACHMENT_WRITE) {
            acc = Some(common_layout(
                acc,
                rendy_core::hal::image::Layout::ColorAttachmentOptimal,
            ));
        }
        if access.contains(rendy_core::hal::image::Access::DEPTH_STENCIL_ATTACHMENT_READ) {
            acc = Some(common_layout(
                acc,
                rendy_core::hal::image::Layout::DepthStencilReadOnlyOptimal,
            ));
        }
        if access.contains(rendy_core::hal::image::Access::DEPTH_STENCIL_ATTACHMENT_WRITE) {
            acc = Some(common_layout(
                acc,
                rendy_core::hal::image::Layout::DepthStencilAttachmentOptimal,
            ));
        }
        if access.contains(rendy_core::hal::image::Access::TRANSFER_READ) {
            acc = Some(common_layout(
                acc,
                rendy_core::hal::image::Layout::TransferSrcOptimal,
            ));
        }
        if access.contains(rendy_core::hal::image::Access::TRANSFER_WRITE) {
            acc = Some(common_layout(
                acc,
                rendy_core::hal::image::Layout::TransferDstOptimal,
            ));
        }
        acc.unwrap_or(rendy_core::hal::image::Layout::General)
    }
}

fn common_layout(
    acc: Option<rendy_core::hal::image::Layout>,
    layout: rendy_core::hal::image::Layout,
) -> rendy_core::hal::image::Layout {
    match (acc, layout) {
        (None, layout) => layout,
        (Some(left), right) if left == right => left,
        (
            Some(rendy_core::hal::image::Layout::DepthStencilReadOnlyOptimal),
            rendy_core::hal::image::Layout::DepthStencilAttachmentOptimal,
        ) => rendy_core::hal::image::Layout::DepthStencilAttachmentOptimal,
        (
            Some(rendy_core::hal::image::Layout::DepthStencilAttachmentOptimal),
            rendy_core::hal::image::Layout::DepthStencilReadOnlyOptimal,
        ) => rendy_core::hal::image::Layout::DepthStencilAttachmentOptimal,
        (Some(_), _) => rendy_core::hal::image::Layout::General,
    }
}