From 57d32722a262edf52ab5f520d9e827bcc647653a Mon Sep 17 00:00:00 2001 From: Pekka Paalanen Date: Fri, 3 Jun 2022 14:15:01 +0300 Subject: [PATCH] gl-renderer: simplify main() in frag By moving the application of view_alpha after pre-multiplication we can simplify main() considerably. The cost is that for straight-alpha input or color_pipeline() we might be doing three multiplications more than before. However, a) the cost of running color_pipeline() probably dominates anyway, and b) to get straight-alpha input you have to use a future Wayland extension that probably won't be advertised without color management. So we keep the optimization for the simple case (no color management) while potentially incurring a small cost on the heavy case (with color management). Thanks to Pierre-Yves Mordred for the inspiration in https://gitlab.freedesktop.org/wayland/weston/-/merge_requests/889#note_1411774 Signed-off-by: Pekka Paalanen --- libweston/renderer-gl/fragment.glsl | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/libweston/renderer-gl/fragment.glsl b/libweston/renderer-gl/fragment.glsl index 6271c9cd..7de8d984 100644 --- a/libweston/renderer-gl/fragment.glsl +++ b/libweston/renderer-gl/fragment.glsl @@ -271,24 +271,14 @@ main() /* Electrical (non-linear) RGBA values, may be premult or not */ color = sample_input_texture(); - if (c_need_color_pipeline) { + if (c_need_color_pipeline) color = color_pipeline(color); /* Produces straight alpha */ - color.a *= view_alpha; - - /* pre-multiply for blending */ + /* Ensure pre-multiplied for blending */ + if (!c_input_is_premult || c_need_color_pipeline) color.rgb *= color.a; - } else { - /* Fast path for disabled color management */ - if (c_input_is_premult) { - color *= view_alpha; - } else { - color.a *= view_alpha; - /* pre-multiply for blending */ - color.rgb *= color.a; - } - } + color *= view_alpha; if (c_green_tint) color = vec4(0.0, 0.3, 0.0, 0.2) + color * 0.8;