Shader: a load through a built CODE descriptor starts the constant table at the byte it reads #3155

Open
github-bot wants to merge 1 commit from github-bot/gh-3155 into main AGit
Owner

@⁠pablogonz12 on GitHub · 2026-11-26 10:27 UTC · original
pablogonz12:up/197 → main · +98 −8 · 3 files · 1 commits · open

What

code_table_start picks the first byte of the program's constant table from the tracked CODE addresses: the scalar s_load_* through s_getpc_b64, and every built descriptor's base.

A buffer load through a built CODE descriptor (s_buffer_load_dword on a descriptor the code builds around s_getpc_b64) reads its base plus the load's own byte offset, and that offset can be negative (s_sub_u32 against s_getpc_b64), so the byte the load reads can lie below the base. Only the base was recorded, the table started there, and the load's index was then rebased against a start the load never reaches: it read a dword of the table shifted by the difference, or past its end.

Now resource::track records the effective byte of every fixed s_buffer_* load whose descriptor is built around the code (Resources::code_reads: base plus the load's offset, wrapping_add, so a load below the base wraps low as the instruction does), and code_dword lets those bytes take part in choosing the table's start. Loads whose offset is not a constant, and descriptors that are not built around the code, are untouched.

Why

This is a correctness fix, not a size or speed one: the wrong dword ends up in the emitted module wherever the code does this, and the console would have read the right one.

How tested

  • New test lower::tests::code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads: a program whose instructions end at 0xa0, a descriptor built around s_getpc_b64 with base 0x80, a buffer load reading byte 0x40 through it. It asserts the table starts at 0x40. Without the change it fails ("the table starts at the load's byte": start 0x80), so the case is real and not accepted silently.
  • cargo xwin test -p victory-shader --lib (x86_64-pc-windows-msvc, under Wine on Linux): 215 passed, 88 failed, 3 ignored. The 88 are the tests that need an SM 6.6 D3D12 device ("no SM 6.6 device"); main on the same host gives 214 passed, 88 failed, 3 ignored - same 88, one more pass from the new test.
  • cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings: clean.
  • cargo xwin check -p victory-shader --all-targets --target x86_64-pc-windows-msvc: clean.

The translated DXIL is unchanged for every program that has no fixed load through a built CODE descriptor: the address list code_table_start sees only gains entries in that case, and code_reads is empty otherwise. This tree has no offline dump example, so I did not put a per-program DXIL count next to it.

Other hardware

The change is in the shader translator, ahead of any adapter: it adds tracked addresses and can only move the table's start down to a byte a load actually reads, which is what the console reads. Nothing is gated on vendor or wave width, and the emitted module's layout is unchanged (same code constant array and indices; only its first byte can differ).

  • x86_64-pc-windows-msvc build and tests as above; not tested on NVIDIA hardware, and not run in a game.

Written with an AI assistant.

**@⁠pablogonz12** on GitHub · 2026-11-26 10:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/3155) `pablogonz12:up/197` → `main` · +98 −8 · 3 files · 1 commits · open ## What `code_table_start` picks the first byte of the program's constant table from the tracked `CODE` addresses: the scalar `s_load_*` through `s_getpc_b64`, and every *built* descriptor's base. A buffer load through a built `CODE` descriptor (`s_buffer_load_dword` on a descriptor the code builds around `s_getpc_b64`) reads its base **plus the load's own byte offset**, and that offset can be negative (`s_sub_u32` against `s_getpc_b64`), so the byte the load reads can lie below the base. Only the base was recorded, the table started there, and the load's index was then rebased against a start the load never reaches: it read a dword of the table shifted by the difference, or past its end. Now `resource::track` records the effective byte of every *fixed* `s_buffer_*` load whose descriptor is built around the code (`Resources::code_reads`: base plus the load's offset, `wrapping_add`, so a load below the base wraps low as the instruction does), and `code_dword` lets those bytes take part in choosing the table's start. Loads whose offset is not a constant, and descriptors that are not built around the code, are untouched. ## Why This is a correctness fix, not a size or speed one: the wrong dword ends up in the emitted module wherever the code does this, and the console would have read the right one. ## How tested - New test `lower::tests::code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads`: a program whose instructions end at `0xa0`, a descriptor built around `s_getpc_b64` with base `0x80`, a buffer load reading byte `0x40` through it. It asserts the table starts at `0x40`. **Without the change it fails** ("the table starts at the load's byte": start `0x80`), so the case is real and not accepted silently. - `cargo xwin test -p victory-shader --lib` (x86_64-pc-windows-msvc, under Wine on Linux): **215 passed, 88 failed, 3 ignored**. The 88 are the tests that need an SM 6.6 D3D12 device ("no SM 6.6 device"); `main` on the same host gives **214 passed, 88 failed, 3 ignored** - same 88, one more pass from the new test. - `cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings`: clean. - `cargo xwin check -p victory-shader --all-targets --target x86_64-pc-windows-msvc`: clean. The translated DXIL is unchanged for every program that has no fixed load through a built `CODE` descriptor: the address list `code_table_start` sees only gains entries in that case, and `code_reads` is empty otherwise. This tree has no offline dump example, so I did not put a per-program DXIL count next to it. ## Other hardware The change is in the shader translator, ahead of any adapter: it adds tracked addresses and can only move the table's start **down** to a byte a load actually reads, which is what the console reads. Nothing is gated on vendor or wave width, and the emitted module's layout is unchanged (same `code` constant array and indices; only its first byte can differ). - `x86_64-pc-windows-msvc` build and tests as above; not tested on NVIDIA hardware, and not run in a game. Written with an AI assistant.
code_table_start picked the table's first byte from the tracked CODE
addresses, and a built descriptor's base is one of them: the byte a
buffer load through it reads is that base plus the load's own offset,
which can lie below the base (s_sub_u32 against s_getpc_b64), so the
table started above the bytes the load reads and its index was rebased
against a start the load never reaches.

