Skip to content

Commit b2f543f

Browse files
committed
Refactor cell shader to take cursor shape as input
Less code for one const array lookup per vertex. And will allow implementation of multiple cursors more easily.
1 parent 8f5dc42 commit b2f543f

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

kitty/cell_vertex.glsl

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ layout(std140) uniform CellRenderData {
99

1010
uint default_fg, highlight_fg, highlight_bg, cursor_fg, cursor_bg, url_color, url_style, inverted;
1111

12-
uint columns, lines, sprites_xnum, sprites_ynum, cursor_fg_sprite_idx, cell_width, cell_height;
12+
uint columns, lines, sprites_xnum, sprites_ynum, cursor_shape, cell_width, cell_height;
1313
uint cursor_x1, cursor_x2, cursor_y1, cursor_y2;
1414
float cursor_opacity, inactive_text_alpha, dim_opacity;
1515

@@ -26,15 +26,22 @@ uniform usampler2D sprite_decorations_map;
2626
layout(location=0) in uvec3 colors;
2727
layout(location=1) in uvec2 sprite_idx;
2828
layout(location=2) in uint is_selected;
29+
// }}}
2930

3031
const int fg_index_map[] = int[3](0, 1, 0);
3132
const uvec2 cell_pos_map[] = uvec2[4](
32-
uvec2(1, 0), // right, top
33-
uvec2(1, 1), // right, bottom
34-
uvec2(0, 1), // left, bottom
35-
uvec2(0, 0) // left, top
33+
uvec2(1u, 0u), // right, top
34+
uvec2(1u, 1u), // right, bottom
35+
uvec2(0u, 1u), // left, bottom
36+
uvec2(0u, 0u) // left, top
37+
);
38+
const uint cursor_shape_map[] = uint[5]( // maps cursor shape to foreground sprite index
39+
0u, // NO_CURSOR
40+
0u, // BLOCK (this is rendered as background)
41+
2u, // BEAM
42+
3u, // UNDERLINE
43+
4u // UNFOCUSED
3644
);
37-
// }}}
3845

3946

4047
out vec3 background;
@@ -71,13 +78,7 @@ vec3 color_to_vec(uint c) {
7178
return vec3(gamma_lut[r], gamma_lut[g], gamma_lut[b]);
7279
}
7380

74-
float one_if_equal_zero_otherwise(int a, int b) {
75-
return 1.0f - clamp(abs(float(a) - float(b)), 0.0f, 1.0f);
76-
}
77-
78-
float one_if_equal_zero_otherwise(uint a, uint b) {
79-
return 1.0f - clamp(abs(float(a) - float(b)), 0.0f, 1.0f);
80-
}
81+
#define one_if_equal_zero_otherwise(a, b) (1.0f - zero_or_one(abs(float(a) - float(b))))
8182

8283

8384
uint resolve_color(uint c, uint defval) {
@@ -147,6 +148,7 @@ float is_cursor(uint x, uint y) {
147148
struct CellData {
148149
float has_cursor, has_block_cursor;
149150
uvec2 pos;
151+
uint cursor_fg_sprite_idx;
150152
} cell_data;
151153

152154
CellData set_vertex_position() {
@@ -166,9 +168,9 @@ CellData set_vertex_position() {
166168
sprite_pos = to_sprite_pos(pos, sprite_idx[0] & SPRITE_INDEX_MASK);
167169
colored_sprite = float((sprite_idx[0] & SPRITE_COLORED_MASK) >> SPRITE_COLORED_SHIFT);
168170
#endif
169-
float is_block_cursor = step(float(cursor_fg_sprite_idx), 0.5);
171+
float is_block_cursor = one_if_equal_zero_otherwise(cursor_shape, 1u);
170172
float has_cursor = is_cursor(column, row);
171-
return CellData(has_cursor, has_cursor * is_block_cursor, pos);
173+
return CellData(has_cursor, has_cursor * is_block_cursor, pos, cursor_shape_map[cursor_shape]);
172174
}
173175

174176
float background_opacity_for(uint bg, uint colorval, float opacity_if_matched) { // opacity_if_matched if bg == colorval else 1
@@ -277,7 +279,7 @@ void main() {
277279
vec3 final_cursor_text_color = mix(foreground, color_to_vec(cursor_fg), cursor_opacity);
278280
foreground = if_one_then(cell_data.has_block_cursor, final_cursor_text_color, foreground);
279281
decoration_fg = if_one_then(cell_data.has_block_cursor, final_cursor_text_color, decoration_fg);
280-
cursor_pos = to_sprite_pos(cell_data.pos, cursor_fg_sprite_idx * uint(cell_data.has_cursor));
282+
cursor_pos = to_sprite_pos(cell_data.pos, cell_data.cursor_fg_sprite_idx * uint(cell_data.has_cursor));
281283
#endif
282284
// }}}
283285

kitty/shaders.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ cell_update_uniform_block(ssize_t vao_idx, Screen *screen, int uniform_buffer, C
440440

441441
GLuint default_fg, highlight_fg, highlight_bg, cursor_fg, cursor_bg, url_color, url_style, inverted;
442442

443-
GLuint columns, lines, sprites_xnum, sprites_ynum, cursor_fg_sprite_idx, cell_width, cell_height;
443+
GLuint columns, lines, sprites_xnum, sprites_ynum, cursor_shape, cell_width, cell_height;
444444
GLuint cursor_x1, cursor_x2, cursor_y1, cursor_y2;
445445
GLfloat cursor_opacity, inactive_text_alpha, dim_opacity;
446446

@@ -474,23 +474,13 @@ cell_update_uniform_block(ssize_t vao_idx, Screen *screen, int uniform_buffer, C
474474
}
475475
rd->use_cell_for_selection_bg = IS_SPECIAL_COLOR(highlight_bg) ? 1. : 0.;
476476
// Cursor position
477-
enum { BLOCK_IDX = 0, BEAM_IDX = 2, UNDERLINE_IDX = 3, UNFOCUSED_IDX = 4 };
478477
Line *line_for_cursor = NULL;
479-
if (cursor->opacity > 0) {
478+
rd->cursor_opacity = MAX(0, MIN(cursor->opacity, 1));
479+
if (rd->cursor_opacity != 0) {
480480
rd->cursor_x1 = cursor->x, rd->cursor_y1 = cursor->y;
481481
rd->cursor_x2 = cursor->x, rd->cursor_y2 = cursor->y;
482-
rd->cursor_opacity = cursor->opacity;
483482
CursorShape cs = (cursor->is_focused || OPT(cursor_shape_unfocused) == NO_CURSOR_SHAPE) ? cursor->shape : OPT(cursor_shape_unfocused);
484-
switch(cs) {
485-
case CURSOR_BEAM:
486-
rd->cursor_fg_sprite_idx = BEAM_IDX; break;
487-
case CURSOR_UNDERLINE:
488-
rd->cursor_fg_sprite_idx = UNDERLINE_IDX; break;
489-
case CURSOR_BLOCK: case NUM_OF_CURSOR_SHAPES: case NO_CURSOR_SHAPE:
490-
rd->cursor_fg_sprite_idx = BLOCK_IDX; break;
491-
case CURSOR_HOLLOW:
492-
rd->cursor_fg_sprite_idx = UNFOCUSED_IDX; break;
493-
};
483+
rd->cursor_shape = cs;
494484
color_type cell_fg = rd->default_fg, cell_bg = rd->bg_colors0;
495485
index_type cell_color_x = cursor->x;
496486
bool reversed = false;
@@ -536,6 +526,7 @@ cell_update_uniform_block(ssize_t vao_idx, Screen *screen, int uniform_buffer, C
536526
// store last rendered cursor color for trail rendering
537527
screen->last_rendered.cursor_bg = rd->cursor_bg;
538528
} else {
529+
rd->cursor_shape = 0;
539530
rd->cursor_x1 = screen->columns + 1; rd->cursor_x2 = screen->columns;
540531
rd->cursor_y1 = screen->lines + 1; rd->cursor_y2 = screen->lines;
541532
}

0 commit comments

Comments
 (0)