From b40a4f5f7deed3dd03347c58d53456b01e6d4b7c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 13 Jun 2023 14:58:51 +0530 Subject: [PATCH] DRYer --- kitty/alpha_blend.glsl | 22 ++++++++++++++++++++++ kitty/cell_fragment.glsl | 24 +----------------------- kitty/graphics_fragment.glsl | 10 +--------- 3 files changed, 24 insertions(+), 32 deletions(-) create mode 100644 kitty/alpha_blend.glsl diff --git a/kitty/alpha_blend.glsl b/kitty/alpha_blend.glsl new file mode 100644 index 000000000..d611c546f --- /dev/null +++ b/kitty/alpha_blend.glsl @@ -0,0 +1,22 @@ +vec4 alpha_blend(vec4 over, vec4 under) { + // Alpha blend two colors returning the resulting color pre-multiplied by its alpha + // and its alpha. + // See https://en.wikipedia.org/wiki/Alpha_compositing + float alpha = mix(under.a, 1.0f, over.a); + vec3 combined_color = mix(under.rgb * under.a, over.rgb, over.a); + return vec4(combined_color, alpha); +} + +vec4 alpha_blend_premul(vec4 over, vec4 under) { + // Same as alpha_blend() except that it assumes over and under are both premultiplied. + float inv_over_alpha = 1.0f - over.a; + float alpha = over.a + under.a * inv_over_alpha; + return vec4(over.rgb + under.rgb * inv_over_alpha, alpha); +} + +vec4 alpha_blend_premul(vec4 over, vec3 under) { + // same as alpha_blend_premul with under_alpha = 1 outputs a blended color + // with alpha 1 which is effectively pre-multiplied since alpha is 1 + float inv_over_alpha = 1.0f - over.a; + return vec4(over.rgb + under.rgb * inv_over_alpha, 1.0); +} diff --git a/kitty/cell_fragment.glsl b/kitty/cell_fragment.glsl index 8ce592e21..573ff0703 100644 --- a/kitty/cell_fragment.glsl +++ b/kitty/cell_fragment.glsl @@ -1,3 +1,4 @@ +#pragma kitty_include_shader #pragma kitty_include_shader #define {WHICH_PROGRAM} @@ -40,29 +41,6 @@ in float colored_sprite; out vec4 final_color; // Util functions {{{ -vec4 alpha_blend(vec4 over, vec4 under) { - // Alpha blend two colors returning the resulting color pre-multiplied by its alpha - // and its alpha. - // See https://en.wikipedia.org/wiki/Alpha_compositing - float alpha = mix(under.a, 1.0f, over.a); - vec3 combined_color = mix(under.rgb * under.a, over.rgb, over.a); - return vec4(combined_color, alpha); -} - -vec4 alpha_blend_premul(vec4 over, vec4 under) { - // Same as alpha_blend() except that it assumes over and under are both premultiplied. - float inv_over_alpha = 1.0f - over.a; - float alpha = over.a + under.a * inv_over_alpha; - return vec4(over.rgb + under.rgb * inv_over_alpha, alpha); -} - -vec4 alpha_blend_premul(vec4 over, vec3 under) { - // same as alpha_blend_premul with under_alpha = 1 outputs a blended color - // with alpha 1 which is effectively pre-multiplied since alpha is 1 - float inv_over_alpha = 1.0f - over.a; - return vec4(over.rgb + under.rgb * inv_over_alpha, 1.0); -} - vec4 vec4_premul(vec3 rgb, float a) { return vec4(rgb * a, a); } diff --git a/kitty/graphics_fragment.glsl b/kitty/graphics_fragment.glsl index 709698868..95ce0cbbb 100644 --- a/kitty/graphics_fragment.glsl +++ b/kitty/graphics_fragment.glsl @@ -1,3 +1,4 @@ +#pragma kitty_include_shader #define ALPHA_TYPE uniform sampler2D image; @@ -11,15 +12,6 @@ uniform float inactive_text_alpha; in vec2 texcoord; out vec4 color; -#ifdef ALPHA_MASK -vec4 alpha_blend_premul(vec4 over, vec4 under) { - // Same as alpha_blend() except that it assumes over and under are both premultiplied. - float inv_over_alpha = 1.0f - over.a; - float alpha = over.a + under.a * inv_over_alpha; - return vec4(over.rgb + under.rgb * inv_over_alpha, alpha); -} -#endif - void main() { color = texture(image, texcoord); #ifdef ALPHA_MASK