From f31de214d93b07cc192393175b90f2ec2151c991 Mon Sep 17 00:00:00 2001 From: Pekka Paalanen Date: Thu, 2 Jun 2022 11:24:52 +0300 Subject: [PATCH] gl-renderer: fix performance regression in frag When color management is disabled, the fragment shader was still first ensuring straight alpha and then immediately just going back to pre-multiplied. This is near-impossible for a shader compiler to optimize out, I guess because of the if-statement to handle division by zero. Having view alpha applied in between certainly didn't make it easier. That causes extra fragment computations that are unnecessary. In the issue report this was found to cause a notable performance regression. Fix the performance regression by introducing special-case paths for when straight alpha is not needed. This skips the unnecessary computations. Fixes: https://gitlab.freedesktop.org/wayland/weston/-/issues/623 Fixes: 9a6a4e7032669be727c965ca19e3e30098c892e7 Signed-off-by: Pekka Paalanen (cherry picked from commit 6234cb98d1b2b3b3cd39b18f69ee8cf4aa7efe32) Dropped SHADER_COLOR_MAPPING_IDENTITY as that is not available in weston 10.0. --- libweston/renderer-gl/fragment.glsl | 43 ++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/libweston/renderer-gl/fragment.glsl b/libweston/renderer-gl/fragment.glsl index 63a20cd6..cfadb885 100644 --- a/libweston/renderer-gl/fragment.glsl +++ b/libweston/renderer-gl/fragment.glsl @@ -67,6 +67,9 @@ compile_const bool c_input_is_premult = DEF_INPUT_IS_PREMULT; compile_const bool c_green_tint = DEF_GREEN_TINT; compile_const int c_color_pre_curve = DEF_COLOR_PRE_CURVE; +compile_const bool c_need_color_pipeline = + c_color_pre_curve != SHADER_COLOR_CURVE_IDENTITY; + vec4 yuva2rgba(vec4 yuva) { @@ -202,9 +205,6 @@ color_pre_curve(vec3 color) vec4 color_pipeline(vec4 color) { - /* View alpha (opacity) */ - color.a *= alpha; - color.rgb = color_pre_curve(color.rgb); return color; @@ -218,18 +218,35 @@ main() /* Electrical (non-linear) RGBA values, may be premult or not */ color = sample_input_texture(); - /* Ensure straight alpha */ - if (c_input_is_premult) { - if (color.a == 0.0) - color.rgb = vec3(0, 0, 0); - else - color.rgb *= 1.0 / color.a; - } + if (c_need_color_pipeline) { + /* Ensure straight alpha */ + if (c_input_is_premult) { + if (color.a == 0.0) + color.rgb = vec3(0, 0, 0); + else + color.rgb *= 1.0 / color.a; + } - color = color_pipeline(color); + color = color_pipeline(color); - /* pre-multiply for blending */ - color.rgb *= color.a; + /* View alpha (opacity) */ + color.a *= alpha; + + /* pre-multiply for blending */ + color.rgb *= color.a; + } else { + /* Fast path for disabled color management */ + + if (c_input_is_premult) { + /* View alpha (opacity) */ + color *= alpha; + } else { + /* View alpha (opacity) */ + color.a *= alpha; + /* pre-multiply for blending */ + color.rgb *= color.a; + } + } if (c_green_tint) color = vec4(0.0, 0.3, 0.0, 0.2) + color * 0.8;