spirv_cross2/reflect/constants/
gfx_maths.rs

1#![cfg(feature = "gfx-math-types")]
2#![cfg_attr(docsrs, doc(cfg(feature = "gfx-math-types")))]
3use crate::reflect::ConstantValue;
4use crate::sealed::Sealed;
5use gfx_maths::{Mat4, Vec2, Vec3, Vec4};
6use crate::reflect::constants::impl_vec_constant;
7
8impl_vec_constant!(Vec2 [f32; 2] for [x, y]);
9impl_vec_constant!(Vec3 [f32; 3] for [x, y, z]);
10impl_vec_constant!(Vec4 [f32; 4] for [x, y, z, w]);
11
12impl Sealed for Mat4 {}
13impl ConstantValue for Mat4 {
14    const COLUMNS: usize = 4;
15    const VECSIZE: usize = 4;
16    type BaseArrayType = [f32; 4];
17    type ArrayType = [[f32; 4]; 4];
18    type BaseType = f32;
19
20    fn from_array(value: Self::ArrayType) -> Self {
21        value.into()
22    }
23
24    fn to_array(value: Self) -> Self::ArrayType {
25        let mut array = [[0f32; 4]; 4];
26        // gfx-math uses
27        // so we assign it back in the same order.
28
29        // const fn cr(c: usize, r: usize) -> usize {
30        //     r + c * 4
31        // }
32        array[0][0] = value[(0, 0)];
33        array[0][1] = value[(0, 1)];
34        array[0][2] = value[(0, 2)];
35        array[0][3] = value[(0, 3)];
36
37        array[1][0] = value[(1, 0)];
38        array[1][1] = value[(1, 1)];
39        array[1][2] = value[(1, 2)];
40        array[1][3] = value[(1, 3)];
41
42        array[2][0] = value[(2, 0)];
43        array[2][1] = value[(2, 1)];
44        array[2][2] = value[(2, 2)];
45        array[2][3] = value[(2, 3)];
46
47        array[3][0] = value[(3, 0)];
48        array[3][1] = value[(3, 1)];
49        array[3][2] = value[(3, 2)];
50        array[3][3] = value[(3, 3)];
51
52        array
53    }
54}
55
56#[cfg(test)]
57mod test {
58    use crate::reflect::ConstantValue;
59
60    #[test]
61    pub fn round_trip_mat4() {
62        let mat4 = gfx_maths::Mat4::inverse_orthographic_opengl(1.0, 2.0, 3.0, 4.0,5.0, 6.0);
63        let arr = ConstantValue::to_array(mat4.clone());
64        let returned = ConstantValue::from_array(arr);
65
66        assert_eq!(mat4, returned);
67    }
68}