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
//! Type system encoded queue capabilities.
use crate::queue::QueueType;

/// General capability, supporting graphics, compute and transfer operations.
#[derive(Debug)]
pub enum General {}
/// Graphics capability, supporting graphics and transfer operations.
#[derive(Debug)]
pub enum Graphics {}
/// Compute capability, supporting compute and transfer operations.
#[derive(Debug)]
pub enum Compute {}
/// Transfer capability, supporting only transfer operations.
#[derive(Debug)]
pub enum Transfer {}

/// Graphics or compute capability.
#[derive(Debug)]
pub enum GraphicsOrCompute {}

/// A Capability is an object that specifies what kind of operations
/// a queue type performs, allowing what types support what queue operations
/// to be described at runtime by the type system.
pub trait Capability {
    /// Return true if this type level capability is supported by
    /// a run-time queue type.
    fn supported_by(qt: QueueType) -> bool;
}
impl Capability for General {
    fn supported_by(qt: QueueType) -> bool {
        match qt {
            QueueType::General => true,
            _ => false,
        }
    }
}
impl Capability for Graphics {
    fn supported_by(qt: QueueType) -> bool {
        match qt {
            QueueType::General | QueueType::Graphics => true,
            _ => false,
        }
    }
}
impl Capability for Compute {
    fn supported_by(qt: QueueType) -> bool {
        match qt {
            QueueType::General | QueueType::Compute => true,
            _ => false,
        }
    }
}
impl Capability for Transfer {
    fn supported_by(qt: QueueType) -> bool {
        match qt {
            QueueType::General | QueueType::Compute | QueueType::Graphics | QueueType::Transfer => {
                true
            }
        }
    }
}

/// A trait that indicates that a particular type of queue supports
/// a particular `Capability`.
pub trait Supports<T> {}
impl<T> Supports<T> for T {}
impl Supports<Graphics> for General {}
impl Supports<Compute> for General {}
impl Supports<Transfer> for General {}
impl Supports<Transfer> for Graphics {}
impl Supports<Transfer> for Compute {}

impl Supports<GraphicsOrCompute> for General {}
impl Supports<GraphicsOrCompute> for Graphics {}
impl Supports<GraphicsOrCompute> for Compute {}

/// Encoding the minimal capability to support a combination of other capabilities.
pub trait Upper {
    /// Resulting minimal required capability.
    type Result;
}

impl<T> Upper for (T, T) {
    type Result = T;
}
impl Upper for (General, Graphics) {
    type Result = General;
}
impl Upper for (General, Compute) {
    type Result = General;
}
impl Upper for (General, Transfer) {
    type Result = General;
}
impl Upper for (Graphics, General) {
    type Result = General;
}
impl Upper for (Graphics, Compute) {
    type Result = General;
}
impl Upper for (Graphics, Transfer) {
    type Result = Graphics;
}
impl Upper for (Compute, General) {
    type Result = General;
}
impl Upper for (Compute, Graphics) {
    type Result = General;
}
impl Upper for (Compute, Transfer) {
    type Result = Compute;
}
impl Upper for (Transfer, General) {
    type Result = General;
}
impl Upper for (Transfer, Graphics) {
    type Result = Graphics;
}
impl Upper for (Transfer, Compute) {
    type Result = Compute;
}