Skip to content

Commit afb3a7e

Browse files
committed
abi layout: improve documentation on Scalar and Vector
1 parent b7f069e commit afb3a7e

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

crates/spirv-std/src/scalar.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ use core::num::NonZeroUsize;
55

66
/// Abstract trait representing a SPIR-V scalar type.
77
///
8+
/// Implemented on types that map to spirv "scalar" types, which includes:
9+
/// * Floating-point type: f32, f64
10+
/// * Integer type: u8, u16, u32, u64, i8, i16, i32, i64
11+
/// * Boolean type: bool
12+
///
13+
/// See the SPIRV spec on [Types](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_types).
14+
///
815
/// # Safety
9-
/// Implementing this trait on non-scalar types breaks assumptions of other unsafe code, and should
10-
/// not be done.
16+
/// Must only be implemented on spirv "scalar" types, as mentioned above.
1117
pub unsafe trait Scalar: VectorOrScalar<Scalar = Self> + crate::sealed::Sealed {}
1218

1319
macro_rules! impl_scalar {

crates/spirv-std/src/vector.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
1211
pub 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.
3367
pub unsafe trait Vector<T: Scalar, const N: usize>: VectorOrScalar<Scalar = T> {}
3468

3569
macro_rules! impl_vector {

0 commit comments

Comments
 (0)