Skip to content

Recompiler: image_sample_b/d/cd/c_o offsets move by the mip they read - #247

Open
Kalmarv wants to merge 1 commit into
ivall:mainfrom
Kalmarv:fix/sample-o-lod-variants
Open

Kalmarv wants to merge 1 commit into
ivall:mainfrom
Kalmarv:fix/sample-o-lod-variants

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Follows #229. #229 moves the coordinates by offset / size of the mip the console reads, but it picks that mip only for the plain form (implicit LOD in a PS, or a given LOD). The bias, gradient and comparison forms still take mip 0's size. With a point mip filter, the texel read is then off by up to one texel of the mip actually sampled. This PR picks the mip for those forms too.

What main does today:

  • image_sample_b_o: in a PS, the implicit LOD without the bias; outside a PS, mip 0, though the sample is SampleLevel(bias).
  • image_sample_d_o / cd_o: mip 0 whatever the gradients say.
  • image_sample_c_o in a PS: mip 0, because the plain-form branch excludes comparison samplers. CalculateLevelOfDetail takes no SamplerComparisonState; DXC rejects it.

What is in it

One commit, a60db9f, on main 9ab7d76:

  • crates/xcl/src/gpu/recomp/emit.rs: the offset's level for these forms is:

    • the given LOD (unchanged);
    • else the LOD of the given gradients (log2 of the longer gradient in mip-0 texels), or in a PS the pixel's own LOD (CalculateLevelOfDetailUnclamped, or for comparison samplers the same log2 of ddx / ddy of the coordinates);
    • plus the bias;
    • then clamped to the sampler's MIN_LOD / MAX_LOD (S# word 1), rounded, and clamped to the view's last mip.

    The LOD is unclamped before the bias, so a bias on a magnified pixel starts below 0. @ryotdev found that the first head (fd45f20) got this wrong. The plain form, lz, gather and given-LOD forms take the same path as before.

  • crates/xcl/src/gpu/recomp/tests.rs: five new GPU tests on a 2-mip texture (8², 4²) with point min, mag and mip filters, so each pixel reads exactly one mip.

Tested

  • RTX 5070 Ti, NVIDIA 610.74, Ryzen 9 9950X, Windows 11 Pro 25H2 (build 26200.9711), Rust 1.99.0, debug build, SDK DXC 1.7.2308.16.
  • cargo test --workspace -- --test-threads=4 on a60db9f: 656 passed, 0 failed, 40 ignored (SKIPPED GPU tests: gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware, both "no wave64").
  • The new tests, built from main with and without the emit.rs change (the same test code in both):
test main this PR
gpu_ps_image_sample_offset_point_mip (control, plain sample_o) pass pass
gpu_ps_image_sample_offset_bias_lod (b_o, LOD 0 + bias 1) FAIL: pixel (0,0) = 100, expected 101 pass
gpu_ps_image_sample_offset_bias_lod_magnified (b_o, LOD -1 + bias 1, @ryotdev's case) pass pass (fd45f20: FAIL, (0,0) = 2, expected 1)
gpu_ps_image_sample_offset_gradient_lod (d_o and cd_o, gradient LOD 1) FAIL: pixel (0,0) = 100, expected 101 (cd_o fails the same way, 48 of 256 pixels) pass
gpu_ps_image_sample_offset_compare_lod (c_o, implicit LOD 1, LESS_EQUAL) FAIL: pixel (0,0) = 0, expected 1 pass
gpu_ps_image_sample_offset_implicit_lod_between_mips (#229's) pass pass

Regression run

Run by @srcamarda on a retail title (RX 7800 XT, RDNA3; release; 90 s; #247 (comment)). Three runs of each, the first with a cold translation cache: main 901 / 1180 / 1184 frames, this PR 1168 / 1167 / 1357; no crash or device removal in any. The gpu: line names a virtual display adapter; XCL ran on the RX 7800 XT. No _o sample form appears in the title's first menus, so this is a regression check, not a test of the change. The second run of each, as posted:

main:

xcl-smoke v1
commit: 9ab7d763197c7f420bff10cd9bf1310f07e2a9aa
gpu: SudoMaker Virtual Display Adapter
title: RetailGame_Dump
seconds: 90
exit: 0x0
frames shown: 1180 (PresentX 1180)
presents per 2 s (last 3): 27 28 26
device removals: 0
crashes: 0
peak commit: 17197 MB

PR:

xcl-smoke v1
commit: a60db9f3e5f279e66b7e1a73ba66a8c13a98d14c
gpu: SudoMaker Virtual Display Adapter
title: RetailGame_Dump
seconds: 90
exit: 0x0
frames shown: 1167 (PresentX 1167)
presents per 2 s (last 3): 25 26 25
device removals: 0
crashes: 0
peak commit: 17201 MB

Not tested

  • The sampler's LOD bias is not added to the offset's level on image_sample_d/cd/c*_o (main used level 0 there). On image_sample_l_o the offset uses the given LOD before the sampler's bias and MIN/MAX_LOD, as on main. Sampler bias on image_sample_b_o follows it on NVIDIA 610.74 (measured); AMD not measured.
  • The regression run on a title, main and PR (above): none yet.
  • No title is known to use any of these forms with a VGPR offset. They come from the ISA, not from a shader seen in use.
  • AMD RDNA2 for gpu_ps_image_sample_offset_bias_lod_magnified and for the head a60db9f: the RX 6600 XT run above is of fd45f20. RDNA4 ran a60db9f (@nullmarsh, above).
  • Linear mip filters on these forms. Only the level choice is fixed; the per-mip blend Recompiler: image_sample_o under an implicit LOD moves each blended mip by its own texels #229 does for the plain form is not extended here.
  • b_o and d_o outside a pixel shader, 1D / 3D / array textures, A16 addresses, and a min-LOD clamp (_cl_) together with an offset.
  • The sampler's own LOD bias (S# word 2) is not added to the offset's level.
  • Anisotropic samplers. The gradient LOD here is the isotropic log2 of the longer gradient, which can differ from the hardware's at a mip boundary. The plain form's CalculateLevelOfDetail has the same limit.

AI use

  • Claude Code (Claude Opus 5.5), code, tests and this description

@ryotdev

ryotdev commented Nov 23, 2026

Copy link
Copy Markdown

I reproduced a magnification edge case on native Windows 11 with an RTX 5070 (driver 32.0.16.1088) and DXC 1.9.2609.5. I applied fd45f206's diff on main@7a033c8c and used the same synthetic test code for both builds.

I duplicated gpu_ps_image_sample_offset_bias_lod, changed the scale from 1/8 to 1/16, kept the instruction bias and offset at +1, and expected texel_moved(0, u). That gives implicit LOD −1, plus bias 1, so the sample and offset should both use mip 0.

Case Main PR
Original LOD 0 + bias 1 (0,0) = 100, expected 101 Pass
Magnification LOD −1 + bias 1 Pass (0,0) = 2, expected 1

Using CalculateLevelOfDetailUnclamped in the biased pixel path let the instruction bias see the raw negative LOD. With that local change, all six gpu_ps_image_sample_offset_ tests passed on the GPU, including the original positive case and the magnification regression. This checks the sampler in the existing fixture. Any sampler and view bounds still need to be applied after bias. Direct3D LOD calculation

Synthetic textures only. Rust 1.100.0, clang-cl/lld 19.1.1 with xwin SDK 10.0.26100. AI-assisted (Codex).

ivall#229 picks the offset's mip for the plain form only; the bias, gradient
and (in a PS) comparison forms still divided by mip 0's size. The level
is now the given LOD, else that of the given gradients or the pixel's
own (screen derivatives for comparison samplers, which
CalculateLevelOfDetail does not take), plus the bias. That LOD is the
unclamped one, so a bias on a magnified pixel starts below 0 (found by
ryotdev); the sampler's MIN_LOD / MAX_LOD and the view's last mip are
applied after the bias.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Kalmarv
Kalmarv force-pushed the fix/sample-o-lod-variants branch from fd45f20 to a60db9f Compare November 23, 2026 22:07
@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @ryotdev, that was a real regression. Your case is now gpu_ps_image_sample_offset_bias_lod_magnified: on fd45f20 it fails as you saw it ((0,0) = 2, expected 1), and on the new head it passes. RTX 5070 Ti, 610.74: all 6 gpu_ps_image_sample_offset_* pass, and gpu::recomp:: gives 187 passed, 0 failed, 18 ignored.

The new head a60db9f (one commit, rebased on 9ab7d76) uses CalculateLevelOfDetailUnclamped in the biased path, as you suggested. The sampler's MIN_LOD / MAX_LOD and the view's last mip are applied after the bias. The gradient log2 and the c_o ddx/ddy LOD were already unclamped before the bias.

🤖 Claude Opus 5.5

@Kalmarv Kalmarv changed the title Recompiler: image_sample_b_o / d_o / cd_o / c_o move the offset by the mip they read Recompiler: image_sample_b/d/cd/c_o offsets move by the mip they read Nov 23, 2026
@WarmMilkCodes

Copy link
Copy Markdown
Contributor

Tested #247 fd45f20 merged onto main 9ab7d76 on AMD Radeon RX 6600 XT (RDNA2), driver 32.0.21045.29003, Windows 11 23H2 (22631.6199): pass

cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_* data: main 651 passed / 0 failed; this 655 passed / 0 failed, 0 GPU tests skipped. The PR's 4 new tests pass on RDNA2; no new failures.

@denio4321

Copy link
Copy Markdown

Tested a60db9f merged onto main 9ab7d76 on RTX 3060 12 GB (Ampere, 32-lane waves), NVIDIA 610.88, Windows 11 Pro 23H2 (22631), rustc 1.100.0 MSVC, DXC 1.9.2602.17: pass.

.\scripts\test.ps1 (debug, --test-threads=4), no XCL_SAMPLE_* set. Baseline main 9ab7d76 alone on the same machine: 651 passed, 0 failed, 40 ignored.

  • This PR: 656 passed, 0 failed, 40 ignored.
  • The five new tests pass on this card: gpu_ps_image_sample_offset_point_mip, _bias_lod, _bias_lod_magnified, _gradient_lod and _compare_lod.
  • GPU skips are the same 2 as on main (no wave64).

The magnification case from the comment above (_bias_lod_magnified) passes here too.

Not checked: the negative control, or any game.

AI-assisted (Claude Code).

@PHeitor1

Copy link
Copy Markdown

Tested on RTX 5060 Ti 8 GB, driver 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native), Rust 1.100.0 msvc, debug. cargo test --workspace --no-fail-fast -- --test-threads=4 on the head a60db9f: 656 passed, 0 failed, 40 ignored (main 9ab7d76: 651 / 0 / 40). SKIPPED: only the usual two no wave64 (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware).

Run and written with Claude Code (Claude Opus 5.5).

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Regression run wanted. We have no title on our machine. If you can run scripts/smoke.ps1 -Game <your title folder> twice, once built from main (9ab7d76) and once from this PR's head (a60db9f), please paste both xcl-smoke blocks here. I'll copy them into the description with your name and hardware.

🤖 Claude Opus 5.5

@JGonzalezRodriguez

Copy link
Copy Markdown

Tested PR #247 head a60db9f against base 9ab7d76 on both NVIDIA and AMD RDNA2 (native wave64).

Tested

  • Host / stack: CachyOS Linux, NVIDIA RTX 4090 (NVIDIA 610.57.4, wave32) and AMD RDNA2 iGPU (RADV RAPHAEL_MENDOCINO, radv 26.2.4, native wave64 + wave32) via Proton Experimental wine + relaxed vkd3d-proton; DXC 1.9.2609.5; cross-built x86_64-pc-windows-msvc debug (rustc 1.98.1, clang-cl 23.1.1 + lld-link, 0 errors, 0 warnings).
  • image_sample_offset tests (a60db9f vs 9ab7d76 emit.rs with a60db9f tests.rs):
Test Base 9ab7d76 (RTX 4090 / RADV) PR a60db9f (RTX 4090 / RADV)
gpu_image_sample_offset pass / pass pass / pass
gpu_ps_image_sample_offset_implicit_lod_between_mips pass / pass pass / pass
gpu_ps_image_sample_offset_point_mip pass / pass pass / pass
gpu_ps_image_sample_offset_bias_lod_magnified pass / pass pass / pass
gpu_ps_image_sample_offset_bias_lod FAIL (100, want 101) / FAIL (100, want 101) pass / pass
gpu_ps_image_sample_offset_gradient_lod FAIL (100, want 101) / FAIL (100, want 101) pass / pass
gpu_ps_image_sample_offset_compare_lod FAIL (0, want 1) / FAIL (0, want 1) pass / pass
  • Confirmed a60db9f fixes the symmetric b_o minification + negative bias case (LOD +2 + bias -1 → 101), b_o with MaxLOD = 0 (MIP_FILTER = point → 1), and d_o with MinLOD = 1 (101) on both GPUs.

Findings (synthetic probes reproduced identically on both RTX 4090 and AMD RDNA2)

  1. MIP_FILTER == 0 (NONE, S# w[2][27:26]) is ignored (emit.rs:2190-2212) — regresses vs 9ab7d76:

    • desc::sampler_d3d12 (desc.rs:290,322) forces MaxLOD = MinLOD when MIP_FILTER == 0, while a60db9f reads only S# w[1] (MAX_LOD). With MIP_FILTER = NONE and MAX_LOD = 0xFFF:
      • d_o (grad 1): pass (1) on 9ab7d76 → FAIL (2, want 1) on a60db9f
      • b_o (LOD 0, bias +1): pass (1) on 9ab7d76 → FAIL (2, want 1) on a60db9f
      • c_o (implicit LOD 1): pass (1) on 9ab7d76 → FAIL (0, want 1) on a60db9f
    • Reading S# w[2] (as emit.rs:2257 already does) and setting lod_max = lod_min when ((w2 >> 26) & 3u) == 0u matches desc::sampler_d3d12.
  2. T# ResourceMinLODClamp (tw1[19:8] u4.8, desc.rs:73,251) is dropped on b_o (emit.rs:2203-2212) — new regression in a60db9f:

    • CalculateLevelOfDetail included SRV.ResourceMinLODClamp, whereas CalculateLevelOfDetailUnclamped omits it, and a60db9f clamps only against S# w[1] MIN_LOD:
      • b_o (LOD 0, bias 0) with ResourceMinLODClamp = 1.0: pass (101) on 9ab7d76 → FAIL (100, want 101) on a60db9f
    • tw1 is already bound at emit.rs:2088, so lod_min = max(s_min, (float)((tw1 >> 8) & 0xFFFu) / 256.0f) fixes this for b_o, d_o, cd_o, and c_o.
  3. S# MipLODBias (w[2][13:0] s5.8) is omitted on grad_lod paths (d_o, cd_o, c_o) (emit.rs:2201-2202):

    • SampleGrad and SampleCmp both apply sampler.MipLODBias, whereas raw log2(...) does not:
      • d_o (grad 0) + MipLODBias = +1.0: FAIL (100, want 101)
      • c_o (implicit LOD 0) + MipLODBias = +1.0: FAIL (0, want 1)
    • Adding (float)(((int)(w2 << 18)) >> 18) / 256.0f to the grad_lod branches aligns them with CalculateLevelOfDetailUnclamped (which already includes sampler.MipLODBias).
  4. Comparison forms with explicit gradients/bias (c_d_o, c_cd_o, c_b_o) regress against SampleCmp / SampleCmpLevelZero (emit.rs:2200-2209 vs 2233-2234):

    • emit.rs:2233-2234 emits SampleCmp (implicit LOD, ignoring grads/bias) in PS and SampleCmpLevelZero (mip 0) outside PS, while the offset-level path now uses grad_lod(dx, dy) / + bias:
      • c_d_o in PS (implicit LOD 0, grad 1, GREATER_EQUAL z=1.5): pass (1) on 9ab7d76 → FAIL (0, want 1) on a60db9f
      • c_b_o in PS (implicit LOD 0, bias +1, GREATER_EQUAL z=1.5): pass (1) on 9ab7d76 → FAIL (0, want 1) on a60db9f
    • Until SM 6.7 SampleCmpGrad/SampleCmpBias/SampleCmpLevel are used, has_c should match the emitted instruction (0u when !ps || has_lz, pure screen-derivative LOD + sampler bias/clamps when ps).
  5. Minor: emit.rs:2188 calls self.texel_offset(&t, dim, o, "0u", "0") just to obtain {t}_w0/{t}_h0/{t}_d0/{t}_n0, leaving an unused float3 {t}_o0.

Not tested

  • Native Windows, Windows SDK DXC 1.7, full workspace test run on a60db9f (ran targeted image_sample_offset + probes on a60db9f; full gpu::recomp::tests:: ran on fd45f20), retail titles.

AI use

Antigravity (Gemini frontier model) assisted with code review, dual-GPU synthetic probe execution, and drafting this report; every line cited and repro result was directly verified on this host.

@nullmarsh

Copy link
Copy Markdown

Tested a60db9f merged onto main 4017f8f (clean merge) on AMD RDNA4: pass, and three of the five new tests fail without the fix.

Tested

  • AMD Radeon RX 9060 XT (RDNA4), 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: 657 passed, 0 failed, 40 ignored; no SKIPPED GPU tests on this adapter.
  • XCL_REQUIRE_GPU=wave64,mesh cargo test -p xcl --lib image_sample_offset: 7 passed, 0 failed (the five new gpu_ps_image_sample_offset_* tests, ..._implicit_lod_between_mips and gpu_image_sample_offset).
  • Negative control: only this PR's tests.rs on main 4017f8f (the emit.rs change left out): ..._bias_lod, ..._gradient_lod and ..._compare_lod fail (tests.rs:2005). ..._point_mip and ..._bias_lod_magnified pass with or without the fix, so on this adapter they guard against regressions rather than show this change.

Not tested

  • No XCL_SAMPLE_* set and no retail title, so no smoke.ps1 regression run; vertex/compute stages with a bias or gradients (the new tests are PS only); NVIDIA.

AI use

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

@srcamarda

Copy link
Copy Markdown

@Kalmarv the regression run you asked for, plus the first RDNA3 run.

Tested

  • AMD Radeon RX 7800 XT (RDNA3, native 64-lane waves), driver 32.0.32029.2010; Ryzen 5 5600G, 32 GB; Windows 11 Pro 26300; Rust 1.100.0 MSVC; DXC 1.8.2502.11 (SDK 10.0.26100)
  • Built from the head a60db9f itself (on 9ab7d76), no uncommitted changes
  • XCL_REQUIRE_GPU=wave64,mesh cargo test -p xcl --lib image_sample_offset: 7 passed, 0 failed (the five new tests, …_implicit_lod_between_mips, gpu_image_sample_offset)
  • cargo test --workspace -- --test-threads=4 (debug): 656 passed, 0 failed, 40 ignored, no SKIPPED

Regression run (retail title, scripts/smoke.ps1, 90 s, release). Three runs of each binary, the first with a cold translation cache: main 901 / 1 180 / 1 184 frames, this PR 1 168 / 1 167 / 1 357. No crash or device removal in any, and the same peak commit. gpu: names a virtual display adapter on this machine; XCL ran on the RX 7800 XT. The second run of each:

main 9ab7d76:

xcl-smoke v1
commit: 9ab7d763197c7f420bff10cd9bf1310f07e2a9aa
gpu: SudoMaker Virtual Display Adapter
title: RetailGame_Dump
seconds: 90
exit: 0x0
frames shown: 1180 (PresentX 1180)
presents per 2 s (last 3): 27 28 26
device removals: 0
crashes: 0
peak commit: 17197 MB

this PR's head a60db9f:

xcl-smoke v1
commit: a60db9f3e5f279e66b7e1a73ba66a8c13a98d14c
gpu: SudoMaker Virtual Display Adapter
title: RetailGame_Dump
seconds: 90
exit: 0x0
frames shown: 1167 (PresentX 1167)
presents per 2 s (last 3): 25 26 25
device removals: 0
crashes: 0
peak commit: 17201 MB

Not tested

  • The changed forms in a title. In these 90 s (the title's first menus) the shaders it loads use image_sample, _l, _lz, _c_lz, _d and _b (report.json → shaders.rdna2_mnemonics), but no _o form, so this run is a regression check only. The negative control was also not run here; nullmarsh did that on RDNA4.

AI use

  • Claude Code with Claude Opus 5.5: built both commits, ran the tests and the smoke runs, wrote this report.

@PHeitor1

Copy link
Copy Markdown

Re-tested a60db9f merged onto main 2b4bb43 (clean merge) on RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native, Graphics Tools + XGameRuntime installed, so the debug-layer tests run), rustc 1.100.0 MSVC, debug, cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_*.

Pass: 675 passed, 0 failed, 41 ignored. SKIPPED: only the 2 no wave64 tests.

Run and written with Claude Code (Claude Opus 5.5).

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@srcamarda thank you, both blocks are in the description under Regression run, with your three-run spread and the note that the title's first menus don't reach an _o form.

🤖 Claude Opus 5.5

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.

8 participants