From 3774cc54c7aee737ba770039ba99e9bd4314a46f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 2 Feb 2020 11:45:48 +0530 Subject: [PATCH] Fix rendering of semi-transparent bg images in semi-transparent windows Output colors must be premultiplied --- kitty/bgimage_fragment.glsl | 6 +++++- kitty/shaders.c | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/kitty/bgimage_fragment.glsl b/kitty/bgimage_fragment.glsl index 405784be5..bd15691a6 100644 --- a/kitty/bgimage_fragment.glsl +++ b/kitty/bgimage_fragment.glsl @@ -2,10 +2,14 @@ uniform sampler2D image; uniform float opacity; +uniform float premult; in vec2 texcoord; out vec4 color; void main() { color = texture(image, texcoord); - color = vec4(color.rgb, color.a * opacity); + float alpha = color.a * opacity; + vec4 premult_color = vec4(color.rgb * alpha, alpha); + color = vec4(color.rgb, alpha); + color = premult * premult_color + (1 - premult) * color; } diff --git a/kitty/shaders.c b/kitty/shaders.c index fc569aa54..8bfa15c8d 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -165,7 +165,7 @@ typedef struct { static CellProgramLayout cell_program_layouts[NUM_PROGRAMS]; static ssize_t blit_vertex_array; typedef struct { - GLint image_location, tiled_location, sizes_location, opacity_location; + GLint image_location, tiled_location, sizes_location, opacity_location, premult_location; } BGImageProgramLayout; static BGImageProgramLayout bgimage_program_layout = {0}; typedef struct { @@ -194,6 +194,7 @@ init_cell_program(void) { bgimage_program_layout.opacity_location = get_uniform_location(BGIMAGE_PROGRAM, "opacity"); bgimage_program_layout.sizes_location = get_uniform_location(BGIMAGE_PROGRAM, "sizes"); bgimage_program_layout.tiled_location = get_uniform_location(BGIMAGE_PROGRAM, "tiled"); + bgimage_program_layout.premult_location = get_uniform_location(BGIMAGE_PROGRAM, "premult"); tint_program_layout.tint_color_location = get_uniform_location(TINT_PROGRAM, "tint_color"); tint_program_layout.edges_location = get_uniform_location(TINT_PROGRAM, "edges"); } @@ -362,6 +363,7 @@ draw_bg(OSWindow *w) { } glUniform4f(bgimage_program_layout.sizes_location, (GLfloat)w->window_width, (GLfloat)w->window_height, (GLfloat)w->bgimage->width, (GLfloat)w->bgimage->height); + glUniform1f(bgimage_program_layout.premult_location, w->is_semi_transparent ? 1.f : 0.f); glActiveTexture(GL_TEXTURE0 + BGIMAGE_UNIT); glBindTexture(GL_TEXTURE_2D, w->bgimage->texture_id); glDrawArrays(GL_TRIANGLE_FAN, 0, 4);