Skip to content

Shaders: lane masks spilled to VGPR lanes and read back - #298

Closed
permadiego wants to merge 1 commit into
Marvisak:mainfrom
permadiego:shader/lane-mask-spills
Closed

permadiego wants to merge 1 commit into
Marvisak:mainfrom
permadiego:shader/lane-mask-spills

Conversation

@permadiego

@permadiego permadiego commented Nov 24, 2026 •

Copy link
Copy Markdown

What changed

The console compiler spills lane masks like any other SGPR: v_writelane_b32 to a constant lane, then v_readlane_b32 of that lane later. In CS_RaytracingResolve, for example, a compare result (v_cmp_eq_f32 s7, 0, v21) is spilled to lane 1 of v128. Thousands of instructions later it is read back and fed to s_and_saveexec.

Two things failed on that pattern:

  • The lane pass took the read-back value for an opaque mask.
  • The spill write read the mask as data. This fails for a mask holding lanes that are not running there, such as the complement s_xor_b32 s8, s1, -1: "a lane mask holding lanes elsewhere read as data".

Changes:

  • structure.rs: a v_readlane_b32 of a constant lane is taken as the mask itself when it reads back a v_writelane_b32 of that mask. The lookup follows writes of other lanes and joins whose arguments agree, and only applies where the mask's value dominates the read. Each lane's bit is then the one it had when spilled. A wave64 mask needs both dwords read back from the same mask.
  • lower.rs (mask_spills): a read-back of this kind leaves its value to the mask's readers. The spill write is left out (the VGPR keeps its old value) when that VGPR lane is read only by such reads; no vector instruction or computed-lane read sees it. Neither instruction counts as reading the console's lanes.

This no longer depends on #54, which was closed. It applies on main alone.

Measured

victory translate --validate over the title's shaders.rpf, main ffe310e against this branch (54f97db):

main this branch
compute validated 1,417 1,424
vertex and pixel validated 12,155 12,182
mesh validated 8,152 8,152
"a lane mask holding lanes elsewhere read as data" 273 13

34 programs gain, 0 lose. Most CS_RaytracingResolve variants now stop at "a descriptor read only as data", which #180 covers.

cargo test -p victory-shader -- --skip valu_tests --skip wave_tests: 105 passed. The skipped tests and the two waterfall_tests need an SM 6.6 device, which this machine lacks (sampled() returns nothing). Clippy is clean.

Not verified

No GPU run here. @qntic's lane-spill hardware test and @Nixitov's world run were done on the earlier, #54-based head d0261f1, so they need a re-run on this head.

🤖 Generated with Claude Code

@SpaghettiMaster545

Copy link
Copy Markdown

Tested with the title at d0261f1. The branch also carries #54's three commits, so this measures #54 + this PR together.

Not tested: #54 alone, AMD GPUs, a release build, image results.

(Claude Code, Claude Opus 5.5: ran the title on both builds and compared the logs program by program.)

@jeremygold02

Copy link
Copy Markdown
Contributor

Tested PR #298 at head d0261f1174368e2c203b42942e19b24f8f8d7787 (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at d0261f1174368e2c203b42942e19b24f8f8d7787.

  • cargo test --locked -p victory-shader wave64_64thread_sgpr_spills_run_on_wave32_host — passed (ok. 1 passed; 0 failed; 0 ignored; 0 measured; 73 filtered out; finished in 0.59s ); cases: emit::wave_tests::wave64_64thread_sgpr_spills_run_on_wave32_host.
  • cargo test --locked -p victory-shader pixel_shader_sgpr_spill_lowers_and_draws — passed (ok. 1 passed; 0 failed; 0 ignored; 0 measured; 73 filtered out; finished in 0.32s ); cases: emit::wave_tests::pixel_shader_sgpr_spill_lowers_and_draws.

Behavior covered: Checks wave64 SGPR spill lowering on a wave32 host and pixel-shader SGPR spill drawing behavior; the latter cases require a supported host GPU.
Code: crates/victory-shader/src/emit/lower.rs, crates/victory-shader/src/emit/wave_tests.rs, crates/victory-gpu/src/host.rs

@GamingRipperDoc

Copy link
Copy Markdown

A combination finding, from victory translate <dump> --validate (offline, dxil.dll validator) on Windows 10 + RX 9070 XT, base d2a4bae + #175 + #336. Counts are compute programs, out of 2,581.

on the base emitted validated Entry function performs some operation that is incompatible with the shader stage or other entry properties
base 1,693 1,670 23
+ this PR (with #54) 1,731 1,698 33
+ #136 1,796 1,697 99
+ #136 + this PR 2,332 1,725 607
+ #193 + #437 + this PR 2,442 1,795 647
+ #193 + this PR 1,822 1,768 54

On its own this PR is clean (+38 emitted, +10 of these errors). But together with #136, or with #437 (both let more scalar buffer loads through), ~540 more compute programs get through this PR's path and come out failing validation with the error above. So it looks like a shape #136/#437 expose that this PR's lowering doesn't handle validly in compute. The validator only prints the summary line here ("See other errors for details" without others).

Why it matters: VIctory doesn't validate at run time, so in game these programs go to the driver. In a play run with this PR + #136 + #305 + #385 + #302 + #193 + #437 + #54 on the stack, AMD's shader compiler crashed (amdxc64.dll+0x85fd0f, access violation reading 0x8) after ~1 min in the world, with 250–1,000 pipelines still compiling. I can't say which program it was compiling, but this stack sends ~1,200 non-validating programs to it (these, plus #305's).

I can share the per-program --list output for the combination privately if it helps (it names the title's shaders, so not here).

No game data in this comment.

AI-assisted (Claude Code); I ran the measurements on my machine.

@Nixitov

Nixitov commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Big effect on the RTX 5070 world run — this is the single most valuable shader PR I have tested.

Setup: Windows 11 Pro 26200.9974, RTX 5070, driver 617.14, d2a4bae + the GPU stack #493 + #483 + #581 + #595, release build. Same scene (first mission intro), same run length.

build shaders that do not translate a lane mask holding lanes elsewhere read as data draws dropped without a host pipeline
without this PR 399 85 1408
with this PR (#298) 370 10 —
with this PR + #585 + #259 + #302 + #307 + #539 361-362 10-11 952

(399 → 952 dropped draws is not a like-for-like ratio; the two dropped-draw numbers are the last frame line of runs at the same scene, with and without the stack.)

The count of the reason this PR targets drops from 85 to 10; the 97 a select on an untracked mask entries disappear with #585 on top. Nothing regressed in the run: still 2 fps, no device removals, and the frame log reaches the same intro scene.

One integration note: merging #298 with #54-era code needed only two additive hunks in lower.rs (guard_slots next to resolved_reads / pure_spill_writes, and both in the constructor), plus deriving guard_slot from count + dynamic when stacked on #483.

No game files; only counts and log lines.

AI-assisted (DeepSeek V4.1 Flash coding agent).

Kam471 pushed a commit to Kam471/VIctory that referenced this pull request Nov 24, 2026
Brings in Marvisak#704/Marvisak#193 raw-view stride table and VSH3 cache, Marvisak#329 PSV record counts, and precise copy invalidation. Kept Marvisak#54's host wave range and Marvisak#298's spilled-mask resolution; Parts now carries psv, options and host_waves, and test initializers cover all three.
@qntic

qntic commented Nov 24, 2026

Copy link
Copy Markdown

tested on rx 6600 xt with my compute regression harness. the vgpr lane mask spill test gives 24 incorrect lanes out of 64 on main. with this pr all 64 lanes produce the expected values.
hardware tests:
https://github.com/qntic/VIctory/tree/hwtest/compute-lanes

The console compiler spills lane masks like other SGPRs: v_writelane_b32
to a constant lane, v_readlane_b32 of that lane later. The lane pass took
the read-back value for an opaque mask, and the spill write read the
mask as data, which fails for a mask holding lanes not running there
("a lane mask holding lanes elsewhere read as data").

structure: a v_readlane_b32 of a constant lane that reads back a
v_writelane_b32 of a mask (through writes of other lanes and agreeing
joins), where the mask's value dominates the read, is that mask: each
lane's bit is the one it had when spilled. Wave64 masks need both dwords
read back from the same mask.

lower: such a read leaves its value to the mask's readers, and a spill
write of a mask is left out where that VGPR lane is read only by such
reads (no vector instruction or computed-lane read sees it). Neither
counts as reading the console's lanes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@permadiego
permadiego force-pushed the shader/lane-mask-spills branch from d0261f1 to 54f97db Compare November 24, 2026 11:42
@permadiego permadiego changed the title Shaders: lane masks spilled to VGPR lanes and read back (stacked on #54) Shaders: lane masks spilled to VGPR lanes and read back Nov 24, 2026
@permadiego

Copy link
Copy Markdown
Author

Rebased onto main ffe310e as 54f97db, now standalone: #54 was closed, so the spill handling this needs for masks is in this PR (mask_spills in lower.rs). Over shaders.rpf against main: +34 validated, 0 lost, "a lane mask holding lanes elsewhere read as data" 273 → 13. Tests done on d0261f1 (thanks @qntic, @Nixitov, @jeremygold02) were on the #54-based head.

🤖 Generated with Claude Code

@qntic

qntic commented Nov 24, 2026

Copy link
Copy Markdown

re-tested on the rebased head 54f97db on my rx 6600 xt.

the mask spill test still produces correct values, but the select pattern after vcc_hi = 0 still silently produces incorrect values in 16 of 64 lanes.

#660 is still needed to safely reject that pattern.

harness:
https://github.com/qntic/VIctory/tree/hwtest/compute-lanes

@mercuryVM mercuryVM 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 54f97db on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 120 passed, and cargo clippy -p victory-shader --all-targets is clean.

Measured on the title's shaders.rpf with victory translate --list, counts only, against upstream main ffe310e (this PR's base):

  • "a select on an untracked mask": 289 → 0.
  • Emitted: vertex/pixel 12,231 → 12,258, compute 1,441 → 1,458, mesh unchanged (8,214).
  • Most of the programs it unblocks now stop at the next refusal, "a descriptor read only as data", which #180 addresses (combined numbers on #180).

Code:

  • Taking a v_readlane_b32 of a constant lane as the mask itself only when it reads back a v_writelane_b32 of that same mask matches the console compiler's spill pattern. The lookup follows writes of other lanes, and joins only when all arguments agree.
  • Accepting the spill write of a mask whose lanes are elsewhere, instead of reading it as data, fixes the complement case.
  • Overlap: #309 (approved) forwards spilled SGPRs from VGPR lanes generally, in a pre-pass (sgpr_spill::forward). It covers the same pattern by another route, so whichever lands second should be checked against the other. If #309 lands, this PR's structure.rs lookup may become redundant for masks.

AI-assisted (Claude Code).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
…anes and read back

Conflict in emit/lower.rs (console_lanes): this PR's spill filter plus the fork's v_permlane entry (Marvisak#576).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
Merges the PR's current head (54f97db). Conflicts: the Lower and Lanes
fields keep both sides (`spin`/`spun` and `mask_spills`; `console` and
`idom`); the compute console-lane test, split here into `whole` and
`in_half`, leaves the spill writes and read-backs out of `whole` as the
PR did. A structure test covers a wave64 mask spilled to two lanes and
read back, and a read of a lane the spill did not write.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
Upstream PR #298 (lane masks spilled to VGPR lanes and read back), the
two mesh commits (a lane mask's "has lanes" test and reductions over the
host's wave) and PR #269 (scalars leave divergent loops and branches as
the wave's last path left them). #269's new tests pass here on the RTX
4060 Ti (32 lanes), so none is ignored.

Conflicts:
- lower.rs: both sides' fields and helpers (guard_slots and mask_sccs
  with #298's mask_spills, mask_set_op and only_scc_read); the lane-read
  test skips both our SGPR spills (`dead`) and #298's mask spills; the
  mask SCC arm and #298's LANE_MASK arm side by side; data joins try
  the lazy barycentric first, then take #269's loop finals.
- structure.rs: our console_pending with #269's idom; paired_masks stays
  where our tree sets it last.
- valu_tests.rs: both test sets.
- sgpr_spill.rs: a spilled else-mask that the per-lane form refused
  unforwarded now lowers there, since #298 takes the reload for the mask
  itself; the test asks for that, and for the console flow or the
  per-lane form when the lowering may choose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rafaconvaitis

Copy link
Copy Markdown

I tested 54f97db independently on the 45 mesh programs that reached a lane-mask join of an untracked mask only after the narrow descriptor-field experiments in #798. The common base was #1269 8c3092f plus #953 c42cbb0, with the same experimental descriptor-bit and offline stride settings in both runs. The candidate applied only the official #298 patch. I matched the same program records across both conditions.

  • Control: the same lane-mask join refusal on all 45 mesh programs.
  • With 54f97db: all 45 passed Microsoft DXIL validation with the 1.8.2502.8 validator.

In one representative the two restored halves both trace to a single dominating conditional EXEC mask, saved in two constant lanes of the same VGPR. That matches the spill recovery this PR implements. I only tested these 45 offline. In-title binding, draws, and the separate high-half correctness case in the RX 6600 XT report remain unverified by this test.

AI-assisted testing. No title files, shader containers or disassembly are attached.

Jarrrk pushed a commit to Jarrrk/VIctory that referenced this pull request Nov 24, 2026
A lane mask the console compiler spills with v_writelane_b32 and reads
back with v_readlane_b32 no longer stops the program: the structuriser
reconstructs each lane's bit from the dominating write, and the emitter
drops the spill and read-back, whose readers take the lanes' bits.

Ported from Marvisak/VIctory Marvisak#298.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
Taken from try/wave64:
- #189 Wave64Mode (native / wave32 / emulate) and its LDS emulation of
  64-lane waves (its helpers in emit/lower/wave64.rs, keeping lower.rs
  under the 512 KiB policy limit), selected with VICTORY_WAVE64_EMULATE
  (emulate|1|force, wave32|32, native|0, auto). Default (auto) is Native,
  which keeps our Target-based path; the mode is part of the shader cache
  key. Their wave64 tests (wave64_tests.rs, host recommendation test).
- #940 mesh lanes counted the host's way when an emulate mode is chosen.
- Wave scalars and whole-wave sections: sections skip dead SGPR spills
  as ours did; wave scalars stay off under the emulate modes.
- Translator inferences: a descriptor read only as data, and a scalar use
  point, infer the view (ported onto our descriptor_data).
- 1 MiB windows for title memory outside buffers (shadow.rs).
- Trace every mesh draw (draw.rs); WARP adapter check in the test host.
- #737 mip drop floor: VICTORY_TEXTURE_MIP_DROP_MIN_SIZE now defaults to
  2048 (spares the 1920x1080 artwork).

Kept from our side:
- Target (--host-lanes, host wave sizes), split halves, console flow,
  whole-wave sections with SGPR-spill (dead) filtering, lane-mask
  lowering, #298, #269, waterfall, wave scalars (off under emulate modes).
- Our VICTORY_TEXTURE_MIP_DROP (#1296: auto for <= 8 GB, NPOT exemption,
  subresource mapping and copy routing around dropped mips, SRV mip
  adjustment): list.rs, resource.rs, texture.rs, descriptor/host.rs are
  ours apart from the floor default.
- Shader cache: VSH3 magic, KeySpec/store for offline warming, cache
  errors after a good translation, target key (+ wave64 mode byte).
- Write-watch journal, CommittedRegions, bindless plans, copystat
  counters, compute early start, #1388 queue thread, #1418, #704 raw
  view pool/stride table, the 16 MiB stack in build.rs.

Left out: #729 Agility SDK opt-in (build.rs exports, agility.rs, README
note): without D3D12Core.dll beside the exe the runtime throws
D3D12_ERROR_INVALID_REDISTRIBUTABLE at startup, and Windows 11 needs none.
Their host-recommended Wave32 default (it never remapped lanes on NVIDIA).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Sil3ntGam3r

Copy link
Copy Markdown

@permadiego results for 54f97db on top of the #439 stack. It gains programs there too and loses none.

Tree: main + #180 aa69258 + #439 bf0f951 (with #223/#233/#320) + #431 + #876 + #334, then this PR, then #660 19b02ec. The merge conflicts were all additive: both sides' fields in lower.rs, and one duplicate paired_masks initialiser in structure.rs. All 433 tests pass. RTX 3070, driver 621.14, Windows 11, diag build.

Offline, victory translate --validate --host-lanes 32-32 over shaders.rpf, the stack without and with this PR:

without with this PR
vertex and pixel validated 12,288 12,315
compute validated 2,363 2,371
mesh validated 8,236 8,283

82 programs gain and 0 lose. Before this PR they stopped at:

reason before programs
a lane-mask join of an untracked mask 47 mesh, 15 pixel
an opaque lane mask 12 pixel
a program the console structure cannot hold 7 compute
a descriptor's address or format read as data 1 compute

On this stack, "a lane-mask join of an untracked mask" goes from 62 programs to 0. Your PR's own reason, "a lane mask holding lanes elsewhere read as data", stays at 4 both ways, so the stack already took most of those. The 47 mesh programs fit @rafaconvaitis's 45 above. Adding #660 on top changes no program's outcome, as @rpmaster2 found on main in #660.

The CS_RaytracingResolve variants on this stack: 27 move from "a lane mask read as data" to "a program the console structure cannot hold" (at 0x0). That refusal comes from #439's console flow, not from main. They are refused either way. With #180 on the stack, they don't reach "a descriptor read only as data" as they do on main.

In the title, Series X, cold cache, into the world until translation caught up:

  • 5 programs that were refused in the run before this merge now run: 3 pixel (b37565055f2f7d3b, cc6def9058a4bc0a, d642969b6ff7579e) and 2 compute (015832d72477fb26, e81277c8be37103b).
  • No program that translated in that run is refused now. The earlier run ended before translation caught up, so this covers only programs that have a result in both runs. The offline table covers everything.
  • What reaches the screen is unchanged: scenery in the G-buffer, no ground, roads or buildings. No device removal.

Not tested: AMD, a release build, this PR on plain main.

(AI-assisted: Claude Code ran the builds and compared both runs program by program.)

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.

10 participants