Decode sRGB in the shader for formats the host image cannot carry #167

Closed
github-bot wants to merge 1 commit from gh-pr/167/head into gh-pr/167/base
Owner

@⁠Death-Whispers-U on GitHub · 2026-08-04 14:39 UTC · original
Death-Whispers-U:fix/shader-srgb-decode → main · +242 −10 · 9 files · 1 commits · closed without merging

PAC-MAN WORLD 2 Re-PAC renders its cutscenes magenta. Everything is there — geometry, detail, motion — but drowned in pink.

R8_SRGB and R8G8_SRGB are optional in Vulkan and plenty of devices expose no features for them at all, so k8Srgb and k8_8Srgb are created as unorm images. That part is already here and is the right call. What is missing is the other half: nothing then supplies the sRGB decode those formats are owed, so the sampled texels stay gamma-encoded and any title that binds one gets a colour cast.

PAC-MAN WORLD 2 binds exactly these — its decoded-video planes are k8Srgb luma and k8_8Srgb chroma.

Change

Emit the decode in the shader.

IsShaderDecodedSrgbTextureFormat names the two formats. It is deliberately not a column in the format table, because this is a property of what the host can express, not of the guest format: every other guest sRGB format has a real host counterpart whose sampler already decodes it, and a shared column would sweep those in and decode them twice.

Specialization carries it. ResourceMaterialization fills srgb_decode in from the runtime descriptor and rejects a descriptor that no longer matches the permutation that was compiled, and the flag joins the program key — two modules differing only in the decode produce different SPIR-V and must not collide. The uint check cannot stand in for it: k8UNorm and k8Srgb are both non-uint and indistinguishable to it.

Three details worth flagging for review:

  • The decode runs before the bitcast, since the bitcast reinterprets the float as an integer bit pattern for VGPR storage and decoding after it would corrupt the value.
  • Alpha is never decoded, matching Vulkan's own sRGB sampling semantics.
  • Gather4 returns one channel of four neighbouring texels, so it decides the alpha exclusion once, on that channel.

Testing

Same cutscene frame, same second, mean channel values:

R G B
before 239.0 88.1 253.2
after 68.8 79.6 106.3

Red and blue pinned high with green far below them is magenta, numerically. Green minus the average of red and blue goes from −158.0 to −7.9.

Verified again after rebasing onto current main.

**@⁠Death-Whispers-U** on GitHub · 2026-08-04 14:39 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/167) `Death-Whispers-U:fix/shader-srgb-decode` → `main` · +242 −10 · 9 files · 1 commits · closed without merging PAC-MAN WORLD 2 Re-PAC renders its cutscenes magenta. Everything is there — geometry, detail, motion — but drowned in pink. `R8_SRGB` and `R8G8_SRGB` are optional in Vulkan and plenty of devices expose no features for them at all, so `k8Srgb` and `k8_8Srgb` are created as unorm images. That part is already here and is the right call. What is missing is the other half: nothing then supplies the sRGB decode those formats are owed, so the sampled texels stay gamma-encoded and any title that binds one gets a colour cast. PAC-MAN WORLD 2 binds exactly these — its decoded-video planes are `k8Srgb` luma and `k8_8Srgb` chroma. ## Change Emit the decode in the shader. `IsShaderDecodedSrgbTextureFormat` names the two formats. It is deliberately not a column in the format table, because this is a property of what the host can express, not of the guest format: every other guest sRGB format has a real host counterpart whose sampler already decodes it, and a shared column would sweep those in and decode them twice. Specialization carries it. `ResourceMaterialization` fills `srgb_decode` in from the runtime descriptor and rejects a descriptor that no longer matches the permutation that was compiled, and the flag joins the program key — two modules differing only in the decode produce different SPIR-V and must not collide. The uint check cannot stand in for it: `k8UNorm` and `k8Srgb` are both non-uint and indistinguishable to it. Three details worth flagging for review: - The decode runs **before** the bitcast, since the bitcast reinterprets the float as an integer bit pattern for VGPR storage and decoding after it would corrupt the value. - Alpha is never decoded, matching Vulkan's own sRGB sampling semantics. - `Gather4` returns one channel of four neighbouring texels, so it decides the alpha exclusion once, on that channel. ## Testing Same cutscene frame, same second, mean channel values: | | R | G | B | | --- | --- | --- | --- | | before | 239.0 | 88.1 | 253.2 | | after | 68.8 | 79.6 | 106.3 | Red and blue pinned high with green far below them is magenta, numerically. Green minus the average of red and blue goes from −158.0 to −7.9. Verified again after rebasing onto current main.
R8_SRGB and R8G8_SRGB are optional in Vulkan and plenty of devices expose
no features for them at all, so k8Srgb and k8_8Srgb are created as unorm
images. That part was already here - but nothing then supplied the sRGB
decode those formats are owed, so the sampled texels stayed gamma-encoded
and every title that binds one gets a colour cast.

