Replies: 2 comments
That's correct, but also note that you can look up this information authoritatively here: https://www.w3.org/TR/WGSL/#alignment-and-size
What I would recommend is that you should define a Rust struct that matches Also, either with this or without this, you can avoid explicit padding fields by using #[derive(Clone, Copy, Debug, bytemuck::NoUninit)]
#[repr(C)]
struct WgslMat3x3F32 {
data: [[f32; 4]; 3],
}
impl WgslMat3x3F32 {
pub fn new(mat: [[f32; 3]; 3]) -> Self {
Self {
data: mat.map(|[x, y, z]| [x, y, z, 0.0])
}
}
} |
Uh oh!
There was an error while loading. Please reload this page.
I was trying to pass a model transformation matrix along with a normal transformation matrix (the upper-left 3x3 of the model transformation matrix) as a uniform. The WGSL looks like this:
However, mapping that to a Rust structure with
#[repr(C)]proved difficult. After some trial & error, it seems like the proper alignment of amat3x3<f32>is as threevec3<f32>s, with 4 bytes of padding after each one, since avec3<f32>needs to be aligned on 16-byte boundaries. So my Rust struct winds up looking like this:This is obviously tedious to update, as I need to split the normal matrix up into three columns and set each one individually:
Is the proper way to do this just to generate the 3x3 in the shader, e.g.:
I saw that done in a couple of examples. That's also kind of annoying, though perhaps less annoying than manually padding out the 3x3 struct in Rust.
All reactions