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]
, likeglam
’s non-SIMD “scalar” vector types. VectorOrScalar::DIM == N
, since const equality is behind rustc featureassociated_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.