Prevent scalar raw-buffer loads from wrapping at 4 GiB #1145

Open
BradRoland wants to merge 1 commit from upstream-pr/1148 into main
BradRoland commented 2026-11-25 21:39:19 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by BradRoland on 2026-11-25T21:39:19Z.


A scalar buffer load can return the first words of a raw SRV when its byte offset crosses 4 GiB. On WARP, an eight-dword load starting at 0xfffffffc against a 48-byte view returned real data from the beginning of that view. With grouped loads enabled, the second DXIL call started at byte 12. Adding the instruction's immediate to its register offset could also wrap before the first load.

The shared lowering for table-bound and heap-selected raw SRVs now tests for carry when adding a positive instruction immediate and when advancing to a later host load. It returns zero for the affected words. This preserves the individual and grouped load sizes selected by VICTORY_SHADER_OPT.

I extended the existing synthetic WARP test with offsets that cross the limit both ways. It failed before the fix and passed with VICTORY_SHADER_OPT=63 and 127. The same test also checks reads that straddle the end of a small view. All 216 enabled shader crate tests passed (three ignored), as did cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings. Gameplay and shader-pack performance remain unmeasured.

<!-- greyfinch-sync:fxceless/victory-fork:issue:6614284545 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1148). Originally posted by [BradRoland](https://github.com/BradRoland) on 2026-11-25T21:39:19Z. --- A scalar buffer load can return the first words of a raw SRV when its byte offset crosses 4 GiB. On WARP, an eight-dword load starting at `0xfffffffc` against a 48-byte view returned real data from the beginning of that view. With grouped loads enabled, the second DXIL call started at byte 12. Adding the instruction's immediate to its register offset could also wrap before the first load. The shared lowering for table-bound and heap-selected raw SRVs now tests for carry when adding a positive instruction immediate and when advancing to a later host load. It returns zero for the affected words. This preserves the individual and grouped load sizes selected by `VICTORY_SHADER_OPT`. I extended the existing synthetic WARP test with offsets that cross the limit both ways. It failed before the fix and passed with `VICTORY_SHADER_OPT=63` and `127`. The same test also checks reads that straddle the end of a small view. All 216 enabled shader crate tests passed (three ignored), as did `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings`. Gameplay and shader-pack performance remain unmeasured.
fladit123 commented 2026-11-25 23:09:59 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by fladit123 on 2026-11-25T23:01:48Z.


I investigated the grouped-load boundary hypothesis mentioned in the README using synthetic raw SRVs on an RTX 4060 (driver 610.88) and WARP.

  • View sizes: 48 and 340 bytes.
  • 13 offset pairs per view.
  • VICTORY_SHADER_OPT=63 and 127, in separate processes.
  • Two fixtures: all loaded components consumed, and every fourth component unused.
  • 208 dispatches and 5,096 checked output words across eight successful test executions.

For the 340-byte view, a load starting at byte 328 preserved the three valid dwords in both modes. The out-of-range fourth component returned zero when consumed.

I could not reproduce the suspected boundary regression on this hardware with these fixtures.

Dynamic heap-selected descriptors, the actual affected shaders, and other GPU architectures remain untested. I have not independently confirmed the optimizer's elimination of unused scalar loads.

This is not a validation of the 4 GiB wrap fix itself.

AI-assisted investigation with ChatGPT; all tests executed locally using synthetic data.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7177064376 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1148#issuecomment-7177064376). Originally posted by [fladit123](https://github.com/fladit123) on 2026-11-25T23:01:48Z. --- I investigated the grouped-load boundary hypothesis mentioned in the README using synthetic raw SRVs on an RTX 4060 (driver 610.88) and WARP. - View sizes: 48 and 340 bytes. - 13 offset pairs per view. - VICTORY_SHADER_OPT=63 and 127, in separate processes. - Two fixtures: all loaded components consumed, and every fourth component unused. - 208 dispatches and 5,096 checked output words across eight successful test executions. For the 340-byte view, a load starting at byte 328 preserved the three valid dwords in both modes. The out-of-range fourth component returned zero when consumed. I could not reproduce the suspected boundary regression on this hardware with these fixtures. Dynamic heap-selected descriptors, the actual affected shaders, and other GPU architectures remain untested. I have not independently confirmed the optimizer's elimination of unused scalar loads. This is not a validation of the 4 GiB wrap fix itself. AI-assisted investigation with ChatGPT; all tests executed locally using synthetic data.
BradRoland commented 2026-11-26 00:06:07 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by BradRoland on 2026-11-25T23:11:51Z.


Thanks for the RTX 4060 results. I tested the separate 4 GiB wrap on both a table-bound raw SRV and a ResourceHeap view selected at runtime. The shader chooses between two slots with different lengths and contents. The heap case binds the renderer’s stride-table root SRV. The in-bounds controls distinguish the two buffers.

With the original lowering, an eight-dword load starting at 0xfffffffc returned several words from the start of the selected view. This reproduced on the RTX 4080 (621.14) and WARP, with VICTORY_SHADER_OPT=63 and 127, for both binding paths. With e8f3e66 each of the eight adapter/mode/binding combinations passed all ten offset cases. That includes carry in the register-plus-immediate addition and in the later words of a load.

I published the test fixture as commit f908625 atop this PR. Run cargo test --locked -p victory-shader --lib scalar_buffer_loads_ -- --nocapture with VICTORY_WARP=0 for hardware or 1 for WARP. These results establish the 4 GiB regression on hardware. The 328/340-byte water case has its own fixture and disassembly in #1179.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7177254221 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1148#issuecomment-7177254221). Originally posted by [BradRoland](https://github.com/BradRoland) on 2026-11-25T23:11:51Z. --- Thanks for the RTX 4060 results. I tested the separate 4 GiB wrap on both a table-bound raw SRV and a `ResourceHeap` view selected at runtime. The shader chooses between two slots with different lengths and contents. The heap case binds the renderer’s stride-table root SRV. The in-bounds controls distinguish the two buffers. With the original lowering, an eight-dword load starting at `0xfffffffc` returned several words from the start of the selected view. This reproduced on the RTX 4080 (621.14) and WARP, with `VICTORY_SHADER_OPT=63` and `127`, for both binding paths. With `e8f3e66` each of the eight adapter/mode/binding combinations passed all ten offset cases. That includes carry in the register-plus-immediate addition and in the later words of a load. I published the test fixture as [commit f908625 atop this PR](https://github.com/BradRoland/victory-fork/commit/f908625). Run `cargo test --locked -p victory-shader --lib scalar_buffer_loads_ -- --nocapture` with `VICTORY_WARP=0` for hardware or `1` for WARP. These results establish the 4 GiB regression on hardware. The 328/340-byte water case has its own fixture and disassembly in #1179.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1148:upstream-pr/1148'
git switch 'upstream-pr/1148'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1148'
git switch 'upstream-pr/1148'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1148'
git switch 'upstream-pr/1148'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1148'
git switch 'main'
git merge --squash 'upstream-pr/1148'
git switch 'main'
git merge --ff-only 'upstream-pr/1148'
git switch 'main'
git merge 'upstream-pr/1148'
git push origin 'main'
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
fxceless/victory-fork!1145
No description provided.