From 169005c8287739100ef5e132e54a09313f8af198 Mon Sep 17 00:00:00 2001 From: firestar99 Date: Thu, 16 Oct 2025 16:39:08 +0200 Subject: [PATCH 1/4] Matrix4x3: rename members to be more consistent with glam's matrices --- crates/spirv-std/src/matrix.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/spirv-std/src/matrix.rs b/crates/spirv-std/src/matrix.rs index 1e6e7ca79d..81a04fbbd5 100644 --- a/crates/spirv-std/src/matrix.rs +++ b/crates/spirv-std/src/matrix.rs @@ -10,10 +10,10 @@ use glam::{Affine3A, Mat3, Mat3A, Mat4, Vec3, Vec3A}; #[spirv(matrix)] #[allow(missing_docs)] pub struct Matrix4x3 { - pub x: Vec3A, - pub y: Vec3A, - pub z: Vec3A, - pub w: Vec3A, + pub x_axis: Vec3A, + pub y_axis: Vec3A, + pub z_axis: Vec3A, + pub w_axis: Vec3A, } /// The `from_*` fn signatures should match [`Affine3A`], to make it easier to switch to [`Affine3A`] later. @@ -22,10 +22,10 @@ impl Matrix4x3 { /// Convert from glam's [`Affine3A`] pub fn from_affine3a(affine: Affine3A) -> Self { Self { - x: affine.x_axis, - y: affine.y_axis, - z: affine.z_axis, - w: affine.w_axis, + x_axis: affine.x_axis, + y_axis: affine.y_axis, + z_axis: affine.z_axis, + w_axis: affine.w_axis, } } @@ -53,11 +53,11 @@ impl Matrix4x3 { pub fn to_affine3a(self) -> Affine3A { Affine3A { matrix3: Mat3A { - x_axis: self.x, - y_axis: self.y, - z_axis: self.z, + x_axis: self.x_axis, + y_axis: self.y_axis, + z_axis: self.z_axis, }, - translation: self.w, + translation: self.w_axis, } } From 680317c271e413edb4a1968d6fb69416021a53ad Mon Sep 17 00:00:00 2001 From: firestar99 Date: Thu, 16 Oct 2025 16:40:03 +0200 Subject: [PATCH 2/4] Matrix4x3: impl Debug and Display like a `Mat4` --- crates/spirv-std/src/matrix.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/spirv-std/src/matrix.rs b/crates/spirv-std/src/matrix.rs index 81a04fbbd5..6f19c073e5 100644 --- a/crates/spirv-std/src/matrix.rs +++ b/crates/spirv-std/src/matrix.rs @@ -1,5 +1,6 @@ //! a set of common SPIR-V Matrices, used for intrinsics +use core::fmt::{Debug, Display, Formatter}; use glam::{Affine3A, Mat3, Mat3A, Mat4, Vec3, Vec3A}; /// A Matrix with 4 columns of [`Vec3`], very similar to glam's [`Affine3A`]. @@ -76,3 +77,15 @@ impl Matrix4x3 { Mat4::from(self.to_affine3a()) } } + +impl Debug for Matrix4x3 { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + Debug::fmt(&self.to_mat4(), f) + } +} + +impl Display for Matrix4x3 { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + Display::fmt(&self.to_mat4(), f) + } +} From 6b699a5f63a459a90e4ecc6789fab74e14aeae7e Mon Sep 17 00:00:00 2001 From: firestar99 Date: Thu, 9 Oct 2025 14:52:13 +0200 Subject: [PATCH 3/4] Matrix4x3: document limitations of `#[spirv(matrix)]` --- crates/spirv-std/src/matrix.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/spirv-std/src/matrix.rs b/crates/spirv-std/src/matrix.rs index 6f19c073e5..1ae2f52d78 100644 --- a/crates/spirv-std/src/matrix.rs +++ b/crates/spirv-std/src/matrix.rs @@ -6,6 +6,13 @@ use glam::{Affine3A, Mat3, Mat3A, Mat4, Vec3, Vec3A}; /// A Matrix with 4 columns of [`Vec3`], very similar to glam's [`Affine3A`]. /// /// Primarily used in ray tracing extensions to represent object rotation, scale and translation. +/// +/// # Limitations +/// These Limitations apply to all structs marked with `#[spirv(matrix)]`, which `Matrix4x3` is the only one in +/// `spirv-std`: +/// * Cannot be used within buffers, push constants or anything that requires an "explicit layout". Use [`Affine3A`], +/// [`Mat4`] or the combination of [`Mat3`] with [`Vec3`] instead and convert them to `Matrix4x3` in the shader. +/// * There may be other situations where this type may surprisingly fail! #[derive(Clone, Copy, Default, PartialEq)] #[repr(C)] #[spirv(matrix)] @@ -18,7 +25,7 @@ pub struct Matrix4x3 { } /// The `from_*` fn signatures should match [`Affine3A`], to make it easier to switch to [`Affine3A`] later. -/// The `to_*` fn signatures are custom +/// The `to_*` fn signatures are custom. impl Matrix4x3 { /// Convert from glam's [`Affine3A`] pub fn from_affine3a(affine: Affine3A) -> Self { From 951a990aad18c7355810a6f696395739bf377304 Mon Sep 17 00:00:00 2001 From: firestar99 Date: Thu, 9 Oct 2025 14:53:10 +0200 Subject: [PATCH 4/4] Matrix4x3: verify abi layout in difftest --- tests/difftests/tests/lang/abi/vector_layout/cpu/src/layout.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/difftests/tests/lang/abi/vector_layout/cpu/src/layout.rs b/tests/difftests/tests/lang/abi/vector_layout/cpu/src/layout.rs index c9b660f63b..88b6774ea3 100644 --- a/tests/difftests/tests/lang/abi/vector_layout/cpu/src/layout.rs +++ b/tests/difftests/tests/lang/abi/vector_layout/cpu/src/layout.rs @@ -2,6 +2,7 @@ use core::mem::offset_of; use core::ops::Range; use experiments::*; use spirv_std::glam::*; +use spirv_std::matrix::Matrix4x3; pub struct BumpAlloc(usize); @@ -61,6 +62,7 @@ pub fn eval_layouts(gid: u32, out: &mut [u32]) { 0x11 => write_layout!(out, offset, Struct0x11(a, b)), 0x12 => write_layout!(out, offset, Struct0x12(a, b, c, d, e)), 0x13 => write_layout!(out, offset, Struct0x13(a)), + 0x14 => write_layout!(out, offset, Matrix4x3(x_axis, y_axis, z_axis, w_axis)), // mat 0x20 => write_layout!(out, offset, Mat2()), // private members