diff --git a/crates/project/src/configuration.rs b/crates/project/src/configuration.rs index 2fee1bef1c..3bc1413179 100644 --- a/crates/project/src/configuration.rs +++ b/crates/project/src/configuration.rs @@ -1100,11 +1100,11 @@ impl ScreenMovementSpring { /// How far ahead of its segment an early zoom starts moving. pub const EARLY_START_SECS: f64 = 0.35; - /// New projects' zoom motion: reaches 90% of a zoom in 317 ms with no - /// overshoot and settles within 1% by 500 ms. + /// New projects' zoom motion: critically damped, reaching 50% of a zoom + /// in 123 ms and 90% in 290 ms, with no overshoot. pub const NEW_PROJECT: Self = Self { - stiffness: 205.0, - damping: 33.3, + stiffness: 286.0, + damping: 42.2, mass: 1.5, start_zooms_early: false, }; diff --git a/crates/rendering/src/layers/cursor.rs b/crates/rendering/src/layers/cursor.rs index e4fe588766..ee5d0cb172 100644 --- a/crates/rendering/src/layers/cursor.rs +++ b/crates/rendering/src/layers/cursor.rs @@ -24,6 +24,8 @@ const CURSOR_IDLE_RESUME_LOOKAHEAD_MS: f64 = 250.0; /// it at 60fps). const CURSOR_VECTOR_CAP: f32 = 480.0; const CURSOR_BASELINE_FPS: f32 = 60.0; +/// Below a pixel of travel per frame the smear is the identity. +const CURSOR_ACTIVATION_PX: f32 = 1.0; const CURSOR_MULTIPLIER: f32 = 1.0; /// Upper bound on amount * (fps/60). Generous: it must not clip the fps /// normalization at high frame rates (120fps needs 2x to keep the real-time @@ -474,7 +476,10 @@ impl CursorLayer { let fps_scale = fps / CURSOR_BASELINE_FPS; let cursor_strength = (uniforms.motion_blur_amount * CURSOR_MULTIPLIER * fps_scale) .clamp(0.0, CURSOR_MAX_STRENGTH); - let child_motion = uniforms + // How far the cursor moved on the frame since the previous frame: + // its own travel over the screen plus however far the screen carried + // it (a pan, a zoom). + let frame_motion = uniforms .prev_cursor .as_ref() .filter(|prev| prev.cursor_id == interpolated_cursor.cursor_id) @@ -483,14 +488,23 @@ impl CursorLayer { .display .source_uv_to_target(interpolated_cursor.position.coord); let previous = uniforms.display.source_uv_to_target(prev.position.coord); - (current - previous).map(|value| value as f32) + let own = (current - previous).map(|value| value as f32); + let carried = uniforms.display_motion.map_or(XY::new(0.0, 0.0), |motion| { + motion.carried(previous.map(|value| value as f32)) + }); + own + carried }) .unwrap_or_else(|| XY::new(0.0, 0.0)); let combined_motion_px = if cursor_strength <= f32::EPSILON { XY::new(0.0, 0.0) } else { - child_motion + cursor_own_motion( + frame_motion, + uniforms + .display_motion + .map_or(XY::new(0.0, 0.0), |motion| motion.size_delta), + ) }; // Length-based semantics: smear length = per-frame travel x amount, @@ -611,6 +625,9 @@ impl CursorLayer { cursor_opacity, ); + let (screen_blur, screen_card, screen_reach) = + screen_blur_over_cursor(&uniforms.display, position_size); + let cursor_grade = if uniforms.project.color_correction.grade_cursor { uniforms.screen_color_grade } else { @@ -647,6 +664,9 @@ impl CursorLayer { color_adjust_a: cursor_grade.color_adjust_a, color_adjust_b: cursor_grade.color_adjust_b, grain_params: cursor_grade.grain_params, + screen_blur, + screen_card, + screen_reach: [screen_reach, 0.0, 0.0, 0.0], }; constants.queue.write_buffer( @@ -763,6 +783,88 @@ fn finite_positive_or(value: f32, fallback: f32) -> f32 { } } +/// The motion the cursor's own blur follows. The cursor rides in a +/// screen-sized box at its position, so the box's centre moves with the +/// cursor plus half the screen's change in size; when the size change is the +/// larger, the frame is a zoom, and the screen's zoom blur is the cursor's +/// only blur. Under a pixel is none. +fn cursor_own_motion(frame_motion: XY, screen_size_delta: XY) -> XY { + let length = |v: XY| (v.x * v.x + v.y * v.y).sqrt(); + let centre_motion = frame_motion + screen_size_delta * 0.5; + if length(screen_size_delta) > length(centre_motion) + || length(centre_motion) < CURSOR_ACTIVATION_PX + { + XY::new(0.0, 0.0) + } else { + centre_motion + } +} + +/// Same ceiling as the display's radial blur (composite-video-frame.wgsl). +const MAX_ZOOM_RAY_UV: f32 = 0.10; + +/// The screen's blur this frame, for the cursor that rides inside it, as +/// `cursor.wgsl` reads it: the mode and its parameters, the card in output +/// px, and how far a zoom blur can carry the sprite (output px). Mirrors the +/// gates in `composite-video-frame.wgsl`. +fn screen_blur_over_cursor( + display: &crate::composite_frame::CompositeVideoFrameUniforms, + sprite: [f32; 4], +) -> ([f32; 4], [f32; 4], f32) { + let card = [ + display.target_bounds[0], + display.target_bounds[1], + display.target_size[0].max(1.0), + display.target_size[1].max(1.0), + ]; + let [mode, strength, zoom_amount, outward] = display.motion_blur_params; + if mode < 0.5 || strength < 0.001 { + return ([0.0; 4], card, 0.0); + } + if mode < 1.5 { + let [x, y] = display.motion_blur_vector; + if (x * x + y * y).sqrt() < 1e-5 { + return ([0.0; 4], card, 0.0); + } + return ([1.0, x * card[2], y * card[3], 0.0], card, 0.0); + } + let amount = zoom_amount.min(1.0); + if amount < 1e-4 { + return ([0.0; 4], card, 0.0); + } + let [center_x, center_y] = display.motion_blur_zoom_center; + // The longest ray from the sprite's corners, with room to spare. + let reach = [ + (sprite[0], sprite[1]), + (sprite[0] + sprite[2], sprite[1]), + (sprite[0], sprite[1] + sprite[3]), + (sprite[0] + sprite[2], sprite[1] + sprite[3]), + ] + .into_iter() + .map(|(x, y)| { + let dx = center_x - (x - card[0]) / card[2]; + let dy = center_y - (y - card[1]) / card[3]; + let distance = (dx * dx + dy * dy).sqrt(); + if distance < 1e-4 { + return 0.0; + } + let ray = (distance * amount).min(MAX_ZOOM_RAY_UV) / distance; + ((dx * ray * card[2]).powi(2) + (dy * ray * card[3]).powi(2)).sqrt() + }) + .fold(0.0f32, f32::max); + // A negative amount carries the display's outward taps (a zoom in). + let signed_amount = if outward > 0.5 { + -zoom_amount + } else { + zoom_amount + }; + ( + [2.0, center_x, center_y, signed_amount], + card, + reach * 1.25 + 2.0, + ) +} + fn cursor_blur_vector(motion: XY, strength: f32) -> XY { clamp_cursor_vector(motion * strength) } @@ -789,6 +891,10 @@ pub struct CursorUniforms { color_adjust_a: [f32; 4], color_adjust_b: [f32; 4], grain_params: [f32; 4], + /// The screen's blur this frame (see `cursor.wgsl`). + screen_blur: [f32; 4], + screen_card: [f32; 4], + screen_reach: [f32; 4], } fn compute_cursor_idle_opacity( @@ -1142,6 +1248,75 @@ impl CursorTexture { mod tests { use super::*; + #[test] + fn a_pan_smears_the_cursor_with_the_screen() { + // The cursor holds still on a screen panning 30px right: it moves + // 30px on the frame, so it smears 30px like the screen under it. + assert_eq!( + cursor_own_motion(XY::new(30.0, 0.0), XY::new(0.0, 0.0)), + XY::new(30.0, 0.0) + ); + // Its own travel adds to the pan's. + assert_eq!( + cursor_own_motion(XY::new(30.0, 12.0), XY::new(0.0, 0.0)), + XY::new(30.0, 12.0) + ); + // Under a pixel is no smear. + assert_eq!( + cursor_own_motion(XY::new(0.6, 0.0), XY::new(0.0, 0.0)), + XY::new(0.0, 0.0) + ); + } + + #[test] + fn a_zoom_leaves_the_cursor_to_the_screens_zoom_blur() { + // The screen grows faster than the cursor's box moves: a zoom, so + // the cursor has no smear of its own. + assert_eq!( + cursor_own_motion(XY::new(4.0, 3.0), XY::new(80.0, 45.0)), + XY::new(0.0, 0.0) + ); + // A fast flick through a slow zoom is a move, measured at the box's + // centre (half the size change added). + assert_eq!( + cursor_own_motion(XY::new(120.0, 0.0), XY::new(8.0, 4.0)), + XY::new(124.0, 2.0) + ); + } + + #[test] + fn the_screen_blur_reaches_the_cursor_in_output_pixels() { + let mut display = crate::composite_frame::CompositeVideoFrameUniforms { + target_bounds: [100.0, 50.0, 1100.0, 550.0], + target_size: [1000.0, 500.0], + ..bytemuck::Zeroable::zeroed() + }; + let sprite = [600.0, 300.0, 40.0, 40.0]; + + // No screen blur. + assert_eq!(screen_blur_over_cursor(&display, sprite).0, [0.0; 4]); + + // A pan of 0.02 x 0.01 of the card is 20 x 5 px. + display.motion_blur_params = [1.0, 1.0, 0.0, 0.0]; + display.motion_blur_vector = [0.02, 0.01]; + let (blur, card, reach) = screen_blur_over_cursor(&display, sprite); + assert_eq!(blur, [1.0, 20.0, 5.0, 0.0]); + assert_eq!(card, [100.0, 50.0, 1000.0, 500.0]); + assert_eq!(reach, 0.0); + + // A zoom about the card's centre reaches the sprite by its rays. + display.motion_blur_params = [2.0, 1.0, 0.04, 0.0]; + display.motion_blur_zoom_center = [0.5, 0.5]; + let (blur, _, reach) = screen_blur_over_cursor(&display, sprite); + assert_eq!(blur, [2.0, 0.5, 0.5, 0.04]); + // The farthest corner, (640, 340), is (40, 40) px from the centre + // (600, 300): its ray is 0.04 of that, 2.26 px, padded. + assert!( + (reach - (2.0f32.sqrt() * 40.0 * 0.04 * 1.25 + 2.0)).abs() < 1e-3, + "{reach}" + ); + } + fn alpha_coverage(level: &CursorMipLevel) -> f64 { let alpha_sum: u64 = level .rgba diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs index 0b787bd26a..7db6c325f2 100644 --- a/crates/rendering/src/lib.rs +++ b/crates/rendering/src/lib.rs @@ -3403,7 +3403,10 @@ pub struct ProjectUniforms { /// follows `split`. pub takeover: Option, pub resolution_base: XY, - pub display_parent_motion_px: XY, + /// How the display card moved since the previous frame, per frame; + /// `None` on the first frame and across a zoom snap. The cursor rides + /// inside the card, so its blur is measured with this motion added. + pub display_motion: Option, pub motion_blur_amount: f32, pub masks: Vec, pub texts: Vec, @@ -3516,13 +3519,6 @@ impl MotionBounds { (size.x * size.x + size.y * size.y).sqrt().max(f64::EPSILON) } - fn contains(&self, other: &Self) -> bool { - self.start.coord.x <= other.start.coord.x - && self.start.coord.y <= other.start.coord.y - && self.end.coord.x >= other.end.coord.x - && self.end.coord.y >= other.end.coord.y - } - fn point_to_uv(&self, point: XY) -> XY { let size = self.size(); XY::new( @@ -3534,18 +3530,6 @@ impl MotionBounds { fn top_left(&self) -> XY { self.start.coord } - - fn top_right(&self) -> XY { - XY::new(self.end.coord.x, self.start.coord.y) - } - - fn bottom_left(&self) -> XY { - XY::new(self.start.coord.x, self.end.coord.y) - } - - fn bottom_right(&self) -> XY { - self.end.coord - } } #[derive(Clone, Copy, Debug, Default)] @@ -3558,6 +3542,8 @@ struct MotionAnalysis { size_delta_px: f32, zoom_center_uv: XY, zoom_magnitude: f32, + /// The frame is larger than it was: a zoom in. + zoom_growing: bool, /// Forces the zoom (radial) branch regardless of dominance — used by /// scene transitions that inject synthetic zoom blur. prefer_zoom: bool, @@ -3566,14 +3552,39 @@ struct MotionAnalysis { #[derive(Clone, Copy, Debug)] struct MotionBlurComputation { descriptor: MotionBlurDescriptor, - parent_movement_px: XY, +} + +/// The display card this frame and how it changed since the previous one, +/// in output px per frame. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct DisplayMotionDelta { + /// `[x0, y0, x1, y1]`. + pub bounds: [f32; 4], + pub start_delta: XY, + pub size_delta: XY, +} + +impl DisplayMotionDelta { + /// How far the card carried a point at `position` (output px, this + /// frame) since the previous frame. + pub fn carried(&self, position: XY) -> XY { + let width = (self.bounds[2] - self.bounds[0]).max(f32::EPSILON); + let height = (self.bounds[3] - self.bounds[1]).max(f32::EPSILON); + let relative = XY::new( + (position.x - self.bounds[0]) / width, + (position.y - self.bounds[1]) / height, + ); + XY::new( + self.start_delta.x + relative.x * self.size_delta.x, + self.start_delta.y + relative.y * self.size_delta.y, + ) + } } impl MotionBlurComputation { fn none() -> Self { Self { descriptor: MotionBlurDescriptor::none(), - parent_movement_px: XY::new(0.0, 0.0), } } } @@ -3613,57 +3624,43 @@ fn analyze_motion(current: &MotionBounds, previous: &MotionBounds) -> MotionAnal ((curr_diag - prev_diag).abs() / prev_diag) as f32 }; - let zoom_center_point = if previous.contains(current) { - previous.center() - } else { - zoom_vanishing_point(current, previous).unwrap_or(previous.center()) - }; + // The radial blur converges on the point the frame scales about, for a + // zoom in and a zoom out alike. + let zoom_center_point = zoom_fixed_point(current, previous).unwrap_or(previous.center()); analysis.movement_px = movement_px; analysis.movement_uv = movement_uv; analysis.movement_magnitude = movement_magnitude; analysis.size_delta_px = size_delta_px; analysis.zoom_magnitude = zoom_magnitude; - let clamp_uv = |v: f32| { - if v.is_finite() { - v.clamp(0.0, 1.0) - } else { - 0.5 - } - }; + analysis.zoom_growing = curr_diag > prev_diag; + // Not clamped to the card: a zoom that also pans scales about a point + // off the card, and the rays still converge on it. let zoom_center_uv = current.point_to_uv(zoom_center_point); - analysis.zoom_center_uv = XY::new(clamp_uv(zoom_center_uv.x), clamp_uv(zoom_center_uv.y)); + analysis.zoom_center_uv = if zoom_center_uv.x.is_finite() && zoom_center_uv.y.is_finite() { + zoom_center_uv + } else { + XY::new(0.5, 0.5) + }; analysis } -fn zoom_vanishing_point(current: &MotionBounds, previous: &MotionBounds) -> Option> { - line_intersection( - previous.top_left(), - current.top_left(), - previous.bottom_right(), - current.bottom_right(), - ) - .or_else(|| { - line_intersection( - previous.top_right(), - current.top_right(), - previous.bottom_left(), - current.bottom_left(), - ) - }) -} - -fn line_intersection(a1: XY, a2: XY, b1: XY, b2: XY) -> Option> { - let denom = (a1.x - a2.x) * (b1.y - b2.y) - (a1.y - a2.y) * (b1.x - b2.x); - if denom.abs() <= f64::EPSILON { +/// The point that stays put while the frame scales from `previous` to +/// `current`: where every corner's path meets. Solved per axis from +/// `x' = s * x + t` (`s` the diagonal ratio), so it holds for every target, +/// including one on the frame's diagonal where pairing opposite corners' +/// paths gives two collinear lines and no single crossing. +fn zoom_fixed_point(current: &MotionBounds, previous: &MotionBounds) -> Option> { + let previous_diagonal = previous.diagonal(); + if previous_diagonal <= f64::EPSILON { return None; } - - let a_det = a1.x * a2.y - a1.y * a2.x; - let b_det = b1.x * b2.y - b1.y * b2.x; - let x = (a_det * (b1.x - b2.x) - (a1.x - a2.x) * b_det) / denom; - let y = (a_det * (b1.y - b2.y) - (a1.y - a2.y) * b_det) / denom; - Some(XY::new(x, y)) + let scale = current.diagonal() / previous_diagonal; + if !scale.is_finite() || (1.0 - scale).abs() <= 1e-9 { + return None; + } + let point = (current.top_left() - previous.top_left() * scale) / (1.0 - scale); + (point.x.is_finite() && point.y.is_finite()).then_some(point) } fn clamp_vector(vec: XY, max_len: f32) -> XY { @@ -3714,7 +3711,12 @@ fn resolve_motion_descriptor( if zoom_amount <= f32::EPSILON { return MotionBlurDescriptor::none(); } - MotionBlurDescriptor::zoom(analysis.zoom_center_uv, zoom_amount, 1.0) + MotionBlurDescriptor::zoom( + analysis.zoom_center_uv, + zoom_amount, + 1.0, + analysis.zoom_growing, + ) } else { let move_strength = base_amount * move_multiplier; if move_strength <= 0.0 || move_px < MOTION_ACTIVATION_PX { @@ -3883,6 +3885,9 @@ struct MotionBlurDescriptor { movement_vector_uv: [f32; 2], zoom_center_uv: [f32; 2], zoom_amount: f32, + /// The frame grew this frame: the radial taps run away from the zoom + /// centre, so the smear trails the motion as it does zooming out. + zoom_outward: bool, } impl Default for MotionBlurDescriptor { @@ -3893,6 +3898,7 @@ impl Default for MotionBlurDescriptor { movement_vector_uv: [0.0, 0.0], zoom_center_uv: [0.5, 0.5], zoom_amount: 0.0, + zoom_outward: false, } } } @@ -3909,18 +3915,25 @@ impl MotionBlurDescriptor { movement_vector_uv: [vector_uv.x, vector_uv.y], zoom_center_uv: [0.5, 0.5], zoom_amount: 0.0, + zoom_outward: false, } } - fn zoom(center_uv: XY, zoom_amount: f32, strength: f32) -> Self { + fn zoom(center_uv: XY, zoom_amount: f32, strength: f32, outward: bool) -> Self { Self { mode: MotionBlurMode::Zoom, strength, movement_vector_uv: [0.0, 0.0], zoom_center_uv: [center_uv.x, center_uv.y], zoom_amount, + zoom_outward: outward, } } + + /// The uniform's fourth parameter: 1 when the radial taps run outward. + fn zoom_direction(&self) -> f32 { + if self.zoom_outward { 1.0 } else { 0.0 } + } } /// Final rendered placement of the display and camera layers in output-frame @@ -4459,19 +4472,7 @@ impl ProjectUniforms { DISPLAY_MOVE_MULTIPLIER, DISPLAY_ZOOM_MULTIPLIER, ); - let move_px = (analysis.movement_px.x * analysis.movement_px.x - + analysis.movement_px.y * analysis.movement_px.y) - .sqrt(); - let parent_vector = if move_px >= MOTION_ACTIVATION_PX { - analysis.movement_px - } else { - XY::new(0.0, 0.0) - }; - - MotionBlurComputation { - descriptor, - parent_movement_px: parent_vector, - } + MotionBlurComputation { descriptor } } fn camera_zoom_factor( @@ -5044,7 +5045,7 @@ impl ProjectUniforms { let mut camera3d_zoom: Option = None; let ( display, - display_motion_parent, + display_motion_delta, display_slowest_motion_px, frame_chrome, display_outer_bounds, @@ -5119,7 +5120,20 @@ impl ProjectUniforms { ) }; let descriptor = display_motion.descriptor; - let display_parent_motion_px = display_motion.parent_movement_px; + let display_motion_delta = (has_previous && !zoom_snapped_in_window).then(|| { + let span = motion_frame_span.max(1.0); + DisplayMotionDelta { + bounds: [start.x as f32, start.y as f32, end.x as f32, end.y as f32], + start_delta: XY::new( + ((start.x - prev_start.x) as f32) / span, + ((start.y - prev_start.y) as f32) / span, + ), + size_delta: XY::new( + (((end.x - start.x) - (prev_end.x - prev_start.x)) as f32) / span, + (((end.y - start.y) - (prev_end.y - prev_start.y)) as f32) / span, + ), + } + }); let display_slowest_motion_px = if has_previous && !zoom_snapped_in_window { screen_enhance::slowest_pixel_motion( [start.x as f32, start.y as f32, end.x as f32, end.y as f32], @@ -5188,6 +5202,15 @@ impl ProjectUniforms { final_target_bounds[2] - final_target_bounds[0], final_target_bounds[3] - final_target_bounds[1], ]; + // The cursor is placed on the card after the split and takeover + // morphs, which the zoom bounds above know nothing about; while + // either moves the card, its carried motion is not the zoom's. + let display_motion_delta = display_motion_delta.filter(|_| { + final_target_bounds + .iter() + .zip(base_target_bounds) + .all(|(placed, zoomed)| (placed - zoomed).abs() < 0.01) + }); let final_min_axis = final_target_size[0].min(final_target_size[1]) as f64; let display_rounding_px = @@ -5284,7 +5307,7 @@ impl ProjectUniforms { descriptor.mode.as_f32(), descriptor.strength, descriptor.zoom_amount, - 0.0, + descriptor.zoom_direction(), ], shadow: if decorated { project.background.shadow @@ -5418,7 +5441,7 @@ impl ProjectUniforms { descriptor.mode.as_f32(), descriptor.strength, descriptor.zoom_amount, - 0.0, + descriptor.zoom_direction(), ], shadow: project.background.shadow * display_decoration_fade @@ -5459,7 +5482,7 @@ impl ProjectUniforms { enhance_region: [0.0; 4], enhance_params: [0.0; 4], }, - display_parent_motion_px, + display_motion_delta, display_slowest_motion_px, frame_chrome, display_outer_bounds, @@ -5662,7 +5685,7 @@ impl ProjectUniforms { camera_descriptor.mode.as_f32(), camera_descriptor.strength, camera_descriptor.zoom_amount, - 0.0, + camera_descriptor.zoom_direction(), ], shadow: project.camera.shadow * chrome_fade @@ -5762,6 +5785,7 @@ impl ProjectUniforms { XY::new(0.5, 0.5), camera_only_blur.min(TRANSITION_ZOOM_CAP), 1.0, + false, ) }; @@ -5789,7 +5813,7 @@ impl ProjectUniforms { camera_only_descriptor.mode.as_f32(), camera_only_descriptor.strength, camera_only_descriptor.zoom_amount, - 0.0, + camera_only_descriptor.zoom_direction(), ], shadow: if padding > 0.0 { project.background.shadow * visibility @@ -5920,7 +5944,7 @@ impl ProjectUniforms { cursor_clip_visibility, click_ripples, cursor_event_order, - display_parent_motion_px: display_motion_parent, + display_motion: display_motion_delta, motion_blur_amount: cursor_motion_blur, masks, texts, @@ -6892,6 +6916,83 @@ mod tests { assert_eq!(zoom_out.descriptor.strength, 1.0); } + #[test] + fn display_zoom_blur_converges_on_the_point_the_frame_scales_about() { + // A frame scaled by `scale` about `fixed`: x' = fixed + scale * (x - fixed). + let frame = |scale: f64, fixed: XY| { + let start = XY::new(0.0, 0.0); + let end = XY::new(1920.0, 1080.0); + motion_bounds( + fixed + (start - fixed) * scale, + fixed + (end - fixed) * scale, + ) + }; + // The centre and another point on the frame's diagonal (where pairing + // opposite corners' paths gives no single crossing), one off the + // diagonal, and one off the card (a zoom that also pans). + for fixed in [ + XY::new(960.0, 540.0), + XY::new(480.0, 270.0), + XY::new(1500.0, 200.0), + XY::new(2400.0, -100.0), + ] { + // In, then out. + for (from, to) in [(1.0, 1.04), (1.04, 1.0)] { + let current = frame(to, fixed); + let blur = ProjectUniforms::compute_display_motion_blur( + current, + frame(from, fixed), + true, + 1.0, + 0.0, + 1.0, + ); + assert_eq!(blur.descriptor.mode, MotionBlurMode::Zoom); + let size = current.size(); + let expected = XY::new( + ((fixed.x - current.start.coord.x) / size.x) as f32, + ((fixed.y - current.start.coord.y) / size.y) as f32, + ); + let [x, y] = blur.descriptor.zoom_center_uv; + assert!( + (x - expected.x).abs() < 1e-4 && (y - expected.y).abs() < 1e-4, + "{fixed:?} {from} -> {to}: centre ({x}, {y}), expected {expected:?}" + ); + } + } + } + + #[test] + fn display_zoom_blur_trails_the_motion_both_ways() { + // Zooming in, the frame grows and content moves away from the centre, + // so the taps run outward; zooming out they run inward. + let small = motion_bounds(XY::new(0.0, 0.0), XY::new(1920.0, 1080.0)); + let large = motion_bounds(XY::new(-38.4, -21.6), XY::new(1958.4, 1101.6)); + let zoom_in = + ProjectUniforms::compute_display_motion_blur(large, small, true, 1.0, 0.0, 1.0); + let zoom_out = + ProjectUniforms::compute_display_motion_blur(small, large, true, 1.0, 0.0, 1.0); + assert_eq!(zoom_in.descriptor.mode, MotionBlurMode::Zoom); + assert!(zoom_in.descriptor.zoom_outward); + assert_eq!(zoom_in.descriptor.zoom_direction(), 1.0); + assert_eq!(zoom_out.descriptor.mode, MotionBlurMode::Zoom); + assert!(!zoom_out.descriptor.zoom_outward); + } + + #[test] + fn display_motion_carries_a_point_with_the_card() { + // The card moved 10px right and grew 100x50px since the last frame. + let motion = DisplayMotionDelta { + bounds: [100.0, 100.0, 1100.0, 600.0], + start_delta: XY::new(10.0, 0.0), + size_delta: XY::new(100.0, 50.0), + }; + let carried = |x, y| motion.carried(XY::new(x, y)); + assert_eq!(carried(100.0, 100.0), XY::new(10.0, 0.0)); + assert_eq!(carried(600.0, 350.0), XY::new(60.0, 25.0)); + assert_eq!(carried(1100.0, 600.0), XY::new(110.0, 50.0)); + } + #[test] fn display_movement_blur_length_is_linear_in_velocity() { // The smear length must track the per-frame delta linearly (Screen diff --git a/crates/rendering/src/shaders/composite-video-frame.wgsl b/crates/rendering/src/shaders/composite-video-frame.wgsl index 6b000a10fb..a8dd666da3 100644 --- a/crates/rendering/src/shaders/composite-video-frame.wgsl +++ b/crates/rendering/src/shaders/composite-video-frame.wgsl @@ -365,18 +365,20 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { return composite_source_over(apply_color_grade(base_color, target_uv, p), shadow_color); } - // 21-tap box along [0, +v]: matches the reference directional filter - // (kernel 21 with offset -|v|/2, which anchors the span at the pixel). + // 21 taps along [0, 0.95 v], the pixel's own tap counted twice: + // t = 0, 0, 1/20 ... 19/20, divided by 21. The streak trails the + // motion by just under a frame. var accum = vec3(0.0); var alpha_sum = 0.0; let k = 20.0; - for (var i = 0; i <= 20; i = i + 1) { + for (var i = 0; i < 20; i = i + 1) { let sample_uv = target_uv + velocity_uv * (f32(i) / k); var tap = sample_layer(sample_uv, crop_bounds_uv, shrink); tap = apply_rounded_corners(tap, sample_uv); - accum += tap.rgb * tap.a; - alpha_sum += tap.a; + let weight = select(1.0, 2.0, i == 0); + accum += tap.rgb * tap.a * weight; + alpha_sum += tap.a * weight; } let out_alpha = (alpha_sum / 21.0) * uniforms.opacity; @@ -408,7 +410,11 @@ fn fs_main(@builtin(position) frag_coord: vec4) -> @location(0) vec4 { // from the center (ray = center_dist * amount), parabolic weights peaked // mid-ray, and a per-pixel random phase to dither the 13-tap banding — // all matching the reference zoom filter. - let scaled_dir = dir / center_dist * min(center_dist * min(zoom_amount, 1.0), MAX_ZOOM_RAY_UV); + // Taps run toward the centre while the frame shrinks and away from it + // while it grows, so the smear always trails where the content was. + let ray_sign = select(1.0, -1.0, uniforms.motion_blur_params.w > 0.5); + let scaled_dir = dir / center_dist * min(center_dist * min(zoom_amount, 1.0), MAX_ZOOM_RAY_UV) + * ray_sign; let max_kernel = 13.0; let dither = interleaved_noise(p); diff --git a/crates/rendering/src/shaders/cursor.wgsl b/crates/rendering/src/shaders/cursor.wgsl index e7c2284d91..b70f608e99 100644 --- a/crates/rendering/src/shaders/cursor.wgsl +++ b/crates/rendering/src/shaders/cursor.wgsl @@ -16,6 +16,15 @@ struct Uniforms { color_adjust_b: vec4, // (grain amount, grain seed, grade-active flag, unused). grain_params: vec4, + // The screen's motion blur this frame, which the cursor rides inside: + // (mode 0 none / 1 move / 2 zoom, then for move the smear in output px, + // for zoom the centre in card UV and the zoom amount). + screen_blur: vec4, + // The display card in output px (x, y, width, height). + screen_card: vec4, + // (how far the screen's zoom blur can carry the sprite, in output px, + // unused x3). + screen_reach: vec4, }; @group(0) @binding(0) @@ -55,6 +64,29 @@ fn cursor_velocity_uv() -> vec2 { return raw_velocity_uv * min(1.0, MAX_CURSOR_BLUR_UV / raw_vel_len); } +// An offset on the frame (output px, y down) in the sprite's own UV, through +// the inverse of the vertex transform, so the screen's blur keeps its +// direction however the cursor tilts. +fn px_to_sprite_uv(offset: vec2) -> vec2 { + let size = max(uniforms.position_size.zw, vec2(1.0)); + let angle = clamp(uniforms.rotation_params.z, -MAX_ROTATION_RADIANS, MAX_ROTATION_RADIANS) + + uniforms.rotation_params.y; + let s = sin(angle); + let c = cos(angle); + return vec2( + c * offset.x / size.x - s * offset.y / size.y, + s * offset.x / size.x + c * offset.y / size.y + ); +} + +// The screen's directional smear in sprite UV; zero unless it is moving. +fn screen_move_uv() -> vec2 { + if (uniforms.screen_blur.x < 0.5 || uniforms.screen_blur.x >= 1.5) { + return vec2(0.0, 0.0); + } + return px_to_sprite_uv(uniforms.screen_blur.yz); +} + fn rotate_point(p: vec2, center: vec2, angle: f32) -> vec2 { let cos_a = cos(angle); let sin_a = sin(angle); @@ -74,13 +106,19 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput { vec2(1.0, 1.0) ); + let screen_pos = uniforms.position_size.xy; + let cursor_size = uniforms.position_size.zw; + // The quad covers the sprite plus everywhere its own smear and the + // screen's smear can carry it. let blur_uv = cursor_velocity_uv(); - let uv_min = min(vec2(0.0, 0.0), -blur_uv); - let uv_max = max(vec2(1.0, 1.0), vec2(1.0, 1.0) - blur_uv); + let screen_uv = screen_move_uv(); + // Rotation mixes the axes, so a zoom's reach widens both by the larger. + let reach_uv = vec2(uniforms.screen_reach.x / max(min(cursor_size.x, cursor_size.y), 1.0)); + let uv_min = min(vec2(0.0), -blur_uv) + min(vec2(0.0), -screen_uv) - reach_uv; + let uv_max = vec2(1.0) + max(vec2(0.0), -blur_uv) + + max(vec2(0.0), -screen_uv) + reach_uv; let local_uv = uv_min + (uv_max - uv_min) * corners[vertex_index]; let pos = vec2(local_uv.x, -local_uv.y); - let screen_pos = uniforms.position_size.xy; - let cursor_size = uniforms.position_size.zw; let base_rotation = uniforms.rotation_params.y; let x_movement_tilt = uniforms.rotation_params.z; @@ -200,29 +238,76 @@ fn apply_color_grade(color: vec4, frag_pos: vec2) -> vec4 { @fragment fn fs_main(input: VertexOutput) -> @location(0) vec4 { let velocity_uv = cursor_velocity_uv(); - let blur_strength = uniforms.motion_vector_strength.z; - let opacity = uniforms.motion_vector_strength.w * screen_bounds_mask(input.position.xy); - let base_color = sample_cursor(input.uv); - - if (length(velocity_uv) < 0.005 || blur_strength < 0.001) { - return apply_color_grade(base_color * opacity, input.position.xy); + let opacity = uniforms.motion_vector_strength.w; + let p = input.position.xy; + let mode = uniforms.screen_blur.x; + + // The cursor rides inside the screen, so the screen's blur lands on it + // too, over the cursor's own: a pan smears it along the pan, a zoom + // pulls it toward the zoom centre. Each of the screen's taps is clipped + // to the card where that tap lands, as the screen's own content is. + var color = vec4(0.0); + if (mode < 0.5) { + color = own_blurred(input.uv, velocity_uv) * screen_bounds_mask(p); + } else if (mode < 1.5) { + let smear_px = uniforms.screen_blur.yz; + color = own_blurred(input.uv, velocity_uv) * screen_bounds_mask(p) * 2.0; + for (var i = 1; i < 20; i = i + 1) { + let offset_px = smear_px * (f32(i) / 20.0); + color += own_blurred(input.uv + px_to_sprite_uv(offset_px), velocity_uv) + * screen_bounds_mask(p + offset_px); + } + color /= 21.0; + } else { + let card = uniforms.screen_card; + let card_size = max(card.zw, vec2(1.0)); + let dir = uniforms.screen_blur.yz - (p - card.xy) / card_size; + let dist = length(dir); + // A negative amount is a zoom in: the taps run outward, as the + // display's do. + let amount = min(abs(uniforms.screen_blur.w), 1.0); + let ray_sign = select(1.0, -1.0, uniforms.screen_blur.w < 0.0); + if (dist < 1e-4 || amount < 1e-4) { + color = own_blurred(input.uv, velocity_uv) * screen_bounds_mask(p); + } else { + let ray_px = dir / dist * min(dist * amount, MAX_ZOOM_RAY_UV) * card_size + * ray_sign; + let dither = interleaved_noise(p); + var weight_sum = 0.0; + for (var i = 0; i < 13; i = i + 1) { + let percent = (f32(i) + dither) / 13.0; + let weight = 4.0 * (percent - percent * percent); + let offset_px = ray_px * percent; + color += own_blurred(input.uv + px_to_sprite_uv(offset_px), velocity_uv) + * screen_bounds_mask(p + offset_px) * weight; + weight_sum += weight; + } + color /= max(weight_sum, 1e-4); + } } - // 21-tap box along the motion vector, output fully blurred: the amount - // scales the smear LENGTH (baked into velocity_uv on the CPU side), never - // a crossfade with the sharp sprite — a sharp copy over a smear reads as - // ghosting. Transparent taps shorten the effective alpha, which is what - // stretches and fades the sprite along fast motion. - let kernel_size = 21.0; - let k = kernel_size - 1.0; - var color = base_color; - - for (var i = 1; i <= 20; i = i + 1) { - let bias = velocity_uv * (f32(i) / k); - let sample_uv = input.uv + bias; - color += sample_cursor(sample_uv); - } + return apply_color_grade(color * opacity, p); +} + +// Same ceiling as the display's radial blur (composite-video-frame.wgsl). +const MAX_ZOOM_RAY_UV: f32 = 0.10; - color /= kernel_size; - return apply_color_grade(color * opacity, input.position.xy); +// Same per-pixel phase as the display's radial blur, so the cursor's taps +// line up with the screen's around it. +fn interleaved_noise(p: vec2) -> f32 { + return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); } + +// The cursor's own directional smear: 21 taps along [0, 0.95 v], the first +// counted twice, the same kernel as the display's. +fn own_blurred(uv: vec2, velocity_uv: vec2) -> vec4 { + if (length(velocity_uv) < 0.005) { + return sample_cursor(uv); + } + var color = sample_cursor(uv) * 2.0; + for (var i = 1; i < 20; i = i + 1) { + color += sample_cursor(uv + velocity_uv * (f32(i) / 20.0)); + } + return color / 21.0; +} + diff --git a/crates/rendering/src/zoom_spring.rs b/crates/rendering/src/zoom_spring.rs index 131bf0f5cb..aaf248ff7f 100644 --- a/crates/rendering/src/zoom_spring.rs +++ b/crates/rendering/src/zoom_spring.rs @@ -1456,12 +1456,12 @@ mod tests { #[test] fn new_project_zoom_is_quicker_without_overshoot() { - // New projects reach 90% of a zoom by about 317 ms with no + // New projects reach 90% of a zoom by about 290 ms with no // overshoot; saved projects keep the old spring, about 383 ms. let segments = [manual_segment(0.5, 6.0, 2.0, 0.5, 0.5)]; let fresh = precomputed(&segments, ScreenMovementSpring::NEW_PROJECT, 6.0); let (t90, peak) = rise(&fresh, 0.5, 2.0, 0.9); - assert!((0.30..=0.33).contains(&t90), "new project t90 {t90}"); + assert!((0.28..=0.30).contains(&t90), "new project t90 {t90}"); assert!(peak <= 2.0 + 2e-3, "new project overshoot {peak}"); let saved = precomputed(&segments, ScreenMovementSpring::default(), 6.0); diff --git a/crates/rendering/tests/cursor_texture_edges.rs b/crates/rendering/tests/cursor_texture_edges.rs index b1cbd9ff8f..ecce2bd730 100644 --- a/crates/rendering/tests/cursor_texture_edges.rs +++ b/crates/rendering/tests/cursor_texture_edges.rs @@ -17,7 +17,7 @@ struct Case { } impl Case { - fn uniforms(self) -> [[f32; 4]; 8] { + fn uniforms(self) -> [[f32; 4]; 11] { [ [ 96.0 + self.offset[0], @@ -32,6 +32,10 @@ impl Case { [0.0; 4], [0.0; 4], [0.0; 4], + // No screen blur. + [0.0; 4], + [0.0, 0.0, OUTPUT_WIDTH as f32, OUTPUT_HEIGHT as f32], + [0.0; 4], ] } } @@ -61,8 +65,8 @@ fn fs_main(input: VertexOutput) -> @location(0) vec4 { if (length(velocity_uv) < 0.005 || blur_strength < 0.001) { return apply_color_grade(base_color * opacity, input.position.xy); } - var color = base_color; - for (var i = 1; i <= 20; i = i + 1) { + var color = base_color * 2.0; + for (var i = 1; i < 20; i = i + 1) { let sample_uv = input.uv + velocity_uv * (f32(i) / 20.0); let texel = textureSample(t_cursor, s_cursor, sample_uv); let inside = all(vec4(sample_uv >= vec2(0.0), sample_uv <= vec2(1.0))); @@ -293,7 +297,7 @@ fn assert_transparent_exterior(pixels: &[u8], case: Case) { c * dx / width - s * dy / height, s * dx / width + c * dy / height, ]; - let outside = (0..21).all(|tap| { + let outside = (0..20).all(|tap| { let sample = [ uv[0] + velocity[0] * tap as f32 / 20.0, uv[1] + velocity[1] * tap as f32 / 20.0,