1// Wave 35C compute shaders: CoordSpace, LensDistort, TriangleMask, and Trails.
2// WebGPU-safe (`wasm32` compatible): write-only storage texture (patched at pipeline build time
3// for `rgba16float`/`rgba32float`), no `read_write` storage textures, 256-byte uniform block.
4
5@group(0) @binding(0) var src_a: texture_2d<f32>;
6@group(0) @binding(1) var src_b: texture_2d<f32>;
7@group(0) @binding(2) var mask_tex: texture_2d<f32>;
8@group(0) @binding(3) var samp_lin: sampler;
9@group(0) @binding(4) var dst_tex: texture_storage_2d<rgba8unorm, write>;
10
11const HDR_FLOAT: bool = false;
12const PI: f32 = 3.141592653589793;
13const TAU: f32 = 6.283185307179586;
14const SQRT2: f32 = 1.4142135623730951;
15const INV_SQRT2: f32 = 0.7071067811865476;
16
17struct Params {
18 p: array<vec4<f32>, 16>,
19};
20@group(0) @binding(5) var<uniform> u: Params;
21
22fn 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
30fn pix_to_fu(gid: vec2<u32>, dims: vec2<u32>) -> vec2<f32> {
31 let f = vec2<f32>(dims);
32 return vec2<f32>((f32(gid.x) + 0.5) / f.x, 1.0 - (f32(gid.y) + 0.5) / f.y);
33}
34
35fn fu_to_uv(fu: vec2<f32>) -> vec2<f32> {
36 return vec2<f32>(fu.x, 1.0 - fu.y);
37}
38
39fn read_mask(gid: vec2<u32>, dims: vec2<u32>, has_mask: f32, invert: f32) -> f32 {
40 if (has_mask < 0.5) {
41 return 1.0;
42 }
43 let uv = (vec2<f32>(gid) + 0.5) / vec2<f32>(dims);
44 var m = 0.0;
45 if (uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0) {
46 m = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a;
47 }
48 return select(m, 1.0 - m, invert > 0.5);
49}
50
51fn sample_edges(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32) -> vec4<f32> {
52 var uv = fu_to_uv(fu);
53 if (edges == 0) {
54 if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {
55 return vec4<f32>(0.0);
56 }
57 return textureSampleLevel(tex, samp_lin, uv, 0.0);
58 } else if (edges == 1) {
59 uv = fract(uv);
60 return textureSampleLevel(tex, samp_lin, uv, 0.0);
61 } else if (edges == 2) {
62 uv = clamp(uv, vec2<f32>(0.0), vec2<f32>(1.0));
63 return textureSampleLevel(tex, samp_lin, uv, 0.0);
64 } else {
65 let m = abs(fract(uv * 0.5 + 0.5) * 2.0 - 1.0);
66 return textureSampleLevel(tex, samp_lin, m, 0.0);
67 }
68}
69
70// Bilinear sampling with seamless horizontal wrap (u) and clamped vertical (v) for Polar->Rect.
71fn sample_wrap_u_clamp_v(tex: texture_2d<f32>, fu: vec2<f32>) -> vec4<f32> {
72 if (fu.y < 0.0 || fu.y > 1.0) {
73 return vec4<f32>(0.0);
74 }
75 let dims = textureDimensions(tex);
76 let w = i32(dims.x);
77 let h = i32(dims.y);
78 let fx = fract(fu.x) * f32(w) - 0.5;
79 let fy = clamp((1.0 - fu.y) * f32(h) - 0.5, 0.0, max(f32(h - 1), 0.0));
80 let x0_raw = i32(floor(fx));
81 let y0 = clamp(i32(floor(fy)), 0, h - 1);
82 let x0 = ((x0_raw % w) + w) % w;
83 let x1 = (x0 + 1) % w;
84 let y1 = min(y0 + 1, h - 1);
85 let tx = fx - floor(fx);
86 let ty = fy - floor(fy);
87 let c00 = textureLoad(tex, vec2<i32>(x0, y0), 0);
88 let c10 = textureLoad(tex, vec2<i32>(x1, y0), 0);
89 let c01 = textureLoad(tex, vec2<i32>(x0, y1), 0);
90 let c11 = textureLoad(tex, vec2<i32>(x1, y1), 0);
91 return mix(mix(c00, c10, tx), mix(c01, c11, tx), ty);
92}
93
94fn apply_channel_blend(orig: vec4<f32>, effect: vec4<f32>, eff: f32, mask_v: f32, mbm: f32, skip: vec4<f32>) -> vec4<f32> {
95 var out = mix(orig, effect, eff);
96 if (mbm > 0.5) {
97 out = out * mask_v;
98 }
99 return vec4<f32>(
100 select(out.r, orig.r, skip.x > 0.5),
101 select(out.g, orig.g, skip.y > 0.5),
102 select(out.b, orig.b, skip.z > 0.5),
103 select(out.a, orig.a, skip.w > 0.5),
104 );
105}
106
107// ============================================================================
108// 1. CoordSpace (Shape=0: Rectangular to Polar, Shape=1: Polar to Rectangular)
109// p[0] = (shape, blend, has_mask, mask_invert)
110// p[1] = (mbm, 0, 0, 0)
111// p[2] = process_skip (r, g, b, a)
112// ============================================================================
113@compute @workgroup_size(8, 8)
114fn coord_space_op(@builtin(global_invocation_id) gid: vec3<u32>) {
115 let dims = textureDimensions(dst_tex);
116 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
117 let p = vec2<i32>(gid.xy);
118 let fu = pix_to_fu(gid.xy, dims);
119 let shape = i32(u.p[0].x + 0.5);
120 let blend = clamp(u.p[0].y, 0.0, 1.0);
121 let has_mask = u.p[0].z;
122 let mask_inv = u.p[0].w;
123 let mbm = u.p[1].x;
124 let skip = u.p[2];
125
126 var warped: vec4<f32>;
127 if (shape == 0) {
128 // Rectangular to Polar: output (x=angle, y=radius) samples source cartesian disk.
129 let theta = fu.x * TAU - PI;
130 let r = fu.y * INV_SQRT2;
131 let src_fu = vec2<f32>(0.5 + r * cos(theta), 0.5 + r * sin(theta));
132 warped = sample_edges(src_a, src_fu, 0);
133 } else {
134 // Polar to Rectangular: output cartesian (x, y) samples source polar (angle, radius).
135 let d = fu - vec2<f32>(0.5, 0.5);
136 let r = length(d) * SQRT2;
137 let theta = select(0.0, atan2(d.y, d.x), dot(d, d) > 1e-12);
138 let src_u = (theta + PI) / TAU;
139 warped = sample_wrap_u_clamp_v(src_a, vec2<f32>(src_u, r));
140 }
141
142 let orig = textureLoad(src_a, p, 0);
143 let m = read_mask(gid.xy, dims, has_mask, mask_inv);
144 let out = apply_channel_blend(orig, warped, blend * m, m, mbm, skip);
145 store_dst(p, out);
146}
147
148// ============================================================================
149// 2. LensDistort (3DE Classic LD Model / Brown-Conrady / FusionRadial / FusionRadialDiv)
150// p[0] = (mode: 0=Distort 1=Undistort, k1, k2, anamorphic_squeeze)
151// p[1] = (curvature_x, curvature_y, p1_tangential, p2_tangential)
152// p[2] = (center_x, center_y, pixel_aspect, scale)
153// p[3] = (edges, is_division_model, output_distortion_map, blend)
154// p[4] = (has_mask, mask_invert, mbm, 0)
155// p[5] = process_skip (r, g, b, a)
156// ============================================================================
157fn eval_lens_distort_fwd(
158 p: vec2<f32>,
159 k1: f32,
160 k2: f32,
161 anam: f32,
162 cx: f32,
163 cy: f32,
164 p1: f32,
165 p2: f32,
166 is_div: bool,
167) -> vec2<f32> {
168 let x = p.x;
169 let y = p.y;
170 let x2 = x * x;
171 let y2 = y * y;
172 let r2 = x2 + y2;
173 let r4 = r2 * r2;
174 let inv_sq = 1.0 / max(anam, 0.05);
175 let dx_t = 2.0 * p1 * x * y + p2 * (r2 + 2.0 * x2);
176 let dy_t = p1 * (r2 + 2.0 * y2) + 2.0 * p2 * x * y;
177 if (is_div) {
178 let denom = max(1.0 + k1 * r2 + k2 * r4, 0.1);
179 return vec2<f32>(x / denom + dx_t, y / denom + dy_t);
180 }
181 let cxx = k1 * inv_sq;
182 let cxy = (k1 + cx) * inv_sq;
183 let cyx = k1 + cy;
184 let cyy = k1;
185 let sx = 1.0 + cxx * x2 + cxy * y2 + (k2 * inv_sq) * r4;
186 let sy = 1.0 + cyx * x2 + cyy * y2 + k2 * r4;
187 return vec2<f32>(x * sx + dx_t, y * sy + dy_t);
188}
189
190fn eval_lens_distort_inv(
191 dst_pt: vec2<f32>,
192 k1: f32,
193 k2: f32,
194 anam: f32,
195 cx: f32,
196 cy: f32,
197 p1: f32,
198 p2: f32,
199 is_div: bool,
200) -> vec2<f32> {
201 var cur = dst_pt;
202 let eps = 1e-4;
203 for (var iter = 0; iter < 10; iter = iter + 1) {
204 let f = eval_lens_distort_fwd(cur, k1, k2, anam, cx, cy, p1, p2, is_div) - dst_pt;
205 if (dot(f, f) < 1e-12) {
206 break;
207 }
208 let fx = (eval_lens_distort_fwd(cur + vec2<f32>(eps, 0.0), k1, k2, anam, cx, cy, p1, p2, is_div) - dst_pt - f) / eps;
209 let fy = (eval_lens_distort_fwd(cur + vec2<f32>(0.0, eps), k1, k2, anam, cx, cy, p1, p2, is_div) - dst_pt - f) / eps;
210 let det = fx.x * fy.y - fy.x * fx.y;
211 if (abs(det) < 1e-8) {
212 break;
213 }
214 let step = vec2<f32>(
215 (fy.y * f.x - fy.x * f.y) / det,
216 (-fx.y * f.x + fx.x * f.y) / det,
217 );
218 // Clamp Newton step for stability under extreme barrel/pincushion coefficients.
219 let step_len = length(step);
220 let damped = select(step, step * (0.5 / step_len), step_len > 0.5);
221 cur = cur - damped;
222 }
223 return cur;
224}
225
226@compute @workgroup_size(8, 8)
227fn lens_distort_op(@builtin(global_invocation_id) gid: vec3<u32>) {
228 let dims = textureDimensions(dst_tex);
229 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
230 let p = vec2<i32>(gid.xy);
231 let fu = pix_to_fu(gid.xy, dims);
232
233 let mode = i32(u.p[0].x + 0.5); // 0 = Distort, 1 = Undistort
234 let k1 = u.p[0].y;
235 let k2 = u.p[0].z;
236 let anam = max(u.p[0].w, 0.05);
237 let curv_x = u.p[1].x;
238 let curv_y = u.p[1].y;
239 let p1 = u.p[1].z;
240 let p2 = u.p[1].w;
241 let center = u.p[2].xy;
242 let pixel_aspect = max(u.p[2].z, 1e-4);
243 let scale = max(u.p[2].w, 1e-4);
244 let edges = i32(u.p[3].x + 0.5);
245 let is_div = u.p[3].y > 0.5;
246 let out_map = u.p[3].z > 0.5;
247 let blend = clamp(u.p[3].w, 0.0, 1.0);
248 let has_mask = u.p[4].x;
249 let mask_inv = u.p[4].y;
250 let mbm = u.p[4].z;
251 let skip = u.p[5];
252
253 let aspect_x = (f32(dims.x) / max(f32(dims.y), 1.0)) * pixel_aspect;
254 let norm_p = (fu - center) * vec2<f32>(aspect_x, 1.0) * (2.0 / scale);
255
256 let has_coeff = (abs(k1) + abs(k2) + abs(curv_x) + abs(curv_y) + abs(p1) + abs(p2)) > 1e-7;
257 var src_p = norm_p;
258 if (has_coeff) {
259 if (mode == 1) {
260 // Undistort: output is undistorted, sample distorted source directly.
261 src_p = eval_lens_distort_fwd(norm_p, k1, k2, anam, curv_x, curv_y, p1, p2, is_div);
262 } else {
263 // Distort: output is distorted, invert forward model to sample undistorted source.
264 src_p = eval_lens_distort_inv(norm_p, k1, k2, anam, curv_x, curv_y, p1, p2, is_div);
265 }
266 }
267
268 let src_fu = center + src_p * vec2<f32>(0.5 / aspect_x, 0.5);
269 var warped: vec4<f32>;
270 if (out_map) {
271 warped = vec4<f32>(src_fu.x, src_fu.y, 0.0, 1.0);
272 } else {
273 warped = sample_edges(src_a, src_fu, edges);
274 }
275
276 let orig = textureLoad(src_a, p, 0);
277 let m = read_mask(gid.xy, dims, has_mask, mask_inv);
278 let out = apply_channel_blend(orig, warped, blend * m, m, mbm, skip);
279 store_dst(p, out);
280}
281
282// ============================================================================
283// 3. TriangleMask (exact 3-point triangle SDF in isotropic frame-width units)
284// p[0] = (p1.x, p1.y, p2.x, p2.y)
285// p[1] = (p3.x, p3.y, soft_edge, border_width)
286// p[2] = (solid, invert, level, has_prev_mask)
287// p[3] = (paint_mode, 0, 0, 0)
288// ============================================================================
289fn combine_mask_val(prev: f32, cur: f32, mode: i32) -> f32 {
290 if (mode == 1) { return clamp(prev + cur, 0.0, 1.0); }
291 if (mode == 2) { return clamp(prev - cur, 0.0, 1.0); }
292 if (mode == 3) { return prev * cur; }
293 if (mode == 4) { return min(prev, cur); }
294 if (mode == 5) { return max(prev, cur); }
295 if (mode == 6) { return mix(prev, 1.0 - prev, cur); }
296 if (mode == 7) { return select(prev, cur, cur > 0.002); }
297 if (mode == 8) { return 0.5 * (prev + cur); }
298 return clamp(prev + cur * (1.0 - prev), 0.0, 1.0);
299}
300
301fn sd_triangle_2d(p: vec2<f32>, a: vec2<f32>, b: vec2<f32>, c: vec2<f32>) -> f32 {
302 let e0 = b - a;
303 let e1 = c - b;
304 let e2 = a - c;
305 let v0 = p - a;
306 let v1 = p - b;
307 let v2 = p - c;
308 let pq0 = v0 - e0 * clamp(dot(v0, e0) / max(dot(e0, e0), 1e-12), 0.0, 1.0);
309 let pq1 = v1 - e1 * clamp(dot(v1, e1) / max(dot(e1, e1), 1e-12), 0.0, 1.0);
310 let pq2 = v2 - e2 * clamp(dot(v2, e2) / max(dot(e2, e2), 1e-12), 0.0, 1.0);
311 let area = e0.x * e2.y - e0.y * e2.x;
312 if (abs(area) < 1e-10) {
313 return sqrt(min(dot(pq0, pq0), min(dot(pq1, pq1), dot(pq2, pq2))));
314 }
315 let s = sign(area);
316 let d0 = vec2<f32>(dot(pq0, pq0), s * (v0.x * e0.y - v0.y * e0.x));
317 let d1 = vec2<f32>(dot(pq1, pq1), s * (v1.x * e1.y - v1.y * e1.x));
318 let d2 = vec2<f32>(dot(pq2, pq2), s * (v2.x * e2.y - v2.y * e2.x));
319 let d = min(min(d0, d1), d2);
320 return -sqrt(d.x) * sign(d.y);
321}
322
323@compute @workgroup_size(8, 8)
324fn triangle_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) {
325 let dims = textureDimensions(dst_tex);
326 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
327 let p = vec2<i32>(gid.xy);
328 let level = u.p[2].z;
329 let has_prev = u.p[2].w > 0.5;
330 let paint_mode = i32(u.p[3].x + 0.5);
331 let inv_dims = vec2<f32>(1.0) / vec2<f32>(dims);
332
333 if (level <= 0.0) {
334 var a = 0.0;
335 if (has_prev) {
336 let uv = (vec2<f32>(gid.xy) + 0.5) * inv_dims;
337 let prev = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a;
338 a = combine_mask_val(prev, 0.0, paint_mode);
339 }
340 store_dst(p, vec4<f32>(a, a, a, a));
341 return;
342 }
343
344 let inv_aspect = max(f32(dims.y), 1.0) * inv_dims.x;
345 let scale_iso = vec2<f32>(1.0, inv_aspect);
346 let fu = pix_to_fu(gid.xy, dims);
347
348 let pt1 = u.p[0].xy * scale_iso;
349 let pt2 = u.p[0].zw * scale_iso;
350 let pt3 = u.p[1].xy * scale_iso;
351 let soft = max(u.p[1].z, 0.5 * inv_dims.x);
352 let border_w = u.p[1].w;
353 let solid = u.p[2].x > 0.5;
354 let invert = u.p[2].y > 0.5;
355
356 var dist = sd_triangle_2d(fu * scale_iso, pt1, pt2, pt3);
357 if (solid) {
358 if (abs(border_w) > 1e-7) {
359 dist = dist - border_w * 0.5;
360 }
361 } else {
362 dist = abs(dist) - abs(border_w) * 0.5;
363 }
364
365 var a = 1.0 - smoothstep(-soft, soft, dist);
366 if (invert) {
367 a = 1.0 - a;
368 }
369 a = clamp(a * level, 0.0, 1.0);
370 if (has_prev) {
371 let uv = (vec2<f32>(gid.xy) + 0.5) * inv_dims;
372 let prev = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a;
373 a = combine_mask_val(prev, a, paint_mode);
374 }
375 store_dst(p, vec4<f32>(a, a, a, a));
376}
377
378// ============================================================================
379// 4. Trails per-step transform + RGBA gain
380// p[0] = (offset_x, offset_y, scale_x, scale_y)
381// p[1] = (angle_rad, pivot_x, pivot_y, 0)
382// p[2] = (gain_r, gain_g, gain_b, gain_a)
383// ============================================================================
384@compute @workgroup_size(8, 8)
385fn trails_step_op(@builtin(global_invocation_id) gid: vec3<u32>) {
386 let dims = textureDimensions(dst_tex);
387 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
388 let p = vec2<i32>(gid.xy);
389 let fu = pix_to_fu(gid.xy, dims);
390
391 let offset = u.p[0].xy;
392 let sx = select(u.p[0].z, 1e-4, abs(u.p[0].z) < 1e-4);
393 let sy = select(u.p[0].w, 1e-4, abs(u.p[0].w) < 1e-4);
394 let angle = u.p[1].x;
395 let pivot = u.p[1].yz;
396 let gain = u.p[2];
397
398 let aspect = f32(dims.x) / max(f32(dims.y), 1.0);
399 let d = fu - pivot - offset;
400 let d_iso = vec2<f32>(d.x * aspect, d.y);
401 let c = cos(angle);
402 let s = sin(angle);
403 let r_iso = vec2<f32>(d_iso.x * c + d_iso.y * s, -d_iso.x * s + d_iso.y * c);
404 let r = vec2<f32>(r_iso.x / aspect, r_iso.y);
405 let src_fu = pivot + vec2<f32>(r.x / sx, r.y / sy);
406
407 let sampled = sample_edges(src_a, src_fu, 0);
408 store_dst(p, sampled * gain);
409}
410
411// ============================================================================
412// 5. Trails final blend + EffectMask + ProcessRed/Green/Blue/Alpha
413// src_a = original current frame, src_b = accumulated trails result
414// p[0] = (blend, has_mask, mask_invert, mbm)
415// p[1] = process_skip (r, g, b, a)
416// ============================================================================
417@compute @workgroup_size(8, 8)
418fn trails_blend_op(@builtin(global_invocation_id) gid: vec3<u32>) {
419 let dims = textureDimensions(dst_tex);
420 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
421 let p = vec2<i32>(gid.xy);
422 let orig = textureLoad(src_a, p, 0);
423 let eff_col = textureLoad(src_b, p, 0);
424 let blend = clamp(u.p[0].x, 0.0, 1.0);
425 let m = read_mask(gid.xy, dims, u.p[0].y, u.p[0].z);
426 let out = apply_channel_blend(orig, eff_col, blend * m, m, u.p[0].w, u.p[1]);
427 store_dst(p, out);
428}