Skip to content

Commit

Permalink
shader/execution: Fix struct padding for f16s
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
ben-clayton committed Mar 13, 2024
1 parent 0771e27 commit 392da47
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/webgpu/shader/execution/expression/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down

0 comments on commit 392da47

Please sign in to comment.