Skip to content

Recompiler: comparison samples use their supplied pixel gradients - #350

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/ps-compare-grad
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/ps-compare-grad

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Pixel-shader image_sample_c_d* and image_sample_c_cd* now use the instruction’s gradients for depth comparisons. Main selected the mip from screen derivatives even when the instruction requested a finer or coarser footprint. This overlaps #247 in emit.rs and builds directly on main.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs — Uses the instruction’s X and Y gradients, keeping array layers discrete and projecting cube-face coordinates back to a direction. At ps_6_6, QuadReadAcrossX and QuadReadAcrossY on SV_Position identify the pixel in the quad. An unrolled four-iteration loop uses QuadReadLaneAt for the coordinate and both derivatives, then issues one native SampleCmp per iteration. Each pixel selects its own result.
  • crates/xcl/src/gpu/recomp/tests.rs — six 16×16 raster draws check coarser, finer and per-pixel gradients, the coarse-derivative zero-offset form, matching gradients and a minimum-LOD clamp against two opposite depth checkerboards with point mip filtering.

Tested

  • Head 08866f9 is f09eca5's two commits squashed into one, with the same tree (git diff f09eca5 08866f9 is empty). f09eca5 is bf32301 rebased from 9ab7d76 onto dd2ae9a (git range-diff: both commits =). cargo test --workspace -- --test-threads=4 on 08866f9 merged onto main 9e3831e (a clean merge, 9cac114): 777 passed, 0 failed, 41 ignored (the xcl library: 728 passed, 21 ignored). SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware (no wave64), and heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows; main's own test).
  • The earlier head bf32301d87b8, on 9ab7d76. SDK DXC 1.7 on the RTX 5070 Ti passed all nine comparison cases on both the full-screen and partial-quad draws. Positive infinity, negative infinity and NaN each selected a wrong texel with the old emitter; the fix preserves the sample coordinate on a stationary axis. The full workspace passed (652 passed, 0 failed, 40 ignored, including the same two native-wave64 skips described below).
  • Ryzen 9 9950X, RTX 5070 Ti (610.74), Windows 11 25H2 build 26200.9711, Rust 1.99.0 (MSVC x64), debug, SDK DXC 1.7.2308.16.
  • Head 13d8a1f5e592 on 9ab7d763197c. cargo build --workspace passed. cargo test --workspace -- --test-threads=4 returned 652 passed, 0 failed, 40 ignored. GPU tests reporting SKIPPED (included in Cargo’s pass count): gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu_wave32_program_in_wave64_hardware (no wave64).
  • Base 9ab7d763197c with only the tests added failed four of six draw cases. The two controls passed on base, and all six passed on this head.
  • Integration with the companion fix and Recompiler: image_sample_b/d/cd/c_o offsets move by the mip they read #247 at a60db9f3e5f2: two checks passed. Both offset forms read the expected texel at mip 1 with a nonzero offset. SDK DXC compiled 20 opcode and view combinations.
  • Serial debug-layer suite on clean main: 593 passed, 18 failed, 20 ignored. On both main and the gradient run (594 passed, 18 failed, 20 ignored), CreateReservedResource returned E_OUTOFMEMORY in gpu_sparse_vram_descriptors_buffers_and_xmem. Three sparse windows had zero GPU addresses, then pipeline creation returned DXGI_ERROR_DEVICE_REMOVED.
  • Independent run by @throny (Recompiler: comparison samples use their supplied pixel gradients #350 (comment)): RTX 4080 (NVIDIA 610.88), i9-12900K, Windows 11 26300, Rust 1.100.0, debug, 13d8a1f merged onto main 7ebf1b8. cargo test --workspace -- --test-threads=4: 678 passed, 0 failed, 41 ignored. With emit.rs from main the new test fails in four of its six cases. With Recompiler: pixel comparison samples apply their LOD bias #349 merged too, both tests pass.
  • Earlier smoke runs by @CuteBooty, RTX 3070 (NVIDIA 617.14), release, on main 9ab7d76 and this head: 918 and 909 frames, no crash or device removal (Recompiler: comparison samples use their supplied pixel gradients #350 (comment)).

Regression run

Our own runs, one after the other on the machine under Tested (RTX 5070 Ti), release builds, no uncommitted changes: main at dd2ae9a (this PR's base), then this PR's head 08866f9. 1260 / 1260 frames (100 %), peak commit 27098 / 26962 MB (101 %); no device removal and no crash on either run.

main:

xcl-smoke v1
commit: dd2ae9a70af5eb5aad60f02d86e386b72abe1e95
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1260 (PresentX 1261)
presents per 2 s (last 3): 22 24 12
device removals: 0
crashes: 0
peak commit: 26962 MB

PR:

xcl-smoke v1
commit: 08866f93ae63d9d73cf13ca96b47fca028f775b0
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1260 (PresentX 1260)
presents per 2 s (last 3): 27 23 12
device removals: 0
crashes: 0
peak commit: 27098 MB

Not tested

  • Nonzero texel offsets on explicit-gradient comparisons at mip 1 or higher depend on Recompiler: image_sample_b/d/cd/c_o offsets move by the mip they read #247. The standalone head uses mip-0 texel units for these offsets.
  • AMD. The pixel-shader quad operations emitted by this change have never been compiled by an AMD driver in our testing.
  • Whether the title's frames reach these sample forms: the smoke runs above are a regression check only.
  • Cube and array sampling on hardware, and the throughput cost of issuing four comparison samples.

AI use

  • Codex (GPT-6.2 Astra): code, tests and this description
  • Claude Code (Claude Opus 5.5): the smoke runs under Regression run, the squash into one commit and the suite run on 08866f9

🤖 Generated with Claude Code

@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

AMD run, since Not tested says no AMD driver has compiled these quad operations yet: 13d8a1f merged onto main a6f5f7a (clean merge): pass on RDNA4, and the new test fails without the emit.rs change.

Tested

  • AMD Radeon RX 9060 XT (RDNA4, wave64 + mesh shaders), driver 32.0.32029.2010; Ryzen 7 5800X3D; Windows 11 Pro 26200; Rust 1.100.0; DXC 1.9.2602.17 (SDK 10.0.26100); debug build.
  • .\scripts\test.ps1: 677 passed, 0 failed, 41 ignored; no SKIPPED GPU tests (the two wave64 skips on your 5070 Ti run here).
  • XCL_REQUIRE_GPU=wave64,mesh cargo test -p xcl --lib gpu_ps_compare_mip: gpu_ps_compare_mip_gradients passes.
  • Negative control: the merged tree with only emit.rs back on main. The test fails with IMAGE_SAMPLE_C_D, scale 0.125, slope 0: pixel (0,0): 0 instead of 1 from mip 1, plus the _C_D_CL, _C_CD_O and slope 0.0625 cases.
  • With Recompiler: pixel comparison samples apply their LOD bias #349 also merged (the shared sample_cmp_grad is identical, so git merges it cleanly): both gpu_ps_compare_mip_gradients and gpu_ps_compare_mip_bias pass.

Not tested

  • A retail title (smoke.ps1); cube and array sampling on hardware; NVIDIA.

AI use

  • Claude Code (Claude Opus 5.5) ran the commands and wrote this report from the logs; I checked it before posting.

@throny

throny commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

NVIDIA RTX 40 run of 13d8a1f merged onto main 7ebf1b8 (clean merge, merge commit 5e66bc9): pass, and the new test fails without the emit.rs change.

Tested

Reviewed (read the whole diff)

  • The new arm comes before the has_lz || !ps one and is limited to pixel shaders, so comparison samples with gradients outside PS still take SampleCmpLevelZero as on main.

Not tested

  • No regression run (scripts/smoke.ps1) on a title, no shader-pack run (XCL_SAMPLE_RPF), wave64 hardware, the cube, array and 3D forms (the test draws 2D only).

AI use

  • Claude Code (Claude Fable 5.2) ran the builds, the tests and the controls, read the diff and wrote this report. The runs above are real, on the hardware named.

@QemmHD

QemmHD commented Nov 24, 2026

Copy link
Copy Markdown

Review: the quad construction is exact (the coordinate is affine across the quad and each lane's own coordinate is unchanged), and staying on ps_6_6 avoids SM 6.8's SampleCmpGrad. Three points:

  1. emit.rs:2191-2196: offsets for comparison samples still use level 0u (the _ => "0u" arm), so _c_d_o / _c_cd_o / _c_cd_cl_o with a non-zero offset move by mip-0 texels while the gradients now select coarser mips. The _O rows in the test use offset 0. If this is meant to be fixed by Recompiler: image_sample_b/d/cd/c_o offsets move by the mip they read #247, a note on the dependency (or a non-zero-offset row once Recompiler: image_sample_b/d/cd/c_o offsets move by the mip they read #247 lands) would help.
  2. check_ps_depth_mip draws [0,0],[0,32],[32,0] over the whole target, so no quad ever has helper lanes, which the QuadReadLaneAt method relies on. A triangle edge through quads (e.g. [0,0],[0,16],[16,0], checking only covered pixels) would cover that.
  3. Minor, emit.rs:1927: the own-lane term is delta * grad with delta == 0, so a ±inf/NaN gradient turns the lane's own coordinate into NaN. Using QuadReadLaneAt(coord, q) unchanged when delta == 0 avoids it.

AI-assisted review (Claude Code, Claude Opus 5.5).
Discord: bigpeppa.

@CuteBooty

Copy link
Copy Markdown

Regression run on a retail title for 13d8a1f, which the description lists as pending: pass (no device removal, no crash, frames and peak commit level with main).

Tested

  • RTX 3070 8 GB (32-lane waves), NVIDIA 617.14; Ryzen 5 3600, 32 GB; Windows 11 Pro 26200; rustc 1.100.0 MSVC; release build.
  • scripts\smoke.ps1 -Game <folder> -Seconds 60 on the PR's head as it is and on main 9ab7d76, the commit it is based on, each built from a clean checkout of its commit.
  • Each build ran twice back to back and the blocks are the second runs. The first runs: 912 frames (main) and 919 frames (PR), also with no device removal or crash. The main block is one run, shared with my reports on Memory: a freed sparse block is decommitted before its address is reused #348 and Recompiler: pixel comparison samples apply their LOD bias #349.
  • Video memory at 60 s, from XCL's memory: line: 6311 MB local / 4619 MB non-local (main), 6247 MB local / 4620 MB non-local (PR).

main:

xcl-smoke v1
commit: 9ab7d763197c7f420bff10cd9bf1310f07e2a9aa
gpu: NVIDIA GeForce RTX 3070
title: game
seconds: 60
exit: 0x0
frames shown: 918 (PresentX 918)
presents per 2 s (last 3): 40 41 40
device removals: 0
crashes: 0
peak commit: 14793 MB

PR:

xcl-smoke v1
commit: 13d8a1f5e5927ba445bacd13e9dfb71deb3933d4
gpu: NVIDIA GeForce RTX 3070
title: game
seconds: 60
exit: 0x0
frames shown: 909 (PresentX 909)
presents per 2 s (last 3): 41 40 40
device removals: 0
crashes: 0
peak commit: 14733 MB

Not tested

  • The test suite on this head (others have posted it), anything past the first 60 s, and whether a shader the title draws with in these 60 s has a comparison sample with gradients (the case this changes).

AI use: Claude Code with Claude Fable 5.2 made the runs with me present and wrote this comment.

@Kalmarv

Kalmarv commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for checking the mip calculation and the triangle coverage.

The standalone head uses mip-0 texel units for the explicit-gradient offset forms, and both offset rows pass zero. I added that dependency to the PR body draft. With #247 at a60db9f, a synthetic IMAGE_SAMPLE_C_D_O draw with a +1 X offset read the expected neighbor at mip 1.

The full-screen triangle left the helper-lane case uncovered. The test now keeps that draw and adds a diagonal that leaves one or three covered pixels in each edge quad. It checks the depth comparison for all 96 covered pixels and the clear value for the other 160.

With the tests carried onto 13d8a1f, +∞ and NaN in dU/dX returned 1 instead of 0 at pixel (2,0), and −∞ in dV/dY did the same at (0,2). The zero-displacement multiply contaminated the sample center. The emitter still reads the gradients across the quad, but applies an axis displacement only when it is nonzero. The new cases use positive infinity, negative infinity and NaN with the mip clamped to 1 to isolate the sampling coordinate.

Tested

PR head bf32301d87b8 on an RTX 5070 Ti (610.74), SDK DXC 1.7, ps_6_6: the targeted raster test passed. The full workspace returned 652 passed, 0 failed, 40 ignored; two native-wave64 tests reported SKIPPED within the pass count.

Not tested

AMD on this follow-up. Our title smoke pair on this head is in the description; the title doesn't show whether it reaches these sampling forms.

🤖 GPT-6.2 Astra

@thehollowedone

Copy link
Copy Markdown

Independent native D3D12 check of f09eca5 on RTX 3080 Ti / 621.35, Windows 11 build 26300, Rust 1.99.1 MSVC, release.

Both DXC 1.8.2502.11 and 1.6.2112.16 passed all nine explicit-gradient cases on both triangle coverage patterns, including NaN, positive infinity and negative infinity with the mip fixed. The flat integer selector control also passed on both compilers. Four test runs passed, with zero failures, ignores or skips. The offset variants use zero offsets.

I used XCL_ADAPTER=3080 Ti and cargo test --locked --offline --release -p xcl --lib <filter> -- --test-threads=1 --nocapture with gpu_ps_compare_mip_gradients and gpu_ps_p0_reads_flat_integer_selectors. The older compiler was selected through XCL_DXC_DIR and confirmed in the test output.

AI use: Codex (GPT-6, exact variant unavailable) ran the tests and drafted this report.

SM 6.6 has no comparison sample that takes gradients, so a pixel shader's
image_sample_c_d* and image_sample_c_cd* selected their mip from the screen
derivatives. SampleCmp now runs once for each pixel of the quad
(QuadReadLaneAt) with a footprint built from that pixel's supplied
gradients, array layers kept whole and cube-face gradients projected back to
directions; each pixel keeps its own result. A lane's own coordinate is used
unchanged on an axis it does not move along, so an infinite or NaN gradient
cannot turn it into NaN.

Assisted-by: Codex (GPT-6.2 Astra)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Squashed into one commit (08866f9, the same tree as before) as CONTRIBUTING asks; the smoke pair for it is queued.

A note for the merge order with #247: this PR makes the PS SampleCmp take the gradients, and #247 moves a comparison offset by the mip that sample reads. With both in a tree, one line in #247's offset code has to take the gradients too, or gpu_image_sample_c_b_d_offset_moves_by_the_mip_read fails (sample_c_d_o, PS: pixel (0,0) = 0). We have that line ready and tested (3edfb6f; with it the test passes, and main + all eleven of our open PRs + both follow-ups pass the whole suite, 815 passed, 0 failed). Whichever of the two lands second, we push it there.

AI use: Claude Code (Claude Opus 5.5)

@JJAutoCompare

Copy link
Copy Markdown

Tested

Independent native Windows run for PR #350 at clean detached 08866f93ae63d9d73cf13ca96b47fca028f775b0. STOR-WINLAB, Windows 11 Enterprise Evaluation x64 26200.9974, Ryzen 7 3800X, Intel Arc A770 driver 32.0.101.9175, Rust/Cargo 1.100.0 x86_64-pc-windows-msvc, DXC and DXIL 1.9.2609.5. Unoptimized test profile with debug info and incremental compilation disabled.

cargo test --locked -p xcl --lib --no-run -j 2 --message-format=json built outside the GPU lock and exited 0. Used XCL_ADAPTER=A770, XCL_DIRECT_DXIL=0, and XCL_DXC_DIR=%LOCALAPPDATA%/XclTooling/dxc-1.9.2609/bin/x64. Ran the Cargo-emitted executable in the shared Arc lock wrapper:

& 'L:/XclTooling/Invoke-ArcTest.ps1' -Test {
    & 'L:/XclTooling/verify-pr1523-1362129-t3-cf2e05e7/target/debug/build/xcl/00e28da3621caf64/out/xcl-00e28da3621caf64.exe' --exact gpu::recomp::tests::gpu_ps_compare_mip_gradients --nocapture --test-threads=1
}

1 passed, 0 failed, 0 ignored, 0 GPU skips, 671 filtered out, 2.76 s. The fixture and mutex wrapper both exited 0. Executable SHA-256 matched before the wait, under the mutex, and after the fixture exited.

All nine stock HLSL/DXC cases passed on both triangle geometries. The fixture checks exact red-channel comparison results on the 16×16 readback, including the gradient that selects adjacent mips across pixel columns, the non-finite gradient controls, and the clear value for uncovered pixels. The offset forms use zero offsets.

Not tested

Nonzero offsets, cube and array textures, direct DXIL lowering, the PR base, other adapters, the workspace suite, and title smoke runs.

AI use

T3 Code / Codex (gpt-6.2-sol) verified the checkout, ran the checked-in fixture, and wrote this report.

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