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
//! Capability module docs.

pub use gfx_hal::QueueType;

/// Capable of transfer only.
#[derive(Clone, Copy, Debug)]
pub struct Transfer;

/// Capable of either compute or graphics commands execution.
#[derive(Clone, Copy, Debug)]
pub struct Execute;

/// Capable of compute commands execution.
#[derive(Clone, Copy, Debug)]
pub struct Compute;

/// Capable of graphics command execution.
#[derive(Clone, Copy, Debug)]
pub struct Graphics;

/// Capable of any commands execution.
#[derive(Clone, Copy, Debug)]
pub struct General;

/// Abstract capability specifier.
pub trait Capability: Copy + std::fmt::Debug + 'static {
    /// Try to create capability instance from queue_type.
    /// Instance will be created if all required queue_type set.
    fn from_queue_type(queue_type: QueueType) -> Option<Self>;

    /// Convert into `QueueType`
    fn into_queue_type(self) -> QueueType;
}

impl Capability for QueueType {
    fn from_queue_type(queue_type: QueueType) -> Option<Self> {
        Some(queue_type)
    }

    fn into_queue_type(self) -> QueueType {
        self
    }
}

impl Capability for Transfer {
    fn from_queue_type(_queue_type: QueueType) -> Option<Self> {
        Some(Transfer)
    }

    fn into_queue_type(self) -> QueueType {
        QueueType::Transfer
    }
}

impl Capability for Execute {
    fn from_queue_type(queue_type: QueueType) -> Option<Self> {
        match queue_type {
            QueueType::Transfer => None,
            _ => Some(Execute),
        }
    }

    fn into_queue_type(self) -> QueueType {
        QueueType::General
    }
}

impl Capability for Compute {
    fn from_queue_type(queue_type: QueueType) -> Option<Self> {
        match queue_type {
            QueueType::Compute | QueueType::General => Some(Compute),
            _ => None,
        }
    }

    fn into_queue_type(self) -> QueueType {
        QueueType::Compute
    }
}

impl Capability for Graphics {
    fn from_queue_type(queue_type: QueueType) -> Option<Self> {
        match queue_type {
            QueueType::Graphics | QueueType::General => Some(Graphics),
            _ => None,
        }
    }

    fn into_queue_type(self) -> QueueType {
        QueueType::Graphics
    }
}

impl Capability for General {
    fn from_queue_type(queue_type: QueueType) -> Option<Self> {
        match queue_type {
            QueueType::General => Some(General),
            _ => None,
        }
    }

    fn into_queue_type(self) -> QueueType {
        QueueType::General
    }
}

/// Check if capability supported.
pub trait Supports<C>: Capability {
    /// Check runtime capability.
    fn supports(&self) -> Option<C>;

    /// Assert capability.
    fn assert(&self) {
        assert!(self.supports().is_some());
    }
}

impl Supports<Transfer> for Transfer {
    fn supports(&self) -> Option<Transfer> {
        Some(Transfer)
    }
}

impl Supports<Transfer> for Compute {
    fn supports(&self) -> Option<Transfer> {
        Some(Transfer)
    }
}

impl Supports<Transfer> for Graphics {
    fn supports(&self) -> Option<Transfer> {
        Some(Transfer)
    }
}

impl Supports<Transfer> for General {
    fn supports(&self) -> Option<Transfer> {
        Some(Transfer)
    }
}

impl Supports<Execute> for Compute {
    fn supports(&self) -> Option<Execute> {
        Some(Execute)
    }
}

impl Supports<Execute> for Graphics {
    fn supports(&self) -> Option<Execute> {
        Some(Execute)
    }
}

impl Supports<Execute> for General {
    fn supports(&self) -> Option<Execute> {
        Some(Execute)
    }
}

impl Supports<Compute> for Compute {
    fn supports(&self) -> Option<Compute> {
        Some(Compute)
    }
}

impl Supports<Compute> for General {
    fn supports(&self) -> Option<Compute> {
        Some(Compute)
    }
}

impl Supports<Graphics> for Graphics {
    fn supports(&self) -> Option<Graphics> {
        Some(Graphics)
    }
}

impl Supports<Graphics> for General {
    fn supports(&self) -> Option<Graphics> {
        Some(Graphics)
    }
}

impl<C> Supports<C> for QueueType
where
    C: Capability,
{
    fn supports(&self) -> Option<C> {
        C::from_queue_type(*self)
    }
}