11#pragma kitty_include_shader < alpha_blend.glsl>
22#pragma kitty_include_shader < linear2srgb.glsl>
3-
4- #define {WHICH_PROGRAM}
5- #define NOT_TRANSPARENT
6- #define NO_FG_OVERRIDE
7- #define TEXT_NEW_GAMMA
8-
9- #if defined(SIMPLE) || defined(BACKGROUND) || defined(SPECIAL)
10- #define NEEDS_BACKROUND
11- #endif
12-
13- #if defined(SIMPLE) || defined(FOREGROUND)
14- #define NEEDS_FOREGROUND
15- #endif
3+ #pragma kitty_include_shader < cell_defines.glsl>
164
175#ifdef NEEDS_BACKROUND
186in vec3 background;
197in float draw_bg;
20- #if defined(TRANSPARENT) || defined(SPECIAL)
8+ #ifdef NEEDS_BG_ALPHA
219in float bg_alpha;
2210#endif
2311#endif
@@ -53,8 +41,10 @@ vec4 vec4_premul(vec4 rgba) {
5341
5442/*
5543 * Explanation of rendering:
56- * There are a couple of cases, in order of increasing complexity:
57- * 1) Simple -- this path is used when there are either no images, or all images are
44+ * There are two types of rendering, single pass and multi-pass. Multi-pass rendering is used when there
45+ * are images that are below the foreground. Single pass rendering has PHASE=PHASE_BOTH. Otherwise, there
46+ * are three passes, PHASE=PHASE_BACKGROUND, PHASE=PHASE_SPECIAL, PHASE=PHASE_FOREGROUND.
47+ * 1) Single pass -- this path is used when there are either no images, or all images are
5848 * drawn on top of text and the background is opaque. In this case, there is a single pass,
5949 * of this shader with cell foreground and background colors blended directly.
6050 * Expected output is a color premultiplied by alpha, with an alpha specified as well.
@@ -77,10 +67,9 @@ vec4 vec4_premul(vec4 rgba) {
7767 * 2b) Transparent bg with images
7868 * Same as (2a) except blending is done with PREMULT_BLEND and TRANSPARENT is defined in the shaders. background_opacity
7969 * is applied to default colored background cells in step (1).
80- *
81- * In this shader exactly *one* of SIMPLE, SPECIAL, FOREGROUND or BACKGROUND will be defined, corresponding
82- * to the appropriate rendering pass from above.
8370 */
71+
72+ // foreground functions {{{
8473#ifdef NEEDS_FOREGROUND
8574// sRGB luminance values
8675const vec3 Y = vec3 (0.2126 , 0.7152 , 0.0722 );
@@ -92,12 +81,12 @@ float clamp_to_unit_float(float x) {
9281 return clamp (x, 0 .0f, 1 .0f);
9382}
9483
95- #ifdef FG_OVERRIDE
84+ #if ( FG_OVERRIDE == 1 )
9685vec3 fg_override(float under_luminance, float over_lumininace, vec3 over) {
9786 // If the difference in luminance is too small,
9887 // force the foreground color to be black or white.
9988 float diff_luminance = abs (under_luminance - over_lumininace);
100- float override_level = (1 .f - colored_sprite) * step (diff_luminance, FG_OVERRIDE );
89+ float override_level = (1 .f - colored_sprite) * step (diff_luminance, FG_OVERRIDE_THRESHOLD );
10190 float original_level = 1 .f - override_level;
10291 return original_level * over + override_level * vec3 (step (under_luminance, 0 .5f));
10392}
@@ -107,7 +96,7 @@ vec4 foreground_contrast(vec4 over, vec3 under) {
10796 float under_luminance = dot (under, Y);
10897 float over_lumininace = dot (over.rgb, Y);
10998
110- #ifdef FG_OVERRIDE
99+ #if ( FG_OVERRIDE == 1 )
111100 over.rgb = fg_override(under_luminance, over_lumininace, over.rgb);
112101#endif
113102
@@ -117,12 +106,12 @@ vec4 foreground_contrast(vec4 over, vec3 under) {
117106 return over;
118107}
119108
120- #ifdef TEXT_OLD_GAMMA
109+ #if (TEXT_NEW_GAMMA == 0 )
121110vec4 foreground_contrast_incorrect(vec4 over, vec3 under) {
122111 // Simulation of gamma-incorrect blending
123112 float under_luminance = dot (under, Y);
124113 float over_lumininace = dot (over.rgb, Y);
125- #ifdef FG_OVERRIDE
114+ #if ( FG_OVERRIDE == 1 )
126115 over.rgb = fg_override(under_luminance, over_lumininace, over.rgb);
127116#endif
128117 // This is the original gamma-incorrect rendering, it is the solution of the following equation:
@@ -156,14 +145,15 @@ vec4 calculate_premul_foreground_from_sprites(vec4 text_fg) {
156145vec4 adjust_foreground_contrast_with_background(vec4 text_fg, vec3 bg) {
157146 // When rendering on a background we can adjust the alpha channel contrast
158147 // to improve legibility based on the source and destination colors
159- #ifdef TEXT_OLD_GAMMA
148+ #if (TEXT_NEW_GAMMA == 0 )
160149 return foreground_contrast_incorrect(text_fg, bg);
161150#else
162151 return foreground_contrast(text_fg, bg);
163152#endif
164153}
165154
166155#endif
156+ // end foreground functions }}}
167157
168158float adjust_alpha_for_incorrect_blending_by_compositor(float text_fg_alpha, float final_alpha) {
169159 // Adjust the transparent alpha-channel to account for incorrect
@@ -179,7 +169,7 @@ float adjust_alpha_for_incorrect_blending_by_compositor(float text_fg_alpha, flo
179169}
180170
181171void main() {
182- #ifdef SIMPLE
172+ #if (PHASE == PHASE_BOTH)
183173 vec4 text_fg = load_text_foreground_color();
184174 text_fg = adjust_foreground_contrast_with_background(text_fg, background);
185175 vec4 text_fg_premul = calculate_premul_foreground_from_sprites(text_fg);
@@ -191,23 +181,23 @@ void main() {
191181#endif
192182#endif
193183
194- #ifdef SPECIAL
184+ #if (PHASE == PHASE_SPECIAL)
195185#ifdef TRANSPARENT
196186 final_color = vec4_premul(background, bg_alpha);
197187#else
198188 final_color = vec4 (background, bg_alpha);
199189#endif
200190#endif
201191
202- #ifdef BACKGROUND
192+ #if (PHASE == PHASE_BACKGROUND)
203193#ifdef TRANSPARENT
204194 final_color = vec4_premul(background, bg_alpha);
205195#else
206196 final_color = vec4 (background, draw_bg);
207197#endif
208198#endif
209199
210- #ifdef FOREGROUND
200+ #if (PHASE == PHASE_FOREGROUND)
211201 vec4 text_fg = load_text_foreground_color();
212202 vec4 text_fg_premul = calculate_premul_foreground_from_sprites(text_fg);
213203 final_color = text_fg_premul;
0 commit comments