Cleanup previous PR

- Use SampleLevel() for the sprite samples in the cell fragment shader.
  They are now inside a branch that is not uniform across a 2x2 pixel
  quad at cell boundaries, where implicit derivatives are undefined. The
  sprites texture has no mipmaps so LOD 0 is identical.
- Pack the skip text flag into the existing flat underline_exclusion_pos
  varying instead of adding another one, to not reduce varying headroom
  on older drivers that do not pack flat ints with floats.
- Make the requirement that callers rewrite every byte of an invalidated
  buffer explicit with a dedicated alloc_and_map_vao_buffer_for_full_rewrite()
  and drop the now unused frequently_updated parameter.
- Cross reference text_composition_strategy() in C and
  text_contrast_is_identity() in Python so they are kept in sync.
This commit is contained in:
Kovid Goyal 2026-10-12 09:57:24 +05:30
parent 0931296a15
commit 085a2e4010
6 changed files with 39 additions and 25 deletions

View file

@ -691,13 +691,21 @@ map_vao_buffer_for_write_only(ssize_t vao_idx, size_t bufnum, int offset, unsign
}
void *
alloc_and_map_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum, bool frequently_updated) {
ssize_t buf_idx = alloc_vao_buffer(vao_idx, size, bufnum, frequently_updated ? GL_STREAM_DRAW : GL_STATIC_DRAW);
// The whole buffer is discarded. Callers rewrite every byte.
if (frequently_updated) return map_buffer_range(buf_idx, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT, 0, (unsigned)size);
alloc_and_map_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum) {
ssize_t buf_idx = alloc_vao_buffer(vao_idx, size, bufnum, GL_STATIC_DRAW);
return map_buffer(buf_idx, GL_WRITE_ONLY);
}
void *
alloc_and_map_vao_buffer_for_full_rewrite(ssize_t vao_idx, GLsizeiptr size, size_t bufnum) {
// The previous contents are discarded so the driver can hand out fresh
// storage instead of waiting for the GPU to finish reading the old data.
// The caller MUST write every byte of the mapped range, any byte not
// written is undefined.
ssize_t buf_idx = alloc_vao_buffer(vao_idx, size, bufnum, GL_STREAM_DRAW);
return map_buffer_range(buf_idx, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT, 0, (unsigned)size);
}
void
bind_vao_uniform_buffer(ssize_t vao_idx, size_t bufnum, GLuint block_index) {
ssize_t buf_idx = vaos[vao_idx].buffers[bufnum];

View file

@ -76,7 +76,9 @@ size_t add_buffer_to_vao(ssize_t vao_idx, GLenum usage);
void add_attribute_to_vao(ssize_t vao_idx, int location, GLint size, GLenum data_type, GLsizei stride, void *offset, GLuint divisor);
void set_vao_attribute(ssize_t vao_idx, size_t buffer_idx, int location, GLint size, GLenum data_type, GLsizei stride, void *offset, GLuint divisor);
ssize_t alloc_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum, GLenum usage);
void *alloc_and_map_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum, bool frequently_updated);
void *alloc_and_map_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum);
// Discards the old contents, the caller must write every byte of the buffer
void *alloc_and_map_vao_buffer_for_full_rewrite(ssize_t vao_idx, GLsizeiptr size, size_t bufnum);
void unmap_vao_buffer(ssize_t vao_idx, size_t bufnum);
void *map_vao_buffer(ssize_t vao_idx, size_t bufnum, GLenum access);
void *map_vao_buffer_for_write_only(ssize_t vao_idx, size_t bufnum, int offset, unsigned size);

View file

