impr: Warn when schema is not uniform aligned - #2750
Conversation
|
pkg.pr.new packages benchmark commit |
Resolution Time Benchmark---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Random Branching (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.82, 1.63, 3.56, 5.67, 6.56, 10.34, 19.29, 20.72]
line [0.79, 1.64, 3.59, 5.41, 7.19, 9.84, 19.40, 21.42]
line [0.85, 1.64, 3.74, 5.92, 7.20, 11.96, 20.39, 23.71]
---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Linear Recursion (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.27, 0.42, 0.59, 0.76, 1.01, 1.04, 1.21, 1.35]
line [0.29, 0.79, 0.70, 0.70, 0.99, 1.01, 1.18, 1.27]
line [0.28, 0.49, 0.60, 0.72, 0.99, 1.05, 1.23, 1.38]
---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Full Tree (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.76, 1.88, 4.09, 6.20, 11.69, 21.99, 47.39, 96.40]
line [0.74, 1.84, 3.44, 5.83, 10.60, 22.63, 46.73, 96.57]
line [0.91, 1.81, 3.38, 5.62, 11.01, 22.94, 49.36, 102.75]
|
Bundle size comparison (
|
| 🟢 Decreased | ➖ Unchanged | 🔴 Increased (max 0.61%) | ❔ Unknown |
|---|---|---|---|
| 0 | 37 | 285 | 0 |
import * as ... in PR vs import * as ... in target (did bundle size increase?):
| Test | tsdown |
|---|---|
| STATIC_tgpu.ts | 269.67 kB ( |
| tgpu_fn.ts | 269.67 kB ( |
| tgpu_init.ts | 269.68 kB ( |
| tgpu_lazy.ts | 269.68 kB ( |
| tgpu_slot.ts | 269.68 kB ( |
| tgpu_const.ts | 269.68 kB ( |
| tgpu_unroll.ts | 269.68 kB ( |
| tgpu_resolve.ts | 269.68 kB ( |
| tgpu_accessor.ts | 269.68 kB ( |
| tgpu_comptime.ts | 269.68 kB ( |
| tgpu_vertexFn.ts | 269.68 kB ( |
| tgpu_computeFn.ts | 269.68 kB ( |
| tgpu_fragmentFn.ts | 269.68 kB ( |
| tgpu_privateVar.ts | 269.68 kB ( |
| tgpu_vertexLayout.ts | 269.68 kB ( |
| tgpu_workgroupVar.ts | 269.68 kB ( |
| tgpu_initFromDevice.ts | 269.69 kB ( |
| tgpu_bindGroupLayout.ts | 269.69 kB ( |
| tgpu_mutableAccessor.ts | 269.69 kB ( |
| tgpu_resolveWithContext.ts | 269.69 kB ( |
| STATIC_allImports.ts | 295.44 kB ( |
import { ... } in PR vs import * as ... in PR (is the library tree-Shakeable?):
| Test | tsdown |
|---|---|
| tgpu_init.ts | 261.16 kB ( |
| tgpu_initFromDevice.ts | 260.62 kB ( |
| tgpu_resolve.ts | 164.95 kB ( |
| tgpu_resolveWithContext.ts | 164.88 kB ( |
| tgpu_bindGroupLayout.ts | 69.29 kB ( |
| tgpu_mutableAccessor.ts | 66.30 kB ( |
| tgpu_accessor.ts | 66.29 kB ( |
| tgpu_privateVar.ts | 65.63 kB ( |
| tgpu_workgroupVar.ts | 65.63 kB ( |
| tgpu_const.ts | 64.88 kB ( |
| tgpu_fn.ts | 38.48 kB ( |
| tgpu_fragmentFn.ts | 38.48 kB ( |
| tgpu_vertexFn.ts | 38.29 kB ( |
| tgpu_computeFn.ts | 38.00 kB ( |
| tgpu_vertexLayout.ts | 27.11 kB ( |
| tgpu_comptime.ts | 14.80 kB ( |
| tgpu_unroll.ts | 1.66 kB ( |
| tgpu_slot.ts | 1.54 kB ( |
| tgpu_lazy.ts | 1.19 kB ( |
If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu.
|
@pullfrog review |
There was a problem hiding this comment.
ℹ️ No critical issues — one minor polish item inline.
Reviewed changes — the PR introduces runtime warnings for schemas that violate WGSL uniform-buffer layout constraints, moves buffer-count limit checks into a shared webgpuLimitations.ts module, removes now-misaligned uniform usage from two confetti examples, and adds tests for the new warnings.
- Add
warnIfNotUniformAligned— warns when a schema used asuniformhas struct member offsets or array strides that are not compatible with the default WGSL uniform address space layout. - Move
limitsOverflow.tstowebgpuLimitations.ts— consolidates WebGPU validation helpers; updates imports inapplyPipelineState.tsandlimitsOverflow.test.ts. - Clean up confetti examples — drops unused
'uniform'buffer usage for particle data that now triggers the new warning. - Add test coverage — snapshots legitimate schemas and several misalignment cases in
buffer.test.ts.
Kimi K2 (free via Pullfrog for OSS) | 𝕏
|
The newly added Referenced at
|
There was a problem hiding this comment.
Important
The new Uniform alignment tests are failing because their inline snapshots include an extra } in the warning prefix (e.g. [uniform-schema-misaligned}]). Fix the snapshots before merging.
Reviewed changes — added a runtime warning for schemas used as uniform that don't meet WebGPU's strict uniform-buffer layout constraints, plus tests and example updates.
- Merge
limitsOverflow.tsintowebgpuLimitations.ts— consolidates WebGPU limit warnings in one file. - Add
warnIfNotUniformAligned— checks array stride, struct member offset alignment, and spacing for nested structs in uniform buffers. - Wire warning into
$usage('uniform')— catches bothcreateUniform()and explicit$usage('uniform'). - Update confetti examples — removes
uniformusage from particle data buffers that are not uniform-aligned. - Add tests — covers legit schemas, misaligned structs, nested structs, and arrays.
⚠️ Consider enabling the warning in production
uniform-schema-misaligned is currently enabled only in dev/test, unlike webgpu-limits-exceeded which is also enabled in production. Since both are WebGPU-portability warnings that can cause runtime failure on some devices, consider adding the new type to the production subset in packages/typegpu/src/tgpuLogger.ts.
Kimi K2 (free via Pullfrog for OSS) | 𝕏
|
Line-anchored details for the findings in my review above:
|
| root.createUniform(d.struct({ p: d.size(32, d.struct({ p: d.u32 })), q: d.u32 })); | ||
| root.createUniform(d.arrayOf(d.vec4f, 3)); | ||
| root.createUniform(d.arrayOf(d.vec3f, 3)); | ||
| root.createUniform(d.arrayOf(d.align(16, d.u32), 3)); |
There was a problem hiding this comment.
It is false negative, see #2754
error: uniform storage requires that array elements are aligned to 16 bytes, but array element of type u32 has a stride of 4 bytes. Consider using a vector or struct as the element type instead.
var<uniform> a : array<u32, 3>;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| continue; | ||
| } | ||
|
|
||
| const minimumDifference = roundUp(16, sizeOf(thisValue)); |
There was a problem hiding this comment.
| const minimumDifference = roundUp(16, sizeOf(thisValue)); | |
| const minimumDifference = roundUp(sizeOf(thisValue), 16); |
| `\ | ||
| Schema '${getName(schema.elementType) ?? '<unnamed>'}' is used in an array in an uniform buffer, and its stride (${stride}) is not a multiple of 16. | ||
| This is not portable (see https://www.w3.org/TR/WGSL/#address-space-layout-constraints), and will break on some devices. | ||
| To address this, wrap the element in 'd.align(16, ...)'.`, |
There was a problem hiding this comment.
You cannot wrap array element with d.align

I checked and all the fixes suggested indeed do silence the errors