Track the effective byte of every fixed s_buffer_* load whose descriptor
is built around the code (resource::Resources::code_reads) and let it
choose the table's start.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:08:55 +00:00
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 10:40 UTC · original

Independent local verification of head e86940c on Windows 11, Rust 1.100.0/MSVC:

  • cargo test --locked -p victory-shader --lib emit::lower::tests::code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads — 1 passed, 0 failed.
  • git diff --check origin/main HEAD — clean.

I used a worktree-local CARGO_TARGET_DIR because the shared target dir served a stale test binary from a different worktree of the same crate. Synthetic shader inputs only; no game assets, extracted shaders or DRM material used. AI-assisted (Hy4 flash).

**@⁠cloph-dsp** on GitHub · 2026-11-26 10:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/3155#issuecomment-7189672707) Independent local verification of head `e86940c` on Windows 11, Rust 1.100.0/MSVC: - `cargo test --locked -p victory-shader --lib emit::lower::tests::code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads` — 1 passed, 0 failed. - `git diff --check origin/main HEAD` — clean. I used a worktree-local `CARGO_TARGET_DIR` because the shared target dir served a stale test binary from a different worktree of the same crate. Synthetic shader inputs only; no game assets, extracted shaders or DRM material used. AI-assisted (Hy4 flash).
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 11:11 UTC · original · approved these changes

Verified against origin/main (63bcca4):

  • cargo test -p victory-shader --lib emit::lower::tests:: -- --test-threads=1: 34/34 passed (including code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 192 files ok.

Cleanly starts the constant table at the lowest byte read through a built CODE descriptor.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 11:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/3155#pullrequestreview-6073114222) · **approved these changes** Verified against `origin/main` (`63bcca4`): - `cargo test -p victory-shader --lib emit::lower::tests:: -- --test-threads=1`: 34/34 passed (including `code_table::the_code_table_starts_at_the_lowest_byte_a_load_reads`). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 192 files ok. Cleanly starts the constant table at the lowest byte read through a built `CODE` descriptor. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3155/head:github-bot/gh-3155'
git switch 'github-bot/gh-3155'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3155'
git switch 'github-bot/gh-3155'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3155'
git switch 'github-bot/gh-3155'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3155'
git switch 'main'
git merge --squash 'github-bot/gh-3155'
git switch 'main'
git merge --ff-only 'github-bot/gh-3155'
git switch 'main'
git merge 'github-bot/gh-3155'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3155
No description provided.