PAC-MAN WORLD 2 Re-PAC binds exactly these: its decoded-video planes are
k8Srgb luma and k8_8Srgb chroma. Its cutscenes render magenta.

Emit the decode instead. IsShaderDecodedSrgbTextureFormat names the two
formats and is deliberately not a column in the format table, because
this is a property of what the host can express, not of the guest format:
every other guest sRGB format has a real host counterpart whose sampler
already decodes it, and a shared column would decode those twice.

Specialization carries it. SpecializeResources fills srgb_decode in from
the runtime descriptor, and ValidateResourceSpecialization rejects a
descriptor that no longer matches the permutation that was compiled. That
rejection is what keeps two modules differing only in the decode from
colliding: the program cache reuses a permutation only when this
validation accepts the live snapshot. The uint check cannot stand in for
it: k8UNorm and k8Srgb are both non-uint. srgb_decode also joins the
indirect-image binding class, since one module is compiled for the whole
candidate table.

The decode runs before the bitcast, since the bitcast reinterprets the
float as an integer bit pattern for VGPR storage. Alpha is never decoded,
matching Vulkan's own sRGB sampling. Gather4 decides the alpha exclusion
once, on the single channel it returns.

Measured on the same cutscene frame, mean channel values:

  before   R=239.0  G=88.1  B=253.2
  after    R=68.8   G=79.6  B=106.3

Green minus the magenta average goes from -158.0 to -7.9.

Ported from the 2026-08-04 base of PR #167 onto c52bf450. Four of the
nine files had moved: the emitter now lives under recompiler/backend/
spirv/ and ResourceMaterialization under recompiler/ir/passes/. The
emitter rewrite funnels image load, sample and gather through one
ResultVector, so the decode is applied to the sampled vector at the call
sites - alongside UnpackImageTexel and UnpackImageGather, which already
correct sampled texels there - instead of being repeated inside three
per-component loops. That also covers the 16-bit return path, which did
not exist at the original base.

The shader.cpp half is dropped as redundant: c167375f removed
ShaderAppendNativeSpecialization and ShaderDescribeSpecialization when it
centralized permutation caching, and the static key it replaced them with
carries no image data at all. Validation is now the whole mechanism, so
the flag reaches the key through ValidateResourceSpecialization alone.

A resource-tracking test covers both halves: that a k8Srgb sampled
descriptor specializes to a decode, and that a unorm, null, or sRGB
descriptor bound against the opposite permutation is refused.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:12:57 +00:00
github-bot closed this pull request 2026-11-26 09:12:57 +00:00
Author
Owner

@⁠Death-Whispers-U on GitHub · 2026-08-28 14:23 UTC · original

Rebased onto c52bf450. Windows and macOS are green, Linux is still running.

The diff is larger than when this was opened, +242 rather than +130, because main grew indirect image tables in the meantime. One SPIR-V module is compiled for a whole table, so two candidates that disagree about srgb_decode must not share one. The flag now threads through SpecializeResources and the null-indirect-root check as well. The added test carries two negative controls, so it fails if the flag ever stops discriminating.

One thing I would rather flag than leave implicit. For a plain sampled image with no conversion format, shader_swizzle stays identity and the guest DstSel is applied by the host image view. The alpha exclusion therefore operates on post-swizzle slots, while Vulkan decodes before component mapping. A descriptor that swizzled the single sRGB channel into slot 3 would go undecoded. I have not seen a title do it, and correcting it needs an inverse-swizzle decision I would rather make with review input than silently.

**@⁠Death-Whispers-U** on GitHub · 2026-08-28 14:23 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/167#issuecomment-5453696414) Rebased onto `c52bf450`. Windows and macOS are green, Linux is still running. The diff is larger than when this was opened, +242 rather than +130, because main grew indirect image tables in the meantime. One SPIR-V module is compiled for a whole table, so two candidates that disagree about `srgb_decode` must not share one. The flag now threads through `SpecializeResources` and the null-indirect-root check as well. The added test carries two negative controls, so it fails if the flag ever stops discriminating. One thing I would rather flag than leave implicit. For a plain sampled image with no conversion format, `shader_swizzle` stays identity and the guest `DstSel` is applied by the host image view. The alpha exclusion therefore operates on post-swizzle slots, while Vulkan decodes before component mapping. A descriptor that swizzled the single sRGB channel into slot 3 would go undecoded. I have not seen a title do it, and correcting it needs an inverse-swizzle decision I would rather make with review input than silently.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.