compute-toys / wgpu-compute-toy

Cross-platform compute shader engine
https://compute.toys
MIT License
119 stars 16 forks source link

Add matrix type aliases to prelude #1

Closed slerpyyy closed 1 year ago

slerpyyy commented 1 year ago

Adds the following type aliases to the prelude.

alias bool2 = vec2<bool>;
alias bool3 = vec3<bool>;
alias bool4 = vec4<bool>;
alias float2x2 = mat2x2<f32>;
alias float2x3 = mat2x3<f32>;
alias float2x4 = mat2x4<f32>;
alias float3x2 = mat3x2<f32>;
alias float3x3 = mat3x3<f32>;
alias float3x4 = mat3x4<f32>;
alias float4x2 = mat4x2<f32>;
alias float4x3 = mat4x3<f32>;
alias float4x4 = mat4x4<f32>;

Since vecN<T> requires T to be a concrete scalar, and matNxM<T> requires T to be a float, this new set of type aliases covers all possible use cases (with the exception of f16).

The downside is that some shaders already contain a subset of these aliases, meaning some shaders will break and need to be fixed manually. Though this should be a 5-second fix in most cases, and the impact of such a change would be a lot greater further down the line.

davidar commented 1 year ago

The reason we had avoided officially adding HLSL style matrix type aliases so far is that the dimensions are the opposite way around (WGSL is column-major like GLSL, unlike HLSL), so we weren't sure whether that would cause more confusion than it's worth? Obviously square matrices are fine, but then only having aliases for those would be odd.

We have a small system for fixing up shaders on the website whenever there's a breaking change, so that part should be fine.

CornuAmmonis commented 1 year ago

What do you think of just adding a comment in the prelude that says "these types are column major" for when people check the prelude? I know one of the first things I check when working with matrices in a new environment is whether they are row or column major.

slerpyyy commented 1 year ago

I'm glad the breaking change isn't an issue, I can amend that system later.

A comment about which way around matrices go is always welcome, though I think this should probably go into the explainer on the website rather than into the prelude itself?

davidar commented 1 year ago

Sounds good. Can you submit a PR for the fixup and explainer? Then I'll merge both at once