Skip to content

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

Closed
likecrank-dotcom wants to merge 1 commit into
boykopovar:mainfrom
likecrank-dotcom:fix/agc-srgb-sampling-fallback
Closed

likecrank-dotcom wants to merge 1 commit into
boykopovar:mainfrom
likecrank-dotcom:fix/agc-srgb-sampling-fallback

Conversation

@likecrank-dotcom

Copy link
Copy Markdown

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

AI assistance: OpenAI Codex (AI) implemented and validated this change. The human author remains responsible for review and submission.
@coltken95

Copy link
Copy Markdown

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

  • 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 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@fogydes

fogydes commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Verdict

stale+rebase

Findings

  1. [verified] Merge conflicts against current origin/main (bb04a4f1ea): git merge-tree --write-tree origin/main 273e2695e4df17f5036cef403b761ebb406547cf exits with code 1. The branch is 14,030 commits behind main and conflicts on multiple files including:
    • core/libs/prx/libSceAgcDriver/CMakeLists.txt
    • core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp
    • core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
  2. [verified] Conventions check passed: python tools/check_conventions.py --base 18c438b1092b09cef54e4cfacab441c009a6b501 in a scratch worktree succeeds with 0 findings and 0 errors.
  3. [read-only] Missing PR checklist line: The PR checklist omits one required template line from .github/pull_request_template.md:
  4. [read-only] Shader recompiler validation layer comparison requirement: Per AGENTS.md, AGC and shader recompiler changes require running the same focused tests on main and the branch with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation and comparing logs to verify no new VUID- validation errors are logged. The PR description reports focused CTest runs on RTX 5060 Ti, but no Vulkan validation layer comparison.
  5. [author-reported] Focused CTest group (agc_shader_memory, agc_driver_image_sample_force_degamma, agc_driver_image_srgb_load, agc_driver_image_sample_srgb_fallback, agc_driver_emulated_color_compare_srgb_decode) reported passing on Windows 11 with RTX 5060 Ti.

Freshness envelope

  • Head: 273e2695e4df17f5036cef403b761ebb406547cf
  • Base: 18c438b1092b09cef54e4cfacab441c009a6b501
  • Behind origin/main: 14,030 commits
  • Merge-tree: exit 1 (conflicts in CMakeLists.txt, Resources.hpp, DescriptorBindingBuilder.cpp)

Cross-PR ledger

  • No other open PR currently touches these shader recompiler files.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@likecrank-dotcom I agree with adriadam10's second review on this head, and the decision is yours to make. Since #6054 main already samples R8 and RG8 sRGB through an RGBA8 copy, so this shader decode only runs on a device that cannot blit R8/RG8 to RGBA8, or with APS5_SRGB_SHADER_DECODE=1. Which real device hits that case? If none does, the fallback has no user and should be dropped. If one does, please rebase onto main (the PR is conflicting), follow the rejectImage(PoisonReason::SrgbUnsupported, ...) model for the unsupported shapes, and add the address mode and offset rows he listed.

@blawesom

Copy link
Copy Markdown
Contributor

Validation run: PR #5371 fix(shader): emulate sRGB sampling when unsupported (queue item 9)

Environment

  • Machine: CachyOS, kernel 7.3.4-1-cachyos, Ryzen 7 9800X3D, AMD Radeon RX 9070 XT (RADV GFX1201, selected via ANYPS5_GPU='AMD Radeon RX 9070 XT'; AI PRO R9700 also present), /dev/kfd present
  • Validation layer: VK_LAYER_KHRONOS_validation from VK_ADD_LAYER_PATH=/home/benj/scratch/vvl-layer (explicit layer, enabled by env var)
  • Parallelism capped at 12 (CMAKE_BUILD_PARALLEL_LEVEL=12, cmake --build build --parallel 12)

SHAs

  • PR head (frozen, validated as-is, no rebase): 273e2695e4df17f5036cef403b761ebb406547cf (branch fix/agc-srgb-sampling-fallback; PR state re-checked: OPEN, not locked, not merged, head matches)
  • PR branch base (merge-base with main): 18c438b1092b09cef54e4cfacab441c009a6b501 — the branch is stale relative to main
  • Baseline origin/main: 0f4538fa022a7d82d467fd74e112514abc30345f (prebuilt worktree /home/benj/scratch/wt5/base-main, clean, reused read-only)
  • PR worktree: /home/benj/scratch/wt5/pr-5371 at the head SHA, submodules initialized

Build

  • Configure: cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DBUILD_TESTING=ON → exit 0, wall 7 s
  • Build: cmake --build build --parallel 12 → exit 0, wall 124 s (3160 targets)
  • Logs: t9-pr5371-configure.log, t9-pr5371-build.log

Focused test pattern (shader-recompiler sRGB-sampling change)

Pattern (identical matched set of 17 tests in both trees, verified with ctest -N -R + diff):

^(agc_driver_image_sample_(aniso_override|bias_a16|border_color|cube_derivatives|derivatives|force_degamma|lod_clamp|reduction|shared_sampler|unnormalized)|agc_driver_image_srgb_load|agc_driver_emulated_color_compare(_srgb_decode)?|agc_driver_srgb8_color_target|agc_shader_memory|image_sample_gather|texture_format_lookup)$

