Skip to content

Pick the discrete GPU on laptops; fix driver-dependent and wrong shader lowering - #1483

Open
spy0eye-beep wants to merge 1 commit into
Marvisak:mainfrom
spy0eye-beep:fix/hybrid-gpu-and-driver-portable-shaders
Open

spy0eye-beep wants to merge 1 commit into
Marvisak:mainfrom
spy0eye-beep:fix/hybrid-gpu-and-driver-portable-shaders

Conversation

@spy0eye-beep

Copy link
Copy Markdown

What

Found on a hybrid laptop (Intel UHD Graphics + NVIDIA GeForce RTX 4060 Laptop GPU). One GPU-selection fix and six shader/runtime fixes. Each fix has a test that fails on main and passes here.

1. GPU: use the discrete GPU on laptops (victory-gpu, shader test host)

The host device was created with a null adapter, i.e. adapter 0, which on a laptop is the integrated GPU unless the user set a per-app preference in Windows. On this machine VIctory (and every "hardware" GPU test) ran on Intel UHD Graphics.
Now the first hardware adapter in DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE order that creates a D3D12 device is used, and its name is logged. Falls back to the old behaviour if DXGI cannot order adapters. Note: this overrides a user's "power saving" choice for VIctory in Windows graphics settings, which is the usual behaviour for games.
The shader test host does the same, and VICTORY_TEST_ADAPTER=n picks the n-th hardware adapter so the suite can be run on each GPU of a machine.

2. Shaders: SCC of s_min_* / s_max_* / s_lshlN_add_u32

SCC was set to result != 0. For s_min/s_max it is the comparison that picked the result (RDNA2 ISA); s_lshlN_add sets it to a carry, which is now Missing (fails loudly if read) instead of silently wrong.
Test: scalar_min_max_set_scc_to_the_comparison (on main: s_min_i32(2, 1) gives SCC = 1, want 0).

3. Shaders: a loop stopped by LOOP_LIMIT through a conditional back edge kept running

The limit was folded into the branch condition, so a lane at the limit took the loop's exit and continued through the program with unfinished values, possibly storing/exporting them, contrary to LOOP_LIMIT's doc ("leaves the program as killed"). Conditional back edges now go through a latch block that branches to the header below the limit and to the abort block at it (as Jump back edges already did); header phis take this edge's values from the latch via edges.
Test: loop_stopped_by_the_limit_stores_nothing (on main: stores 0x100000).

4. Shaders: NaN handling in v_cmp_*_f32/f16 independent of the driver

On Intel UHD Graphics, v_cmp_eq_f32 v1, v1 with a NaN gives true (the driver folds x == x). NaN operands are now found from the bits (|x| > 0x7F800000) and the float compare only orders numbers: ordered compares are false with a NaN, unordered ones true. No xor of a compare is emitted (see the old comment about the AMD driver hang): "not NaN" is its own compare (is_number). This also replaces the v_cmp_lg special case.
Test: v_cmp_f32_all_predicates_with_nan (all 13 non-constant predicates, two registers and one register against itself).

5. Shaders: v_med3_f32/f16

  • NaN detected from the bits: on Intel UHD v_med3_f32(1.0, 2.0, NaN) gave 2 (the existing v_med3_f32_matches_rdna2_with_nan fails there on main).
  • Built from max_min, so -0 orders below +0 like the hardware's min/max. On main WARP gives med3(+0, -0, 1) = -0, and Intel med3(+0, NaN, -0) = +0 (want -0).
    Test: v_med3_f32_orders_negative_zero_below_positive_zero.
    v_cvt_u16/i16_f16 also uses the bit test for NaN now (no failure observed; same pattern).

6. Shaders: comparison samplers

s.filter |= 0x80 on a filter that already holds a reduction in bits 7-8 turned a minimum sampler (0x100) into a maximum one (0x180). StaticSampler::make_comparison replaces the reduction.
Test: comparison_replaces_the_reduction.

7. HLE: XAsyncComplete race

