fix: drop the runtime division from the buffer_store_format index bound #2785

Closed
tedin7 wants to merge 1 commit from upstream-pr/2793 into main
tedin7 commented 2026-11-26 09:53:17 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T09:53:17Z.


Part of #1723

What this unblocks: buffer_store_format_* shaders (the packed D16 and xyzw store helpers) now create a compute pipeline on NVIDIA GPUs instead of failing with NVVM compilation failed: 3.

What changed

The two store helpers in tools/startup/gfx2hlsl.py (d16_x and format_xyzw) bounded idx * stride + rec_off against 32-bit overflow with idx > (0xffffffffu - rec_off) / stride, a divide by the runtime stride. The NVIDIA driver rejects the format_xyzw helper that holds this divide. The same bound is now checked without a division: idx is split into 16-bit halves, each partial product is range checked, then rec_off is added with a carry check. The stride is already limited to 0x3fff just above, so every partial product fits in 32 bits.

Why it is correct

The new test is the same predicate: idx*stride + rec_off > 0xffffffff. A 2,000,000-case randomized comparison against the old division form, with strides 1..0x3fff and indices at and around the overflow boundary, showed 0 differences. The bound is only an overflow guard; the in-range checks before it are unchanged.

Source: Microsoft DirectX-Specs, HLSL integer arithmetic (32-bit uint wraps modulo 2^32, so the guard has to be explicit); the failure itself is the driver's own compile error, seen only on the NVIDIA adapter.

Verification

Isolation on the RTX 3090 (vkd3d-proton, Linux): one X-f32 dispatch with only that line removed created the pipeline and ran; variants that removed the GetDimensions call, the unorm packing, the f16 packing or precise all still failed with NVVM compilation failed: 3.

  • python3 -B tests/startup_compute_store_format_xyzw_check.py passes.
  • python3 -B tests/startup_compute_store_d16_x_check.py passes.
  • python3 -B tests/startup_compute_store_format_xyzw_check.py dispatch <dxc> <offscreen_check> warp on the RTX 3090 (vkd3d-proton, Linux): every case matches (before the change: NVVM compilation failed: 3 on the first case).
  • python3 -B tests/startup_compute_store_d16_x_check.py dispatch <dxc> <offscreen_check> warp on the RTX 3090: passes.
  • Translation break-it comparison against origin/main: 173 shapes, 173 same.
  • Win2025 WARP dev+release: pending Stage-2.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6624020453 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2793). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T09:53:17Z. --- Part of #1723 What this unblocks: `buffer_store_format_*` shaders (the packed D16 and xyzw store helpers) now create a compute pipeline on NVIDIA GPUs instead of failing with `NVVM compilation failed: 3`. ## What changed The two store helpers in `tools/startup/gfx2hlsl.py` (d16_x and format_xyzw) bounded `idx * stride + rec_off` against 32-bit overflow with `idx > (0xffffffffu - rec_off) / stride`, a divide by the runtime stride. The NVIDIA driver rejects the format_xyzw helper that holds this divide. The same bound is now checked without a division: `idx` is split into 16-bit halves, each partial product is range checked, then `rec_off` is added with a carry check. The stride is already limited to 0x3fff just above, so every partial product fits in 32 bits. ## Why it is correct The new test is the same predicate: `idx*stride + rec_off > 0xffffffff`. A 2,000,000-case randomized comparison against the old division form, with strides 1..0x3fff and indices at and around the overflow boundary, showed 0 differences. The bound is only an overflow guard; the in-range checks before it are unchanged. Source: Microsoft DirectX-Specs, HLSL integer arithmetic (32-bit `uint` wraps modulo 2^32, so the guard has to be explicit); the failure itself is the driver's own compile error, seen only on the NVIDIA adapter. ## Verification Isolation on the RTX 3090 (vkd3d-proton, Linux): one X-f32 dispatch with only that line removed created the pipeline and ran; variants that removed the GetDimensions call, the unorm packing, the f16 packing or `precise` all still failed with `NVVM compilation failed: 3`. - `python3 -B tests/startup_compute_store_format_xyzw_check.py` passes. - `python3 -B tests/startup_compute_store_d16_x_check.py` passes. - `python3 -B tests/startup_compute_store_format_xyzw_check.py dispatch <dxc> <offscreen_check> warp` on the RTX 3090 (vkd3d-proton, Linux): every case matches (before the change: `NVVM compilation failed: 3` on the first case). - `python3 -B tests/startup_compute_store_d16_x_check.py dispatch <dxc> <offscreen_check> warp` on the RTX 3090: passes. - Translation break-it comparison against origin/main: 173 shapes, 173 same. - Win2025 WARP dev+release: pending Stage-2. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-26 11:02:50 +00:00
tedin7 commented 2026-11-26 11:02:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T10:13:02Z.


Withdrawn: shader translation is moving to the native C++ frontend (see #870), so we are not continuing this change to the Python translator. The branch stays available if any part is useful later. Thank you to everyone who reviewed and tested it.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7189065038 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2793#issuecomment-7189065038). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T10:13:02Z. --- Withdrawn: shader translation is moving to the native C++ frontend (see #870), so we are not continuing this change to the Python translator. The branch stays available if any part is useful later. Thank you to everyone who reviewed and tested it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2785
No description provided.