fix(shader): emulate sRGB sampling when unsupported #5371

Closed
likecrank-dotcom wants to merge 1 commit from upstream-pr/5371 into main
likecrank-dotcom commented 2026-10-13 18:13:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by likecrank-dotcom on 2026-10-13T18:13:42Z.


What

Issue #3751 reports sampled R8_SRGB/R8G8_SRGB images failing on devices that cannot sample those Vulkan formats. This adds a shader fallback for direct, single-mip 2D images: fetch texels through the UNORM view, decode each texel from sRGB, then apply point or bilinear filtering. The shader receives the sampler state through runtime metadata in registration modules or specialization constants in specialized modules.

Unsupported shapes and sampler modes still fail with explicit diagnostics; gathers and bindless image tables remain unsupported. Updated the old sRGB load test so it no longer expects ordinary sampling to fail when the fallback is enabled. The separate fallback test checks R8 and RG8 point and bilinear results against a per-texel decode-and-filter oracle.

Tested

Windows 11 Pro, WinLibs MinGW GCC 15.2.0, CMake 4.4.4, NVIDIA GeForce RTX 5060 Ti (driver 617.14).

  • Built the shader and AGC test targets.
  • Focused CTest group: agc_shader_memory, agc_driver_image_sample_force_degamma, agc_driver_image_srgb_load, agc_driver_image_sample_srgb_fallback, and agc_driver_emulated_color_compare_srgb_decode: 5/5 passed, repeated three times (15 successful executions).
  • tools/check_conventions.py --base origin/main --head HEAD and git diff --check origin/main...HEAD passed.
  • No Ghost of Yōtei package was available; title runtime and PS5 gfx1013 equivalence were not tested. The decode-before-filter order follows the measurements cited in issue #3751.

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; gathers and unsupported fallback cases retain explicit diagnostics
  • No investigation notes or screenshots added to the repository
  • New third-party code is a submodule built from source: n/a
  • Generic behavior, not specific to one title
  • Depends on: none
  • Unblocks: none claimed; title runtime was not available
  • GPUs tested: NVIDIA GeForce RTX 5060 Ti, driver 617.14, Windows 11 Pro
  • AI-assisted: yes (OpenAI Codex); the human author remains responsible for review and submission
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4847507629 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5371). Originally posted by [likecrank-dotcom](https://github.com/likecrank-dotcom) on 2026-10-13T18:13:42Z. --- ### What Issue #3751 reports sampled R8_SRGB/R8G8_SRGB images failing on devices that cannot sample those Vulkan formats. This adds a shader fallback for direct, single-mip 2D images: fetch texels through the UNORM view, decode each texel from sRGB, then apply point or bilinear filtering. The shader receives the sampler state through runtime metadata in registration modules or specialization constants in specialized modules. Unsupported shapes and sampler modes still fail with explicit diagnostics; gathers and bindless image tables remain unsupported. Updated the old sRGB load test so it no longer expects ordinary sampling to fail when the fallback is enabled. The separate fallback test checks R8 and RG8 point and bilinear results against a per-texel decode-and-filter oracle. ### Tested Windows 11 Pro, WinLibs MinGW GCC 15.2.0, CMake 4.4.4, NVIDIA GeForce RTX 5060 Ti (driver 617.14). - Built the shader and AGC test targets. - Focused CTest group: `agc_shader_memory`, `agc_driver_image_sample_force_degamma`, `agc_driver_image_srgb_load`, `agc_driver_image_sample_srgb_fallback`, and `agc_driver_emulated_color_compare_srgb_decode`: 5/5 passed, repeated three times (15 successful executions). - `tools/check_conventions.py --base origin/main --head HEAD` and `git diff --check origin/main...HEAD` passed. - No Ghost of Yōtei package was available; title runtime and PS5 gfx1013 equivalence were not tested. The decode-before-filter order follows the measurements cited in [issue #3751](https://github.com/boykopovar/AnyPS5/issues/3751). ### 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 - [x] Unimplemented paths throw; gathers and unsupported fallback cases retain explicit diagnostics - [x] No investigation notes or screenshots added to the repository - [x] New third-party code is a submodule built from source: n/a - [x] Generic behavior, not specific to one title - [x] Depends on: none - [x] Unblocks: none claimed; title runtime was not available - [x] GPUs tested: NVIDIA GeForce RTX 5060 Ti, driver 617.14, Windows 11 Pro - [x] AI-assisted: yes (OpenAI Codex); the human author remains responsible for review and submission
coltken95 commented 2026-10-13 19:17:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-13T19:17:34Z.


Tested on Linux x86-64 (Release build, lavapipe / llvmpipe LLVM 20.1.2), with 273e2695e merged into main 18c438b10:

  • With the PR: all eight tests in the PR's targets pass, including the new agc_driver_image_sample_srgb_fallback, plus agc_driver_image_srgb_load and agc_driver_image_sample_force_degamma.
  • The other seven also pass on main; the new fallback test only exists with the PR, so there is no failing-on-main comparison.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6153278368 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5371#issuecomment-6153278368). Originally posted by [coltken95](https://github.com/coltken95) on 2026-10-13T19:17:34Z. --- Tested on Linux x86-64 (Release build, lavapipe / llvmpipe LLVM 20.1.2), with 273e2695e merged into main 18c438b10: - With the PR: all eight tests in the PR's targets pass, including the new `agc_driver_image_sample_srgb_fallback`, plus `agc_driver_image_srgb_load` and `agc_driver_image_sample_force_degamma`. - The other seven also pass on main; the new fallback test only exists with the PR, so there is no failing-on-main comparison. AI-assisted: yes (Claude Code)
adriadam10 commented 2026-10-14 18:48:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T18:48:22Z.

Review: CHANGES_REQUESTED


Measured on RDNA2 (gfx1035) with tools/hw-oracle: image_sample_lz of 8_SRGB and 8_8_SRGB with point filtering returns the bf16 SrgbToLinear table value bit for bit. That holds under wrap, clamp-to-edge and all three clamp-to-border colours, out-of-range coordinates included. Bilinear decodes before filtering and stays within 1.1e-3 of a decode-then-filter model (8-bit HW weights). So the fallback's semantics match the hardware. Build and the AgcDriver/Agc tests pass on the PR head. The PR needs to be reworked on current main before it can go in:

  • Conflicts with main that aren't mechanical. #4475 replaced ImageResource::indirectRoot/NoIndirectImage with image tables (NoTable), and the runtime image metadata block this PR extends in DescriptorBindingBuilder.cpp:448-450 no longer exists. The new table path has to decide what happens to an sRGB table entry (still zero-read, or refused).
  • main now turns unsupported images into rejectImage(PoisonReason::SrgbUnsupported, ...) in decodeImageDescriptor instead of throwing. The throws in emulatedSrgbSampleState (ResourceMaterializer.cpp:587-627) should follow that model, so an unsupported shape behaves the same way as on main.
  • The execution test only exercises clamp-to-edge without offsets (SamplerDescriptor word0 0x92). EmitEmulatedSrgbSample also implements wrap, clamp-to-border (transparent black, opaque black, white) and texel offsets, and none of these run. Please add rows with coordinates outside [0, 1] for each address mode and one with an offset. The point rows can also compare against SrgbToLinear bit for bit instead of within 0.005, because the hardware does.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5522994596 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5371#pullrequestreview-5522994596). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T18:48:22Z. **Review:** CHANGES_REQUESTED --- Measured on RDNA2 (gfx1035) with `tools/hw-oracle`: `image_sample_lz` of 8_SRGB and 8_8_SRGB with point filtering returns the bf16 `SrgbToLinear` table value bit for bit. That holds under wrap, clamp-to-edge and all three clamp-to-border colours, out-of-range coordinates included. Bilinear decodes before filtering and stays within 1.1e-3 of a decode-then-filter model (8-bit HW weights). So the fallback's semantics match the hardware. Build and the AgcDriver/Agc tests pass on the PR head. The PR needs to be reworked on current `main` before it can go in: - Conflicts with `main` that aren't mechanical. #4475 replaced `ImageResource::indirectRoot`/`NoIndirectImage` with image tables (`NoTable`), and the runtime image metadata block this PR extends in `DescriptorBindingBuilder.cpp:448-450` no longer exists. The new table path has to decide what happens to an sRGB table entry (still zero-read, or refused). - `main` now turns unsupported images into `rejectImage(PoisonReason::SrgbUnsupported, ...)` in `decodeImageDescriptor` instead of throwing. The throws in `emulatedSrgbSampleState` (`ResourceMaterializer.cpp:587-627`) should follow that model, so an unsupported shape behaves the same way as on `main`. - The execution test only exercises clamp-to-edge without offsets (`SamplerDescriptor` word0 `0x92`). `EmitEmulatedSrgbSample` also implements wrap, clamp-to-border (transparent black, opaque black, white) and texel offsets, and none of these run. Please add rows with coordinates outside [0, 1] for each address mode and one with an offset. The point rows can also compare against `SrgbToLinear` bit for bit instead of within 0.005, because the hardware does.
adriadam10 commented 2026-10-24 01:09:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-24T01:09:13Z.

Review: CHANGES_REQUESTED


Thanks for the work; the semantics are right (decode before filter matches RDNA2 point and bilinear sampling, measured earlier in this thread). The problem is that the base moved under it:

  • #6054 (merged 2026-10-18) already samples 8_SRGB / 8_8_SRGB on devices without R8_SRGB / R8G8_SRGB through an RGBA8 sRGB copy, which decodes before filtering. The shader decode now only runs when the device cannot blit R8/RG8 to RGBA8, or with APS5_SRGB_SHADER_DECODE=1; those cases still throw on main (docs/dev/debt/shader.md, "Samples and gathers"). Please say in the PR which real device hits that case. If none does, this fallback has no user and should be dropped; if one does, rebase onto main and scope the PR to it.
  • The branch conflicts with main beyond adjacency: ImageResource::indirectRoot / NoIndirectImage became image tables (NoTable), and decodeImageDescriptor / inspectImageDescriptor (ResourceMaterializer.cpp) now return rejectImage(PoisonReason::SrgbUnsupported, ...) instead of throwing. The throws in emulatedSrgbSampleState should follow that model, and the table path has to decide what an sRGB table entry does.
  • SpirvImageEmitter.cpp EmitEmulatedSrgbSample: wrap, clamp-to-border (transparent black, opaque black, white) and texel offsets are implemented but no test runs them. Add rows with coordinates outside [0, 1] for each address mode and one with an offset, and compare the point rows bit for bit against SrgbToLinear (the hardware does).
  • docs/dev/debt/shader.md: the new line must replace the current "Samples and gathers" line on main (it now describes the copy path), not the old throw line.

Happy to re-review once rebased.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5636021413 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5371#pullrequestreview-5636021413). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-24T01:09:13Z. **Review:** CHANGES_REQUESTED --- Thanks for the work; the semantics are right (decode before filter matches RDNA2 point and bilinear sampling, measured earlier in this thread). The problem is that the base moved under it: - #6054 (merged 2026-10-18) already samples 8_SRGB / 8_8_SRGB on devices without R8_SRGB / R8G8_SRGB through an RGBA8 sRGB copy, which decodes before filtering. The shader decode now only runs when the device cannot blit R8/RG8 to RGBA8, or with `APS5_SRGB_SHADER_DECODE=1`; those cases still throw on `main` (`docs/dev/debt/shader.md`, "Samples and gathers"). Please say in the PR which real device hits that case. If none does, this fallback has no user and should be dropped; if one does, rebase onto `main` and scope the PR to it. - The branch conflicts with `main` beyond adjacency: `ImageResource::indirectRoot` / `NoIndirectImage` became image tables (`NoTable`), and `decodeImageDescriptor` / `inspectImageDescriptor` (`ResourceMaterializer.cpp`) now return `rejectImage(PoisonReason::SrgbUnsupported, ...)` instead of throwing. The throws in `emulatedSrgbSampleState` should follow that model, and the table path has to decide what an sRGB table entry does. - `SpirvImageEmitter.cpp` `EmitEmulatedSrgbSample`: wrap, clamp-to-border (transparent black, opaque black, white) and texel offsets are implemented but no test runs them. Add rows with coordinates outside [0, 1] for each address mode and one with an offset, and compare the point rows bit for bit against `SrgbToLinear` (the hardware does). - `docs/dev/debt/shader.md`: the new line must replace the current "Samples and gathers" line on `main` (it now describes the copy path), not the old throw line. Happy to re-review once rebased.
MarcZX10 commented 2026-11-23 03:06:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T03:06:13Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

Changes are required before merge: fix sRGB mode dispatch so specialized modules emit fallback sampling only for sRGB descriptors.

What I ran

  • Reviewed head 273e2695e4df17f5036cef403b761ebb406547cf and the prepared patch; no commands or tests were run.
  • Read description.md, commits.md, manifest.json, diff.patch, ResourceMaterializer.cpp, DescriptorBindingBuilder.cpp, SpirvImageEmitter.cpp, RuntimeAbi.hpp, Resources.hpp, and ImageSampleForceDegamma.cpp.

Results

Check Result Note
Patch review failed One verified dispatch defect
Tests not run Author reports Windows GPU tests
Excluded files not applicable files.json was not available through the reader

Findings

Blocking

  • SpirvImageEmitter.cpp:1506 — The fallback is selected using the static image.srgbDecode mode before the runtime descriptor format is resolved. RuntimeImageModes adds both ordinary and sRGB modes for eligible images, and EmitImage emits both as separate specialized branches, but the sRGB branch’s sample operation is emitted as unconditional fallback code. When the descriptor is UNORM, that branch still decodes its texels as sRGB; when the descriptor is sRGB, the ordinary branch can also be selected and sample through the native path. The format-dependent mode therefore does not reliably select matching behavior. Make the fallback conditional on the selected mode or descriptor format, and verify both descriptor variants.

Non-blocking

None.

Not verified

  • CMakeLists.txt, ShaderMemory.cpp, ImageSrgbLoad.cpp, PipelineSpecialization.hpp, and docs/dev/debt/shader.md were not read directly from repository paths; their prepared diff sections were reviewed where present.
  • The manifest lists these ten changed files, all marked included. files.json and the packaged full-file texts were unavailable through the reader, so complete file coverage and exclusions could not be confirmed.
  • No tests, build, console runtime, or PS5 gfx1013 behavior was run or confirmed. Test results in the description are author-reported.

To get this to a good state

Fix mode-dependent sRGB dispatch, then run the focused CTest group on the new head and confirm both sRGB and UNORM descriptor behavior.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6016120746 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5371#pullrequestreview-6016120746). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T03:06:13Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested Changes are required before merge: fix sRGB mode dispatch so specialized modules emit fallback sampling only for sRGB descriptors. ## What I ran - Reviewed head `273e2695e4df17f5036cef403b761ebb406547cf` and the prepared patch; no commands or tests were run. - Read `description.md`, `commits.md`, `manifest.json`, `diff.patch`, `ResourceMaterializer.cpp`, `DescriptorBindingBuilder.cpp`, `SpirvImageEmitter.cpp`, `RuntimeAbi.hpp`, `Resources.hpp`, and `ImageSampleForceDegamma.cpp`. ## Results | Check | Result | Note | |---|---|---| | Patch review | failed | One verified dispatch defect | | Tests | not run | Author reports Windows GPU tests | | Excluded files | not applicable | `files.json` was not available through the reader | ## Findings ### Blocking - `SpirvImageEmitter.cpp:1506` — The fallback is selected using the static `image.srgbDecode` mode before the runtime descriptor format is resolved. `RuntimeImageModes` adds both ordinary and sRGB modes for eligible images, and `EmitImage` emits both as separate specialized branches, but the sRGB branch’s sample operation is emitted as unconditional fallback code. When the descriptor is UNORM, that branch still decodes its texels as sRGB; when the descriptor is sRGB, the ordinary branch can also be selected and sample through the native path. The format-dependent mode therefore does not reliably select matching behavior. Make the fallback conditional on the selected mode or descriptor format, and verify both descriptor variants. ### Non-blocking None. ## Not verified - `CMakeLists.txt`, `ShaderMemory.cpp`, `ImageSrgbLoad.cpp`, `PipelineSpecialization.hpp`, and `docs/dev/debt/shader.md` were not read directly from repository paths; their prepared diff sections were reviewed where present. - The manifest lists these ten changed files, all marked included. `files.json` and the packaged full-file texts were unavailable through the reader, so complete file coverage and exclusions could not be confirmed. - No tests, build, console runtime, or PS5 gfx1013 behavior was run or confirmed. Test results in the description are author-reported. ## To get this to a good state Fix mode-dependent sRGB dispatch, then run the focused CTest group on the new head and confirm both sRGB and UNORM descriptor behavior.

Pull request closed

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!5371
No description provided.