fix(agc): bind a T# minimum LOD through an S# that holds the LOD at 0 #12980

Merged
Gonic0 merged 2 commits from upstream-pr/12980 into main 2026-11-02 22:35:28 +00:00
Gonic0 commented 2026-10-29 18:56:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-10-29T18:56:54Z.


What

Builds on #12662. Until it merges, this branch carries its commits; only 4e2a028e43 is new here.

#12662 applies a T#'s MIN_LOD clamp in the shader when VK_EXT_image_view_min_lod is missing (MoltenVK). It throws when a paired S# has a MAX_LOD below the clamp, because the shader's MinLod would then exceed the sampler's maximum. That includes an S# with no mip filter, which holds the LOD at 0.

For such an S# the view clamp has a defined result. The sampler clamps λ to 0, so the texel is magnified and the level is d' = max(level_base + 0, level_base + c). That level is rounded to the nearest one with no mip filter, or blended with a linear one. An S# with MIN_LOD = MAX_LOD = c and the same mip filter reaches the same level. The only difference is filter choice: λ = c > 0 picks the minification filter, where the view clamp picks the magnification filter, so the variant is used only when the two filters are the same.

  • Driver: applyAnisoOverride becomes applySamplerVariants. For each S# that holds the LOD at 0 (HoldsLodAtZero: not unnormalized, and no mip filter or MAX_LOD 0) and is paired with an image with a clamp, it binds PinnedLodSamplerWords:

    • MIN_LOD = MAX_LOD = the relative clamp;
    • mip filter kept, with none becoming point.

    The shader constant from #12662 stays the clamp, so lodmin = lodmax = c.

  • Builder: such an S# no longer throws the MAX_LOD, filter and bias rejections, since the variant makes them hold. It still throws in these cases:

    • its magnification and minification filters differ, anisotropy included;
    • it also samples a table image;
    • it also samples a mipmapped image with another clamp.

    An image with a single-level view may share the S#, because the variant still reads its only level with the same filter.

The view clamp's filter for such an S# also differs between vendors. On an AMD RX 9070 XT (amdvlk), the image-view lane sampled a T# MIN_LOD of 3.0 through an S# with bilinear magnification and point minification with the point filter (run); the spec and the NVIDIA run use the magnification filter. The console's gfx1013 is unmeasured, so the shader lane throws there instead of picking one.

#12662's existing rejections stay: image_get_lod, gathers at a clamp of one level or more, run-time S#s, and the emulated samples.

PPSA21564 (ASTRO BOT 01.007.000) needs this. A pixel shader samples one image with a T# MIN_LOD of 3.0 through two S#s: one mipmapped, and one that is all zeros, so it has no mip filter. The all-zero S# also samples a single-level image.

An instruction _cl clamp above the T# clamp through such an S# makes lodmin > lodmax here, which Vulkan leaves undefined. The view clamp is undefined there too (_cl > 0 against MAX_LOD 0). Before this change the shader lane threw instead.

Tested

Apple M5 Pro, macOS 27.2, MoltenVK from Vulkan SDK 1.4.363, x86-64 Release under Rosetta 2. main alone does not build on macOS. I ran the tests in a tree with the open macOS port PRs; this commit's changes are identical there.

agc_driver_image_sample_lod_clamp_tests passes in the shader lane, with VK_LAYER_KHRONOS_validation loaded and no VUID- messages. A new wave32 program, assembled with llvm-mc (gfx1013), samples one T# through a mipmapped S# and through a second S#, and a second T# through the second S#. These pass in both lanes:

  • the second S# all zeros, as in the game;
  • the second S# with a point mip filter and MAX_LOD 0, over BASE_LEVEL 1;
  • both filters bilinear, checked against a bilinear reference on x and y;
  • an S# LOD bias of -1 under an explicit LOD;
  • a second T# with the same clamp.

In the shader lane only: a linear mip filter with MAX_LOD 0 at a fractional clamp (1.25), checked as a blend of levels 1 and 2 on the level, x and y channels. The view clamp may use minLodInteger there, so the image-view lane cannot assert one result. There is also a rejection with an exact message: a second mipmapped T# with another clamp.

Negative controls, each of which fails a test:

  • a variant with MIN/MAX LOD 0;
  • forcing the mip filter to point;
  • no other-clamp check.

One-off experiments, not asserted by a test: no filter check, a filter check on point against bilinear only, and one on anisotropy only each let an S# with differing filters through.

PPSA21564 01.007.000: the shader no longer throws. Together with #12724, the game ran for the full 900 s of the run, through the intro after the title, where it previously stopped at about 400 s.

