Skip to content

Shaders: read root arguments the console spills to memory - #302

Closed
dadou000 wants to merge 1 commit into
Marvisak:mainfrom
dadou000:spill-table-user-data
Closed

dadou000 wants to merge 1 commit into
Marvisak:mainfrom
dadou000:spill-table-user-data

Conversation

@dadou000

Copy link
Copy Markdown

What

Compute programs whose root signature does not fit the 16 user SGPRs read their arguments through a spill table: s[0:1] holds a pointer (high dword masked to 40 bits, usually copied to s2/s3 first) to a table of user data, and the program s_loads descriptors and constants from it. Table dword k is user SGPR 16 + k. The translator only knew addresses that come from the declared root layout, so all of these failed with an address in an SGPR that is no root parameter.

For a compute program whose root layout lies entirely at user SGPR 16 and above, loads from that pointer with a constant offset become s_mov_b32 copies of the user SGPRs the dwords stand for. This happens before the SSA is built, so resource tracking and lowering see ordinary root arguments.

Only straight-line code from the pointer's set-up is rewritten. Tags are dropped at branch targets and whenever a pointer register is overwritten. Any other load stays a load.

Evidence

Example from the world shaders (a 64-thread group; layout is two constants then two descriptors, user data 16 to 23):

s_mov_b32 s2, s0 ; s_and_b32 s3, s1, 0xff
s_load_dwordx4 s[8:11], s[2:3], 0x10      ; table dwords 4..7 = Descriptor(20,2), Descriptor(22,2)
s_load_dwordx4 s[12:15], s[2:3], null     ; table dwords 0..3 = Constants(16,2), Constants(18,2)
s_or_b32 s5, s11, 0x10000 ; s_mov_b32 s6, -1 ...   ; buffer descriptor built from the root descriptor's address

One 64-thread-group dispatch in the title sets nine root parameters (about 35 dwords), and its shader loads at offsets 0x0, 0x20 and 0x4c, which is what a spill table of those arguments would look like.

On the world's compute shaders, an address in an SGPR that is no root parameter went from 34 programs to 4 in one run (RTX 4090, Windows 11). Failures that were hidden behind it now show: an address not from a root parameter, a root UAV or typed root view, an address from a phi, a descriptor read only as data.

Not verified

  • I only measured how many programs translate. I have not confirmed that the resulting images are right.
  • The rule that decides a program uses the spill table (every root parameter at user SGPR 16 or above, compute only) is what fits the programs I looked at. I don't know whether another program uses the same pointer with a different layout.
  • Two unit tests cover the rewrite and a load that must stay a load. cargo test -p victory-shader --lib passes (71 tests).

🤖 Generated with Claude Code

A compute program whose root signature does not fit the 16 user SGPRs
finds its arguments through a spill table: s[0:1] holds a pointer (high
dword masked to 40 bits, usually copied to s2/s3 first) to a table of
the user data, and the program s_loads descriptors and constants from
it. Table dword k is user SGPR 16 + k. The translator only knew
addresses that come from the declared root layout, so every such
program failed with "an address in an SGPR that is no root parameter".

For compute programs whose root layout lies entirely at user SGPR 16 and
above, loads from that pointer with a constant offset become s_mov_b32
copies of the user SGPRs the dwords stand for, before the SSA is built,
so resource tracking and lowering see what they see for ordinary root
arguments. Only straight-line code from the pointer's set-up is
rewritten (tags are dropped at branch targets and on any overwrite of
the pointer registers); other loads stay loads.

On the title's world shaders this took the "SGPR that is no root
parameter" failures from 34 programs to 4 in one run. Other failures
that were hidden behind it now show (address not from a root parameter,
root UAV or typed root view, an address from a phi...). Whether the
translated programs render correctly has not been checked beyond that.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@gw1337x

gw1337x commented Nov 24, 2026

Copy link
Copy Markdown

In-game test on an RX 7800 XT (Windows 11 26200), diag build. Build: main d2a4bae + #135 + #242 + #230 + #124 + #130 + this PR (clean merge). One run into the world, measured with the drop summary the same way as in #293.

The targeted reason drops a lot:

Reason Before (#293) With this PR
an address in an SGPR that is no root parameter 31 shaders, ~600 per frame 3 shaders, ~206 per frame
a root UAV or typed root view – 7 shaders, ~156 per frame
an address not from a root parameter 1 shader, ~63 per frame 10 shaders, ~93 per frame

The two growing rows look like shaders that now get past the spill table and stop at the next unsupported construct. Draws and dispatches lost per frame went from about 1,770 to about 1,340 in total (rates averaged over the run, menus included).

The rates are not strictly comparable: this run was shorter, so the menus weigh less. It ended at 67 s on the known page fault inside freed textures (#135's remaining case); comparable builds reached 113–177 s. That is one run, and it may just be that more draws now reach those textures sooner. No world frame was captured, so I can't say whether the picture changed.

@matheushagedorn

Copy link
Copy Markdown

Tested on top of d2a4bae (+ #177, #151, #55, #189), cross-built on Linux and run under Wine.

victory translate does not apply the rule. apps/victory/src/translate.rs builds the IR with ir::build(&p.patched_code(), p.wave64, p.vgprs), so the CLI statistics never see the rewrite, only the runtime path through emit::translate_with does. Anyone measuring with the tool sees no change. Locally I used the same condition there:

let spilled = p.stage == victory_shader::HwStage::Cs
    && !p.layout.is_empty()
    && p.layout.iter().all(|r| p.stage.user_sgpr(r.user_data) >= 16);
match ir::build_with(&p.patched_code(), p.wave64, p.vgprs, spilled) {

Maybe worth exposing one helper for that rule so the two call sites cannot drift.

XboxBvhBuilder.dll's 52 containers (victory translate <game dir>\XboxBvhBuilder.dll), which matter for the world on NVIDIA: every builder program the title uses failed with an address in an SGPR that is no root parameter before this PR. The one I disassembled has exactly the pattern described here (copy of s[0:1] with the high dword masked to 0xff, s0 reused for a literal right after, then s_load_dwordx4 at offset 0 and s_load_dwordx16 at 0x10; two root descriptors and 16 constants at user data 16, 18 and 20).

before with this PR (CLI patched as above)
resources tracked 0 of 52 19 of 52
DXIL emitted 0 0

What blocks them now:

Programs First failure
18 resources: an address not from a root parameter
12 emit: a root UAV or typed root view
7 emit: a select on an untracked mask
7 resources: an address in an SGPR that is no root parameter
6 resources: an address from a root parameter that is no address
2 resources: an address from a phi

The second row comes from the Source::Built arm in lower.rs, which handles root CBVs and raw root SRVs; the builder's root descriptors are UAVs (in the program I disassembled, both are), so they fall through. I haven't checked each of the 12.

For context on why this matters: on Linux (RTX 5060 Ti, vkd3d-proton 3.2.0) the world's Xid 13 MMU fault (#71) goes away when one compute program of the BVH-preparation pass is skipped (48426bae580a080c, see my comment there), and since the builder never runs, my guess is that the title's ray-tracing shaders walk memory that was never built (see #274 on the console node format); I have not confirmed that part. This PR is the first step towards that builder translating.

No game data attached.

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Review note, with a small repro. The rewrite turns a spill-table load into s_mov_b32 sK, s(16+k), which reads whatever that register holds at that point in the program, not the entry value. If the program has already written s16 and up (as the destination of an earlier load, say), a later load reads the wrong root argument, with no refusal.

With this PR on main (d2a4bae), ir::build_with(.., spilled = true) on

s_load_dwordx8 s[16:23], s[0:1], 0x20   ; table dwords 8..15
s_load_dwordx4 s[8:11],  s[0:1], 0x0    ; table dwords 0..3

gives

s_mov_b32 reads=[S(24)] writes=[S(16)]   ... S(31) -> S(23)
s_mov_b32 reads=[(S(16), Value(8))] writes=[S(8)]   ... S(19) -> S(11)

The second load's moves read the values the first load just wrote (Value(8..11)), so s[8:11] become user data 24..27 instead of 16..19, and p.inputs never contains s16.

The example in the description loads into s8..s15, so it does not hit this, and the 34 to 4 count may well be unaffected. But nothing stops a program from loading into s16+. Could the rewrite read the entry values directly (the program's inputs for user SGPR 16+k) rather than the live registers, or be skipped once any of those registers has been written earlier in the scan?

This is the PR for the top row of #293, so it matters that it cannot bind the wrong argument silently. From a synthetic test only; I have no copy of the title.

@louketa

louketa commented Nov 24, 2026

Copy link
Copy Markdown

Measured this PR against the title's own world shaders (v1.0.2.1). Summary: it clears the first failure of every program it targets, but none of them translate fully yet. The next stop for most is an address that comes out of the spill table.

How

Merge note

This PR conflicts with #309 in emit::translate. I kept both: ir::build_with(..., spilled) followed by sgpr_spill::forward(&mut p).

Result: 34 programs that fail with an address in an SGPR that is no root parameter without this PR

Next failure with this PR Programs
an address not from a root parameter 12
a root UAV or typed root view (emit) 8
an address from a root parameter that is no address 6
an address in an SGPR that is no root parameter (at a later instruction) 4
an address from a phi 2
a select on an untracked mask (emit) 2
fully translated 0

The smallest case

3b90165a77d95cdf is a 22-instruction compute program. Its layout is two root descriptors and one root-constant block, all at user data 16 or above, so spilled is true. It reads the constants from the spill table, which this PR now resolves. It also loads the address of its root UAV from the table (the 64-bit value at that parameter's offset) and builds a buffer descriptor around it for a buffer_store. That address is still reported as an address in an SGPR that is no root parameter at the store. Treating a 64-bit spill-table load at a root-descriptor parameter's offset as that parameter's address looks like the natural next step. Most of the rows above look like the same case.

Side note

victory translate (apps/victory/src/translate.rs) still calls ir::build, so static scans do not apply the spill rule and show none of these programs failing the way they do at run time. With the same spilled condition there, the CLI matches the runtime.

No game data attached.

@louketa

louketa commented Nov 24, 2026

Copy link
Copy Markdown

@dadou000 @r4tedx I opened #489 on top of this PR. It fixes the entry-value issue r4tedx reported: the user SGPRs the table loads stand for are copied into an unused SGPR block on entry, and the loads read that block.

It does show up in the title's shaders. 3b90165a77d95cdf (the largest drop in my world run) hits exactly that case, and with the fix it gets through resource tracking. Numbers over the 34 programs and a regression test that fails on this PR alone are in #489. dadou000, feel free to pull the commit into this branch if you'd rather keep it in one PR.

@etcetera-yt

Copy link
Copy Markdown

translate_for now builds compute programs whose root arguments all sit past the 16 user SGPRs with
ir::build_with(..., spilled), but victory translate still calls ir::build (apps/victory/src/translate.rs,
the per-program loop), so the CLI never applies the spill rewrite. Checking this PR from the command line therefore
shows no change for such programs, while the runtime path does use it.

Example: the ray-tracing builder's DLL (XboxBvhBuilder.dll) holds 52 compute programs whose layout entries all
start at user-data dword 16 with 4 user SGPRs. victory translate <dll> --list reports all 52 failing with
"an address in an SGPR that is no root parameter" with this PR merged, while in a title run only 1 of the 35 used
ones still fails for that reason (the others fail later, on root UAVs and addresses in root constants).

Building the CLI's programs the way translate_for does (same spilled test, ir::build_with) would make the two
agree. Tested on Windows 10 19045, RTX 5060 Ti, with this PR on d2a4bae plus other open PRs.

AI use: Claude Code, Claude Opus 5.5.

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

Independent check of head 815178d on main d2a4bae, read together with #489, which is stacked on it. I read the comments above first. The entry-value problem (@r4tedx, and the overlap case @marquezinii added on #489) is real and #489 fixes it; my run below is one more confirmation, with a shape that ends in a refusal rather than a wrong value. I found nothing else wrong in this commit; the details of what I checked in the rewrite itself are in my note on #489, since that is the version that should go in.

Read against the RDNA2 ISA (30-November-2020)

  • 7.2.1 "S_LOAD_DWORD", p. 52: ADDR = SGPR[base] + inst_offset + { M0 or SGPR[offset] or zero }, the two LSBs ignored. The rewrite takes loads with a constant, non-negative, dword-aligned offset and no SGPR offset; the rest stay loads. Narrower than the hardware, never wrong.
  • That s[0:1] holds the table pointer and table dword k is user SGPR 16 + k is the console compiler's convention, not ISA; the title counts above are the evidence for it.

Run on hardware. NVIDIA RTX 3070, driver 616.56, Windows 11, Rust 1.98.1, dxil.dll from the Windows SDK 10.0.26100 validating; cargo test -p victory-shader -- --test-threads=1:

  • this PR on main: 71 passed, 0 failed (main: 69).

  • A local end-to-end probe (not pushed): a compute program whose whole layout is in the table (8 root constants at user data 16..23 holding 100..107, a UAV table at 24), which stores the constants it sees, on the GPU and on WARP:

    loads this PR Shaders: spill-table loads read the arguments as they were on entry (stacked on #302) #489
    s_load_dwordx8 s[40:47] 100..107 100..107
    s_load_dwordx8 s[16:23] first, then the same dwords into s[40:47] 100..107 (each s_mov_b32 of the first load is s16+k <- s16+k, so nothing is lost) 100..107
    s_load_dwordx16 s[12:27], then the table descriptor from dword 8 and the constants refused: an address in an SGPR that is no root parameter at the descriptor load, because s24 then holds what stood for table dword 12 100..107

    The third row is the shape described for 3b90165a77d95cdf above: a wide load over s16 and up, then a load of an address from the table. Here it ends in the old refusal; with other registers it binds the wrong argument, as shown above.

  • IR probes with spilled = true: a load at a branch target (forward, and a loop header), a pointer copy overwritten by s_movk_i32, a pointer copied with s_mov_b64, and a load whose destination covers the pointer copy all leave the affected load a load. Correct, and conservative.

Not raised above, small

  • The rewritten s_mov_b32 run shares the load's offset. In build_with that makes each of them "end" its block when that offset is a leader, setting a self-jump exit that the block's real last instruction then overwrites. It comes out right, but only through that overwrite.
  • apps/victory/src/translate.rs still calls ir::build (already noted above); one helper for the spilled condition would keep the CLI and emit::translate from drifting.

Verdict from here: correct only together with #489's commit; I would merge them as one.

Not checked: the title (no game files here), AMD hardware, the 16-or-more user SGPR case discussed on #489.

AI-assisted (Claude Code).

Kam471 pushed a commit to Kam471/VIctory that referenced this pull request Nov 24, 2026
The ray-tracing builder passes buffer addresses as a pair of root
constants rather than as root views, so its programs failed with
"an address from a root parameter that is no address".

The tracker now takes a root constant as an address's low half when the
next dword of the same parameter is its high half, and records which
dword (`Address::dword`). The emitter binds such an address as a root CBV
or raw SRV, as it does for root descriptors, at a register that names the
parameter and the dword (`RootView`, `(dword + 1) << 8 | slot`); root
view registers are 16 bits now. The GPU side reads the address from the
two constants the title set and binds it as it does a root view.

The shader cache format is VSH2 (16-bit registers).

Root UAVs (Marvisak#294) and the spill table (Marvisak#302) are still needed for the
builder's programs to translate in the title; tested with synthetic
programs only (tracking, lowering, dxil.dll validation, pipeline
creation on an RTX 5080), no game data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MfDev0 referenced this pull request in MfDev0/victory Nov 24, 2026
…pill only with s16 free

Port of the rest of upstream Marvisak#489 (Brazlucas: 915d13d, 608f150, fe7210e; JMAN730#302 was already in). Spill-table loads read the root arguments as they were at entry even after the program overwrote the SGPR; the spill rule applies only when the hardware puts nothing in s16 (with 16 user SGPRs the group id sits there), and victory translate uses the same rule; a spill-table pointer mask must keep every address bit.

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
@YaBulochka

Copy link
Copy Markdown

Retail-run confirmation on Linux/RADV: this is the change that turned a black screen into a visible, coloured 3D scene here.

Title 1.0.2.1, Series X dump
OS Linux, Wine 11.22 in a Proton 11.0 prefix, vkd3d-proton master
GPU RX 7600 XT, RADV from Mesa 26.3.1
Tree ffe310e + #994 + #491 + #1548 + #1583 + #1319 + #1402 + this PR

Before, the run logged 27 GFX10 code at 0x30: an address in an SGPR that is no root parameter is not supported lines (and more at 0x40/0x2c), and those programs' draws were dropped.

After this PR: 4 such lines left, the scene reaches 3,770-3,881 draws/frame and, with VICTORY_DEBUG_PS=1, a coloured 3D frame renders at 1.5 fps (menus at 2 fps). Without the debug pixel shader the same tree gets to the prologue and then the GPU is removed (0x887A0005) at 0.2 fps, which I read as the normal pixel path being far heavier, not as a fault of this PR.

Merge note, in case someone stacks it the same way: on top of #1402 this PR conflicts in crates/victory-shader/src/emit/mod.rs. I resolved it as ir::build_with(..., spilled) from this PR, then structure::structure(&p), then resource::track(&p, &native.layout, native.stage, st.as_ref().ok()) (the four-argument track), and it builds and runs.

Remaining in the log: 4 x a descriptor read only as data, and a structured buffer of unknown stride - the compute side, unrelated to this PR.

suirentakamura referenced this pull request in suirentakamura/victory-suiren Nov 24, 2026
The ray-tracing builder passes buffer addresses as a pair of root
constants rather than as root views, so its programs failed with
"an address from a root parameter that is no address".

The tracker now takes a root constant as an address's low half when the
next dword of the same parameter is its high half, and records which
dword (`Address::dword`). The emitter binds such an address as a root CBV
or raw SRV, as it does for root descriptors, at a register that names the
parameter and the dword (`RootView`, `(dword + 1) << 8 | slot`); root
view registers are 16 bits now. The GPU side reads the address from the
two constants the title set and binds it as it does a root view.

The shader cache format is VSH2 (16-bit registers).

Root UAVs (#294) and the spill table (#302) are still needed for the
builder's programs to translate in the title; tested with synthetic
programs only (tracking, lowering, dxil.dll validation, pipeline
creation on an RTX 5080), no game data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 995f7f3ee720270108dfe93decb0f8c3c53f613b)
@KevinPratap

Copy link
Copy Markdown
Contributor

A note for when this lands: victory translate (apps/victory/src/translate.rs) builds programs with ir::build, without the spill rewrite that translate_for applies through build_with. So the offline report still counts these programs as an address in an SGPR that is no root parameter, although they translate in the title.

Measured on a full shader pack, --host-lanes 32-32, with the same decision shared (a NativeProgram::spills_roots() used by both emit and translate.rs): that refusal goes from 247 to 197 programs, and compute programs emitted go from 2,425 to 2,445. It's a few lines; happy to send it as a pull request stacked on this one if that helps. AI-assisted.

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.