@@ -7,8 +7,7 @@ use glam::{Vec3Swizzles, Vec4Swizzles};
77/// Abstract trait representing either a vector or a scalar type.
88///
99/// # Safety
10- /// Implementing this trait on non-scalar or non-vector types may break assumptions about other
11- /// unsafe code, and should not be done.
10+ /// Your type must also implement [`Vector`] or [`Scalar`], see their safety sections as well.
1211pub unsafe trait VectorOrScalar : Copy + Default + Send + Sync + ' static {
1312 /// Either the scalar component type of the vector or the scalar itself.
1413 type Scalar : Scalar ;
@@ -27,9 +26,44 @@ pub(crate) const fn create_dim(n: usize) -> NonZeroUsize {
2726
2827/// Abstract trait representing a SPIR-V vector type.
2928///
29+ /// To implement this trait, your struct must be marked with:
30+ /// ```no_run
31+ /// #[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
32+ /// # struct Bla(f32, f32);
33+ /// ```
34+ ///
35+ /// This places these additional constraints on your type, checked by the spirv codegen:
36+ /// * must be a struct
37+ /// * members must be a spirv [`Scalar`] type, which includes:
38+ /// * Floating-point type: f32, f64
39+ /// * Integer type: u8, u16, u32, u64, i8, i16, i32, i64
40+ /// * Boolean type: bool
41+ /// * all members must be of the same primitive type
42+ /// * must have 2, 3 or 4 vector components / members
43+ /// * type must derive Copy, Clone, Default
44+ ///
45+ /// The spirv codegen backend will then emit your type as an `OpTypeVector` instead of an `OpTypeStruct`. The layout of
46+ /// your type is unaffected, the size, alignment and member offsets will follow standard rustc layout rules. This hint
47+ /// does nothing on other target platforms.
48+ ///
49+ /// See the SPIRV spec on [Types](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_types) and the
50+ /// "Data rules" in the [Universal Validation Rules](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_universal_validation_rules).
51+ ///
52+ /// # Example
53+ /// ```no_run
54+ /// #[derive(Copy, Clone, Default)]
55+ /// #[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
56+ /// struct MyColor {
57+ /// r: f32,
58+ /// b: f32,
59+ /// g: f32,
60+ /// }
61+ /// ```
62+ ///
63+ ///
3064/// # Safety
31- /// Implementing this trait on non-simd-vector types breaks assumptions of other unsafe code, and
32- /// should not be done .
65+ /// Must only be implemented on types that the spirv codegen emits as valid `OpTypeVector`. This includes all structs
66+ /// marked with `#[rust_gpu::vector::v1]`, like [`glam`]'s non-SIMD "scalar" vector types .
3367pub unsafe trait Vector < T : Scalar , const N : usize > : VectorOrScalar < Scalar = T > { }
3468
3569macro_rules! impl_vector {
0 commit comments