Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(rendering): simplify cursor and display motion blur shaders
Richie McIlroydcd31ef1a886
diff --git a/crates/rendering/src/shaders/composite-video-frame.wgsl b/crates/rendering/src/shaders/composite-video-frame.wgslindex f72302e61..398a55862 100644--- a/crates/rendering/src/shaders/composite-video-frame.wgsl+++ b/crates/rendering/src/shaders/composite-video-frame.wgsl@@ -158,23 +158,18 @@ fn fs_main(@builtin(position) frag_coord: vec4<f32>) -> @location(0) vec4<f32> {return mix(shadow_color, base_color, base_color.a);}- let direction = motion_vec / motion_len;- let stroke = min(motion_len, 0.35);- let num_samples = i32(clamp(6.0 + 24.0 * blur_strength, 6.0, 36.0));-- for (var i = 1; i < num_samples; i = i + 1) {- let t = f32(i) / f32(num_samples);- let eased = smoothstep(0.0, 1.0, t);- let offset = direction * stroke * eased;- let jitter_seed = target_uv + vec2<f32>(t, f32(i) * 0.37);- let jitter = (rand(jitter_seed) - 0.5) * stroke * 0.15;- let sample_uv = target_uv - offset + direction * jitter;+ let velocity_uv = motion_vec;+ let offset_base = -0.5;+ let k = 20;++ for (var i = 0; i < 20; i = i + 1) {+ let bias = velocity_uv * (f32(i) / f32(k) + offset_base);+ let sample_uv = target_uv + bias;if sample_uv.x >= 0.0 && sample_uv.x <= 1.0 && sample_uv.y >= 0.0 && sample_uv.y <= 1.0 {var sample_color = sample_texture(sample_uv, crop_bounds_uv);sample_color = apply_rounded_corners(sample_color, sample_uv);- let weight = 1.0 - t * 0.8;- let sample_weight = weight * sample_color.a;+ let sample_weight = sample_color.a;if sample_weight > 1e-6 {accum += sample_color * sample_weight;weight_sum += sample_weight;@@ -183,27 +178,25 @@ fn fs_main(@builtin(position) frag_coord: vec4<f32>) -> @location(0) vec4<f32> {}} else {let center = uniforms.motion_blur_zoom_center;- let to_center = target_uv - center;- let dist = length(to_center);+ let dir = center - target_uv;+ let dist = length(dir);if dist < 1e-4 || zoom_amount < 1e-4 {return mix(shadow_color, base_color, base_color.a);}- let radial_dir = to_center / dist;- let sample_span = zoom_amount * 1.2;- let num_samples = i32(clamp(8.0 + 26.0 * blur_strength, 8.0, 40.0));+ let scaled_dir = dir * blur_strength;+ let max_kernel = 13.0;++ let offset_rand = rand(vec2<f32>(target_uv.x * 7.37, target_uv.y * 11.23));- for (var i = 1; i < num_samples; i = i + 1) {- let t = f32(i) / f32(num_samples);- let offset = radial_dir * sample_span * t;- let jitter_seed = vec2<f32>(t, target_uv.x + target_uv.y);- let jitter = (rand(jitter_seed) - 0.5) * zoom_amount * 0.1;- let sample_uv = target_uv - offset + radial_dir * jitter;+ for (var i = 0; i < 13; i = i + 1) {+ let percent = (f32(i) + offset_rand) / max_kernel;+ let weight = 4.0 * (percent - percent * percent);+ let sample_uv = target_uv + scaled_dir * percent;if sample_uv.x >= 0.0 && sample_uv.x <= 1.0 && sample_uv.y >= 0.0 && sample_uv.y <= 1.0 {var sample_color = sample_texture(sample_uv, crop_bounds_uv);sample_color = apply_rounded_corners(sample_color, sample_uv);- let weight = 1.0 - t * 0.9;let sample_weight = weight * sample_color.a;if sample_weight > 1e-6 {accum += sample_color * sample_weight;diff --git a/crates/rendering/src/shaders/cursor.wgsl b/crates/rendering/src/shaders/cursor.wgslindex 1e880b27b..34e5103e6 100644--- a/crates/rendering/src/shaders/cursor.wgsl+++ b/crates/rendering/src/shaders/cursor.wgsl@@ -20,8 +20,7 @@ var t_cursor: texture_2d<f32>;@group(0) @binding(2)var s_cursor: sampler;-const MAX_ROTATION_RADIANS: f32 = 0.25;-const ROTATION_VELOCITY_SCALE: f32 = 0.003;+const MAX_ROTATION_RADIANS: f32 = 0.34906584;fn rotate_point(p: vec2<f32>, center: vec2<f32>, angle: f32) -> vec2<f32> {let cos_a = cos(angle);@@ -53,13 +52,11 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {let screen_pos = uniforms.position_size.xy;let cursor_size = uniforms.position_size.zw;- let rotation_amount = uniforms.rotation_params.x;let base_rotation = uniforms.rotation_params.y;+ let x_movement_tilt = uniforms.rotation_params.z;- let motion_x = uniforms.motion_vector_strength.x;- let normalized_velocity = clamp(motion_x * ROTATION_VELOCITY_SCALE, -1.0, 1.0);- let velocity_rotation = normalized_velocity * MAX_ROTATION_RADIANS * rotation_amount;- let rotation_angle = velocity_rotation + base_rotation;+ let clamped_tilt = clamp(x_movement_tilt, -MAX_ROTATION_RADIANS, MAX_ROTATION_RADIANS);+ let rotation_angle = clamped_tilt + base_rotation;let pivot = vec2<f32>(0.0, 0.0);let rotated_pos = rotate_point(pos, pivot, rotation_angle);@@ -87,50 +84,26 @@ fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {}let cursor_size = uniforms.position_size.zw;- let blur_offset_uv = motion_vec * 0.45 / cursor_size;- let blur_len = length(blur_offset_uv);+ let velocity_uv = motion_vec / cursor_size;+ let vel_len = length(velocity_uv);- if (blur_len < 0.005) {+ if (vel_len < 0.005) {return textureSample(t_cursor, s_cursor, input.uv) * opacity;}- let num_samples = 24;- var color_sum = vec4<f32>(0.0);- var alpha_sum = 0.0;- var weight_sum = 0.0;+ let kernel_size = 21;+ let k = kernel_size - 1;+ let offset_base = -vel_len / 2.0 / vel_len - 0.5;- let blur_center = 0.3;- let blur_spread = 2.5;+ var color = textureSample(t_cursor, s_cursor, input.uv);- for (var i = 0; i < num_samples; i++) {- let t = f32(i) / f32(num_samples - 1);- let centered_t = t - blur_center;- let sample_offset = blur_offset_uv * centered_t;- let sample_uv = input.uv + sample_offset;-- let gauss_t = centered_t * blur_spread;- var weight = exp(-gauss_t * gauss_t);-- if (centered_t > 0.0) {- weight *= 1.0 + centered_t * 0.3;- }-- let sample_color = textureSample(t_cursor, s_cursor, sample_uv);- let premul_rgb = sample_color.rgb * sample_color.a;- color_sum += vec4<f32>(premul_rgb * weight, 0.0);- alpha_sum += sample_color.a * weight;- weight_sum += weight;- }-- let avg_alpha = alpha_sum / max(weight_sum, 0.001);- var final_color: vec4<f32>;- if (avg_alpha > 0.001) {- let final_rgb = color_sum.rgb / max(alpha_sum, 0.001);- final_color = vec4<f32>(final_rgb, avg_alpha);- } else {- final_color = vec4<f32>(0.0);+ for (var i = 0; i < 20; i++) {+ let bias = velocity_uv * (f32(i) / f32(k) + offset_base);+ let sample_uv = input.uv + bias;+ color += textureSample(t_cursor, s_cursor, sample_uv);}- final_color *= opacity;- return final_color;+ color = color / f32(kernel_size);+ color *= opacity;+ return color;}