mew/
shaderstruct.rs

1/*
2#[macro_export]
3macro_rules! shader_struct2 {
4    { $struct:ident [
5        $( $num:tt $field:ident => $ty:ty ),* $(,)?
6    ] } => {
7        use $crate::primitives::*;
8
9        #[derive(Debug, Default, Copy, Clone)]
10        #[repr(C)]
11        pub struct $struct {
12            $( $field : $ty , )*
13        }
14
15        impl $struct {
16            pub fn new() -> Self {
17                Self::default()
18            }
19
20            pub fn as_bytes(&self) -> &[u8; ::std::mem::size_of::<Self>()] {
21                unsafe { ::std::mem::transmute(self) }
22            }
23        }
24    };
25}
26*/
27
28
29#[macro_export]
30macro_rules! shader_struct {
31    { $struct:ident [
32        $( $num:tt $field:ident => $ty:ident ),* $(,)?
33    ] } => {
34        #[derive(Clone, Debug)]
35        #[repr(C)]
36        pub struct $struct {
37            $( pub $field: $crate::shaderprimitive::$ty, )*
38            _padding: [u8; Self::PADDING],
39        }
40
41        impl $struct {
42            //pub const SUM_SIZE: usize = ::std::mem::size_of::<( $( $ty, )* )>();
43            pub const SUM_SIZE: usize = {
44                #[repr(C)]
45                struct SizeTest {
46                    $( $field: $crate::shaderprimitive::$ty, )*
47                }
48                ::std::mem::size_of::<SizeTest>()
49            };
50            pub const MAX_SIZE: usize = max_size! { $( $crate::shaderprimitive::$ty )* };
51            pub const NEXT_MULT: usize = Self::SUM_SIZE.next_multiple_of(Self::MAX_SIZE);
52            pub const PADDING: usize = Self::NEXT_MULT - Self::SUM_SIZE;
53        }
54
55        impl $crate::shaderstruct::MewShaderStruct for $struct {
56            type Bytes = [u8; Self::NEXT_MULT];
57            type Inners = ( $( <$crate::shaderprimitive::$ty as $crate::shaderprimitive::MewShaderPrimitive>::Inner , )* );
58
59            fn from_inners(inners: Self::Inners) -> Self {
60                use $crate::shaderprimitive::MewShaderPrimitive;
61                Self {
62                    $( $field: $crate::shaderprimitive::$ty::from_inner(inners.$num), )*
63                    _padding: [0; Self::PADDING],
64                }
65            }
66
67            fn from_bytes(bytes: Self::Bytes) -> Self {
68                unsafe { ::std::mem::transmute(bytes) }
69            }
70
71            fn zeroed() -> Self {
72                unsafe {
73                    Self {
74                        //$( $field: [0; $crate::shader_struct! { @size $ty } ], )*
75                        $( $field: ::std::mem::zeroed(), )*
76                        _padding: [0; Self::PADDING],
77                    }
78                }
79            }
80        }
81
82
83        impl From<[u8; Self::NEXT_MULT]> for $struct {
84            fn from(bytes: [u8; Self::NEXT_MULT]) -> Self {
85                Self::from_bytes(bytes)
86            }
87        }
88        impl From<( $( <$crate::shaderprimitive::$ty as $crate::shaderprimitive::MewShaderPrimitive>::Inner , )* )> for $struct {
89            fn from(inners: ( $( <$crate::shaderprimitive::$ty as $crate::shaderprimitive::MewShaderPrimitive>::Inner , )* )) -> Self {
90                Self::from_inners(inners)
91            }
92        }
93
94        impl AsRef<[u8; Self::NEXT_MULT]> for $struct {
95            fn as_ref(&self) -> &[u8; Self::NEXT_MULT] {
96                unsafe { ::std::mem::transmute(self) }
97            }
98        }
99        impl AsMut<[u8; Self::NEXT_MULT]> for $struct {
100            fn as_mut(&mut self) -> &mut [u8; Self::NEXT_MULT] {
101                unsafe { ::std::mem::transmute(self) }
102            }
103        }
104
105        impl Default for $struct {
106            fn default() -> Self {
107                Self::zeroed()
108            }
109        }
110    }
111}
112pub use shader_struct;
113
114
115pub trait MewShaderStruct {
116    type Bytes;
117    type Inners;
118    fn from_inners(inners: Self::Inners) -> Self;
119    fn from_bytes(bytes: Self::Bytes) -> Self;
120    fn zeroed() -> Self;
121}