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 {
4848 comp.eval_number(node, "Blue", time, 1.0, &self.expr) as f32,
4949 comp.eval_number(node, "Alpha", time, 0.0, &self.expr) as f32,
5050 ];
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]));
5359 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
5460 stats.cache_hits += 1;
5561 return hit;
......@@ -127,28 +133,22 @@ impl Engine {
127133 comp.eval_number(node, "WheelTintLength3", time, 0.0, &self.expr) as f32,
128134 ];
129135 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] = [
136136 comp.eval_number(node, "WheelHue4", time, 0.0, &self.expr) as f32,
137137 comp.eval_number(node, "WheelSaturation4", time, 1.0, &self.expr) as f32,
138138 comp.eval_number(node, "WheelTintAngle4", time, 0.0, &self.expr) as f32,
139139 comp.eval_number(node, "WheelTintLength4", time, 0.0, &self.expr) as f32,
140140 ];
141 u.p[9] = [
141 u.p[8] = [
142142 comp.eval_number(node, "HighlightsRGBGain", time, 1.0, &self.expr) as f32,
143143 comp.eval_number(node, "HighlightsRGBLift", time, 0.0, &self.expr) as f32,
144144 comp.eval_number(node, "HighlightsRGBGamma", time, 1.0, &self.expr) as f32,
145145 comp.eval_number(node, "HighlightsRGBContrast", time, 1.0, &self.expr) as f32,
146146 ];
147 u.p[10] = [
147 u.p[9] = [
148148 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,
150149 comp.eval_number(node, "HighlightsRGBBrightness", time, 0.0, &self.expr) as f32,
151150 0.0,
151 0.0,
152152 ];
153153 let wheel_is_active = |w: [f32; 4], lgg: [f32; 4], b: f32| -> bool {
154154 w[0].abs() > 1e-5
......@@ -160,9 +160,9 @@ impl Engine {
160160 || (lgg[3] - 1.0).abs() > 1e-5
161161 || b.abs() > 1e-5
162162 };
163 let has_smh = wheel_is_active(u.p[4], u.p[5], u.p[10][0])
164 || wheel_is_active(u.p[6], u.p[7], u.p[10][1])
165 || wheel_is_active(u.p[8], u.p[9], u.p[10][2]);
163 let has_smh = wheel_is_active(u.p[4], u.p[5], u.p[9][0])
164 || wheel_is_active(u.p[6], [1.0, 0.0, 1.0, 1.0], 0.0)
165 || wheel_is_active(u.p[7], u.p[8], u.p[9][1]);
166166 u.p[3] = [
167167 if mask_inv { 1.0 } else { 0.0 },
168168 tint_mode,
......@@ -170,7 +170,7 @@ impl Engine {
170170 0.0,
171171 ];
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]));
174174 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
175175 stats.cache_hits += 1;
176176 return hit;
......@@ -647,7 +647,7 @@ impl Engine {
647647 if src_sp == 0.0 && dst_sp == 0.0 {
648648 return src;
649649 }
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;
651651 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);
652652 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
653653
......@@ -696,9 +696,9 @@ impl Engine {
696696 node.connected_node(curve_names[idx]).and_then(|s| comp.splines.get(s))
697697 });
698698
699 // Bake 256x3 RGBA8 LUT over Hue domain [0..6], encoded as (val / 4.0) * 255
699 // Bake 256x3 RGBA8 LUT over Hue domain [0..6], encoded as val * 64 so neutral 1.0 is exact
700700 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;
702702 for i in 0..256 {
703703 let hx = (i as f64 / 255.0) * 6.0;
704704 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 @@
44// p[1] = (brightness, saturation, prediv, blend)
55// p[2] = (has_mask, mask_invert, low, high)
66// p[3] = (proc_r, proc_g, proc_b, proc_a)
7// p[4] = (clip_black, clip_white, 0, 0)
78// ============================================================================
89fn bc_curve_scalar(v_in: f32, low: f32, high: f32, gain: f32, lift: f32, gamma: f32, contrast: f32, brightness: f32) -> f32 {
910 var v = v_in;
......@@ -12,7 +13,9 @@ fn bc_curve_scalar(v_in: f32, low: f32, high: f32, gain: f32, lift: f32, gamma:
1213 v = (v - low) / span;
1314 }
1415 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 }
1619 v = (v - 0.5) * (1.0 + contrast) + 0.5;
1720 v = v + brightness;
1821 return v;
......@@ -49,6 +52,8 @@ fn bc_op(@builtin(global_invocation_id) gid: vec3<u32>) {
4952 );
5053 let luma = dot(adj_rgb, vec3<f32>(0.299, 0.587, 0.114));
5154 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
5358 let out_rgb_unpre = vec3<f32>(
5459 select(rgb.r, adj_rgb.r, proc_r),
......@@ -72,11 +77,10 @@ fn bc_op(@builtin(global_invocation_id) gid: vec3<u32>) {
7277// p[3] = (mask_invert, tint_mode, has_smh, 0)
7378// p[4] = (hue2, sat2, tint_angle2, tint_len2) // Shadows
7479// p[5] = (gain2, lift2, gamma2, contrast2)
75// p[6] = (hue3, sat3, tint_angle3, tint_len3) // Midtones
76// p[7] = (gain3, lift3, gamma3, contrast3)
77// p[8] = (hue4, sat4, tint_angle4, tint_len4) // Highlights
78// p[9] = (gain4, lift4, gamma4, contrast4)
79// p[10] = (bright2, bright3, bright4, 0)
80// p[6] = (hue3, sat3, tint_angle3, tint_len3) // Midtones (wheel only)
81// p[7] = (hue4, sat4, tint_angle4, tint_len4) // Highlights
82// p[8] = (gain4, lift4, gamma4, contrast4)
83// p[9] = (bright2, bright4, 0, 0)
8084// ============================================================================
8185fn rotate_hue_preserve_luma(c: vec3<f32>, angle: f32) -> vec3<f32> {
8286 // 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(
189193 return rgb;
190194}
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
192210@compute @workgroup_size(8, 8)
193211fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) {
194212 let dims = textureDimensions(dst_tex);
......@@ -204,19 +222,19 @@ fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) {
204222 rgb = rgb / src.a;
205223 }
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
207227 if (has_smh) {
208228 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);
210 let w_h = clamp(2.0 * y0 - 1.0, 0.0, 1.0);
229 let w_s = cc_shadow_weight(y0);
230 let w_h = cc_shadow_weight(1.0 - y0);
211231 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);
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);
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);
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);
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);
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);
215235 rgb = rgb_s * w_s + rgb_m * w_m + rgb_h * w_h;
216236 }
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
220238 if (prediv) {
221239 rgb = rgb * src.a;
222240 }
......@@ -462,7 +480,7 @@ fn gamut_convert_op(@builtin(global_invocation_id) gid: vec3<u32>) {
462480// ============================================================================
463481// 25. HueCurves (9 hue-indexed LUT curves baked into 256x3 RGBA8 texture in src_b)
464482// 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.
466484// p[0] = (prediv, blend, has_mask, mask_invert)
467485// ============================================================================
468486@compute @workgroup_size(8, 8)
......@@ -477,14 +495,15 @@ fn hue_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) {
477495 if (prediv && src.a > 1e-5) {
478496 rgb = rgb / src.a;
479497 }
498 let min_c = min(rgb.r, min(rgb.g, rgb.b));
480499 let c_pos = max(rgb, vec3<f32>(0.0));
481500 let hsv = rgb_to_hsv(c_pos);
482501 let h_u = fract(hsv.x);
483502 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;
486 let row1 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 1.5 / 3.0), 0.0) * 4.0;
487 let row2 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 2.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);
505 let row1 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 1.5 / 3.0), 0.0) * (255.0 / 64.0);
506 let row2 = textureSampleLevel(src_b, samp_lin, vec2<f32>(h_u, 2.5 / 3.0), 0.0) * (255.0 / 64.0);
488507
489508 // 1. Hue shift (curve neutral=1.0, domain 0..6 => delta = (hue_v - 1.0) / 6.0)
490509 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>) {
501520 }
502521 // 4. Per-channel Red / Green / Blue multipliers
503522 rgb = rgb * row1.xyz;
504 // 5. Red / Green / Blue spill suppression
505 if (row2.x < 0.999) {
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 }
523 // 5. Red / Green / Blue suppression toward the source minimum channel
524 rgb = mix(vec3<f32>(min_c), rgb, row2.xyz);
517525
518526 if (prediv) {
519527 rgb = rgb * src.a;