From 025dccf182d7f9bfb4f45459284d8c0c9045e291 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 10 Dec 2025 08:49:02 +0530 Subject: [PATCH] Micro optimization: When sending cell uniform data dont even map the color table region in memory unless the color table is dirty --- kitty/gl.c | 10 ++++++++-- kitty/gl.h | 1 + kitty/shaders.c | 6 ++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/kitty/gl.c b/kitty/gl.c index 7ced321f9..9e593449d 100644 --- a/kitty/gl.c +++ b/kitty/gl.c @@ -391,6 +391,12 @@ map_buffer(ssize_t idx, GLenum access) { return ans; } +static void* +map_buffer_range(ssize_t idx, GLbitfield access, int offset, unsigned size) { + return glMapBufferRange(buffers[idx].usage, offset, size, access); +} + + static void unmap_buffer(ssize_t idx) { glUnmapBuffer(buffers[idx].usage); @@ -500,10 +506,10 @@ alloc_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum, GLenum usage) } void* -map_vao_buffer(ssize_t vao_idx, size_t bufnum, GLenum access) { +map_vao_buffer_for_write_only(ssize_t vao_idx, size_t bufnum, int offset, unsigned size) { ssize_t buf_idx = vaos[vao_idx].buffers[bufnum]; bind_buffer(buf_idx); - return map_buffer(buf_idx, access); + return map_buffer_range(buf_idx, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT, offset, size); } void* diff --git a/kitty/gl.h b/kitty/gl.h index 783e80964..99c2a00ef 100644 --- a/kitty/gl.h +++ b/kitty/gl.h @@ -55,6 +55,7 @@ ssize_t alloc_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum, GLenum void* alloc_and_map_vao_buffer(ssize_t vao_idx, GLsizeiptr size, size_t bufnum, GLenum usage, GLenum access); 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); void bind_program(int program); void bind_vertex_array(ssize_t vao_idx); void bind_vao_uniform_buffer(ssize_t vao_idx, size_t bufnum, GLuint block_index); diff --git a/kitty/shaders.c b/kitty/shaders.c index d281d7ec4..b72f9fb9e 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -450,9 +450,11 @@ cell_update_uniform_block(ssize_t vao_idx, Screen *screen, int uniform_buffer, c GLfloat bg_opacities0, bg_opacities1, bg_opacities2, bg_opacities3, bg_opacities4, bg_opacities5, bg_opacities6, bg_opacities7; }; // Send the uniform data - struct GPUCellRenderData *rd = (struct GPUCellRenderData*)map_vao_buffer(vao_idx, uniform_buffer, GL_WRITE_ONLY); ColorProfile *cp = screen->paused_rendering.expires_at ? &screen->paused_rendering.color_profile : screen->color_profile; - if (UNLIKELY(cp->dirty || screen->reload_all_gpu_data)) { + const bool color_table_needs_upload = cp->dirty || screen->reload_all_gpu_data; + const unsigned sz = color_table_needs_upload ? cell_program_layouts[CELL_PROGRAM].render_data.size : cell_program_layouts[CELL_PROGRAM].color_table.offset; + struct GPUCellRenderData *rd = (struct GPUCellRenderData*)map_vao_buffer_for_write_only(vao_idx, uniform_buffer, 0, sz); + if (color_table_needs_upload) { copy_color_table_to_buffer(cp, (GLuint*)rd, cell_program_layouts[CELL_PROGRAM].color_table.offset / sizeof(GLuint), cell_program_layouts[CELL_PROGRAM].color_table.stride / sizeof(GLuint)); } #define COLOR(name) colorprofile_to_color(cp, cp->overridden.name, cp->configured.name).rgb