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: 9a6a4e7032

Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.com>
(cherry picked from commit 6234cb98d1)

Dropped SHADER_COLOR_MAPPING_IDENTITY as that is not available in weston
10.0.
dev
Pekka Paalanen 2 years ago committed by Marius Vlad
parent f7c5fa175e
commit f31de214d9
  1. 43
      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;

Loading…
Cancel
Save