shader: decode narrow sRGB textures when sampling #415
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!415
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/415/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?
@MehmetCambaz on GitHub · 2026-08-30 15:11 UTC · original
MehmetCambaz:fix/narrow-srgb-shader-decode→main· +149 −6 · 8 files · 1 commits · closed without mergingProblem
Guest texture descriptors can name the single- and dual-channel sRGB formats (k8Srgb, k8_8Srgb). Both are mapped to UNORM in vulkanCommon.cpp, because VK_FORMAT_R8_SRGB and VK_FORMAT_R8G8_SRGB are optional in Vulkan and are genuinely absent on common desktop drivers — vulkaninfo lists them under Unsupported Formats on both GPUs in my machine (RTX 2060 and Intel UHD). The existing comment acknowledged the gap and named the missing piece:
// Narrow-channel sRGB formats are optional in Vulkan. Keep a same-width fallback until
// sampler-aware sRGB emulation is available.
With the image stored as UNORM, nothing performs the sRGB → linear conversion the guest sampler would have performed, so a shader sampling these formats receives gamma-encoded values where it expects linear ones. Four-channel sRGB is unaffected: it maps to a real host sRGB format and the sampler still does the work.
How it shows up
PAC-MAN WORLD 2 Re-PAC (PPSA18189) decodes video into an NV12 pair, tagged k8Srgb for the luma plane and k8_8Srgb for chroma. Measured at runtime:
luma 2048x1088 guest_format=128 (k8Srgb) -> VK_FORMAT_R8_UNORM
chroma 1024x544 guest_format=129 (k8_8Srgb) -> VK_FORMAT_R8G8_UNORM
Its YUV shader converts the samples back to gamma space before the BT.601 matrix — the disassembly applies 12.92c / 1.055c^(1/2.4) - 0.055 to each sample, then c/12.92 / ((c+0.055)/1.055)^2.4 to the resulting colour. That only balances if the sampler decoded first. Without it the values are encoded twice and every frame of the intro comes out magenta.
Modelling the double encode from the measured plane contents predicts (203, 48, 251) against the (219, 66, 248) actually on screen, which is what identified the cause.
Fix
Specialize the shader to apply the transfer function after sampling when the descriptor names a format with no host equivalent. Prospero::TextureNeedsShaderSrgbDecode is the single source of truth for which formats those are, and the format mapping comment now points at it.
The flag rides along with the existing image specialization, so it participates in the shader key: a descriptor that later names a sampler-decoded format invalidates the compiled shader rather than silently reusing a redundant decode. It is also carried through indirect image classes, so a table mixing sRGB and non-sRGB candidates is rejected instead of decoding inconsistently.
Why this matters beyond one title
The bug is in the format layer, not in any game's code, so it applies to any title whose shaders sample these formats. The common case is video playback: engines tag decoded luma/chroma planes as sRGB precisely so the sampler linearizes them, which is exactly the path that was missing. Any title playing an FMV through that path currently renders it with a double gamma.
To be clear about the evidence: PAC-MAN WORLD 2 Re-PAC is the title I confirmed end to end. The mechanism is format-generic rather than title-specific, so I expect it to recur wherever these descriptors are used, but I have only measured the one game.
This is a correctness fix, not a performance one — it does not change frame rates. The added work is a handful of instructions per sample, and only in shaders that sample these two formats.
Regression surface
Everything is gated on those two format enums. A shader that does not sample k8Srgb or k8_8Srgb generates byte-identical SPIR-V, so titles that never touch them cannot be affected.
One known imprecision, stated plainly: hardware decodes before filtering, while this decodes after. For chroma upsampling the difference is not visible, but it is not bit-exact.
Testing
ctest — 35/35 passing.
New resource tracking case covering all three paths: a narrow sRGB descriptor sets the flag, a four-channel sRGB descriptor does not, and switching between them invalidates the specialization.
PAC-MAN WORLD 2 Re-PAC: intro video verified correct, measured G/R = 1.451 against 1.491 from a reference BT.601 conversion of the decoded frame, versus 0.30 before the fix.
Tetris Forever (PPSA25646): no regression, renders correctly at 60 fps.
Verifying this game end to end also needs #414, since PAC-MAN does not reach the video without it. The two changes are independent and I confirmed they apply and run together on top of current main.
AI use disclosure
Per the AI Use policy in the README. I used an AI assistant for research and development assistance on this change: reading the RDNA2 disassembly of the game's YUV shader, decoding its float constants, modelling the double-gamma arithmetic, and drafting the patch and its test.
I reviewed the change in full, and every claim above is from a measurement I ran on my own machine rather than from the model's assertion: the runtime descriptor formats, the vulkaninfo format support, the before/after pixel ratios, the test suite, and the Tetris regression check. I first tried the obvious fix of mapping to the native sRGB formats and discarded it after it crashed on unsupported-format validation, which is what led to the shader-side approach.
Pull request closed