Tests (both trees): agc_driver_emulated_color_compare, agc_driver_emulated_color_compare_srgb_decode, agc_driver_image_sample_aniso_override, agc_driver_image_sample_bias_a16, agc_driver_image_sample_border_color, agc_driver_image_sample_cube_derivatives, agc_driver_image_sample_derivatives, agc_driver_image_sample_force_degamma, agc_driver_image_sample_lod_clamp, agc_driver_image_sample_reduction, agc_driver_image_sample_shared_sampler, agc_driver_image_sample_unnormalized, agc_driver_image_srgb_load, agc_driver_srgb8_color_target, agc_shader_memory, image_sample_gather, texture_format_lookup
Pattern + name lists: t9-pattern.txt, t9-pattern-base-main-names.txt, t9-pattern-pr-5371-names.txt
PR-only new test (not in baseline tree): agc_driver_image_sample_srgb_fallback — run separately on the PR tree (t9-tests-pr-newtest.log): Passed (0.06 s), ran on the RX 9070 XT with the validation layer inserted, printed "image R8/RG8 sRGB fallback sampling tests passed"

Test runs (identical env on both trees, timeout 3600 each)

ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_ADD_LAYER_PATH=/home/benj/scratch/vvl-layer VK_LOADER_DEBUG=layer ANYPS5_GPU='AMD Radeon RX 9070 XT' ctest --test-dir build -R '<pattern>' -V
  • Baseline (base-main): exit 0, wall 2 s — 100% tests passed out of 17, no skips (t9-tests-base.log)
  • PR tree: exit 0, wall 1 s — 100% tests passed out of 17, no skips (t9-tests-pr.log)
  • Per-test comparison: identical 17/17 Passed in both trees; no test differs in pass/fail/skip

Layer checks (per AGENTS.md)

  • was not found but was requested by env var VK_INSTANCE_LAYERS: absent in all three logs (0 occurrences)
  • Insert instance layer "VK_LAYER_KHRONOS_validation": present — 14 occurrences in base log, 14 in PR log, 2 in the new-test log; loader confirms explicit layer enabled by VK_INSTANCE_LAYERS, device selected = AMD Radeon RX 9070 XT (RADV GFX1201)

VUID / hazard ID diff

  • Base log: 0 VUID-, 0 SYNC-HAZARD, 0 Validation Error/Warning lines
  • PR log: 6 × VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 (3 Validation Error lines), all in agc_driver_srgb8_color_target (vkCreateGraphicsPipelines(): pAttachments[1] is different than pAttachments[0] and independentBlend feature was not enabled); 0 SYNC-HAZARD
  • New-test log: 0 IDs
  • Attribution (git log): the PR commit 273e2695 touches only shader-recompiler image-sampling files (SpirvImageEmitter.cpp, ResourceMaterializer.cpp/hpp, DescriptorBindingBuilder.cpp, Resources.hpp, PipelineSpecialization.hpp, RuntimeAbi.hpp) plus tests (ImageSampleForceDegamma.cpp, ImageSrgbLoad.cpp, ShaderMemory.cpp), CMakeLists and docs — the diff contains no blend/pAttachments/independentBlend/graphics-pipeline changes. The VUID is produced by the stale branch base 18c438b1, which predates main commit 3926edb1bb1 ("fix(agc): enable independentBlend for color targets with different blend states", merged via fix(agc): enable independentBlend for color targets with different blend states #6782, in 18c438b1..0f4538fa, adds 2 lines enabling the independentBlend feature in VulkanDevice.cpp). With that feature enabled on current main, the differing per-attachment blend states are legal and the VUID does not occur — which is why the baseline at 0f4538fa is clean. The new ID is therefore not attributable to the PR commit.

Push

  • Pushed frozen head to fork: git push fork +273e2695e4df17f5036cef403b761ebb406547cf:refs/heads/validation/pr-5371 → refs/heads/validation/pr-5371 on blawesom/AnyPS5

Verdict

PASS — build clean; focused set identical in both trees and 17/17 passed on both with the validation layer active on the RX 9070 XT; the PR's new sRGB-fallback execution test passes; no missing-layer condition; the only new VUID ID is attributed to the stale branch base (missing main-side independentBlend fix), not to this PR's commits.

Limitations

  • Results are scoped to Linux + AMD RADV (RX 9070 XT); no other GPU/vendor validated
  • The PR branch is based on an old main (18c438b1), so the base-vs-PR diff includes unrelated main drift; attribution of the one new VUID was done via git history, not by rebuilding the branch base
  • Single run per tree (no repetition), so intermittent faults are not excluded
  • ctest reports Passed even with validation errors logged, hence the manual VUID diff above

@MarcZX10 MarcZX10 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.

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.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

I am closing this for now. The maintainer review of 14 Oct asked for the sRGB emulation to match the RDNA2 image_sample_lz measurements on 8_SRGB and 8_8_SRGB bit for bit, and there has been no push since 13 Oct. The branch also conflicts with main in several shader files, and part of it has landed already. You are welcome to reopen or push a rebased branch at any time and we will look at it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build docs needs rebase Conflicts with main, rebase needed shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants