Skip to content

fix(translator): clamp explicit forms and _b_o bias to S# word 1 MIN_LOD/MAX_LOD in --texel-offset-fallback (SHADER-491) - #2783

Open
shadshuvo wants to merge 1 commit into
ItsNotRexed:mainfrom
shadshuvo:fix/texoff-fallback-sampler-lod-clamp
Open

shadshuvo wants to merge 1 commit into
ItsNotRexed:mainfrom
shadshuvo:fix/texoff-fallback-sampler-lod-clamp

Conversation

@shadshuvo

Copy link
Copy Markdown

Summary

Implements SHADER-491 (follow-up to @Jolymmiles's finding in #2516).

In --texel-offset-fallback (Gen.texel_offset_fold), texel offsets for image_sample*_o are folded into the UV coordinates by dividing by the size of the sampled level.

Previously:

  • Explicit forms (_l_o, _lz_o, _c_lz_o, and explicit LOD 0 outside PS) ignored the sampler descriptor's [MIN_LOD, MAX_LOD] bounds. For example, if a sampler had MIN_LOD = 1.0, an image_sample_lz_o operation would calculate the texel shift using level 0's dimensions while the GPU hardware sampled from level 1 (halving the effective offset).
  • _b_o added its instruction bias after OpImageQueryLod had already applied the sampler clamp, potentially overshooting MAX_LOD or falling below MIN_LOD.

Changes

  • In tools/rdna2spirv.py:
    • Passed smp_word1 (a[3]["r"] + 1) to texel_offset_fold.
    • Clamped explicit LOD (_l_o) and biased LOD (_b_o) to S# word 1's [MIN_LOD, MAX_LOD] using clamp_gather_lod.
    • For _lz_o and _c_lz_o (requested LOD 0.0): if S# word 1 is a literal constant with MIN_LOD == 0.0, emit level 0 directly with zero instruction overhead; otherwise clamp LOD 0.0 to [MIN_LOD, MAX_LOD].
  • In docs/design.md:
    • Updated the contract table for texel offset fallback rules.
  • In tests/test_texel_offsets.py:
    • Added 5 new regression tests covering literal MIN_LOD clamping, literal MAX_LOD clamping, literal zero MIN_LOD level-0 bypass, runtime sampler bounds extraction, and bias clamping.

Testing

  • python -m compileall -q make_game.py tools tests: OK
  • python -I make_game.py --help: OK
  • python tests/semantic/regen_fixtures.py --check: OK
  • python tests/test_shader_checks.py: 60/60 OK
  • pytest -v tests/test_texel_offsets.py: 26 passed, 9 skipped (0 regressions, 5 new tests)
  • python -m unittest discover -s tests/semantic: 2,567 passed, 3 skipped (0 regressions)
  • Hardware: Verified on AMD Radeon RX 7800 XT (driver 24.20.19.05 / Vulkan 1.4.359 / Windows 11).
  • AI-assisted. Standard library / Python only; no game content or carved assets.

Item: SHADER-491
Claim: #2716 (comment)

@padhossein

Copy link
Copy Markdown
Contributor

Independent nearest-mipmap fractional clamp check (5012b848). All 35 offset tests passed. I found a remaining sampling case on Mesa llvmpipe 26.1.6 with Khronos validation. Using a synthetic 128x128 / 64x64 texture whose mip 1 encodes its X coordinate in red, point filtering and nearest mip selection, I sampled at u=10.25/64 with an integer X offset of +1. At MIN_LOD=0.75, the native maintenance8 Offset path returns texel X=11 on 4096/4096 pixels and the fallback returns X=10 on 4096/4096. An independent explicit +1/64 shift without Offset also returns X=11. At MIN_LOD=1.0 all three agree. Both shaders passed validation. The fallback floors the clamped 0.75, so the shift uses the 128-wide level while hardware samples the 64-wide level. Could we add a fractional nearest-mip regression and select the nearest mip for this scaling when the sampler uses nearest filtering?

Synthetic inputs only. AI-assisted (Codex).

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted independent RTX 5070 readback (driver 616.56). Synthetic raw LOD 2, instruction bias −1, sampler bounds [0,1], offset (+2,0). Parent 4947579 and head 5012b84 both sampled mip 1 at all 4,096 pixels. Both produced the RGBA value for a one-texel shift throughout. Only the 64 pixels in the clamped rightmost column matched the expected two-texel shift. The native Vulkan textureOffset control matched all 4,096 pixels. All three shaders passed Vulkan 1.2 spirv-val and created graphics pipelines. The fallback adds bias to the sampler-clamped query, selecting mip 0 dimensions when scaling the offset. This is a second failure case alongside the fractional clamp review.

…LOD/MAX_LOD in --texel-offset-fallback (SHADER-491)

In --texel-offset-fallback (Gen.texel_offset_fold), texel offsets for
image_sample*_o are folded into the UV coordinates by dividing by the
size of the sampled level.

Previously, explicit forms (_l_o, _lz_o, _c_lz_o, and explicit LOD 0
outside PS) ignored the sampler descriptor's [MIN_LOD, MAX_LOD] bounds,
and _b_o added its instruction bias after OpImageQueryLod had already
applied sampler clamping, potentially overshooting MAX_LOD or falling
below MIN_LOD.

Fix this by reading MIN_LOD and MAX_LOD from S# word 1 (unsigned 4.8
fixed-point, bits [11:0] and [23:12]):
- Clamp explicit LOD (_l_o) and biased LOD (_b_o) using clamp_gather_lod.
- For _lz_o and _c_lz_o (LOD 0.0), if S# word 1 is literal and MIN_LOD is
  0.0, emit level 0 directly without extra instructions; otherwise clamp
  LOD 0.0 to [MIN_LOD, MAX_LOD].
- Updated docs/design.md contract.
- Added 5 new regression tests in tests/test_texel_offsets.py covering
  literal MIN_LOD, literal MAX_LOD, literal zero MIN_LOD direct level 0,
  runtime sampler clamping, and bias clamping.

Item: SHADER-491
@MiguelMatMar

Copy link
Copy Markdown

Code read of f9d6038 (CPU only, no GPU run):

  1. Unrelated change in the diff. Gen.cmp_class now applies abs/neg as raw sign-bit ops ("RDNA2 (gfx1036, ...) never quiets an sNaN or flushes a denormal on v_cmp(x)_class_f32"). It has nothing to do with SHADER-491. It's a new translator rule with no docs/design.md row and no test in this PR, and the hardware claim isn't backed by a measurement here. It would be easier to review and credit as its own item/PR (cf. rdna2spirv: v_cmp_class_f16 and v_cmpx_class_f16, measured on RDNA2 #2254 for the f16 class compares).
  2. _b_o, +1 to @kacper-serewis's readback. The bias is still added to OpImageQueryLod's component 0, which is already clamped to the image's levels (and the sampler's range). Clamping again afterwards can't undo it. It fails in both directions: raw 2, bias −1, [0,1] (their case), and raw −3, bias +2, MIN_LOD 0, where hardware gives clamp(−1) → level 0 but this gives 0 + 2 → level 2 (offset scaled 4× too small). A fix that matches the hardware order: take component 1 (the unclamped LOD, relative to the base level), add the bias, then clamp_gather_lod, then the existing floor + SClamp to the levels. @padhossein's nearest-mip rounding point applies to the same level choice, so both could land together.
  3. The PR is CONFLICTING with main and needs a rebase.

The rest (_l_o clamp, the literal fast path for _lz_o/_c_lz_o) reads correctly to me.

@jeremygold02 jeremygold02 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed @MiguelMatMar’s negative-LOD / positive-bias case on hardware at f9d60386d0fb6fcb3d1bcd0bd29d29ef60eed27a. With measured raw LOD −3, instruction bias +2 and sampler LOD bounds [0,1], native Vulkan applied offset (−1,+1) to mip 0. The fallback applied (−2,+2) to that same mip. 3,840 of 4,096 pixels differed. The remaining 256 matched because both coordinates clamped to the same edges.

Path Bias Pixels matching native Texel at pixel (36,36)
Native Vulkan Bias|Offset +2 4,096/4,096 (3,5)
Head --texel-offset-fallback +2 256/4,096 (2,6)
Fallback with only the offset calculation’s query component changed from 0 to 1 +2 4,096/4,096 (3,5)
Native and head fallback, zero-bias control 0 4,096/4,096 each (3,5)

Reproduction used hand-written image_sample_b_o with address registers [0x013f, bias, u, v], UV ((px+0.5)/64, (py+0.5)/64), point sampling, and S# words 00000092 00100000 04000000 00000000. The synthetic RGBA8 texture had 8×8 and 4×4 levels with bytes (x,y,level+1,255). Each draw exported the sampled texel alongside the unclamped image_get_lod result. All 4,096 pixels in all five draws independently reported raw LOD −3 and sampled mip 0. The harness was a scratch version of vk_texture_test from d1dea0ad with only the synthetic texture sizes and texels changed. All shaders came from the tested PR's translator except the single-component diagnostic edit, and all passed Vulkan 1.2 SPIR-V validation. Native used VK_KHR_maintenance8.

In Gen.texel_offset_fold, component 0 already reflects the sampler clamp. Adding +2 there and clamping again uses the 4×4 mip dimensions for the shift. The intended order takes raw −3 from component 1, adds +2, then clamps −1 to zero and uses the 8×8 dimensions. A regression with both signs of instruction bias would cover this case and the previously reported raw +2 / bias −1 case. The diagnostic module establishes this change for the measured case.

GPU execution used NVIDIA GeForce RTX 5070, driver 621.01 (32.0.16.2101), Intel Core Ultra 7 265K, Windows 11 Home 25H2 build 26200.9974.

AI-assisted by GPT-6.2 Astra.

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