fix(agc): keep sRGB storage images in their UNORM twin on every driver #10859

Merged
maximilianfv merged 1 commit from upstream-pr/10859 into main 2026-11-02 10:24:48 +00:00
maximilianfv commented 2026-10-25 05:51:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-25T05:51:28Z.


What

StorageFormatOrUndefined (libSceAgcDriver/Graphics/src/Texture.cpp) kept an 8-bit sRGB texture in its sRGB format for storage whenever the driver reported VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT for it, and used the UNORM twin only as a fallback. The Vulkan spec applies a linear-to-sRGB encode when a shader stores to an sRGB storage image (Texel Encode), so a guest store of raw code 8 lands as 49: agc_driver_image_srgb_sample_copy and its forced variant sample the decode of 49. agc_driver_recover_srgb_allocation fails differently: CanSrgbExpandFrom requires the storage format to equal the sampled UNORM format, so it refuses the sRGB storage surface as the source of an expansion copy.

Drivers that report no storage support for these formats took the UNORM fallback and never hit this. MoltenVK 1.4.2 reports it for R8_SRGB, R8G8_SRGB, R8G8B8A8_SRGB, B8G8R8A8_SRGB and A8B8G8R8_SRGB_PACK32, and is conformant in doing so; the bug is that the code relied on the driver lacking sRGB storage.

The five 8-bit sRGB formats now always use their UNORM twin for storage, before the sRGB format itself is checked. Packed formats keep their branch first. With no platform guard, drivers without sRGB storage take the same path as before. A driver that supports sRGB storage but not storage for the UNORM twin now gets VK_FORMAT_UNDEFINED instead of the sRGB format.

Prior art, credited with Co-authored-by trailers:

  • Ycaro Gabriel, commit d88533e09c (2026-10-18) in the closed whole-port PR #7332. It makes the same UNORM-first choice, but only under #ifdef __APPLE__, keeps sRGB-first on other platforms, and falls back to the sRGB format when the UNORM twin lacks storage support. It predates the packed-format branch.
  • Himansh Raj, commit e262c24be1 (2026-10-20) in the closed whole-port PR #8658. On Apple it skips the five sRGB formats in the direct check, so they reach the existing UNORM fallback; it has code comments. On Apple it behaves the same as this change. This change drops the platform guard and the comments, because the encode-on-store behaviour comes from the spec, not from MoltenVK.

Tested

main does not build on macOS by itself. I tested on mugurc's macOS series (ci/macos-series-2, 27e968974) merged with main at 522e32e3b6, plus the other open macOS fixes (#10419 build parts, #10540, #10562, #10641) and some unopened test fixes. Only this change differed between the two sets of runs.

