luminance/shader/
types.rs

1//! Shader type wrappers.
2//!
3//! These types are used, mostly, to be passed to shaders as [`Uniform`] data.
4//!
5//! [`Uniform`]: crate::shader::Uniform
6
7use std::ops::{Deref, DerefMut};
8
9/// An array of values.
10///
11/// The array length is indexed at compile time with `N`.
12#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
13pub struct Arr<T, const N: usize>(pub [T; N]);
14
15impl<T, const N: usize> From<[T; N]> for Arr<T, N> {
16  fn from(a: [T; N]) -> Self {
17    Arr(a)
18  }
19}
20
21impl<T, const N: usize> From<Arr<T, N>> for [T; N] {
22  fn from(Arr(a): Arr<T, N>) -> Self {
23    a
24  }
25}
26
27impl<T, const N: usize> AsRef<[T; N]> for Arr<T, N> {
28  fn as_ref(&self) -> &[T; N] {
29    &self.0
30  }
31}
32
33impl<T, const N: usize> Deref for Arr<T, N> {
34  type Target = [T; N];
35
36  fn deref(&self) -> &Self::Target {
37    &self.0
38  }
39}
40
41impl<T, const N: usize> DerefMut for Arr<T, N> {
42  fn deref_mut(&mut self) -> &mut Self::Target {
43    &mut self.0
44  }
45}
46
47impl<T, const N: usize> Arr<T, N> {
48  /// Create a new array.
49  pub const fn new(arr: [T; N]) -> Self {
50    Self(arr)
51  }
52}
53
54/// A 2 dimensional vector.
55///
56/// This is akin to a `[T; 2]`.
57#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
58pub struct Vec2<T>(pub [T; 2]);
59
60impl<T> From<[T; 2]> for Vec2<T> {
61  fn from(a: [T; 2]) -> Self {
62    Vec2(a)
63  }
64}
65
66impl<T> From<Vec2<T>> for [T; 2] {
67  fn from(Vec2(a): Vec2<T>) -> Self {
68    a
69  }
70}
71
72impl<T> AsRef<[T; 2]> for Vec2<T> {
73  fn as_ref(&self) -> &[T; 2] {
74    &self.0
75  }
76}
77
78impl<T> Deref for Vec2<T> {
79  type Target = [T; 2];
80
81  fn deref(&self) -> &Self::Target {
82    &self.0
83  }
84}
85
86impl<T> DerefMut for Vec2<T> {
87  fn deref_mut(&mut self) -> &mut Self::Target {
88    &mut self.0
89  }
90}
91
92impl<T> Vec2<T> {
93  /// Create a new vector.
94  pub const fn new(x: T, y: T) -> Self {
95    Self([x, y])
96  }
97}
98
99/// A 3 dimensional vector.
100///
101/// This is akin to a `[T; 3]`.
102#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
103pub struct Vec3<T>(pub [T; 3]);
104
105impl<T> From<[T; 3]> for Vec3<T> {
106  fn from(a: [T; 3]) -> Self {
107    Vec3(a)
108  }
109}
110
111impl<T> From<Vec3<T>> for [T; 3] {
112  fn from(Vec3(a): Vec3<T>) -> Self {
113    a
114  }
115}
116
117impl<T> AsRef<[T; 3]> for Vec3<T> {
118  fn as_ref(&self) -> &[T; 3] {
119    &self.0
120  }
121}
122
123impl<T> Deref for Vec3<T> {
124  type Target = [T; 3];
125
126  fn deref(&self) -> &Self::Target {
127    &self.0
128  }
129}
130
131impl<T> DerefMut for Vec3<T> {
132  fn deref_mut(&mut self) -> &mut Self::Target {
133    &mut self.0
134  }
135}
136
137impl<T> Vec3<T> {
138  /// Create a new vector.
139  pub const fn new(x: T, y: T, z: T) -> Self {
140    Self([x, y, z])
141  }
142}
143
144/// A 4 dimensional vector.
145///
146/// This is akin to a `[T; 4]`.
147#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
148pub struct Vec4<T>(pub [T; 4]);
149
150impl<T> From<[T; 4]> for Vec4<T> {
151  fn from(a: [T; 4]) -> Self {
152    Vec4(a)
153  }
154}
155
156impl<T> From<Vec4<T>> for [T; 4] {
157  fn from(Vec4(a): Vec4<T>) -> Self {
158    a
159  }
160}
161
162impl<T> AsRef<[T; 4]> for Vec4<T> {
163  fn as_ref(&self) -> &[T; 4] {
164    &self.0
165  }
166}
167
168impl<T> Deref for Vec4<T> {
169  type Target = [T; 4];
170
171  fn deref(&self) -> &Self::Target {
172    &self.0
173  }
174}
175
176impl<T> DerefMut for Vec4<T> {
177  fn deref_mut(&mut self) -> &mut Self::Target {
178    &mut self.0
179  }
180}
181
182impl<T> Vec4<T> {
183  /// Create a new vector.
184  pub const fn new(x: T, y: T, z: T, w: T) -> Self {
185    Self([x, y, z, w])
186  }
187}
188
189macro_rules! matrix {
190  ($t:ident, $r:literal, $c:literal) => {
191    #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
192    /// Matrix N×M.
193    pub struct $t<T>(pub [[T; $c]; $r]);
194
195    impl<T> From<[[T; $c]; $r]> for $t<T> {
196      fn from(a: [[T; $c]; $r]) -> Self {
197        $t(a)
198      }
199    }
200
201    impl<T> From<$t<T>> for [[T; $c]; $r] {
202      fn from($t(a): $t<T>) -> Self {
203        a
204      }
205    }
206
207    impl<T> AsRef<[[T; $c]; $r]> for $t<T> {
208      fn as_ref(&self) -> &[[T; $c]; $r] {
209        &self.0
210      }
211    }
212
213    impl<T> Deref for $t<T> {
214      type Target = [[T; $c]; $r];
215
216      fn deref(&self) -> &Self::Target {
217        &self.0
218      }
219    }
220
221    impl<T> DerefMut for $t<T> {
222      fn deref_mut(&mut self) -> &mut Self::Target {
223        &mut self.0
224      }
225    }
226
227    impl<T> $t<T> {
228      /// Create a matrix via its array representation.
229      pub fn new(array: impl Into<[[T; $c]; $r]>) -> Self {
230        $t(array.into())
231      }
232    }
233  };
234}
235
236matrix!(Mat22, 2, 2);
237matrix!(Mat33, 3, 3);
238matrix!(Mat44, 4, 4);