| 1 | // Wave 35A — Alpha, Color & Keying compute shaders (100% WebGPU-safe). |
| 2 | // |
| 3 | // All input textures are bound as unfilterable `texture_2d<f32>` and read via `textureLoad` |
| 4 | // (with manual bilinear interpolation where needed), so every pass works uniformly on |
| 5 | // `Rgba8Unorm`, `Rgba16Float`, and `Rgba32Float` on native and `wasm32` without requiring |
| 6 | // `FLOAT32_FILTERABLE` or `read_write` storage textures. |
| 7 | |
| 8 | @group(0) @binding(0) var tex_a: texture_2d<f32>; |
| 9 | @group(0) @binding(1) var tex_b: texture_2d<f32>; |
| 10 | @group(0) @binding(2) var tex_c: texture_2d<f32>; |
| 11 | @group(0) @binding(3) var mask_tex: texture_2d<f32>; |
| 12 | @group(0) @binding(4) var dst_tex: texture_storage_2d<rgba8unorm, write>; |
| 13 | |
| 14 | struct Params { |
| 15 | p: array<vec4<f32>, 16>, |
| 16 | }; |
| 17 | @group(0) @binding(5) var<uniform> u: Params; |
| 18 | |
| 19 | const HDR_FLOAT: bool = false; |
| 20 | const TAU: f32 = 6.283185307179586; |
| 21 | |
| 22 | fn store_dst(p: vec2<i32>, col: vec4<f32>) { |
| 23 | if (HDR_FLOAT) { |
| 24 | textureStore(dst_tex, p, col); |
| 25 | } else { |
| 26 | textureStore(dst_tex, p, clamp(col, vec4<f32>(0.0), vec4<f32>(1.0))); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn load_clamped(tex: texture_2d<f32>, p: vec2<i32>) -> vec4<f32> { |
| 31 | let d = vec2<i32>(textureDimensions(tex)); |
| 32 | let c = clamp(p, vec2<i32>(0), max(d - vec2<i32>(1), vec2<i32>(0))); |
| 33 | return textureLoad(tex, c, 0); |
| 34 | } |
| 35 | |
| 36 | fn sample_bilinear(tex: texture_2d<f32>, uv: vec2<f32>) -> vec4<f32> { |
| 37 | let dims = vec2<f32>(textureDimensions(tex)); |
| 38 | let pos = uv * dims - vec2<f32>(0.5); |
| 39 | let p0 = vec2<i32>(floor(pos)); |
| 40 | let f = pos - vec2<f32>(p0); |
| 41 | let c00 = load_clamped(tex, p0); |
| 42 | let c10 = load_clamped(tex, p0 + vec2<i32>(1, 0)); |
| 43 | let c01 = load_clamped(tex, p0 + vec2<i32>(0, 1)); |
| 44 | let c11 = load_clamped(tex, p0 + vec2<i32>(1, 1)); |
| 45 | return mix(mix(c00, c10, f.x), mix(c01, c11, f.x), f.y); |
| 46 | } |
| 47 | |
| 48 | fn read_mask(gid: vec2<u32>, dims: vec2<u32>, has_mask: f32, inv: f32) -> f32 { |
| 49 | if (has_mask < 0.5) { |
| 50 | return 1.0; |
| 51 | } |
| 52 | let uv = (vec2<f32>(gid) + vec2<f32>(0.5)) / vec2<f32>(dims); |
| 53 | let m = sample_bilinear(mask_tex, uv).a; |
| 54 | return select(m, 1.0 - m, inv > 0.5); |
| 55 | } |
| 56 | |
| 57 | fn apply_mask_blend( |
| 58 | src: vec4<f32>, |
| 59 | res: vec4<f32>, |
| 60 | gid: vec2<u32>, |
| 61 | dims: vec2<u32>, |
| 62 | blend: f32, |
| 63 | has_mask: f32, |
| 64 | mask_inv: f32, |
| 65 | mbm: f32, |
| 66 | skip: vec4<f32>, |
| 67 | ) -> vec4<f32> { |
| 68 | let raw_mask = read_mask(gid, dims, has_mask, mask_inv); |
| 69 | let m = clamp(raw_mask * blend, 0.0, 1.0); |
| 70 | let use_mbm = has_mask > 0.5 && mbm > 0.5; |
| 71 | var out_col = select(mix(src, res, m), res * m, use_mbm); |
| 72 | let unproc = select(src, vec4<f32>(0.0), use_mbm); |
| 73 | out_col = select(out_col, unproc, skip > vec4<f32>(0.5)); |
| 74 | return out_col; |
| 75 | } |
| 76 | |
| 77 | // ============================================================================ |
| 78 | // 1 & 2. AlphaMultiply / AlphaDivide |
| 79 | // p[0] = (mode: 0=multiply 1=divide, blend, has_mask, mask_inv) |
| 80 | // p[1] = (mbm, quantize_8bit, 0, 0) |
| 81 | // p[2] = (skip_r, skip_g, skip_b, skip_a) |
| 82 | // ============================================================================ |
| 83 | @compute @workgroup_size(8, 8) |
| 84 | fn alpha_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 85 | let dims = textureDimensions(dst_tex); |
| 86 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 87 | let p = vec2<i32>(gid.xy); |
| 88 | let src = textureLoad(tex_a, p, 0); |
| 89 | var res: vec4<f32>; |
| 90 | if (u.p[0].x < 0.5) { |
| 91 | res = vec4<f32>(src.rgb * src.a, src.a); |
| 92 | } else { |
| 93 | let rgb = select(vec3<f32>(0.0), src.rgb / src.a, src.a > 1e-6); |
| 94 | res = vec4<f32>(rgb, src.a); |
| 95 | } |
| 96 | // Fusion stores an 8-bit effect result before Blend mixes it with the source. |
| 97 | if (u.p[1].y > 0.5) { |
| 98 | res = floor(clamp(res, vec4<f32>(0.0), vec4<f32>(1.0)) * 255.0 + 0.5) / 255.0; |
| 99 | } |
| 100 | let out_col = apply_mask_blend( |
| 101 | src, res, gid.xy, dims, u.p[0].y, u.p[0].z, u.p[0].w, u.p[1].x, u.p[2], |
| 102 | ); |
| 103 | store_dst(p, out_col); |
| 104 | } |
| 105 | |
| 106 | // ============================================================================ |
| 107 | // 3. PseudoColor (Fusion per-channel waveform false-color mapper on premultiplied values) |
| 108 | // p[c*3] = (low, high, soft_edge, waveform: 0=Sine 1=Triangle 2=Sawtooth 3=Square) |
| 109 | // p[c*3+1] = (frequency, phase, mean, amplitude) |
| 110 | // p[c*3+2] = (enabled, wrap, 0, 0) |
| 111 | // p[12] = (blend, has_mask, mask_inv, mbm) |
| 112 | // p[13] = (skip_r, skip_g, skip_b, skip_a) |
| 113 | // p[14] = (quantize_8bit, 0, 0, 0) |
| 114 | // ============================================================================ |
| 115 | fn pseudo_channel(v: f32, c: i32) -> f32 { |
| 116 | let range = u.p[c * 3]; |
| 117 | let wave_cfg = u.p[c * 3 + 1]; |
| 118 | let flags = u.p[c * 3 + 2]; |
| 119 | if (flags.x < 0.5 || v < range.x || v > range.y) { return v; } |
| 120 | let phi = v * wave_cfg.x + wave_cfg.y; |
| 121 | // Fusion's double-precision sin(2 pi phi) is slightly negative at positive integers. |
| 122 | let pos_int = phi > 0.0 && fract(phi) == 0.0; |
| 123 | // Fusion takes `fmod` (truncating) for the periodic shapes, so negative phases fall off the [-1, 1] wave. |
| 124 | var wave: f32; |
| 125 | switch i32(range.w) { |
| 126 | case 1: { wave = 1.0 - 4.0 * abs((phi + 0.25) % 1.0 - 0.5); } |
| 127 | case 2: { wave = 2.0 * (((v + 0.5) * wave_cfg.x + wave_cfg.y) % 1.0) - 1.0; } |
| 128 | case 3: { wave = select(-1.0, 1.0, fract(phi) <= 0.5 && !pos_int); } |
| 129 | default: { wave = select(sin(TAU * (phi % 1.0)), -1e-6, pos_int); } |
| 130 | } |
| 131 | let raw = wave_cfg.z + wave_cfg.w * wave; |
| 132 | let res = select(clamp(raw, 0.0, 1.0), raw % 1.0, flags.y > 0.5); |
| 133 | let edge = min(range.z, 0.5 * (range.y - range.x)); |
| 134 | let s = select(1.0, clamp(min(v - range.x, range.y - v) / edge, 0.0, 1.0), edge > 0.0); |
| 135 | return mix(v, res, s); |
| 136 | } |
| 137 | |
| 138 | @compute @workgroup_size(8, 8) |
| 139 | fn pseudo_color_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 140 | let dims = textureDimensions(dst_tex); |
| 141 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 142 | let p = vec2<i32>(gid.xy); |
| 143 | let src = textureLoad(tex_a, p, 0); |
| 144 | var res = vec4<f32>(pseudo_channel(src.r, 0), pseudo_channel(src.g, 1), pseudo_channel(src.b, 2), pseudo_channel(src.a, 3)); |
| 145 | // Fusion stores an 8-bit effect result before Blend mixes it with the source. |
| 146 | if (u.p[14].x > 0.5) { |
| 147 | res = floor(clamp(res, vec4<f32>(0.0), vec4<f32>(1.0)) * 255.0 + 0.5) / 255.0; |
| 148 | } |
| 149 | let out_col = apply_mask_blend( |
| 150 | src, res, gid.xy, dims, u.p[12].x, u.p[12].y, u.p[12].z, u.p[12].w, u.p[13], |
| 151 | ); |
| 152 | store_dst(p, out_col); |
| 153 | } |
| 154 | |
| 155 | // ============================================================================ |
| 156 | // 4. DifferenceKeyer |
| 157 | // p[0] = (low, high, gain, softness) |
| 158 | // p[1] = (matte_contrast, matte_gamma, invert, post_mult) |
| 159 | // p[2] = (blend, has_mask, mask_inv, mbm) |
| 160 | // p[3] = (has_garbage, garbage_inv, has_solid, solid_inv) |
| 161 | // p[4] = (pass_mode: 0=raw_matte 1=combine_mattes 2=composite 3=single, 0, 0, 0) |
| 162 | // p[5] = (skip_r, skip_g, skip_b, skip_a) |
| 163 | // ============================================================================ |
| 164 | fn combine_garbage_solid(a_in: f32, uv: vec2<f32>, cfg: vec4<f32>) -> f32 { |
| 165 | var a = clamp(a_in, 0.0, 1.0); |
| 166 | if (cfg.z > 0.5) { |
| 167 | var s = sample_bilinear(tex_c, uv).a; |
| 168 | if (cfg.w > 0.5) { s = 1.0 - s; } |
| 169 | a = max(a, clamp(s, 0.0, 1.0)); |
| 170 | } |
| 171 | if (cfg.x > 0.5) { |
| 172 | var g = sample_bilinear(tex_b, uv).a; |
| 173 | if (cfg.y > 0.5) { g = 1.0 - g; } |
| 174 | a = a * (1.0 - clamp(g, 0.0, 1.0)); |
| 175 | } |
| 176 | return a; |
| 177 | } |
| 178 | |
| 179 | @compute @workgroup_size(8, 8) |
| 180 | fn diff_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 181 | let dims = textureDimensions(dst_tex); |
| 182 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 183 | let p = vec2<i32>(gid.xy); |
| 184 | let uv = (vec2<f32>(gid.xy) + vec2<f32>(0.5)) / vec2<f32>(dims); |
| 185 | let pass_mode = i32(u.p[4].x + 0.5); |
| 186 | |
| 187 | if (pass_mode == 1) { |
| 188 | let m = textureLoad(tex_a, p, 0).a; |
| 189 | let a = combine_garbage_solid(m, uv, u.p[3]); |
| 190 | textureStore(dst_tex, p, vec4<f32>(a, a, a, a)); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | let fg = textureLoad(tex_a, p, 0); |
| 195 | var a: f32; |
| 196 | if (pass_mode == 2) { |
| 197 | a = clamp(textureLoad(tex_b, p, 0).a, 0.0, 1.0); |
| 198 | } else { |
| 199 | let bg_dims = textureDimensions(tex_b); |
| 200 | let bg = select( |
| 201 | sample_bilinear(tex_b, uv), |
| 202 | textureLoad(tex_b, p, 0), |
| 203 | bg_dims.x == dims.x && bg_dims.y == dims.y, |
| 204 | ); |
| 205 | let dv = abs(fg.rgb - bg.rgb); |
| 206 | let diff = max(dv.r, max(dv.g, dv.b)) * u.p[0].z; |
| 207 | let low = u.p[0].x; |
| 208 | let high = max(u.p[0].y, low + u.p[0].w); |
| 209 | a = clamp((diff - low) / max(high - low, 1e-5), 0.0, 1.0); |
| 210 | let contrast = u.p[1].x; |
| 211 | if (contrast >= 0.0) { |
| 212 | a = clamp(a * (1.0 + 4.0 * contrast), 0.0, 1.0); |
| 213 | } else { |
| 214 | a = clamp(1.0 - (1.0 - a) * (1.0 - 4.0 * contrast), 0.0, 1.0); |
| 215 | } |
| 216 | a = pow(clamp(a, 0.0, 1.0), 1.0 / max(u.p[1].y, 1e-4)); |
| 217 | if (u.p[1].z > 0.5) { |
| 218 | a = 1.0 - a; |
| 219 | } |
| 220 | if (pass_mode == 0) { |
| 221 | textureStore(dst_tex, p, vec4<f32>(a, a, a, a)); |
| 222 | return; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | let post_mult = u.p[1].w > 0.5; |
| 227 | let keyed = vec4<f32>(select(fg.rgb, fg.rgb * a, post_mult), a); |
| 228 | let out_col = apply_mask_blend( |
| 229 | fg, keyed, gid.xy, dims, u.p[2].x, u.p[2].y, u.p[2].z, u.p[2].w, u.p[5], |
| 230 | ); |
| 231 | store_dst(p, out_col); |
| 232 | } |
| 233 | |
| 234 | // ============================================================================ |
| 235 | // 5. ChromaKeyer (Fusion Chroma/RGB/HLS range keyer + spill suppression) |
| 236 | // p[0] = (r_lo, r_hi, g_lo, g_hi) |
| 237 | // p[1] = (b_lo, b_hi, l_lo, l_hi) |
| 238 | // p[2] = (key_type: 0=YUV 1=RGB 2=HLS, soft_range, matte_gamma, invert_matte) |
| 239 | // p[3] = (spill_method: 0..4, spill_sup, spill_ch: 0=R 1=G 2=B, post_mult) |
| 240 | // p[4] = (blend, has_mask, mask_inv, mbm) |
| 241 | // p[5] = (has_garbage, garbage_inv, has_solid, solid_inv) |
| 242 | // p[6] = (pass_mode: 0=raw_matte 1=combine_mattes 2=composite, range_active, 0, 0) |
| 243 | // p[7] = (skip_r, skip_g, skip_b, skip_a) |
| 244 | // ============================================================================ |
| 245 | fn axis_dist(val: f32, lo: f32, hi: f32, soft: f32, wrap_hue: bool) -> f32 { |
| 246 | if (lo <= 1e-5 && hi >= 1.0 - 1e-5) { |
| 247 | return 0.0; |
| 248 | } |
| 249 | var d: f32; |
| 250 | if (wrap_hue) { |
| 251 | if (lo <= hi) { |
| 252 | if (val >= lo && val <= hi) { |
| 253 | d = 0.0; |
| 254 | } else { |
| 255 | let d_lo = min(abs(val - lo), 1.0 - abs(val - lo)); |
| 256 | let d_hi = min(abs(val - hi), 1.0 - abs(val - hi)); |
| 257 | d = min(d_lo, d_hi); |
| 258 | } |
| 259 | } else { |
| 260 | if (val >= lo || val <= hi) { |
| 261 | d = 0.0; |
| 262 | } else { |
| 263 | d = min(lo - val, val - hi); |
| 264 | } |
| 265 | } |
| 266 | } else { |
| 267 | d = max(max(lo - val, val - hi), 0.0); |
| 268 | } |
| 269 | if (soft > 1e-6) { |
| 270 | return clamp(d / soft, 0.0, 1.0); |
| 271 | } |
| 272 | return select(0.0, 1.0, d > 0.0); |
| 273 | } |
| 274 | |
| 275 | fn rgb_to_hls(rgb: vec3<f32>) -> vec3<f32> { |
| 276 | let mx = max(rgb.r, max(rgb.g, rgb.b)); |
| 277 | let mn = min(rgb.r, min(rgb.g, rgb.b)); |
| 278 | let d = mx - mn; |
| 279 | let l = 0.5 * (mx + mn); |
| 280 | var s = 0.0; |
| 281 | var h = 0.0; |
| 282 | if (d > 1e-6) { |
| 283 | s = d / max(1.0 - abs(2.0 * l - 1.0), 1e-6); |
| 284 | if (mx == rgb.r) { |
| 285 | h = (rgb.g - rgb.b) / d + select(0.0, 6.0, rgb.g < rgb.b); |
| 286 | } else if (mx == rgb.g) { |
| 287 | h = (rgb.b - rgb.r) / d + 2.0; |
| 288 | } else { |
| 289 | h = (rgb.r - rgb.g) / d + 4.0; |
| 290 | } |
| 291 | h = fract(h / 6.0); |
| 292 | } |
| 293 | return vec3<f32>(h, clamp(s, 0.0, 1.0), clamp(l, 0.0, 1.0)); |
| 294 | } |
| 295 | |
| 296 | fn despill_rgb(rgb: vec3<f32>, method: i32, amount: f32, ch: i32) -> vec3<f32> { |
| 297 | if (method <= 0 || amount <= 1e-5) { |
| 298 | return rgb; |
| 299 | } |
| 300 | var dom = rgb.g; |
| 301 | var o1 = rgb.r; |
| 302 | var o2 = rgb.b; |
| 303 | if (ch == 0) { |
| 304 | dom = rgb.r; o1 = rgb.g; o2 = rgb.b; |
| 305 | } else if (ch == 2) { |
| 306 | dom = rgb.b; o1 = rgb.r; o2 = rgb.g; |
| 307 | } |
| 308 | let hi = max(o1, o2); |
| 309 | let lo = min(o1, o2); |
| 310 | var lim = hi; |
| 311 | if (method == 2) { |
| 312 | lim = 0.5 * (o1 + o2); |
| 313 | } else if (method == 3) { |
| 314 | lim = 0.25 * hi + 0.75 * lo; |
| 315 | } else if (method >= 4) { |
| 316 | lim = lo; |
| 317 | } |
| 318 | var new_dom = dom; |
| 319 | // Rare & Medium only suppress where the key channel is dominant; WellDone/Burnt clamp down to lim. |
| 320 | let do_despill = select(dom > lim, dom > hi, method <= 2); |
| 321 | if (do_despill) { |
| 322 | new_dom = mix(dom, min(dom, lim), clamp(amount, 0.0, 1.0)); |
| 323 | } |
| 324 | if (ch == 0) { return vec3<f32>(new_dom, rgb.g, rgb.b); } |
| 325 | if (ch == 2) { return vec3<f32>(rgb.r, rgb.g, new_dom); } |
| 326 | return vec3<f32>(rgb.r, new_dom, rgb.b); |
| 327 | } |
| 328 | |
| 329 | @compute @workgroup_size(8, 8) |
| 330 | fn chroma_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 331 | let dims = textureDimensions(dst_tex); |
| 332 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 333 | let p = vec2<i32>(gid.xy); |
| 334 | let uv = (vec2<f32>(gid.xy) + vec2<f32>(0.5)) / vec2<f32>(dims); |
| 335 | let pass_mode = i32(u.p[6].x + 0.5); |
| 336 | |
| 337 | if (pass_mode == 1) { |
| 338 | let m = textureLoad(tex_a, p, 0).a; |
| 339 | let a = combine_garbage_solid(m, uv, u.p[5]); |
| 340 | textureStore(dst_tex, p, vec4<f32>(a, a, a, a)); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | let src = textureLoad(tex_a, p, 0); |
| 345 | if (pass_mode == 0) { |
| 346 | var a = src.a; |
| 347 | if (u.p[6].y > 0.5) { |
| 348 | let kt = i32(u.p[2].x + 0.5); |
| 349 | let soft = u.p[2].y; |
| 350 | let lum = dot(src.rgb, vec3<f32>(0.299, 0.587, 0.114)); |
| 351 | var c0 = src.r; |
| 352 | var c1 = src.g; |
| 353 | var c2 = src.b; |
| 354 | let c3 = lum; |
| 355 | if (kt == 0) { |
| 356 | c0 = 0.5 + (src.r - lum) / 1.60; |
| 357 | c1 = 0.5 + (src.g - lum) / 1.32; |
| 358 | c2 = 0.5 + (src.b - lum) / 2.24; |
| 359 | } else if (kt == 2) { |
| 360 | let hls = rgb_to_hls(src.rgb); |
| 361 | c0 = hls.x; |
| 362 | c1 = hls.y; |
| 363 | c2 = hls.z; |
| 364 | } |
| 365 | let t0 = axis_dist(c0, u.p[0].x, u.p[0].y, soft, kt == 2); |
| 366 | let t1 = axis_dist(c1, u.p[0].z, u.p[0].w, soft, false); |
| 367 | let t2 = axis_dist(c2, u.p[1].x, u.p[1].y, soft, false); |
| 368 | let t3 = axis_dist(c3, u.p[1].z, u.p[1].w, soft, false); |
| 369 | a = max(max(t0, t1), max(t2, t3)); |
| 370 | a = pow(clamp(a, 0.0, 1.0), 1.0 / max(u.p[2].z, 1e-4)); |
| 371 | if (u.p[2].w > 0.5) { |
| 372 | a = 1.0 - a; |
| 373 | } |
| 374 | } |
| 375 | textureStore(dst_tex, p, vec4<f32>(a, a, a, a)); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | // Pass 2: final despill + post-multiply + mask/blend |
| 380 | let a = clamp(textureLoad(tex_b, p, 0).a, 0.0, 1.0); |
| 381 | var rgb = despill_rgb(src.rgb, i32(u.p[3].x + 0.5), u.p[3].y, i32(u.p[3].z + 0.5)); |
| 382 | if (u.p[3].w > 0.5) { |
| 383 | rgb = rgb * a; |
| 384 | } |
| 385 | let keyed = vec4<f32>(rgb, a); |
| 386 | let out_col = apply_mask_blend( |
| 387 | src, keyed, gid.xy, dims, u.p[4].x, u.p[4].y, u.p[4].z, u.p[4].w, u.p[7], |
| 388 | ); |
| 389 | store_dst(p, out_col); |
| 390 | } |
| 391 | |
| 392 | // ============================================================================ |
| 393 | // 6. CleanPLate (GPU pull-push + Jacobi diffusion inpainting) |
| 394 | // ============================================================================ |
| 395 | |
| 396 | // Init: mark valid clean-plate pixels with a=1 and holes with a=0. |
| 397 | // p[0] = (bg_r, bg_g, bg_b, use_bg_key) |
| 398 | // p[1] = (low, high, has_garbage, garbage_inv) |
| 399 | @compute @workgroup_size(8, 8) |
| 400 | fn clean_plate_init_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 401 | let dims = textureDimensions(dst_tex); |
| 402 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 403 | let p = vec2<i32>(gid.xy); |
| 404 | let uv = (vec2<f32>(gid.xy) + vec2<f32>(0.5)) / vec2<f32>(dims); |
| 405 | let src = textureLoad(tex_a, p, 0); |
| 406 | |
| 407 | var valid = select(0.0, 1.0, src.a >= 0.99); |
| 408 | if (u.p[0].w > 0.5 && valid > 0.5) { |
| 409 | let dv = abs(src.rgb - u.p[0].xyz); |
| 410 | let d = max(dv.r, max(dv.g, dv.b)); |
| 411 | let lo = max(u.p[1].x, 0.0); |
| 412 | let hi = max(u.p[1].y, lo + 0.01); |
| 413 | if (d >= hi) { |
| 414 | valid = 0.0; |
| 415 | } else if (d > lo) { |
| 416 | valid = clamp((hi - d) / (hi - lo), 0.0, 1.0); |
| 417 | } |
| 418 | } |
| 419 | if (u.p[1].z > 0.5 && valid > 0.0) { |
| 420 | var g = sample_bilinear(tex_b, uv).a; |
| 421 | if (u.p[1].w > 0.5) { g = 1.0 - g; } |
| 422 | if (g > 0.5) { |
| 423 | valid = 0.0; |
| 424 | } |
| 425 | } |
| 426 | let rgb = select(vec3<f32>(0.0), src.rgb, valid > 0.0); |
| 427 | store_dst(p, vec4<f32>(rgb, valid)); |
| 428 | } |
| 429 | |
| 430 | // Erode valid mask only (preserving RGB of remaining valid pixels). |
| 431 | // p[0] = (radius_px, axis: 0=horiz 1=vert, 0, 0) |
| 432 | @compute @workgroup_size(8, 8) |
| 433 | fn clean_plate_erode_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 434 | let dims = textureDimensions(dst_tex); |
| 435 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 436 | let p = vec2<i32>(gid.xy); |
| 437 | let center = textureLoad(tex_a, p, 0); |
| 438 | let rad = clamp(i32(ceil(u.p[0].x)), 1, 32); |
| 439 | let dir = select(vec2<i32>(1, 0), vec2<i32>(0, 1), u.p[0].y > 0.5); |
| 440 | var min_a = center.a; |
| 441 | for (var k = -rad; k <= rad; k = k + 1) { |
| 442 | let s = load_clamped(tex_a, p + dir * k); |
| 443 | min_a = min(min_a, s.a); |
| 444 | } |
| 445 | let valid = select(0.0, 1.0, min_a > 0.5); |
| 446 | let rgb = select(vec3<f32>(0.0), center.rgb, valid > 0.5); |
| 447 | store_dst(p, vec4<f32>(rgb, valid)); |
| 448 | } |
| 449 | |
| 450 | // Pull (2x downsample weighted average of valid texels). |
| 451 | @compute @workgroup_size(8, 8) |
| 452 | fn clean_plate_pull_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 453 | let dst_dims = textureDimensions(dst_tex); |
| 454 | if (gid.x >= dst_dims.x || gid.y >= dst_dims.y) { return; } |
| 455 | let src_dims = vec2<i32>(textureDimensions(tex_a)); |
| 456 | let base = vec2<i32>(gid.xy) * 2; |
| 457 | let x_end = min(base.x + select(2, 3, i32(gid.x) == i32(dst_dims.x) - 1 && (src_dims.x & 1) == 1), src_dims.x); |
| 458 | let y_end = min(base.y + select(2, 3, i32(gid.y) == i32(dst_dims.y) - 1 && (src_dims.y & 1) == 1), src_dims.y); |
| 459 | |
| 460 | var sum_rgb = vec3<f32>(0.0); |
| 461 | var sum_w = 0.0; |
| 462 | var count = 0.0; |
| 463 | for (var y = base.y; y < y_end; y = y + 1) { |
| 464 | for (var x = base.x; x < x_end; x = x + 1) { |
| 465 | let s = textureLoad(tex_a, vec2<i32>(x, y), 0); |
| 466 | sum_rgb = sum_rgb + s.rgb * s.a; |
| 467 | sum_w = sum_w + s.a; |
| 468 | count = count + 1.0; |
| 469 | } |
| 470 | } |
| 471 | if (sum_w > 1e-6) { |
| 472 | store_dst(vec2<i32>(gid.xy), vec4<f32>(sum_rgb / sum_w, clamp(sum_w / max(count, 1.0), 0.0, 1.0))); |
| 473 | } else { |
| 474 | store_dst(vec2<i32>(gid.xy), vec4<f32>(0.0)); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | // Push (upsample coarse level `tex_b` into holes of fine level `tex_a`). |
| 479 | @compute @workgroup_size(8, 8) |
| 480 | fn clean_plate_push_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 481 | let dims = textureDimensions(dst_tex); |
| 482 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 483 | let p = vec2<i32>(gid.xy); |
| 484 | let fine = textureLoad(tex_a, p, 0); |
| 485 | if (fine.a >= 0.99) { |
| 486 | store_dst(p, vec4<f32>(fine.rgb, 1.0)); |
| 487 | return; |
| 488 | } |
| 489 | let uv = (vec2<f32>(gid.xy) + vec2<f32>(0.5)) / vec2<f32>(dims); |
| 490 | let coarse = sample_bilinear(tex_b, uv); |
| 491 | if (fine.a > 1e-5) { |
| 492 | let rgb = mix(coarse.rgb, fine.rgb, fine.a); |
| 493 | store_dst(p, vec4<f32>(rgb, select(fine.a, 1.0, coarse.a > 1e-4))); |
| 494 | } else if (coarse.a > 1e-5) { |
| 495 | store_dst(p, vec4<f32>(coarse.rgb, 1.0)); |
| 496 | } else { |
| 497 | store_dst(p, vec4<f32>(0.0)); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // Jacobi relaxation / iterative 1px edge-grow pass. |
| 502 | // tex_a = current estimate, tex_b = original locked init (orig_rgb, orig_valid). |
| 503 | @compute @workgroup_size(8, 8) |
| 504 | fn clean_plate_relax_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 505 | let dims = textureDimensions(dst_tex); |
| 506 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 507 | let p = vec2<i32>(gid.xy); |
| 508 | let orig = textureLoad(tex_b, p, 0); |
| 509 | if (orig.a >= 0.99) { |
| 510 | store_dst(p, vec4<f32>(orig.rgb, 1.0)); |
| 511 | return; |
| 512 | } |
| 513 | var sum_rgb = vec3<f32>(0.0); |
| 514 | var sum_w = 0.0; |
| 515 | for (var dy = -1; dy <= 1; dy = dy + 1) { |
| 516 | for (var dx = -1; dx <= 1; dx = dx + 1) { |
| 517 | if (dx == 0 && dy == 0) { continue; } |
| 518 | let k = select(1.0, 2.0, dx == 0 || dy == 0); |
| 519 | let s = load_clamped(tex_a, p + vec2<i32>(dx, dy)); |
| 520 | let w = k * s.a; |
| 521 | sum_rgb = sum_rgb + s.rgb * w; |
| 522 | sum_w = sum_w + w; |
| 523 | } |
| 524 | } |
| 525 | if (sum_w > 1e-6) { |
| 526 | store_dst(p, vec4<f32>(sum_rgb / sum_w, 1.0)); |
| 527 | } else { |
| 528 | store_dst(p, vec4<f32>(0.0)); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | // Finalize CleanPLate: enforce solid alpha on filled pixels + EffectMask / Blend / ProcessRGBA. |
| 533 | // tex_a = original input, tex_b = filled plate, mask_tex = EffectMask. |
| 534 | // p[0] = (blend, has_mask, mask_inv, mbm) |
| 535 | // p[1] = (skip_r, skip_g, skip_b, skip_a) |
| 536 | @compute @workgroup_size(8, 8) |
| 537 | fn clean_plate_finish_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 538 | let dims = textureDimensions(dst_tex); |
| 539 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 540 | let p = vec2<i32>(gid.xy); |
| 541 | let src = textureLoad(tex_a, p, 0); |
| 542 | let filled = textureLoad(tex_b, p, 0); |
| 543 | let res = select(vec4<f32>(0.0), vec4<f32>(filled.rgb, 1.0), filled.a > 0.5); |
| 544 | let out_col = apply_mask_blend( |
| 545 | src, res, gid.xy, dims, u.p[0].x, u.p[0].y, u.p[0].z, u.p[0].w, u.p[1], |
| 546 | ); |
| 547 | store_dst(p, out_col); |
| 548 | } |