spirv_std/
vector.rs

1//! Traits related to vectors.
2
3use glam::{Vec3Swizzles, Vec4Swizzles};
4
5/// Abstract trait representing a SPIR-V vector type.
6///
7/// # Safety
8/// Implementing this trait on non-simd-vector types breaks assumptions of other unsafe code, and
9/// should not be done.
10pub unsafe trait Vector<T: crate::scalar::Scalar, const N: usize>: Default {}
11
12unsafe impl Vector<f32, 2> for glam::Vec2 {}
13unsafe impl Vector<f32, 3> for glam::Vec3 {}
14unsafe impl Vector<f32, 3> for glam::Vec3A {}
15unsafe impl Vector<f32, 4> for glam::Vec4 {}
16
17unsafe impl Vector<f64, 2> for glam::DVec2 {}
18unsafe impl Vector<f64, 3> for glam::DVec3 {}
19unsafe impl Vector<f64, 4> for glam::DVec4 {}
20
21unsafe impl Vector<u32, 2> for glam::UVec2 {}
22unsafe impl Vector<u32, 3> for glam::UVec3 {}
23unsafe impl Vector<u32, 4> for glam::UVec4 {}
24
25unsafe impl Vector<i32, 2> for glam::IVec2 {}
26unsafe impl Vector<i32, 3> for glam::IVec3 {}
27unsafe impl Vector<i32, 4> for glam::IVec4 {}
28
29/// Trait that implements slicing of a vector into a scalar or vector of lower dimensions, by
30/// ignoring the highter dimensions
31pub trait VectorTruncateInto<T> {
32    /// Slices the vector into a lower dimensional type by ignoring the higher components
33    fn truncate_into(self) -> T;
34}
35
36macro_rules! vec_trunc_impl {
37    ($a:ty, $b:ty, $self:ident $(.$($e:tt)*)?) => {
38        impl VectorTruncateInto<$a> for $b {
39            fn truncate_into($self) -> $a {
40                $self $(. $($e)*)?
41            }
42        }
43    };
44}
45macro_rules! vec_trunc_impls {
46    ($s:ty, $v2:ty, $v3:ty, $v4:ty) => {
47        vec_trunc_impl! {$s, $s, self}
48        vec_trunc_impl! {$s, $v2, self.x}
49        vec_trunc_impl! {$s, $v3, self.x}
50        vec_trunc_impl! {$s, $v4, self.x}
51
52        vec_trunc_impl! {$v2, $v2, self}
53        vec_trunc_impl! {$v2, $v3, self.xy()}
54        vec_trunc_impl! {$v2, $v4, self.xy()}
55
56        vec_trunc_impl! {$v3, $v3, self}
57        vec_trunc_impl! {$v3, $v4, self.xyz()}
58
59        vec_trunc_impl! {$v4, $v4, self}
60    };
61}
62
63vec_trunc_impls! { f32, glam::Vec2, glam::Vec3, glam::Vec4 }
64vec_trunc_impls! { f64, glam::DVec2, glam::DVec3, glam::DVec4 }
65vec_trunc_impls! { i32, glam::IVec2, glam::IVec3, glam::IVec4 }
66vec_trunc_impls! { u32, glam::UVec2, glam::UVec3, glam::UVec4 }