Not tested on Linux, Windows, lavapipe, or a device with VK_EXT_image_view_min_lod.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #12662
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M5 Pro, MoltenVK (Vulkan SDK 1.4.363), macOS 27.2
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5073862366 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12980). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-10-29T18:56:54Z. --- ### What **Builds on #12662.** Until it merges, this branch carries its commits; only `4e2a028e43` is new here. #12662 applies a T#'s MIN_LOD clamp in the shader when `VK_EXT_image_view_min_lod` is missing (MoltenVK). It throws when a paired S# has a MAX_LOD below the clamp, because the shader's `MinLod` would then exceed the sampler's maximum. That includes an S# with no mip filter, which holds the LOD at 0. For such an S# the view clamp has a defined result. The sampler clamps λ to 0, so the texel is magnified and the level is `d' = max(level_base + 0, level_base + c)`. That level is rounded to the nearest one with no mip filter, or blended with a linear one. An S# with MIN_LOD = MAX_LOD = c and the same mip filter reaches the same level. The only difference is filter choice: λ = c > 0 picks the minification filter, where the view clamp picks the magnification filter, so the variant is used only when the two filters are the same. - **Driver:** `applyAnisoOverride` becomes `applySamplerVariants`. For each S# that holds the LOD at 0 (`HoldsLodAtZero`: not unnormalized, and no mip filter or MAX_LOD 0) and is paired with an image with a clamp, it binds `PinnedLodSamplerWords`: - MIN_LOD = MAX_LOD = the relative clamp; - mip filter kept, with none becoming point. The shader constant from #12662 stays the clamp, so `lodmin = lodmax = c`. - **Builder:** such an S# no longer throws the MAX_LOD, filter and bias rejections, since the variant makes them hold. It still throws in these cases: - its magnification and minification filters differ, anisotropy included; - it also samples a table image; - it also samples a mipmapped image with another clamp. An image with a single-level view may share the S#, because the variant still reads its only level with the same filter. The view clamp's filter for such an S# also differs between vendors. On an AMD RX 9070 XT (amdvlk), the image-view lane sampled a T# MIN_LOD of 3.0 through an S# with bilinear magnification and point minification with the point filter ([run](https://github.com/boykopovar/AnyPS5/pull/12980#issuecomment-6518850809)); the spec and the NVIDIA run use the magnification filter. The console's gfx1013 is unmeasured, so the shader lane throws there instead of picking one. #12662's existing rejections stay: `image_get_lod`, gathers at a clamp of one level or more, run-time S#s, and the emulated samples. PPSA21564 (ASTRO BOT 01.007.000) needs this. A pixel shader samples one image with a T# MIN_LOD of 3.0 through two S#s: one mipmapped, and one that is all zeros, so it has no mip filter. The all-zero S# also samples a single-level image. An instruction `_cl` clamp above the T# clamp through such an S# makes `lodmin > lodmax` here, which Vulkan leaves undefined. The view clamp is undefined there too (`_cl` > 0 against MAX_LOD 0). Before this change the shader lane threw instead. ### Tested Apple M5 Pro, macOS 27.2, MoltenVK from Vulkan SDK 1.4.363, x86-64 Release under Rosetta 2. `main` alone does not build on macOS. I ran the tests in a tree with the open macOS port PRs; this commit's changes are identical there. `agc_driver_image_sample_lod_clamp_tests` passes in the shader lane, with `VK_LAYER_KHRONOS_validation` loaded and no `VUID-` messages. A new wave32 program, assembled with `llvm-mc` (gfx1013), samples one T# through a mipmapped S# and through a second S#, and a second T# through the second S#. These pass in both lanes: - the second S# all zeros, as in the game; - the second S# with a point mip filter and MAX_LOD 0, over BASE_LEVEL 1; - both filters bilinear, checked against a bilinear reference on x and y; - an S# LOD bias of -1 under an explicit LOD; - a second T# with the same clamp. In the shader lane only: a linear mip filter with MAX_LOD 0 at a fractional clamp (1.25), checked as a blend of levels 1 and 2 on the level, x and y channels. The view clamp may use `minLodInteger` there, so the image-view lane cannot assert one result. There is also a rejection with an exact message: a second mipmapped T# with another clamp. Negative controls, each of which fails a test: - a variant with MIN/MAX LOD 0; - forcing the mip filter to point; - no other-clamp check. One-off experiments, not asserted by a test: no filter check, a filter check on point against bilinear only, and one on anisotropy only each let an S# with differing filters through. PPSA21564 01.007.000: the shader no longer throws. Together with #12724, the game ran for the full 900 s of the run, through the intro after the title, where it previously stopped at about 400 s. Not tested on Linux, Windows, lavapipe, or a device with `VK_EXT_image_view_min_lod`. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #12662 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M5 Pro, MoltenVK (Vulkan SDK 1.4.363), macOS 27.2 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12980
No description provided.