@ -625,6 +625,8 @@ remap_modifiers(PyObject *val, Options *opts) {
}
}
// Keep in sync with text_contrast_is_identity() in kitty/shaders/slang.py,
// which selects a cell shader specialized for gamma 1 and contrast 0.
static inline void
text_composition_strategy(PyObject *val, Options *opts) {
if (!PyUnicode_Check(val)) {

View file

@ -4109,6 +4109,8 @@ screen_update_cell_data(Screen *self, void *address, FONTS_DATA_HANDLE fonts_dat
if (self->paused_rendering.expires_at) {
if (!self->paused_rendering.cell_data_updated) {
LineBuf *linebuf = self->paused_rendering.linebuf;
// The GPU buffer was invalidated so every row must be written. Pixel
// scrolling is off while paused, so lines == render_lines_for_screen().
for (index_type y = 0; y < self->lines; y++) {
linebuf_init_line(linebuf, y);
if (linebuf->line->attrs.has_dirty_text) {

View file

@ -1141,7 +1141,7 @@ cell_prepare_to_render(ssize_t vao_idx, Screen *screen, FONTS_DATA_HANDLE fonts_
{ \
const unsigned int render_lines = render_lines_for_screen(screen); \
sz = sizeof(GPUCell) * render_lines * screen->columns; \
address = alloc_and_map_vao_buffer(vao_idx, sz, cell_data_buffer, true); \
address = alloc_and_map_vao_buffer_for_full_rewrite(vao_idx, sz, cell_data_buffer); \
screen_update_cell_data(screen, address, fonts_data, disable_ligatures && cursor_pos_changed); \
unmap_vao_buffer(vao_idx, cell_data_buffer); \
address = NULL; \
@ -1157,7 +1157,7 @@ cell_prepare_to_render(ssize_t vao_idx, Screen *screen, FONTS_DATA_HANDLE fonts_
{ \
const unsigned int render_lines = render_lines_for_screen(screen); \
sz = (size_t)render_lines * screen->columns; \
address = alloc_and_map_vao_buffer(vao_idx, sz, selection_buffer, true); \
address = alloc_and_map_vao_buffer_for_full_rewrite(vao_idx, sz, selection_buffer); \
screen_apply_selection(screen, address, sz); \
unmap_vao_buffer(vao_idx, selection_buffer); \
address = NULL; \
@ -2122,11 +2122,11 @@ draw_window_padding(const UIRenderData *ui, unsigned int cl, unsigned int ct, un
}
unmap_vao_buffer(vao_idx, selection_buffer);
void *dst = alloc_and_map_vao_buffer(vao_idx, (GLsizeiptr)(sizeof(GPUCell) * nV * lines), padding_cell_data_buffer, false);
void *dst = alloc_and_map_vao_buffer(vao_idx, (GLsizeiptr)(sizeof(GPUCell) * nV * lines), padding_cell_data_buffer);
memcpy(dst, gathered_cells, sizeof(GPUCell) * nV * lines);
unmap_vao_buffer(vao_idx, padding_cell_data_buffer);
dst = alloc_and_map_vao_buffer(vao_idx, (GLsizeiptr)(sizeof(GLubyte) * nV * lines), padding_selection_buffer, false);
dst = alloc_and_map_vao_buffer(vao_idx, (GLsizeiptr)(sizeof(GLubyte) * nV * lines), padding_selection_buffer);
memcpy(dst, gathered_sel, sizeof(GLubyte) * nV * lines);
unmap_vao_buffer(vao_idx, padding_selection_buffer);
@ -2350,7 +2350,7 @@ draw_borders(
if (has_background_image) background_opacity = OPT(background_tint) * OPT(background_tint_gaps);
if (rect_data_is_dirty) {
const size_t sz = sizeof(BorderRect) * num_border_rects;
void *borders_buf_address = alloc_and_map_vao_buffer(vao_idx, sz, 0, false);
void *borders_buf_address = alloc_and_map_vao_buffer(vao_idx, sz, 0);
if (borders_buf_address) memcpy(borders_buf_address, rect_buf, sz);
unmap_vao_buffer(vao_idx, 0);
}

View file

@ -28,6 +28,10 @@ uniform Sampler2D<uint4> sprite_decorations_map;
static const uint SPRITE_INDEX_MASK = 0x7fffffff;
static const uint SPRITE_COLORED_MASK = 0x80000000;
static const uint SPRITE_COLORED_SHIFT = 31u;
// Set in underline_exclusion_pos when the glyph, decorations, and cursor sprite
// are all 0. The block cursor is sprite 0 and its color is already in the
// background. Packed into an existing flat varying to not use another slot.
static const uint SKIP_TEXT_FLAG = 0x80000000u;
struct VertexOutput {
float4 position : SV_Position;
@ -39,10 +43,7 @@ struct VertexOutput {
float3 underline_pos;
float3 cursor_pos;
float3 strike_pos;
nointerpolation uint underline_exclusion_pos;
// 1 when the glyph, decorations, and cursor sprite are all 0. The block
// cursor is sprite 0 and its color is already in the background.
nointerpolation uint skip_text;
nointerpolation uint underline_exclusion_pos; // has SKIP_TEXT_FLAG set for blank cells
float3 cell_foreground;
float4 cursor_color_premult;
float3 decoration_fg;
@ -157,7 +158,6 @@ vertex_main(
uint fg_as_uint = bo.fg_as_uint;
uint text_attrs = bo.text_attrs;
CellData cell_data = bo.cell_data;
uint skip_text = 1u;
// Foreground {{{
if (!ONLY_BACKGROUND) { // background does not depend on foreground
@ -182,7 +182,6 @@ vertex_main(
uint2 decs = get_decorations_indices(sprite_idx, uint(in_url), text_attrs);
vo.strike_pos = to_sprite_pos(cell_data.pos, decs[0]);
vo.underline_pos = to_sprite_pos(cell_data.pos, decs[1]);
vo.underline_exclusion_pos = to_underline_exclusion_pos(sprite_idx);
// Cursor
vo.cursor_color_premult = float4(cell_data.cursor.bg * crd.cursor_opacity, crd.cursor_opacity);
@ -191,12 +190,10 @@ vertex_main(
vo.decoration_fg = if_one_then(cell_data.has_block_cursor, final_cursor_text_color, vo.decoration_fg);
uint cursor_sprite = cell_data.cursor_fg_sprite_idx * uint(cell_data.has_cursor);
vo.cursor_pos = to_sprite_pos(cell_data.pos, cursor_sprite);
uint glyph = sprite_idx[0] & SPRITE_INDEX_MASK;
if (glyph == 0u && decs[0] == 0u && decs[1] == 0u && cursor_sprite == 0u) skip_text = 1u;
else skip_text = 0u;
uint skip_text = uint((sprite_idx[0] & SPRITE_INDEX_MASK) == 0u && decs[0] == 0u && decs[1] == 0u && cursor_sprite == 0u);
vo.underline_exclusion_pos = to_underline_exclusion_pos(sprite_idx) | (skip_text * SKIP_TEXT_FLAG);
}
// }}}
vo.skip_text = skip_text;
vo.background = background_rgb;
@ -267,7 +264,7 @@ float4
load_text_foreground_color(float3 sprite_pos, float colored_sprite, float3 cell_foreground) {
// For colored sprites use the color from the sprite rather than the text foreground
// Return non-premultiplied foreground color
float4 text_fg = sprites.Sample(sprite_pos);
float4 text_fg = sprites.SampleLevel(sprite_pos, 0);
return float4(lerp(cell_foreground, text_fg.xyz, colored_sprite), text_fg.w);
}
@ -287,13 +284,13 @@ calculate_premul_foreground_from_sprites(
sprites.GetDimensions(width, height, layer);
float3 sz = float3(float(width), float(height), float(layer));
float underline_alpha = sprites.Sample(underline_pos).w;
float underline_alpha = sprites.SampleLevel(underline_pos, 0).w;
int3 fetch_coord = int3(int(sprite_pos.x * sz.x), int(underline_exclusion_pos), int(sprite_pos.z));
float underline_exclusion = sprites[fetch_coord].w;
underline_alpha *= 1.0 - underline_exclusion;
float strike_alpha = sprites.Sample(strike_pos).w;
float cursor_alpha = sprites.Sample(cursor_pos).w;
float strike_alpha = sprites.SampleLevel(strike_pos, 0).w;
float cursor_alpha = sprites.SampleLevel(cursor_pos, 0).w;
float combined_alpha = min(text_fg.w + strike_alpha, 1.0);
@ -314,7 +311,10 @@ fragment_main(VertexOutput vo, uniform float text_contrast, uniform float text_g
float4 ans_premul = 0;
if (!ONLY_FOREGROUND) ans_premul = vo.effective_background_premul;
if (!ONLY_BACKGROUND && vo.skip_text == 0u) {
// The sprite samples below use SampleLevel() as this branch is not uniform
// across a 2x2 pixel quad at cell boundaries, so implicit derivatives would
// be undefined. The sprites texture has no mipmaps, so LOD 0 is identical.
if (!ONLY_BACKGROUND && (vo.underline_exclusion_pos & SKIP_TEXT_FLAG) == 0u) {
// blend in the foreground color
float4 text_fg = load_text_foreground_color(vo.sprite_pos, vo.colored_sprite, vo.cell_foreground);
text_fg = adjust_foreground_contrast_with_background(text_fg, vo.background, text_contrast, text_gamma_adjustment);