From b45d7ae21f4b44858f097c1f5267e8ea391788ff Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 24 May 2023 13:50:19 +0530 Subject: [PATCH] Dont require restart to change text_fg_override_threshold from zero to non-zero --- kitty/boss.py | 3 ++- kitty/fast_data_types.pyi | 2 +- kitty/options/definition.py | 3 +-- kitty/shaders.c | 9 ++++++--- kitty/window.py | 27 ++++++++++++++++++++------- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index 860ed8072..866a90728 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -147,7 +147,7 @@ from .utils import ( startup_notification_handler, which, ) -from .window import CommandOutput, CwdRequest, Window +from .window import CommandOutput, CwdRequest, Window, load_shader_programs if TYPE_CHECKING: from .rc.base import ResponseType @@ -2409,6 +2409,7 @@ class Boss: for w in self.all_windows: self.default_bg_changed_for(w.id) w.refresh(reload_all_gpu_data=True) + load_shader_programs.recompile_if_needed() @ac('misc', ''' Reload the config file diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 5d0b39bea..ece66bee4 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -458,7 +458,7 @@ def add_window(os_window_id: int, tab_id: int, title: str) -> int: def compile_program( - which: int, vertex_shader: str, fragment_shader: str + which: int, vertex_shader: str, fragment_shader: str, allow_recompile: bool = False ) -> int: pass diff --git a/kitty/options/definition.py b/kitty/options/definition.py index 2d5fdd1f0..509956102 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -276,8 +276,7 @@ ranging from :code:`0` to :code:`100`. If the difference in luminance of the foreground and background is below this threshold, the foreground color will be set to white if the background is dark or black if the background is light. The default value is :code:`0`, which means no overriding is performed. Useful when working with applications -that use colors that do not contrast well with your preferred color scheme. Changing this option -from zero to a non-zero value or vice versa requires a restart of kitty. +that use colors that do not contrast well with your preferred color scheme. ''') egr() # }}} diff --git a/kitty/shaders.c b/kitty/shaders.c index 56462e58b..0145bdd23 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -1071,12 +1071,15 @@ draw_borders(ssize_t vao_idx, unsigned int num_border_rects, BorderRect *rect_bu static PyObject* compile_program(PyObject UNUSED *self, PyObject *args) { const char *vertex_shader, *fragment_shader; - int which; + int which, allow_recompile = 0; GLuint vertex_shader_id = 0, fragment_shader_id = 0; - if (!PyArg_ParseTuple(args, "iss", &which, &vertex_shader, &fragment_shader)) return NULL; + if (!PyArg_ParseTuple(args, "iss|p", &which, &vertex_shader, &fragment_shader, &allow_recompile)) return NULL; if (which < 0 || which >= NUM_PROGRAMS) { PyErr_Format(PyExc_ValueError, "Unknown program: %d", which); return NULL; } Program *program = program_ptr(which); - if (program->id != 0) { PyErr_SetString(PyExc_ValueError, "program already compiled"); return NULL; } + if (program->id != 0) { + if (allow_recompile) { glDeleteProgram(program->id); program->id = 0; } + else { PyErr_SetString(PyExc_ValueError, "program already compiled"); return NULL; } + } program->id = glCreateProgram(); vertex_shader_id = compile_shader(GL_VERTEX_SHADER, vertex_shader); fragment_shader_id = compile_shader(GL_FRAGMENT_SHADER, fragment_shader); diff --git a/kitty/window.py b/kitty/window.py index 2885d9434..4a1a6d5da 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -374,8 +374,20 @@ def multi_replace(src: str, **replacements: Any) -> str: class LoadShaderPrograms: - def __call__(self, semi_transparent: bool = False) -> None: - compile_program(BLIT_PROGRAM, *load_shaders('blit')) + text_fg_override_threshold: float = 0 + semi_transparent: bool = False + + @property + def needs_recompile(self) -> bool: + return get_options().text_fg_override_threshold != self.text_fg_override_threshold + + def recompile_if_needed(self) -> None: + if self.needs_recompile: + self(self.semi_transparent, allow_recompile=True) + + def __call__(self, semi_transparent: bool = False, allow_recompile: bool = False) -> None: + self.semi_transparent = semi_transparent + compile_program(BLIT_PROGRAM, *load_shaders('blit'), allow_recompile) v, f = load_shaders('cell') for which, p in { @@ -397,12 +409,13 @@ class LoadShaderPrograms: DECORATION_MASK=DECORATION_MASK, STRIKE_SPRITE_INDEX=NUM_UNDERLINE_STYLES + 1, ) - if get_options().text_fg_override_threshold != 0.: + self.text_fg_override_threshold = get_options().text_fg_override_threshold + if self.text_fg_override_threshold != 0.: ff = ff.replace('#define NO_FG_OVERRIDE', '#define FG_OVERRIDE') if semi_transparent: vv = vv.replace('#define NOT_TRANSPARENT', '#define TRANSPARENT') ff = ff.replace('#define NOT_TRANSPARENT', '#define TRANSPARENT') - compile_program(p, vv, ff) + compile_program(p, vv, ff, allow_recompile) v, f = load_shaders('graphics') for which, p in { @@ -411,12 +424,12 @@ class LoadShaderPrograms: 'ALPHA_MASK': GRAPHICS_ALPHA_MASK_PROGRAM, }.items(): ff = f.replace('ALPHA_TYPE', which) - compile_program(p, v, ff) + compile_program(p, v, ff, allow_recompile) v, f = load_shaders('bgimage') - compile_program(BGIMAGE_PROGRAM, v, f) + compile_program(BGIMAGE_PROGRAM, v, f, allow_recompile) v, f = load_shaders('tint') - compile_program(TINT_PROGRAM, v, f) + compile_program(TINT_PROGRAM, v, f, allow_recompile) init_cell_program()