present_compose: fix premultiplied sRGB and additive alpha plane 1 decoding #2508
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2508"
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?
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-27T21:35:25Z.
Summary
Fixes two alpha-decoding bugs in the PresentX composition shader (
runtime/src/present_compose.comp+ regeneratedruntime/src/present_compose.inc) for premultiplied plane 1 overlays:Premultiplied
*_UNORM_SRGBplane 1 overlays (flag_srgb_source != 0,flag_straight_alpha == 0,!opaque):An
*_UNORM_SRGBrender target blends in linear light and sRGB-encodes the premultiplied RGB on write, sotexelFetchalready returns linear premultiplied RGB. Previouslyfetch_texelre-encodedc.rgbwithsrgb_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 (255instead of188and137).pc.sdr_eotf == 0u),fetch_texelnow returnsmax(c.rgb, 0.0)directly without re-encoding or dividing by alpha.pc.sdr_eotf == 1u),fetch_texelsplits continuously into the[0, a]unassociated sRGB component (preserving stored-byte identity for opaque pixels and avoiding 8-bit sRGB quantization jumps atlin ~= a) plus any additive excessmax(lin - a, 0.0).*_UNORM_SRGBoverlays (flag_straight_alpha != 0) and opaque*_UNORM_SRGBplane 0 are unchanged.Additive premultiplied pixels (
rgb > 0, a == 0) on plane 1:Previously
fetch_texelreturnedvec4(0.0)whenevera == 0.0(andto_workingreturnedvec3(0.0)whenplane_hdr && !out_hdr && a <= 0.0), discarding additive premultiplied glow/UI pixels even thoughpresent_compose.compexplicitly notes additive premultiplied colour support.*_UNORM_SRGBplanes now decodee/ preservelinwhena == 0.0, while transparent black(0, 0, 0, 0)and straight-alphaa == 0pixels still contribute zero light.to_workingnow tone-maps HDR10 additive emission whena <= 0.0instead of zeroing it.Updated
docs/d3d12_vulkan.mdsection 4.5 to document both rules.SPIR-V generator note (
runtime/src/present_compose.inc)main'spresent_compose.incwas compiled withglslc --target-env=vulkan1.1 -O -mfmt=c(shaderc generator word0x000d000b, ID bound0xbb3). Becauseglslcis not installed locally,present_compose.incin this PR was regenerated frompresent_compose.compusingglslang -V --target-env vulkan1.1(glslang generator word0x0008000b, ID bound0x35f, SPIR-V 1.3, 5041 words), which accounts for the full-file.incdiff.Verification
main39f52f39c):xbox_present_testfailed immediately at C2b withpixel 0,0 is 255,255,255, expected 188,188,188on a 50% premultipliedR8G8B8A8_UNORM_SRGBoverlay over black.xbox_present_testpass on RTX 5070: premultiplied and straight*_UNORM_SRGBoverlays under piecewise sRGB and gamma 2.2 (50% -> 188/186, 25% -> 137, opaque gamma 2.2 byte-20 identity), additive premultiplieda == 0for sRGB, UNORM, and HDR10 (PQ and SDR tone-map), plus transparent-black and straight-alphaa == 0invariants.main(39f52f39c) has a pre-existing resize-readback failure later inxbox_presentcase F (9f98dcc61regression, fixed separately in #2507). With #2507 applied alongside this PR,ctest --output-on-failure -R ^xbox_present$passes 1/1 (2.78 s).python -I scripts/check_no_game_data.pyon all 4 changed files: pass.python -I tests/test_ctest_registration.py -v: pass (6/6).git diff --check: clean;git merge-treeclean against open PRs #2001, #2014, and #2507.Claimed in https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7228468595 (
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_validationandspirv-valnot installed locally).AI-assisted (Antigravity, Gemini).
Independent Vulkan validation passed the composition assertions on RTX 3080 and the full test, including the HDR-to-SDR additive edge, on AMD Radeon integrated graphics. The RTX 3080 full run passed with #2507 applied for swapchain resize. Test report.
Imported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T00:53:27Z.
Independently checked head
ef5620bf6on Windows 11 Pro 26200, MSVC 2022 (19.44.35230.0, RelWithDebInfo, Ninja), Vulkan SDK1.4.363.0, no game files.Local build & dual-GPU verification (
xbox_present_test,LEONIDA_PRESENT_PLANE1=0,LEONIDA_VK_VALIDATION=1):xbox_present_test.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: okcompose: additive premultiplied (a == 0) sRGB and UNORM plane 1 overlays: okcompose: HDR10 plane 1 additive premultiplied (a == 0) over SDR plane 0: ok616.64): all compose cases above passed (0 validation errors); the later swapchain resize step failed atline 1304: present_planes(&one, 1)(VkResult -11), which also reproduces identically on#2503(unrelated to this PR).Code review notes:
runtime/src/present_compose.comp(avoiding encode+divide on premultiplied*_UNORM_SRGBplanes, splitting[0, a]vs additive excess under gamma 2.2, decodingedirectly whena == 0for UNORM/HDR10 additive pixels, andto_workinghandlinga <= 0on HDR→SDR tone mapping) and the new pixel tests inxbox_present_test.cpplook sound.runtime/src/present_compose.inc: the embedded SPIR-V generator word changed from0x000d000b(tool 13, shaderc/glslc) to0x0008000b(tool 8,glslangValidator) and the ID bound dropped from0xbb3to0x35f, producing a ~2,800-line.incdiff that can't easily be diff-reviewed against the GLSL change. Regeneratingpresent_compose.incwith 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.
Imported from ubiquitarianism/leonida. Originally posted by akimfomin on 2026-11-28T01:25:18Z.
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.
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T08:17:04Z.
@timbot103 Thanks for the dual-GPU + validation verification!
glslcis not installed locally here (onlyglslang), so I updated the PR description with a dedicated SPIR-V generator note explicitly documenting the generator switch from0x000d000b(shaderc/glslc, ID bound0xbb3) to0x0008000b(glslang -V --target-env vulkan1.1, ID bound0x35f, 5041 words).@akimfomin Good check --
xbox_present_test.cppcovers those controls side by side:*_UNORM_SRGBcontrols: opaquesrgbplane 0 (0.5linear -> byte188) and opaquedark_opaque(a = 1.0, byte20) on plane 1 undergamma22.*_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).R8G8B8A8_UNORM) premultiplied control (half_white{128, 128, 128, 128}->188/186right above in C2).AI-assisted (Antigravity, Gemini).
Imported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T14:36:31Z.
Review: COMMENTED
Static code audit against
ef5620bf6andec740bcc3. 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.
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T15:45:28Z.
@timbot103 Thanks for pointing out both edge cases.
10f14f613keeps 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_presenttest passed.AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by Maticcm on 2026-11-28T20:21:58Z.
Independent native Windows verification of
10f14f6130e8cf4b4014dc9cf93ec05f497caf74, syntheticxbox_present_test, RelWithDebInfo, MSVC 19.50.35717, Vulkan SDK 1.4.363.1, Vulkan validation enabled.present_planes(&one, 1).b8486b4a58e6f0748b350225bf84ff3156c86694(local combinationcc6f37308a0c2a4cb1d3a96c880d955ac10f713d)No Vulkan validation errors were reported in these runs.
spirv-val --target-env vulkan1.1passed for the submitted embedded synthetic module (5,414 words) and for a separateglslc 2026.4 -Ocompilation of the submitted GLSL. The GPU test executable used the embedded module from its recorded source revision. Windows 11 build 26340. AI-assisted (Codex).Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-29T05:58:30Z.
Tested
10f14f613merged (no commit) withmainc8bf0e7b6. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40, Windows 11 10.0.26300; MSVC 19.52 (Visual Studio 18), Ninja, RelWithDebInfo.check_no_game_data.pyis clean.xbox_presentexits non-zero both onmainand on the merge: it stops at the swapchain resize step (line 1319: present_planes(&one, 1),VkResult -11), which #2507 fixes. So the verdict below comes from itscompose:lines, not from the exit code.compose:check runs and passes, includingpremultiplied and straight *_UNORM_SRGB plane 1 overlays: ok, until that pre-existing resize stop.present_compose.compandpresent_compose.incback tomain, it stops earlier, at the new checkline 1085: region_is(frame, 0, 0, width, height, g22 ? Px{186, 186, 186} : Px{188, 188, 188}, 1). The old shader clips to 255.gta6rt.exelinked withoutws2_32.libgivesmain's 26 unresolved Rust-std symbols, nothing new. Its imports are identical tomain's.10f14f613) answers the review's two points (HDR additive light through bilinear filtering, UNORM alpha codes 0/1) with tests.present_compose.incis regenerated with glslang, as the description says.No blocker from my side. AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T08:20:16Z.
Merged after TheGeogeo's red/green check (check: green with this PR merged onto
main, red without it;gta6rt.exestill links) and a re-check here: no conflict withmaina6a65a452, same head as checked, not frozen,no-game-datagreen.AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.