From 392da47f9ba72b78189ed0035d1aabf15dbc7187 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Wed, 13 Mar 2024 11:33:37 +0000 Subject: [PATCH] shader/execution: Fix struct padding for `f16`s `wgslMembers()` was using an `i32` to pad the structure to its alignment, however `i32` has an alignment and size of 4 bytes. If the structure holds an odd number of `f16` scalars, then `i32` cannot be used to account fo this padding due to the 4-byte size and alignment. Pad with `f16` if the padding is an odd multiple of 2 bytes. --- src/webgpu/shader/execution/expression/expression.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/webgpu/shader/execution/expression/expression.ts b/src/webgpu/shader/execution/expression/expression.ts index 0359e6e901a1..e86d068c6daa 100644 --- a/src/webgpu/shader/execution/expression/expression.ts +++ b/src/webgpu/shader/execution/expression/expression.ts @@ -158,7 +158,10 @@ function wgslMembers(members: Type[], source: InputSource, memberName: (i: numbe }); const padding = layout.stride - layout.size; if (padding > 0) { - lines.push(` @size(${padding}) padding : i32,`); + // Pad with a 'f16' if the padding requires an odd multiple of 2 bytes. + // This is required as 'i32' has an alignment and size of 4 bytes. + const ty = (padding & 2) !== 0 ? 'f16' : 'i32'; + lines.push(` @size(${padding}) padding : ${ty},`); } return lines.join('\n'); }