1pub mod primitives {
2 use std::ops::{ Deref, DerefMut, };
3
4 macro_rules! deref_align {
5 {
6 $struct:ident $inner:tt
7 } => {
8 #[derive(Debug, Default, Copy, Clone)]
9 #[repr(C)]
10 pub struct $struct($inner);
11 deref_align!{ @impl $struct $inner }
12 };
13 {
14 $struct:ident $inner:tt => $align:literal
15 } => {
16 #[derive(Debug, Default, Copy, Clone)]
17 #[repr(align($align))]
18 pub struct $struct($inner);
19 deref_align!{ @impl $struct $inner }
20 };
21 {
22 @impl $struct:ident [ $inner:ty ; $len:literal ]
23 } => {
24 impl<T: Into<[$inner; $len]>> From<T> for $struct {
33 fn from(inner: T) -> Self {
34 Self(inner.into())
35 }
36 }
37 impl Deref for $struct {
38 type Target = [$inner; $len];
39 fn deref(&self) -> &Self::Target {
40 &self.0
41 }
42 }
43 impl DerefMut for $struct {
44 fn deref_mut(&mut self) -> &mut Self::Target {
45 &mut self.0
46 }
47 }
48 impl AsRef<[$inner; $len]> for $struct {
49 fn as_ref(&self) -> &[$inner; $len] {
50 &*self
51 }
52 }
53 impl AsMut<[$inner; $len]> for $struct {
54 fn as_mut(&mut self) -> &mut [$inner; $len] {
55 &mut *self
56 }
57 }
58 };
59 }
60
61 deref_align!{ vec2i [i32; 2] => 8 }
62 deref_align!{ vec2u [u32; 2] => 8 }
63 deref_align!{ vec2f [f32; 2] => 8 }
64
65 deref_align!{ vec3i [i32; 3] => 16 }
66 deref_align!{ vec3u [u32; 3] => 16 }
67 deref_align!{ vec3f [f32; 3] => 16 }
68
69 deref_align!{ vec4i [i32; 4] => 16 }
70 deref_align!{ vec4u [u32; 4] => 16 }
71 deref_align!{ vec4f [f32; 4] => 16 }
72
73
74 deref_align!{ mat2x2i [vec2i; 2] }
75 deref_align!{ mat2x2u [vec2u; 2] }
76 deref_align!{ mat2x2f [vec2f; 2] }
77
78 deref_align!{ mat3x2i [vec2i; 3] }
79 deref_align!{ mat3x2u [vec2u; 3] }
80 deref_align!{ mat3x2f [vec2f; 3] }
81
82 deref_align!{ mat4x2i [vec2i; 4] }
83 deref_align!{ mat4x2u [vec2u; 4] }
84 deref_align!{ mat4x2f [vec2f; 4] }
85
86
87 deref_align!{ mat2x3i [vec3i; 2] }
88 deref_align!{ mat2x3u [vec3u; 2] }
89 deref_align!{ mat2x3f [vec3f; 2] }
90
91 deref_align!{ mat3x3i [vec3i; 3] }
92 deref_align!{ mat3x3u [vec3u; 3] }
93 deref_align!{ mat3x3f [vec3f; 3] }
94
95 deref_align!{ mat4x3i [vec3i; 4] }
96 deref_align!{ mat4x3u [vec3u; 4] }
97 deref_align!{ mat4x3f [vec3f; 4] }
98
99
100 deref_align!{ mat2x4i [vec4i; 2] }
101 deref_align!{ mat2x4u [vec4u; 2] }
102 deref_align!{ mat2x4f [vec4f; 2] }
103
104 deref_align!{ mat3x4i [vec4i; 3] }
105 deref_align!{ mat3x4u [vec4u; 3] }
106 deref_align!{ mat3x4f [vec4f; 3] }
107
108 deref_align!{ mat4x4i [vec4i; 4] }
109 deref_align!{ mat4x4u [vec4u; 4] }
110 deref_align!{ mat4x4f [vec4f; 4] }
111}
112
113
114#[macro_export]
115macro_rules! shader_struct2 {
116 { $struct:ident [
117 $( $num:tt $field:ident => $ty:ty ),* $(,)?
118 ] } => {
119 use $crate::primitives::*;
120
121 #[derive(Debug, Default, Copy, Clone)]
122 #[repr(C)]
123 pub struct $struct {
124 $( $field : $ty , )*
125 }
126
127 impl $struct {
128 pub fn new() -> Self {
129 Self::default()
130 }
131
132 pub fn as_bytes(&self) -> &[u8; ::std::mem::size_of::<Self>()] {
133 unsafe { ::std::mem::transmute(self) }
134 }
135 }
136 };
137}