From d7cd5a18c2d537f51e961f8fcffef260d812d7ff Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 26 Sep 2026 12:36:01 +0530 Subject: [PATCH] Cleanup previous PR on motion blur Move it into a separate shader --- docs/changelog.rst | 3 + docs/custom-shaders.rst | 10 +- kitty/cursor_trail.c | 17 +- kitty/options/definition.py | 5 +- kitty/shaders.c | 6 +- .../shaders/custom/cursor-trail-default.slang | 195 ++++-------------- .../custom/cursor-trail-motion-blur.pipeline | 5 + .../custom/cursor-trail-motion-blur.slang | 148 +++++++++++++ kitty/shaders/custom/demo.py | 6 +- kitty/state.h | 5 +- 10 files changed, 219 insertions(+), 181 deletions(-) create mode 100644 kitty/shaders/custom/cursor-trail-motion-blur.pipeline create mode 100644 kitty/shaders/custom/cursor-trail-motion-blur.slang diff --git a/docs/changelog.rst b/docs/changelog.rst index 9604f912b..eaec5d11e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -214,6 +214,9 @@ Detailed list of changes - Custom shaders: The builtin :code:`dim-inactive-windows` shader no longer dims the tab bar and the padding, border and margin around the active window (:iss:`10524`) +- Custom shaders: A new :code:`cursor-trail-motion-blur` shader that makes + the cursor trail glide smoothly during fast cursor movements + - Vertical tabs: Improve rendering of tabs when using the ``powerline`` :opt:`tab_bar_style` - Graphics: Fix images briefly disappearing for one frame the first time an image is replaced during a synchronized update (:iss:`10538`) diff --git a/docs/custom-shaders.rst b/docs/custom-shaders.rst index 54f2f09e4..e59df7367 100644 --- a/docs/custom-shaders.rst +++ b/docs/custom-shaders.rst @@ -29,9 +29,13 @@ Cursor trails You must have enabled cursor trails with something like ``cursor_trail 1`` in :file:`kitty.conf` for these shaders to take effect. -The bundled ``cursor-trail-default`` shader provides temporally supersampled motion -blur and spatial antialiasing. Enable it with :code:`custom_shaders cursor-trail-default`; -its compile-time rendering options are documented in the shader source. +When the cursor moves quickly, the trail can appear to hop from place to place +instead of gliding. The ``cursor-trail-motion-blur`` shader smooths this out +the way a camera does, by blurring the trail slightly along the direction it +is moving, and it also smooths the trail's jagged edges. Enable it with +:code:`custom_shaders cursor-trail-motion-blur`. The options it supports are +explained at the top of the :repo_file:`shader file +`. .. include:: generated/custom-shaders-cursor-trails.rst diff --git a/kitty/cursor_trail.c b/kitty/cursor_trail.c index 9263461bf..85bd21310 100644 --- a/kitty/cursor_trail.c +++ b/kitty/cursor_trail.c @@ -160,12 +160,9 @@ update_cursor_trail_needs_render(CursorTrail *ct, Window *w, ndc_coords g) { bool update_cursor_trail(CursorTrail *ct, Window *w, monotonic_t now, OSWindow *os_window) { - if (ct->has_previous_position) { - for (int i = 0; i < 4; i++) { - ct->previous_corner_x[i] = ct->corner_x[i]; - ct->previous_corner_y[i] = ct->corner_y[i]; - } - } + // remember where the trail was drawn in the previous frame, for shaders that do motion blur + memcpy(ct->previous_corner_x, ct->corner_x, sizeof(ct->previous_corner_x)); + memcpy(ct->previous_corner_y, ct->corner_y, sizeof(ct->previous_corner_y)); ct->target_updated = false; ndc_coords g = { .xstart = gl_pos_x(w->render_data.geometry.left, os_window->viewport_width), @@ -185,14 +182,6 @@ update_cursor_trail(CursorTrail *ct, Window *w, monotonic_t now, OSWindow *os_wi bool needs_render_prev = ct->needs_render; update_cursor_trail_needs_render(ct, w, g); - if (!ct->has_previous_position) { - for (int i = 0; i < 4; i++) { - ct->previous_corner_x[i] = ct->corner_x[i]; - ct->previous_corner_y[i] = ct->corner_y[i]; - } - ct->has_previous_position = true; - } - ct->updated_at = now; // returning true here will cause the cells to be drawn diff --git a/kitty/options/definition.py b/kitty/options/definition.py index 7e4aa0b13..c554b5b36 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -459,9 +459,8 @@ for cursors that rapidly change their positions during UI updates in complex app See :opt:`cursor_trail_decay` to control the animation speed and :opt:`cursor_trail_start_threshold` to control when a cursor trail is started. You can also have different styles of trail by using the :opt:`custom_shaders` option, for example: :code:`custom_shaders cursor-trail-blaze`. -The bundled :code:`cursor-trail-default` shader provides temporally supersampled motion blur -and spatial antialiasing; enable it with :code:`custom_shaders cursor-trail-default` and see -the shader source for its compile-time options. +To make fast cursor movements look smoother, use :code:`custom_shaders cursor-trail-motion-blur`, +which blurs the trail slightly along the direction it is moving. """, ) diff --git a/kitty/shaders.c b/kitty/shaders.c index 806334ff1..82ec10f96 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -2587,10 +2587,8 @@ run_custom_end_shader(OSWindow *os_window, float sx, float sy, monotonic_t now) for (int i = 0; i < 4; i++) { d->cursor_trail_corners_x[i] = NDC_TO_UV(ct->corner_x[i]); d->cursor_trail_corners_y[i] = NDC_TO_UV(ct->corner_y[i]); - if (ct->has_previous_position) { - d->cursor_trail_prev_corners_x[i] = NDC_TO_UV(ct->previous_corner_x[i]); - d->cursor_trail_prev_corners_y[i] = NDC_TO_UV(ct->previous_corner_y[i]); - } + d->cursor_trail_prev_corners_x[i] = NDC_TO_UV(ct->previous_corner_x[i]); + d->cursor_trail_prev_corners_y[i] = NDC_TO_UV(ct->previous_corner_y[i]); } d->cursor_trail_edge[0] = NDC_TO_UV(ct->cursor_edge_x[0]); // left d->cursor_trail_edge[1] = NDC_TO_UV(ct->cursor_edge_x[1]); // right diff --git a/kitty/shaders/custom/cursor-trail-default.slang b/kitty/shaders/custom/cursor-trail-default.slang index 513f03176..9a446d3aa 100644 --- a/kitty/shaders/custom/cursor-trail-default.slang +++ b/kitty/shaders/custom/cursor-trail-default.slang @@ -2,168 +2,57 @@ // Copyright (C) 2026 Kovid Goyal // Distributed under terms of the GPLv3 license. // -// The default custom cursor trail. Set custom_shaders cursor-trail-default -// to use this shader instead of kitty's built-in cursor trail renderer. +// Custom cursor trail shader — replicates the built-in trail using the +// cursor_trail_* uniforms. Use as a starting point for custom effects. // -// The options below are compile-time shader options. They can be changed -// here, or overridden from cursor-trail-default.pipeline with `var` lines. -// For example: -// var bool MOTION_BLUR true -// var int MOTION_BLUR_SAMPLES 64 +// Suggested pipeline config: +// startgroup +// animation_start cursor-trail-move +// animation_stop cursor-trail-stop +// shaders cursor-trail +// endgroup // -// MOTION_BLUR integrates the moving trail over time. ANTIALIASING integrates -// coverage over a regular subpixel grid. Both are enabled by default here; -// set either to false to disable that part of the effect. The sample counts -// are capped at 256 to keep accidental settings from producing an unusable -// shader. CONNECTED makes the blur terminate at the target cursor; when false -// it preserves kitty's original disconnected, lagging-cursor appearance. +// Note: animation_progress is not tied to the trail duration (which is +// an exponential decay of variable length). Use cursor_trail_color.a +// for opacity and the corner positions for geometry instead. import kitty_custom_shader_types; -// Integrate the trail between the previous and current animated positions. -static const bool MOTION_BLUR = true; -// Estimate pixel coverage using a regular grid of subpixel samples. -static const bool ANTIALIASING = true; -// Number of positions integrated per frame. Valid range is 1..256. -static const int MOTION_BLUR_SAMPLES = 64; -// Number of subpixel samples per frame. Valid range is 1..256. -static const int ANTIALIASING_SAMPLES = 16; -// If true, integrate to the target cursor rectangle; otherwise preserve the -// original disconnected, lagging-cursor shape. -static const bool CONNECTED = false; -// Opacity floor for the motion-blurred trail, in the range 0..1. -static const float MIN_OPACITY = 0.2; -// Fractions of the distance from the target over which the trail becomes -// white. The blend uses a smooth radial S-curve and reaches white at the end. -static const float TARGET_BLEND_START = 0.1; -static const float TARGET_BLEND_END = 0.02; - -// Corner order: top-right, bottom-right, bottom-left, top-left. -static bool -inside_triangle(float2 p, float2 a, float2 b, float2 c) { - float ab = (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x); - float bc = (c.x - b.x) * (p.y - b.y) - (c.y - b.y) * (p.x - b.x); - float ca = (a.x - c.x) * (p.y - c.y) - (a.y - c.y) * (p.x - c.x); - return ab <= 0.0 && bc <= 0.0 && ca <= 0.0; -} - -static float -trail_coverage(float2 p, float4 xs, float4 ys) { - float2 c0 = float2(xs.x, ys.x); - float2 c1 = float2(xs.y, ys.y); - float2 c2 = float2(xs.z, ys.z); - float2 c3 = float2(xs.w, ys.w); - // Match the GL_TRIANGLE_FAN used by kitty's original trail exactly. - return (inside_triangle(p, c0, c1, c2) || inside_triangle(p, c0, c2, c3)) ? 1.0 : 0.0; -} - -static bool -inside_rect(float2 p, float4 edge) { - return p.x >= edge.x && p.x <= edge.y && p.y >= edge.w && p.y <= edge.z; -} - -static float -distance_to_rect_pixels(float2 p, float4 edge, float2 pixel_dims) { - float2 lo = float2(edge.x, edge.w) * pixel_dims; - float2 hi = float2(edge.y, edge.z) * pixel_dims; - float2 px = p * pixel_dims; - float2 delta = max(max(lo - px, px - hi), float2(0.0)); - return length(delta); +// Returns true when uv is inside the convex quad c0→c1→c2→c3 (clockwise, y-up). +// Corner order from cursor_trail_corners_*: 0=top-right, 1=bottom-right, 2=bottom-left, 3=top-left. +bool +inside_trail_quad(float2 uv, float2 c0, float2 c1, float2 c2, float2 c3) { + float2 e0 = c1 - c0; + float2 e1 = c2 - c1; + float2 e2 = c3 - c2; + float2 e3 = c0 - c3; + float s0 = e0.x * (uv.y - c0.y) - e0.y * (uv.x - c0.x); + float s1 = e1.x * (uv.y - c1.y) - e1.y * (uv.x - c1.x); + float s2 = e2.x * (uv.y - c2.y) - e2.y * (uv.x - c2.x); + float s3 = e3.x * (uv.y - c3.y) - e3.y * (uv.x - c3.x); + return s0 <= 0.0 && s1 <= 0.0 && s2 <= 0.0 && s3 <= 0.0; } public float4 fragment_main(float4 color, KittyTextures t, KittyCustomShaderData d) { - if (d.cursor_trail_color.a <= 0.0 || d.cursor_trail_state <= 0.0) return color; - - float2 pixel_dims = float2(d.viewport_size_pixels) * t.viewport.zw; - pixel_dims = max(pixel_dims, float2(1.0)); - float2 pixel_size = 1.0 / pixel_dims; - - const int temporal_samples = MOTION_BLUR ? min(max(MOTION_BLUR_SAMPLES, 1), 256) : 1; - const int spatial_samples = ANTIALIASING ? min(max(ANTIALIASING_SAMPLES, 1), 256) : 1; - const int grid_size = ANTIALIASING ? int(ceil(sqrt(float(spatial_samples)))) : 1; - - float4 end_x = d.cursor_trail_corners_x; - float4 end_y = d.cursor_trail_corners_y; - if (MOTION_BLUR && CONNECTED) { - // Connected mode ends at the target cursor rectangle. - end_x = float4(d.cursor_trail_edge.y, d.cursor_trail_edge.y, d.cursor_trail_edge.x, d.cursor_trail_edge.x); - end_y = float4(d.cursor_trail_edge.z, d.cursor_trail_edge.w, d.cursor_trail_edge.w, d.cursor_trail_edge.z); - } - - float4 start_x = MOTION_BLUR ? d.cursor_trail_prev_corners_x : end_x; - float4 start_y = MOTION_BLUR ? d.cursor_trail_prev_corners_y : end_y; - - // Custom end shaders run over the whole viewport. Reject pixels outside - // the swept trail before entering the expensive temporal/spatial loops. - float2 swept_min = float2(1.0, 1.0); - float2 swept_max = float2(0.0, 0.0); - for (int i = 0; i < 4; i++) { - float2 start = float2(start_x[i], start_y[i]); - float2 end = float2(end_x[i], end_y[i]); - swept_min = min(swept_min, min(start, end)); - swept_max = max(swept_max, max(start, end)); - } - swept_min -= pixel_size * 0.5; - swept_max += pixel_size * 0.5; - if (t.pos.x < swept_min.x || t.pos.x > swept_max.x || t.pos.y < swept_min.y || t.pos.y > swept_max.y) return color; - - float2 start_center = float2((start_x.x + start_x.y + start_x.z + start_x.w) * 0.25, (start_y.x + start_y.y + start_y.z + start_y.w) * 0.25); - float2 end_center = float2((end_x.x + end_x.y + end_x.z + end_x.w) * 0.25, (end_y.x + end_y.y + end_y.z + end_y.w) * 0.25); - float motion_distance = length((end_center - start_center) * pixel_dims); - - float blend_start = max(clamp(TARGET_BLEND_START, 0.0, 1.0), clamp(TARGET_BLEND_END, 0.0, 1.0)); - float blend_end = min(clamp(TARGET_BLEND_START, 0.0, 1.0), clamp(TARGET_BLEND_END, 0.0, 1.0)); - float start_radius = motion_distance * blend_start; - float end_radius = motion_distance * blend_end; - float target_blend = 0.0; - if (MOTION_BLUR && start_radius > end_radius && motion_distance > 0.0) { - float distance = distance_to_rect_pixels(t.pos, d.cursor_trail_edge, pixel_dims); - // Apply the radial blend before coverage so antialiased edges never - // become opaque merely because they are close to the target. - target_blend = 1.0 - smoothstep(end_radius, start_radius, distance); - } - - float coverage_sum = 0.0; - for (int ti = 0; ti < temporal_samples; ti++) { - float time = MOTION_BLUR ? (float(ti) + 0.5) / float(temporal_samples) : 1.0; - float4 xs = lerp(start_x, end_x, time); - float4 ys = lerp(start_y, end_y, time); - float2 sample_min = float2(1.0, 1.0); - float2 sample_max = float2(0.0, 0.0); - for (int ci = 0; ci < 4; ci++) { - float2 corner = float2(xs[ci], ys[ci]); - sample_min = min(sample_min, corner); - sample_max = max(sample_max, corner); - } - sample_min -= pixel_size * 0.5; - sample_max += pixel_size * 0.5; - if (t.pos.x < sample_min.x || t.pos.x > sample_max.x || t.pos.y < sample_min.y || t.pos.y > sample_max.y) continue; - - float sample_coverage = 0.0; - for (int si = 0; si < spatial_samples; si++) { - float2 sample_pos = t.pos; - if (ANTIALIASING) { - int sx = si % grid_size; - int sy = si / grid_size; - sample_pos += (float2(float(sx) + 0.5, float(sy) + 0.5) / float(grid_size) - 0.5) * pixel_size; - } - // Preserve the terminal-rendered cursor, including hollow and - // blinking cursor states. The trail never paints its rectangle. - if (d.cursor_color.a > 0.0 && inside_rect(sample_pos, d.cursor_trail_edge)) continue; - sample_coverage += trail_coverage(sample_pos, xs, ys); - } - coverage_sum += sample_coverage / float(spatial_samples); - } - float coverage = coverage_sum / float(temporal_samples); - if (coverage <= 0.0) return color; - float opacity = d.cursor_trail_color.a; - if (MOTION_BLUR) { - float min_opacity = clamp(MIN_OPACITY, 0.0, 1.0); - opacity = min_opacity + (1.0 - min_opacity) * max(opacity, d.cursor_color.a); - opacity = lerp(opacity, 1.0, target_blend); - } - opacity = clamp(opacity * coverage, 0.0, 1.0); + if (opacity <= 0.0) return color; + + float2 uv = t.pos; + + float2 c0 = float2(d.cursor_trail_corners_x.x, d.cursor_trail_corners_y.x); // top-right + float2 c1 = float2(d.cursor_trail_corners_x.y, d.cursor_trail_corners_y.y); // bottom-right + float2 c2 = float2(d.cursor_trail_corners_x.z, d.cursor_trail_corners_y.z); // bottom-left + float2 c3 = float2(d.cursor_trail_corners_x.w, d.cursor_trail_corners_y.w); // top-left + + if (!inside_trail_quad(uv, c0, c1, c2, c3)) return color; + + // Mask out the cursor's current rectangle so the real cursor renders cleanly on top. + // cursor_trail_edge: .x=left, .y=right, .z=top, .w=bottom (UV, y-up) + float inside_cursor = + step(d.cursor_trail_edge.x, uv.x) * step(uv.x, d.cursor_trail_edge.y) * step(d.cursor_trail_edge.w, uv.y) * step(uv.y, d.cursor_trail_edge.z); + opacity *= 1.0 - inside_cursor; + if (opacity <= 0.0) return color; + return float4(lerp(color.rgb, d.cursor_trail_color.rgb, opacity), color.a); } diff --git a/kitty/shaders/custom/cursor-trail-motion-blur.pipeline b/kitty/shaders/custom/cursor-trail-motion-blur.pipeline new file mode 100644 index 000000000..a438cfc00 --- /dev/null +++ b/kitty/shaders/custom/cursor-trail-motion-blur.pipeline @@ -0,0 +1,5 @@ +startgroup + animation_start cursor-trail-move + animation_stop cursor-trail-stop + shaders cursor-trail-motion-blur +endgroup diff --git a/kitty/shaders/custom/cursor-trail-motion-blur.slang b/kitty/shaders/custom/cursor-trail-motion-blur.slang new file mode 100644 index 000000000..0c50b2b09 --- /dev/null +++ b/kitty/shaders/custom/cursor-trail-motion-blur.slang @@ -0,0 +1,148 @@ +#language slang 2026 +// Copyright (C) 2026 Kovid Goyal +// Distributed under terms of the GPLv3 license. +// +// A smoother cursor trail, using motion blur. +// +// When the cursor jumps to a new place, kitty draws a trail that stretches +// from where the cursor was towards where it is now, and then quickly shrinks +// into the cursor. The screen is redrawn many times a second and each redraw +// shows the trail in a new position. When the cursor moves fast, those +// positions are far apart, so the trail can look like it is hopping from place +// to place instead of gliding. +// +// This shader smooths that out the same way a camera does. A photo of +// something moving fast comes out streaked, because the object kept moving +// while the shutter was open. Similarly, every time the screen is redrawn, +// this shader draws the trail at many in-between positions, from where it was +// in the previous redraw to where it is now, and blends them together. The +// result is a soft streak that looks like continuous motion. It also smooths +// the slanted edges of the trail, so they look clean rather than jagged +// (antialiasing). +// +// To use it, add the following to kitty.conf: +// cursor_trail 1 +// custom_shaders cursor-trail-motion-blur +// +// You can tune the options below without editing this file. Save a pipeline +// file such as shaders/my-trail.pipeline in your kitty config directory, +// containing, for example: +// startgroup +// animation_start cursor-trail-move +// animation_stop cursor-trail-stop +// var bool CONNECTED true +// var int MOTION_BLUR_SAMPLES 32 +// shaders cursor-trail-motion-blur +// endgroup +// and then use custom_shaders my-trail in kitty.conf. + +import kitty_custom_shader_types; + +// Turn motion blur on or off. When off, only antialiasing is applied. +static const bool MOTION_BLUR = true; +// Number of in-between positions blended per redraw. More is smoother but +// slower. Values are limited to the range 1..64. +static const int MOTION_BLUR_SAMPLES = 16; +// Smooth the edges of the trail. +static const bool ANTIALIASING = true; +// When true, the blur reaches all the way into the cursor, so the trail looks +// like one continuous streak. When false, the trail keeps kitty's usual look, +// where its tail lags slightly behind the cursor. +static const bool CONNECTED = false; + +// All positions below are in pixels. + +// How much of the pixel centered at p lies outside the edge a→b, from 0 (none +// of it) to 1 (all of it). orientation is +1 for triangles whose corners are +// in counter-clockwise order and -1 for clockwise. +static float +outside_edge(float2 p, float2 a, float2 b, float orientation) { + float2 e = b - a; + // Signed distance from the edge, positive on the inner side + float dist = orientation * (e.x * (p.y - a.y) - e.y * (p.x - a.x)) / length(e); + if (!ANTIALIASING) return dist < 0.0 ? 1.0 : 0.0; + return 1.0 - saturate(0.5 + dist); +} + +// The fraction of the pixel centered at p that is covered by the triangle abc. +// Subtracting the part of the pixel outside each edge is exact for straight +// edges and also stays correct for slivers thinner than a pixel. +static float +triangle_coverage(float2 p, float2 a, float2 b, float2 c) { + float area = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); + // A triangle that has collapsed to a line or point covers nothing + if (abs(area) < 1e-6) return 0.0; + float orientation = area > 0.0 ? 1.0 : -1.0; + return saturate(1.0 - outside_edge(p, a, b, orientation) - outside_edge(p, b, c, orientation) - outside_edge(p, c, a, orientation)); +} + +// The fraction of the pixel centered at p covered by the trail. Corner order +// is top-right, bottom-right, bottom-left, top-left. The quad is split into +// the same two triangles as kitty's built-in trail renderer. +static float +trail_coverage(float2 p, float4 xs, float4 ys) { + float2 c0 = float2(xs.x, ys.x); + float2 c1 = float2(xs.y, ys.y); + float2 c2 = float2(xs.z, ys.z); + float2 c3 = float2(xs.w, ys.w); + return min(triangle_coverage(p, c0, c1, c2) + triangle_coverage(p, c0, c2, c3), 1.0); +} + +// The fraction of the pixel centered at p covered by the axis aligned +// rectangle from lo to hi. +static float +rect_coverage(float2 p, float2 lo, float2 hi) { + if (!ANTIALIASING) return (p.x >= lo.x && p.x <= hi.x && p.y >= lo.y && p.y <= hi.y) ? 1.0 : 0.0; + float2 overlap = saturate(min(p + 0.5, hi) - max(p - 0.5, lo)); + return overlap.x * overlap.y; +} + +static float +min4(float4 v) { + return min(min(v.x, v.y), min(v.z, v.w)); +} + +static float +max4(float4 v) { + return max(max(v.x, v.y), max(v.z, v.w)); +} + +public float4 +fragment_main(float4 color, KittyTextures t, KittyCustomShaderData d) { + float opacity = d.cursor_trail_color.a; + if (opacity <= 0.0 || d.cursor_trail_state <= 0.0) return color; + + float2 pixel_dims = max(float2(d.viewport_size_pixels) * t.viewport.zw, float2(1.0)); + float2 p = t.pos * pixel_dims; + // cursor_trail_edge: .x=left, .y=right, .z=top, .w=bottom + float2 cursor_lo = float2(d.cursor_trail_edge.x, d.cursor_trail_edge.w) * pixel_dims; + float2 cursor_hi = float2(d.cursor_trail_edge.y, d.cursor_trail_edge.z) * pixel_dims; + + float4 end_x = d.cursor_trail_corners_x * pixel_dims.x; + float4 end_y = d.cursor_trail_corners_y * pixel_dims.y; + if (MOTION_BLUR && CONNECTED) { + end_x = float4(cursor_hi.x, cursor_hi.x, cursor_lo.x, cursor_lo.x); + end_y = float4(cursor_hi.y, cursor_lo.y, cursor_lo.y, cursor_hi.y); + } + float4 start_x = MOTION_BLUR ? d.cursor_trail_prev_corners_x * pixel_dims.x : end_x; + float4 start_y = MOTION_BLUR ? d.cursor_trail_prev_corners_y * pixel_dims.y : end_y; + + // This runs for every pixel on screen, so skip pixels that are outside + // the area swept by the trail in this redraw. + float2 swept_lo = float2(min(min4(start_x), min4(end_x)), min(min4(start_y), min4(end_y))) - 1.0; + float2 swept_hi = float2(max(max4(start_x), max4(end_x)), max(max4(start_y), max4(end_y))) + 1.0; + if (p.x < swept_lo.x || p.x > swept_hi.x || p.y < swept_lo.y || p.y > swept_hi.y) return color; + + const int samples = MOTION_BLUR ? clamp(MOTION_BLUR_SAMPLES, 1, 64) : 1; + float coverage = 0.0; + for (int i = 0; i < samples; i++) { + float time = MOTION_BLUR ? (float(i) + 0.5) / float(samples) : 1.0; + coverage += trail_coverage(p, lerp(start_x, end_x, time), lerp(start_y, end_y, time)); + } + coverage /= float(samples); + // Never paint over the cursor itself, so that it renders cleanly on top. + coverage *= 1.0 - rect_coverage(p, cursor_lo, cursor_hi); + if (coverage <= 0.0) return color; + + return float4(lerp(color.rgb, d.cursor_trail_color.rgb, saturate(opacity * coverage)), color.a); +} diff --git a/kitty/shaders/custom/demo.py b/kitty/shaders/custom/demo.py index c63f63e80..1cfde020f 100755 --- a/kitty/shaders/custom/demo.py +++ b/kitty/shaders/custom/demo.py @@ -146,7 +146,11 @@ metadata: dict[str, dict[str, Any]] = { 'pond-ripple': {'animate': pond_ripple, 'category': 'mouse', 'tagline': 'Clicking is like throwing stones in a pond.'}, 'spotlight': {'animate': spotlight, 'category': 'mouse', 'tagline': 'Spotlight your mouse pointer as it moves around.'}, # Cursor trail - 'cursor-trail-default': {'animate': cursor_trail, 'category': 'cursor-trail', 'tagline': 'A smooth, antialiased cursor trail with temporal motion blur.'}, + 'cursor-trail-motion-blur': { + 'animate': cursor_trail, + 'category': 'cursor-trail', + 'tagline': 'Make the cursor trail glide smoothly, like a fast moving object caught on camera.', + }, 'cursor-trail-blaze': {'animate': cursor_trail, 'category': 'cursor-trail', 'tagline': 'Set your cursor on fire as it moves around.'}, 'cursor-trail-lightning': {'animate': cursor_trail, 'category': 'cursor-trail', 'tagline': 'Make your cursor shoot lightning as it moves around.'}, # Navigation diff --git a/kitty/state.h b/kitty/state.h index 0575a01ee..c4753b584 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -439,13 +439,12 @@ typedef struct CursorTrail { float opacity; float corner_x[4]; float corner_y[4]; - float previous_corner_x[4]; - float previous_corner_y[4]; + float previous_corner_x[4]; // corner_x as of the previous frame + float previous_corner_y[4]; // corner_y as of the previous frame float cursor_edge_x[2]; float cursor_edge_y[2]; float prev_cursor_edge_x[2]; // cursor_edge_x before the most recent cursor move float prev_cursor_edge_y[2]; // cursor_edge_y before the most recent cursor move - bool has_previous_position; } CursorTrail; typedef struct Tab {