Skip to content

Conversation

@Firestar99
Copy link
Member

@Firestar99 Firestar99 commented Oct 9, 2025

  • impl Debug and Display
  • document limitations
  • verify abi layout in difftest (the only part that requires #380)

Base automatically changed from vec3-12-bytes to main October 15, 2025 10:09
/// 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit too inward facing. Just state the limitations. I guess you can optionally ask for a task.

pub y: Vec3A,
pub z: Vec3A,
pub w: Vec3A,
pub x_axis: Vec3A,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this change? Is it more or less consistent with other APIs?

Copy link
Member Author

@Firestar99 Firestar99 Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More consistent, now the member names are the same of glam's matrices. Split it out into a separate commit to make it easier to reason about

impl Matrix4x3 {
/// Convert from glam's [`Affine3A`]
pub fn from_affine3a(affine: Affine3A) -> Self {
Self {
x_axis: affine.x_axis,
y_axis: affine.y_axis,
z_axis: affine.z_axis,
w_axis: affine.w_axis,
}
}

@Firestar99 Firestar99 marked this pull request as ready for review October 16, 2025 15:45
@LegNeato LegNeato added this pull request to the merge queue Nov 26, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 26, 2025
@Firestar99
Copy link
Member Author

Firestar99 commented Nov 26, 2025

What? Never seen a DXC (the DirectX 12 shader compiler) error, could be some kind of wgpu bug or an env change due to github updating the runners? Just gonna retry seems like other PRs are failing as well...

wgpu error: Validation Error

Caused by:
  In Device::create_compute_pipeline, label = 'Compute Pipeline'
    Internal error: DXC compile error: Compute Shader:138:14: error: expected identifier
            [8][8] _value3[2][8][8] = _e378;
             ^
Compute Shader:138:17: error: expected identifier
            [8][8] _value3[2][8][8] = _e378;
                ^
Compute Shader:138:20: error: use of undeclared identifier '_value3'
            [8][8] _value3[2][8][8] = _e378;

@Firestar99 Firestar99 added this pull request to the merge queue Nov 26, 2025
@Firestar99 Firestar99 removed this pull request from the merge queue due to a manual request Nov 26, 2025
/// # 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`],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my education - why don't we use one of these types instead? Or wrap one?

Copy link
Member Author

@Firestar99 Firestar99 Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly effort vs value.

Whereas glam vectors are OpTypeVector types, Matrices are not OpTypeMatrix but regular OpTypeStruct with regular members, aka. N times VecN members. There's only very few operations in SPIR-V that actually accept an OpTypeMatrix: OpTranspose, OpMatrixTimesVector, OpVectorTimesMatrix, OpMarixTimesMatrix and OpOuterProduct. Instead of using those, we just emit regular code based on the glam implementations of the equivalent instructions, which is also what most hardware would do, so very low value in getting those to work. (AI workloads use CooperativeMatrix, an entirely different system.)

To get glam Matrices to be actual OpTypeMatrices, they'd need a very similar refactor to Vectors in #380 first. Cause their size and alignment, as vectors previously, is not defined by the rustc layouter but by whatever "intrinsic values" we define in the codegen, so you'd similarly get different sizes and alignments between GPU and CPU. And then they'd still be useless, since we'd need to wrap all the glam functions for matrices to emit the SPIR-V intrinsics instead of regular instructions, requiring another breakage in glam compatability. That's a lot of effort.

The raytracing extensions (ray query and ray pipeline) require a 4x3 Matrix, which doesn't exist directly in glam. It is semantically equivalent to an Affine3A except in layout, since that type is (Mat3A, Vec3A) but we need [Vec3; 4] (or Vec3A), so we need a custom type anyway. All that for literally two intrinsics, which I've never used since they are ray query and I've only played with ray pipelines (outside of rust-gpu):

  • get_candidate_intersection_object_to_world
  • get_committed_intersection_object_to_world

Prior to #391, these intrinsics "created" the Matrix type within their asm! block, extracted the vectors and returned either a [Vec3; 4] or [Vec3A; 4]. That got changed to this matrix type not just because it's more convenient, but also in preparation for #392, which disallows declaring OpTypeVector in asm! blocks, but we need those to declare the OpTypeMatrix. And that PR was a requirement for #380, the vector layout refactor, since there's no way to declare whether an asm OpTypeVector %f32 3 would be a Vec3 or a Vec3A. Instead the type is "imported" into the asm blocks via type inference (_) or an explicit typeof{type} / typeof*{type}, if necessary.

On top of that, there's very few people actually using them, at least I don't know of anyone currently. We still don't want to break ray tracing but it's not exactly a priority. So the minimal effort solution was to not bother touching SpirvType::Matrix at all, and just create a new type that works for those intrinsics. And explicitly warn people not to put them into any buffers, instead to convert them from / to glam types and read / write those.

@Firestar99 Firestar99 added this pull request to the merge queue Nov 27, 2025
Merged via the queue into main with commit a496aea Nov 27, 2025
13 checks passed
@Firestar99 Firestar99 deleted the matrix4x3_v2 branch November 27, 2025 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants