From 7113d5c293ef2c9a7b5eda0453d9868e275926a9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 9 Sep 2017 10:56:55 +0530 Subject: [PATCH] Use a mapped buffer to send color table to the GPU --- kitty/char_grid.py | 21 ++++++++++----------- kitty/shaders.py | 4 ++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/kitty/char_grid.py b/kitty/char_grid.py index 67fd36e90..fab7864a9 100644 --- a/kitty/char_grid.py +++ b/kitty/char_grid.py @@ -14,9 +14,10 @@ from .constants import ( ) from .fast_data_types import ( CELL, CURSOR_BEAM, CURSOR_BLOCK, CURSOR_UNDERLINE, GL_BLEND, GL_FLOAT, - GL_LINE_LOOP, GL_TRIANGLE_FAN, GL_UNSIGNED_INT, GL_UNSIGNED_SHORT, - glDisable, glDrawArrays, glDrawArraysInstanced, glEnable, glUniform1i, - glUniform2f, glUniform2i, glUniform2ui, glUniform4f, glUniform4ui + GL_LINE_LOOP, GL_STATIC_DRAW, GL_TRIANGLE_FAN, GL_UNSIGNED_INT, + GL_UNSIGNED_SHORT, glDisable, glDrawArrays, glDrawArraysInstanced, + glEnable, glUniform1i, glUniform2f, glUniform2i, glUniform2ui, glUniform4f, + glUniform4ui ) from .rgb import to_color from .shaders import ShaderProgram, load_shaders @@ -32,22 +33,19 @@ class DynamicColor(Enum): default_fg, default_bg, cursor_color, highlight_fg, highlight_bg = range(1, 6) -class CellProgram(ShaderProgram): +class CellProgram(ShaderProgram): # {{{ def __init__(self, *args): ShaderProgram.__init__(self, *args) - self.color_table_buf = None - def send_color_table(self, color_profile): + def send_color_table(self, color_profile, cell_program): if color_profile.ubo is None: color_profile.ubo = self.init_uniform_block('ColorTable', 'color_table') ubo = color_profile.ubo - if self.color_table_buf is None: - self.color_table_buf = (GLuint * (ubo.size // sizeof(GLuint)))() offset = ubo.offsets['color_table'] // sizeof(GLuint) stride = ubo.size // (256 * sizeof(GLuint)) - color_profile.copy_color_table(addressof(self.color_table_buf), offset, stride) - self.send_uniform_buffer_data(ubo, self.color_table_buf) + with cell_program.mapped_uniform_data(ubo, usage=GL_STATIC_DRAW) as address: + color_profile.copy_color_table(address, offset, stride) def create_sprite_map(self): with self.array_object_creator() as add_attribute: @@ -66,6 +64,7 @@ def load_shader_programs(): with cursor.array_object_creator() as add_attribute: cursor.vao_id = add_attribute.vao_id return cell, cursor +# }}} class Selection: # {{{ @@ -139,7 +138,7 @@ def calculate_gl_geometry(window_geometry, viewport_width, viewport_height, cell def render_cells(vao_id, sg, cell_program, sprites, color_profile, invert_colors=False, screen_reversed=False): if color_profile.dirty: - cell_program.send_color_table(color_profile) + cell_program.send_color_table(color_profile, cell_program) color_profile.dirty = False ul = cell_program.uniform_location glUniform2ui(ul('dimensions'), sg.xnum, sg.ynum) diff --git a/kitty/shaders.py b/kitty/shaders.py index 54f09bd04..7df9f7028 100644 --- a/kitty/shaders.py +++ b/kitty/shaders.py @@ -368,8 +368,8 @@ class ShaderProgram: # {{{ buf_id = buffer_manager.create(GL_UNIFORM_BUFFER) return UBO(size=size, index=idx, offsets=offsets, buf_id=buf_id) - def send_uniform_buffer_data(self, ubo, data, usage=GL_STREAM_DRAW): - buffer_manager.set_data(ubo.buf_id, data, usage=usage) + def mapped_uniform_data(self, ubo, usage=GL_STREAM_DRAW, access=GL_WRITE_ONLY): + return buffer_manager.mapped_buffer(ubo.buf_id, ubo.size, usage=usage, access=access) @contextmanager def bound_uniform_buffer(self, ubo):