Skip to content

present_compose: fix premultiplied sRGB and additive alpha plane 1 decoding - #2508

Open
lixonik wants to merge 2 commits into
ProjectLeonida:mainfrom
lixonik:rt-picture-2d-layers-compose-alpha
Open

lixonik wants to merge 2 commits into
ProjectLeonida:mainfrom
lixonik:rt-picture-2d-layers-compose-alpha

Conversation

@lixonik

@lixonik lixonik commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes two alpha-decoding bugs in the PresentX composition shader (runtime/src/present_compose.comp + regenerated runtime/src/present_compose.inc) for premultiplied plane 1 overlays:

  1. Premultiplied *_UNORM_SRGB plane 1 overlays (flag_srgb_source != 0, flag_straight_alpha == 0, !opaque):
    An *_UNORM_SRGB render target blends in linear light and sRGB-encodes the premultiplied RGB on write, so texelFetch already returns linear premultiplied RGB. Previously fetch_texel re-encoded c.rgb with srgb_encode, divided the encoded value by linear alpha (srgb_encode(0.5) / 0.5 = 1.471), decoded it back (2.426), and multiplied by alpha (1.213), clipping translucent 50% and 25% white UI overlays to solid white (255 instead of 188 and 137).

    • Under piecewise sRGB (pc.sdr_eotf == 0u), fetch_texel now returns max(c.rgb, 0.0) directly without re-encoding or dividing by alpha.
    • Under gamma 2.2 (pc.sdr_eotf == 1u), fetch_texel splits continuously into the [0, a] unassociated sRGB component (preserving stored-byte identity for opaque pixels and avoiding 8-bit sRGB quantization jumps at lin ~= a) plus any additive excess max(lin - a, 0.0).
    • Straight-alpha *_UNORM_SRGB overlays (flag_straight_alpha != 0) and opaque *_UNORM_SRGB plane 0 are unchanged.
  2. Additive premultiplied pixels (rgb > 0, a == 0) on plane 1:
    Previously fetch_texel returned vec4(0.0) whenever a == 0.0 (and to_working returned vec3(0.0) when plane_hdr && !out_hdr && a <= 0.0), discarding additive premultiplied glow/UI pixels even though present_compose.comp explicitly notes additive premultiplied colour support.

    • Premultiplied UNORM (SDR and HDR10) and *_UNORM_SRGB planes now decode e / preserve lin when a == 0.0, while transparent black (0, 0, 0, 0) and straight-alpha a == 0 pixels still contribute zero light.
    • to_working now tone-maps HDR10 additive emission when a <= 0.0 instead of zeroing it.

Updated docs/d3d12_vulkan.md section 4.5 to document both rules.

SPIR-V generator note (runtime/src/present_compose.inc)

main's present_compose.inc was compiled with glslc --target-env=vulkan1.1 -O -mfmt=c (shaderc generator word 0x000d000b, ID bound 0xbb3). Because glslc is not installed locally, present_compose.inc in this PR was regenerated from present_compose.comp using glslang -V --target-env vulkan1.1 (glslang generator word 0x0008000b, ID bound 0x35f, SPIR-V 1.3, 5041 words), which accounts for the full-file .inc diff.

Verification

Claimed in #1608 (comment) (RT-PICTURE-2D-LAYERS). No game files used.

GPUs tested: NVIDIA GeForce RTX 5070 12 GB (Windows 11, driver 610.88, Vulkan 1.4.341; VK_LAYER_KHRONOS_validation and spirv-val not installed locally).
AI-assisted (Antigravity, Gemini).

@timbot103

Copy link
Copy Markdown
Contributor

Independently checked head ef5620bf6 on Windows 11 Pro 26200, MSVC 2022 (19.44.35230.0, RelWithDebInfo, Ninja), Vulkan SDK 1.4.363.0, no game files.

Local build & dual-GPU verification (xbox_present_test, LEONIDA_PRESENT_PLANE1=0, LEONIDA_VK_VALIDATION=1):

  • Clean MSVC build of xbox_present_test.
  • AMD Radeon(TM) Graphics (25.10.30.02): xbox_present_test: all cases pass (exit 0, 0 validation errors), including all new PR cases:
    • compose: premultiplied and straight *_UNORM_SRGB plane 1 overlays: ok
    • compose: additive premultiplied (a == 0) sRGB and UNORM plane 1 overlays: ok
    • compose: HDR10 plane 1 additive premultiplied (a == 0) over SDR plane 0: ok
  • NVIDIA GeForce RTX 5080 (616.64): all compose cases above passed (0 validation errors); the later swapchain resize step failed at line 1304: present_planes(&one, 1) (VkResult -11), which also reproduces identically on #2503 (unrelated to this PR).

