| 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, 0, 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 | let out_col = apply_mask_blend( |
| 97 | src, res, gid.xy, dims, u.p[0].y, u.p[0].z, u.p[0].w, u.p[1].x, u.p[2], |
| 98 | ); |
| 99 | store_dst(p, out_col); |
| 100 | } |
| 101 | |
| 102 | // ============================================================================ |
| 103 | // 3. PseudoColor (Fusion procedural per-channel sine-wave false-color mapper) |
| 104 | // p[0] = (r_lo, r_hi, r_freq, r_phase) |
| 105 | // p[1] = (g_lo, g_hi, g_freq, g_phase) |
| 106 | // p[2] = (b_lo, b_hi, b_freq, b_phase) |
| 107 | // p[3] = (a_lo, a_hi, a_freq, a_phase) |
| 108 | // p[4] = (use_alpha, blend, has_mask, mask_inv) |
| 109 | // p[5] = (mbm, 0, 0, 0) |
| 110 | // p[6] = (skip_r, skip_g, skip_b, skip_a) |
| 111 | // ============================================================================ |
| 112 | fn pseudo_channel(val: f32, cfg: vec4<f32>) -> f32 { |
| 113 | if (val >= cfg.x && val <= cfg.y) { |
| 114 | return 0.5 + 0.5 * sin(TAU * (val * cfg.z + cfg.w)); |
| 115 | } |
| 116 | return val; |
| 117 | } |
| 118 | |
| 119 | @compute @workgroup_size(8, 8) |
| 120 | fn pseudo_color_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 121 | let dims = textureDimensions(dst_tex); |
| 122 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 123 | let p = vec2<i32>(gid.xy); |
| 124 | let src = textureLoad(tex_a, p, 0); |
| 125 | let r = pseudo_channel(src.r, u.p[0]); |
| 126 | let g = pseudo_channel(src.g, u.p[1]); |
| 127 | let b = pseudo_channel(src.b, u.p[2]); |
| 128 | let a = select(src.a, pseudo_channel(src.a, u.p[3]), u.p[4].x > 0.5); |
| 129 | let res = vec4<f32>(r, g, b, a); |
| 130 | let out_col = apply_mask_blend( |
| 131 | src, res, gid.xy, dims, u.p[4].y, u.p[4].z, u.p[4].w, u.p[5].x, u.p[6], |
| 132 | ); |
| 133 | store_dst(p, out_col); |
| 134 | } |
| 135 | |
| 136 | // ============================================================================ |
| 137 | // 4. DifferenceKeyer |
| 138 | // p[0] = (low, high, gain, softness) |
| 139 | // p[1] = (matte_contrast, matte_gamma, invert, post_mult) |
| 140 | // p[2] = (blend, has_mask, mask_inv, mbm) |
| 141 | // p[3] = (has_garbage, garbage_inv, has_solid, solid_inv) |
| 142 | // p[4] = (pass_mode: 0=raw_matte 1=combine_mattes 2=composite 3=single, 0, 0, 0) |
| 143 | // p[5] = (skip_r, skip_g, skip_b, skip_a) |
| 144 | // ============================================================================ |
| 145 | fn combine_garbage_solid(a_in: f32, uv: vec2<f32>, cfg: vec4<f32>) -> f32 { |
| 146 | var a = clamp(a_in, 0.0, 1.0); |
| 147 | if (cfg.z > 0.5) { |
| 148 | var s = sample_bilinear(tex_c, uv).a; |
| 149 | if (cfg.w > 0.5) { s = 1.0 - s; } |
| 150 | a = max(a, clamp(s, 0.0, 1.0)); |
| 151 | } |
| 152 | if (cfg.x > 0.5) { |
| 153 | var g = sample_bilinear(tex_b, uv).a; |
| 154 | if (cfg.y > 0.5) { g = 1.0 - g; } |
| 155 | a = a * (1.0 - clamp(g, 0.0, 1.0)); |
| 156 | } |
| 157 | return a; |
| 158 | } |
| 159 | |
| 160 | @compute @workgroup_size(8, 8) |
| 161 | fn diff_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 162 | let dims = textureDimensions(dst_tex); |
| 163 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 164 | let p = vec2<i32>(gid.xy); |
| 165 | let uv = (vec2<f32>(gid.xy) + vec2<f32>(0.5)) / vec2<f32>(dims); |
| 166 | let pass_mode = i32(u.p[4].x + 0.5); |
| 167 | |
| 168 | if (pass_mode == 1) { |
| 169 | let m = textureLoad(tex_a, p, 0).a; |
| 170 | let a = combine_garbage_solid(m, uv, u.p[3]); |
| 171 | textureStore(dst_tex, p, vec4<f32>(a, a, a, a)); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | let fg = textureLoad(tex_a, p, 0); |
| 176 | var a: f32; |
| 177 | if (pass_mode == 2) { |
| 178 | a = clamp(textureLoad(tex_b, p, 0).a, 0.0, 1.0); |
| 179 | } else { |
| 180 | let bg_dims = textureDimensions(tex_b); |
| 181 | let bg = select( |
| 182 | sample_bilinear(tex_b, uv), |
| 183 | textureLoad(tex_b, p, 0), |
| 184 | bg_dims.x == dims.x && bg_dims.y == dims.y, |
| 185 | ); |
| 186 | let dv = abs(fg.rgb - bg.rgb); |
| 187 | let diff = max(dv.r, max(dv.g, dv.b)) * u.p[0].z; |
| 188 | let low = u.p[0].x; |
| 189 | let high = max(u.p[0].y, low + u.p[0].w); |
| 190 | a = clamp((diff - low) / max(high - low, 1e-5), 0.0, 1.0); |
| 191 | let contrast = u.p[1].x; |
| 192 | if (contrast >= 0.0) { |
| 193 | a = clamp(a * (1.0 + 4.0 * contrast), 0.0, 1.0); |
| 194 | } else { |
| 195 | a = clamp(1.0 - (1.0 - a) * (1.0 - 4.0 * contrast), 0.0, 1.0); |
| 196 | } |
| 197 | a = pow(clamp(a, 0.0, 1.0), 1.0 / max(u.p[1].y, 1e-4)); |
| 198 | if (u.p[1].z > 0.5) { |
| 199 | a = 1.0 - a; |
| 200 | } |
| 201 | if (pass_mode == 0) { |
| 202 | textureStore(dst_tex, p, vec4<f32>(a, a, a, a)); |
| 203 | return; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | let post_mult = u.p[1].w > 0.5; |
| 208 | let keyed = vec4<f32>(select(fg.rgb, fg.rgb * a, post_mult), a); |
| 209 | let out_col = apply_mask_blend( |
| 210 | fg, keyed, gid.xy, dims, u.p[2].x, u.p[2].y, u.p[2].z, u.p[2].w, u.p[5], |
| 211 | ); |
| 212 | store_dst(p, out_col); |
| 213 | } |
| 214 | |
| 215 | // ============================================================================ |
| 216 | // 5. ChromaKeyer (Fusion Chroma/RGB/HLS range keyer + spill suppression) |
| 217 | // p[0] = (r_lo, r_hi, g_lo, g_hi) |
| 218 | // p[1] = (b_lo, b_hi, l_lo, l_hi) |
| 219 | // p[2] = (key_type: 0=YUV 1=RGB 2=HLS, soft_range, matte_gamma, invert_matte) |
| 220 | // p[3] = (spill_method: 0..4, spill_sup, spill_ch: 0=R 1=G 2=B, post_mult) |
| 221 | // p[4] = (blend, has_mask, mask_inv, mbm) |
| 222 | // p[5] = (has_garbage, garbage_inv, has_solid, solid_inv) |
| 223 | // p[6] = (pass_mode: 0=raw_matte 1=combine_mattes 2=composite, range_active, 0, 0) |
| 224 | // p[7] = (skip_r, skip_g, skip_b, skip_a) |
| 225 | // ============================================================================ |
| 226 | fn axis_dist(val: f32, lo: f32, hi: f32, soft: f32, wrap_hue: bool) -> f32 { |
| 227 | if (lo <= 1e-5 && hi >= 1.0 - 1e-5) { |
| 228 | return 0.0; |
| 229 | } |
| 230 | var d: f32; |
| 231 | if (wrap_hue) { |
| 232 | if (lo <= hi) { |
| 233 | if (val >= lo && val <= hi) { |
| 234 | d = 0.0; |
| 235 | } else { |
| 236 | let d_lo = min(abs(val - lo), 1.0 - abs(val - lo)); |
| 237 | let d_hi = min(abs(val - hi), 1.0 - abs(val - hi)); |
| 238 | d = min(d_lo, d_hi); |
| 239 | } |
| 240 | } else { |
| 241 | if (val >= lo || val <= hi) { |
| 242 | d = 0.0; |
| 243 | } else { |
| 244 | d = min(lo - val, val - hi); |
| 245 | } |
| 246 | } |
| 247 | } else { |
| 248 | d = max(max(lo - val, val - hi), 0.0); |
| 249 | } |
| 250 | if (soft > 1e-6) { |
| 251 | return clamp(d / soft, 0.0, 1.0); |
| 252 | } |
| 253 | return select(0.0, 1.0, d > 0.0); |
| 254 | } |
| 255 | |
| 256 | fn rgb_to_hls(rgb: vec3<f32>) -> vec3<f32> { |
| 257 | let mx = max(rgb.r, max(rgb.g, rgb.b)); |
| 258 | let mn = min(rgb.r, min(rgb.g, rgb.b)); |
| 259 | let d = mx - mn; |
| 260 | let l = 0.5 * (mx + mn); |
| 261 | var s = 0.0; |
| 262 | var h = 0.0; |
| 263 | if (d > 1e-6) { |
| 264 | s = d / max(1.0 - abs(2.0 * l - 1.0), 1e-6); |
| 265 | if (mx == rgb.r) { |
| 266 | h = (rgb.g - rgb.b) / d + select(0.0, 6.0, rgb.g < rgb.b); |
| 267 | } else if (mx == rgb.g) { |
| 268 | h = (rgb.b - rgb.r) / d + 2.0; |
| 269 | } else { |
| 270 | h = (rgb.r - rgb.g) / d + 4.0; |
| 271 | } |
| 272 | h = fract(h / 6.0); |
| 273 | } |
| 274 | return vec3<f32>(h, clamp(s, 0.0, 1.0), clamp(l, 0.0, 1.0)); |
| 275 | } |
| 276 | |
| 277 | fn despill_rgb(rgb: vec3<f32>, method: i32, amount: f32, ch: i32) -> vec3<f32> { |
| 278 | if (method <= 0 || amount <= 1e-5) { |
| 279 | return rgb; |
| 280 | } |
| 281 | var dom = rgb.g; |
| 282 | var o1 = rgb.r; |
| 283 | var o2 = rgb.b; |
| 284 | if (ch == 0) { |
| 285 | dom = rgb.r; o1 = rgb.g; o2 = rgb.b; |
| 286 | } else if (ch == 2) { |
| 287 | dom = rgb.b; o1 = rgb.r; o2 = rgb.g; |
| 288 | } |
| 289 | let hi = max(o1, o2); |
| 290 | let lo = min(o1, o2); |
| 291 | var lim = hi; |
| 292 | if (method == 2) { |
| 293 | lim = 0.5 * (o1 + o2); |
| 294 | } else if (method == 3) { |
| 295 | lim = 0.25 * hi + 0.75 * lo; |
| 296 | } else if (method >= 4) { |
| 297 | lim = lo; |
| 298 | } |
| 299 | var new_dom = dom; |
| 300 | // Rare & Medium only suppress where the key channel is dominant; WellDone/Burnt clamp down to lim. |
| 301 | let do_despill = select(dom > lim, dom > hi, method <= 2); |
| 302 | if (do_despill) { |
| 303 | new_dom = mix(dom, min(dom, lim), clamp(amount, 0.0, 1.0)); |
| 304 | } |
| 305 | if (ch == 0) { return vec3<f32>(new_dom, rgb.g, rgb.b); } |
| 306 | if (ch == 2) { return vec3<f32>(rgb.r, rgb.g, new_dom); } |
| 307 | return vec3<f32>(rgb.r, new_dom, rgb.b); |
| 308 | } |
| 309 | |
| 310 | @compute @workgroup_size(8, 8) |
| 311 | fn chroma_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 312 | let dims = textureDimensions(dst_tex); |
| 313 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 314 | let p = vec2<i32>(gid.xy); |
| 315 | let uv = (vec2<f32>(gid.xy) + vec2<f32>(0.5)) / vec2<f32>(dims); |
| 316 | let pass_mode = i32(u.p[6].x + 0.5); |
| 317 | |
| 318 | if (pass_mode == 1) { |
| 319 | let m = textureLoad(tex_a, p, 0).a; |
| 320 | let a = combine_garbage_solid(m, uv, u.p[5]); |
| 321 | textureStore(dst_tex, p, vec4<f32>(a, a, a, a)); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | let src = textureLoad(tex_a, p, 0); |
| 326 | if (pass_mode == 0) { |
| 327 | var a = src.a; |
| 328 | if (u.p[6].y > 0.5) { |
| 329 | let kt = i32(u.p[2].x + 0.5); |
| 330 | let soft = u.p[2].y; |
| 331 | let lum = dot(src.rgb, vec3<f32>(0.299, 0.587, 0.114)); |
| 332 | var c0 = src.r; |
| 333 | var c1 = src.g; |
| 334 | var c2 = src.b; |
| 335 | let c3 = lum; |
| 336 | if (kt == 0) { |
| 337 | c0 = 0.5 + (src.r - lum) / 1.60; |
| 338 | c1 = 0.5 + (src.g - lum) / 1.32; |
| 339 | c2 = 0.5 + (src.b - lum) / 2.24; |
| 340 | } else if (kt == 2) { |
| 341 | let hls = rgb_to_hls(src.rgb); |
| 342 | c0 = hls.x; |
| 343 | c1 = hls.y; |
| 344 | c2 = hls.z; |
| 345 | } |
| 346 | let t0 = axis_dist(c0, u.p[0].x, u.p[0].y, soft, kt == 2); |
| 347 | let t1 = axis_dist(c1, u.p[0].z, u.p[0].w, soft, false); |
| 348 | let t2 = axis_dist(c2, u.p[1].x, u.p[1].y, soft, false); |
| 349 | let t3 = axis_dist(c3, u.p[1].z, u.p[1].w, soft, false); |
| 350 | a = max(max(t0, t1), max(t2, t3)); |
| 351 | a = pow(clamp(a, 0.0, 1.0), 1.0 / max(u.p[2].z, 1e-4)); |
| 352 | if (u.p[2].w > 0.5) { |
| 353 | a = 1.0 - a; |
| 354 | } |
| 355 | } |
| 356 | textureStore(dst_tex, p, vec4<f32>(a, a, a, a)); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | // Pass 2: final despill + post-multiply + mask/blend |
| 361 | let a = clamp(textureLoad(tex_b, p, 0).a, 0.0, 1.0); |
| 362 | var rgb = despill_rgb(src.rgb, i32(u.p[3].x + 0.5), u.p[3].y, i32(u.p[3].z + 0.5)); |
| 363 | if (u.p[3].w > 0.5) { |
| 364 | rgb = rgb * a; |
| 365 | } |
| 366 | let keyed = vec4<f32>(rgb, a); |
| 367 | let out_col = apply_mask_blend( |
| 368 | src, keyed, gid.xy, dims, u.p[4].x, u.p[4].y, u.p[4].z, u.p[4].w, u.p[7], |
| 369 | ); |
| 370 | store_dst(p, out_col); |
| 371 | } |
| 372 | |
| 373 | // ============================================================================ |
| 374 | // 6. CleanPLate (GPU pull-push + Jacobi diffusion inpainting) |
| 375 | // ============================================================================ |
| 376 | |
| 377 | // Init: mark valid clean-plate pixels with a=1 and holes with a=0. |
| 378 | // p[0] = (bg_r, bg_g, bg_b, use_bg_key) |
| 379 | // p[1] = (low, high, has_garbage, garbage_inv) |
| 380 | @compute @workgroup_size(8, 8) |
| 381 | fn clean_plate_init_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 382 | let dims = textureDimensions(dst_tex); |
| 383 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 384 | let p = vec2<i32>(gid.xy); |
| 385 | let uv = (vec2<f32>(gid.xy) + vec2<f32>(0.5)) / vec2<f32>(dims); |
| 386 | let src = textureLoad(tex_a, p, 0); |
| 387 | |
| 388 | var valid = select(0.0, 1.0, src.a >= 0.99); |
| 389 | if (u.p[0].w > 0.5 && valid > 0.5) { |
| 390 | let dv = abs(src.rgb - u.p[0].xyz); |
| 391 | let d = max(dv.r, max(dv.g, dv.b)); |
| 392 | let lo = max(u.p[1].x, 0.0); |
| 393 | let hi = max(u.p[1].y, lo + 0.01); |
| 394 | if (d >= hi) { |
| 395 | valid = 0.0; |
| 396 | } else if (d > lo) { |
| 397 | valid = clamp((hi - d) / (hi - lo), 0.0, 1.0); |
| 398 | } |
| 399 | } |
| 400 | if (u.p[1].z > 0.5 && valid > 0.0) { |
| 401 | var g = sample_bilinear(tex_b, uv).a; |
| 402 | if (u.p[1].w > 0.5) { g = 1.0 - g; } |
| 403 | if (g > 0.5) { |
| 404 | valid = 0.0; |
| 405 | } |
| 406 | } |
| 407 | let rgb = select(vec3<f32>(0.0), src.rgb, valid > 0.0); |
| 408 | store_dst(p, vec4<f32>(rgb, valid)); |
| 409 | } |
| 410 | |
| 411 | // Erode valid mask only (preserving RGB of remaining valid pixels). |
| 412 | // p[0] = (radius_px, axis: 0=horiz 1=vert, 0, 0) |
| 413 | @compute @workgroup_size(8, 8) |
| 414 | fn clean_plate_erode_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 415 | let dims = textureDimensions(dst_tex); |
| 416 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 417 | let p = vec2<i32>(gid.xy); |
| 418 | let center = textureLoad(tex_a, p, 0); |
| 419 | let rad = clamp(i32(ceil(u.p[0].x)), 1, 32); |
| 420 | let dir = select(vec2<i32>(1, 0), vec2<i32>(0, 1), u.p[0].y > 0.5); |
| 421 | var min_a = center.a; |
| 422 | for (var k = -rad; k <= rad; k = k + 1) { |
| 423 | let s = load_clamped(tex_a, p + dir * k); |
| 424 | min_a = min(min_a, s.a); |
| 425 | } |
| 426 | let valid = select(0.0, 1.0, min_a > 0.5); |
| 427 | let rgb = select(vec3<f32>(0.0), center.rgb, valid > 0.5); |
| 428 | store_dst(p, vec4<f32>(rgb, valid)); |
| 429 | } |
| 430 | |
| 431 | // Pull (2x downsample weighted average of valid texels). |
| 432 | @compute @workgroup_size(8, 8) |
| 433 | fn clean_plate_pull_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 434 | let dst_dims = textureDimensions(dst_tex); |
| 435 | if (gid.x >= dst_dims.x || gid.y >= dst_dims.y) { return; } |
| 436 | let src_dims = vec2<i32>(textureDimensions(tex_a)); |
| 437 | let base = vec2<i32>(gid.xy) * 2; |
| 438 | 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); |
| 439 | 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); |
| 440 | |
| 441 | var sum_rgb = vec3<f32>(0.0); |
| 442 | var sum_w = 0.0; |
| 443 | var count = 0.0; |
| 444 | for (var y = base.y; y < y_end; y = y + 1) { |
| 445 | for (var x = base.x; x < x_end; x = x + 1) { |
| 446 | let s = textureLoad(tex_a, vec2<i32>(x, y), 0); |
| 447 | sum_rgb = sum_rgb + s.rgb * s.a; |
| 448 | sum_w = sum_w + s.a; |
| 449 | count = count + 1.0; |
| 450 | } |
| 451 | } |
| 452 | if (sum_w > 1e-6) { |
| 453 | store_dst(vec2<i32>(gid.xy), vec4<f32>(sum_rgb / sum_w, clamp(sum_w / max(count, 1.0), 0.0, 1.0))); |
| 454 | } else { |
| 455 | store_dst(vec2<i32>(gid.xy), vec4<f32>(0.0)); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // Push (upsample coarse level `tex_b` into holes of fine level `tex_a`). |
| 460 | @compute @workgroup_size(8, 8) |
| 461 | fn clean_plate_push_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 462 | let dims = textureDimensions(dst_tex); |
| 463 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 464 | let p = vec2<i32>(gid.xy); |
| 465 | let fine = textureLoad(tex_a, p, 0); |
| 466 | if (fine.a >= 0.99) { |
| 467 | store_dst(p, vec4<f32>(fine.rgb, 1.0)); |
| 468 | return; |
| 469 | } |
| 470 | let uv = (vec2<f32>(gid.xy) + vec2<f32>(0.5)) / vec2<f32>(dims); |
| 471 | let coarse = sample_bilinear(tex_b, uv); |
| 472 | if (fine.a > 1e-5) { |
| 473 | let rgb = mix(coarse.rgb, fine.rgb, fine.a); |
| 474 | store_dst(p, vec4<f32>(rgb, select(fine.a, 1.0, coarse.a > 1e-4))); |
| 475 | } else if (coarse.a > 1e-5) { |
| 476 | store_dst(p, vec4<f32>(coarse.rgb, 1.0)); |
| 477 | } else { |
| 478 | store_dst(p, vec4<f32>(0.0)); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | // Jacobi relaxation / iterative 1px edge-grow pass. |
| 483 | // tex_a = current estimate, tex_b = original locked init (orig_rgb, orig_valid). |
| 484 | @compute @workgroup_size(8, 8) |
| 485 | fn clean_plate_relax_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 486 | let dims = textureDimensions(dst_tex); |
| 487 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 488 | let p = vec2<i32>(gid.xy); |
| 489 | let orig = textureLoad(tex_b, p, 0); |
| 490 | if (orig.a >= 0.99) { |
| 491 | store_dst(p, vec4<f32>(orig.rgb, 1.0)); |
| 492 | return; |
| 493 | } |
| 494 | var sum_rgb = vec3<f32>(0.0); |
| 495 | var sum_w = 0.0; |
| 496 | for (var dy = -1; dy <= 1; dy = dy + 1) { |
| 497 | for (var dx = -1; dx <= 1; dx = dx + 1) { |
| 498 | if (dx == 0 && dy == 0) { continue; } |
| 499 | let k = select(1.0, 2.0, dx == 0 || dy == 0); |
| 500 | let s = load_clamped(tex_a, p + vec2<i32>(dx, dy)); |
| 501 | let w = k * s.a; |
| 502 | sum_rgb = sum_rgb + s.rgb * w; |
| 503 | sum_w = sum_w + w; |
| 504 | } |
| 505 | } |
| 506 | if (sum_w > 1e-6) { |
| 507 | store_dst(p, vec4<f32>(sum_rgb / sum_w, 1.0)); |
| 508 | } else { |
| 509 | store_dst(p, vec4<f32>(0.0)); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // Finalize CleanPLate: enforce solid alpha on filled pixels + EffectMask / Blend / ProcessRGBA. |
| 514 | // tex_a = original input, tex_b = filled plate, mask_tex = EffectMask. |
| 515 | // p[0] = (blend, has_mask, mask_inv, mbm) |
| 516 | // p[1] = (skip_r, skip_g, skip_b, skip_a) |
| 517 | @compute @workgroup_size(8, 8) |
| 518 | fn clean_plate_finish_op(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 519 | let dims = textureDimensions(dst_tex); |
| 520 | if (gid.x >= dims.x || gid.y >= dims.y) { return; } |
| 521 | let p = vec2<i32>(gid.xy); |
| 522 | let src = textureLoad(tex_a, p, 0); |
| 523 | let filled = textureLoad(tex_b, p, 0); |
| 524 | let res = select(vec4<f32>(0.0), vec4<f32>(filled.rgb, 1.0), filled.a > 0.5); |
| 525 | let out_col = apply_mask_blend( |
| 526 | src, res, gid.xy, dims, u.p[0].x, u.p[0].y, u.p[0].z, u.p[0].w, u.p[1], |
| 527 | ); |
| 528 | store_dst(p, out_col); |
| 529 | } |