Getting error that array stride is too small when it seems to be correct #2049
-
Perhaps I'm just missing something, but with the following WGSL: struct Cell {
pos : vec2<f32>;
atlas_bounds: vec4<f32>;
hlid : u32;
};
[[block]]
struct Cells {
cells : [[stride(28)]] array<Cell>;
};
[[group(0), binding(1)]]
var<storage, read> cells : Cells; I keep getting the following validation error:
However, this doesn't really make sense to me, as my calculations are:
So why might it be giving me that error? Is there anything in the Rust code side that could be causing this? If I set it to Also I can share relevant parts of the Rust rendering code if necessary, just let me know. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
So this issue is about alignment. The notable bits are that
So your total struct size is actually 48, and your cpu side data needs to take into account that padding too. Your cpu side code should look like struct Cell {
pos: Vec2,
_pad1: [u8; 8]
atlas_bounds: Vec4,
hlid: u32,
_pad2: [u8; 12]
} If you reorder your struct to be: struct Cell {
atlas_bounds: vec4<f32>;
pos : vec2<f32>;
hlid : u32;
}; There is actually only 4 bytes of padding total (at the very end) and your struct size is 32. You might be interested in the https://docs.rs/crevice/ crate. I hope this helps. |
Beta Was this translation helpful? Give feedback.
So this issue is about alignment. The notable bits are that
vec4<f32>
is size 16, align 16 andvec2<f32>
is size 8 align 8.0
:vec2<f32>
->align_up(0, 8) + 2 * 4
->0 + 2 * 4
=8
= 0 bytes of padding, 8 bytes of data8
:vec4<f32>
->align_up(8, 16) + 4 * 4
->16 + 4 * 4
=32
= 8 bytes of padding, 16 bytes of data32
:u32
->align_up(32, 4) + 1 * 4
->32 + 1 * 4
=36
= 0 bytes of padding, 4 bytes of data.36
: Struct is over, but our largest member is align 16, so we're align 16 too =align_up(36, 16)
=48
= 12 bytes of padding.So your total struct size is actually 48, and your cpu side data needs to take into account that padding too. Your cpu side code should look like