Code review notes:

  • The GLSL math in runtime/src/present_compose.comp (avoiding encode+divide on premultiplied *_UNORM_SRGB planes, splitting [0, a] vs additive excess under gamma 2.2, decoding e directly when a == 0 for UNORM/HDR10 additive pixels, and to_working handling a <= 0 on HDR→SDR tone mapping) and the new pixel tests in xbox_present_test.cpp look sound.
  • One change requested on runtime/src/present_compose.inc: the embedded SPIR-V generator word changed from 0x000d000b (tool 13, shaderc/glslc) to 0x0008000b (tool 8, glslangValidator) and the ID bound dropped from 0xbb3 to 0x35f, producing a ~2,800-line .inc diff that can't easily be diff-reviewed against the GLSL change. Regenerating present_compose.inc with the repo's usual compiler/flags (or noting the generator switch in the PR description) would make the SPIR-V diff much cleaner to audit.

AI-assisted.

@akimfomin

Copy link
Copy Markdown
Contributor

Vice-Tay #370 isolates the transfer-function portion: an sRGB view reading a midpoint returned about 0.21582 rather than the raw 0.50196. That provides a simple full-alpha control for the sRGB path here. Then run the exact RGB/alpha pair at half alpha against the premultiplied path and retain a linear-format control. This would distinguish a transfer-function fix from applying the alpha correction twice.

@lixonik

lixonik commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

@timbot103 Thanks for the dual-GPU + validation verification! glslc is not installed locally here (only glslang), so I updated the PR description with a dedicated SPIR-V generator note explicitly documenting the generator switch from 0x000d000b (shaderc/glslc, ID bound 0xbb3) to 0x0008000b (glslang -V --target-env vulkan1.1, ID bound 0x35f, 5041 words).

@akimfomin Good check -- xbox_present_test.cpp covers those controls side by side:

  • Full-alpha *_UNORM_SRGB controls: opaque srgb plane 0 (0.5 linear -> byte 188) and opaque dark_opaque (a = 1.0, byte 20) on plane 1 under gamma22.
  • Half-alpha premultiplied *_UNORM_SRGB ({0.5, 0.5, 0.5, 0.5} -> 188/186) vs straight-alpha *_UNORM_SRGB ({1.0, 1.0, 1.0, 0.5} -> 188/186).
  • Linear-format (R8G8B8A8_UNORM) premultiplied control (half_white {128, 128, 128, 128} -> 188/186 right above in C2).

AI-assisted (Antigravity, Gemini).

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

Static code audit against ef5620bf6 and ec740bcc3. The embedded SPIR-V matches an in-memory glslang compile byte for byte and passes Vulkan 1.1, 1.2, and 1.3 validation. The new uniform overlay expectations agree with the hand calculations.

  • runtime/src/present_compose.comp:153 — Please preserve additive HDR light separately through bilinear filtering and SDR tone mapping. With 203-nit white and a 1000-nit peak, the 100-nit zero-alpha grey from C4 gives byte 186 over black. Mixing in just 2.5% opaque black gives about byte 45 because the shader divides the additive light by the filtered alpha before tone mapping. Weighting the additive contribution gives about byte 184. A mixed-texel C4 case would catch the resulting dark seam.
  • runtime/src/present_compose.comp:100 — C2b's UNORM red code 188 over grey 64 gives red byte 196 at alpha code 0 and clips to 255 at alpha code 1. Please handle the encoded additive excess independently of coverage and test those adjacent alpha codes.

AI-assisted.

@lixonik

lixonik commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

@timbot103 Thanks for pointing out both edge cases. 10f14f613 keeps additive light separate from covered RGB through bilinear filtering and HDR-to-SDR tone mapping. For premultiplied UNORM, encoded RGB above alpha is decoded separately as additive light. I added checks for adjacent alpha codes 0 and 1 and for an HDR additive texel filtered against opaque black, and regenerated the embedded SPIR-V with the PR's existing glslang command.

The previous shader failed at alpha code 1 with the new fixture. With the updated shader both new cases passed on Windows 11 with RTX 5070 (610.88). The standalone run still hit the existing swapchain resize failure later in the test. With #2507 applied locally, the complete xbox_present test passed.

AI-assisted.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants