Start work on shader refactor for correct linear blending

This commit is contained in:
Kovid Goyal 2025-08-01 15:54:02 +05:30
parent c86ec79e32
commit 774d53debe
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
6 changed files with 35 additions and 319 deletions

View file

@ -1,9 +1,3 @@
#define PHASE_BOTH 1
#define PHASE_BACKGROUND 2
#define PHASE_SPECIAL 3
#define PHASE_FOREGROUND 4
#define PHASE {WHICH_PHASE}
#define HAS_TRANSPARENCY {TRANSPARENT}
#define DO_FG_OVERRIDE {DO_FG_OVERRIDE}
#define FG_OVERRIDE_THRESHOLD {FG_OVERRIDE_THRESHOLD}
@ -19,14 +13,6 @@
#define USE_SELECTION_FG
#define NUM_COLORS 256
#if (PHASE == PHASE_BOTH) || (PHASE == PHASE_BACKGROUND) || (PHASE == PHASE_SPECIAL)
#define NEEDS_BACKROUND
#endif
#if (PHASE == PHASE_BOTH) || (PHASE == PHASE_FOREGROUND)
#define NEEDS_FOREGROUND
#endif
#if (HAS_TRANSPARENCY == 1)
#define TRANSPARENT
#endif

View file

@ -2,14 +2,12 @@
#pragma kitty_include_shader <linear2srgb.glsl>
#pragma kitty_include_shader <cell_defines.glsl>
in vec3 background;
in float draw_bg;
in float bg_alpha;
#ifdef NEEDS_FOREGROUND
uniform sampler2DArray sprites;
uniform float text_contrast;
uniform float text_gamma_adjustment;
in vec3 background;
in float bg_alpha;
in float effective_text_alpha;
in vec3 sprite_pos;
in vec3 underline_pos;
@ -20,7 +18,6 @@ in vec3 foreground;
in vec4 cursor_color_premult;
in vec3 decoration_fg;
in float colored_sprite;
#endif
out vec4 output_color;
@ -35,38 +32,6 @@ vec4 vec4_premul(vec4 rgba) {
// }}}
/*
* Explanation of rendering:
* There are two types of rendering, single pass and multi-pass. Multi-pass rendering is used when there
* are images that are below the foreground. Single pass rendering has PHASE=PHASE_BOTH. Otherwise, there
* are three passes, PHASE=PHASE_BACKGROUND, PHASE=PHASE_SPECIAL, PHASE=PHASE_FOREGROUND.
* 1) Single pass -- this path is used when there are either no images, or all images are
* drawn on top of text. In this case, there is a single pass,
* of this shader with cell foreground and background colors blended directly.
* Expected output is either opaque colors or pre-multiplied colors.
*
* 2) Interleaved -- this path is used if background is not opaque and there are images or
* if the background is opaque but there are images under text. Rendering happens in
* multiple passes drawing the background and foreground separately and blending.
*
* 2a) Opaque bg with images under text
* There are multiple passes, each pass is blended onto the previous using the opaque blend func (alpha, 1- alpha). TRANSPARENT is not
* defined in the shaders.
* 1) Draw background for all cells
* 2) Draw the images that are supposed to be below both the background and text, if any. This happens in the graphics shader
* 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 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
* Same as (2a) except blending is done with PREMULT_BLEND and TRANSPARENT is defined in the shaders. background_opacity
* is applied to default colored background cells in step (1).
*/
// foreground functions {{{
#ifdef NEEDS_FOREGROUND
// Scaling factor for the extra text-alpha adjustment for luminance-difference.
const float text_gamma_scaling = 0.5;
@ -129,8 +94,6 @@ vec4 adjust_foreground_contrast_with_background(vec4 text_fg, vec3 bg) {
return foreground_contrast(text_fg, bg);
}
#endif
// end foreground functions }}}
float adjust_alpha_for_incorrect_blending_by_compositor(float text_fg_alpha, float final_alpha) {
// Adjust the transparent alpha-channel to account for incorrect
@ -146,7 +109,6 @@ float adjust_alpha_for_incorrect_blending_by_compositor(float text_fg_alpha, flo
void main() {
vec4 final_color;
#if (PHASE == PHASE_BOTH)
vec4 text_fg = load_text_foreground_color();
text_fg = adjust_foreground_contrast_with_background(text_fg, background);
vec4 text_fg_premul = calculate_premul_foreground_from_sprites(text_fg);
@ -155,33 +117,6 @@ void main() {
final_color.a = adjust_alpha_for_incorrect_blending_by_compositor(text_fg_premul.a, final_color.a);
#else
final_color = alpha_blend_premul(text_fg_premul, background);
#endif
#endif
#if (PHASE == PHASE_SPECIAL)
#ifdef TRANSPARENT
final_color = vec4_premul(background, bg_alpha);
#else
final_color = vec4(background, bg_alpha);
#endif
#endif
#if (PHASE == PHASE_BACKGROUND)
#ifdef TRANSPARENT
final_color = vec4_premul(background, bg_alpha);
#else
final_color = vec4(background, draw_bg * bg_alpha);
#endif
#endif
#if (PHASE == PHASE_FOREGROUND)
vec4 text_fg = load_text_foreground_color();
text_fg = adjust_foreground_contrast_with_background(text_fg, background);
vec4 text_fg_premul = calculate_premul_foreground_from_sprites(text_fg);
final_color = text_fg_premul;
#ifdef TRANSPARENT
final_color.a = adjust_alpha_for_incorrect_blending_by_compositor(text_fg_premul.a, final_color.a);
#endif
#endif
output_color = final_color;
}

View file

@ -18,12 +18,9 @@ layout(std140) uniform CellRenderData {
uint color_table[NUM_COLORS + MARK_MASK + MARK_MASK + 2];
};
uniform float gamma_lut[256];
#ifdef NEEDS_FOREGROUND
uniform usampler2D sprite_decorations_map;
#endif
#if (PHASE == PHASE_BACKGROUND)
uniform uint draw_bg_bitfield;
#endif
uniform float inactive_text_alpha;
uniform float dim_opacity;
// Have to use fixed locations here as all variants of the cell program share the same VAOs
layout(location=0) in uvec3 colors;
@ -41,12 +38,8 @@ const uvec2 cell_pos_map[] = uvec2[4](
out vec3 background;
out float draw_bg;
out float bg_alpha;
#ifdef NEEDS_FOREGROUND
uniform float inactive_text_alpha;
uniform float dim_opacity;
out vec3 sprite_pos;
out vec3 underline_pos;
out vec3 cursor_pos;
@ -57,7 +50,6 @@ out vec3 foreground;
out vec3 decoration_fg;
out float colored_sprite;
out float effective_text_alpha;
#endif
// Utility functions {{{
@ -100,7 +92,6 @@ vec3 to_color(uint c, uint defval) {
return color_to_vec(resolve_color(c, defval));
}
#ifdef NEEDS_FOREGROUND
uvec3 to_sprite_coords(uint idx) {
uint sprites_per_page = sprites_xnum * sprites_ynum;
@ -144,7 +135,6 @@ uvec2 get_decorations_indices(uint in_url /* [0, 1] */, uint text_attrs) {
uint has_underline = uint(step(0.5f, float(underline_style))); // [0, 1]
return uvec2(strike_idx, has_underline * (decorations_idx + underline_style));
}
#endif
vec3 choose_color(float q, vec3 a, vec3 b) {
return mix(b, a, q);
@ -180,11 +170,9 @@ CellData set_vertex_position() {
vec2 ypos = vec2(top, top - dy);
uvec2 pos = cell_pos_map[gl_VertexID];
gl_Position = vec4(xpos[pos.x], ypos[pos.y], 0, 1);
#ifdef NEEDS_FOREGROUND
// The character sprite being rendered
sprite_pos = to_sprite_pos(pos, sprite_idx[0] & SPRITE_INDEX_MASK);
colored_sprite = float((sprite_idx[0] & SPRITE_COLORED_MASK) >> SPRITE_COLORED_SHIFT);
#endif
float is_block_cursor = step(float(cursor_fg_sprite_idx), 0.5);
float has_cursor = is_cursor(c, r);
return CellData(has_cursor, has_cursor * is_block_cursor, pos);
@ -209,7 +197,7 @@ float calc_background_opacity(uint bg) {
}
// Overriding of foreground colors for contrast requirements {{{
#if defined(NEEDS_FOREGROUND) && DO_FG_OVERRIDE == 1
#if DO_FG_OVERRIDE == 1
#define OVERRIDE_FG_COLORS
#pragma kitty_include_shader <hsluv.glsl>
#if (FG_OVERRIDE_ALGO == 1)
@ -272,7 +260,6 @@ void main() {
// }}}
// Foreground {{{
#ifdef NEEDS_FOREGROUND
// Foreground
fg_as_uint = has_mark * color_table[NUM_COLORS + MARK_MASK + mark] + (ONE - has_mark) * fg_as_uint;
foreground = color_to_vec(fg_as_uint);
@ -297,43 +284,22 @@ void main() {
foreground = choose_color(cell_data.has_block_cursor, final_cursor_text_color, foreground);
decoration_fg = choose_color(cell_data.has_block_cursor, final_cursor_text_color, decoration_fg);
cursor_pos = to_sprite_pos(cell_data.pos, cursor_fg_sprite_idx * uint(cell_data.has_cursor));
#endif
// }}}
// Background {{{
float orig_bg_alpha = 1;
#if PHASE == PHASE_BOTH && !defined(TRANSPARENT) // fast case single pass opaque background
bg_alpha = 1;
draw_bg = 1;
#else
bg_alpha = calc_background_opacity(bg_as_uint);
orig_bg_alpha = bg_alpha;
#if (PHASE == PHASE_BACKGROUND)
// draw_bg_bitfield has bit 0 set to draw default bg cells and bit 1 set to draw non-default bg cells
float cell_has_non_default_bg = step(1.f, abs(float(bg_as_uint - bg_colors0))); // 0 if has default bg else 1
uint draw_bg_mask = uint(2.f * cell_has_non_default_bg + (1.f - cell_has_non_default_bg)); // 1 if has default bg else 2
draw_bg = step(0.5, float(draw_bg_bitfield & draw_bg_mask));
#else
draw_bg = 1;
#endif
float is_special_cell = cell_data.has_block_cursor + float(is_selected & ONE);
#if PHASE == PHASE_SPECIAL
// Only special cells must be drawn and they must have bg_alpha 1
bg_alpha = step(0.5, is_special_cell); // bg_alpha = 1 if is_special_cell else 0
#else
is_special_cell += float(is_reversed); // bg_alpha should be 1 for reverse video cells as well
is_special_cell = step(0.5, is_special_cell); // is_special_cell = 1 if is_special_cell else 0
bg_alpha = bg_alpha * (1. - float(is_special_cell)) + is_special_cell; // bg_alpha = 1 if is_special_cell else bg_alpha
#endif
bg_alpha *= draw_bg;
#endif // ends fast case #if else
// Selection and cursor
bg = choose_color(float(is_selected & ONE), choose_color(use_cell_for_selection_bg, color_to_vec(fg_as_uint), color_to_vec(highlight_bg)), bg);
background = choose_color(cell_data.has_block_cursor, mix(bg, color_to_vec(cursor_bg), cursor_opacity), bg);
// we use max so that opacity of the block cursor cell background goes from orig_bg_alpha to 1
float effective_cursor_opacity = max(cursor_opacity, orig_bg_alpha) * draw_bg;
float effective_cursor_opacity = max(cursor_opacity, orig_bg_alpha);
bg_alpha = choose_alpha(cell_data.has_block_cursor, effective_cursor_opacity, bg_alpha);
// }}}

View file

@ -270,10 +270,7 @@ NO_CURSOR_SHAPE: int
CURSOR_UNDERLINE: int
DECAWM: int
BGIMAGE_PROGRAM: int
CELL_BG_PROGRAM: int
CELL_FG_PROGRAM: int
CELL_PROGRAM: int
CELL_SPECIAL_PROGRAM: int
DECORATION: int
DIM: int
GRAPHICS_ALPHA_MASK_PROGRAM: int

View file

@ -14,11 +14,25 @@
#include "srgb_gamma.h"
#include "uniforms_generated.h"
/*
* TODO: for shader refactoring
* Get rid of TRANSPARENT define and always use premul colors
* Change rendering of cursor -- should now be rendered with full blending as a layer between bg and fg layers
* Check that rendering of special cells aka selections still works
* Port graphics rendering to start use a dummy 1 pixel empty texture then possibly replace with defines so that the most
* common use case of no graphics has zero performance overhead.
* Convert all images loaded to GPU to linear space for correct blending or alternately do conversion to linear space in
* the new graphics shader.
* background image with tint and various layout options
* window logo image that is in the under foreground layer I think? need to check
* test that window numbering and URL hover rendering both still work
* test cursor trail and scroll indicator rendering
*/
#define BLEND_ONTO_OPAQUE glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // blending onto opaque colors
#define BLEND_ONTO_OPAQUE_WITH_OPAQUE_OUTPUT glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE); // blending onto opaque colors with final color having alpha 1
#define BLEND_PREMULT glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); // blending of pre-multiplied colors
enum { CELL_PROGRAM, CELL_BG_PROGRAM, CELL_SPECIAL_PROGRAM, CELL_FG_PROGRAM, BORDERS_PROGRAM, GRAPHICS_PROGRAM, GRAPHICS_PREMULT_PROGRAM, GRAPHICS_ALPHA_MASK_PROGRAM, BGIMAGE_PROGRAM, TINT_PROGRAM, TRAIL_PROGRAM, NUM_PROGRAMS };
enum { CELL_PROGRAM, BORDERS_PROGRAM, GRAPHICS_PROGRAM, GRAPHICS_PREMULT_PROGRAM, GRAPHICS_ALPHA_MASK_PROGRAM, BGIMAGE_PROGRAM, TINT_PROGRAM, TRAIL_PROGRAM, NUM_PROGRAMS };
enum { SPRITE_MAP_UNIT, GRAPHICS_UNIT, BGIMAGE_UNIT, SPRITE_DECORATIONS_MAP_UNIT };
// Sprites {{{
@ -684,19 +698,6 @@ has_bgimage(OSWindow *w) {
return w->bgimage && w->bgimage->texture_id > 0;
}
static void
draw_tint(bool premult, Screen *screen, const CellRenderData *crd) {
if (premult) { BLEND_PREMULT } else { BLEND_ONTO_OPAQUE_WITH_OPAQUE_OUTPUT }
bind_program(TINT_PROGRAM);
color_type window_bg = colorprofile_to_color(screen->color_profile, screen->color_profile->overridden.default_bg, screen->color_profile->configured.default_bg).rgb;
#define C(shift) srgb_color((window_bg >> shift) & 0xFF) * premult_factor
GLfloat premult_factor = premult ? OPT(background_tint) : 1.0f;
glUniform4f(tint_program_layout.uniforms.tint_color, C(16), C(8), C(0), OPT(background_tint));
#undef C
glUniform4f(tint_program_layout.uniforms.edges, crd->gl.xstart, crd->gl.ystart - crd->gl.height, crd->gl.xstart + crd->gl.width, crd->gl.ystart);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
static bool
draw_scroll_indicator(bool premult, Screen *screen, const CellRenderData *crd) {
if (OPT(scrollback_indicator_opacity) <= 0 || screen->linebuf != screen->main_linebuf || !screen->scrolled_by) return false;
@ -734,17 +735,13 @@ set_cell_uniforms(float current_inactive_text_alpha, bool force) {
for (int i = GRAPHICS_PROGRAM; i <= GRAPHICS_PREMULT_PROGRAM; i++) {
bind_program(i); glUniform1i(graphics_program_layouts[i].uniforms.image, GRAPHICS_UNIT);
}
for (int i = CELL_PROGRAM; i <= CELL_FG_PROGRAM; i++) {
for (int i = CELL_PROGRAM; i <= CELL_PROGRAM; i++) {
bind_program(i); const CellUniforms *cu = &cell_program_layouts[i].uniforms;
switch(i) {
case CELL_PROGRAM: case CELL_FG_PROGRAM:
glUniform1i(cu->sprites, SPRITE_MAP_UNIT);
glUniform1i(cu->sprite_decorations_map, SPRITE_DECORATIONS_MAP_UNIT);
glUniform1f(cu->dim_opacity, OPT(dim_opacity));
glUniform1f(cu->text_contrast, text_contrast);
glUniform1f(cu->text_gamma_adjustment, text_gamma_adjustment);
break;
}
glUniform1i(cu->sprites, SPRITE_MAP_UNIT);
glUniform1i(cu->sprite_decorations_map, SPRITE_DECORATIONS_MAP_UNIT);
glUniform1f(cu->dim_opacity, OPT(dim_opacity));
glUniform1f(cu->text_contrast, text_contrast);
glUniform1f(cu->text_gamma_adjustment, text_gamma_adjustment);
}
constants_set = true;
}
@ -754,7 +751,7 @@ set_cell_uniforms(float current_inactive_text_alpha, bool force) {
bind_program(i); glUniform1f(graphics_program_layouts[i].uniforms.inactive_text_alpha, current_inactive_text_alpha);
}
#define S(prog, loc) bind_program(prog); glUniform1f(cell_program_layouts[prog].uniforms.inactive_text_alpha, current_inactive_text_alpha);
S(CELL_PROGRAM, cploc); S(CELL_FG_PROGRAM, cfploc);
S(CELL_PROGRAM, cploc);
#undef S
}
}
@ -830,60 +827,6 @@ draw_hyperlink_target(OSWindow *os_window, Screen *screen, const CellRenderData
Py_DECREF(ref);
}
static void
draw_window_logo(ssize_t vao_idx, OSWindow *os_window, const WindowLogoRenderData *wl, const CellRenderData *crd) {
if (os_window->live_resize.in_progress) return;
BLEND_PREMULT;
GLfloat logo_width_gl = gl_size(wl->instance->width, os_window->viewport_width);
GLfloat logo_height_gl = gl_size(wl->instance->height, os_window->viewport_height);
if (OPT(window_logo_scale.width) > 0 || OPT(window_logo_scale.height) > 0) {
unsigned int scaled_wl_width = os_window->viewport_width;
unsigned int scaled_wl_height = os_window->viewport_height;
// [sx] Scales logo to sx % of the viewports shortest dimension, preserving aspect ratio
if (OPT(window_logo_scale.height) < 0) {
if (os_window->viewport_height < os_window->viewport_width) {
scaled_wl_height = (int)(os_window->viewport_height * OPT(window_logo_scale.width) / 100);
scaled_wl_width = wl->instance->width * scaled_wl_height / wl->instance->height;
} else {
scaled_wl_width = (int)(os_window->viewport_width * OPT(window_logo_scale.width) / 100);
scaled_wl_height = wl->instance->height * scaled_wl_width / wl->instance->width;
}
}
// [0 sy] Scales logo's y dimension to sy % of viewporty keeping original x dimension
else if (OPT(window_logo_scale.width) == 0.0) {
scaled_wl_height = (int)(scaled_wl_height * OPT(window_logo_scale.height) / 100);
scaled_wl_width = wl->instance->width;
}
// [sx 0] Scales logo's x dimension to sx % of viewportx keeping original y dimension
else if (OPT(window_logo_scale.height) == 0.0) {
scaled_wl_width = (int)(scaled_wl_width * OPT(window_logo_scale.width) / 100);
scaled_wl_height = wl->instance->height;
}
// [sx sy] Scales logo's x and y dimension to sx and sy % of viewportx and viewporty respectively
else {
scaled_wl_height = (int)(scaled_wl_height * OPT(window_logo_scale.height) / 100);
scaled_wl_width = (int)(scaled_wl_width * OPT(window_logo_scale.width) / 100);
}
logo_height_gl = gl_size(scaled_wl_height, os_window->viewport_height);
logo_width_gl = gl_size(scaled_wl_width, os_window->viewport_width);
}
GLfloat logo_left_gl = clamp_position_to_nearest_pixel(
crd->gl.xstart + crd->gl.width * wl->position.canvas_x - logo_width_gl * wl->position.image_x, os_window->viewport_width);
GLfloat logo_top_gl = clamp_position_to_nearest_pixel(
crd->gl.ystart - crd->gl.height * wl->position.canvas_y + logo_height_gl * wl->position.image_y, os_window->viewport_height);
static ImageRenderData ird = {.group_count=1};
ird.texture_id = wl->instance->texture_id;
gpu_data_for_image(&ird, logo_left_gl, logo_top_gl, logo_left_gl + logo_width_gl, logo_top_gl - logo_height_gl);
bind_program(GRAPHICS_PREMULT_PROGRAM);
glUniform1f(graphics_program_layouts[GRAPHICS_PREMULT_PROGRAM].uniforms.inactive_text_alpha, prev_inactive_text_alpha * wl->alpha);
draw_graphics(GRAPHICS_PREMULT_PROGRAM, vao_idx, &ird, 0, 1, viewport_for_cells(crd));
glUniform1f(graphics_program_layouts[GRAPHICS_PREMULT_PROGRAM].uniforms.inactive_text_alpha, prev_inactive_text_alpha);
}
static void
draw_window_number(OSWindow *os_window, Screen *screen, const CellRenderData *crd, Window *window) {
GLfloat left = os_window->viewport_width * (crd->gl.xstart + 1.f) / 2.f;
@ -957,97 +900,6 @@ draw_visual_bell_flash(GLfloat intensity, const CellRenderData *crd, Screen *scr
glDisable(GL_BLEND);
}
static void
draw_cells_interleaved(ssize_t vao_idx, Screen *screen, OSWindow *w, const CellRenderData *crd, GraphicsRenderData grd, const WindowLogoRenderData *wl) {
glEnable(GL_BLEND);
BLEND_ONTO_OPAQUE;
// draw background for all cells
if (!has_bgimage(w)) {
bind_program(CELL_BG_PROGRAM);
glUniform1ui(cell_program_layouts[CELL_BG_PROGRAM].uniforms.draw_bg_bitfield, 3);
glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, screen->lines * screen->columns);
} else if (OPT(background_tint) > 0) {
draw_tint(false, screen, crd);
BLEND_ONTO_OPAQUE;
}
if (grd.num_of_below_refs || has_bgimage(w) || wl) {
if (wl) {
draw_window_logo(vao_idx, w, wl, crd);
BLEND_ONTO_OPAQUE;
}
if (grd.num_of_below_refs) draw_graphics(
GRAPHICS_PROGRAM, vao_idx, grd.images, 0, grd.num_of_below_refs, viewport_for_cells(crd));
bind_program(CELL_BG_PROGRAM);
// draw background for non-default bg cells
glUniform1ui(cell_program_layouts[CELL_BG_PROGRAM].uniforms.draw_bg_bitfield, 2);
glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, screen->lines * screen->columns);
}
if (grd.num_of_negative_refs) draw_graphics(GRAPHICS_PROGRAM, vao_idx, grd.images, grd.num_of_below_refs, grd.num_of_negative_refs, viewport_for_cells(crd));
bind_program(CELL_SPECIAL_PROGRAM);
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 (grd.num_of_positive_refs) draw_graphics(GRAPHICS_PROGRAM, vao_idx, grd.images, grd.num_of_negative_refs + grd.num_of_below_refs, grd.num_of_positive_refs, viewport_for_cells(crd));
glDisable(GL_BLEND);
}
static void
draw_cells_interleaved_premult(ssize_t vao_idx, Screen *screen, OSWindow *os_window, const CellRenderData *crd, GraphicsRenderData grd, const WindowLogoRenderData *wl) {
if (OPT(background_tint) > 0.f) {
glEnable(GL_BLEND);
draw_tint(true, screen, crd);
glDisable(GL_BLEND);
}
bind_program(CELL_BG_PROGRAM);
if (!has_bgimage(os_window)) {
// draw background for all cells
glUniform1ui(cell_program_layouts[CELL_BG_PROGRAM].uniforms.draw_bg_bitfield, 3);
glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, screen->lines * screen->columns);
}
glEnable(GL_BLEND);
BLEND_PREMULT;
if (grd.num_of_below_refs || has_bgimage(os_window) || wl) {
if (wl) {
draw_window_logo(vao_idx, os_window, wl, crd);
BLEND_PREMULT;
}
if (grd.num_of_below_refs) draw_graphics(
GRAPHICS_PREMULT_PROGRAM, vao_idx, grd.images, 0, grd.num_of_below_refs, viewport_for_cells(crd));
bind_program(CELL_BG_PROGRAM);
// Draw background for non-default bg cells
glUniform1ui(cell_program_layouts[CELL_BG_PROGRAM].uniforms.draw_bg_bitfield, 2);
glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, screen->lines * screen->columns);
} else {
// Apply background_opacity
glUniform1ui(cell_program_layouts[CELL_BG_PROGRAM].uniforms.draw_bg_bitfield, 0);
glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, screen->lines * screen->columns);
}
if (grd.num_of_negative_refs) {
draw_graphics(GRAPHICS_PREMULT_PROGRAM, vao_idx, grd.images, grd.num_of_below_refs, grd.num_of_negative_refs, viewport_for_cells(crd));
}
bind_program(CELL_SPECIAL_PROGRAM);
glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, screen->lines * screen->columns);
bind_program(CELL_FG_PROGRAM);
glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, screen->lines * screen->columns);
if (grd.num_of_positive_refs) draw_graphics(GRAPHICS_PREMULT_PROGRAM, vao_idx, grd.images, grd.num_of_negative_refs + grd.num_of_below_refs, grd.num_of_positive_refs, viewport_for_cells(crd));
glDisable(GL_BLEND);
}
void
blank_canvas(float background_opacity, color_type color) {
// See https://github.com/glfw/glfw/issues/1538 for why we use pre-multiplied alpha
@ -1133,13 +985,8 @@ draw_cells(ssize_t vao_idx, const WindowRenderData *srd, OSWindow *os_window, bo
}
}
has_underlying_image |= grd.num_of_below_refs > 0 || grd.num_of_negative_refs > 0;
if (os_window->is_semi_transparent) {
if (has_underlying_image) { draw_cells_interleaved_premult(vao_idx, screen, os_window, &crd, grd, wl); }
else draw_cells_simple(vao_idx, screen, &crd, grd, os_window->is_semi_transparent);
} else {
if (has_underlying_image) draw_cells_interleaved(vao_idx, screen, os_window, &crd, grd, wl);
else draw_cells_simple(vao_idx, screen, &crd, grd, os_window->is_semi_transparent);
}
(void)has_underlying_image;
draw_cells_simple(vao_idx, screen, &crd, grd, os_window->is_semi_transparent);
draw_scroll_indicator(os_window->is_semi_transparent, screen, &crd);
if (screen->start_visual_bell_at) {
@ -1377,7 +1224,7 @@ finalize(void) {
bool
init_shaders(PyObject *module) {
#define C(x) if (PyModule_AddIntConstant(module, #x, x) != 0) { PyErr_NoMemory(); return false; }
C(CELL_PROGRAM); C(CELL_BG_PROGRAM); C(CELL_SPECIAL_PROGRAM); C(CELL_FG_PROGRAM); C(BORDERS_PROGRAM); C(GRAPHICS_PROGRAM); C(GRAPHICS_PREMULT_PROGRAM); C(GRAPHICS_ALPHA_MASK_PROGRAM); C(BGIMAGE_PROGRAM); C(TINT_PROGRAM); C(TRAIL_PROGRAM);
C(CELL_PROGRAM); C(BORDERS_PROGRAM); C(GRAPHICS_PROGRAM); C(GRAPHICS_PREMULT_PROGRAM); C(GRAPHICS_ALPHA_MASK_PROGRAM); C(BGIMAGE_PROGRAM); C(TINT_PROGRAM); C(TRAIL_PROGRAM);
C(GLSL_VERSION);
C(GL_VERSION);
C(GL_VENDOR);

View file

@ -10,10 +10,7 @@ from typing import Any, Literal, NamedTuple, Optional
from .constants import read_kitty_resource
from .fast_data_types import (
BGIMAGE_PROGRAM,
CELL_BG_PROGRAM,
CELL_FG_PROGRAM,
CELL_PROGRAM,
CELL_SPECIAL_PROGRAM,
DECORATION,
DECORATION_MASK,
DIM,
@ -180,28 +177,16 @@ class LoadShaderPrograms:
DECORATION_MASK=DECORATION_MASK,
)
def resolve_cell_defines(which: str, src: str) -> str:
def resolve_cell_defines(src: str) -> str:
r = self.cell_program_replacer.replacements
r['WHICH_PHASE'] = f'PHASE_{which}'
r['TRANSPARENT'] = '1' if semi_transparent else '0'
r['DO_FG_OVERRIDE'] = '1' if self.text_fg_override_threshold.scaled_value else '0'
r['FG_OVERRIDE_ALGO'] = '1' if self.text_fg_override_threshold.unit == '%' else '2'
r['FG_OVERRIDE_THRESHOLD'] = str(self.text_fg_override_threshold.scaled_value)
r['TEXT_NEW_GAMMA'] = '0' if self.text_old_gamma else '1'
return self.cell_program_replacer(src)
for which, p in {
'BOTH': CELL_PROGRAM,
'BACKGROUND': CELL_BG_PROGRAM,
'SPECIAL': CELL_SPECIAL_PROGRAM,
'FOREGROUND': CELL_FG_PROGRAM,
}.items():
cell.apply_to_sources(
vertex=partial(resolve_cell_defines, which),
frag=partial(resolve_cell_defines, which),
)
cell.compile(p, allow_recompile)
cell.apply_to_sources(vertex=resolve_cell_defines, frag=resolve_cell_defines)
cell.compile(CELL_PROGRAM, allow_recompile)
graphics = program_for('graphics')
def resolve_graphics_fragment_defines(which: str, f: str) -> str: