Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: grade the cursor with the screen color grade

Richie McIlroy17cb54325ccc
diff --git a/crates/rendering/src/layers/cursor.rs b/crates/rendering/src/layers/cursor.rs
index f02b64205..4e4664f90 100644
--- a/crates/rendering/src/layers/cursor.rs
+++ b/crates/rendering/src/layers/cursor.rs
@@ -8,7 +8,7 @@ use wgpu::{BindGroup, FilterMode, include_wgsl, util::DeviceExt};
use crate::{
Coord, DecodedSegmentFrames, FrameSpace, ProjectUniforms, RenderVideoConstants,
- STANDARD_CURSOR_HEIGHT, zoom::InterpolatedZoom,
+ STANDARD_CURSOR_HEIGHT, composite_frame::ColorGradeUniformParams, zoom::InterpolatedZoom,
};
const CURSOR_CLICK_DURATION: f64 = 0.13;
@@ -594,6 +594,15 @@ impl CursorLayer {
],
};
+ // The cursor wears the screen's grade so it reads as part of the
+ // graded footage; the same shared params keep its grain and vignette
+ // continuous with the card and backdrop underneath.
+ let cursor_grade = if uniforms.project.color_correction.grade_cursor {
+ uniforms.screen_color_grade
+ } else {
+ ColorGradeUniformParams::IDENTITY
+ };
+
let cursor_uniforms = CursorUniforms {
position_size,
output_size: [
@@ -621,6 +630,9 @@ impl CursorLayer {
uniforms.cursor_x_axis_tilt_radians,
0.0,
],
+ color_adjust_a: cursor_grade.color_adjust_a,
+ color_adjust_b: cursor_grade.color_adjust_b,
+ grain_params: cursor_grade.grain_params,
};
constants.queue.write_buffer(
@@ -714,6 +726,11 @@ pub struct CursorUniforms {
screen_bounds: [f32; 4],
motion_vector_strength: [f32; 4],
rotation_params: [f32; 4],
+ /// Screen color grade (see `ColorGradeUniformParams`); identity when the
+ /// user opts the cursor out of the grade.
+ color_adjust_a: [f32; 4],
+ color_adjust_b: [f32; 4],
+ grain_params: [f32; 4],
}
fn compute_cursor_idle_opacity(
diff --git a/crates/rendering/src/shaders/cursor.wgsl b/crates/rendering/src/shaders/cursor.wgsl
index d8ad1142c..6b0d95981 100644
--- a/crates/rendering/src/shaders/cursor.wgsl
+++ b/crates/rendering/src/shaders/cursor.wgsl
@@ -9,6 +9,17 @@ struct Uniforms {
screen_bounds: vec4<f32>,
motion_vector_strength: vec4<f32>,
rotation_params: vec4<f32>,
+ // Screen color grade, identity when the cursor opts out. The math MUST
+ // stay in sync with apply_color_grade in composite-video-frame.wgsl and
+ // color-grade.wgsl: grain and vignette are computed in the same
+ // output-pixel space so the sprite blends seamlessly into the graded
+ // scene beneath it.
+ // (exposure stops, contrast, saturation, temperature).
+ color_adjust_a: vec4<f32>,
+ // (tint, fade, split_tone, vignette).
+ color_adjust_b: vec4<f32>,
+ // (grain amount, per-frame grain seed, grade-active flag, unused).
+ grain_params: vec4<f32>,
};
@group(0) @binding(0)
@@ -116,6 +127,86 @@ fn screen_bounds_mask(frag_pos: vec2<f32>) -> f32 {
return clamp(inside + 0.5, 0.0, 1.0);
}
+fn grain_hash(p: vec2<f32>) -> f32 {
+ var p3 = fract(vec3<f32>(p.x, p.y, p.x) * 0.1031);
+ p3 += dot(p3, p3.yzx + 33.33);
+ return fract((p3.x + p3.y) * p3.z);
+}
+
+// Applies the screen's color grade to the resolved sprite color. This
+// pipeline blends premultiplied (One, OneMinusSrcAlpha), so the color is
+// lifted to straight alpha before grading and re-premultiplied after —
+// otherwise the grade's additive terms (fade lift, split tone, grain) would
+// bleed into the transparent parts of the motion smear.
+fn apply_color_grade(color: vec4<f32>, frag_pos: vec2<f32>) -> vec4<f32> {
+ if uniforms.grain_params.z < 0.5 || color.a < 0.001 {
+ return color;
+ }
+
+ let exposure = uniforms.color_adjust_a.x;
+ let contrast = uniforms.color_adjust_a.y;
+ let saturation = uniforms.color_adjust_a.z;
+ let temperature = uniforms.color_adjust_a.w;
+ let tint = uniforms.color_adjust_b.x;
+ let fade = uniforms.color_adjust_b.y;
+ let split_tone = uniforms.color_adjust_b.z;
+ let vignette = uniforms.color_adjust_b.w;
+ let grain = uniforms.grain_params.x;
+
+ // Exposure in stops.
+ var rgb = (color.rgb / color.a) * exp2(exposure);
+
+ // White balance: temperature trades red against blue, tint trades green
+ // against magenta. Multiplicative so black stays black.
+ rgb = rgb * vec3<f32>(
+ 1.0 + 0.10 * temperature + 0.04 * tint,
+ 1.0 - 0.07 * tint,
+ 1.0 - 0.10 * temperature + 0.04 * tint,
+ );
+
+ // Contrast around mid gray.
+ rgb = (rgb - vec3<f32>(0.5)) * (1.0 + contrast) + vec3<f32>(0.5);
+
+ // Saturation via luma mix (-1 = grayscale).
+ let luma = dot(clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0)), vec3<f32>(0.2126, 0.7152, 0.0722));
+ rgb = mix(vec3<f32>(luma), rgb, 1.0 + saturation);
+
+ // Split toning: teal into shadows, orange into highlights (reversed when
+ // the amount is negative), weighted by luma bands.
+ let shadow_w = 1.0 - smoothstep(0.2, 0.65, luma);
+ let highlight_w = smoothstep(0.35, 0.8, luma);
+ rgb += split_tone * (
+ shadow_w * vec3<f32>(-0.06, 0.02, 0.08) +
+ highlight_w * vec3<f32>(0.08, 0.02, -0.06)
+ );
+
+ // Film fade: raised blacks, gently dulled highlights.
+ rgb = rgb * (1.0 - 0.18 * fade) + vec3<f32>(0.09 * fade);
+
+ // The cursor lives in the full output frame, so its vignette always uses
+ // the frame-wide field the background and display card share.
+ if vignette > 0.0 {
+ let r = length((frag_pos / uniforms.output_size.xy - vec2<f32>(0.5)) * 2.0);
+ rgb = rgb * (1.0 - vignette * 0.65 * smoothstep(0.5, 1.5, r));
+ }
+
+ // Animated monochrome grain, peaked in the midtones like scanned film.
+ // Same hash, coordinates, and seed as the layers underneath so the noise
+ // field stays continuous through the sprite's semi-transparent pixels.
+ if grain > 0.0 {
+ let seed = uniforms.grain_params.y;
+ let noise = grain_hash(frag_pos + vec2<f32>(seed * 17.0, seed * 29.0));
+ let graded_luma = dot(
+ clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0)),
+ vec3<f32>(0.2126, 0.7152, 0.0722)
+ );
+ let response = 0.25 + 0.75 * (1.0 - abs(2.0 * graded_luma - 1.0));
+ rgb += (noise - 0.5) * grain * 0.35 * response;
+ }
+
+ return vec4<f32>(clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0)) * color.a, color.a);
+}
+
@fragment
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
let velocity_uv = cursor_velocity_uv();
@@ -124,7 +215,7 @@ fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
let base_color = sample_cursor(input.uv);
if (length(velocity_uv) < 0.005 || blur_strength < 0.001) {
- return base_color * opacity;
+ return apply_color_grade(base_color * opacity, input.position.xy);
}
// 21-tap box along the motion vector, output fully blurred: the amount
@@ -143,5 +234,5 @@ fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
}
color /= kernel_size;
- return color * opacity;
+ return apply_color_grade(color * opacity, input.position.xy);
}