fix(agc): apply the sampler LOD bias in the shader where the portability subset disables it #20030

Open
XIIYAZ wants to merge 1 commit from upstream-pr/20030 into main
XIIYAZ commented 2026-11-14 12:27:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XIIYAZ on 2026-11-14T12:27:20Z.


What

On MoltenVK, a guest sampler's LOD bias is lost. VK_KHR_portability_subset reports samplerMipLodBias = VK_FALSE, so VkSamplerCreateInfo::mipLodBias must be 0 there and the driver ignores any other value. agc_driver_image_sample_lod_bias_sum, agc_driver_image_sample_lod_clamp and agc_driver_image_sample_lod_clamp_shader_lane read the wrong mip level as a result. The shader already has a path that applies a bias: PipelineSpecialization::SamplerLodBiasBase specialization constants, today used only for biases beyond SamplerLodBiasShaderThreshold. Setting the threshold to 0 in an experiment made all three tests pass, which confirms the cause.

When the device does not support sampler LOD bias, a non-zero bias now goes through that shader path, and the sampler is created with a bias of 0:

  • VulkanDevice reads samplerMipLodBias from the portability features it already queries. It sets SpirvTarget::samplerLodBias and Graphics::Context::samplerLodBias; both default to true, so devices without the portability subset are unchanged.
  • DescriptorBindingBuilder::Prepare specializes the bias into the shader when the device drops it. It does this only for samplers the shader can bias: not queried for LOD, not adjusted, not paired with a cube image. Other samplers keep the existing behavior.
  • Sampler writes a mipLodBias of 0 when the device drops the bias.
  • ShaderDeviceProfile rejects a target that claims sampler LOD bias on a portability device that disables it. Its source file gets VK_ENABLE_BETA_EXTENSIONS, like VulkanDevice.cpp, for the portability struct.
  • The flag is part of the target, so it is added to CacheKey. It is also added to RequestSerializer, which moves to version 58. Version 57 requests still read, with samplerLodBias = true. RequestSerializerTests and ShaderMemory move their version 57 expectations to 58 and cover reading version 57.

Tested

Setup:

  • Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0
  • full Release build with -DCMAKE_OSX_ARCHITECTURES=x86_64; tests run under Rosetta
  • ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1
  • on main with the open macOS build fixes applied

Results:

  • agc_driver_image_sample_lod_bias_sum, agc_driver_image_sample_lod_clamp and agc_driver_image_sample_lod_clamp_shader_lane fail before this change and pass after it.
  • These pass after the change: agc_shader_sampler_limits, agc_shader_device_profile, the agc_shader_disk_cache* tests, agc_lod_stats, agc_driver_image_sample_*, request_serializer, agc_shader_memory and agc_shader_memory_coherent_atomic_loads.
  • With VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (layer from the same SDK), the LOD tests pass with no VUID- messages.
  • I ran ctest -R '^(agc|shader|job)' (1578 tests, -j 2) and compared it with the same run before this change. The three LOD tests are fixed and no test that passed before fails because of this change. agc_driver_scratch_pool and agc_driver_label_after_import_copy failed in this run, but they fail the same way with this change taken out of the same tree, so they are not caused by it.

