mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-27 10:41:58 +02:00
Move loading of shader programs into shaders module
This commit is contained in:
@@ -123,6 +123,7 @@ from .options.utils import MINIMUM_FONT_SIZE, KeyMap, SubSequenceMap
|
|||||||
from .os_window_size import initial_window_size_func
|
from .os_window_size import initial_window_size_func
|
||||||
from .rgb import color_from_int
|
from .rgb import color_from_int
|
||||||
from .session import Session, create_sessions, get_os_window_sizing_data
|
from .session import Session, create_sessions, get_os_window_sizing_data
|
||||||
|
from .shaders import load_shader_programs
|
||||||
from .tabs import SpecialWindow, SpecialWindowInstance, Tab, TabDict, TabManager
|
from .tabs import SpecialWindow, SpecialWindowInstance, Tab, TabDict, TabManager
|
||||||
from .types import _T, AsyncResponse, SingleInstanceData, WindowSystemMouseEvent, ac
|
from .types import _T, AsyncResponse, SingleInstanceData, WindowSystemMouseEvent, ac
|
||||||
from .typing import PopenType, TypedDict
|
from .typing import PopenType, TypedDict
|
||||||
@@ -148,7 +149,7 @@ from .utils import (
|
|||||||
startup_notification_handler,
|
startup_notification_handler,
|
||||||
which,
|
which,
|
||||||
)
|
)
|
||||||
from .window import CommandOutput, CwdRequest, Window, load_shader_programs
|
from .window import CommandOutput, CwdRequest, Window
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .rc.base import ResponseType
|
from .rc.base import ResponseType
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ from .options.types import Options
|
|||||||
from .options.utils import DELETE_ENV_VAR
|
from .options.utils import DELETE_ENV_VAR
|
||||||
from .os_window_size import initial_window_size_func
|
from .os_window_size import initial_window_size_func
|
||||||
from .session import create_sessions, get_os_window_sizing_data
|
from .session import create_sessions, get_os_window_sizing_data
|
||||||
|
from .shaders import load_shader_programs
|
||||||
from .types import SingleInstanceData
|
from .types import SingleInstanceData
|
||||||
from .utils import (
|
from .utils import (
|
||||||
cleanup_ssh_control_masters,
|
cleanup_ssh_control_masters,
|
||||||
@@ -61,7 +62,6 @@ from .utils import (
|
|||||||
startup_notification_handler,
|
startup_notification_handler,
|
||||||
unix_socket_paths,
|
unix_socket_paths,
|
||||||
)
|
)
|
||||||
from .window import load_shader_programs
|
|
||||||
|
|
||||||
|
|
||||||
def set_custom_ibeam_cursor() -> None:
|
def set_custom_ibeam_cursor() -> None:
|
||||||
|
|||||||
121
kitty/shaders.py
121
kitty/shaders.py
@@ -2,11 +2,34 @@
|
|||||||
# License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>
|
# License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from functools import lru_cache
|
from functools import lru_cache, partial
|
||||||
from typing import Callable, Iterator, Optional
|
from typing import Any, Callable, Iterator, Optional
|
||||||
|
|
||||||
from .constants import read_kitty_resource
|
from .constants import read_kitty_resource
|
||||||
from .fast_data_types import GLSL_VERSION, compile_program
|
from .fast_data_types import (
|
||||||
|
BGIMAGE_PROGRAM,
|
||||||
|
BLIT_PROGRAM,
|
||||||
|
CELL_BG_PROGRAM,
|
||||||
|
CELL_FG_PROGRAM,
|
||||||
|
CELL_PROGRAM,
|
||||||
|
CELL_SPECIAL_PROGRAM,
|
||||||
|
DECORATION,
|
||||||
|
DECORATION_MASK,
|
||||||
|
DIM,
|
||||||
|
GLSL_VERSION,
|
||||||
|
GRAPHICS_ALPHA_MASK_PROGRAM,
|
||||||
|
GRAPHICS_PREMULT_PROGRAM,
|
||||||
|
GRAPHICS_PROGRAM,
|
||||||
|
MARK,
|
||||||
|
MARK_MASK,
|
||||||
|
NUM_UNDERLINE_STYLES,
|
||||||
|
REVERSE,
|
||||||
|
STRIKETHROUGH,
|
||||||
|
TINT_PROGRAM,
|
||||||
|
compile_program,
|
||||||
|
get_options,
|
||||||
|
init_cell_program,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def identity(x: str) -> str:
|
def identity(x: str) -> str:
|
||||||
@@ -55,3 +78,95 @@ class Program:
|
|||||||
@lru_cache(maxsize=64)
|
@lru_cache(maxsize=64)
|
||||||
def program_for(name: str) -> Program:
|
def program_for(name: str) -> Program:
|
||||||
return Program(name)
|
return Program(name)
|
||||||
|
|
||||||
|
|
||||||
|
def multi_replace(src: str, **replacements: Any) -> str:
|
||||||
|
r = {k: str(v) for k, v in replacements.items()}
|
||||||
|
|
||||||
|
def sub(m: 're.Match[str]') -> str:
|
||||||
|
return r.get(m.group(1), m.group(1))
|
||||||
|
|
||||||
|
return re.sub(r'\{([A-Z_]+)\}', sub, src)
|
||||||
|
|
||||||
|
|
||||||
|
class LoadShaderPrograms:
|
||||||
|
|
||||||
|
text_fg_override_threshold: float = 0
|
||||||
|
text_old_gamma: bool = False
|
||||||
|
semi_transparent: bool = False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def needs_recompile(self) -> bool:
|
||||||
|
opts = get_options()
|
||||||
|
return opts.text_fg_override_threshold != self.text_fg_override_threshold or (opts.text_composition_strategy == 'legacy') != self.text_old_gamma
|
||||||
|
|
||||||
|
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
|
||||||
|
opts = get_options()
|
||||||
|
self.text_old_gamma = opts.text_composition_strategy == 'legacy'
|
||||||
|
self.text_fg_override_threshold = max(0, min(opts.text_fg_override_threshold, 100)) * 0.01
|
||||||
|
program_for('blit').compile(BLIT_PROGRAM, allow_recompile)
|
||||||
|
cell = program_for('cell')
|
||||||
|
|
||||||
|
def resolve_cell_vertex_defines(which: str, v: str) -> str:
|
||||||
|
v = multi_replace(
|
||||||
|
v,
|
||||||
|
WHICH_PROGRAM=which,
|
||||||
|
REVERSE_SHIFT=REVERSE,
|
||||||
|
STRIKE_SHIFT=STRIKETHROUGH,
|
||||||
|
DIM_SHIFT=DIM,
|
||||||
|
DECORATION_SHIFT=DECORATION,
|
||||||
|
MARK_SHIFT=MARK,
|
||||||
|
MARK_MASK=MARK_MASK,
|
||||||
|
DECORATION_MASK=DECORATION_MASK,
|
||||||
|
STRIKE_SPRITE_INDEX=NUM_UNDERLINE_STYLES + 1,
|
||||||
|
)
|
||||||
|
if semi_transparent:
|
||||||
|
v = v.replace('#define NOT_TRANSPARENT', '#define TRANSPARENT')
|
||||||
|
return v
|
||||||
|
|
||||||
|
def resolve_cell_fragment_defines(which: str, f: str) -> str:
|
||||||
|
f = f.replace('{WHICH_PROGRAM}', which)
|
||||||
|
if self.text_fg_override_threshold != 0.:
|
||||||
|
f = f.replace('#define NO_FG_OVERRIDE', f'#define FG_OVERRIDE {self.text_fg_override_threshold}')
|
||||||
|
if self.text_old_gamma:
|
||||||
|
f = f.replace('#define TEXT_NEW_GAMMA', '#define TEXT_OLD_GAMMA')
|
||||||
|
if semi_transparent:
|
||||||
|
f = f.replace('#define NOT_TRANSPARENT', '#define TRANSPARENT')
|
||||||
|
return f
|
||||||
|
|
||||||
|
for which, p in {
|
||||||
|
'SIMPLE': CELL_PROGRAM,
|
||||||
|
'BACKGROUND': CELL_BG_PROGRAM,
|
||||||
|
'SPECIAL': CELL_SPECIAL_PROGRAM,
|
||||||
|
'FOREGROUND': CELL_FG_PROGRAM,
|
||||||
|
}.items():
|
||||||
|
cell.apply_to_sources(
|
||||||
|
vertex=partial(resolve_cell_vertex_defines, which),
|
||||||
|
frag=partial(resolve_cell_fragment_defines, which),
|
||||||
|
)
|
||||||
|
cell.compile(p, allow_recompile)
|
||||||
|
|
||||||
|
graphics = program_for('graphics')
|
||||||
|
|
||||||
|
def resolve_graphics_fragment_defines(which: str, f: str) -> str:
|
||||||
|
return f.replace('ALPHA_TYPE', which)
|
||||||
|
|
||||||
|
for which, p in {
|
||||||
|
'SIMPLE': GRAPHICS_PROGRAM,
|
||||||
|
'PREMULT': GRAPHICS_PREMULT_PROGRAM,
|
||||||
|
'ALPHA_MASK': GRAPHICS_ALPHA_MASK_PROGRAM,
|
||||||
|
}.items():
|
||||||
|
graphics.apply_to_sources(frag=partial(resolve_cell_fragment_defines, which))
|
||||||
|
graphics.compile(p, allow_recompile)
|
||||||
|
|
||||||
|
program_for('bgimage').compile(BGIMAGE_PROGRAM, allow_recompile)
|
||||||
|
program_for('tint').compile(TINT_PROGRAM)
|
||||||
|
init_cell_program()
|
||||||
|
|
||||||
|
|
||||||
|
load_shader_programs = LoadShaderPrograms()
|
||||||
|
|||||||
111
kitty/window.py
111
kitty/window.py
@@ -42,34 +42,16 @@ from .constants import (
|
|||||||
wakeup_io_loop,
|
wakeup_io_loop,
|
||||||
)
|
)
|
||||||
from .fast_data_types import (
|
from .fast_data_types import (
|
||||||
BGIMAGE_PROGRAM,
|
|
||||||
BLIT_PROGRAM,
|
|
||||||
CELL_BG_PROGRAM,
|
|
||||||
CELL_FG_PROGRAM,
|
|
||||||
CELL_PROGRAM,
|
|
||||||
CELL_SPECIAL_PROGRAM,
|
|
||||||
CURSOR_BEAM,
|
CURSOR_BEAM,
|
||||||
CURSOR_BLOCK,
|
CURSOR_BLOCK,
|
||||||
CURSOR_UNDERLINE,
|
CURSOR_UNDERLINE,
|
||||||
DCS,
|
DCS,
|
||||||
DECORATION,
|
|
||||||
DECORATION_MASK,
|
|
||||||
DIM,
|
|
||||||
GLFW_MOD_CONTROL,
|
GLFW_MOD_CONTROL,
|
||||||
GRAPHICS_ALPHA_MASK_PROGRAM,
|
|
||||||
GRAPHICS_PREMULT_PROGRAM,
|
|
||||||
GRAPHICS_PROGRAM,
|
|
||||||
MARK,
|
|
||||||
MARK_MASK,
|
|
||||||
NO_CURSOR_SHAPE,
|
NO_CURSOR_SHAPE,
|
||||||
NUM_UNDERLINE_STYLES,
|
|
||||||
OSC,
|
OSC,
|
||||||
REVERSE,
|
|
||||||
SCROLL_FULL,
|
SCROLL_FULL,
|
||||||
SCROLL_LINE,
|
SCROLL_LINE,
|
||||||
SCROLL_PAGE,
|
SCROLL_PAGE,
|
||||||
STRIKETHROUGH,
|
|
||||||
TINT_PROGRAM,
|
|
||||||
Color,
|
Color,
|
||||||
KeyEvent,
|
KeyEvent,
|
||||||
Screen,
|
Screen,
|
||||||
@@ -83,7 +65,6 @@ from .fast_data_types import (
|
|||||||
get_boss,
|
get_boss,
|
||||||
get_click_interval,
|
get_click_interval,
|
||||||
get_options,
|
get_options,
|
||||||
init_cell_program,
|
|
||||||
last_focused_os_window_id,
|
last_focused_os_window_id,
|
||||||
mark_os_window_dirty,
|
mark_os_window_dirty,
|
||||||
mouse_selection,
|
mouse_selection,
|
||||||
@@ -106,7 +87,6 @@ from .notify import (
|
|||||||
)
|
)
|
||||||
from .options.types import Options
|
from .options.types import Options
|
||||||
from .rgb import to_color
|
from .rgb import to_color
|
||||||
from .shaders import program_for
|
|
||||||
from .terminfo import get_capabilities
|
from .terminfo import get_capabilities
|
||||||
from .types import MouseEvent, OverlayType, WindowGeometry, ac, run_once
|
from .types import MouseEvent, OverlayType, WindowGeometry, ac, run_once
|
||||||
from .typing import BossType, ChildType, EdgeLiteral, TabType, TypedDict
|
from .typing import BossType, ChildType, EdgeLiteral, TabType, TypedDict
|
||||||
@@ -362,97 +342,6 @@ def as_text(
|
|||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
def multi_replace(src: str, **replacements: Any) -> str:
|
|
||||||
r = {k: str(v) for k, v in replacements.items()}
|
|
||||||
|
|
||||||
def sub(m: 're.Match[str]') -> str:
|
|
||||||
return r.get(m.group(1), m.group(1))
|
|
||||||
|
|
||||||
return re.sub(r'\{([A-Z_]+)\}', sub, src)
|
|
||||||
|
|
||||||
|
|
||||||
class LoadShaderPrograms:
|
|
||||||
|
|
||||||
text_fg_override_threshold: float = 0
|
|
||||||
text_old_gamma: bool = False
|
|
||||||
semi_transparent: bool = False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def needs_recompile(self) -> bool:
|
|
||||||
opts = get_options()
|
|
||||||
return opts.text_fg_override_threshold != self.text_fg_override_threshold or (opts.text_composition_strategy == 'legacy') != self.text_old_gamma
|
|
||||||
|
|
||||||
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
|
|
||||||
opts = get_options()
|
|
||||||
self.text_old_gamma = opts.text_composition_strategy == 'legacy'
|
|
||||||
self.text_fg_override_threshold = max(0, min(opts.text_fg_override_threshold, 100)) * 0.01
|
|
||||||
program_for('blit').compile(BLIT_PROGRAM, allow_recompile)
|
|
||||||
cell = program_for('cell')
|
|
||||||
|
|
||||||
def resolve_cell_vertex_defines(which: str, v: str) -> str:
|
|
||||||
v = multi_replace(
|
|
||||||
v,
|
|
||||||
WHICH_PROGRAM=which,
|
|
||||||
REVERSE_SHIFT=REVERSE,
|
|
||||||
STRIKE_SHIFT=STRIKETHROUGH,
|
|
||||||
DIM_SHIFT=DIM,
|
|
||||||
DECORATION_SHIFT=DECORATION,
|
|
||||||
MARK_SHIFT=MARK,
|
|
||||||
MARK_MASK=MARK_MASK,
|
|
||||||
DECORATION_MASK=DECORATION_MASK,
|
|
||||||
STRIKE_SPRITE_INDEX=NUM_UNDERLINE_STYLES + 1,
|
|
||||||
)
|
|
||||||
if semi_transparent:
|
|
||||||
v = v.replace('#define NOT_TRANSPARENT', '#define TRANSPARENT')
|
|
||||||
return v
|
|
||||||
|
|
||||||
def resolve_cell_fragment_defines(which: str, f: str) -> str:
|
|
||||||
f = f.replace('{WHICH_PROGRAM}', which)
|
|
||||||
if self.text_fg_override_threshold != 0.:
|
|
||||||
f = f.replace('#define NO_FG_OVERRIDE', f'#define FG_OVERRIDE {self.text_fg_override_threshold}')
|
|
||||||
if self.text_old_gamma:
|
|
||||||
f = f.replace('#define TEXT_NEW_GAMMA', '#define TEXT_OLD_GAMMA')
|
|
||||||
if semi_transparent:
|
|
||||||
f = f.replace('#define NOT_TRANSPARENT', '#define TRANSPARENT')
|
|
||||||
return f
|
|
||||||
|
|
||||||
for which, p in {
|
|
||||||
'SIMPLE': CELL_PROGRAM,
|
|
||||||
'BACKGROUND': CELL_BG_PROGRAM,
|
|
||||||
'SPECIAL': CELL_SPECIAL_PROGRAM,
|
|
||||||
'FOREGROUND': CELL_FG_PROGRAM,
|
|
||||||
}.items():
|
|
||||||
cell.apply_to_sources(
|
|
||||||
vertex=partial(resolve_cell_vertex_defines, which),
|
|
||||||
frag=partial(resolve_cell_fragment_defines, which),
|
|
||||||
)
|
|
||||||
cell.compile(p, allow_recompile)
|
|
||||||
|
|
||||||
graphics = program_for('graphics')
|
|
||||||
|
|
||||||
def resolve_graphics_fragment_defines(which: str, f: str) -> str:
|
|
||||||
return f.replace('ALPHA_TYPE', which)
|
|
||||||
|
|
||||||
for which, p in {
|
|
||||||
'SIMPLE': GRAPHICS_PROGRAM,
|
|
||||||
'PREMULT': GRAPHICS_PREMULT_PROGRAM,
|
|
||||||
'ALPHA_MASK': GRAPHICS_ALPHA_MASK_PROGRAM,
|
|
||||||
}.items():
|
|
||||||
graphics.apply_to_sources(frag=partial(resolve_cell_fragment_defines, which))
|
|
||||||
graphics.compile(p, allow_recompile)
|
|
||||||
|
|
||||||
program_for('bgimage').compile(BGIMAGE_PROGRAM, allow_recompile)
|
|
||||||
program_for('tint').compile(TINT_PROGRAM)
|
|
||||||
init_cell_program()
|
|
||||||
|
|
||||||
|
|
||||||
load_shader_programs = LoadShaderPrograms()
|
|
||||||
|
|
||||||
|
|
||||||
def setup_colors(screen: Screen, opts: Options) -> None:
|
def setup_colors(screen: Screen, opts: Options) -> None:
|
||||||
screen.color_profile.update_ansi_color_table(build_ansi_color_table(opts))
|
screen.color_profile.update_ansi_color_table(build_ansi_color_table(opts))
|
||||||
|
|||||||
Reference in New Issue
Block a user