1// ============================================================================
2// 6. LumaKeyer
3// p[0] = (low, high, invert, blend)
4// p[1] = (has_mask, mask_invert, mbm, post_multiply)
5// p[2] = (matte_contrast, matte_gamma, channel, pass_mode: 0=matte, 1=composite, 2=single)
6// p[3] = (skip_r, skip_g, skip_b, skip_a)
7// ============================================================================
8fn luma_key_channel(src: vec4<f32>, ch: i32) -> f32 {
9 if (ch == 0) { return src.b; }
10 if (ch == 1) { return src.g; }
11 if (ch == 2) { return src.r; }
12 if (ch == 3) { return src.a; }
13 if (ch == 5) {
14 let mx = max(src.r, max(src.g, src.b));
15 let mn = min(src.r, min(src.g, src.b));
16 return select((mx - mn) / mx, 0.0, mx <= 1e-5);
17 }
18 return dot(src.rgb, vec3<f32>(0.299, 0.587, 0.114));
19}
20
21@compute @workgroup_size(8, 8)
22fn luma_key_op(@builtin(global_invocation_id) gid: vec3<u32>) {
23 let dims = textureDimensions(dst_tex);
24 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
25 let p = vec2<i32>(gid.xy);
26 let src = textureLoad(src_a, p, 0);
27 let pass_mode = i32(u.p[2].w + 0.5);
28
29 var a: f32;
30 if (pass_mode == 1) {
31 a = clamp(textureLoad(src_b, p, 0).a, 0.0, 1.0);
32 } else {
33 let low = u.p[0].x;
34 let high = u.p[0].y;
35 let invert = u.p[0].z > 0.5;
36 let val = luma_key_channel(src, i32(u.p[2].z + 0.5));
37 a = clamp((val - low) / max(high - low, 1e-5), 0.0, 1.0);
38 let contrast = u.p[2].x;
39 if (contrast >= 0.0) {
40 a = clamp(a * (1.0 + 4.0 * contrast), 0.0, 1.0);
41 } else {
42 a = clamp(1.0 - (1.0 - a) * (1.0 - 4.0 * contrast), 0.0, 1.0);
43 }
44 a = pow(clamp(a, 0.0, 1.0), 1.0 / max(u.p[2].y, 1e-4));
45 if (invert) {
46 a = 1.0 - a;
47 }
48 if (pass_mode == 0) {
49 textureStore(dst_tex, p, vec4<f32>(a, a, a, a));
50 return;
51 }
52 }
53
54 let post_mult = u.p[1].w > 0.5;
55 let keyed = vec4<f32>(select(src.rgb, src.rgb * a, post_mult), a);
56 let has_mask = u.p[1].x > 0.5;
57 let mbm = has_mask && u.p[1].z > 0.5;
58 let raw_mask = read_mask(gid.xy, dims, u.p[1].x, u.p[1].y);
59 let m = clamp(raw_mask * u.p[0].w, 0.0, 1.0);
60 var out_col = select(mix(src, keyed, m), keyed * m, mbm);
61 let unproc = select(src, vec4<f32>(0.0), mbm);
62 out_col = select(out_col, unproc, u.p[3] > vec4<f32>(0.5));
63 store_dst(p, out_col);
64}
65// ============================================================================
66// 8. FastNoise (3D simplex FBM with Seethe, Angle, Discontinuous & gradient LUT)
67// p[0] = (x_scale, y_scale, detail, contrast)
68// p[1] = (brightness, seethe, discontinuous, center_x)
69// p[2] = (center_y, angle_rad, inverted, type) with type 2 = Gradient, else Two Color
70// p[3] = color1 RGBA, p[4] = color2 RGBA (used directly in Two Color mode without clamping `val`)
71// src_b = 256x1 straight-alpha color LUT indexed by floor(256 * value) in Gradient mode
72// ============================================================================
73const PERM: array<u32, 256> = array<u32, 256>(
74 151u, 160u, 137u, 91u, 90u, 15u, 131u, 13u, 201u, 95u, 96u, 53u, 194u, 233u, 7u, 225u,
75 140u, 36u, 103u, 30u, 69u, 142u, 8u, 99u, 37u, 240u, 21u, 10u, 23u, 190u, 6u, 148u,
76 247u, 120u, 234u, 75u, 0u, 26u, 197u, 62u, 94u, 252u, 219u, 203u, 117u, 35u, 11u, 32u,
77 57u, 177u, 33u, 88u, 237u, 149u, 56u, 87u, 174u, 20u, 125u, 136u, 171u, 168u, 68u, 175u,
78 74u, 165u, 71u, 134u, 139u, 48u, 27u, 166u, 77u, 146u, 158u, 231u, 83u, 111u, 229u, 122u,
79 60u, 211u, 133u, 230u, 220u, 105u, 92u, 41u, 55u, 46u, 245u, 40u, 244u, 102u, 143u, 54u,
80 65u, 25u, 63u, 161u, 1u, 216u, 80u, 73u, 209u, 76u, 132u, 187u, 208u, 89u, 18u, 169u,
81 200u, 196u, 135u, 130u, 116u, 188u, 159u, 86u, 164u, 100u, 109u, 198u, 173u, 186u, 3u, 64u,
82 52u, 217u, 226u, 250u, 124u, 123u, 5u, 202u, 38u, 147u, 118u, 126u, 255u, 82u, 85u, 212u,
83 207u, 206u, 59u, 227u, 47u, 16u, 58u, 17u, 182u, 189u, 28u, 42u, 223u, 183u, 170u, 213u,
84 119u, 248u, 152u, 2u, 44u, 154u, 163u, 70u, 221u, 153u, 101u, 155u, 167u, 43u, 172u, 9u,
85 129u, 22u, 39u, 253u, 19u, 98u, 108u, 110u, 79u, 113u, 224u, 232u, 178u, 185u, 112u, 104u,
86 218u, 246u, 97u, 228u, 251u, 34u, 242u, 193u, 238u, 210u, 144u, 12u, 191u, 179u, 162u, 241u,
87 81u, 51u, 145u, 235u, 249u, 14u, 239u, 107u, 49u, 192u, 214u, 31u, 181u, 199u, 106u, 157u,
88 184u, 84u, 204u, 176u, 115u, 121u, 50u, 45u, 127u, 4u, 150u, 254u, 138u, 236u, 205u, 93u,
89 222u, 114u, 67u, 29u, 24u, 72u, 243u, 141u, 128u, 195u, 78u, 66u, 215u, 61u, 156u, 180u
90);
91
92const GRAD16: array<vec3<f32>, 16> = array<vec3<f32>, 16>(
93 vec3<f32>(0.0, 1.0, 1.0), vec3<f32>(0.0, 1.0, -1.0), vec3<f32>(0.0, -1.0, 1.0), vec3<f32>(0.0, -1.0, -1.0),
94 vec3<f32>(1.0, 0.0, 1.0), vec3<f32>(1.0, 0.0, -1.0), vec3<f32>(-1.0, 0.0, 1.0), vec3<f32>(-1.0, 0.0, -1.0),
95 vec3<f32>(1.0, 1.0, 0.0), vec3<f32>(1.0, -1.0, 0.0), vec3<f32>(-1.0, 1.0, 0.0), vec3<f32>(-1.0, -1.0, 0.0),
96 vec3<f32>(1.0, 0.0, -1.0), vec3<f32>(-1.0, 0.0, -1.0), vec3<f32>(0.0, -1.0, 1.0), vec3<f32>(0.0, 1.0, 1.0),
97);
98
99fn simplex_corner(ijk: vec3<i32>, d: vec3<f32>) -> f32 {
100 let t = SIMPLEX_RADIUS2 - dot(d, d);
101 if (t <= 0.0) { return 0.0; }
102 let c = vec3<u32>(ijk & vec3<i32>(255));
103 let h = PERM[(c.z + PERM[(c.y + PERM[c.x]) & 255u]) & 255u] & 15u;
104 let t2 = t * t;
105 return t2 * t2 * dot(GRAD16[h], d);
106}
107
108// Fusion's lattice: Gustavson-style 3D simplex noise with kernel (0.5196 - r^2)^4 and a 16-entry
109// gradient table, recovered from Float32 probes (residual 2e-7).
110fn simplex3d(p: vec3<f32>) -> f32 {
111 let cell = floor(p + (p.x + p.y + p.z) * (1.0 / 3.0));
112 let ijk = vec3<i32>(cell);
113 let d0 = p - (cell - (cell.x + cell.y + cell.z) * (1.0 / 6.0));
114 var a = vec3<i32>(0);
115 var b = vec3<i32>(0);
116 if (d0.x >= d0.y) {
117 if (d0.y >= d0.z) { a = vec3<i32>(1, 0, 0); b = vec3<i32>(1, 1, 0); }
118 else if (d0.x >= d0.z) { a = vec3<i32>(1, 0, 0); b = vec3<i32>(1, 0, 1); }
119 else { a = vec3<i32>(0, 0, 1); b = vec3<i32>(1, 0, 1); }
120 } else {
121 if (d0.y < d0.z) { a = vec3<i32>(0, 0, 1); b = vec3<i32>(0, 1, 1); }
122 else if (d0.x < d0.z) { a = vec3<i32>(0, 1, 0); b = vec3<i32>(0, 1, 1); }
123 else { a = vec3<i32>(0, 1, 0); b = vec3<i32>(1, 1, 0); }
124 }
125 let g = 1.0 / 6.0;
126 return SIMPLEX_GAIN * (
127 simplex_corner(ijk, d0)
128 + simplex_corner(ijk + a, d0 - vec3<f32>(a) + g)
129 + simplex_corner(ijk + b, d0 - vec3<f32>(b) + 2.0 * g)
130 + simplex_corner(ijk + vec3<i32>(1), d0 - 0.5)
131 );
132}
133
134// Octave i samples the lattice at 2^i * (x, y, seethe) + 50.3 * i with weight 0.5^i; a fractional
135// Detail scales the last octave. Discontinuous sums 2|n| per octave, Contrast pivots on 0.5 (on 0
136// when Discontinuous), Inverted runs before Brightness, Brightness adds half its value, and
137// Gradient + Discontinuous shifts the lookup by 0.25.
138const SIMPLEX_RADIUS2: f32 = 0.5196152;
139const SIMPLEX_GAIN: f32 = 16.642;
140const OCTAVE_OFFSET: f32 = 50.3;
141
142@compute @workgroup_size(8, 8)
143fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {
144 let dims = textureDimensions(dst_tex);
145 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
146 let center = vec2<f32>(u.p[1].w, u.p[2].x);
147 let scale = u.p[0].xy;
148 let detail = clamp(u.p[0].z, 0.0, 10.0);
149 let contrast = u.p[0].w;
150 let brightness = u.p[1].x;
151 let seethe = u.p[1].y;
152 let disc = u.p[1].z > 0.5;
153 let angle = u.p[2].y;
154 let gradient = u.p[2].w > 1.5;
155
156 var sum = 0.0;
157 if (abs(contrast) > 1e-6 && detail > 0.0) {
158 // Frame-width units, y up, measured from the pixel corner.
159 let w = f32(dims.x);
160 let h = f32(dims.y);
161 let inv_w = 1.0 / w;
162 let rel = vec2<f32>(f32(gid.x) * inv_w, (h - 1.0 - f32(gid.y)) * inv_w) - center * vec2<f32>(1.0, h * inv_w);
163 var p: vec2<f32>;
164 if (abs(angle) < 1e-6) {
165 p = rel * scale;
166 } else {
167 let ca = cos(angle);
168 let sa = sin(angle);
169 p = vec2<f32>(rel.x * ca + rel.y * sa, rel.y * ca - rel.x * sa) * scale;
170 }
171
172 let full_octaves = i32(floor(detail));
173 let frac_w = detail - f32(full_octaves);
174 var amp = select(1.0, 2.0, disc);
175 var cur = vec3<f32>(p, seethe);
176 var off = 0.0;
177 for (var i = 0; i < full_octaves; i = i + 1) {
178 let n = simplex3d(cur + vec3<f32>(off));
179 sum = sum + amp * select(n, abs(n), disc);
180 cur = cur * 2.0;
181 off = off + OCTAVE_OFFSET;
182 amp = amp * 0.5;
183 }
184 if (frac_w > 0.0) {
185 let n = simplex3d(cur + vec3<f32>(off));
186 sum = sum + (amp * frac_w) * select(n, abs(n), disc);
187 }
188 }
189
190 var val = select(0.5 + sum, sum, disc);
191 val = select((val - 0.5) * contrast + 0.5, val * contrast, disc);
192 if (gradient && disc) { val = val + 0.25; }
193 if (u.p[2].z > 0.5) { val = 1.0 - val; }
194 val = val + 0.5 * brightness;
195
196 // Fusion leaves the ramp color unpremultiplied; downstream Merge treats it as premultiplied.
197 // Two-Color mode (Type == 0) interpolates Color1 -> Color2 without clamping `val`, allowing
198 // Contrast > 1.0 to extrapolate HDR/bright highlights; Gradient mode clamps lookup to [0, 1].
199 var out_col: vec4<f32>;
200 if (gradient) {
201 let i = clamp(i32(floor(val * 256.0)), 0, 255);
202 out_col = textureLoad(src_b, vec2<i32>(i, 0), 0);
203 } else {
204 out_col = mix(u.p[3], u.p[4], val);
205 }
206 store_dst(vec2<i32>(gid.xy), out_col);
207}
208// ============================================================================
209// 12. Fuse.Outline (Clover's exact 2020 Outline.fuse passes!)
210// outline_boost: boosts alpha * 120 (clamped in R/A via normalized scale, so we pass scale factor!)
211// outline_finish: clamps dilated alpha, tints with outline color, then composites original on top!
212// ============================================================================
213@compute @workgroup_size(8, 8)
214fn outline_boost(@builtin(global_invocation_id) gid: vec3<u32>) {
215 let dims = textureDimensions(dst_tex);
216 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
217 let src = textureLoad(src_a, vec2<i32>(gid.xy), 0);
218 let a = clamp(src.a * 120.0, 0.0, 1.0);
219 textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(a, a, a, a));
220}
221
222@compute @workgroup_size(8, 8)
223fn outline_tint(@builtin(global_invocation_id) gid: vec3<u32>) {
224 let dims = textureDimensions(dst_tex);
225 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
226 let blurred = textureLoad(src_a, vec2<i32>(gid.xy), 0);
227 let col = u.p[0]; // (r, g, b, a)
228 // Because we clamped boosted alpha to 1.0 in u8, emulate the 120x dilation threshold
229 let a = clamp(blurred.a * u.p[1].x, 0.0, 1.0) * col.a;
230 textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(col.rgb * a, a));
231}
232
233@compute @workgroup_size(8, 8)
234fn outline_composite(@builtin(global_invocation_id) gid: vec3<u32>) {
235 let dims = textureDimensions(dst_tex);
236 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
237 let outline = textureLoad(src_a, vec2<i32>(gid.xy), 0);
238 let orig = textureLoad(src_b, vec2<i32>(gid.xy), 0);
239 // Original over outline
240 let out_rgb = orig.rgb + outline.rgb * (1.0 - orig.a);
241 let out_a = orig.a + outline.a * (1.0 - orig.a);
242 store_dst(vec2<i32>(gid.xy), vec4<f32>(out_rgb, out_a));
243}
244
245// ============================================================================
246// 13. Custom Wave Shader (matches `sin(x|y * w * n1 + n2 * n1 * w) * 0.5 + 0.5` per channel)
247// p[0] = (n1, n2, w_ref, 0)
248// p[1] = (r_mode, g_mode, b_mode, a_const) where mode: 0=const 0, 1=sin(x*...), 2=sin(y*...)
249// ============================================================================
250fn eval_wave_ch(mode: i32, fu: vec2<f32>, n1: f32, n2: f32, w: f32) -> f32 {
251 let deg2rad = 0.017453292519943295;
252 if (mode == 1) {
253 return sin((fu.x * w * n1 + n2 * n1 * w) * deg2rad) * 0.5 + 0.5;
254 } else if (mode == 2) {
255 return sin((fu.y * w * n1 + n2 * n1 * w) * deg2rad) * 0.5 + 0.5;
256 }
257 return 0.0;
258}
259
260@compute @workgroup_size(8, 8)
261fn custom_wave_op(@builtin(global_invocation_id) gid: vec3<u32>) {
262 let dims = textureDimensions(dst_tex);
263 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
264 let fu = pix_to_fu(gid.xy, dims);
265 let n1 = u.p[0].x;
266 let n2 = u.p[0].y;
267 let w = u.p[0].z;
268 // Fusion CustomTool trig functions operate in DEGREES!
269 let r = eval_wave_ch(i32(u.p[1].x + 0.5), fu, n1, n2, w);
270 let g = eval_wave_ch(i32(u.p[1].y + 0.5), fu, n1, n2, w);
271 let b = eval_wave_ch(i32(u.p[1].z + 0.5), fu, n1, n2, w);
272 let a = u.p[1].w;
273 textureStore(dst_tex, vec2<i32>(gid.xy), vec4<f32>(r, g, b, a));
274}
275// ============================================================================
276// 26. UltraKeyer (Fusion color-difference chroma keyer + background spill subtract)
277// p[0] = (bg_r, bg_g, bg_b, matte_sep)
278// p[1] = (low_thresh, high_thresh, post_mult, spill_sup)
279// p[2] = (blend, has_mask, mask_invert, invert_matte)
280// p[3] = (r_lo, r_hi, g_lo, g_hi)
281// p[4] = (b_lo, b_hi, l_lo, l_hi)
282// p[5] = (bg_untouched, gamma, mbm, range_active)
283// p[6] = (skip_r, skip_g, skip_b, skip_a)
284// ============================================================================
285fn uk_axis_in(v: f32, lo: f32, hi: f32) -> bool {
286 if (lo <= 1e-5 && hi >= 1.0 - 1e-5) { return true; }
287 return v >= lo && v <= hi;
288}
289
290fn uk_in_range(src: vec4<f32>) -> bool {
291 if (u.p[5].w < 0.5) { return false; }
292 let lum = dot(src.rgb, vec3<f32>(0.299, 0.587, 0.114));
293 let v_r = 0.5 + (src.r - lum) / 1.60;
294 let v_g = 0.5 + (src.g - lum) / 1.32;
295 let v_b = 0.5 + (src.b - lum) / 2.24;
296 return uk_axis_in(v_r, u.p[3].x, u.p[3].y)
297 && uk_axis_in(v_g, u.p[3].z, u.p[3].w)
298 && uk_axis_in(v_b, u.p[4].x, u.p[4].y)
299 && uk_axis_in(lum, u.p[4].z, u.p[4].w);
300}
301
302@compute @workgroup_size(8, 8)
303fn ultra_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) {
304 let dims = textureDimensions(dst_tex);
305 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
306 let src = textureLoad(src_a, vec2<i32>(gid.xy), 0);
307 let bg = u.p[0].xyz;
308 let lo_th = u.p[1].x;
309 let hi_th = max(u.p[1].y, lo_th + 1e-4);
310 let post_mult = u.p[1].z > 0.5;
311 let spill_sup = u.p[1].w;
312 let blend = u.p[2].x;
313 let inv_matte = u.p[2].w > 0.5;
314 let bg_untouched = u.p[5].x > 0.5;
315 let gamma = max(u.p[5].y, 1e-4);
316
317 var rgb = src.rgb;
318 var alpha: f32;
319 if (bg_untouched) {
320 var raw_a = clamp(1.0 - max(src.b - src.r, 0.0), 0.0, 1.0);
321 if (u.p[0].w > 1e-4) {
322 raw_a = clamp(pow(raw_a, 1.0 + u.p[0].w * 4.0), 0.0, 1.0);
323 }
324 alpha = pow(clamp((raw_a - lo_th) / (hi_th - lo_th), 0.0, 1.0), 1.0 / gamma);
325 if (inv_matte) { alpha = 1.0 - alpha; }
326 if (uk_in_range(src)) { alpha = 0.0; }
327 rgb = src.rgb * alpha;
328 } else {
329 // Fusion 21.1 UltraKeyer exact color-difference matte + background subtraction
330 var k = 0.0;
331 if (bg.g >= bg.r && bg.g >= bg.b) {
332 let bg_ex = max(bg.g - 0.5 * (bg.r + bg.b), 1e-4);
333 k = max((src.g - 0.5 * (src.r + src.b)) / bg_ex, 0.0);
334 } else if (bg.b >= bg.r && bg.b >= bg.g) {
335 let bg_ex = max(bg.b - 0.5 * (bg.r + bg.g), 1e-4);
336 k = max((src.b - 0.5 * (src.r + src.g)) / bg_ex, 0.0);
337 } else {
338 let bg_ex = max(bg.r - 0.5 * (bg.g + bg.b), 1e-4);
339 k = max((src.r - 0.5 * (src.g + src.b)) / bg_ex, 0.0);
340 }
341
342 rgb = max(src.rgb - bg * k, vec3<f32>(0.0));
343 var raw_a = clamp(max(1.0 - k, max(rgb.r, max(rgb.g, rgb.b))), 0.0, 1.0);
344 if (u.p[0].w > 1e-4) {
345 raw_a = clamp(pow(raw_a, 1.0 + u.p[0].w * 4.0), 0.0, 1.0);
346 }
347 alpha = pow(clamp((raw_a - lo_th) / (hi_th - lo_th), 0.0, 1.0), 1.0 / gamma);
348
349 if (inv_matte) { alpha = 1.0 - alpha; }
350 alpha = alpha * src.a;
351 if (uk_in_range(src)) { alpha = 0.0; }
352 if (alpha <= 1e-5) {
353 rgb = vec3<f32>(0.0);
354 }
355
356 if (spill_sup > 1e-4) {
357 if (bg.g >= bg.r && bg.g >= bg.b) {
358 let lim = max(rgb.r, rgb.b);
359 if (rgb.g > lim) { rgb.g = mix(rgb.g, lim, spill_sup); }
360 } else if (bg.b >= bg.r && bg.b >= bg.g) {
361 let lim = max(rgb.r, rgb.g);
362 if (rgb.b > lim) { rgb.b = mix(rgb.b, lim, spill_sup); }
363 } else {
364 let lim = max(rgb.g, rgb.b);
365 if (rgb.r > lim) { rgb.r = mix(rgb.r, lim, spill_sup); }
366 }
367 }
368 if (post_mult) {
369 rgb = rgb * alpha;
370 }
371 }
372
373 let keyed = vec4<f32>(rgb, alpha);
374 let has_mask = u.p[2].y > 0.5;
375 let mbm = has_mask && u.p[5].z > 0.5;
376 let raw_mask = read_mask(gid.xy, dims, u.p[2].y, u.p[2].z);
377 let m = clamp(raw_mask * blend, 0.0, 1.0);
378 var out_col = select(mix(src, keyed, m), keyed * m, mbm);
379 let unproc = select(src, vec4<f32>(0.0), mbm);
380 out_col = select(out_col, unproc, u.p[6] > vec4<f32>(0.5));
381 store_dst(vec2<i32>(gid.xy), out_col);
382}
383// ============================================================================
384// 30. Plasma (Fusion 4-circle concentric/sine plasma interference generator)
385// p[0..3] = (cx, cy, c_scale, c_type) for Circles 1..4 (type 0=Off, 1=Linear, >=2=Sine)
386// p[4] = (scale, phase, red_phase, green_phase)
387// p[5] = (blue_phase, blend, has_mask, mask_invert)
388// p[6] = (alpha_phase, operation: 0=Add 1=Subtract 2=Multiply 3=Divide 4=Maximum 5=Minimum 6=Difference, 0, 0)
389// ============================================================================
390fn plasma_circle_term(u_pos: vec2<f32>, aspect_inv: f32, c: vec4<f32>, master_scale: f32, wrap: bool) -> f32 {
391 let ctype = i32(c.w + 0.5);
392 if (ctype <= 0) { return 0.0; }
393 let delta = vec2<f32>(u_pos.x - c.x, (u_pos.y - c.y) * aspect_inv);
394 let d = length(delta);
395 let s = max(master_scale * c.z, 1e-5);
396 if (ctype == 1) {
397 let lin = 39.26990817 * d / s;
398 // Smooth the Type-1 cone tip at d = 0 with a tiny radial offset matching Fusion,
399 // preventing 1/t spikes in Divide mode without visibly shifting linear fringes.
400 let lin_r = sqrt(lin * lin + 0.001916 * 0.001916) - 0.001916;
401 return select(lin_r, lin_r - 6.2831853 * floor(lin_r / 6.2831853), wrap);
402 }
403 let r0 = 0.0125 * s;
404 let dr = sqrt(d * d + r0 * r0) - r0;
405 return 2.2089323 * (1.0 + sin(33.6842105 * dr / s));
406}
407
408@compute @workgroup_size(8, 8)
409fn plasma_op(@builtin(global_invocation_id) gid: vec3<u32>) {
410 let dims = textureDimensions(dst_tex);
411 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
412 let fdims = vec2<f32>(dims);
413 // Fusion Plasma maps center (0.5, 0.5) to pixel (W/2 - 1, H/2) => u=(x+1)/W, v=(H-y)/H
414 let u_pos = vec2<f32>((f32(gid.x) + 1.0) / fdims.x, (fdims.y - f32(gid.y)) / fdims.y);
415 let aspect_inv = fdims.y / max(fdims.x, 1.0);
416 let master_scale = max(u.p[4].x, 1e-5);
417 let tau = 6.2831853;
418 let ph = u.p[4].y;
419
420 // Fusion folds the four circle terms with `Operation`. Maximum and Minimum see Type 1 as a sawtooth
421 // in [0, 2pi); Add, Subtract, Multiply, Divide and Difference see the raw linear ramp.
422 let op = i32(u.p[6].y + 0.5);
423 let wrap = op == 4 || op == 5;
424 var t = array<f32, 4>(0.0, 0.0, 0.0, 0.0);
425 var active_count = 0u;
426 for (var i = 0u; i < 4u; i = i + 1u) {
427 t[i] = plasma_circle_term(u_pos, aspect_inv, u.p[i], master_scale, wrap);
428 if (i32(u.p[i].w + 0.5) > 0) {
429 active_count = active_count + 1u;
430 }
431 }
432 var th = 0.0;
433 switch op {
434 case 1: { th = -(t[0] + t[1] + t[2] + t[3]); }
435 case 2: {
436 // Fusion multiplies all 4 circles normalized by (tau / 4) and scales back by tau.
437 let sc = tau * 0.25;
438 th = tau * (t[0] / sc) * (t[1] / sc) * (t[2] / sc) * (t[3] / sc);
439 }
440 case 3: {
441 // Divide starts at 1.0 and divides by each active circle normalized by (tau / N).
442 if (active_count > 0u) {
443 let sc = tau / f32(active_count);
444 var acc = 1.0;
445 var bad = false;
446 for (var i = 0u; i < 4u; i = i + 1u) {
447 if (i32(u.p[i].w + 0.5) <= 0) { continue; }
448 if (abs(t[i]) <= 1e-6) {
449 bad = true;
450 break;
451 }
452 acc = acc / (t[i] / sc);
453 }
454 let res = tau * acc;
455 th = select(res, -tau * ph, bad || !(abs(res) < 205887.4));
456 }
457 }
458 case 4: { th = max(max(t[0], t[1]), max(t[2], t[3])); }
459 case 5: { th = min(min(t[0], t[1]), min(t[2], t[3])); }
460 case 6: { th = abs(abs(abs(t[0] - t[1]) - t[2]) - t[3]); }
461 default: { th = t[0] + t[1] + t[2] + t[3]; }
462 }
463
464 if (!(abs(th) < 1e6)) { th = -tau * ph; }
465 let r = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].z));
466 let g = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].w));
467 let b = 0.5 + 0.5 * cos(th + tau * (ph + u.p[5].x));
468 let a = 0.5 + 0.5 * cos(th + tau * (ph + u.p[6].x));
469
470 let m = read_mask(gid.xy, dims, u.p[5].z, u.p[5].w) * u.p[5].y;
471 let out_col = vec4<f32>(r, g, b, a) * clamp(m, 0.0, 1.0);
472 store_dst(vec2<i32>(gid.xy), out_col);
473}