Commit graph

43 commits

Author SHA1 Message Date
Kovid Goyal
3b2d4e7e37
Bump generated MSL version to that used on macOS 14 2026-11-19 19:57:34 +05:30
Kovid Goyal
6a297b6f06
Add a Metal GPU driver and an MSL shader target
On macOS, kitty now tries to render using Metal, falling back to OpenGL,
see the gpu_driver option. The driver has not yet been compiled or run
on macOS, it was written on Linux and only syntax checked against stub
headers.

Shader targets of the form msl-NN (for MSL N.N) compile each program to
a single MSL source file containing the entry points for all stages,
compiled by the driver at runtime, for the built-in and custom shaders.
slangc ignores [[vk::binding]] for Metal, so the program metadata keeps
the kitty binding slots and passes the argument table indices assigned
by slangc as native names. The push constant block, emitted as an
ordinary constant buffer, is recognized by the name of its type. As
Metal has y=1 at the top in NDC, the generated code is fixed up to
negate the y coordinate of the position output by vertex entry points.
Layouts that would differ because bool is a single byte in MSL are
rejected. The MSL is validated with the Metal compiler when available.
The MSL shaders are temporarily built on all platforms, for development.

The driver follows the design of the Vulkan driver: command buffers with
serials and at most three in flight, copy on write of buffer storage the
GPU may still use, private textures uploaded with blits from staging
memory, pipeline variants created when first used and a CAMetalLayer
per window. Vertex inputs whose offset or stride is not a multiple of
four bytes, as Metal requires, are repacked into a cached copy.
2026-11-19 19:57:33 +05:30
Kovid Goyal
6da5508145
Add a SPIR-V shader target for the built-in and custom shaders
Shader targets of the form spirv-NN (for SPIR-V N.N) compile each
program to a single SPIR-V module containing the entry points for all
stages, along with the same backend neutral program metadata as for
GLSL. The compiled modules are validated with spirv-val, when it is
available. Custom shaders are compiled at runtime for SPIR-V targets as
well, and the tests compile and validate all bundled custom shaders for
SPIR-V in addition to the target of the loaded driver.

The program metadata now includes the names of the entry points, which
are passed to drivers in GPUShaderSource, since a module can contain
the entry points of several stages. compile_program() accepts binary
shaders as bytes.

This replaces the unused, untargeted .spv files that were built before.
2026-11-19 19:57:33 +05:30
Kovid Goyal
cac2f0da42
Use the coordinate conventions of Vulkan and Metal for the GPU API
Pixel coordinates of viewports, scissor rectangles and read back pixels
now have their origin at the top left of the render target, rows are
read back top to bottom, NDC y=-1 is the top of the viewport and render
targets have their top row at texture coordinate t=0, the same as
uploaded images. Vulkan and Metal drivers can then render with no flips
at all.

The OpenGL driver is now the special case. Framebuffer objects already
behave this way, so it converts only when rendering to a window: vertex
shaders negate gl_Position.y via a kitty_flip_y uniform that the GLSL
fixup adds, viewport and scissor y coordinates are converted, rows of
read back pixels are reversed and copying to a texture uses a flipping
blit.

kitty's code gets simpler: viewports and scissor rectangles no longer
need the framebuffer height, screenshots no longer need their rows
flipped and textures produced by rendering are sampled the same way as
uploaded ones. The rounded rect shader no longer uses SV_Position, whose
origin differs between backends.

User facing custom shader UV coordinates are unchanged, still with
their origin at the bottom left.

Rendering is pixel identical to before.
2026-11-19 19:57:33 +05:30
Kovid Goyal
67dac47940
Pass the uniform parameters of shaders in a single push constant block
Declare the uniform parameters of every program as a single Params
struct, shared by all stages of the program, in a push constant block,
instead of as uniform parameters of the entry points. Slang lays out
the uniform parameters of each entry point starting at offset zero, so
the vertex and fragment parameters overlap, which Vulkan does not allow
in push constants. A single block shared by all stages is also what
Metal wants, and it is still emitted as loose uniforms for OpenGL.

The build now fails if a program has entry point uniform parameters or
more than 128 bytes of them, the push constant size guaranteed by Vulkan.

Since a program now has at most one block of uniform parameters, drop
the stage from GPUParamBlock, bump the GPU API version, and simplify
the lookup of parameters by name in kitty, as each name is now present
in only one parameter block.
2026-11-19 19:57:33 +05:30
Kovid Goyal
8adf263398
Make shader targets driver defined and program metadata reflection based
Each GPU driver now declares the shader target it consumes (glsl-140 for
the OpenGL driver), a fixed property available as soon as the driver is
loaded. setup.py compiles the built-in shaders for the targets of the
drivers it builds, into a directory per target, and custom shaders are
compiled at runtime for the target of the loaded driver. The GLSL_VERSION
constant is no longer exported to Python.

The program metadata passed to the C code is now generated from the
reflection data produced by slangc and is backend neutral: vertex input
locations, uniform names, uniform block sizes and array layouts, all
identified by their names in the Slang source. Block sizes are verified
against the driver. The only target specific part is a map from Slang
names to the identifiers in the compiled code, which the OpenGL driver
needs to look up parameters by name and which is passed to the driver
when creating a pipeline. The driver no longer needs to be queried for
uniform array layouts.

The debug config output now uses the name reported by the driver.
2026-11-19 19:57:33 +05:30
Kovid Goyal
87d06354dd
Custom shaders: Fix focus driven groups ending in the wrong state after hide/unhide
Shader animation events are accumulated per OS window and only consumed
when the window is next rendered. A focus-out followed by a focus-in with
no rendered frame in between, as happens when the window is hidden and
shown again, or a brief focus steal within one repaint interval, left both
bits pending. The state machine then treated them as simultaneous, so the
focus-out hard stop prevented os-window-focus-in groups from starting and
groups started by focus-out were turned on while focused.

Now a focus change replaces any pending opposite focus change, so only the
final focus state is reported. Genuinely simultaneous events, such as a
click that moves focus away, still give focus-out priority.

Closes #10871
2026-11-15 08:39:25 +05:30
Kovid Goyal
bc9d0b87e3
Custom shaders: Add command and alternate screen events and uniforms
Add the command-start, command-finish, alternate-screen-enter and
alternate-screen-exit events, which fire for changes in the active window,
and the active_window_command_running, active_window_alternate_screen,
active_window_command_state_age and active_window_alternate_screen_age
fields to KittyCustomShaderData, so shaders can show effects while
commands run and hide them in full screen programs.
2026-11-05 08:43:37 +05:30
Kovid Goyal
f9b57ba283
Allow trailing comments in shader pipeline files\n\nFixes #10718
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run
2026-10-22 15:50:55 +05:30
Kovid Goyal
bcc6627322
Custom shaders: Add animation_duration, animation_reverse and animation_loops
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run
animation_duration sets how long animation_progress takes to rise to one,
after which it stays there, so effects can fade in and stay on. Previously
groups with no stop duration, such as toggled ones, always had a progress
of zero.

animation_reverse makes a group play its animation in reverse when it stops,
for any reason, instead of turning off immediately. With yes it takes as
long as animation_duration, or a number of milliseconds can be given.
Stopping part way through rising reverses from the current progress and
starting again while reversing rises from there.

animation_loops, used with animation_reverse, plays the animation forwards
and in reverse again after the first reverse, without holding at one, for
effects such as blinking a few times.

Also fix attached groups not having the same animation_progress as the group
they are attached to, as documented, and explain why the per-group values
are in KittyTextures rather than KittyCustomShaderData.

Fixes #10693
2026-10-19 07:57:46 +05:30
Kovid Goyal
a2b7721bfb
Fix #10694 2026-10-18 21:03:47 +05:30
Kovid Goyal
7dbfb0e1ce
Custom shaders: Use pointer_map() of groups turned on and off by events
Groups that are active until an event turns them off, such as those
using animation_toggle, now also move pointer events, while they are
active. The pointer map is recomputed only when the set of active such
groups changes, and pointer events are left alone once all of them have
been turned off.
2026-10-15 21:27:18 +05:30
Kovid Goyal
28aad4345e
Custom shaders: Allow controlling shaders with key presses
Add a shader_event action that sends user:<name> events to the custom
shader pipeline, and an animation_toggle pipeline directive to turn
groups on and off with an event. User events occupy the bits above the
builtin events in a now 64 bit event mask, so firing them costs no more
than any other event and groups that are off are not drawn at all.

Fixes #10667
2026-10-15 20:56:31 +05:30
Jesse Miller
c57495436d Skip foreground samples for blank cells
The cell fragment shader sampled the glyph, underline, strike, cursor, and underline-exclusion texels for every cell. A space is sprite 0, and that sprite is empty, so those samples add nothing. The vertex shader now sets skip_text when the glyph, both decoration indices, and the cursor sprite are all zero. The fragment writes the background it already computed and skips the samples. A block cursor is sprite 0, so its color stays in the background. An underline, strike, URL underline, or beam, underline, or hollow cursor still samples. The foreground-only pass writes zero for a skipped cell.

