spirv_cross2/reflect/constants/
glam.rs

1#![cfg(feature = "glam-types")]
2#![cfg_attr(docsrs, doc(cfg(feature = "glam-types")))]
3use glam::*;
4use crate::reflect::constants::impl_vec_constant;
5
6impl_vec_constant!(Vec2 [f32; 2] for [x, y]);
7impl_vec_constant!(Vec3 [f32; 3] for [x, y, z]);
8impl_vec_constant!(Vec3A [f32; 3] for [x, y, z]);
9
10impl_vec_constant!(Vec4 [f32; 4] for [x, y, z, w]);
11
12impl_vec_constant!(DVec2 [f64; 2] for [x, y]);
13impl_vec_constant!(DVec3 [f64; 3] for [x, y, z]);
14impl_vec_constant!(DVec4 [f64; 4] for [x, y, z, w]);
15
16impl_vec_constant!(IVec2 [i32; 2] for [x, y]);
17impl_vec_constant!(IVec3 [i32; 3] for [x, y, z]);
18impl_vec_constant!(IVec4 [i32; 4] for [x, y, z, w]);
19
20impl_vec_constant!(BVec2 [bool; 2] for [x, y]);
21impl_vec_constant!(BVec3 [bool; 3] for [x, y, z]);
22impl_vec_constant!(BVec4 [bool; 4] for [x, y, z, w]);
23
24impl_vec_constant!(UVec2 [u32; 2] for [x, y]);
25impl_vec_constant!(UVec3 [u32; 3] for [x, y, z]);
26impl_vec_constant!(UVec4 [u32; 4] for [x, y, z, w]);
27
28impl_vec_constant!(I16Vec2 [i16; 2] for [x, y]);
29impl_vec_constant!(I16Vec3 [i16; 3] for [x, y, z]);
30impl_vec_constant!(I16Vec4 [i16; 4] for [x, y, z, w]);
31
32impl_vec_constant!(U16Vec2 [u16; 2] for [x, y]);
33impl_vec_constant!(U16Vec3 [u16; 3] for [x, y, z]);
34impl_vec_constant!(U16Vec4 [u16; 4] for [x, y, z, w]);
35
36impl_vec_constant!(I64Vec2 [i64; 2] for [x, y]);
37impl_vec_constant!(I64Vec3 [i64; 3] for [x, y, z]);
38impl_vec_constant!(I64Vec4 [i64; 4] for [x, y, z, w]);
39
40impl_vec_constant!(U64Vec2 [u64; 2] for [x, y]);
41impl_vec_constant!(U64Vec3 [u64; 3] for [x, y, z]);
42impl_vec_constant!(U64Vec4 [u64; 4] for [x, y, z, w]);
43
44macro_rules! impl_mat_constant {
45     ($mat_ty:ty [$base_ty:ty; $len:literal] for [$vec_ty:ty; $($component:literal),*])  => {
46         impl $crate::sealed::Sealed for $mat_ty {}
47         impl $crate::reflect::ConstantValue for $mat_ty {
48             const COLUMNS: usize = $len;
49             const VECSIZE: usize = $len;
50             type BaseArrayType = [$base_ty; $len];
51             type ArrayType = [[$base_ty; $len]; $len];
52             type BaseType = $base_ty;
53
54             fn to_array(value: Self) -> Self::ArrayType {
55                 value.to_cols_array_2d()
56             }
57
58             fn from_array(value: Self::ArrayType) -> Self {
59                <$mat_ty>::from_cols(
60                    $(<$vec_ty>::from_array(value[$component])),*
61                )
62             }
63         }
64     };
65}
66
67impl_mat_constant!(Mat4 [f32; 4] for [Vec4; 0, 1, 2, 3]);
68impl_mat_constant!(Mat3 [f32; 3] for [Vec3; 0, 1, 2]);
69impl_mat_constant!(Mat3A [f32; 3] for [Vec3A; 0, 1, 2]);
70impl_mat_constant!(Mat2 [f32; 2] for [Vec2; 0, 1]);
71
72impl_mat_constant!(DMat4 [f64; 4] for [DVec4; 0, 1, 2, 3]);
73impl_mat_constant!(DMat3 [f64; 3] for [DVec3; 0, 1, 2]);
74impl_mat_constant!(DMat2 [f64; 2] for [DVec2; 0, 1]);
75
76#[cfg(test)]
77mod test {
78    use crate::reflect::ConstantValue;
79
80    #[test]
81    pub fn round_trip_mat4() {
82        let mat4 = glam::Mat4::orthographic_lh(1.0, 2.0, 3.0, 4.0,5.0, 6.0);
83        let arr = ConstantValue::to_array(mat4.clone());
84        let returned = ConstantValue::from_array(arr);
85
86        assert_eq!(mat4, returned);
87    }
88
89    #[test]
90    pub fn round_trip_mat3() {
91        let mat4 = glam::Mat4::orthographic_lh(1.0, 2.0, 3.0, 4.0,5.0, 6.0);
92        let mat3 = glam::Mat3::from_mat4_minor(mat4, 1, 2);
93        let arr = ConstantValue::to_array(mat3.clone());
94        let returned = ConstantValue::from_array(arr);
95
96        assert_eq!(mat3, returned);
97    }
98
99    #[test]
100    pub fn round_trip_mat2() {
101        let mat4 = glam::Mat4::orthographic_lh(1.0, 2.0, 3.0, 4.0,5.0, 6.0);
102        let mat3 = glam::Mat3::from_mat4_minor(mat4, 1, 2);
103        let mat2 = glam::Mat2::from_mat3_minor(mat3, 1, 2);
104        let arr = ConstantValue::to_array(mat2.clone());
105        let returned = ConstantValue::from_array(arr);
106
107        assert_eq!(mat2, returned);
108    }
109}