mew/
vertexformat.rs

1use std::ops::{ Deref, DerefMut, };
2
3
4macro_rules! vertex_format {
5    { @if_not_u8 $name:ident u8 } => {
6    };
7    { @if_not_u8 $name:ident $ty:ident } => {
8        impl From<[u8; Self::BYTES]> for $name {
9            fn from(bytes: [u8; Self::BYTES]) -> Self {
10                Self::from_bytes(bytes)
11            }
12        }
13    };
14
15    { $name:ident [ $ty:ident ; $len:literal ] } => {
16        #[derive(Clone, Copy, Debug)]
17        //#[repr(align(1))]
18        #[repr(transparent)]
19        pub struct $name([$ty; $len]);
20
21        impl $name {
22            pub const BYTES: usize = size_of::<$ty>() * $len;
23            //const PADDING: usize = Self::BYTES.next_multiple_of($crate::wgpu::VERTEX_STRIDE_ALIGNMENT as usize) - Self::BYTES;
24        }
25
26        impl $crate::vertexformat::MewVertexFormat for $name {
27            type Bytes = [u8; Self::BYTES];
28            type Inner = [$ty; $len];
29
30            fn from_inner(inner: Self::Inner) -> Self {
31                //value.iter_mut().for_each(|val| *val = ::std::mem::transmute(val.to_le_bytes()));
32                Self(inner)
33            }
34
35            fn from_bytes(bytes: Self::Bytes) -> Self {
36                unsafe { Self(::std::mem::transmute(bytes)) }
37            }
38
39            fn zeroed() -> Self {
40                unsafe { Self(::std::mem::zeroed()) }
41            }
42        }
43
44        impl From<[$ty; $len]> for $name {
45            fn from(inner: [$ty; $len]) -> Self {
46                Self::from_inner(inner)
47            }
48        }
49        vertex_format! { @if_not_u8 $name $ty }
50
51        impl Deref for $name {
52            type Target = [$ty; $len];
53            fn deref(&self) -> &Self::Target {
54                &self.0
55            }
56        }
57        impl DerefMut for $name {
58            fn deref_mut(&mut self) -> &mut Self::Target {
59                &mut self.0
60            }
61        }
62
63        impl AsRef<[u8; Self::BYTES]> for $name {
64            fn as_ref(&self) -> &[u8; Self::BYTES] {
65                unsafe { ::std::mem::transmute(&self.0) }
66            }
67        }
68        impl AsMut<[u8; Self::BYTES]> for $name {
69            fn as_mut(&mut self) -> &mut [u8; Self::BYTES] {
70                unsafe { ::std::mem::transmute(&mut self.0) }
71            }
72        }
73
74        impl Default for $name {
75            fn default() -> Self {
76                Self::zeroed()
77            }
78        }
79    };
80}
81
82vertex_format! { Uint8 [u8; 1] }  // => 1 }
83vertex_format! { Uint8x2 [u8; 2] }  // => 2 }
84vertex_format! { Uint8x4 [u8; 4] }  // => 4 }
85vertex_format! { Sint8 [i8; 1] }  // => 1 }
86vertex_format! { Sint8x2 [i8; 2] }  // => 2 }
87vertex_format! { Sint8x4 [i8; 4] }  // => 4 }
88
89vertex_format! { Unorm8 [u8; 1] }  // => 1 }
90vertex_format! { Unorm8x2 [u8; 2] }  // => 2 }
91vertex_format! { Unorm8x4 [u8; 4] }  // => 4 }
92vertex_format! { Snorm8 [i8; 1] }  // => 1 }
93vertex_format! { Snorm8x2 [i8; 2] }  // => 2 }
94vertex_format! { Snorm8x4 [i8; 4] }  // => 4 }
95
96
97vertex_format! { Uint16 [u16; 1] }  // => 2 }
98vertex_format! { Uint16x2 [u16; 2] }  // => 4 }
99vertex_format! { Uint16x4 [u16; 4] }  // => 8 }
100vertex_format! { Sint16 [i16; 1] }  // => 2 }
101vertex_format! { Sint16x2 [i16; 2] }  // => 4 }
102vertex_format! { Sint16x4 [i16; 4] }  // => 8 }
103
104vertex_format! { Unorm16 [u16; 1] }  // => 2 }
105vertex_format! { Unorm16x2 [u16; 2] }  // => 4 }
106vertex_format! { Unorm16x4 [u16; 4] }  // => 8 }
107vertex_format! { Snorm16 [i16; 1] }  // => 2 }
108vertex_format! { Snorm16x2 [i16; 2] }  // => 4 }
109vertex_format! { Snorm16x4 [i16; 4] }  // => 8 }
110
111vertex_format! { Float16 [u16; 1] }  // => 2 }
112vertex_format! { Float16x2 [u16; 2] }  // => 4 }
113vertex_format! { Float16x4 [u16; 4] }  // => 8 }
114
115
116vertex_format! { Uint32 [u32; 1] }  // => 4 }
117vertex_format! { Uint32x2 [u32; 2] }  // => 8 }
118vertex_format! { Uint32x3 [u32; 3] }  // => 16 }
119vertex_format! { Uint32x4 [u32; 4] }  // => 16 }
120vertex_format! { Sint32 [i32; 1] }  // => 4 }
121vertex_format! { Sint32x2 [i32; 2] }  // => 8 }
122vertex_format! { Sint32x3 [i32; 3] }  // => 16 }
123vertex_format! { Sint32x4 [i32; 4] }  // => 16 }
124
125vertex_format! { Float32 [f32; 1] }  // => 4 }
126vertex_format! { Float32x2 [f32; 2] }  // => 4 }
127vertex_format! { Float32x3 [f32; 3] }  // => 8 }
128vertex_format! { Float32x4 [f32; 4] }  // => 8 }
129
130
131vertex_format! { Float64 [f64; 1] }  // => 8 }
132vertex_format! { Float64x2 [f64; 2] }  // => 16 }
133vertex_format! { Float64x3 [f64; 3] }  // => 16 }
134vertex_format! { Float64x4 [f64; 4] }  // => 16 }
135
136
137pub type Unorm10_10_10_2 = Uint32;
138pub type Unorm8x4Bgra = Unorm8x4;
139
140
141pub trait MewVertexFormat {
142    type Bytes;
143    type Inner;
144    fn from_inner(inner: Self::Inner) -> Self;
145    fn from_bytes(bytes: Self::Bytes) -> Self;
146    fn zeroed() -> Self;
147}