Trait Vector

Source
pub unsafe trait Vector<T: Scalar, const N: usize>: ScalarOrVector<Scalar = T> { }
Expand description

Abstract trait representing a SPIR-V vector type.

To implement this trait, your struct must be marked with:

#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]

This places these additional constraints on your type, checked by the spirv codegen:

  • must be a struct
  • members must be a spirv Scalar type, which includes:
    • Floating-point type: f32, f64
    • Integer type: u8, u16, u32, u64, i8, i16, i32, i64
    • Boolean type: bool
  • all members must be of the same primitive type
  • must have 2, 3 or 4 vector components / members
  • type must derive Copy, Clone, Default

The spirv codegen backend will then emit your type as an OpTypeVector instead of an OpTypeStruct. The layout of your type is unaffected, the size, alignment and member offsets will follow standard rustc layout rules. This hint does nothing on other target platforms.

See the SPIRV spec on Types and the “Data rules” in the Universal Validation Rules.

§Example

#[derive(Copy, Clone, Default)]
#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
struct MyColor {
    r: f32,
    b: f32,
    g: f32,
}

§Safety

  • Must only be implemented on types that the spirv codegen emits as valid OpTypeVector. This includes all structs marked with #[rust_gpu::vector::v1], like glam’s non-SIMD “scalar” vector types.
  • VectorOrScalar::DIM == N, since const equality is behind rustc feature associated_const_equality

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Vector<bool, 2> for BVec2

Source§

impl Vector<bool, 3> for BVec3

Source§

impl Vector<bool, 4> for BVec4

Source§

impl Vector<f32, 2> for Vec2

Source§

impl Vector<f32, 3> for Vec3A

Source§

impl Vector<f32, 3> for Vec3

Source§

impl Vector<f32, 4> for Vec4

Source§

impl Vector<f64, 2> for DVec2

Source§

impl Vector<f64, 3> for DVec3

Source§

impl Vector<f64, 4> for DVec4

Source§

impl Vector<i32, 2> for IVec2

Source§

impl Vector<i32, 3> for IVec3

Source§

impl Vector<i32, 4> for IVec4

Source§

impl Vector<u32, 2> for UVec2

Source§

impl Vector<u32, 3> for UVec3

Source§

impl Vector<u32, 4> for UVec4

Implementors§