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
//! Convenience macros

/// Macro to produce an array of [`VertexAttributeDescriptor`].
///
/// Output has type: `[VertexAttributeDescriptor; _]`. Usage is as follows:
/// ```
/// # use wgpu::vertex_attr_array;
/// let attrs = vertex_attr_array![0 => Float2, 1 => Float, 2 => Ushort4];
/// ```
/// This example specifies a list of three [`VertexAttributeDescriptor`],
/// each with the given `shader_location` and `format`.
/// Offsets are calculated automatically.
#[macro_export]
macro_rules! vertex_attr_array {
    ($($loc:expr => $fmt:ident),*) => {
        $crate::vertex_attr_array!([] ; 0; $($loc => $fmt ,)*)
    };
    ([$($t:expr,)*] ; $off:expr ;) => { [$($t,)*] };
    ([$($t:expr,)*] ; $off:expr ; $loc:expr => $item:ident, $($ll:expr => $ii:ident ,)*) => {
        $crate::vertex_attr_array!(
            [$($t,)*
            $crate::VertexAttributeDescriptor {
                format: $crate::VertexFormat :: $item,
                offset: $off,
                shader_location: $loc,
            },];
            $off + $crate::vertex_format_size!($item);
            $($ll => $ii ,)*
        )
    };
}

/// Helper macro which turns a vertex attribute type into a size.
///
/// Mainly used as a helper for [`vertex_attr_array`] but might be externally useful.
#[macro_export]
macro_rules! vertex_format_size {
    (Uchar2) => {
        2
    };
    (Uchar4) => {
        4
    };
    (Char2) => {
        2
    };
    (Char4) => {
        4
    };
    (Uchar2Norm) => {
        2
    };
    (Uchar4Norm) => {
        4
    };
    (Char2Norm) => {
        2
    };
    (Char4Norm) => {
        4
    };
    (Ushort2) => {
        4
    };
    (Ushort4) => {
        8
    };
    (Short2) => {
        4
    };
    (Short4) => {
        8
    };
    (Ushort2Norm) => {
        4
    };
    (Ushort4Norm) => {
        8
    };
    (Short2Norm) => {
        4
    };
    (Short4Norm) => {
        8
    };
    (Half2) => {
        4
    };
    (Half4) => {
        8
    };
    (Float) => {
        4
    };
    (Float2) => {
        8
    };
    (Float3) => {
        12
    };
    (Float4) => {
        16
    };
    (Uint) => {
        4
    };
    (Uint2) => {
        8
    };
    (Uint3) => {
        12
    };
    (Uint4) => {
        16
    };
    (Int) => {
        4
    };
    (Int2) => {
        8
    };
    (Int3) => {
        12
    };
    (Int4) => {
        16
    };
}

#[test]
fn test_vertex_attr_array() {
    let attrs = vertex_attr_array![0 => Float2, 3 => Ushort4];
    // VertexAttributeDescriptor does not support PartialEq, so we cannot test directly
    assert_eq!(attrs.len(), 2);
    assert_eq!(attrs[0].offset, 0);
    assert_eq!(attrs[0].shader_location, 0);
    assert_eq!(attrs[1].offset, std::mem::size_of::<(f32, f32)>() as u64);
    assert_eq!(attrs[1].shader_location, 3);
}

/// Macro to load a SPIR-V module statically.
///
/// It ensure the word alignment as well as the magic number.
#[macro_export]
macro_rules! include_spirv {
    ($path:literal) => {
        $crate::util::make_spirv(&$crate::util::WordAligned(*include_bytes!($path)).0)
    };
}