From 3c2e521dd79f7bfa01a20426833a3bef4fba5c77 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 14 Oct 2021 21:29:02 +0530 Subject: [PATCH] Make blending of foreground more efficient Avoids an unnecessary reversal of premultiplication when rendering interleaved. Also fixes rendering on wayland which for some reason was using a different blend function than X11. Fixes #4120 --- kitty/cell_fragment.glsl | 10 ++-------- kitty/shaders.c | 2 ++ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/kitty/cell_fragment.glsl b/kitty/cell_fragment.glsl index 87994dc9d..c1d639627 100644 --- a/kitty/cell_fragment.glsl +++ b/kitty/cell_fragment.glsl @@ -82,7 +82,7 @@ vec4 blend_onto_opaque_premul(vec3 over, float over_alpha, vec3 under) { * 3) Draw the background of cells that don't have the default background if any images were drawn in 2 above * 4) Draw the images that are supposed to be below text but not background, again in graphics shader. * 5) Draw the special cells (selection/cursor). Output is same as from step 1, with bg_alpha 1 for special cells and 0 otherwise - * 6) Draw the foreground -- expected output is color with alpha which is blended using the opaque blend func + * 6) Draw the foreground -- expected output is color with alpha premultiplied which is blended using the premult blend func * 7) Draw the images that are supposed to be above text again in the graphics shader * * 2b) Transparent bg with images @@ -137,13 +137,7 @@ void main() { #endif #ifdef FOREGROUND - vec4 fg = calculate_foreground(); // pre-multiplied foreground -#ifdef TRANSPARENT - final_color = fg; -#else - final_color = vec4(fg.rgb / fg.a, fg.a); -#endif - + final_color = calculate_foreground(); // pre-multiplied foreground #endif } diff --git a/kitty/shaders.c b/kitty/shaders.c index fc3562870..4ee313305 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -538,7 +538,9 @@ draw_cells_interleaved(ssize_t vao_idx, ssize_t gvao_idx, Screen *screen, OSWind glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, screen->lines * screen->columns); bind_program(CELL_FG_PROGRAM); + BLEND_PREMULT; glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, screen->lines * screen->columns); + BLEND_ONTO_OPAQUE; if (screen->grman->num_of_positive_refs) draw_graphics(GRAPHICS_PROGRAM, vao_idx, gvao_idx, screen->grman->render_data, screen->grman->num_of_negative_refs + screen->grman->num_of_below_refs, screen->grman->num_of_positive_refs);