Skip to content

Recompiler: image_sample_o under an implicit LOD moves each blended mip by its own texels - #229

Merged
ivall merged 1 commit into
ivall:mainfrom
artor410:recomp/sample-offset-per-mip
Nov 23, 2026
Merged

ivall merged 1 commit into
ivall:mainfrom
artor410:recomp/sample-offset-per-mip

Conversation

@artor410

@artor410 artor410 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

HANDOFF "Next steps" 5: image_sample_o with an offset from a VGPR. A pixel shader's implicit LOD with a linear mip filter blends two mips; the console moves each one by the offset in its own texels. XCL moved both by offset / size of the rounded LOD's mip, so the second mip read one texel off whenever the offset was half its texel or less.

Summary

 image_sample_o, PS, implicit LOD
-  o = offset / size(round(L))
-  Sample(coord + o)
+  L  = min(max(LOD, clamp), last mip);  l0 = floor(L)
+  m0 = SampleLevel(coord + offset / size(l0),     l0)
+  m1 = SampleLevel(coord + offset / size(l0 + 1), l0 + 1)
+  per_mip = MIP_FILTER linear
+            && not anisotropic (as desc::sampler_d3d12 decides)
+            && (l0 >= 1 || min filter == mag filter)   // level-0 SampleLevel uses the mag filter
+  per_mip ? lerp(m0, m1, frac(L)) : Sample(coord + o)  // the old form, unchanged
  • emit.rs: texel_offset / sample_coord / moved_coords, shared with the existing offset path (no second copy of the GetDimensions / sign-extension code).
  • runner.rs: ViewTexture uploads every mip level (data = the levels one after the other); draw_views = draw with textures and samplers. draw now goes through draw_on_multi.
  • tests.rs: draw_console_views (SGPRs + textures) and the new test.

Evidence

gpu_ps_image_sample_offset_implicit_lod_between_mips: a PS draw, 8² / 4² / 2² / 1² texture (level k texel x = 100·k + x), point min/mag, linear mip, clamp, uv = pixel centre · 2^0.25 / 8 (LOD 0.25), offset (+1, 0). Expected per pixel, from the console rule: 0.75 · texel₈(⌊8u⌋+1) + 0.25 · (100 + texel₄(⌊4u⌋+1)).

  • Before: pixel (0,0) = 25.75, expected 26 (mip 1 read texel 0 instead of 1)
    After: passes; gpu_image_sample_offset (lz) still passes.

Rebased on d4f17ca (draw_gs from #199 and draw_views share one draw_gs_views). cargo test --workspace --no-fail-fast: lib 606 passed, every other target green except e2e::test_title_draws_with_a_root_signature_under_the_debug_layer, which fails the same way on main here: my checkout's path is over 260 characters and the test title's overlay file writes hit os error 3. Tested on an NVIDIA RTX 3070 Ti (driver 610.88, 32-lane waves); not run on AMD.

Merge Danger

Door: two-way

Blast Radius: PS image_sample*_o with an implicit LOD

Only that form changes; every other sample keeps its HLSL. Its HLSL now has three samples instead of one (the fallback Sample is always emitted; DXC folds the select only for static samplers) – I could not count how many pack programs use it without the pack. Not covered / not verified:

  • CalculateLevelOfDetail and the sampler's MipLODBias: not checked that it adds the bias as the hardware does (the test uses bias 0).
  • _l_o with a fractional LOD and _b_o outside a PS still use the one-mip form.

Overlap: none of the open PRs that touch emit.rs (#128, #134, #138, #152, #174, #187, #199, #202, #214) change the MIMG sample code.

AI-assisted (Claude Code), synthetic inputs only, no game data.

🤖 Generated with Claude Code

…ip by its own texels

A pixel shader's image_sample_o with an implicit LOD and a linear mip filter blends two
mips; the console moves each one's texel coordinate by the offset in that mip's texels.
XCL moved both by the offset / the size of the rounded LOD's mip, so the second mip read
a texel off when the offset was half its texel or less. Now both mips are sampled with
SampleLevel (each offset by its own size) and blended by the LOD's fraction.

The per-mip form is taken when it is exact: MIP_FILTER linear, not anisotropic (as
desc::sampler_d3d12 decides), and LOD >= 1 or min = mag filter (a level-0 SampleLevel
filters with the mag filter). Otherwise the single Sample is kept as before. A clamp
past the last mip samples the last mip.

texel_offset / sample_coord / moved_coords are shared with the existing offset path.
Test harness: draw textures with several mips (runner::draw_views, ViewTexture data =
the levels one after the other).

Test: gpu_ps_image_sample_offset_implicit_lod_between_mips (LOD 0.25 between 8x8 and
4x4, offset +1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@artor410
artor410 force-pushed the recomp/sample-offset-per-mip branch from 20271d4 to 8a98da6 Compare November 23, 2026 21:26
ivall added a commit that referenced this pull request Nov 23, 2026
@ivall
ivall merged commit a6f5368 into ivall:main Nov 23, 2026
ivall added a commit that referenced this pull request Nov 23, 2026
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Merged (main 60565c4), your rebased head; main had merged the earlier head with draw_views(vs, gs, ...), the runner now has your draw_gs_views form. Checked on an RX 9070 (RDNA4): cargo build --workspace && cargo test --workspace -- --test-threads=4 - 611 unit tests passed, 0 failed; the retail game still reaches its first menu at 30 fps.

Kalmarv added a commit to Kalmarv/xcl that referenced this pull request Nov 23, 2026
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>
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.

2 participants