Skip to content

Shaders: trace multi-term, scaled, masked, and phi-joined addresses in resource tracking - #645

Closed
nathanbzrr wants to merge 1 commit into
Marvisak:mainfrom
nathanbzrr:shader/resource-address-tracing
Closed

nathanbzrr wants to merge 1 commit into
Marvisak:mainfrom
nathanbzrr:shader/resource-address-tracing

Conversation

@nathanbzrr

Copy link
Copy Markdown
Contributor

Summary

Fixes five resource-address tracking failures in crates/victory-shader/src/resource.rs and adds SSA-dominance-safe relative offset lowering in crates/victory-shader/src/emit/lower.rs blocking compute, raytracing/BVH, and multi-term buffer shaders:

  1. Defect A: RootType::Constants dword 0 was treated as a pointer base whenever param_of saw dword 0, colliding with real base pointers ("an address not from a root parameter", "an offset from a root parameter", or "an address from a root parameter that is no address").
  2. Defect B: Adding more than one dynamic term ((base + row) + col, or base + dyn plus soffset/vaddr) discarded base.
  3. Defect C: Standard GFX10 address instructions (s_addk_i32, s_lshl{1..4}_add_u32, v_lshl_add_u32, v_add3_u32, v_add_nc_i32, subtractions on the minuend, aligned constant s_or_b32/v_or_b32, and address-local v_mov_b32 -> v_readfirstlane_b32/v_readlane_b32) were untraced.
  4. Defect D: High-half validation rejected v_add_co_ci_u32(want, 0, _), skipped table dynamic carries (high == None), and did not strip VA-preserving and masks (mask & 0xFF == 0xFF).
  5. Defect E: Control-flow joined (if/else) and loop-carried induction pointer Phis failed immediately with "an address from a phi".

Changes (crates/victory-shader/src/resource.rs, emit/lower.rs, emit/valu_tests.rs)

  • Add RelOffset { value, base, extra_dynamic } to Address (keeping Address: Copy and keeping dynamic populated for downstream readers such as translate.rs).
  • Restrict root-parameter pointer bases in sum_value to Table, ResourceHeap, SamplerHeap, and Descriptor.
  • Add memoized DAG sum tracing with cycle-safe Phi base validation (requiring at least one non-cyclic edge and a matching base across all non-cyclic edges), multi-term retention (multi: bool), and all listed GFX10 address ops without double-counting s.offset or extra_dynamic.
  • Update high_half, carried(Term, Term), and Source::Built mask stripping (set_bits) to accept v_add_co_ci_u32, Term::Const(0) table carries, VA masks, and high-half phis.
  • Implement address_offset and rel_offset in emit/lower.rs with per-call local caching for non-phi expressions and global addr_phis caching resolved in fill_phis to preserve DXIL SSA dominance across blocks, wired into flat(), code_smem(), and built_offset().
  • Add 5 unit tests in resource.rs and 3 end-to-end DXIL + D3D12 GPU/WARP compute dispatch tests (root_srv_*) in emit/valu_tests.rs.

Verification

Isolated verification on branch shader/resource-address-tracing (499bd98):

  • rustfmt --edition 2024 --config skip_children=true --check crates/victory-shader/src/resource.rs crates/victory-shader/src/emit/lower.rs crates/victory-shader/src/emit/valu_tests.rs (passed)
  • cargo test -p victory-shader -- --test-threads=1 (77 passed, 0 failed, 0 ignored)
  • VICTORY_D3D12_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-shader root_srv_ -- --test-threads=1 (3 passed, 0 failed, 0 ignored, 74 filtered out)
  • cargo clippy -p victory-shader --all-targets (0 warnings)
  • cargo xtask policy (151 files ok)

No game files. AI-assisted; reviewed and verified with the tests above.

@LAKSHYA-cmd-tech LAKSHYA-cmd-tech left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on Windows 11 with Rust 1.98.1 (x86_64-pc-windows-msvc).

  • cargo test -p victory-shader: all 77 tests passed cleanly (0 failed).
  • All 5 resource address tracking tests ( wo_term_dynamic_addresses_produce_rel_offset,
    oot_constants_dword0_is_offset_not_base, scaled_subtracted_add3_addk_and_aligned_or_addresses, high_half_carry_va_mask_and_table_carry, and phi_joined_and_loop_carried_addresses) passed.
  • All 3 end-to-end DXIL compute dispatch tests (
    oot_srv_s_lshl2_add_u32_with_root_constant_dword0,
    oot_srv_two_term_v_add_co_and_v_lshl_add,
    oot_srv_loop_carried_pointer_induction_phi) passed.
  • cargo xtask policy: 151 files ok.
  • Verified DAG sum tracing with cycle-safe Phi base validation, SSA-dominance-safe relative offset lowering in emit/lower.rs, and GFX10 address op lowering. Resolves key blockers for raytracing/BVH compute shaders in #614.

@xiao47576-glitch

xiao47576-glitch commented Nov 24, 2026 •

Copy link
Copy Markdown

I tested PR #645 (499bd98) against main (d2a4bae) on Windows Server 2025 using D3D12 WARP. Main rejected a root SRV plus a byte offset from the first root-constant dword. On this PR, both operand orders read the expected values at offsets 0, 4, 48, and 112 bytes, plus a fixed 12-byte load offset.

I found one carry case the tracker still accepts:

s_add_u32    s0, s4, s10
s_cmp_eq_u32 s10, 0
s_addc_u32   s1, s5, 0

Here s[4:5] holds the root SRV address and s10 is the byte offset. When the offset is zero, the comparison sets SCC to 1 even though the low add produced no carry. The tracker also accepts a high add with immediate 1 and a signed low add.

Download the tested source patch

With the patch applied, the tracker rejected all three cases. All 12 valid WARP reads returned the expected values, and all 78 shader tests passed. Only the valid reads were dispatched on WARP. The effect on the opening cutscene in #255 is still untested. AI-assisted.

@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

Offline translation and validation of 499bd98 against main at d2a4bae: +32 programs validate, 0 regressions.

Setup:

  • The title's shaders.rpf plus its executable and 16 DLLs: 26,148 programs.
  • victory translate --validate --list, validated with DXC 1.9 (1.9.2609.5).
  • Built on Linux. The base is main plus Build: gate Windows-only paths so the workspace builds and tests on Linux #235 for the Linux build, and a local hook that runs dxv off Windows; neither touches translation.
  • Offline only: no pipelines, no title run.
Stage main: emitted / validated #645: emitted / validated change
compute (of 2,581) 1,693 / 1,670 1,721 / 1,698 +28 / +28
mesh (of 8,506) 8,191 / 8,129 8,191 / 8,129 +0 / +0
vertex and pixel (of 14,013) 12,284 / 12,184 12,288 / 12,188 +4 / +4

What changed:

  • Regressions (validated on main, not with this PR): 0.
  • Outcome changes: 32 programs go from refused to validated, and no other program changes outcome.
    • 24 were refused with resources: an address from a phi (Defect E).
    • 8 were refused with resources: an address not from a root parameter.
  • Reason changes on programs still refused:
    • One program moves from an address not from a root parameter to emit: a descriptor read only as data. It was refused on main too, so this isn't a regression.
    • 4 compute programs are still refused with an address from a phi. All 4 are in the same shader group and fail at nearly the same offset (0x508/0x510), so they are likely one pattern the phi tracing doesn't yet accept.

Caveat: these numbers are from main at d2a4bae.

  • main has moved since (now dd273df).
  • This PR no longer merges cleanly: crates/victory-shader/src/emit/valu_tests.rs conflicts with Shaders: lower v_cmp_class_f16 and v_cmpx_class_f16 #140 (v_cmp_class_f16), which first appears at merge 80c0d12.
  • I didn't resolve the conflict myself, so I haven't measured this PR on current main. I can rerun after a rebase.

@slint6614-dev

Copy link
Copy Markdown

Measured on the full retail shader pack (26,503 containers) with victory translate --validate --pipelines (pipelines created on an AMD RX 9070, Windows 11, dxil.dll from SDK 10.0.26100). Counts are emitted / validated / pipelines. This PR conflicts with current main (lower.rs), so it was measured on its own base, against main 1f858b0 before #612 (VS/PS 12272 / 12172, CS 1693 / 1670, MS 8191 / 8129).

CS 1721 / 1698 (+28 validated): "resources: an address from a phi" 28 → 4 and "an address not from a root parameter" 73 → 64. VS/PS +4 validated, MS unchanged.

MfDev0 referenced this pull request in MfDev0/victory Nov 24, 2026
A title heap of 2^20 descriptors plus the 2^18 dynamic range asked for a
1,310,720-view shader-visible heap. NVIDIA creates it without an error,
but the runtime refuses it (debug layer Marvisak#645, CREATE_DESCRIPTOR_HEAP_INVALID)
and the device is removed once it is used (upstream Marvisak#735,
RTX 4060 Laptop). The sizing only fell back when creation failed, so it
never did there.

Heap sizes are now planned against the device's limit: what
D3D12_OPTIONS19 reports as MaxViewDescriptorHeapSize (Agility runtimes),
else the spec's 1,000,000. The larger dynamic range where it fits on top
of every title slot, else the smaller one, else the title's slots are cut
so the smaller one still fits, as the old cap did.

The diagnosis is LAKSHYA-cmd-tech's (fork branch
fix/gpu-resource-format-flags-and-heap-limit); this takes the limit from
the device instead of a constant.

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

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conflicts with current main (ffe310e) — resource.rs (+1326/-96) and lower.rs (+593) are both hot after the recent shader merges; needs a rebase before anything else.

I can't responsibly approve a 2329-line tracer rework on a conflicted base without running it, and the merged tree is exactly where the five "Defect A-E" fixes interact with #566's waterfall changes. The structure (SSA-dominance-safe relative offsets, multi-term/scaled/masked/phi-joined address tracing) sounds like the right direction for the resource-tracking failures, and the +295 lines in valu_tests.rs suggest host coverage on Windows.

After a rebase I'll run cargo test -p victory-shader --locked on macOS and do a full pass on the tracer invariants (the sums cache from #139 and the dominance reasoning are the parts most likely to interact badly with other in-flight PRs).

@pablogonz12

Copy link
Copy Markdown

Measured on Linux (RX 9070 XT, RADV/Mesa 26.3.1, GE-Proton11-14, vkd3d-proton master), head 499bd98 merged onto a tree that already carries other merged fixes (its offline base is 692 of 751 of the world shader set, not main's number):

  • offline victory translate of the 751-program world set: 692 -> 700; all 700 pass dxil-spirv and spirv-val --target-env vulkan1.3 and build a RADV pipeline.
  • failure classes: "an address from a phi" 6 -> 0, "not from a root parameter" 3 -> 1; the other classes are untouched.
  • cargo test -p victory-shader --lib: 122 pass, 1 known pre-existing failure, 1 ignored; no new failures.
  • cargo xwin clippy --workspace --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.
    Not tested: in-game on this branch; native Windows.

…n resource tracking

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
#	crates/victory-shader/src/emit/valu_tests.rs
#	crates/victory-shader/src/resource.rs
@nathanbzrr
nathanbzrr force-pushed the shader/resource-address-tracing branch from 499bd98 to 6817902 Compare November 24, 2026 21:55
@nathanbzrr

Copy link
Copy Markdown
Contributor Author

Rebased onto origin/main (ffe310e) at 6817902 and addressed the scalar-carry review finding.

@catminou4 @xiao47576-glitch

Rebase integration (ffe310e)

  • Integrated #566 waterfall tracking/lowering in crates/victory-shader/src/resource.rs and crates/victory-shader/src/emit/lower.rs, preserving the 4-argument resource::track(..., st) signature (#801).
  • Merged #612's read_phis worklist in emit/lower.rs (fill_phis) alongside pending_addr_phis.
  • Kept #140 (v_cmp_class_f16_matches_rdna2) and wrapped run_root_srv_cases with ONE_AT_A_TIME.lock() (#433) in crates/victory-shader/src/emit/valu_tests.rs.

Soundness fix (s_addc_u32 / s_subb_u32 carry)

  • Updated high-half carry matching in crates/victory-shader/src/resource.rs so s_addc_u32 and s_subb_u32 require:
    1. a zero constant addend (Term::Const(c) if c == 0, rejecting non-zero immediates), and
    2. an scc value traceable to a matching s_add_u32 on the corresponding low-half value (including loop-carried phi induction chains), rejecting clobbered scc producers (s_cmp_eq_u32, s_add_i32, or unrelated s_add_u32).
  • Added negative test coverage in resource::tests::scalar_carry_requires_matching_add_u32_scc_and_zero_const.

Verification (6817902)

  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/valu_tests.rs: clean (resource.rs and emit/lower.rs only retain pre-existing upstream formatting diffs on origin/main).
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean.
  • cargo test --workspace --locked: 351 passed; 0 failed; 6 ignored (victory-shader: 129 passed; 0 failed; 1 ignored; xtask: 4 passed).
  • VICTORY_WARP=1 cargo test -p victory-shader --locked -- root_srv: 3 passed; 0 failed.

@nathanbzrr

Copy link
Copy Markdown
Contributor Author

Closing this PR as superseded by recent main (70564ae):

  • 27c3dc9 (Shaders and GPU: title memory at raw addresses through a page table, decision 0005) and 0f63757 (GPU: root views of programs that use title memory go through the page table) now intercept the data-computed address failure classes in Tracer::address() (including "an address from a phi", "an address with two computed terms", "an address with an untracked high half", "an address not from a root parameter", and "an address from a root parameter that is no address") and route them through the MEMORY page table, alongside USER_DATA handling for spilled compute root parameters.
  • Maintaining the pre-27c3dc9 ~2,388-line tracer rework would conflict with decision 0005 and 0f63757's root-view page-table aliasing model; any remaining descriptor-heap address edge cases can be targeted cleanly in a focused follow-up PR on main.

Thanks @catminou4 and @xiao47576-glitch for the detailed reviews!

@nathanbzrr nathanbzrr closed this Nov 24, 2026
@nathanbzrr
nathanbzrr deleted the shader/resource-address-tracing branch November 24, 2026 22:23
geiyiliyabumo pushed a commit to geiyiliyabumo/VIctory that referenced this pull request Nov 25, 2026
Codex P1 on Marvisak#645: kind and four are i32, so the leaf test must use
pred::IULT, not the floating-point unordered ULT.

Refs Marvisak#474.

Co-authored-by: JMAN730 <JMAN730@users.noreply.github.com>
geiyiliyabumo pushed a commit to geiyiliyabumo/VIctory that referenced this pull request Nov 25, 2026
…-miss-474-111a

Shaders: lower image_bvh intersect as a miss (Marvisak#474)
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