| ... | ... | @@ -4,6 +4,7 @@ |
| 4 | 4 | // p[1] = (brightness, saturation, prediv, blend) |
| 5 | 5 | // p[2] = (has_mask, mask_invert, low, high) |
| 6 | 6 | // p[3] = (proc_r, proc_g, proc_b, proc_a) |
| 7 | // p[4] = (clip_black, clip_white, 0, 0) |
| 7 | 8 | // ============================================================================ |
| 8 | 9 | fn bc_curve_scalar(v_in: f32, low: f32, high: f32, gain: f32, lift: f32, gamma: f32, contrast: f32, brightness: f32) -> f32 { |
| 9 | 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 | 13 | v = (v - low) / span; |
| 13 | 14 | } |
| 14 | 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 | 19 | v = (v - 0.5) * (1.0 + contrast) + 0.5; |
| 17 | 20 | v = v + brightness; |
| 18 | 21 | return v; |
| ... | ... | @@ -49,6 +52,8 @@ fn bc_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 49 | 52 | ); |
| 50 | 53 | let luma = dot(adj_rgb, vec3<f32>(0.299, 0.587, 0.114)); |
| 51 | 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)); } |
| 52 | 57 | |
| 53 | 58 | let out_rgb_unpre = vec3<f32>( |
| 54 | 59 | select(rgb.r, adj_rgb.r, proc_r), |
| ... | ... | @@ -72,11 +77,10 @@ fn bc_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 72 | 77 | // p[3] = (mask_invert, tint_mode, has_smh, 0) |
| 73 | 78 | // p[4] = (hue2, sat2, tint_angle2, tint_len2) // Shadows |
| 74 | 79 | // 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) |
| 80 | 84 | // ============================================================================ |
| 81 | 85 | fn rotate_hue_preserve_luma(c: vec3<f32>, angle: f32) -> vec3<f32> { |
| 82 | 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 | 193 | return rgb; |
| 190 | 194 | } |
| 191 | 195 | |
| 196 | /// Default Fusion shadows range: one cubic Bezier with control points (0,1) (.4,.2) (.6,0) (1,0). |
| 197 | fn 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 | 210 | @compute @workgroup_size(8, 8) |
| 193 | 211 | fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 194 | 212 | let dims = textureDimensions(dst_tex); |
| ... | ... | @@ -204,19 +222,19 @@ fn cc_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 204 | 222 | rgb = rgb / src.a; |
| 205 | 223 | } |
| 206 | 224 | |
| 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 | 227 | if (has_smh) { |
| 208 | 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); |
| 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); |
| 211 | 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); |
| 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); |
| 215 | 235 | rgb = rgb_s * w_s + rgb_m * w_m + rgb_h * w_h; |
| 216 | 236 | } |
| 217 | 237 | |
| 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 | 238 | if (prediv) { |
| 221 | 239 | rgb = rgb * src.a; |
| 222 | 240 | } |
| ... | ... | @@ -462,7 +480,7 @@ fn gamut_convert_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 462 | 480 | // ============================================================================ |
| 463 | 481 | // 25. HueCurves (9 hue-indexed LUT curves baked into 256x3 RGBA8 texture in src_b) |
| 464 | 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 | 484 | // p[0] = (prediv, blend, has_mask, mask_invert) |
| 467 | 485 | // ============================================================================ |
| 468 | 486 | @compute @workgroup_size(8, 8) |
| ... | ... | @@ -477,14 +495,15 @@ fn hue_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 477 | 495 | if (prediv && src.a > 1e-5) { |
| 478 | 496 | rgb = rgb / src.a; |
| 479 | 497 | } |
| 498 | let min_c = min(rgb.r, min(rgb.g, rgb.b)); |
| 480 | 499 | let c_pos = max(rgb, vec3<f32>(0.0)); |
| 481 | 500 | let hsv = rgb_to_hsv(c_pos); |
| 482 | 501 | let h_u = fract(hsv.x); |
| 483 | 502 | let sat = hsv.y; |
| 484 | 503 | |
| 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); |
| 488 | 507 | |
| 489 | 508 | // 1. Hue shift (curve neutral=1.0, domain 0..6 => delta = (hue_v - 1.0) / 6.0) |
| 490 | 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 | 520 | } |
| 502 | 521 | // 4. Per-channel Red / Green / Blue multipliers |
| 503 | 522 | 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); |
| 517 | 525 | |
| 518 | 526 | if (prediv) { |
| 519 | 527 | rgb = rgb * src.a; |