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
pub use agpu_macro::{VertexLayout, VertexLayoutInstance};

pub trait VertexLayout {
    fn vertex_buffer_layout<const L: u32>() -> wgpu::VertexBufferLayout<'static>;
}

pub trait VertexLayoutImpl {
    fn vertex_buffer_layout<const L: u32>() -> wgpu::VertexBufferLayout<'static>;
}

/// Automatic vertex format derivation
/// Some general assumptions are made here:
/// - All fields are primitive types (or array, or matrix (TODO), or tuple)
/// - (TODO) Zero-sized fields will be ignored
/// ### Tuples vs Arrays
/// - For 8- and 16-bit integers:
///   - tuples will be passed to shader as a uvec or ivec
///   - arrays will be passed to shader as a normalized vec (converted to 0.0..1.0)
/// - For 32- and 64-bit integers, tuples/arrays are passed as uvec or ivec
/// - For floats, tuples/arrays are passed as vec
pub trait VertexFormatType {
    const VERTEX_FORMAT_TYPE: wgpu::VertexFormat;
}

macro_rules! gen_vertex_format_types {
    ($($t:ty => $v:ident),*) => {
        $(
            impl VertexFormatType for $t {
                const VERTEX_FORMAT_TYPE: ::wgpu::VertexFormat = ::wgpu::VertexFormat::$v;
            }
        )*
    };
}

// TODO: Support matrix input, creating multiple attributes for each matrix

gen_vertex_format_types!(
    (u8, u8) => Uint8x2,
    (u8, u8, u8, u8) => Uint8x4,
    (i8, i8) => Sint8x2,
    (i8, i8, i8, i8) => Sint8x4,
    [u8; 2] => Unorm8x2,
    [u8; 4] => Unorm8x4,
    [i8; 2] => Snorm8x2,
    [i8; 4] => Snorm8x4,
    (u16, u16) => Uint16x2,
    (u16, u16, u16, u16) => Uint16x4,
    (i16, i16) => Sint16x2,
    (i16, i16, i16, i16) => Sint16x4,
    [u16; 2] => Unorm16x2,
    [u16; 4] => Unorm16x4,
    [i16; 2] => Snorm16x2,
    [i16; 4] => Snorm16x4,
    f32 => Float32,
    [f32; 1] => Float32,
    [f32; 2] => Float32x2,
    (f32, f32) => Float32x2,
    [f32; 3] => Float32x3,
    (f32, f32, f32) => Float32x3,
    [f32; 4] => Float32x4,
    (f32, f32, f32, f32) => Float32x4,
    u32 => Uint32,
    [u32; 1] => Uint32,
    [u32; 2] => Uint32x2,
    (u32, u32) => Uint32x2,
    [u32; 3] => Uint32x3,
    (u32, u32, u32) => Uint32x3,
    [u32; 4] => Uint32x4,
    (u32, u32, u32, u32) => Uint32x4,
    i32 => Sint32,
    [i32; 1] => Sint32,
    [i32; 2] => Sint32x2,
    (i32, i32) => Sint32x2,
    [i32; 3] => Sint32x3,
    (i32, i32, i32) => Sint32x3,
    [i32; 4] => Sint32x4,
    (i32, i32, i32, i32) => Sint32x4,
    f64 => Float64,
    [f64; 1] => Float64,
    [f64; 2] => Float64x2,
    (f64, f64) => Float64x2,
    [f64; 3] => Float64x3,
    (f64, f64, f64) => Float64x3,
    [f64; 4] => Float64x4,
    (f64, f64, f64, f64) => Float64x4
);

// Vertex types for half floats, supported by half crate feature
#[cfg(feature = "half")]
gen_vertex_format_types!(
    [half::f16; 2] => Float16x2,
    [half::f16; 4] => Float16x4
);