authoravatarclover caruso<git@paperclover.net>2026-11-30 17:16:48-08:00
committeravatarclover caruso<git@paperclover.net>2026-11-30 18:09:38-08:00
log9d4ff1528a341e1708570f7cbe8a14e633dbc57a
tree623f015fdc8d3d4c7484ef35b6dce485ac768cf6
parent6538fb706288a49aab92fd5c4348a0640dc7fc8b
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Fix HDR negative floats, ColorCorrector SMH, GamutConvert, and HueCurves parity (#28)

- BrightnessContrast: skip pow when gamma == 1 so float negatives survive; honor ClipBlack/ClipWhite. - ColorCorrector: default Shadows/Highlights range is one cubic Bezier through (0,1) (.4,.2) (.6,0) (1,0); Midtones has no RGB controls; Master runs before SMH. - GamutConvert: PreDividePostMultiply defaults to 0. - HueCurves: suppress toward the source min channel; LUT encodes val*64 so neutral 1.0 is exact. Assisted-by: claude-sonnet-5.5

2 files changed, 55 insertions(+), 47 deletions(-)

crates/engine/src/eval/color.rs+17-17
...@@ -48,8 +48,14 @@ impl Engine {...@@ -48,8 +48,14 @@ impl Engine {
48 comp.eval_number(node, "Blue", time, 1.0, &self.expr) as f32,48 comp.eval_number(node, "Blue", time, 1.0, &self.expr) as f32,
49 comp.eval_number(node, "Alpha", time, 0.0, &self.expr) as f32,49 comp.eval_number(node, "Alpha", time, 0.0, &self.expr) as f32,
50 ];50 ];
51 u.p[4] = [
52 comp.eval_number(node, "ClipBlack", time, 0.0, &self.expr) as f32,
53 comp.eval_number(node, "ClipWhite", time, 0.0, &self.expr) as f32,
54 0.0,
55 0.0,
56 ];
5157
52 let key = self.make_key(node, "BC", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..4]));58 let key = self.make_key(node, "BC", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..5]));
53 if let Some(hit) = self.cache.get(key, node.id, frame_i) {59 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
54 stats.cache_hits += 1;60 stats.cache_hits += 1;
55 return hit;61 return hit;
...@@ -127,28 +133,22 @@ impl Engine {...@@ -127,28 +133,22 @@ impl Engine {
127 comp.eval_number(node, "WheelTintLength3", time, 0.0, &self.expr) as f32,133 comp.eval_number(node, "WheelTintLength3", time, 0.0, &self.expr) as f32,
128 ];134 ];
129 u.p[7] = [135 u.p[7] = [
130 comp.eval_number(node, "MidtonesRGBGain", time, 1.0, &self.expr) as f32,
131 comp.eval_number(node, "MidtonesRGBLift", time, 0.0, &self.expr) as f32,
132 comp.eval_number(node, "MidtonesRGBGamma", time, 1.0, &self.expr) as f32,
133 comp.eval_number(node, "MidtonesRGBContrast", time, 1.0, &self.expr) as f32,
134 ];
135 u.p[8] = [
136 comp.eval_number(node, "WheelHue4", time, 0.0, &self.expr) as f32,136 comp.eval_number(node, "WheelHue4", time, 0.0, &self.expr) as f32,
137 comp.eval_number(node, "WheelSaturation4", time, 1.0, &self.expr) as f32,137 comp.eval_number(node, "WheelSaturation4", time, 1.0, &self.expr) as f32,
138 comp.eval_number(node, "WheelTintAngle4", time, 0.0, &self.expr) as f32,138 comp.eval_number(node, "WheelTintAngle4", time, 0.0, &self.expr) as f32,
139 comp.eval_number(node, "WheelTintLength4", time, 0.0, &self.expr) as f32,139 comp.eval_number(node, "WheelTintLength4", time, 0.0, &self.expr) as f32,
140 ];140 ];
141 u.p[9] = [141 u.p[8] = [
142 comp.eval_number(node, "HighlightsRGBGain", time, 1.0, &self.expr) as f32,142 comp.eval_number(node, "HighlightsRGBGain", time, 1.0, &self.expr) as f32,
143 comp.eval_number(node, "HighlightsRGBLift", time, 0.0, &self.expr) as f32,143 comp.eval_number(node, "HighlightsRGBLift", time, 0.0, &self.expr) as f32,
144 comp.eval_number(node, "HighlightsRGBGamma", time, 1.0, &self.expr) as f32,144 comp.eval_number(node, "HighlightsRGBGamma", time, 1.0, &self.expr) as f32,
145 comp.eval_number(node, "HighlightsRGBContrast", time, 1.0, &self.expr) as f32,145 comp.eval_number(node, "HighlightsRGBContrast", time, 1.0, &self.expr) as f32,
146 ];146 ];
147 u.p[10] = [147 u.p[9] = [
148 comp.eval_number(node, "ShadowsRGBBrightness", time, 0.0, &self.expr) as f32,148 comp.eval_number(node, "ShadowsRGBBrightness", time, 0.0, &self.expr) as f32,
149 comp.eval_number(node, "MidtonesRGBBrightness", time, 0.0, &self.expr) as f32,
150 comp.eval_number(node, "HighlightsRGBBrightness", time, 0.0, &self.expr) as f32,149 comp.eval_number(node, "HighlightsRGBBrightness", time, 0.0, &self.expr) as f32,
151 0.0,150 0.0,
151 0.0,
152 ];152 ];
153 let wheel_is_active = |w: [f32; 4], lgg: [f32; 4], b: f32| -> bool {153 let wheel_is_active = |w: [f32; 4], lgg: [f32; 4], b: f32| -> bool {
154 w[0].abs() > 1e-5154 w[0].abs() > 1e-5
...@@ -160,9 +160,9 @@ impl Engine {...@@ -160,9 +160,9 @@ impl Engine {
160 || (lgg[3] - 1.0).abs() > 1e-5160 || (lgg[3] - 1.0).abs() > 1e-5
161 || b.abs() > 1e-5161 || b.abs() > 1e-5
162 };162 };
163 let has_smh = wheel_is_active(u.p[4], u.p[5], u.p[10][0])163 let has_smh = wheel_is_active(u.p[4], u.p[5], u.p[9][0])
164 || wheel_is_active(u.p[6], u.p[7], u.p[10][1])164 || wheel_is_active(u.p[6], [1.0, 0.0, 1.0, 1.0], 0.0)
165 || wheel_is_active(u.p[8], u.p[9], u.p[10][2]);165 || wheel_is_active(u.p[7], u.p[8], u.p[9][1]);
166 u.p[3] = [166 u.p[3] = [
167 if mask_inv { 1.0 } else { 0.0 },167 if mask_inv { 1.0 } else { 0.0 },
168 tint_mode,168 tint_mode,
...@@ -170,7 +170,7 @@ impl Engine {...@@ -170,7 +170,7 @@ impl Engine {
170 0.0,170 0.0,
171 ];171 ];
172172
173 let key = self.make_key(node, "CC", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..11]));173 let key = self.make_key(node, "CC", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..10]));
174 if let Some(hit) = self.cache.get(key, node.id, frame_i) {174 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
175 stats.cache_hits += 1;175 stats.cache_hits += 1;
176 return hit;176 return hit;
...@@ -647,7 +647,7 @@ impl Engine {...@@ -647,7 +647,7 @@ impl Engine {
647 if src_sp == 0.0 && dst_sp == 0.0 {647 if src_sp == 0.0 && dst_sp == 0.0 {
648 return src;648 return src;
649 }649 }
650 let prediv = comp.eval_number(node, "PreDividePostMultiply", time, 1.0, &self.expr) as f32;650 let prediv = comp.eval_number(node, "PreDividePostMultiply", time, 0.0, &self.expr) as f32;
651 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);651 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);
652 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);652 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
653653
...@@ -696,9 +696,9 @@ impl Engine {...@@ -696,9 +696,9 @@ impl Engine {
696 node.connected_node(curve_names[idx]).and_then(|s| comp.splines.get(s))696 node.connected_node(curve_names[idx]).and_then(|s| comp.splines.get(s))
697 });697 });
698698
699 // Bake 256x3 RGBA8 LUT over Hue domain [0..6], encoded as (val / 4.0) * 255699 // Bake 256x3 RGBA8 LUT over Hue domain [0..6], encoded as val * 64 so neutral 1.0 is exact
700 let mut lut = vec![0u8; 256 * 3 * 4];700 let mut lut = vec![0u8; 256 * 3 * 4];
701 let enc_u8 = |v: f64| ((v / 4.0).clamp(0.0, 1.0) * 255.0).round() as u8;701 let enc_u8 = |v: f64| (v * 64.0).clamp(0.0, 255.0).round() as u8;
702 for i in 0..256 {702 for i in 0..256 {
703 let hx = (i as f64 / 255.0) * 6.0;703 let hx = (i as f64 / 255.0) * 6.0;
704 let eval_c = |ci: usize| curves[ci].map_or(1.0, |s| s.eval(hx));704 let eval_c = |ci: usize| curves[ci].map_or(1.0, |s| s.eval(hx));
crates/engine/src/shaders/color.wgsl+38-30
...@@ -4,6 +4,7 @@...@@ -4,6 +4,7 @@
4// p[1] = (brightness, saturation, prediv, blend)4// p[1] = (brightness, saturation, prediv, blend)
5// p[2] = (has_mask, mask_invert, low, high)5// p[2] = (has_mask, mask_invert, low, high)
6// p[3] = (proc_r, proc_g, proc_b, proc_a)6// p[3] = (proc_r, proc_g, proc_b, proc_a)
7// p[4] = (clip_black, clip_white, 0, 0)
7// ============================================================================8// ============================================================================
8fn bc_curve_scalar(v_in: f32, low: f32, high: f32, gain: f32, lift: f32, gamma: f32, contrast: f32, brightness: f32) -> f32 {9fn bc_curve_scalar(v_in: f32, low: f32, high: f32, gain: f32, lift: f32, gamma: f32, contrast: f32, brightness: f32) -> f32 {
9 var v = v_in;10 var v = v_in;
...@@ -12,7 +13,9 @@ fn bc_curve_scalar(v_in: f32, low: f32, high: f32, gain: f32, lift: f32, gamma:...@@ -12,7 +13,9 @@ fn bc_curve_scalar(v_in: f32, low: f32, high: f32, gain: f32, lift: f32, gamma:
12 v = (v - low) / span;13 v = (v - low) / span;
13 }14 }
14 v = v * (gain - lift) + lift;15 v = v * (gain - lift) + lift;
15 v = pow(max(v, 0.0), 1.0 / gamma);16 if (gamma != 1.0) {
17 v = pow(max(v, 0.0), 1.0 / gamma);
18 }
16 v = (v - 0.5) * (1.0 + contrast) + 0.5;19 v = (v - 0.5) * (1.0 + contrast) + 0.5;
17 v = v + brightness;20 v = v + brightness;
18 return v;21 return v;
...@@ -49,6 +52,8 @@ fn bc_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -49,6 +52,8 @@ fn bc_op(@builtin(global_invocation_id) gid: vec3<u32>) {
49 );52 );
50 let luma = dot(adj_rgb, vec3<f32>(0.299, 0.587, 0.114));53 let luma = dot(adj_rgb, vec3<f32>(0.299, 0.587, 0.114));
51 adj_rgb = mix(vec3<f32>(luma), adj_rgb, sat);54 adj_rgb = mix(vec3<f32>(luma), adj_rgb, sat);
55 if (u.p[4].x > 0.5) { adj_rgb = max(adj_rgb, vec3<f32>(0.0)); }
56 if (u.p[4].y > 0.5) { adj_rgb = min(adj_rgb, vec3<f32>(1.0)); }
5257
53 let out_rgb_unpre = vec3<f32>(58 let out_rgb_unpre = vec3<f32>(
54 select(rgb.r, adj_rgb.r, proc_r),59 select(rgb.r, adj_rgb.r, proc_r),
...@@ -72,11 +77,10 @@ fn bc_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -72,11 +77,10 @@ fn bc_op(@builtin(global_invocation_id) gid: vec3<u32>) {
72// p[3] = (mask_invert, tint_mode, has_smh, 0)77// p[3] = (mask_invert, tint_mode, has_smh, 0)
73// p[4] = (hue2, sat2, tint_angle2, tint_len2) // Shadows78// p[4] = (hue2, sat2, tint_angle2, tint_len2) // Shadows
74// p[5] = (gain2, lift2, gamma2, contrast2)79// p[5] = (gain2, lift2, gamma2, contrast2)
75// p[6] = (hue3, sat3, tint_angle3, tint_len3) // Midtones80// p[6] = (hue3, sat3, tint_angle3, tint_len3) // Midtones (wheel only)
76// p[7] = (gain3, lift3, gamma3, contrast3)81// p[7] = (hue4, sat4, tint_angle4, tint_len4) // Highlights
77// p[8] = (hue4, sat4, tint_angle4, tint_len4) // Highlights82// p[8] = (gain4, lift4, gamma4, contrast4)
78// p[9] = (gain4, lift4, gamma4, contrast4)83// p[9] = (bright2, bright4, 0, 0)
79// p[10] = (bright2, bright3, bright4, 0)
80// ============================================================================84// ============================================================================
81fn rotate_hue_preserve_luma(c: vec3<f32>, angle: f32) -> vec3<f32> {85fn rotate_hue_preserve_luma(c: vec3<f32>, angle: f32) -> vec3<f32> {
82 // Paul Haeberli luminance-preserving shear-rotate-unshear around (1,1,1) with Rec.601 weights.86 // Paul Haeberli luminance-preserving shear-rotate-unshear around (1,1,1) with Rec.601 weights.
...@@ -189,6 +193,20 @@ fn apply_cc_wheel_and_lgg(...@@ -189,6 +193,20 @@ fn apply_cc_wheel_and_lgg(
189 return rgb;193 return rgb;
190}194}
191195
196/// Default Fusion shadows range: one cubic Bezier with control points (0,1) (.4,.2) (.6,0) (1,0).
197fn cc_shadow_weight(y: f32) -> f32 {
198 var lo = 0.0;
199 var hi = 1.0;
200 for (var i = 0; i < 14; i++) {
201 let t = 0.5 * (lo + hi);
202 let m = 1.0 - t;
203 if (3.0 * m * m * t * 0.4 + 3.0 * m * t * t * 0.6 + t * t * t < y) { lo = t; } else { hi = t; }
204 }
205 let t = 0.5 * (lo + hi);
206 let m = 1.0 - t;
207 return m * m * m + 3.0 * m * m * t * 0.2;
208}
209
192@compute @workgroup_size(8, 8)210@compute @workgroup_size(8, 8)
193fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) {211fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) {
194 let dims = textureDimensions(dst_tex);212 let dims = textureDimensions(dst_tex);
...@@ -204,19 +222,19 @@ fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -204,19 +222,19 @@ fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) {
204 rgb = rgb / src.a;222 rgb = rgb / src.a;
205 }223 }
206224
225 rgb = apply_cc_wheel_and_lgg(rgb, u.p[0].x, u.p[0].y, u.p[0].z, u.p[0].w, tint_mode, u.p[1].x, u.p[1].y, u.p[1].z, u.p[1].w, u.p[2].x);
226
207 if (has_smh) {227 if (has_smh) {
208 let y0 = clamp(dot(rgb, k_luma), 0.0, 1.0);228 let y0 = clamp(dot(rgb, k_luma), 0.0, 1.0);
209 let w_s = clamp(1.0 - 2.0 * y0, 0.0, 1.0);229 let w_s = cc_shadow_weight(y0);
210 let w_h = clamp(2.0 * y0 - 1.0, 0.0, 1.0);230 let w_h = cc_shadow_weight(1.0 - y0);
211 let w_m = max(1.0 - w_s - w_h, 0.0);231 let w_m = max(1.0 - w_s - w_h, 0.0);
212 let rgb_s = apply_cc_wheel_and_lgg(rgb, u.p[4].x, u.p[4].y, u.p[4].z, u.p[4].w, tint_mode, u.p[5].x, u.p[5].y, u.p[5].z, u.p[5].w, u.p[10].x);232 let rgb_s = apply_cc_wheel_and_lgg(rgb, u.p[4].x, u.p[4].y, u.p[4].z, u.p[4].w, tint_mode, u.p[5].x, u.p[5].y, u.p[5].z, u.p[5].w, u.p[9].x);
213 let rgb_m = apply_cc_wheel_and_lgg(rgb, u.p[6].x, u.p[6].y, u.p[6].z, u.p[6].w, tint_mode, u.p[7].x, u.p[7].y, u.p[7].z, u.p[7].w, u.p[10].y);233 let rgb_m = apply_cc_wheel_and_lgg(rgb, u.p[6].x, u.p[6].y, u.p[6].z, u.p[6].w, tint_mode, 1.0, 0.0, 1.0, 1.0, 0.0);
214 let rgb_h = apply_cc_wheel_and_lgg(rgb, u.p[8].x, u.p[8].y, u.p[8].z, u.p[8].w, tint_mode, u.p[9].x, u.p[9].y, u.p[9].z, u.p[9].w, u.p[10].z);234 let rgb_h = apply_cc_wheel_and_lgg(rgb, u.p[7].x, u.p[7].y, u.p[7].z, u.p[7].w, tint_mode, u.p[8].x, u.p[8].y, u.p[8].z, u.p[8].w, u.p[9].y);
215 rgb = rgb_s * w_s + rgb_m * w_m + rgb_h * w_h;235 rgb = rgb_s * w_s + rgb_m * w_m + rgb_h * w_h;
216 }236 }
217237
218 rgb = apply_cc_wheel_and_lgg(rgb, u.p[0].x, u.p[0].y, u.p[0].z, u.p[0].w, tint_mode, u.p[1].x, u.p[1].y, u.p[1].z, u.p[1].w, u.p[2].x);
219
220 if (prediv) {238 if (prediv) {
221 rgb = rgb * src.a;239 rgb = rgb * src.a;
222 }240 }
...@@ -462,7 +480,7 @@ fn gamut_convert_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -462,7 +480,7 @@ fn gamut_convert_op(@builtin(global_invocation_id) gid: vec3<u32>) {
462// ============================================================================480// ============================================================================
463// 25. HueCurves (9 hue-indexed LUT curves baked into 256x3 RGBA8 texture in src_b)481// 25. HueCurves (9 hue-indexed LUT curves baked into 256x3 RGBA8 texture in src_b)
464// Row 0: (Hue, Sat, Lum, 1), Row 1: (Red, Green, Blue, 1), Row 2: (RedSup, GreenSup, BlueSup, 1)482// Row 0: (Hue, Sat, Lum, 1), Row 1: (Red, Green, Blue, 1), Row 2: (RedSup, GreenSup, BlueSup, 1)
465// All encoded as val / 4.0 so neutral 1.0 == 0.25 and boost up to 4.0x fits cleanly.483// All encoded as val * 64 / 255 so neutral 1.0 is exactly 64/255 and values up to 3.98 fit.
466// p[0] = (prediv, blend, has_mask, mask_invert)484// p[0] = (prediv, blend, has_mask, mask_invert)
467// ============================================================================485// ============================================================================
468@compute @workgroup_size(8, 8)486@compute @workgroup_size(8, 8)
...@@ -477,14 +495,15 @@ fn hue_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -477,14 +495,15 @@ fn hue_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) {
477 if (prediv && src.a > 1e-5) {495 if (prediv && src.a > 1e-5) {
478 rgb = rgb / src.a;496 rgb = rgb / src.a;
479 }497 }
498 let min_c = min(rgb.r, min(rgb.g, rgb.b));
480 let c_pos = max(rgb, vec3<f32>(0.0));499 let c_pos = max(rgb, vec3<f32>(0.0));
481 let hsv = rgb_to_hsv(c_pos);500 let hsv = rgb_to_hsv(c_pos);
482 let h_u = fract(hsv.x);501 let h_u = fract(hsv.x);
483 let sat = hsv.y;502 let sat = hsv.y;
484503
485 let row0 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 0.5 / 3.0), 0.0) * 4.0;504 let row0 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 0.5 / 3.0), 0.0) * (255.0 / 64.0);
486 let row1 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 1.5 / 3.0), 0.0) * 4.0;505 let row1 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 1.5 / 3.0), 0.0) * (255.0 / 64.0);
487 let row2 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 2.5 / 3.0), 0.0) * 4.0;506 let row2 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 2.5 / 3.0), 0.0) * (255.0 / 64.0);
488507
489 // 1. Hue shift (curve neutral=1.0, domain 0..6 => delta = (hue_v - 1.0) / 6.0)508 // 1. Hue shift (curve neutral=1.0, domain 0..6 => delta = (hue_v - 1.0) / 6.0)
490 if (abs(row0.x - 1.0) > 1e-3 && sat > 1e-4) {509 if (abs(row0.x - 1.0) > 1e-3 && sat > 1e-4) {
...@@ -501,19 +520,8 @@ fn hue_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -501,19 +520,8 @@ fn hue_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) {
501 }520 }
502 // 4. Per-channel Red / Green / Blue multipliers521 // 4. Per-channel Red / Green / Blue multipliers
503 rgb = rgb * row1.xyz;522 rgb = rgb * row1.xyz;
504 // 5. Red / Green / Blue spill suppression523 // 5. Red / Green / Blue suppression toward the source minimum channel
505 if (row2.x < 0.999) {524 rgb = mix(vec3<f32>(min_c), rgb, row2.xyz);
506 let lim = max(rgb.g, rgb.b);
507 if (rgb.r > lim) { rgb.r = mix(lim, rgb.r, row2.x); }
508 }
509 if (row2.y < 0.999) {
510 let lim = max(rgb.r, rgb.b);
511 if (rgb.g > lim) { rgb.g = mix(lim, rgb.g, row2.y); }
512 }
513 if (row2.z < 0.999) {
514 let lim = max(rgb.r, rgb.g);
515 if (rgb.b > lim) { rgb.b = mix(lim, rgb.b, row2.z); }
516 }
517525
518 if (prediv) {526 if (prediv) {
519 rgb = rgb * src.a;527 rgb = rgb * src.a;