From debe76733cacbd8931815285221e0cadd843cd02 Mon Sep 17 00:00:00 2001 From: Xaver Hugl Date: Tue, 13 Jun 2023 14:44:48 +0200 Subject: [PATCH] libkwineffects/kwinglutils: handle alpha better with colormanagement Alpha from clients is encoded in electrical values, and some of our code also relies on compositing results being encoded in electrical values. To take care of this, we need to undo the premultiplication of rgb values, convert to or from linear, and then re-do the premultiplication. This doesn't result in exactly the same blending behavior with linear blending, but it's close enough --- src/libkwineffects/kwinglutils.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libkwineffects/kwinglutils.cpp b/src/libkwineffects/kwinglutils.cpp index be8892242b..f2bde82cc0 100644 --- a/src/libkwineffects/kwinglutils.cpp +++ b/src/libkwineffects/kwinglutils.cpp @@ -856,7 +856,9 @@ QByteArray ShaderManager::generateFragmentSource(ShaderTraits traits) const } if (traits & ShaderTrait::TransformColorspace) { stream << " if (sourceNamedTransferFunction == sRGB_EOTF) {\n"; + stream << " result.rgb /= max(result.a, 0.001);\n"; stream << " result.rgb = sdrBrightness * srgbToLinear(result.rgb);\n"; + stream << " result.rgb *= result.a;\n"; stream << " }\n"; stream << " result.rgb = doTonemapping(colorimetryTransform * result.rgb, maxHdrBrightness);\n"; } @@ -871,7 +873,9 @@ QByteArray ShaderManager::generateFragmentSource(ShaderTraits traits) const } if (traits & ShaderTrait::TransformColorspace) { stream << " if (destinationNamedTransferFunction == sRGB_EOTF) {\n"; + stream << " result.rgb /= max(result.a, 0.001);\n"; stream << " result.rgb = linearToSrgb(doTonemapping(result.rgb, sdrBrightness) / sdrBrightness);\n"; + stream << " result.rgb *= result.a;\n"; stream << " } else if (destinationNamedTransferFunction == PQ_EOTF) {\n"; stream << " result.rgb = nitsToPq(result.rgb);\n"; stream << " }\n";