Skip to content

Avoid Metal snorm16 vertex-fetch register spills - #4

Draft
matthargett wants to merge 2 commits into
trunkfrom
perf/msl-snorm16-vertex-unpacking
Draft

matthargett wants to merge 2 commits into
trunkfrom
perf/msl-snorm16-vertex-unpacking

Conversation

@matthargett

@matthargett matthargett commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Description

Replace Metal's unpack_snorm2x16_to_float intrinsic in Naga's Snorm16, Snorm16x2 and Snorm16x4 vertex-fetch helpers with explicit signed-short conversion, division by 32767, and clamping at -1.

While profiling a moving-camera glTF scene on iPhone XS/A12, pipelines using packed snorm16 vertex attributes reported 16 bytes of compiler spilling per input, or 32 bytes with two inputs. Minimal controls with float32, snorm8, storage-buffer fetch, different MRT layouts and f16 arithmetic did not reproduce it. A device-reference-versus-struct-copy experiment also did not remove the reports. Replacing this particular unpack helper did.

Packed assets, attribute formats, strides, offsets, bounds checks and vertex/instance stepping remain unchanged. The general WGSL unpack2x16snorm builtin is intentionally untouched. This is a Metal code-generation correction, not a WebGPU semantic relaxation, runtime synchronization change, or memoryless-attachment change.

Naga is part of the wgpu monorepo, so this one draft covers the wgpu/Naga contribution; there is no duplicate PR against the archived standalone Naga repository.

Validation

  • Current-upstream Naga snapshot test passes with NAGA_SNAPSHOT=msl-vpt, covering the existing vertex-pulling/format fixtures. Four generated MSL snapshots change only their helper bodies.
  • Both existing wgpu vertex-format GPU tests pass on M4 Max Metal against this rebased draft.
  • On physical XS and iPhone 12, the diagnostic GPU point-grid checked all 65,536 signed encodings plus endpoint lanes (262,144 values), including -32768 and 32767. Maximum float32 reference difference was 5.96e-8 on each device. Its one readback occurs before the rendering loop, not during performance measurement.
  • The isolated XS trace reports zero spill events across 5,784 encoders after the fix. A later unchanged-scene capture reports zero across 4,347 application encoders; all 89,195 partial-render samples are zero. Those observations do not prove every possible SRAM/register spill mechanism is absent.
  • Four fixed-pose scene images remained pixel-identical across the retained-build recheck, with materials and AO preserved.
  • The iPhone 12 retest reports zero compiler-spill events across 4,500 application encoders. Its separate five-minute, non-AR, continuously moving-camera run at 1170x2532 measured 59.67 FPS overall and 59.39 FPS over the final 120 seconds. The final two minutes held Metal allocation at 200.23 MiB, with no new textures, views, buffers or pipelines. Four reference poses are pixel-identical across the profiling-only keep-awake rebuild. This is a combined-stack result, not a patch-isolated comparison.

The iPhone 12 sustained run used 25% app-set brightness, USB power and a 180-second nominal-thermal start gate. It reached serious reported thermal state, so the result is not evidence of lower power, cool operation or indefinite 60 FPS. An earlier run stopped producing frames and has no completed benchmark receipt; it is excluded. Profiling-only keep-awake was added for the completed repeat, without changing rendering. The cause of that earlier interruption remains unproven.

Physical-device evidence uses the maintained backport on wgpu 30.0.1 (40f4a34ebaf56f9a046231f54125ad046239d3f3); the draft is rebased onto current upstream trunk a1d029a731855a6c8d49b717b0601ff3e2f05817. No patch-only sustained FPS or power benefit is claimed. The full rendering stack still falls below sustained 50 FPS on XS in the final two minutes of its five-minute test.

The broader local cargo xtask test --test-threads 4 run is not green: 1,216 passed, five failed, six skipped. Three passthrough cases require unavailable DXC/Metal compiler tools. The other failures are subgroup_operations (an expected-failure case unexpectedly succeeds) and written_timestamp_resolves_to_nonzero (zeros returned); both reproduce on verified unchanged upstream trunk. The targeted baseline selection also reports three related passes: one executed subgroup case and two unsupported-feature timestamp cases. No test expectations were weakened and no unrelated fixes are included.

NAGA_SNAPSHOT=msl-vpt cargo test -p naga --test naga --features wgsl-in,msl-out snapshots::convert_snapshots_wgsl -- --nocapture

Draft for fork review before any upstream submission. Build-only Cargo workspace pinning and BabylonNative's scheduling/presentation experiments are intentionally excluded.

Implementation and device TODOs

  • Implementation: Preserve format semantics and retest regenerated Metal output and controls before upstreaming.
  • On device: Check matching pixels and actual register pressure at matched thermals on the XS and iPhone 12.

AGENTS.md · Qualification and PR index. Fork branch instructions.

Extracted from the maintained mobile-rendering patch stack with focused regression coverage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant