Skip to content

Shaders: trace a code-table address read from the first lane of a uniform value - #1225

Closed
LooperSalty wants to merge 1 commit into
Marvisak:mainfrom
LooperSalty:shaders/first-lane-uniform-code-table
Closed

LooperSalty wants to merge 1 commit into
Marvisak:mainfrom
LooperSalty:shaders/first-lane-uniform-code-table

Conversation

@LooperSalty

Copy link
Copy Markdown

From a tester working with Claude (AI-assisted); measured on my own machine (i7-13700KF, RTX 4070 Super). No game data in the diff; test programs are synthetic words assembled with llvm-mc.

Complements ffe310e (waterfall loop over a first-lane table offset): this covers the other form of the same idiom, where the table address is a VGPR sum of s_getpc_b64 and a uniform index, read back to SGPRs with v_readfirstlane_b32 before s_load. When every lane holds the same value the first lane's read is that value, so the load is traced through the sum. A per-lane source is still refused, now naming the reason.

Measured on an integration tree: ColorDownsampleCS and InitCS now translate and validate (dxil.dll); in the shadow/luminance containers traced 70 -> 72, emitted 56 -> 58.

Two tests (uniform address traced; per-lane address refused by name). cargo test -p victory-gpu -p victory-shader is green on this branch (based on origin/main ffe310e).

…form value

The console compiler builds the address of a table in the code in VGPRs
(s_getpc_b64 plus a uniform index through v_mov/v_lshlrev/v_add_co) and reads
it back with v_readfirstlane_b32 before the s_load. When every lane holds the
same value the first lane's read is that value, so the load is traced as if
it used the VGPR sum directly; a per-lane source is still refused, now by
name ("an address read from the first lane of a per-lane value").

Uniformity is decided from scalars, constants and first-lane reads through a
list of VALU operations (memoised per value).

Two tests: a uniform code-table address is traced (offset and computed index
checked); a per-lane one is refused by name. Measured on an integration tree:
ColorDownsampleCS and InitCS now translate and validate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LwHrL6fhkpeG9ByE1rJxrt
@SantannaCarlos

Copy link
Copy Markdown
Contributor

Checked offline together with #1198 (address map), against the title's shader pack (victory translate --validate, 32-lane host, dxil.dll validator):

Tree ColorDownsampleInitCS / ColorDownsampleCS (auto exposure) CS_LaserLightShadowRaytracing
main an address not from a root parameter an address not from a root parameter
main + #1198 emit: s_getpc_b64 a lane mask holding lanes elsewhere read as data
main + #1198 + this PR emit and validate an address read from the first lane of a per-lane value
#439 stack + #1198 emit: s_getpc_b64 emits and validates
#439 stack + #1198 + this PR emit and validate an address read from the first lane of a per-lane value

So this PR fixes the exposure pair, but with #1198 it stops a program that #1198 (on the #439 stack) translated.

Why: #1198 turns an access whose address has no root parameter into a read of title memory at run time when runtime_address(why) matches one of its reasons ("an address not from a root parameter", "an address from a phi", "an address loaded from memory", ...). This PR gives a first-lane read of a non-uniform value a reason of its own, "an address read from the first lane of a per-lane value", which is not in that list, so the access is refused where #1198 would have read it. In CS_LaserLightShadowRaytracing the node address of the BVH walk is picked per lane (v_cndmask between s12 and s12 | 48) and read back with v_readfirstlane before an s_load: that case. The exposure pair is the one this PR resolves (s_getpc_b64 plus a uniform index, built in VALU and read back with v_readfirstlane).

Either side can make the two compose: #1198 can add the new reason to runtime_address (an address read from the first lane is one the translation can rebuild at run time), or this PR can keep the old reason for the first-lane reads it does not resolve.

No game data here: program names, counts and refusal messages only.

dieharddillon1 pushed a commit to dieharddillon1/VIctory that referenced this pull request Nov 24, 2026
Adapt the lane-uniform CODE provenance proof from Looper_Salty (Marvisak#1225) and cover the sign-extended shifted index in the exposure shaders.
@pablogonz12

Copy link
Copy Markdown
Contributor

Setup: Linux, RX 9070 XT, RADV (Mesa 26.3.1), dxil-spirv master, spirv-val 1.3. Your head folded onto a base of origin/main ffe310e plus #1017 (main's host build fails without it: E0425 host_resource), then the four-stage offline check over 768 programs (751-program world dump + 52-program BVH capture, 35 duplicate containers skipped).

translate valid SPIR-V RADV pipeline
base 666 / 768 656 656
your head 668 / 768 658 658

+2 programs translate and none is lost: "an address not from a root parameter" goes from 3 compute programs to 1, the same two the uniform-address change clears, and no other reason moves. Same 10 SPIR-V validation failures as the base.

Not tested: in-game.

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at b00573d: cargo test -p victory-shader passes, 122 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

@Rezwo

Rezwo commented Nov 24, 2026

Copy link
Copy Markdown

I reproduced the interaction with #1198 using a five-dword synthetic fixture, and found a counterexample to adding the new error string to the runtime-address allowlist unconditionally. This is resource tracking only; no game files, DXIL execution, or GPU run.

Resource-tracker input #1198 fe078c6 + #1225 b00573d + allowlist the new string
Synthetic runtime address from a first-lane read GLOBAL Refused at byte 8 GLOBAL
#1225 uniform code-table test — Pass Pass
#1225 per-lane code-table test — Refused as expected Incorrectly routed to GLOBAL

In the resource.rs test module, this is the extra fixture (generated instruction words; no title data):

#[test]
fn a_per_lane_runtime_pointer_stays_on_the_address_map() {
    let resources = track_words(
        &[0x7E04_0500, 0x7E06_0502, 0xF400_0181, 0xFA00_0000, S_ENDPGM],
        &[],
        HwStage::Cs,
    );
    assert_eq!(resources.scalar.as_slice(), &[((0, 2), at(GLOBAL, 0))]);
}

The instructions are two v_readfirstlane_b32 reads from v0 and v2 into s[2:3], then s_load_dword and s_endpgm. Run with cargo test --locked -p victory-shader --lib resource::tests::a_ on the combined tree. The two combined runs each passed 17/18, but failed different tests. A composable fix needs to distinguish the code-table case from a runtime pointer before selecting the fallback; the error text alone doesn't carry that distinction.

AI-assisted (Codex).

@Rezwo

Rezwo commented Nov 24, 2026

Copy link
Copy Markdown

I tested a small follow-up for the interaction with #1198: Rezwo@2b68329

The runtime-map fallback now checks the SSA dependencies of both address words and the dynamic offset. If a first-lane address depends on s_getpc_b64, it stays out of the runtime map; a uniform code-table access still uses the existing code-table path. This fallback only applies to memory instructions, not descriptor construction.

On the combined #1198 + #1225 tree, the synthetic runtime-pointer test failed before the patch. With the patch, that test and the uniform/per-lane code-table cases pass, including a new case where only the high word depends on the program counter. The locked shader library suite passed 127 tests (1 ignored) with WARP forced, and shader-crate Clippy passed with warnings denied. No title files or in-game test. This is for review or cherry-pick on the combined revisions; no new upstream PR.

AI-assisted (Codex).

simnJS added a commit to MiamiGame/XTP that referenced this pull request Nov 24, 2026
…orm value

The console compiler builds the address of a table in the code in VGPRs
(s_getpc_b64 plus a uniform index through v_mov/v_lshlrev/v_add_co) and
reads it back with v_readfirstlane_b32 before the s_load. When every lane
holds the same value the first lane's read is that value, so the load is
traced as if it used the VGPR sum directly; a per-lane source is still
refused, now by name ("an address read from the first lane of a per-lane
value").

Uniformity is decided from scalars, constants and first-lane reads through
a list of VALU operations (memoised per value).

Two tests: a uniform code-table address is traced (offset and computed
index checked); a per-lane one is refused by name. Upstream measured
ColorDownsampleCS and InitCS translating on an integration tree.

Applied as is (XTP's resource.rs is upstream's at ffe310e but for
formatting); the review's fixes are two commits on.

Ported from Marvisak/VIctory#1225 (head b00573d) by LooperSalty

Co-authored-by: Looper_Salty <106304476+LooperSalty@users.noreply.github.com>
@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Code-table address read back with v_readfirstlane from a uniform VGPR sum: main traces this (resource.rs reaches_pc through lane reads, VGPR adds and phis, CODE addresses; 27c3dc9).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
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.

6 participants