Custom shaders: Query shader source with a parser instead of regexes

Add a small Slang lexer and top level declaration parser to slang.py, and
SlangShaderQuery which runs the slangc preprocessor when an answer could
depend on it. Use it for:

- Detecting pointer_map(), so that comments, #if 0 blocks and calls no
  longer count, macros are honored, and a pointer_map() that is not public
  or does not return float2 is reported instead of failing obscurely. The
  result is cached alongside the pipeline module, so a cached load does
  not run slangc.
- Applying pipeline var directives, which now work for declarations
  spanning multiple lines and match by name as documented.
- Parsing the builtin shaders, with identical results for all of them.

Also cleanup minor issues form previous PR

- Ignore pointer_map() in groups that only draw to part of the screen
- Share the pointer position conversion between glfw.c and mouse.c
- Pass POINTER_MAP_SIZE to the pipeline as a define instead of
  duplicating it
- Do not free the pointer map render target on every pipeline reload
- Document what pointer_map() may depend on and when d.mouse_pos lines
  up with the pointer
This commit is contained in:
Kovid Goyal 2026-10-10 10:49:22 +05:30
parent c75ed19448
commit be4fef614b
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
9 changed files with 539 additions and 96 deletions

View file

@ -395,6 +395,10 @@ Detailed list of changes
screen no longer misplaces clicks, see :ref:`custom_shader_pointer_map`
(:pull:`10639`)
- Custom shaders: Fix pipeline :code:`var` directives being silently ignored
when the shader declares the variable over multiple lines or with a
different type
0.49.2 [2026-10-01]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -191,13 +191,19 @@ content shown at a given position actually is::
}
``pos`` and the return value are in the same UV co-ordinates as ``t.pos``.
kitty evaluates the function on a grid when the pipeline is loaded and when the
OS Window is resized, and uses it to move all pointer events, so it must not
depend on time or on the mouse position. Only shaders in groups that draw to
the screen and are always active, that is, without ``animation_start``,
``attach`` or ``output_texture``, are used, since only they move content
permanently. When several shaders define it, they are applied starting from the
last one to run. Note that :code:`d.mouse_pos` contains the mapped position.
The function must be ``public`` and return ``float2``. kitty evaluates it on a
grid only when the pipeline is loaded and when the OS Window is resized, and
uses it to move all pointer events, so the only field of ``d`` it may use is
``d.viewport_size_pixels``. Only shaders in groups that draw to the whole
screen and are always active, that is, without ``animation_start``,
``attach``, ``output_texture``, ``viewport_pos`` or ``viewport_size``, are
used, since only they move content permanently. When several shaders define
it, they are applied starting from the last one to run.
Note that :code:`d.mouse_pos` then contains the mapped position, that is, the
position of the content under the mouse before it was moved. Effects that draw
at the mouse position line up with the pointer when they run before the shader
that moves content, not after it.
.. _custom_shader_pipeline:

View file

@ -609,8 +609,8 @@ key_callback(GLFWwindow *w, GLFWkeyevent *ev) {
// Sets the mouse position from window co-ordinates, converting to viewport
// pixels and then to where custom shaders actually show the content under it
static void
set_mouse_position(OSWindow *window, double x, double y) {
void
set_os_window_mouse_position(OSWindow *window, double x, double y) {
window->mouse_x = x * window->viewport_x_ratio;
window->mouse_y = y * window->viewport_y_ratio;
map_pointer_position(window, &window->mouse_x, &window->mouse_y);
@ -624,7 +624,7 @@ cursor_enter_callback(GLFWwindow *w, int entered) {
monotonic_t now = monotonic();
global_state.callback_os_window->last_mouse_activity_at = now;
const double old_mouse_x = global_state.callback_os_window->mouse_x, old_mouse_y = global_state.callback_os_window->mouse_y;
set_mouse_position(global_state.callback_os_window, x, y);
set_os_window_mouse_position(global_state.callback_os_window, x, y);
// focus_follows_mouse should react to the mouse moving, not to a window
// appearing under a stationary cursor (such as when returning to this
// desktop/space). Detect genuine motion by comparing against the last
@ -651,7 +651,7 @@ refresh_mouse_position_for_hit_test(GLFWwindow *w, OSWindow *window) {
// Query the current position for discrete pointer events without emitting a move event.
double x, y;
glfwGetCursorPos(w, &x, &y);
set_mouse_position(window, x, y);
set_os_window_mouse_position(window, x, y);
return true;
}
#else
@ -689,7 +689,7 @@ mouse_button_callback(GLFWwindow *w, int button, int action, int mods) {
if (!position_was_refreshed) {
double x, y;
glfwGetCursorPos(w, &x, &y);
set_mouse_position(window, x, y);
set_os_window_mouse_position(window, x, y);
}
if (is_window_ready_for_callbacks()) mouse_event(-1, mods, -1);
}
@ -707,7 +707,7 @@ on_mouse_position_update(double x, double y) {
global_state.callback_os_window->cursor_blink_zero_time = now;
global_state.callback_os_window->user_is_idle = false;
global_state.callback_os_window->shader_anim_event_registry |= (1u << SHADER_ANIM_EVENT_USER_ACTIVITY);
set_mouse_position(global_state.callback_os_window, x, y);
set_os_window_mouse_position(global_state.callback_os_window, x, y);
global_state.callback_os_window->has_received_cursor_pos_event = true;
if (is_window_ready_for_callbacks()) mouse_event(-1, global_state.mods_at_last_key_or_button_event, -1);
request_tick_callback();
@ -784,7 +784,7 @@ touch_move_mouse(OSWindow *osw, double x, double y, int mods) {
osw->cursor_blink_zero_time = now;
osw->user_is_idle = false;
osw->shader_anim_event_registry |= (1u << SHADER_ANIM_EVENT_USER_ACTIVITY);
set_mouse_position(osw, x, y);
set_os_window_mouse_position(osw, x, y);
osw->has_received_cursor_pos_event = true;
if (is_window_ready_for_callbacks()) mouse_event(-1, mods, -1);
}
@ -1155,11 +1155,9 @@ drop_dest_callback(GLFWwindow *window, GLFWDropEvent *ev) {
case GLFW_DROP_MOVE:
global_state.drop_dest.drop_has_happened = false;
global_state.drop_dest.os_window_id = os_window->id;
double drag_x = ev->xpos * os_window->viewport_x_ratio, drag_y = ev->ypos * os_window->viewport_y_ratio;
map_pointer_position(os_window, &drag_x, &drag_y);
os_window->last_drag_event.x = (int)drag_x;
os_window->last_drag_event.y = (int)drag_y;
on_mouse_position_update(ev->xpos, ev->ypos);
os_window->last_drag_event.x = (int)os_window->mouse_x;
os_window->last_drag_event.y = (int)os_window->mouse_y;
// Re-evaluate which kitty window is now under the cursor after the
// position update, so that drag enter/leave events are sent to the
// correct kitty window when the drag crosses a kitty window boundary

View file

@ -1590,9 +1590,7 @@ scroll_event(const GLFWScrollEvent *ev) {
if (!osw->is_focused && osw->handle) {
double mouse_x, mouse_y;
glfwGetCursorPos((GLFWwindow *)osw->handle, &mouse_x, &mouse_y);
osw->mouse_x = mouse_x * osw->viewport_x_ratio;
osw->mouse_y = mouse_y * osw->viewport_y_ratio;
map_pointer_position(osw, &osw->mouse_x, &osw->mouse_y);
set_os_window_mouse_position(osw, mouse_x, mouse_y);
}
MouseRegion r = mouse_region(false, true);
Window *w = r.window;

View file

@ -3417,7 +3417,6 @@ compile_program(PyObject UNUSED *self, PyObject *args) {
free_custom_shader_pipeline(&custom_shaders.end);
zero_at_ptr(&custom_shaders.end);
custom_shaders.generation++;
free_pointer_map_target();
}
if (program->id != 0) {
glDeleteProgram(program->id);

View file

@ -61,8 +61,8 @@ linear2srgb(float3 x) { // vector
uniform Sampler2D backbuffer, a, b, persist;
// Must match POINTER_MAP_SIZE in state.h
static const float POINTER_MAP_SIZE = 64;
// The size of the pointer map grid, POINTER_MAP_SIZE is defined by kitty from state.h when compiling
static const float pointer_map_size = POINTER_MAP_SIZE;
// Where the content visible at pos actually is, as decided by the pointer_map()
// functions of the shaders that move content around
@ -78,7 +78,7 @@ pipeline_pointer_map(float2 pos) {
float4
encode_pointer_map(float2 texel_pos) {
// Spread the texels so that the first and last ones land exactly on the edges
float2 pos = (texel_pos * POINTER_MAP_SIZE - 0.5f) / (POINTER_MAP_SIZE - 1.f);
float2 pos = (texel_pos * pointer_map_size - 0.5f) / (pointer_map_size - 1.f);
float2 q = round(saturate(pipeline_pointer_map(pos)) * 65535.f);
float2 hi = floor(q / 256.f);
return float4(hi / 255.f, (q - hi * 256.f) / 255.f);

View file

@ -48,6 +48,7 @@ from kitty.fast_data_types import (
MARK_MASK,
MAX_CUSTOM_SHADER_GROUPS,
PADDING_PROGRAM,
POINTER_MAP_SIZE,
REVERSE,
ROUNDED_RECT_PROGRAM,
SCREENSHOT_PROGRAM,
@ -334,6 +335,326 @@ class LoadShaderPrograms:
load_shader_programs = LoadShaderPrograms()
# Querying shader source {{{
# A lexer and a parser for just the top level declarations of Slang source, so
# that questions such as "does this shader define a function" are not fooled by
# comments, strings, formatting or the contents of function bodies.
class TokenType(StrEnum):
identifier = auto()
number = auto()
string = auto()
punctuation = auto()
directive = auto()
comment = auto()
class Token(NamedTuple):
type: TokenType
text: str
start: int # offsets into the source
end: int
@run_once
def slang_token_pat() -> re.Pattern[str]:
return re.compile(
r"""
(?P<ws>\s+)
| (?P<comment>//[^\n]*|/\*[\s\S]*?(?:\*/|\Z))
| (?P<directive>\#(?:\\\r?\n|[^\n])*)
| (?P<string>"(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*')
| (?P<number>(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?[A-Za-z0-9_]*)
| (?P<identifier>[A-Za-z_][A-Za-z0-9_]*)
| (?P<punctuation>::|->|.)
""",
re.VERBOSE,
)
def tokenize_slang(src: str) -> Iterator[Token]:
for m in slang_token_pat().finditer(src):
kind = m.lastgroup
if kind == 'ws':
continue
start, end = m.span()
if kind == 'directive' and src[src.rfind('\n', 0, start) + 1 : start].strip():
# A # that is not the first thing on its line is not a directive
end = start + 1
yield Token(TokenType.punctuation, '#', start, end)
# Lex the rest of the line normally
for t in tokenize_slang(src[end : m.end()]):
yield t._replace(start=t.start + end, end=t.end + end)
continue
yield Token(TokenType(kind), src[start:end], start, end)
# Keywords that can precede the type in a declaration
DECLARATION_MODIFIERS = frozenset(
(
'public',
'private',
'internal',
'static',
'const',
'extern',
'export',
'inline',
'uniform',
'groupshared',
'precise',
'nointerpolation',
'in',
'out',
'inout',
)
)
# Declarations whose body is not code
NON_CODE_DECLARATIONS = frozenset(('struct', 'class', 'enum', 'interface', 'extension', 'namespace', 'cbuffer', 'typedef', 'typealias', 'using', '__generic'))
CLOSING_BRACKETS = {'(': ')', '[': ']', '{': '}'}
class SlangAttribute(NamedTuple):
name: str # for example: shader
args: tuple[str, ...] # for example: ('vertex',) with string quotes removed
class SlangFunction(NamedTuple):
name: str
return_type: str # without whitespace, for example: vector<float,2>
modifiers: frozenset[str]
attributes: tuple[SlangAttribute, ...]
has_body: bool
def returns(self, *types: str) -> bool:
return self.return_type in types
class SlangVariable(NamedTuple):
name: str
type: str # without whitespace
modifiers: frozenset[str]
declaration: str # the source of the whole declaration
type_start: int # offset of the type in the source
end: int # offset of the terminating semi-colon in the source, or the end of the declaration if there is none
class SlangDeclarations(NamedTuple):
module: str = ''
imports: tuple[str, ...] = ()
functions: tuple[SlangFunction, ...] = ()
variables: tuple[SlangVariable, ...] = ()
comments: tuple[str, ...] = ()
def function(self, name: str) -> SlangFunction | None:
for f in self.functions:
if f.name == name and f.has_body:
return f
for f in self.functions:
if f.name == name:
return f
return None
def variable(self, name: str) -> SlangVariable | None:
for v in self.variables:
if v.name == name:
return v
return None
class _Item(NamedTuple):
# A single token or a bracketed group of tokens at the top level of a declaration
first: Token
last: Token
@property
def text(self) -> str:
return self.first.text
@property
def is_group(self) -> bool:
return self.first is not self.last
def parse_slang_declarations(src: str) -> SlangDeclarations:
"""Parse the top level declarations in Slang source. Preprocessor directives
are ignored, use SlangShaderQuery to see the source as the compiler does."""
tokens: list[Token] = []
comments: list[str] = []
for t in tokenize_slang(src):
if t.type is TokenType.comment:
comments.append(t.text)
elif t.type is not TokenType.directive:
tokens.append(t)
module = ''
imports: list[str] = []
functions: list[SlangFunction] = []
variables: list[SlangVariable] = []
def group_end(i: int) -> int:
# Index of the token closing the bracket at i, or of the last token if unbalanced
stack = [CLOSING_BRACKETS[tokens[i].text]]
for j in range(i + 1, len(tokens)):
text = tokens[j].text
if tokens[j].type is TokenType.punctuation:
if text in CLOSING_BRACKETS:
stack.append(CLOSING_BRACKETS[text])
elif text == stack[-1]:
stack.pop()
if not stack:
return j
return len(tokens) - 1
def joined(items: Iterable[_Item]) -> str:
return ''.join(''.join(src[it.first.start : it.last.end].split()) for it in items)
def parse_attribute(start: int, end: int) -> SlangAttribute:
inner = tokens[start + 1 : end]
name = inner[0].text if inner and inner[0].type is TokenType.identifier else ''
args = tuple(t.text[1:-1] if t.type is TokenType.string else t.text for t in inner[1:] if t.type is not TokenType.punctuation)
return SlangAttribute(name, args)
def finish(items: list[_Item], attributes: list[SlangAttribute], terminator: Token | None) -> None:
nonlocal module
if not items:
return
head = items[0].text
if head == 'module':
module = joined(items[1:])
return
if head in ('import', '__import'):
imports.append(joined(items[1:]))
return
if head in NON_CODE_DECLARATIONS:
return
eq = next((i for i, it in enumerate(items) if not it.is_group and it.text == '='), len(items))
paren = next((i for i, it in enumerate(items[:eq]) if it.is_group and it.text == '('), -1)
if paren > 0:
name_idx = paren - 1
if items[name_idx].text == '>': # generic function: name<T>(...)
depth = 0
while name_idx >= 0:
depth += {'>': 1, '<': -1}.get(items[name_idx].text, 0)
if depth == 0:
break
name_idx -= 1
name_idx -= 1
if name_idx < 0 or items[name_idx].first.type is not TokenType.identifier:
return
before = items[:name_idx]
functions.append(
SlangFunction(
items[name_idx].text,
joined(it for it in before if it.text not in DECLARATION_MODIFIERS),
frozenset(it.text for it in before if it.text in DECLARATION_MODIFIERS),
tuple(attributes),
any(it.is_group and it.text == '{' for it in items[paren + 1 :]),
)
)
return
name_idx = next((i for i in range(eq - 1, -1, -1) if items[i].first.type is TokenType.identifier), -1)
if name_idx < 0:
return
type_items = [it for it in items[:name_idx] if it.text not in DECLARATION_MODIFIERS]
if not type_items:
return
end = terminator.start if terminator else items[-1].last.end
variables.append(
SlangVariable(
items[name_idx].text,
joined(type_items),
frozenset(it.text for it in items[:name_idx] if it.text in DECLARATION_MODIFIERS),
src[items[0].first.start : terminator.end if terminator else end],
type_items[0].first.start,
end,
)
)
items: list[_Item] = []
attributes: list[SlangAttribute] = []
i = 0
while i < len(tokens):
t = tokens[i]
if t.type is TokenType.punctuation:
if t.text == ';':
finish(items, attributes, t)
items, attributes = [], []
i += 1
continue
if t.text in CLOSING_BRACKETS:
j = group_end(i)
if t.text == '[' and not items:
attributes.append(parse_attribute(i, j))
else:
items.append(_Item(t, tokens[j]))
i = j + 1
if t.text == '{' and not any(not it.is_group and it.text == '=' for it in items):
# The end of a function or struct definition, as opposed to an initializer list
finish(items, attributes, None)
items, attributes = [], []
if i < len(tokens) and tokens[i].text == ';':
i += 1
continue
if t.text in (')', ']', '}'): # unbalanced, ignore
i += 1
continue
items.append(_Item(t, t))
i += 1
finish(items, attributes, None)
return SlangDeclarations(module, tuple(imports), tuple(functions), tuple(variables), tuple(comments))
class SlangShaderQuery:
"""Answers questions about the declarations in a shader as the compiler sees
them, that is, after preprocessing. Running the preprocessor needs slangc
and so is done only when the answer could depend on it."""
def __init__(self, src: bytes, name: str = '', include_dirs: Iterable[str] = (), invocation_tracker: set[tuple[str, ...]] | None = None):
self.src, self.name, self.include_dirs, self.invocation_tracker = src, name, tuple(include_dirs), invocation_tracker
self._identifiers: frozenset[str] | None = None
self._preprocessed: SlangDeclarations | None = None
@property
def identifiers(self) -> frozenset[str]:
"All identifiers in the source outside comments, including those in preprocessor directives"
if self._identifiers is None:
ans: set[str] = set()
q = [self.src.decode()]
while q:
for t in tokenize_slang(q.pop()):
if t.type is TokenType.identifier:
ans.add(t.text)
elif t.type is TokenType.directive:
q.append(t.text[1:])
self._identifiers = frozenset(ans)
return self._identifiers
@property
def preprocessed(self) -> SlangDeclarations:
if self._preprocessed is None:
cmd = list(slangc()) + ['-lang', 'slang', '-E']
for x in self.include_dirs:
cmd += ['-I', x]
cmd += ['--', '-']
if self.invocation_tracker is not None:
self.invocation_tracker.add(tuple(cmd))
cp = subprocess.run(cmd, input=self.src, capture_output=True)
if cp.returncode != 0:
raise SlangFailed(self.name or 'shader', cp)
self._preprocessed = parse_slang_declarations(cp.stdout.decode())
return self._preprocessed
def function(self, name: str) -> SlangFunction | None:
"The function with the specified name defined at the top level of the shader, if any"
if name not in self.identifiers: # avoid running the preprocessor
return None
return self.preprocessed.function(name)
# }}}
class SlangFile(NamedTuple):
path: str = ''
text: str = ''
@ -414,52 +735,30 @@ class SlangFile(NamedTuple):
def parse_slang_text(src_code: str, path: str = '') -> SlangFile:
text = re.sub(r'/\*[\s\S]*?\*/', '', src_code)
entry_points, imports = [], set()
module = ''
found_entry_point = ''
specializable_variables = {}
decls = parse_slang_declarations(src_code)
entry_points = []
for func in decls.functions:
for attr in func.attributes:
if attr.name == 'shader' and attr.args:
match attr.args[0]:
case 'vertex':
entry_points.append(EntryPoint(Stage.vertex, func.name))
case 'fragment' | 'pixel':
entry_points.append(EntryPoint(Stage.fragment, func.name))
specializable_variables = {v.name: v.declaration for v in decls.variables if {'extern', 'static', 'const'} <= v.modifiers}
disable_warnings = []
for line in text.splitlines():
line = line.strip()
if not line:
continue
if line.startswith('//'):
if line.startswith('// warnings-disable: '):
words = line.split()
for word in words[2:]:
for w in word.split(','):
disable_warnings.append(w)
continue
words = line.split()
if found_entry_point:
if words[0].startswith('['): # ]
continue
for q in words:
if '(' in q:
name = q.partition('(')[0] # ))
match found_entry_point:
case 'vertex':
entry_points.append(EntryPoint(Stage.vertex, name))
case 'fragment' | 'pixel':
entry_points.append(EntryPoint(Stage.fragment, name))
found_entry_point = ''
break
else:
match words[0]:
case 'module':
module = words[1].removesuffix(';')
case 'import':
imports.add(words[1].removesuffix(';'))
case 'extern':
if len(words) > 3 and words[1:3] == ['static', 'const']:
specializable_variables[line.partition('=')[0].split()[-1].rstrip(';')] = line
case _:
if words[0].startswith('[shader('): # ])
text = words[0].partition('(')[2].partition(')')[0].strip()
found_entry_point = text[1:-1]
for comment in decls.comments:
if comment.startswith('// warnings-disable: '):
for word in comment.split()[2:]:
disable_warnings.extend(word.split(','))
return SlangFile(
path, src_code, frozenset(imports), frozenset(entry_points), module, MappingProxyType(specializable_variables), frozenset(disable_warnings)
path,
src_code,
frozenset(decls.imports),
frozenset(entry_points),
decls.module,
MappingProxyType(specializable_variables),
frozenset(disable_warnings),
)
@ -1241,11 +1540,18 @@ def parse_var_directive(parts: list[str]) -> tuple[str, str, str]:
def apply_pipeline_specializations(src: bytes, merged_vars: dict[str, tuple[str, str]]) -> bytes:
for var_name, (var_type, value) in merged_vars.items():
# Replace: static const <any_type> <name> = <default>; → static const <type> <name> = <value>;
pattern = rf'(?m)^(\s*)static\s+const\s+{re.escape(var_type)}\s+{re.escape(var_name)}\s*=.+'
src = re.sub(pattern.encode(), rf'\g<1>static const {var_type} {var_name} = {value};'.encode(), src)
return src
if not merged_vars:
return src
text = src.decode()
# Replace: static const <any_type> <name> = <default>; → static const <type> <name> = <value>;
replacements = sorted(
(v.type_start, v.end, f'{merged_vars[v.name][0]} {v.name} = {merged_vars[v.name][1]}')
for v in parse_slang_declarations(text).variables
if v.name in merged_vars and {'static', 'const'} <= v.modifiers and 'extern' not in v.modifiers
)
for start, end, replacement in reversed(replacements):
text = text[:start] + replacement + text[end:]
return text.encode()
class Group(TypedDict):
@ -1430,28 +1736,48 @@ def merge_pipelines(pipelines: list[Pipeline]) -> Pipeline:
}
@run_once
def pointer_map_pat() -> re.Pattern[bytes]:
return re.compile(rb'\bfloat2\s+pointer_map\s*\(')
def group_moves_content(group: Group) -> bool:
"Whether whatever the shaders in a group do to the content is visible on the whole screen at all times"
return (
not group['animation_start']
and not group['attached']
and group['output_texture'] is NamedTexture.default
and group['viewport_pos'] == (0, 0)
and group['viewport_size'] == (1, 1)
)
def pointer_map_shaders(pipeline: Pipeline) -> tuple[tuple[int, int], ...]:
def pointer_map_shaders(pipeline: Pipeline, defines_pointer_map: Callable[[int, int], bool]) -> tuple[tuple[int, int], ...]:
"""The (group, shader) indices of the shaders whose pointer_map() decides
where pointer events land, in the order they must be applied. Only groups
that are always active and draw to the screen move content permanently, so
only they count. The last shader to run decides what is visible at a given
position, so it is applied first."""
that draw to the whole screen and are always active move content
permanently, so only they count. The last shader to run decides what is
visible at a given position, so it is applied first. defines_pointer_map()
is called with the (group, shader) indices of the shaders that count."""
ans: list[tuple[int, int]] = []
for g_idx, group in enumerate(pipeline['groups']):
if group['animation_start'] or group['attached'] or group['output_texture'] is not NamedTexture.default:
continue
for s_idx, name in enumerate(group['shaders']):
if pointer_map_pat().search(custom_shader(name, group['pipeline_dir'])[2]) is not None:
ans.append((g_idx, s_idx))
if group_moves_content(group):
ans.extend((g_idx, s_idx) for s_idx in range(len(group['shaders'])) if defines_pointer_map(g_idx, s_idx))
return tuple(reversed(ans))
def build_custom_shader_pipeline_ir(pipeline: Pipeline, cache_dir: str, invocation_tracker: set[tuple[str, ...]]) -> tuple[tuple[str, ...], str]:
def defines_pointer_map(query: SlangShaderQuery) -> bool:
if (func := query.function('pointer_map')) is None or not func.has_body:
return False
if 'public' not in func.modifiers:
raise ValueError(f'The pointer_map() function in the shader {query.name} must be declared public')
if not func.returns('float2', 'vector<float,2>'):
raise ValueError(f'The pointer_map() function in the shader {query.name} must return float2 not {func.return_type}')
return True
class PipelineIR(NamedTuple):
import_dirs: tuple[str, ...]
module_path: str
pointer_map_shaders: tuple[tuple[int, int], ...]
def build_custom_shader_pipeline_ir(pipeline: Pipeline, cache_dir: str, invocation_tracker: set[tuple[str, ...]]) -> PipelineIR:
slot = pipeline['slot']
slot_module_name = f'{slot.replace("-", "_")}'
cache_dir = os.path.join(cache_dir, 'c')
@ -1486,7 +1812,7 @@ def build_custom_shader_pipeline_ir(pipeline: Pipeline, cache_dir: str, invocati
mtime = max(mtime, os.stat(ct_key_path).st_mtime_ns)
types_rebuilt = True
slot_key_parts: list[str | bytes] = [json.dumps(pipeline, sort_keys=True), get_custom_shader_src('pipeline')]
slot_key_parts: list[str | bytes] = [json.dumps(pipeline, sort_keys=True), get_custom_shader_src('pipeline'), str(POINTER_MAP_SIZE)]
for group in pipeline['groups']:
for name in group['shaders']:
@ -1496,14 +1822,20 @@ def build_custom_shader_pipeline_ir(pipeline: Pipeline, cache_dir: str, invocati
slot_key_parts.append(content_key)
slot_key = key(*slot_key_parts)
slot_key_file = os.path.join(slot_dir, 'inputs.key')
# What was learned about the shaders when building, needed to use the module
slot_info_file = os.path.join(slot_dir, 'info.json')
ans = os.path.join(slot_dir, f'{slot}.slang-module')
if not types_rebuilt:
with suppress(FileNotFoundError), open(slot_key_file, 'rb') as f:
if f.read() == slot_key:
return tuple(import_dirs), ans
with suppress(OSError, ValueError, KeyError, TypeError), open(slot_info_file) as f:
info = json.load(f)
pm = tuple((int(g), int(s)) for g, s in info['pointer_map_shaders'])
return PipelineIR(tuple(import_dirs), ans, pm)
j = partial(os.path.join, slot_dir)
imports = []
queries: dict[tuple[int, int], SlangShaderQuery] = {}
for g_idx, group in enumerate(pipeline['groups']):
merged_vars = pipeline['vars'].copy()
merged_vars.update(group['vars'])
@ -1512,6 +1844,7 @@ def build_custom_shader_pipeline_ir(pipeline: Pipeline, cache_dir: str, invocati
if import_dir and import_dir not in import_dirs:
import_dirs.append(import_dir)
src = apply_pipeline_specializations(src, merged_vars)
queries[(g_idx, s_idx)] = SlangShaderQuery(src, name, (import_dir,) if import_dir else (), invocation_tracker)
modname = f'm_{g_idx}_{s_idx}'
imports.append(modname)
module_file = j(f'{modname}.slang-module')
@ -1539,22 +1872,25 @@ def build_custom_shader_pipeline_ir(pipeline: Pipeline, cache_dir: str, invocati
mod_src = get_custom_shader_src('pipeline').decode()
mod_src = mod_src.replace('// IMPORTS', '\n'.join(f'import {imp};' for imp in imports), 1)
mod_src = mod_src.replace('// PIPELINE', pipeline_code, 1)
pointer_map_code = '\n '.join(f'pos = {entry_point(g, s, "pointer_map")}(pos, csd);' for g, s in pointer_map_shaders(pipeline))
pm = pointer_map_shaders(pipeline, lambda g, s: defines_pointer_map(queries[(g, s)]))
pointer_map_code = '\n '.join(f'pos = {entry_point(g, s, "pointer_map")}(pos, csd);' for g, s in pm)
mod_src = mod_src.replace('// POINTER_MAP', pointer_map_code, 1)
# subprocess.run(['bat', '-P', '-l', 'cpp'], input=mod_src.encode())
with tempfile.TemporaryDirectory() as tdir:
for x in import_dirs:
inc.extend(('-I', x))
cmd = bc + inc + ['-module-name', slot_module_name, '-o', ans, '--', '-']
cmd = bc + inc + [f'-DPOINTER_MAP_SIZE={POINTER_MAP_SIZE}', '-module-name', slot_module_name, '-o', ans, '--', '-']
invocation_tracker.add(tuple(cmd))
cp = subprocess.run(cmd, cwd=tdir, capture_output=True, input=mod_src.encode())
if cp.returncode != 0:
raise SlangFailed(f'{slot}.slang', cp)
with open(slot_info_file, 'w') as f:
json.dump({'pointer_map_shaders': pm}, f)
with open(slot_key_file, 'wb') as f:
f.write(slot_key)
return tuple(import_dirs), ans
return PipelineIR(tuple(import_dirs), ans, pm)
def module_wrapper_for_slot(slot: str) -> bytes:
@ -1599,7 +1935,8 @@ def build_custom_shader_pipeline_glsl(
with lock_with_file(
os.path.join(cache_dir, 'lock'),
):
import_dirs, slang_module_path = build_custom_shader_pipeline_ir(pipeline, cache_dir, invocation_tracker)
pipeline_ir = build_custom_shader_pipeline_ir(pipeline, cache_dir, invocation_tracker)
import_dirs, slang_module_path = pipeline_ir.import_dirs, pipeline_ir.module_path
glsl_dir = os.path.join(os.path.dirname(os.path.dirname(slang_module_path)), 'glsl')
os.makedirs(glsl_dir, exist_ok=True)
module_mtime = safe_mtime(slang_module_path)
@ -1650,7 +1987,7 @@ def build_custom_shader_pipeline_glsl(
with open(vertex) as vf, open(fragment) as ff:
m = glsl_metadata_for_shader(metadata)
m['pipeline'] = pipeline
m['has_pointer_map'] = bool(pointer_map_shaders(pipeline))
m['has_pointer_map'] = bool(pipeline_ir.pointer_map_shaders)
return vf.read(), ff.read(), m

View file

@ -501,7 +501,7 @@ typedef struct BackgroundImageRenderSettings {
} BackgroundImageRenderSettings;
#define MAX_CUSTOM_SHADER_GROUPS 16
// Must match POINTER_MAP_SIZE in pipeline.slang
// Passed to the custom shader pipeline as a preprocessor define by slang.py
#define POINTER_MAP_SIZE 64
// Summary of the custom shader animation state machine, cached once per frame
@ -809,6 +809,7 @@ monotonic_t update_custom_shader_animations(unsigned event_mask, monotonic_t now
void init_shader_animation_state(OSWindow *os_window);
bool custom_shader_needs_render(const ShaderAnimState *before, const ShaderAnimState *after, unsigned event_mask, monotonic_t now);
void map_pointer_position(const OSWindow *os_window, double *x, double *y);
void set_os_window_mouse_position(OSWindow *window, double x, double y);
void swap_window_buffers(OSWindow *w);
void take_screenshot_of_rectangular_region(OSWindow *os_window, Region region, unsigned char *dst_buf, unsigned *thumb_w, unsigned *thumb_h, bool no_scaling);
bool current_framebuffer_is_ok(void);

View file

@ -15,13 +15,17 @@ from kitty.shaders.slang import (
EntryPoint,
LoadShaderPrograms,
SlangFile,
SlangShaderQuery,
Stage,
apply_pipeline_specializations,
build_custom_shader_pipeline_glsl,
build_import_graph,
clear_caches,
custom_shader,
defines_pointer_map,
get_custom_pipeline_src,
parse_pipeline_definition,
parse_slang_declarations,
parse_slang_text,
parse_var_directive,
pipeline_definition,
@ -395,7 +399,8 @@ fsMain(VertexOutput vo) : SV_Target { return float4(0); }
def test_pointer_map_shaders(self):
def pms(src: str) -> tuple[tuple[int, int], ...]:
return pointer_map_shaders(parse_pipeline_definition(src.splitlines(), 'test'))
p = parse_pipeline_definition(src.splitlines(), 'test')
return pointer_map_shaders(p, lambda g, s: p['groups'][g]['shaders'][s] == 'crt')
self.ae(pms('startgroup\nshaders crt\nendgroup'), ((0, 0),))
self.ae(pms('startgroup\nshaders sample\nendgroup'), ())
@ -405,6 +410,101 @@ fsMain(VertexOutput vo) : SV_Target { return float4(0); }
self.ae(pms('startgroup\nanimation_start tab-change\nshaders crt\nendgroup'), ())
self.ae(pms('startgroup\nshaders sample\nendgroup\nstartgroup\nattach\nshaders crt\nendgroup'), ())
self.ae(pms('textures a\nstartgroup\nshaders crt\noutput_texture a\nendgroup\nstartgroup\nshaders sample\nendgroup'), ())
# Groups that draw only part of the screen do not move pointer events everywhere
self.ae(pms('startgroup\nshaders crt\nviewport_size 0.5 1\nendgroup\nstartgroup\nshaders sample\nendgroup'), ())
self.ae(pms('startgroup\nshaders crt\nviewport_pos 0.5 0\nendgroup\nstartgroup\nshaders sample\nendgroup'), ())
def test_slang_declarations(self):
d = parse_slang_declarations("""
#language slang 2026
module m;
import a.b;
// float2 commented_out(float2 pos) { return pos; }
/* float2 also_commented_out(float2 pos) */
static const float4 TINT = float4(0, 0.8, // the tint
0.6, 1);
static const float ARR[2] = {1, 2};
extern static const bool FLAG;
struct S { float2 not_top_level(float2 p) { return p; } };
public float2
pointer_map(
float2 pos, KittyCustomShaderData d) : SV_Target {
static const float local = 1;
return pos;
}
vector<float, 2> generic<T>(T x) { return float2(0); }
float2 prototype(float2 p);
[shader("fragment")] [numthreads(1, 1, 1)]
float4 frag() { string s = "float2 in_string(float2 p) {}"; return float4(0); }
""")
self.ae(d.module, 'm')
self.ae(d.imports, ('a.b',))
self.ae([f.name for f in d.functions], ['pointer_map', 'generic', 'prototype', 'frag'])
f = d.function('pointer_map')
assert f is not None
self.ae((f.return_type, f.modifiers, f.has_body), ('float2', frozenset({'public'}), True))
self.ae(d.function('generic').return_type, 'vector<float,2>')
self.assertFalse(d.function('prototype').has_body)
self.ae(d.function('frag').attributes, (('shader', ('fragment',)), ('numthreads', ('1', '1', '1'))))
self.ae(
[(v.name, v.type, v.modifiers) for v in d.variables],
[
('TINT', 'float4', frozenset({'static', 'const'})),
('ARR', 'float', frozenset({'static', 'const'})),
('FLAG', 'bool', frozenset({'extern', 'static', 'const'})),
],
)
self.ae(d.variable('FLAG').declaration, 'extern static const bool FLAG;')
# # not at the start of a line is not a directive
self.ae([f.name for f in parse_slang_declarations('float a = 1; # float2 f(float2 p) { return p; }').functions], ['f'])
def test_apply_pipeline_specializations(self):
src = b"""static const float4 TINT = float4(0, 0.8,
0.6, 1); // keep me
// static const float SCAN = 0.5;
static const float SCAN = 0.5; static const float WARP=1;
extern static const bool FLAG = false;
float f() { static const float SCAN = 2; return SCAN; }
"""
self.ae(apply_pipeline_specializations(src, {}), src)
merged_vars = {'TINT': ('float3', '0.1'), 'SCAN': ('float', '1'), 'WARP': ('float', '2'), 'FLAG': ('bool', 'true')}
self.ae(
apply_pipeline_specializations(src, merged_vars).decode(),
"""static const float3 TINT = 0.1; // keep me
// static const float SCAN = 0.5;
static const float SCAN = 1; static const float WARP = 2;
extern static const bool FLAG = false;
float f() { static const float SCAN = 2; return SCAN; }
""",
)
def test_shader_query(self):
if not shutil.which(slangc()[0]):
self.skipTest(f'slangc ({slangc()[0]}) not found in PATH')
def q(src: str) -> SlangShaderQuery:
return SlangShaderQuery(src.encode(), 'test')
def has_pm(src: str) -> bool:
return defines_pointer_map(q(src))
self.assertTrue(has_pm(custom_shader('crt')[2].decode()))
self.assertFalse(has_pm(custom_shader('sample')[2].decode()))
self.assertTrue(has_pm('public float2\npointer_map(float2 pos, KittyCustomShaderData d) { return pos; }'))
self.assertTrue(has_pm('public vector<float, 2> pointer_map(float2 pos, KittyCustomShaderData d) { return pos; }'))
# Comments, disabled code and strings do not count
self.assertFalse(has_pm('// public float2 pointer_map(float2 pos, KittyCustomShaderData d) { return pos; }'))
self.assertFalse(has_pm('#if 0\npublic float2 pointer_map(float2 pos, KittyCustomShaderData d) { return pos; }\n#endif'))
self.assertFalse(has_pm('float2 f(float2 pos) { return pointer_map(pos); }'))
# The preprocessor is applied
self.assertTrue(has_pm('#define PM pointer_map\npublic float2 PM(float2 pos, KittyCustomShaderData d) { return pos; }'))
# Mistakes are reported rather than ignored
self.assertRaises(ValueError, has_pm, 'float2 pointer_map(float2 pos, KittyCustomShaderData d) { return pos; }')
self.assertRaises(ValueError, has_pm, 'public float3 pointer_map(float2 pos, KittyCustomShaderData d) { return float3(pos, 0); }')
# The preprocessor is run only when the answer could depend on it
tracker: set[tuple[str, ...]] = set()
self.assertIsNone(SlangShaderQuery(b'float4 f() { return float4(0); }', '', (), tracker).function('pointer_map'))
self.assertFalse(tracker)
def test_sample_pointer_map(self):
import struct