shader: emulate the sRGB decode of narrow sRGB sampled formats #706
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!706
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/706/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@YuhiAida on GitHub · 2026-09-18 20:27 UTC · original
YuhiAida:agents/update-main-check-pacman-cutscenes→main· +365 −15 · 6 files · 8 commits · closed without mergingSummary
The guest can sample 8-bit sRGB textures (
k8Srgb/k8_8Srgb), but Vulkan onlyguarantees sRGB sampling for RGBA8, so those formats are stored as the linear format of the
same width (
R8_UNORM/R8G8_UNORM) and the sRGB decode the guest texture pipe expects isemulated by the shader.
Related to #524 — the magenta/pink wash in the PAC-MAN WORLD 2 Re-PAC cutscenes. The
cutscene video planes are bound as narrow sRGB textures, so their BT.601-decoded samples were
returned linear and then re-encoded, saturating red and blue.
What changed
ShaderIR.h/ResourceMaterialization.{h,cpp}: newsrgb_sample_decodespecializationfield, set when the image descriptor format is
k8Srgbork8_8Srgb(never for storageimages). The field takes part in the specialization identity, so it is also part of the
permutation/pipeline key.
spirvEmitterImage.cpp: when the sampled image requests it, the sampled value is decodedin the shader —
x <= 0.04045 ? x / 12.92 : pow((x + 0.055) / 1.055, 2.4)per channel, viaGLSLstd450FMax/GLSLstd450Pow(spv::OpFMaxdoes not exist in the bundledSPIRV-Headers).
The wider formats
k8_8_8_8SrgbandBc*Srgbalready map to real sRGB host formats, so thehardware decodes them and no shader decode is emitted for them.
Why shader-side instead of an sRGB image view
Creating a linear/sRGB image view pair over the same image and binding the sRGB view for
sampled reads was implemented and measured first; it cannot work on this device because
VK_FORMAT_R8G8_SRGBreportsoptimal=0x0 linear=0x0 buffer=0x0, andVK_FORMAT_R8_SRGBhas the same restriction. The shared-decoder shader path is device-independent, so the change
was moved there (the descriptor-side experiment was reverted).
Evidence
Same run, same cutscene frame, before/after (frame mean RGB over the cutscene region):
(199.7, 44.4, 242.2)(52, 54, 99)(130, 130, 130)Gameplay is unchanged (it never samples these formats) and the run still presents at 60 fps.
Known approximation: the decode is applied after filtering, while hardware sRGB decoding
happens before it. This is exact for the ~1:1 luma plane and a small approximation for the
2x-upsampled chroma plane; it is unavoidable while sampling
R8_UNORM/R8G8_UNORM.Tests
tests/ResourceMaterializationTests.cpp: newTestNarrowSrgbDescriptorRequestsShaderDecode—
k8Srgbandk8_8Srgbdescriptors request the shader decode,k8UNormdoes not.resource_materialization_testsandresource_tracking_testspass on the containertoolchain; the emulator builds and runs with the change.
Related work
#167 ("Decode sRGB in the shader for formats the host image cannot carry") and #415
("shader: decode narrow sRGB textures when sampling", closed unmerged) target the same bug.
This PR is the minimal form of the same fix; if it is accepted, #167 can be superseded.
Draft for now: the mixed-narrow-sRGB indirect image table case makes the new field a
candidate-compatibility check, and that path has not been exercised on hardware yet.
@coderabbitai[bot] on GitHub · 2026-09-18 20:39 UTC · original · edited 2026-10-04 11:22 UTC
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
2cefcc50-98ca-4b8a-8c54-8b4f22c649e9📥 Commits
Reviewing files that changed from the base of the PR and between
0e8f2465deandcf3715e661.📒 Files selected for processing (4)
src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppsrc/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cpptests/ResourceMaterializationTests.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
📝 Walkthrough
Walkthrough
Image materialization tracks whether sampled narrow sRGB images need shader-side decoding. It sets the flag for supported 8-bit sRGB formats on non-storage images and propagates it through specialization. The SPIR-V emitter decodes eligible sampled results, excluding integer samples and depth-reference results. For indirect images, it decodes mixed candidates in their switch arms and uniformly flagged candidates after the merge. Tests cover single-channel and two-channel sRGB images, linear-format preservation, and materialization behavior.
Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: 🔵 Low · up to
cf371Mixed-format indirect tables no longer show the reported failure. Narrow-sRGB image loads can still return decoded rather than encoded values; this bounded issue needs owner awareness or a follow-up.
Security Architecture Review
Security architecture risk: 🔵 Low · up to
cf371The change affects texture color interpretation without demonstrated expansion of access or privileges. Risk is low rather than minimal because concurrent-use guarantees and runtime validation remain incomplete.
Retained concerns
No architecture-level concerns identified.
Security review details
Security Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 4 | ❓ 1
❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
Full details: Docstring Coverage
Explanation
Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 21 functions across 5 files. (1 skipped: 1 too large.)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@ -228,3 +268,4 @@}if (mem.data_bits == 16u) {uint32_t packed[4] = {ConstantU32(ctx.state, 0), ConstantU32(ctx.state, 0),ConstantU32(ctx.state, 0), ConstantU32(ctx.state, 0)};@coderabbitai[bot] on GitHub · 2026-09-18 20:39 UTC · original · edited 2026-09-18 21:20 UTC
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 50372
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 36841
Add SPIR-V coverage for
ResultVectorsRGB decoding.TestNarrowSrgbDescriptorRequestsShaderDecodeonly checks descriptor specialization. The reachableResultVectorbranch appliesSrgbDecodedVector, but no current SPIR-V test enablessrgb_sample_decodeor checks the emitted transfer instructions. Add a focused emitter test for enabled floating-point color sampling, plus disabled, integer, and depth-reference cases that must not emit the transfer sequence.✅ Addressed in commit
b1392f6@coderabbitai[bot] on GitHub · 2026-09-18 20:39 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
54f06b4e-6bda-4443-97bb-e6eac230a9f2📥 Commits
Reviewing files that changed from the base of the PR and between
5b7d3343cfand5f4bafecd4.📒 Files selected for processing (5)
src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cppsrc/graphics/shader/recompiler/ir/passes/ResourceMaterialization.htests/ResourceMaterializationTests.cppIncluded review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@ -228,3 +268,4 @@}if (mem.data_bits == 16u) {uint32_t packed[4] = {ConstantU32(ctx.state, 0), ConstantU32(ctx.state, 0),ConstantU32(ctx.state, 0), ConstantU32(ctx.state, 0)};@YuhiAida on GitHub · 2026-09-18 21:07 UTC · original
Addressed in
b1392f6(tests: cover the shader sRGB decode in the SPIR-V emitter), which extendstests/ShaderRecompilerComputeTests.cppwith three focused image-load cases plus a negative marker on the existing integer case:ImageLoadDecodesNarrowSrgbTexel- float enabled,k8Srgb(eR8Unorm), low-branch texel0x0a; asserts the exact decoded value0x3b46eb61and requires the transfer sequence (FMax,OpFOrdLessThanEqual %bool,Pow) in the disassembly.ImageLoadDecodesNarrowSrgbChromaPair- float enabled,k8_8Srgb(eR8G8Unorm), dmask0x3, two texels; asserts{0x3a9f22b4, 0x3b1f22b4}(bytes 4 and 8) and requires the same markers.ImageLoadKeepsLinearTexelsLinear- float disabled,k8UNorm; asserts the pass-through value0x3e808081for texel0x40and forbidsFMax/Pow.ImageLoadR32UintUsesIntegerSampledImage- integer path; now also forbidsFMax/Powso a future change cannot start emitting the transfer sequence for integer sampled images.All three new cases are registered with
AddCaseand can be run in isolation with a new--narrow-srgb-onlyflag; the full suite also passes (ShaderRecompilerComputeTests: all cases passed).One honest caveat on guard coverage:
srgb_sample_decodeis only set for descriptor formatsk8Srgb/k8_8Srgb, and those materialize as float sampled images, so the!integerand!drefconditions in the emitter guard are defensive rather than reachable today - no descriptor that sets the flag can also be an integer numeric class or a depth-compare sample. That is why there is no positive integer/dref decode case: such a case cannot be constructed against the current materializer (only the negative integer case above). I have kept the guards and covered them from the negative side; if you would prefer, I can instead add a checks-only test asserting the materializer never producessrgb_sample_decodetogether with a non-float numeric class ordepth_compare.@coderabbitai[bot] on GitHub · 2026-09-18 21:16 UTC · original · reviewed
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
6c1c0b9e-02ae-4f58-b86e-c56df1e29156📥 Commits
Reviewing files that changed from the base of the PR and between
5f4bafecd4andb1392f6a96.📒 Files selected for processing (1)
tests/ShaderRecompilerComputeTests.cppIncluded review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@ -228,3 +268,4 @@}if (mem.data_bits == 16u) {uint32_t packed[4] = {ConstantU32(ctx.state, 0), ConstantU32(ctx.state, 0),ConstantU32(ctx.state, 0), ConstantU32(ctx.state, 0)};@coderabbitai[bot] on GitHub · 2026-09-18 21:20 UTC · original
@YuhiAida, thanks for the detailed update. The added cases cover the reachableResultVectorsRGB decode behavior and the disabled paths.The materializer invariant makes positive integer and depth-reference decode cases unreachable. The existing negative integer marker is sufficient for this emitter coverage. No additional checks-only test is required for this finding.
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-10-04 10:36 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
7537e7f7-64c6-4e96-8336-f543643703d2📥 Commits
Reviewing files that changed from the base of the PR and between
b051a10072and0e8f2465de.📒 Files selected for processing (5)
src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cpptests/ResourceMaterializationTests.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@coderabbitai[bot] on GitHub · 2026-10-04 10:36 UTC · original
On
src/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cppline 564 (outdated):🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 14704
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42657
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 29345
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 18593
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8241
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 12615
Keep compatible mixed sRGB/UNorm indirect tables.
When
k8Srgbandk8UNormcandidates meet the other table constraints, the new decode-flag comparison rejects them even though both sample as floats and use no conversion format. This makesMaterializeResourcesfail for the table. The emitter samples candidates in separate switch arms, merges their results withOpPhi, then appliesResultVectorusing only the root image’s decode flag. Decode each candidate in its switch arm before the merge, skip the root-level decode for that result, and remove the table-wide comparison. Add a regression test for materialization and candidate-specific decoding.🐛 Suggested fix
📝 Committable suggestion
@YuhiAida on GitHub · 2026-10-04 11:26 UTC · original
@Nmzik
Pull request closed