For a call that ends (failure or no result) the call was unregistered before the status was published. A status check starting in between found no call, did not wait, and returned E_PENDING for a call that had ended. Status and unregistering are now done together under the CALLS lock (the call is removed only if it is still the same Arc, so a reused block is never touched). Behaviour change to note: the provider's OP_CLEANUP now runs while the call is still registered, before the status is published.
Test: waiting_never_sees_a_failed_call_still_pending (on main: returns E_PENDING).

Testing

Windows 11 Pro 26200, i7-13620H, 16 GB, NVIDIA GeForce RTX 4060 Laptop GPU + Intel UHD Graphics, Rust 1.98.1, MSVC 14.44.

RTX 4060 (VICTORY_TEST_ADAPTER=0) Intel UHD (VICTORY_TEST_ADAPTER=1)
cargo test --workspace --all-features 354 passed, 0 failed 354 passed, 0 failed
  • cargo clippy --workspace --all-features --all-targets -- -D warnings: clean
  • cargo xtask policy: 154 files ok
  • cargo build --release -p victory: ok
  • rustfmt: no new differences in the touched files

Not tested: an AMD GPU, and the title (no game data used).

AI-assisted (Claude).

🤖 Generated with Claude Code

…er lowering

Found on a hybrid laptop (Intel UHD Graphics + NVIDIA RTX 4060 Laptop GPU).
Each fix has a test that fails without it.

- GPU: the host device was created on the default adapter, which on a
  laptop is the integrated GPU unless the user set a per-app preference.
  Take the first hardware adapter in high-performance order instead. The
  shader test host does the same; VICTORY_TEST_ADAPTER=n picks the n-th
  hardware adapter so the tests can run on each GPU of a machine.
- Shaders: s_min/s_max set SCC to result != 0 instead of the comparison;
  s_lshlN_add's SCC (a carry) is now unmodelled rather than wrong.
- Shaders: a loop stopped by LOOP_LIMIT through a conditional back edge
  went down the loop's exit and on through the program; it now leaves
  through the abort block, as a Jump back edge already did.
- Shaders: v_cmp_*_f32/f16 find NaN operands from the bits. Intel UHD's
  driver folds `x == x` to true; the float compare now only orders numbers.
  No xor of a compare (it hung the AMD driver in a loop exit).
- Shaders: v_med3 detects NaN from the bits (Intel UHD gave the median, not
  the minimum, with a NaN) and orders -0 below +0 via max_min.
- Shaders: a comparison sampler replaces the filter's reduction bits instead
  of OR-ing 0x80 in (a minimum sampler became a maximum one).
- HLE: XAsyncComplete publishes the status and unregisters an ended call
  under the CALLS lock, so a status check starting in between no longer
  returns E_PENDING for a call that already ended.

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

Copy link
Copy Markdown

A Linux/Proton data point on AMD. With this PR, a main test makes the AMD driver crash under the vkd3d-proton that Proton Experimental ships today. A newer vkd3d-proton fixes it, and the PR's own tests pass.

Radeon 660M (RDNA2, RADV Mesa 26.3.1, waves 32..64), head 0daabcc:

main ffe310e this PR
waterfall_tests::lanes_load_from_the_buffer_their_nested_arms_chose, vkd3d-proton 3.1.0 (Proton Experimental 11.0-20261027b) pass process exits (code 3): 0xc0000005 inside libvulkan_radeon.so during vkCreateComputePipelines
the same test, vkd3d-proton 3.2.0 (GE-Proton11-14, cae8353) pass pass
whole victory-shader suite, vkd3d-proton 3.2.0 119 passed 125 passed, all 6 new tests included

Built on Linux with llvm-mingw for x86_64-pc-windows-gnullvm (Rust 1.98.1); dxil.dll from DXC 1.9.2609 was loaded. Synthetic tests only, no game files. AI-assisted (Claude Code, Opus 5.5); every run was on this machine.

@wwwwwwww3333333

Copy link
Copy Markdown
Contributor

Native Windows run on AMD RDNA2: RX 6750 XT (RDNA2, gfx1031), AMD driver 32.0.21045.29003, i5-13400F, Windows 11, Rust 1.98.1 MSVC. Head 0daabcc on main ffe310e (clean merge). Desktop with this one GPU, so the adapter pick has a single candidate.

crate this PR main alone
victory-shader 126 passed, 0 failed, 1 ignored 120
victory-gpu 51 passed, 0 failed, 1 ignored 50
victory-hle 95 passed, 0 failed 94

All the new tests pass, including high_performance_adapter_is_hardware, v_med3_f32_orders_negative_zero_below_positive_zero, v_cmp_f32_all_predicates_with_nan, loop_stopped_by_the_limit_stores_nothing and waiting_never_sees_a_failed_call_still_pending.

On @matheusfermiano-source's RADV crash: waterfall_tests::lanes_load_from_the_buffer_their_nested_arms_chose passes here with this PR, on AMD's own D3D12 driver. So on Windows AMD this PR doesn't crash the driver. That fits the crash being the vkd3d-proton 3.1 / RADV issue rather than something in this PR's DXIL.

Synthetic tests only, no game files. AI-assisted (Claude picked the PRs and wrote the batch script; I ran it on my machine).

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump, and the shader tests on Ada

Result: no count change against main.

Offline. I merged head 0daabcc onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on main, 21,724 with this PR.

No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on main. The count shows that the change rejects nothing. It does not show whether the output is right.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 126, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

simnJS added a commit to MiamiGame/XTP that referenced this pull request Nov 24, 2026
…sing group barriers (watch item 111)

Case 1 (Marvisak/VIctory#1483 fix 3): a conditional back edge folded the
limit into its branch condition, so a lane at the limit took the loop's
exit and stored what it had (WARP: 16384). Each back edge now goes through
a latch of its own that sends a lane at the limit to the abort block.
Ported from Marvisak/VIctory#1483 (head 0daabcc) by spy0eye-beep.

Case 2 (VItoriaBr 808a209): a lane the limit stops returns, and a group
barrier the others reach later would wait for it. In a compute or mesh
program where a group barrier can follow a loop, every lane that ends
(at the limit or at the end) counts itself ended in two group-shared
counts, by the parity of the barriers it has passed (an SSA count, block
to block), and passes group barriers until every thread has ended: all
return after the same barrier (item 94's form, reimplemented; VItoriaBr's
rule, dropping the sync, is not taken).

Outside flow.rs: the `drain` field of `Lower` and its initialisation
(lower/mod.rs, granted by the orchestrator), and the `mod` line of the
new loop_limit_tests.rs (emit/mod.rs).
@aycow

aycow commented Nov 24, 2026

Copy link
Copy Markdown

An independent Windows Agility 1.620 full-GBV test reproduced the MED3 signed-zero defect. The upstream original f32/f16 tests passed on both AMD Radeon Graphics and WARP. Across 90 operand/permutation rows, upstream WARP failed 11 asserted signed-zero cases, including (+0, -0, 1) returning -0. I extracted only the MED3 portion of your 0daabcc change onto ffe310e with your authorship. It passed the full asserted matrix on both devices and kept the signaling-NaN result classes identical to baseline. NVIDIA is currently absent from this machine's DXGI enumeration, so there is no result for the reported NVIDIA failure. The focused extraction is local; your existing work covers the fix.

AI-assisted using Codex.

@KevinPratap

Copy link
Copy Markdown
Contributor

Tested on NVIDIA: an RTX 4050 Laptop on Windows 11, with this pull request merged onto main (ffe310e).

  • cargo test -p victory-shader: 126 passed, 0 failed.
  • v_med3_f32_matches_rdna2_with_nan fails on main on this card: v_med3_f32(1.0, 2.0, NaN) = 2, where RDNA2 gives 1, because the driver folds the fcmp uno NaN test away. With this PR it passes, along with v_med3_f16_matches_rdna2_with_nan and v_med3_f32_orders_negative_zero_below_positive_zero.

I had opened #1689 with only the med3 part of this. I'm closing it in favour of this PR. AI-assisted.

@KevinPratap

Copy link
Copy Markdown
Contributor

Driver for the result above: NVIDIA GeForce RTX 4050 Laptop GPU, driver 621.24. In #1633, an RTX 4090 and an RTX 5070 Ti (driver 32.0.16.1742) did not reproduce the med3 failure on main. The folding seems to depend on the driver or the GPU, and this laptop shows it. Testing NaN from the bits, as this PR does, avoids it on all of them. AI-assisted.

@tumbubu

tumbubu commented Nov 24, 2026

Copy link
Copy Markdown

RX 7900 XT (RDNA3, driver 32.0.31036.15, the machine's only GPU): green.

0daabcc, cargo test -p victory-shader -p victory-gpu -p victory-hle --locked -- --test-threads=1:

Crate 0daabcc main (ffe310e)
victory-shader 126 passed, 1 ignored 120
victory-gpu 51 passed, 1 ignored 50
victory-hle 95 passed 94
  • The new v_cmp/v_med3 NaN tests pass on the 7900 XT and on WARP.
  • No hang on AMD: the commit names a loop exit that once hung the AMD driver, and none of the shader tests here hung.
  • high_performance_adapter_is_hardware passes. With one GPU there is nothing else for it to pick, so this run does not test the laptop case.

AI-assisted (Claude Code); every run was on this machine.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-gpu/src/host.rs, crates/victory-hle/src/modules/xasync.rs, crates/victory-shader/src/emit/host.rs, crates/victory-shader/src/emit/lower.rs, crates/victory-shader/src/emit/sampler.rs). It will be reviewed once it is rebased onto the current main.

On your reply: splitting it into one PR per fix is the right call, thanks. #2812 (comparison sampler reduction) and #2813 (s_lshlN_add SCC) are separate PRs and will be triaged on their own. This PR stays as needs-rebase until you close it once the remaining parts (LOOP_LIMIT latch, is_nan part, xasync on complete_call, adapter selection) are up.


Generated by Claude Code

@rminzao

rminzao commented Nov 25, 2026

Copy link
Copy Markdown

Thanks, the tests that fail on main make each change easy to check. A few things from reading the head (0daabcc):

Adapter choice, crates/victory-gpu/src/host.rs

  • :183-187: on current main the default branch also runs crate::tdr::log(). When rebasing, the new high-performance branch needs it too, or the TDR warning goes away on almost every machine.
  • :236-259: only DXGI_ADAPTER_FLAG_SOFTWARE is filtered out. main's shader host (hardware_adapter, ccfb029) documents that GitHub's runner lists Microsoft Basic Render Driver without that flag. The probe here only checks FL 12_0 device creation. It does not check SM 6.6 or D3D12_OPTIONS1.WaveLaneCountMin/Max. So "first adapter that makes a device" may be one that later refuses the console's wave32 pipelines, while a capable adapter further down the list is skipped. Checking SM 6.6 and WaveLaneCountMax >= 32 (and WaveOps) before accepting would make the fallback meaningful.
  • :255: the probe device is created, dropped, then recreated at :184. Returning the probe device (or passing a null out pointer to test support) avoids the second creation.
  • :185: only the name is logged, and the fallback branch (:187) logs nothing. Vendor/device id, LUID, driver (UMD) version and the wave lane range would make bug reports from hybrid machines easy to read. GPU: log the host GPU, and warn when it is not the one to run on #2053 adds that log on its own, without changing the selection, so it composes with whichever selector lands.
  • There is no override for the runtime: VICTORY_TEST_ADAPTER applies to the shader test host only. As the description says, this also overrides a user's per-app "power saving" choice in Windows. An env override (index or name substring, as GPU: adapter choice and logging, presenter backoff, a texture-description ladder, and a display-deferral test a headless session can pass #1630/GPU: choose an adapter that can run the title, and name the ones that cannot #967 discuss) would let a user go back.
  • :289-298, high_performance_adapter_is_hardware: the function already drops software adapters, so the assertion cannot fail. It doesn't check that the high-performance adapter is the one picked.
  • GPU: choose an adapter that can run the title, and name the ones that cannot #967 is the open PR for runtime adapter choice (the triage closed GPU: select high-performance D3D12 adapter and probe host caps #208 as its duplicate). It may be worth taking this part there.

Shader test host, crates/victory-shader/src/emit/host.rs

LOOP_LIMIT latch, crates/victory-shader/src/emit/lower.rs

  • :6900-6908 with :6911-6922: every conditional back edge now gets a latch whose limit branch goes to the single, memoized abort_block. Before, only Jump back edges did that. Now nearly every loop in a program shares one exit block (a critical edge from each latch), and each latch is a second exit out of all enclosing loops. That is not the dedicated-exit shape the comment at :6844 aims for. It may be what trips RADV under vkd3d-proton 3.1 (matheusfermiano-source's comment) in waterfall_tests::lanes_load_from_the_buffer_their_nested_arms_chose. A fresh abort block per latch (discard + ret, so cheap) would keep exits dedicated. The SSA itself looks right: next and the compare are computed in the branching block, which dominates the latch, and the data, lane-mask and descriptor phis all look up edges for (b, header).

NaN from the bits (:1474-1511, :5366-5417, :4652)

  • The predicate table looks right to me: ordered = number compare AND both numbers; unordered = number compare OR either NaN, including o/u/f/tru. f16 operands arrive as f32 through legacyF16ToF32, so the i32 mask is valid there. On rebase, main's med3 now goes through ftz + max_min_flushed (77053a0), so only the is_nan part of this change is still new.

Comparison sampler

  • sampler.rs:79: filter & !0x180 | 0x80 is right for D3D12's reduction bits 7-8. main's immediate_sampler_slots_distinguish_comparison_uses (mod.rs:779) still builds its expectation with | 0x80, so it should use make_comparison too.

XAsync (crates/victory-hle/src/modules/xasync.rs:605-624)

Scope: one commit with seven independent fixes across three crates, and five of the files conflict with main. Split into one PR per fix (each keeping its failing-on-main test), these would be much easier to review and land. Several are a few lines each (make_comparison, s_lshlN_add SCC).

Static review (AI-assisted), read against the PR head 0daabcc; nothing was run on a GPU.

@spy0eye-beep

Copy link
Copy Markdown
Author

Thanks for the thorough review. Splitting this up on current main as asked:

Still to come, each as its own PR: the LOOP_LIMIT latch with a fresh abort block per latch, so dedicated exits are kept; the is_nan part only, since med3 now has max_min_flushed; xasync rebuilt on complete_call so cleanup never runs while done is held; and adapter selection with the SM 6.6 and wave-lane checks, tdr::log, and an env override, coordinated with #967 and #2053. I'll close this one once those are up.

🤖 Generated with Claude Code

amirshaker000 pushed a commit to amirshaker000/PAG6Port that referenced this pull request Nov 25, 2026
The host device and the shader test host were made on the default adapter,
the integrated GPU on a laptop with two. Now the first hardware adapter in
DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE order that makes a D3D12 device is
used, its name logged; software adapters (WARP, the Basic Render Driver)
never. VICTORY_TEST_ADAPTER=n runs the tests on the n-th hardware adapter.
Same change as PastiiJay/klbf-lab#22 and Marvisak/VIctory#1483.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot pushed a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
The comparison bit was OR-ed into a filter that already holds the
reduction in bits 7-8, which turned a minimum sampler (0x100) into a
maximum one (0x180). StaticSampler::make_comparison replaces the
reduction; comparison_replaces_the_reduction covers standard, minimum and
maximum reductions. immediate_sampler_slots_distinguish_comparison_uses
builds its expectation with it too.

Split out of Marvisak#1483.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants