Fix transposed BT.709 to BT.2020 matrix in Vulkan HDR imgui overlay - #441
Merged
crosire merged 1 commit intoSep 18, 2026
Merged
Conversation
The imgui HDR helper's GLSL compatibility shim defined "mul(a, b)" as "(a) * (b)". GLSL constructs matrices column-major, so the bt709_to_bt2020 constant (written in HLSL row-major order) becomes its own transpose, and "(matrix) * (vector)" then applies that transposed matrix. On neutral colors this shifts the HDR10 PQ and HLG overlay toward green; the sRGB/D3D paths are unaffected because they either skip the matrix or use HLSL's row-major mul. Swap the operands to "(b) * (a)" so the GLSL multiply matches HLSL's "mul(m, v)", and regenerate imgui_ps_450.spv with glslangValidator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The ImGui overlay picks up a colour tint on Vulkan whenever it's drawn onto an HDR swapchain (HDR10 ST2084 / PQ, and the HLG path as well). Neutral greys visibly shift toward green. The Direct3D backends are unaffected.
Cause
res/shaders/imgui_hdr.hlslis compiled both as HLSL (with fxc) and as GLSL (with glslangValidator), so it carries a small compatibility shim that only activates for the GLSL builds, wherefloat3x3is#defined tomat3:The two languages read that same list of coefficients differently. HLSL builds matrices in row-major order [1], and
mul(M, v)treatsvas a column vector [2], somul(bt709_to_bt2020, col)produces the intendedM * col. GLSL fills matrix constructors in column-major order instead (spec §5.4.2 [3]), so the identical coefficients build the transpose,M^T. The shim's(matrix) * (vector)is thenM * colwith a right-hand vector treated as a column vector (spec §5.9 [3]), which comes out asM^T * col, i.e. the matrix applied transposed.A transposed BT.709 to BT.2020 conversion no longer preserves the D65 white point, so a neutral grey
(g, g, g)ends up around(0.71, 1.34, 0.95) * g, which reads as green. Bothto_hdr10_pqandto_hdr10_hlgcallbt709_to_bt2020, so both are affected. The sRGB and scRGB paths never touch the matrix, so they stay correct.Fix
Swap the operands in the GLSL shim:
The multiply is now
col * M. In GLSL a left-hand vector is treated as a row vector (spec §5.9 [3]), socol * Mevaluates toM^T * col, which cancels the constructor transpose and reproduces HLSL's row-majorM * col. The Microsoft docs put it plainly: "swapping the order of the arguments supplied to multiply is equivalent to transposing the matrix" [1].res/shaders/imgui_ps_450.spvis regenerated with the existing glslangValidator flags.The change is about as contained as it gets. Diffing the disassembly of the old and regenerated SPIR-V, the only meaningful difference is a single instruction (everything else is just ID renumbering), and the matrix coefficients are byte-for-byte identical:
So the
color_spaceswitch,to_scrgb, the SDR path, the PQ and HLG transfer functions, the texture sampling and the blending are all left untouched.Scope by backend
#ifdef float3x3is false here (it's a builtin type, not a macro), so the shim never activates and HLSL's own row-majormulwas always correct.imgui_ps_3_0.hlslandimgui_ps_4_0.hlslare unchanged, and this backend never had the bug.imgui_ps_450.spv.imgui_ps_430.glsldoesn't includeimgui_hdr.hlsl, so there's no matrix path there and nothing changes.hlg_inverse_eotf) and the reference-white scaling (/1000rather than/10000), neither of which involves the matrix.Testing
Tested on Arch Linux with KDE Plasma (Wayland) and HDR enabled, on an HDR10 ST2084 Vulkan swapchain (GTA IV running through D3D9 and DXVK). After the change the overlay's neutral greys come out neutral rather than green. I confirmed this with RenderDoc captures taken before and after the fix, where the overlay pixels move from green-dominant (
R, B < G) to neutral (R ≈ G ≈ B). PQ is visually confirmed. HLG goes through the identical code, though I haven't checked it on an HLG display.Screenshots
Before, the green-tinted overlay on the HDR10 ST2084 Vulkan swapchain:
After, the neutral overlay once the fix is applied:
References
mul: "If y is a vector, it [is] treated as a column vector." https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-mul