On Linux, platform text composition is gamma 1 and contrast 0, which is pow(alpha, 1) * 1. TEXT_CONTRAST_IDENTITY compiles that function down to returning the color unchanged. legacy and any other numbers keep the old math. A config reload that crosses that line rebuilds the cell programs. The identity flag is only varied when the new gamma path is on, so the cell programs go from 18 to 27.
2026-10-11 10:38:17 -06:00
Kovid Goyal
be4fef614b
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
2026-10-10 11:04:08 +05:30
Iurii Lunev
30e07a5b09 Custom shaders: Allow shaders that move content to remap pointer events
A shader that draws content away from where kitty put it, such as crt
curving the screen, makes clicks and selections land on the wrong cells,
since kitty maps the pointer to the unmodified cell grid. Up to several
columns near the edges with the bundled crt shader.

Shaders can now define pointer_map(pos, d), returning where the content
shown at pos actually is. kitty renders it into a 64x64 grid when the
pipeline or viewport size changes, reads it back once, and uses it to
move all pointer events. Only always active groups that draw to the
screen are used, since only they move content permanently.

The crt shader defines it, sharing its warp function with fragment_main().
2026-10-09 23:31:29 +03:00
Kovid Goyal
79c2efd8ce
Particle effects for cursor trail shaders
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run
Fixes #10587
2026-10-02 10:05:57 +05:30
Kovid Goyal
03c278a661
also fix pipeline file retention in frozen builds and add test for it 2026-09-24 09:49:33 +05:30
Kovid Goyal
d37f845293
cleanup previous PR fixing one regression and doing code cleanups 2026-09-16 12:57:51 +05:30
白鸽
18023eaafa Custom shaders: Clear cached programs when disabling shaders 2026-09-14 22:41:26 +08:00
白鸽
7b41e33399 Custom shaders: Redraw static effects only on demand 2026-09-14 22:41:26 +08:00
白鸽
cf33820973 Custom shaders: Support tinting complete inactive windows 2026-09-14 22:41:26 +08:00
Kovid Goyal
953ac19e77
Autoformat slang code 2026-08-10 22:29:18 +05:30
Kovid Goyal
28b7c238dc
Use a fresh cache dir for shader compilation test 2026-08-10 12:51:11 +05:30
Kovid Goyal
c97b71e66b
Nicer test cache dir name 2026-08-10 12:38:15 +05:30
Kovid Goyal
e7d41ff7f4
Fix loading of pipeline files by absolute path 2026-08-10 10:16:37 +05:30
Kovid Goyal
17e577f75b
Add a test to check that all custom shaders build 2026-08-07 18:49:56 +05:30
Kovid Goyal
ed025b37af
fix specialisation not working 2026-08-05 08:17:05 +05:30
Kovid Goyal
d6712dc695
... 2026-08-05 08:05:40 +05:30
Kovid Goyal
dd2c59d255
autoformat 2026-08-05 07:16:05 +05:30
Kovid Goyal
81e7d4f1a7
Test to insure that custom shader compilation uses its cache 2026-08-05 05:33:00 +05:30
Kovid Goyal
ae347ec014
allow specialisation of custom shaders at link time 2026-08-05 05:32:59 +05:30
Kovid Goyal
98e2de1a98
move to using a pipeline definition file for custom shaders 2026-08-05 05:32:58 +05:30
Kovid Goyal
5431baa422
More work on custom shaders 2026-08-05 05:32:57 +05:30
Kovid Goyal
16297903f6
test for compiling custom shader to glsl 2026-08-05 05:32:57 +05:30
Kovid Goyal
4b15314267
compile builtin shaders in dependency layers 2026-08-05 05:32:56 +05:30
Kovid Goyal
7f0b072f0d
autoformat 2026-08-05 05:32:56 +05:30
copilot-swe-agent[bot]
a31f9e52f2
Refactor kitty_tests: move __init__.py code to base.py, update all imports 2026-08-05 05:32:54 +05:30
Kovid Goyal
879697e4c5
Move specialisations into compiler code
Allows for dynamic specialisations
2026-08-05 05:32:49 +05:30
Kovid Goyal
9261295448
Fix failing tests 2026-08-05 05:32:49 +05:30
copilot-swe-agent[bot]
a36b259d45
Fix parser to strip semicolons from module/import names; update tests 2026-08-05 05:32:49 +05:30
copilot-swe-agent[bot]
d8041066bf
Add slang parser corner case tests and implement test_slang_ordering 2026-08-05 05:32:49 +05:30
Kovid Goyal
dbfa720387
Work on generating build tree for slang files 2026-08-05 05:32:49 +05:30