Setup: Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2, Vulkan through MoltenVK from the LunarG Vulkan SDK 1.4.363. Every run used ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation with ctest -R '^(agc_driver_.*srgb.*|agc_driver_image_.*)$' -E 'audio_in|voice' -V (68 tests), three runs with and three without this change.

  • Without: 64/68 in every run. agc_driver_image_srgb_sample_copy, agc_driver_image_srgb_sample_copy_forced and agc_driver_recover_srgb_allocation fail. agc_driver_image_plane_bias_on_single_level_volume also fails, which is unrelated and covered by #9125.
  • With: 67/68 in every run. Only agc_driver_image_plane_bias_on_single_level_volume fails.
  • Validation: 123 Validation Error lines without and 123 with, with the same VUID set (VUID-VkDeviceCreateInfo-pProperties-04451 and others, already there before this change). Neither log contains was not found but was requested by env var VK_INSTANCE_LAYERS.
  • On MoltenVK the change also reaches the multisample paths (MultisampleTarget.cpp storage format and sameStorageForm, and StorageSrgbTwin in MultisampleRetirement.cpp no longer skips). Same setup, ctest -R '^(agc_.*multisample.*|agc_driver_.*msaa.*|image_msaa_load|agc_driver_srgb8_color_target|agc_driver_emulated_color_compare_srgb_decode|agc_driver_.*srgb.*)$' (36 tests), three runs each way: 30/36 without, 33/36 with. The three sRGB tests above are the only difference. agc_driver_multisample_retirement, agc_driver_native_multisample_depth and agc_driver_sample_mask_multisample fail both ways with the same message. agc_driver_multisample_retirement stops before StorageSrgbTwin ("the device cannot write the multisampled color target's format as a storage image at its sample count", covered by #8604), so that check is still not exercised on this host. 63 Validation Error lines both ways, same VUID set.
  • Not run on Linux or Windows.

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 M1 Pro, MoltenVK (Vulkan SDK 1.4.363), macOS 27.0.1
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5002226010 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10859). Originally posted by [maximilianfv](https://github.com/maximilianfv) on 2026-10-25T05:51:28Z. --- ### What `StorageFormatOrUndefined` (`libSceAgcDriver/Graphics/src/Texture.cpp`) kept an 8-bit sRGB texture in its sRGB format for storage whenever the driver reported `VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT` for it, and used the UNORM twin only as a fallback. The Vulkan spec applies a linear-to-sRGB encode when a shader stores to an sRGB storage image (Texel Encode), so a guest store of raw code 8 lands as 49: `agc_driver_image_srgb_sample_copy` and its forced variant sample the decode of 49. `agc_driver_recover_srgb_allocation` fails differently: `CanSrgbExpandFrom` requires the storage format to equal the sampled UNORM format, so it refuses the sRGB storage surface as the source of an expansion copy. Drivers that report no storage support for these formats took the UNORM fallback and never hit this. MoltenVK 1.4.2 reports it for `R8_SRGB`, `R8G8_SRGB`, `R8G8B8A8_SRGB`, `B8G8R8A8_SRGB` and `A8B8G8R8_SRGB_PACK32`, and is conformant in doing so; the bug is that the code relied on the driver lacking sRGB storage. The five 8-bit sRGB formats now always use their UNORM twin for storage, before the sRGB format itself is checked. Packed formats keep their branch first. With no platform guard, drivers without sRGB storage take the same path as before. A driver that supports sRGB storage but not storage for the UNORM twin now gets `VK_FORMAT_UNDEFINED` instead of the sRGB format. Prior art, credited with `Co-authored-by` trailers: - Ycaro Gabriel, commit d88533e09c (2026-10-18) in the closed whole-port PR #7332. It makes the same UNORM-first choice, but only under `#ifdef __APPLE__`, keeps sRGB-first on other platforms, and falls back to the sRGB format when the UNORM twin lacks storage support. It predates the packed-format branch. - Himansh Raj, commit e262c24be1 (2026-10-20) in the closed whole-port PR #8658. On Apple it skips the five sRGB formats in the direct check, so they reach the existing UNORM fallback; it has code comments. On Apple it behaves the same as this change. This change drops the platform guard and the comments, because the encode-on-store behaviour comes from the spec, not from MoltenVK. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> `main` does not build on macOS by itself. I tested on mugurc's macOS series (ci/macos-series-2, 27e968974) merged with `main` at 522e32e3b6, plus the other open macOS fixes (#10419 build parts, #10540, #10562, #10641) and some unopened test fixes. Only this change differed between the two sets of runs. Setup: Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2, Vulkan through MoltenVK from the LunarG Vulkan SDK 1.4.363. Every run used `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` with `ctest -R '^(agc_driver_.*srgb.*|agc_driver_image_.*)$' -E 'audio_in|voice' -V` (68 tests), three runs with and three without this change. - Without: 64/68 in every run. `agc_driver_image_srgb_sample_copy`, `agc_driver_image_srgb_sample_copy_forced` and `agc_driver_recover_srgb_allocation` fail. `agc_driver_image_plane_bias_on_single_level_volume` also fails, which is unrelated and covered by #9125. - With: 67/68 in every run. Only `agc_driver_image_plane_bias_on_single_level_volume` fails. - Validation: 123 `Validation Error` lines without and 123 with, with the same VUID set (`VUID-VkDeviceCreateInfo-pProperties-04451` and others, already there before this change). Neither log contains `was not found but was requested by env var VK_INSTANCE_LAYERS`. - On MoltenVK the change also reaches the multisample paths (`MultisampleTarget.cpp` storage format and `sameStorageForm`, and `StorageSrgbTwin` in `MultisampleRetirement.cpp` no longer skips). Same setup, `ctest -R '^(agc_.*multisample.*|agc_driver_.*msaa.*|image_msaa_load|agc_driver_srgb8_color_target|agc_driver_emulated_color_compare_srgb_decode|agc_driver_.*srgb.*)$'` (36 tests), three runs each way: 30/36 without, 33/36 with. The three sRGB tests above are the only difference. `agc_driver_multisample_retirement`, `agc_driver_native_multisample_depth` and `agc_driver_sample_mask_multisample` fail both ways with the same message. `agc_driver_multisample_retirement` stops before `StorageSrgbTwin` ("the device cannot write the multisampled color target's format as a storage image at its sample count", covered by #8604), so that check is still not exercised on this host. 63 `Validation Error` lines both ways, same VUID set. - Not run on Linux or Windows. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: Apple M1 Pro, MoltenVK (Vulkan SDK 1.4.363), macOS 27.0.1 <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 05:53:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T05:53:50Z.

Review: APPROVED


Reviewed exact head 290f9eed01224dd071752c4ad0cd0657d772602b against current main.

The five 8-bit sRGB formats now select their UNORM twin before querying the sRGB format's storage support, while the existing packed-format branch remains first. This matches Vulkan's image-write rule: stores to sRGB formats apply linear-to-sRGB encoding, whereas the guest storage path needs the raw encoded byte values. Using the UNORM twin avoids the extra encode and also lets the sRGB expansion path recognize the source format. If the twin itself lacks storage support, returning VK_FORMAT_UNDEFINED is preferable to using an sRGB view with different write semantics. The Vulkan spec documents this in Texel Encode.

The PR reports same-composition MoltenVK controls with three runs per side: the three targeted regressions fail without the change and pass with it; the unrelated #9125 failure and the same validation VUID set remain on both sides. I did not independently run the GPU tests. Hosted eligibility, conventions, intake, labels and report checks pass; product jobs are skipped by repository policy. Linux and Windows are not reported as tested. I found no blocking issue in the format-selection change.

AI-assisted review: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641772085 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10859#pullrequestreview-5641772085). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T05:53:50Z. **Review:** APPROVED --- Reviewed exact head `290f9eed01224dd071752c4ad0cd0657d772602b` against current main. The five 8-bit sRGB formats now select their UNORM twin before querying the sRGB format's storage support, while the existing packed-format branch remains first. This matches Vulkan's image-write rule: stores to sRGB formats apply linear-to-sRGB encoding, whereas the guest storage path needs the raw encoded byte values. Using the UNORM twin avoids the extra encode and also lets the sRGB expansion path recognize the source format. If the twin itself lacks storage support, returning `VK_FORMAT_UNDEFINED` is preferable to using an sRGB view with different write semantics. The Vulkan spec documents this in [Texel Encode](https://docs.vulkan.org/spec/latest/chapters/images.html#images-texel-encode). The PR reports same-composition MoltenVK controls with three runs per side: the three targeted regressions fail without the change and pass with it; the unrelated #9125 failure and the same validation VUID set remain on both sides. I did not independently run the GPU tests. Hosted eligibility, conventions, intake, labels and report checks pass; product jobs are skipped by repository policy. Linux and Windows are not reported as tested. I found no blocking issue in the format-selection change. AI-assisted review: yes.
jotitadev commented 2026-10-27 06:33:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T06:33:09Z.

Review: COMMENTED


A second Apple GPU, on a main-based tree, with both MoltenVK builds.

Machine: Apple M2 Max, macOS 27.2, x86_64 Release build under Rosetta 2.
Tree: main b986c11a34 + the open macOS PRs needed to build and open a device (#10366, #7631, #10659, #10660, #11249, #11358, #10669, #10552, #7804, #10676, #11447, #11437, #11554, #11705) + this PR at 290f9eed01 (clean merge). Control: the same tree without this PR. ANYPS5_NO_SHADER_CACHE=1, -j1 --timeout 30.
Drivers: MoltenVK 1.4.2 (LunarG SDK 1.4.363.0) and MoltenVK main + SPIRV-Cross #2704.

ctest -R srgb (7 tests) Without With (3 runs)
MoltenVK 1.4.2 image_srgb_sample_copy and _forced fail 7/7 pass (1 skip, see below), 3/3
MoltenVK main + #2704 same 2 fail same, 3/3
  • Full ^agc suite, 1037 tests, MoltenVK 1.4.2: 41 failures without, 39 with. The two sRGB tests are the only difference, so nothing new fails.
  • Validation layer (VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, SDK 1.4.363.0, loader output shows it inserted) on the same 7 tests: the same single VUID on both sides, VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 ×2 from agc_driver_srgb8_color_target, which this PR does not touch. No new VUID-.
  • agc_driver_recover_srgb_allocation cannot be checked on a main-based macOS tree yet: it runs from agc_driver_sampled_cache_budget_tests, which loads libvulkan.so.1, so it reports "skipped, no usable Vulkan device: AGC graphics: cannot load Vulkan" (and fails with that message under ANYPS5_REQUIRE_VULKAN=1) with and without this PR. Your 64→67 result for it came from the series tree with your extra test fixes; that file is one of the ones #10920 touches.

GPUs tested: Apple M2 Max (MoltenVK 1.4.2 and MoltenVK main + #2704)

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5663109683 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10859#pullrequestreview-5663109683). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-27T06:33:09Z. **Review:** COMMENTED --- A second Apple GPU, on a `main`-based tree, with both MoltenVK builds. **Machine:** Apple M2 Max, macOS 27.2, x86_64 Release build under Rosetta 2. **Tree:** `main` `b986c11a34` + the open macOS PRs needed to build and open a device (#10366, #7631, #10659, #10660, #11249, #11358, #10669, #10552, #7804, #10676, #11447, #11437, #11554, #11705) + this PR at `290f9eed01` (clean merge). Control: the same tree without this PR. `ANYPS5_NO_SHADER_CACHE=1`, `-j1 --timeout 30`. **Drivers:** MoltenVK 1.4.2 (LunarG SDK 1.4.363.0) and MoltenVK `main` + SPIRV-Cross #2704. | `ctest -R srgb` (7 tests) | Without | With (3 runs) | |---|---|---| | MoltenVK 1.4.2 | `image_srgb_sample_copy` and `_forced` fail | 7/7 pass (1 skip, see below), 3/3 | | MoltenVK main + #2704 | same 2 fail | same, 3/3 | - Full `^agc` suite, 1037 tests, MoltenVK 1.4.2: 41 failures without, 39 with. The two sRGB tests are the only difference, so nothing new fails. - Validation layer (`VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, SDK 1.4.363.0, loader output shows it inserted) on the same 7 tests: the same single VUID on both sides, `VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605` ×2 from `agc_driver_srgb8_color_target`, which this PR does not touch. No new `VUID-`. - `agc_driver_recover_srgb_allocation` cannot be checked on a `main`-based macOS tree yet: it runs from `agc_driver_sampled_cache_budget_tests`, which loads `libvulkan.so.1`, so it reports "skipped, no usable Vulkan device: AGC graphics: cannot load Vulkan" (and fails with that message under `ANYPS5_REQUIRE_VULKAN=1`) with and without this PR. Your 64→67 result for it came from the series tree with your extra test fixes; that file is one of the ones #10920 touches. GPUs tested: Apple M2 Max (MoltenVK 1.4.2 and MoltenVK main + #2704) Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!10859
No description provided.