I did not run this on lavapipe, AMD or NVIDIA. On those devices samplerLodBias stays true, so the generated shaders are the same as before; only the serialized request carries one more byte.

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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5311217026 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20030). Originally posted by [XIIYAZ](https://github.com/XIIYAZ) on 2026-11-14T12:27:20Z. --- ### What On MoltenVK, a guest sampler's LOD bias is lost. `VK_KHR_portability_subset` reports `samplerMipLodBias = VK_FALSE`, so `VkSamplerCreateInfo::mipLodBias` must be 0 there and the driver ignores any other value. `agc_driver_image_sample_lod_bias_sum`, `agc_driver_image_sample_lod_clamp` and `agc_driver_image_sample_lod_clamp_shader_lane` read the wrong mip level as a result. The shader already has a path that applies a bias: `PipelineSpecialization::SamplerLodBiasBase` specialization constants, today used only for biases beyond `SamplerLodBiasShaderThreshold`. Setting the threshold to 0 in an experiment made all three tests pass, which confirms the cause. When the device does not support sampler LOD bias, a non-zero bias now goes through that shader path, and the sampler is created with a bias of 0: - `VulkanDevice` reads `samplerMipLodBias` from the portability features it already queries. It sets `SpirvTarget::samplerLodBias` and `Graphics::Context::samplerLodBias`; both default to `true`, so devices without the portability subset are unchanged. - `DescriptorBindingBuilder::Prepare` specializes the bias into the shader when the device drops it. It does this only for samplers the shader can bias: not queried for LOD, not adjusted, not paired with a cube image. Other samplers keep the existing behavior. - `Sampler` writes a `mipLodBias` of 0 when the device drops the bias. - `ShaderDeviceProfile` rejects a target that claims sampler LOD bias on a portability device that disables it. Its source file gets `VK_ENABLE_BETA_EXTENSIONS`, like `VulkanDevice.cpp`, for the portability struct. - The flag is part of the target, so it is added to `CacheKey`. It is also added to `RequestSerializer`, which moves to version 58. Version 57 requests still read, with `samplerLodBias = true`. `RequestSerializerTests` and `ShaderMemory` move their version 57 expectations to 58 and cover reading version 57. ### Tested Setup: - Apple M4, macOS 26.7.3, MoltenVK 1.4.2 (LunarG Vulkan SDK 1.4.363.1), Apple clang 21.0.0 - full Release build with `-DCMAKE_OSX_ARCHITECTURES=x86_64`; tests run under Rosetta - `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1` - on `main` with the open macOS build fixes applied Results: - `agc_driver_image_sample_lod_bias_sum`, `agc_driver_image_sample_lod_clamp` and `agc_driver_image_sample_lod_clamp_shader_lane` fail before this change and pass after it. - These pass after the change: `agc_shader_sampler_limits`, `agc_shader_device_profile`, the `agc_shader_disk_cache*` tests, `agc_lod_stats`, `agc_driver_image_sample_*`, `request_serializer`, `agc_shader_memory` and `agc_shader_memory_coherent_atomic_loads`. - With `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (layer from the same SDK), the LOD tests pass with no `VUID-` messages. - I ran `ctest -R '^(agc|shader|job)'` (1578 tests, `-j 2`) and compared it with the same run before this change. The three LOD tests are fixed and no test that passed before fails because of this change. `agc_driver_scratch_pool` and `agc_driver_label_after_import_copy` failed in this run, but they fail the same way with this change taken out of the same tree, so they are not caused by it. I did not run this on lavapipe, AMD or NVIDIA. On those devices `samplerLodBias` stays `true`, so the generated shaders are the same as before; only the serialized request carries one more byte. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M4 (MoltenVK 1.4.2, LunarG Vulkan SDK 1.4.363.1, macOS 26.7.3) - [x] AI-assisted: yes
MarcZX10 commented 2026-11-14 14:32:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T14:32:44Z.

Review: COMMENTED


Reviewed — no blocking findings

This review could not inspect the prepared change or verify its behavior because the repository reader was unavailable in this session.

What I ran

No commands were run; the reader could not be invoked.

Results

Check Result Note
Change review not run Prepared review files not read
Tests not run No checks executed

Findings

Blocking

None verified.

Non-blocking

None.

Not verified

The description, commits, diff, manifest, changed file contents, and test coverage were not read. Files reviewed: none; files excluded: all changed files (not determined).

To get this to a good state

Nothing is required.

Continued review (pass 2)

This continuation pass read the listed added lines and their immediate context at head 412ade641581ce86da96336125462735ad885ba3; it does not cover the full pull request, so a formal merge decision is not justified.

What I ran

No commands or tests were run. I read the requested continuation lines from VulkanDevice.cpp, ShaderDeviceProfile.cpp, Sampler.cpp, Context.hpp, CMakeLists.txt, ShaderMemory.cpp, CacheKey.hpp, RequestSerializer.cpp, DescriptorBindingBuilder.hpp, DescriptorBindingBuilder.cpp, Recompiler.cpp, Recompiler.hpp, and RequestSerializerTests.cpp, plus the corresponding diff hunks. VulkanDevice.cpp was read through the pinned repository reader because its full text was excluded from the review folder.

Results

Check Result Note
Requested continuation lines passed Read with surrounding context
Tests not run No test execution in this pass
Full pull request not run This pass covers only the specified lines

Findings

Blocking

None in the lines reviewed.

Non-blocking

None in the lines reviewed.

Not verified

The rest of the pull request and the earlier pass’s findings were not reviewed in this continuation. No build, test suite, or runtime behavior was verified.

To get this to a good state

Nothing is required for the lines reviewed. Optional points: none.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5904957028 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20030#pullrequestreview-5904957028). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-14T14:32:44Z. **Review:** COMMENTED --- # Reviewed — no blocking findings This review could not inspect the prepared change or verify its behavior because the repository reader was unavailable in this session. ## What I ran No commands were run; the reader could not be invoked. ## Results | Check | Result | Note | |---|---|---| | Change review | not run | Prepared review files not read | | Tests | not run | No checks executed | ## Findings ### Blocking None verified. ### Non-blocking None. ## Not verified The description, commits, diff, manifest, changed file contents, and test coverage were not read. Files reviewed: none; files excluded: all changed files (not determined). ## To get this to a good state Nothing is required. ## Continued review (pass 2) This continuation pass read the listed added lines and their immediate context at head `412ade641581ce86da96336125462735ad885ba3`; it does not cover the full pull request, so a formal merge decision is not justified. ## What I ran No commands or tests were run. I read the requested continuation lines from `VulkanDevice.cpp`, `ShaderDeviceProfile.cpp`, `Sampler.cpp`, `Context.hpp`, `CMakeLists.txt`, `ShaderMemory.cpp`, `CacheKey.hpp`, `RequestSerializer.cpp`, `DescriptorBindingBuilder.hpp`, `DescriptorBindingBuilder.cpp`, `Recompiler.cpp`, `Recompiler.hpp`, and `RequestSerializerTests.cpp`, plus the corresponding diff hunks. `VulkanDevice.cpp` was read through the pinned repository reader because its full text was excluded from the review folder. ## Results | Check | Result | Note | |---|---|---| | Requested continuation lines | passed | Read with surrounding context | | Tests | not run | No test execution in this pass | | Full pull request | not run | This pass covers only the specified lines | ## Findings ### Blocking None in the lines reviewed. ### Non-blocking None in the lines reviewed. ## Not verified The rest of the pull request and the earlier pass’s findings were not reviewed in this continuation. No build, test suite, or runtime behavior was verified. ## To get this to a good state Nothing is required for the lines reviewed. Optional points: none.
kurexw commented 2026-11-17 01:37:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-17T01:37:46Z.

Review: COMMENTED


Tested on an Apple M4 alongside #16953, in four variants with the same tests and environment.

Setup: Apple M4, macOS 27.2, MoltenVK 1.4.2 from the LunarG Vulkan SDK 1.4.363.1, x86-64 build under Rosetta 2. This device reports samplerMipLodBias = false (portability subset).

Base and variants:

Tests: the 29 GPU tests that fail on main on this Mac, via ctest --test-dir build -R '<those 29>' -V, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and VK_LOADER_DEBUG=layer. Three runs per variant. Each run logged Insert instance layer "VK_LAYER_KHRONOS_validation" 32 times (layer 1.4.363) and never the missing-layer message.

base #20030 #16953 both
agc_driver_image_sample_lod_bias_sum Failed Passed Failed Passed
agc_driver_image_sample_lod_clamp Failed Passed Failed Passed
agc_driver_image_sample_lod_clamp_shader_lane Failed Passed Failed Passed
VUID-VkSamplerCreateInfo-samplerMipLodBias-04467 per run 6 0 6 0
VUID-VkDeviceCreateInfo-sType-unique per run (Recorder test device, fixed separately in #21965) 4 4 4 4
other 25 tests fail same same same

One of the 25 is timing-dependent here. agc_driver_label_after_import_copy fails as "round N: a CPU read after the label that followed the copy found 64 of 262144 destination bytes not yet copied" on every variant, including the base:

base #20030 #16953 both
failed / runs (all runs I made) 5/23 5/13 14/19 3/3

It is not a test either PR targets, and it fails on the base too. I'm listing it because #16953 fails it noticeably more often on this machine. I can't tell from these counts whether that comes from the change or from timing.

Not tested: Linux, Windows or another GPU.

Result for this PR: on a device without samplerMipLodBias, the shader-side bias fixes all three LOD tests and removes every samplerMipLodBias-04467 validation error, with or without #16953. No other test in the set changes, apart from the timing-dependent label test above. #16953 alone changes none of the three LOD tests here, so on MoltenVK this PR is the one that fixes them.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5931202877 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20030#pullrequestreview-5931202877). Originally posted by [kurexw](https://github.com/kurexw) on 2026-11-17T01:37:46Z. **Review:** COMMENTED --- Tested on an Apple M4 alongside #16953, in four variants with the same tests and environment. **Setup:** Apple M4, macOS 27.2, MoltenVK 1.4.2 from the LunarG Vulkan SDK 1.4.363.1, x86-64 build under Rosetta 2. This device reports `samplerMipLodBias = false` (portability subset). **Base and variants:** - #20030 conflicts with `main` at 939d3630ed, in `RequestSerializer.cpp`, `RequestSerializerTests.cpp` and `ShaderMemory.cpp`. - #16953's commits don't cherry-pick onto #20030's merge-base (0f4538fa02), but both PRs merge cleanly onto #16953's merge-base, 27b8778d4f (2026-11-16). - So every variant is built on 27b8778d4f, with no conflict resolved by hand: - **base**: 27b8778d4f - **#20030**: 27b8778d4f merged with the #20030 head (412ade6415) - **#16953**: the #16953 head (edac4156bd) - **both**: the #16953 head merged with #20030 - None of this was run on current `main`. **Tests:** the 29 GPU tests that fail on `main` on this Mac, via `ctest --test-dir build -R '<those 29>' -V`, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` and `VK_LOADER_DEBUG=layer`. Three runs per variant. Each run logged `Insert instance layer "VK_LAYER_KHRONOS_validation"` 32 times (layer 1.4.363) and never the missing-layer message. | | base | #20030 | #16953 | both | | --- | --- | --- | --- | --- | | `agc_driver_image_sample_lod_bias_sum` | Failed | **Passed** | Failed | **Passed** | | `agc_driver_image_sample_lod_clamp` | Failed | **Passed** | Failed | **Passed** | | `agc_driver_image_sample_lod_clamp_shader_lane` | Failed | **Passed** | Failed | **Passed** | | `VUID-VkSamplerCreateInfo-samplerMipLodBias-04467` per run | 6 | **0** | 6 | **0** | | `VUID-VkDeviceCreateInfo-sType-unique` per run (Recorder test device, fixed separately in #21965) | 4 | 4 | 4 | 4 | | other 25 tests | fail | same | same | same | One of the 25 is timing-dependent here. `agc_driver_label_after_import_copy` fails as "round N: a CPU read after the label that followed the copy found 64 of 262144 destination bytes not yet copied" on every variant, including the base: | | base | #20030 | #16953 | both | | --- | --- | --- | --- | --- | | failed / runs (all runs I made) | 5/23 | 5/13 | 14/19 | 3/3 | It is not a test either PR targets, and it fails on the base too. I'm listing it because #16953 fails it noticeably more often on this machine. I can't tell from these counts whether that comes from the change or from timing. Not tested: Linux, Windows or another GPU. **Result for this PR:** on a device without `samplerMipLodBias`, the shader-side bias fixes all three LOD tests and removes every `samplerMipLodBias-04467` validation error, with or without #16953. No other test in the set changes, apart from the timing-dependent label test above. #16953 alone changes none of the three LOD tests here, so on MoltenVK this PR is the one that fixes them.
ligmaSec commented 2026-12-03 18:36:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T18:36:45Z.

Review: COMMENTED


When portability requires a Vulkan sampler bias of zero, DescriptorBindingBuilder::Prepare can silently discard an in-range guest bias for a cube sampler, LOD query, or adjusted use. The branch description calls out these exclusions. The existing diagnostic for an above-threshold bias already detects the same unsupported uses. Could the portability path use that diagnostic when the bias must move into the shader, so an unsupported draw cannot quietly select a different mip level?

Source review of 412ade6415. Current main conflicts with the shader binding and request-serialization changes. The MoltenVK execution evidence is from the author's run.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6176095832 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20030#pullrequestreview-6176095832). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T18:36:45Z. **Review:** COMMENTED --- When portability requires a Vulkan sampler bias of zero, `DescriptorBindingBuilder::Prepare` can silently discard an in-range guest bias for a cube sampler, LOD query, or adjusted use. The branch description calls out these exclusions. The existing diagnostic for an above-threshold bias already detects the same unsupported uses. Could the portability path use that diagnostic when the bias must move into the shader, so an unsupported draw cannot quietly select a different mip level? Source review of `412ade6415`. Current `main` conflicts with the shader binding and request-serialization changes. The MoltenVK execution evidence is from the author's run. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/20030:upstream-pr/20030'
git switch 'upstream-pr/20030'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/20030'
git switch 'upstream-pr/20030'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/20030'
git switch 'upstream-pr/20030'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/20030'
git switch 'main'
git merge --squash 'upstream-pr/20030'
git switch 'main'
git merge --ff-only 'upstream-pr/20030'
git switch 'main'
git merge 'upstream-pr/20030'
git push origin 'main'
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!20030
No description provided.