| author | |
| committer | |
| log | fb78b3a9c56af8086ec667546acb77c4163db707 |
| tree | 95f1cd24e3249589ffb4f0ca7a2d16ece677bb23 |
| parent | fe513701b0974d938300ae17e90f83a602883531 |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
- ColorCorrector: Master/Shadows/MidTones/Highlights x RGB/Red/Green/Blue Contrast/Gain/Lift/Gamma/Brightness/Low/Mid/High/Output curves via a 12x3 curve table (src_b); Process toggles. cc_op keeps the legacy uniform layout for transform.rs's RoI path (p[11].x == 0)
- UltraKeyer: untouched Background keys b - r and premultiplies (CustomTool program), unset Background channels default to 0.5, SpillSuppression needs a SpillMethod, blue-before-green tie-break, MultiplyByMask/Process combine pass
- LumaKeyer: PostMultiplyImage=0 and Process toggles via a combine pass
Assisted-by: claude-sonnet-5.53 files changed, 259 insertions(+), 113 deletions(-)
crates/engine/src/eval/color.rs+114-77| ... | ... | @@ -1,5 +1,67 @@ |
| 1 | 1 | use super::*; |
| 2 | 2 | |
| 3 | /// One ColorCorrector "Levels" block (`Master`/`Shadows`/`MidTones`/`Highlights` x `RGB`/`Red`/`Green`/`Blue`). | |
| 4 | #[derive(Clone, Copy)] | |
| 5 | struct CcLevels { | |
| 6 | contrast: f32, | |
| 7 | gain: f32, | |
| 8 | lift: f32, | |
| 9 | gamma: f32, | |
| 10 | brightness: f32, | |
| 11 | low: f32, | |
| 12 | mid: f32, | |
| 13 | high: f32, | |
| 14 | out_low: f32, | |
| 15 | out_high: f32, | |
| 16 | } | |
| 17 | ||
| 18 | impl CcLevels { | |
| 19 | fn read(comp: &Comp, node: &Node, time: f64, range: &str, expr: &ExprEngine) -> [CcLevels; 4] { | |
| 20 | ["RGB", "Red", "Green", "Blue"].map(|set| { | |
| 21 | let n = |name: &str, default: f64| comp.eval_number(node, &format!("{range}{set}{name}"), time, default, expr) as f32; | |
| 22 | CcLevels { | |
| 23 | contrast: n("Contrast", 1.0), | |
| 24 | gain: n("Gain", 1.0), | |
| 25 | lift: n("Lift", 0.0), | |
| 26 | gamma: n("Gamma", 1.0).max(1e-4), | |
| 27 | brightness: n("Brightness", 0.0), | |
| 28 | low: n("Low", 0.0), | |
| 29 | mid: n("Mid", 1.0).max(1e-4), | |
| 30 | high: n("High", 1.0), | |
| 31 | out_low: n("OutputLow", 0.0), | |
| 32 | out_high: n("OutputHigh", 1.0), | |
| 33 | } | |
| 34 | }) | |
| 35 | } | |
| 36 | ||
| 37 | fn is_neutral(&self) -> bool { | |
| 38 | [self.contrast, self.gain, self.gamma, self.mid, self.high, self.out_high].iter().all(|v| (v - 1.0).abs() < 1e-6) | |
| 39 | && [self.lift, self.brightness, self.low, self.out_low].iter().all(|v| v.abs() < 1e-6) | |
| 40 | } | |
| 41 | ||
| 42 | /// Effective curve for a chain of blocks, as three texels: `(contrast, slope, offset, brightness)`, | |
| 43 | /// `(in_scale, in_offset, gamma, 0)` and `(out_scale, out_offset, 0, 0)`. Contrast, slope and gamma multiply while lift and | |
| 44 | /// brightness add; the input and output ranges compose one after another. | |
| 45 | fn combine(chain: &[&CcLevels]) -> [[f32; 4]; 3] { | |
| 46 | let (mut contrast, mut slope, mut offset, mut bright, mut gamma) = (1.0, 1.0, 0.0, 0.0, 1.0); | |
| 47 | let (mut in_scale, mut in_offset, mut out_scale, mut out_offset) = (1.0, 0.0, 1.0, 0.0); | |
| 48 | for l in chain { | |
| 49 | contrast *= l.contrast; | |
| 50 | slope *= l.gain - l.lift; | |
| 51 | offset += l.lift; | |
| 52 | bright += l.brightness; | |
| 53 | gamma *= l.gamma * l.mid; | |
| 54 | let span = (l.high - l.low).max(1e-5); | |
| 55 | in_scale /= span; | |
| 56 | in_offset = (in_offset - l.low) / span; | |
| 57 | let out_span = l.out_high - l.out_low; | |
| 58 | out_scale *= out_span; | |
| 59 | out_offset = out_offset * out_span + l.out_low; | |
| 60 | } | |
| 61 | [[contrast, slope, offset, bright], [in_scale, in_offset, gamma, 0.0], [out_scale, out_offset, 0.0, 0.0]] | |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 3 | 65 | impl Engine { |
| 4 | 66 | /// Resolves `EffectMask` on `node`, walking tool-level `SourceOp` instances when `EffectMask` |
| 5 | 67 | /// is not explicitly present (connected or cleared) in `node`'s own `Inputs` table. |
| ... | ... | @@ -193,95 +255,70 @@ impl Engine { |
| 193 | 255 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); |
| 194 | 256 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 195 | 257 | |
| 196 | let mut u = UniformParams::default(); | |
| 197 | u.p[0] = [ | |
| 198 | comp.eval_number(node, "WheelHue1", time, 0.0, &self.expr) as f32, | |
| 199 | comp.eval_number(node, "WheelSaturation1", time, 1.0, &self.expr) as f32, | |
| 200 | comp.eval_number(node, "WheelTintAngle1", time, 0.0, &self.expr) as f32, | |
| 201 | comp.eval_number(node, "WheelTintLength1", time, 0.0, &self.expr) as f32, | |
| 202 | ]; | |
| 203 | u.p[1] = [ | |
| 204 | comp.eval_number(node, "MasterRGBGain", time, 1.0, &self.expr) as f32, | |
| 205 | comp.eval_number(node, "MasterRGBLift", time, 0.0, &self.expr) as f32, | |
| 206 | comp.eval_number(node, "MasterRGBGamma", time, 1.0, &self.expr) as f32, | |
| 207 | comp.eval_number(node, "MasterRGBContrast", time, 1.0, &self.expr) as f32, | |
| 208 | ]; | |
| 209 | u.p[2] = [ | |
| 210 | comp.eval_number(node, "MasterRGBBrightness", time, 0.0, &self.expr) as f32, | |
| 211 | comp.eval_number(node, "PreDividePostMultiply", time, 0.0, &self.expr) as f32, | |
| 212 | blend, | |
| 213 | if mask.is_some() { 1.0 } else { 0.0 }, | |
| 214 | ]; | |
| 258 | let levels: [[CcLevels; 4]; 4] = ["Master", "Shadows", "MidTones", "Highlights"] | |
| 259 | .map(|range| CcLevels::read(comp, node, time, range, &self.expr)); | |
| 260 | let wheel = |i: usize| -> [f32; 4] { | |
| 261 | let n = |name: &str, default: f64| comp.eval_number(node, &format!("{name}{i}"), time, default, &self.expr) as f32; | |
| 262 | [n("WheelHue", 0.0), n("WheelSaturation", 1.0), n("WheelTintAngle", 0.0), n("WheelTintLength", 0.0)] | |
| 263 | }; | |
| 264 | let wheels = [wheel(1), wheel(2), wheel(3), wheel(4)]; | |
| 215 | 265 | // Fusion ColorCorrector TintMode: 0 = RGB cube tilt matrix, 1 = additive+luma tint, 2 (default when omitted) = luma-weighted tint. |
| 216 | 266 | let tint_mode = comp.eval_number(node, "TintMode", time, 2.0, &self.expr) as f32; |
| 267 | let wheel_is_active = |w: [f32; 4]| w[0].abs() > 1e-5 || (w[1] - 1.0).abs() > 1e-5 || w[3].abs() > 1e-5; | |
| 268 | let has_smh = (1..4).any(|r| wheel_is_active(wheels[r]) || levels[r].iter().any(|l| !l.is_neutral())); | |
| 217 | 269 | |
| 218 | // Shadows (2), Midtones (3), Highlights (4) wheels & LGG | |
| 219 | u.p[4] = [ | |
| 220 | comp.eval_number(node, "WheelHue2", time, 0.0, &self.expr) as f32, | |
| 221 | comp.eval_number(node, "WheelSaturation2", time, 1.0, &self.expr) as f32, | |
| 222 | comp.eval_number(node, "WheelTintAngle2", time, 0.0, &self.expr) as f32, | |
| 223 | comp.eval_number(node, "WheelTintLength2", time, 0.0, &self.expr) as f32, | |
| 224 | ]; | |
| 225 | u.p[5] = [ | |
| 226 | comp.eval_number(node, "ShadowsRGBGain", time, 1.0, &self.expr) as f32, | |
| 227 | comp.eval_number(node, "ShadowsRGBLift", time, 0.0, &self.expr) as f32, | |
| 228 | comp.eval_number(node, "ShadowsRGBGamma", time, 1.0, &self.expr) as f32, | |
| 229 | comp.eval_number(node, "ShadowsRGBContrast", time, 1.0, &self.expr) as f32, | |
| 230 | ]; | |
| 231 | u.p[6] = [ | |
| 232 | comp.eval_number(node, "WheelHue3", time, 0.0, &self.expr) as f32, | |
| 233 | comp.eval_number(node, "WheelSaturation3", time, 1.0, &self.expr) as f32, | |
| 234 | comp.eval_number(node, "WheelTintAngle3", time, 0.0, &self.expr) as f32, | |
| 235 | comp.eval_number(node, "WheelTintLength3", time, 0.0, &self.expr) as f32, | |
| 236 | ]; | |
| 237 | u.p[7] = [ | |
| 238 | comp.eval_number(node, "WheelHue4", time, 0.0, &self.expr) as f32, | |
| 239 | comp.eval_number(node, "WheelSaturation4", time, 1.0, &self.expr) as f32, | |
| 240 | comp.eval_number(node, "WheelTintAngle4", time, 0.0, &self.expr) as f32, | |
| 241 | comp.eval_number(node, "WheelTintLength4", time, 0.0, &self.expr) as f32, | |
| 242 | ]; | |
| 243 | u.p[8] = [ | |
| 244 | comp.eval_number(node, "HighlightsRGBGain", time, 1.0, &self.expr) as f32, | |
| 245 | comp.eval_number(node, "HighlightsRGBLift", time, 0.0, &self.expr) as f32, | |
| 246 | comp.eval_number(node, "HighlightsRGBGamma", time, 1.0, &self.expr) as f32, | |
| 247 | comp.eval_number(node, "HighlightsRGBContrast", time, 1.0, &self.expr) as f32, | |
| 248 | ]; | |
| 249 | u.p[9] = [ | |
| 250 | comp.eval_number(node, "ShadowsRGBBrightness", time, 0.0, &self.expr) as f32, | |
| 251 | comp.eval_number(node, "HighlightsRGBBrightness", time, 0.0, &self.expr) as f32, | |
| 252 | 0.0, | |
| 253 | 0.0, | |
| 254 | ]; | |
| 255 | let wheel_is_active = |w: [f32; 4], lgg: [f32; 4], b: f32| -> bool { | |
| 256 | w[0].abs() > 1e-5 | |
| 257 | || (w[1] - 1.0).abs() > 1e-5 | |
| 258 | || w[3].abs() > 1e-5 | |
| 259 | || (lgg[0] - 1.0).abs() > 1e-5 | |
| 260 | || lgg[1].abs() > 1e-5 | |
| 261 | || (lgg[2] - 1.0).abs() > 1e-5 | |
| 262 | || (lgg[3] - 1.0).abs() > 1e-5 | |
| 263 | || b.abs() > 1e-5 | |
| 264 | }; | |
| 265 | let has_smh = wheel_is_active(u.p[4], u.p[5], u.p[9][0]) | |
| 266 | || wheel_is_active(u.p[6], [1.0, 0.0, 1.0, 1.0], 0.0) | |
| 267 | || wheel_is_active(u.p[7], u.p[8], u.p[9][1]); | |
| 268 | u.p[3] = [ | |
| 269 | if mask_inv { 1.0 } else { 0.0 }, | |
| 270 | tint_mode, | |
| 271 | if has_smh { 1.0 } else { 0.0 }, | |
| 272 | if mbm { 1.0 } else { 0.0 }, | |
| 273 | ]; | |
| 270 | let mut u = UniformParams::default(); | |
| 271 | u.p[0] = wheels[0]; | |
| 272 | u.p[2] = [0.0, comp.eval_number(node, "PreDividePostMultiply", time, 0.0, &self.expr) as f32, blend, if mask.is_some() { 1.0 } else { 0.0 }]; | |
| 273 | u.p[3] = [mask_inv as u8 as f32, tint_mode, has_smh as u8 as f32, mbm as u8 as f32]; | |
| 274 | u.p[4] = wheels[1]; | |
| 275 | u.p[6] = wheels[2]; | |
| 276 | u.p[7] = wheels[3]; | |
| 277 | u.p[10] = self.process_skip(comp, node, time); | |
| 278 | u.p[11][0] = 1.0; | |
| 279 | ||
| 280 | // Per range (master alone, then master combined with each of Shadows/MidTones/Highlights) and channel: 3 texels of effective curve terms. | |
| 281 | let mut table = [0u16; 12 * 3 * 4]; | |
| 282 | let mut table_f32 = Vec::with_capacity(12 * 3 * 4); | |
| 283 | for range in 0..4 { | |
| 284 | for ch in 0..3 { | |
| 285 | let mut chain = vec![&levels[0][0], &levels[0][1 + ch]]; | |
| 286 | if range > 0 { | |
| 287 | chain.extend([&levels[range][0], &levels[range][1 + ch]]); | |
| 288 | } | |
| 289 | let texels = CcLevels::combine(&chain); | |
| 290 | for (k, texel) in texels.iter().enumerate() { | |
| 291 | for (c, v) in texel.iter().enumerate() { | |
| 292 | table[((ch * 12) + range * 3 + k) * 4 + c] = crate::media::f32_to_f16_bits(*v); | |
| 293 | table_f32.push(*v); | |
| 294 | } | |
| 295 | } | |
| 296 | } | |
| 297 | } | |
| 274 | 298 | |
| 275 | let key = self.make_key(node, "CC", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..10])); | |
| 299 | let key = self.make_key( | |
| 300 | node, | |
| 301 | "CC", | |
| 302 | src.size, | |
| 303 | &[src.content_hash, mask_hash], | |
| 304 | &[bytemuck::cast_slice::<[f32; 4], f32>(&u.p[..12]), &table_f32].concat(), | |
| 305 | ); | |
| 276 | 306 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 277 | 307 | stats.cache_hits += 1; |
| 278 | 308 | return hit; |
| 279 | 309 | } |
| 280 | 310 | stats.cache_misses += 1; |
| 281 | 311 | stats.nodes_evaluated += 1; |
| 312 | let curve_tex = create_texture_dod_fmt(&self.device, [12, 3], [0, 0], [12, 3], BitDepth::Float16, wgpu::TextureFormat::Rgba16Float, 0); | |
| 313 | self.queue.write_texture( | |
| 314 | curve_tex.texture.as_image_copy(), | |
| 315 | bytemuck::cast_slice(&table), | |
| 316 | wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(12 * 8), rows_per_image: Some(3) }, | |
| 317 | wgpu::Extent3d { width: 12, height: 3, depth_or_array_layers: 1 }, | |
| 318 | ); | |
| 282 | 319 | let dst = self.alloc_tex_like(&src, src.depth, key); |
| 283 | 320 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 284 | self.dispatch_op(enc, ShaderOp::Cc, &src.view, &self.dummy.view, mv, &dst, &u); | |
| 321 | self.dispatch_op(enc, ShaderOp::Cc, &src.view, &curve_tex.view, mv, &dst, &u); | |
| 285 | 322 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 286 | 323 | dst |
| 287 | 324 | } |
crates/engine/src/eval/keyers.rs+100-11| ... | ... | @@ -23,6 +23,8 @@ impl Engine { |
| 23 | 23 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); |
| 24 | 24 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 25 | 25 | |
| 26 | let post_multiply = comp.eval_number(node, "PostMultiplyImage", time, 1.0, &self.expr) > 0.5; | |
| 27 | let skip = self.process_skip(comp, node, time); | |
| 26 | 28 | let mut u = UniformParams::default(); |
| 27 | 29 | u.p[0] = [ |
| 28 | 30 | comp.eval_number(node, "Low", time, 0.0, &self.expr) as f32, |
| ... | ... | @@ -32,7 +34,13 @@ impl Engine { |
| 32 | 34 | ]; |
| 33 | 35 | u.p[1] = [if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, 0.0, 0.0]; |
| 34 | 36 | |
| 35 | let key = self.make_key(node, "LumaKeyer", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..2])); | |
| 37 | let key = self.make_key( | |
| 38 | node, | |
| 39 | "LumaKeyer", | |
| 40 | src.size, | |
| 41 | &[src.content_hash, mask_hash], | |
| 42 | &[bytemuck::cast_slice::<[f32; 4], f32>(&u.p[..2]), &[post_multiply as u8 as f32], &skip].concat(), | |
| 43 | ); | |
| 36 | 44 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 37 | 45 | stats.cache_hits += 1; |
| 38 | 46 | return hit; |
| ... | ... | @@ -41,7 +49,20 @@ impl Engine { |
| 41 | 49 | stats.nodes_evaluated += 1; |
| 42 | 50 | let dst = self.alloc_tex_like(&src, src.depth, key); |
| 43 | 51 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); |
| 44 | self.dispatch_op(enc, ShaderOp::LumaKey, &src.view, &self.dummy.view, mv, &dst, &u); | |
| 52 | if post_multiply && skip == [0.0; 4] { | |
| 53 | self.dispatch_op(enc, ShaderOp::LumaKey, &src.view, &self.dummy.view, mv, &dst, &u); | |
| 54 | } else { | |
| 55 | // The key pass always premultiplies; the combine pass puts the source back where colour is unpremultiplied or a channel is unprocessed. | |
| 56 | let keyed = self.alloc_tex_like(&src, src.depth, 0); | |
| 57 | self.dispatch_op(enc, ShaderOp::LumaKey, &src.view, &self.dummy.view, mv, &keyed, &u); | |
| 58 | let mut uc = UniformParams::default(); | |
| 59 | uc.p[0] = [1.0, 1.0, 0.0, 0.0]; | |
| 60 | uc.p[1] = [4.0, 0.0, 0.0, 0.0]; // mode 4 = MixOnly | |
| 61 | uc.p[3] = skip; | |
| 62 | let keep_rgb = if post_multiply { 0.0 } else { 1.0 }; | |
| 63 | uc.p[4] = [keep_rgb, keep_rgb, keep_rgb, 0.0]; | |
| 64 | self.dispatch_op(enc, ShaderOp::GlowAdd, &src.view, &keyed.view, &self.dummy.view, &dst, &uc); | |
| 65 | } | |
| 45 | 66 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 46 | 67 | dst |
| 47 | 68 | } |
| ... | ... | @@ -61,19 +82,27 @@ impl Engine { |
| 61 | 82 | }; |
| 62 | 83 | let src = self.eval_node(comp, src_id, time, enc, visiting, stats); |
| 63 | 84 | let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; |
| 64 | if blend <= 1e-6 { | |
| 85 | let mbm = self.multiplies_by_mask(comp, node, time); | |
| 86 | if blend <= 1e-6 && !mbm { | |
| 65 | 87 | return src; |
| 66 | 88 | } |
| 67 | 89 | let src = self.crop_to_canvas(enc, src); |
| 68 | let bg_r = comp.eval_number(node, "BackgroundRed", time, 0.0, &self.expr) as f32; | |
| 69 | let bg_g = comp.eval_number(node, "BackgroundGreen", time, 1.0, &self.expr) as f32; | |
| 70 | let bg_b = comp.eval_number(node, "BackgroundBlue", time, 0.0, &self.expr) as f32; | |
| 90 | let bg_r = comp.eval_number(node, "BackgroundRed", time, 0.5, &self.expr) as f32; | |
| 91 | let bg_g = comp.eval_number(node, "BackgroundGreen", time, 0.5, &self.expr) as f32; | |
| 92 | let mut bg_b = comp.eval_number(node, "BackgroundBlue", time, 0.5, &self.expr) as f32; | |
| 93 | let bg_untouched = ["BackgroundRed", "BackgroundGreen", "BackgroundBlue"].iter().all(|k| !node.inputs.contains_key(*k)); | |
| 71 | 94 | let sep = comp.eval_number(node, "MatteSeparation", time, 0.0, &self.expr) as f32; |
| 72 | 95 | let lo_th = comp.eval_number(node, "LowMatteThreshold", time, 0.0, &self.expr) as f32; |
| 73 | 96 | let hi_th = comp.eval_number(node, "High", time, 1.0, &self.expr) as f32; |
| 74 | 97 | let post_mult = comp.eval_number(node, "PostMultiplyImage", time, 1.0, &self.expr) as f32; |
| 75 | let spill = comp.eval_number(node, "SpillSuppression", time, 0.0, &self.expr) as f32; | |
| 98 | // SpillSuppression only acts once a SpillMethod is chosen. | |
| 99 | let spill = if comp.eval_number(node, "SpillMethod", time, 0.0, &self.expr) > 0.5 { | |
| 100 | comp.eval_number(node, "SpillSuppression", time, 1.0, &self.expr) as f32 | |
| 101 | } else { | |
| 102 | 0.0 | |
| 103 | }; | |
| 76 | 104 | let inv_matte = comp.eval_number(node, "InvertMatte", time, 0.0, &self.expr) as f32; |
| 105 | let gamma = comp.eval_number(node, "MatteGamma", time, 1.0, &self.expr).max(1e-3) as f32; | |
| 77 | 106 | let r_lo = comp.eval_number(node, "RedLow", time, 0.0, &self.expr) as f32; |
| 78 | 107 | let r_hi = comp.eval_number(node, "RedHigh", time, 1.0, &self.expr) as f32; |
| 79 | 108 | let g_lo = comp.eval_number(node, "GreenLow", time, 0.0, &self.expr) as f32; |
| ... | ... | @@ -84,14 +113,32 @@ impl Engine { |
| 84 | 113 | let l_hi = comp.eval_number(node, "LuminanceHigh", time, 1.0, &self.expr) as f32; |
| 85 | 114 | let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); |
| 86 | 115 | let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); |
| 116 | let skip = self.process_skip(comp, node, time); | |
| 117 | // The key passes know nothing of MultiplyByMask, and the generic one nothing of Process toggles: a combine pass applies them. | |
| 118 | let combine = mbm || (!bg_untouched && skip != [0.0; 4]); | |
| 119 | let (key_blend, key_mask) = if combine { (1.0, None) } else { (blend, mask.as_ref()) }; | |
| 120 | ||
| 121 | // A colour with no dominant channel keys nothing; equal top channels key blue before green. | |
| 122 | if !bg_untouched && bg_r == bg_g && bg_g == bg_b && !combine { | |
| 123 | return src; | |
| 124 | } | |
| 125 | if bg_b >= bg_g && bg_b >= bg_r { | |
| 126 | bg_b += 1e-5; | |
| 127 | } | |
| 87 | 128 | |
| 88 | 129 | let mut u = UniformParams::default(); |
| 89 | 130 | u.p[0] = [bg_r, bg_g, bg_b, sep]; |
| 90 | 131 | u.p[1] = [lo_th, hi_th, post_mult, spill]; |
| 91 | u.p[2] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, inv_matte]; | |
| 132 | u.p[2] = [key_blend, key_mask.is_some() as u8 as f32, mask_inv as u8 as f32, inv_matte]; | |
| 92 | 133 | u.p[3] = [r_lo, r_hi, g_lo, g_hi]; |
| 93 | 134 | u.p[4] = [b_lo, b_hi, l_lo, l_hi]; |
| 94 | let key = self.make_key(node, "UltraKeyer", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..5])); | |
| 135 | let key = self.make_key( | |
| 136 | node, | |
| 137 | "UltraKeyer", | |
| 138 | src.size, | |
| 139 | &[src.content_hash, mask_hash], | |
| 140 | &[bytemuck::cast_slice::<[f32; 4], f32>(&u.p[..5]), &[bg_untouched as u8 as f32, gamma, mbm as u8 as f32, blend], &skip].concat(), | |
| 141 | ); | |
| 95 | 142 | if let Some(hit) = self.cache.get(key, node.id, frame_i) { |
| 96 | 143 | stats.cache_hits += 1; |
| 97 | 144 | return hit; |
| ... | ... | @@ -99,8 +146,50 @@ impl Engine { |
| 99 | 146 | stats.cache_misses += 1; |
| 100 | 147 | stats.nodes_evaluated += 1; |
| 101 | 148 | let dst = self.alloc_tex_like(&src, src.depth, key); |
| 102 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); | |
| 103 | self.dispatch_op(enc, ShaderOp::UltraKeyer, &src.view, &self.dummy.view, mv, &dst, &u); | |
| 149 | let keyed = if combine { self.alloc_tex_like(&src, src.depth, 0) } else { dst.clone() }; | |
| 150 | let mv = key_mask.map_or(&self.dummy.view, |m| &m.view); | |
| 151 | if bg_untouched { | |
| 152 | // An untouched Background colour keys `b - r` and premultiplies, which the generic key shader has no path for. | |
| 153 | let hi = hi_th.max(lo_th + 1e-4); | |
| 154 | let sep_pow = if sep > 1e-4 { format!("pow(i1, {})", 1.0 + sep * 4.0) } else { "i1".to_owned() }; | |
| 155 | let mut intermediates: [String; 8] = std::array::from_fn(|_| "0".to_owned()); | |
| 156 | intermediates[0] = "clamp(1 - max(b1 - r1, 0), 0, 1)".to_owned(); | |
| 157 | intermediates[1] = sep_pow; | |
| 158 | intermediates[2] = format!("pow(clamp((i2 - {lo_th}) / ({hi} - {lo_th}), 0, 1), {})", 1.0 / gamma); | |
| 159 | intermediates[3] = if inv_matte > 0.5 { "(1 - i3)" } else { "i3" }.to_owned(); | |
| 160 | let program = crate::custom_shader::CustomProgram { | |
| 161 | intermediates, | |
| 162 | red: "r1 * i4".to_owned(), | |
| 163 | green: "g1 * i4".to_owned(), | |
| 164 | blue: "b1 * i4".to_owned(), | |
| 165 | alpha: "i4".to_owned(), | |
| 166 | ..Default::default() | |
| 167 | }; | |
| 168 | let pscale = self.proxy_scale.max(1); | |
| 169 | let (w, h) = ((src.size[0] * pscale) as f32, (src.size[1] * pscale) as f32); | |
| 170 | let mut params = crate::custom_shader::CustomParams::default(); | |
| 171 | params.img_dims1 = [w, h, w, h]; | |
| 172 | params.img_dims2 = [w, h, time as f32, key_blend]; | |
| 173 | params.mask_proc = [key_mask.is_some() as u8 as f32, mask_inv as u8 as f32, 1.0 - skip[0], 1.0 - skip[1]]; | |
| 174 | params.proc_ba = [1.0 - skip[2], 1.0 - skip[3], self.effective_depth(src.depth).is_float() as u8 as f32, 0.0]; | |
| 175 | let ok = self.custom_shaders.dispatch( | |
| 176 | &self.device, &self.queue, enc, &program, keyed.format, | |
| 177 | &src.view, &self.dummy.view, &self.dummy.view, mv, &keyed.view, keyed.size, &params, | |
| 178 | ); | |
| 179 | if !ok { | |
| 180 | return src; | |
| 181 | } | |
| 182 | } else { | |
| 183 | self.dispatch_op(enc, ShaderOp::UltraKeyer, &src.view, &self.dummy.view, mv, &keyed, &u); | |
| 184 | } | |
| 185 | if combine { | |
| 186 | let mut uc = UniformParams::default(); | |
| 187 | uc.p[0] = [1.0, blend, mask.is_some() as u8 as f32, mask_inv as u8 as f32]; | |
| 188 | uc.p[1] = [4.0, 0.0, 0.0, if mbm { 2.0 } else { 0.0 }]; // mode 4 = MixOnly; MultiplyByMask scales from black | |
| 189 | uc.p[3] = skip; | |
| 190 | let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); | |
| 191 | self.dispatch_op(enc, ShaderOp::GlowAdd, &src.view, &keyed.view, mv, &dst, &uc); | |
| 192 | } | |
| 104 | 193 | self.cache.insert(key, dst.clone(), node.id, frame_i); |
| 105 | 194 | dst |
| 106 | 195 | } |
crates/engine/src/shaders/color.wgsl+45-25| ... | ... | @@ -125,17 +125,16 @@ fn bc_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 125 | 125 | |
| 126 | 126 | |
| 127 | 127 | // ============================================================================ |
| 128 | // 5. ColorCorrector (Master + Shadows/Midtones/Highlights wheels, TintMode 0/1/2, LGG) | |
| 129 | // p[0] = (hue1, sat1, tint_angle1, tint_len1) | |
| 130 | // p[1] = (gain1, lift1, gamma1, contrast1) | |
| 131 | // p[2] = (brightness1, prediv, blend, has_mask) | |
| 128 | // 5. ColorCorrector (Master + Shadows/Midtones/Highlights wheels, TintMode 0/1/2, per-channel Levels curves) | |
| 129 | // p[0] = (hue1, sat1, tint_angle1, tint_len1) // Master wheel | |
| 130 | // p[2] = (0, prediv, blend, has_mask) | |
| 132 | 131 | // p[3] = (mask_invert, tint_mode, has_smh, mbm) |
| 133 | 132 | // p[4] = (hue2, sat2, tint_angle2, tint_len2) // Shadows |
| 134 | // p[5] = (gain2, lift2, gamma2, contrast2) | |
| 135 | // p[6] = (hue3, sat3, tint_angle3, tint_len3) // Midtones (wheel only) | |
| 133 | // p[6] = (hue3, sat3, tint_angle3, tint_len3) // Midtones | |
| 136 | 134 | // p[7] = (hue4, sat4, tint_angle4, tint_len4) // Highlights |
| 137 | // p[8] = (gain4, lift4, gamma4, contrast4) | |
| 138 | // p[9] = (bright2, bright4, 0, 0) | |
| 135 | // p[10] = skip_rgba (Process toggles off), p[11].x = 1 when src_b holds the curve table | |
| 136 | // src_b = 12x3 curve table: row = channel, texels 3*range + {0,1,2} for range 0 = master alone, 1..3 = master with Shadows/Midtones/Highlights: | |
| 137 | // (contrast, slope, offset, brightness), (in_scale, in_offset, gamma, 0), (out_scale, out_offset, 0, 0) | |
| 139 | 138 | // ============================================================================ |
| 140 | 139 | fn rotate_hue_preserve_luma(c: vec3<f32>, angle: f32) -> vec3<f32> { |
| 141 | 140 | // Paul Haeberli luminance-preserving shear-rotate-unshear around (1,1,1) with Rec.601 weights. |
| ... | ... | @@ -233,6 +232,7 @@ fn apply_cc_wheel( |
| 233 | 232 | return rgb; |
| 234 | 233 | } |
| 235 | 234 | |
| 235 | // Legacy RGB-only Gain/Lift/Gamma/Contrast/Brightness for callers that dispatch `cc_op` without a curve table (p[11].x == 0). | |
| 236 | 236 | fn apply_cc_lgg_combined( |
| 237 | 237 | rgb_in: vec3<f32>, |
| 238 | 238 | g1: f32, |
| ... | ... | @@ -268,6 +268,24 @@ fn apply_cc_lgg_combined( |
| 268 | 268 | return rgb; |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | fn cc_curve(x_in: f32, range: i32, ch: i32) -> f32 { | |
| 272 | let t0 = textureLoad(src_b, vec2<i32>(range * 3, ch), 0); | |
| 273 | let t1 = textureLoad(src_b, vec2<i32>(range * 3 + 1, ch), 0); | |
| 274 | let t2 = textureLoad(src_b, vec2<i32>(range * 3 + 2, ch), 0); | |
| 275 | // Contrast -> Lift/Gain -> Brightness -> input range -> Gamma/Mid -> output range | |
| 276 | var x = (x_in - 0.5) * t0.x + 0.5; | |
| 277 | x = x * t0.y + t0.z + t0.w; | |
| 278 | x = x * t1.x + t1.y; | |
| 279 | if (abs(t1.z - 1.0) > 1e-5) { | |
| 280 | x = pow(max(x, 0.0), 1.0 / t1.z); | |
| 281 | } | |
| 282 | return x * t2.x + t2.y; | |
| 283 | } | |
| 284 | ||
| 285 | fn cc_levels(rgb: vec3<f32>, range: i32) -> vec3<f32> { | |
| 286 | return vec3<f32>(cc_curve(rgb.r, range, 0), cc_curve(rgb.g, range, 1), cc_curve(rgb.b, range, 2)); | |
| 287 | } | |
| 288 | ||
| 271 | 289 | /// Default Fusion shadows range: one cubic Bezier with control points (0,1) (.4,.2) (.6,0) (1,0). |
| 272 | 290 | fn cc_shadow_weight(y: f32) -> f32 { |
| 273 | 291 | var lo = 0.0; |
| ... | ... | @@ -301,32 +319,34 @@ fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 301 | 319 | let y0 = clamp(dot(rgb, k_luma), 0.0, 1.0); |
| 302 | 320 | rgb = apply_cc_wheel(rgb, u.p[0].x, u.p[0].y, u.p[0].z, u.p[0].w, tint_mode); |
| 303 | 321 | |
| 322 | let tabled = u.p[11].x > 0.5; | |
| 304 | 323 | let g1 = u.p[1].x; |
| 305 | 324 | let l1 = u.p[1].y; |
| 306 | 325 | let gam1 = u.p[1].z; |
| 307 | 326 | let c1 = u.p[1].w; |
| 308 | 327 | let b1 = u.p[2].x; |
| 309 | ||
| 310 | 328 | if (has_smh) { |
| 311 | 329 | let w_s = cc_shadow_weight(y0); |
| 312 | 330 | let w_h = cc_shadow_weight(1.0 - y0); |
| 313 | 331 | let w_m = max(1.0 - w_s - w_h, 0.0); |
| 314 | let rgb_s = apply_cc_lgg_combined( | |
| 315 | apply_cc_wheel(rgb, u.p[4].x, u.p[4].y, u.p[4].z, u.p[4].w, tint_mode), | |
| 316 | g1, l1, gam1, c1, b1, | |
| 317 | u.p[5].x, u.p[5].y, u.p[5].z, u.p[5].w, u.p[9].x, | |
| 318 | ); | |
| 319 | let rgb_m = apply_cc_lgg_combined( | |
| 320 | apply_cc_wheel(rgb, u.p[6].x, u.p[6].y, u.p[6].z, u.p[6].w, tint_mode), | |
| 321 | g1, l1, gam1, c1, b1, | |
| 322 | 1.0, 0.0, 1.0, 1.0, 0.0, | |
| 323 | ); | |
| 324 | let rgb_h = apply_cc_lgg_combined( | |
| 325 | apply_cc_wheel(rgb, u.p[7].x, u.p[7].y, u.p[7].z, u.p[7].w, tint_mode), | |
| 326 | g1, l1, gam1, c1, b1, | |
| 327 | u.p[8].x, u.p[8].y, u.p[8].z, u.p[8].w, u.p[9].y, | |
| 328 | ); | |
| 332 | let wheel_s = apply_cc_wheel(rgb, u.p[4].x, u.p[4].y, u.p[4].z, u.p[4].w, tint_mode); | |
| 333 | let wheel_m = apply_cc_wheel(rgb, u.p[6].x, u.p[6].y, u.p[6].z, u.p[6].w, tint_mode); | |
| 334 | let wheel_h = apply_cc_wheel(rgb, u.p[7].x, u.p[7].y, u.p[7].z, u.p[7].w, tint_mode); | |
| 335 | var rgb_s: vec3<f32>; | |
| 336 | var rgb_m: vec3<f32>; | |
| 337 | var rgb_h: vec3<f32>; | |
| 338 | if (tabled) { | |
| 339 | rgb_s = cc_levels(wheel_s, 1); | |
| 340 | rgb_m = cc_levels(wheel_m, 2); | |
| 341 | rgb_h = cc_levels(wheel_h, 3); | |
| 342 | } else { | |
| 343 | rgb_s = apply_cc_lgg_combined(wheel_s, g1, l1, gam1, c1, b1, u.p[5].x, u.p[5].y, u.p[5].z, u.p[5].w, u.p[9].x); | |
| 344 | rgb_m = apply_cc_lgg_combined(wheel_m, g1, l1, gam1, c1, b1, 1.0, 0.0, 1.0, 1.0, 0.0); | |
| 345 | rgb_h = apply_cc_lgg_combined(wheel_h, g1, l1, gam1, c1, b1, u.p[8].x, u.p[8].y, u.p[8].z, u.p[8].w, u.p[9].y); | |
| 346 | } | |
| 329 | 347 | rgb = rgb_s * w_s + rgb_m * w_m + rgb_h * w_h; |
| 348 | } else if (tabled) { | |
| 349 | rgb = cc_levels(rgb, 0); | |
| 330 | 350 | } else { |
| 331 | 351 | rgb = apply_cc_lgg_combined(rgb, g1, l1, gam1, c1, b1, 1.0, 0.0, 1.0, 1.0, 0.0); |
| 332 | 352 | } |
| ... | ... | @@ -335,7 +355,7 @@ fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 335 | 355 | rgb = rgb * src.a; |
| 336 | 356 | } |
| 337 | 357 | let out_col = blend_with_mask(src, vec4<f32>(rgb, src.a), gid.xy, dims, u.p[2].w, u.p[3].x, u.p[2].z, u.p[3].w); |
| 338 | store_dst(vec2<i32>(gid.xy), out_col); | |
| 358 | store_dst(vec2<i32>(gid.xy), keep_unprocessed(out_col, src, u.p[10], u.p[2].w, u.p[3].w)); | |
| 339 | 359 | } |
| 340 | 360 | // ============================================================================ |
| 341 | 361 | // 11. Fuse.Colorize (Clover's exact 2020 Colorize.fuse formula!) |