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(transparent)]
19 pub struct $name([$ty; $len]);
20
21 impl $name {
22 pub const BYTES: usize = size_of::<$ty>() * $len;
23 }
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 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] } vertex_format! { Uint8x2 [u8; 2] } vertex_format! { Uint8x4 [u8; 4] } vertex_format! { Sint8 [i8; 1] } vertex_format! { Sint8x2 [i8; 2] } vertex_format! { Sint8x4 [i8; 4] } vertex_format! { Unorm8 [u8; 1] } vertex_format! { Unorm8x2 [u8; 2] } vertex_format! { Unorm8x4 [u8; 4] } vertex_format! { Snorm8 [i8; 1] } vertex_format! { Snorm8x2 [i8; 2] } vertex_format! { Snorm8x4 [i8; 4] } vertex_format! { Uint16 [u16; 1] } vertex_format! { Uint16x2 [u16; 2] } vertex_format! { Uint16x4 [u16; 4] } vertex_format! { Sint16 [i16; 1] } vertex_format! { Sint16x2 [i16; 2] } vertex_format! { Sint16x4 [i16; 4] } vertex_format! { Unorm16 [u16; 1] } vertex_format! { Unorm16x2 [u16; 2] } vertex_format! { Unorm16x4 [u16; 4] } vertex_format! { Snorm16 [i16; 1] } vertex_format! { Snorm16x2 [i16; 2] } vertex_format! { Snorm16x4 [i16; 4] } vertex_format! { Float16 [u16; 1] } vertex_format! { Float16x2 [u16; 2] } vertex_format! { Float16x4 [u16; 4] } vertex_format! { Uint32 [u32; 1] } vertex_format! { Uint32x2 [u32; 2] } vertex_format! { Uint32x3 [u32; 3] } vertex_format! { Uint32x4 [u32; 4] } vertex_format! { Sint32 [i32; 1] } vertex_format! { Sint32x2 [i32; 2] } vertex_format! { Sint32x3 [i32; 3] } vertex_format! { Sint32x4 [i32; 4] } vertex_format! { Float32 [f32; 1] } vertex_format! { Float32x2 [f32; 2] } vertex_format! { Float32x3 [f32; 3] } vertex_format! { Float32x4 [f32; 4] } vertex_format! { Float64 [f64; 1] } vertex_format! { Float64x2 [f64; 2] } vertex_format! { Float64x3 [f64; 3] } vertex_format! { Float64x4 [f64; 4] } pub 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}