This commit is contained in:
Kovid Goyal
2023-06-13 14:58:51 +05:30
parent 288bb034b5
commit b40a4f5f7d
3 changed files with 24 additions and 32 deletions

22
kitty/alpha_blend.glsl Normal file
View File

@@ -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);
}

View File

@@ -1,3 +1,4 @@
#pragma kitty_include_shader <alpha_blend.glsl>
#pragma kitty_include_shader <linear2srgb.glsl>
#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);
}

View File

@@ -1,3 +1,4 @@
#pragma kitty_include_shader <alpha_blend.glsl>
#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