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, mbm: f32, skip: vec4<f32>) -> vec4<f32> {
95 let out_c = select(mix(orig, effect, eff), effect * eff, mbm > 0.5);
96 return select(out_c, select(orig, vec4<f32>(0.0), mbm > 0.5), skip > vec4<f32>(0.5));
97}
98
99// ============================================================================
100// 1. CoordSpace (Shape=0: Rectangular to Polar, Shape=1: Polar to Rectangular)
101// p[0] = (shape, blend, has_mask, mask_invert)
102// p[1] = (mbm, 0, 0, 0)
103// p[2] = process_skip (r, g, b, a)
104// ============================================================================
105@compute @workgroup_size(8, 8)
106fn coord_space_op(@builtin(global_invocation_id) gid: vec3<u32>) {
107 let dims = textureDimensions(dst_tex);
108 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
109 let p = vec2<i32>(gid.xy);
110 let fu = pix_to_fu(gid.xy, dims);
111 let shape = i32(u.p[0].x + 0.5);
112 let blend = clamp(u.p[0].y, 0.0, 1.0);
113 let has_mask = u.p[0].z;
114 let mask_inv = u.p[0].w;
115 let mbm = u.p[1].x;
116 let skip = u.p[2];
117
118 var warped: vec4<f32>;
119 if (shape == 0) {
120 // Rectangular to Polar: output (x=angle, y=radius) samples source cartesian disk.
121 let theta = fu.x * TAU - PI;
122 let r = fu.y * INV_SQRT2;
123 let src_fu = vec2<f32>(0.5 + r * cos(theta), 0.5 + r * sin(theta));
124 warped = sample_edges(src_a, src_fu, 0);
125 } else {
126 // Polar to Rectangular: output cartesian (x, y) samples source polar (angle, radius).
127 let d = fu - vec2<f32>(0.5, 0.5);
128 let r = length(d) * SQRT2;
129 let theta = select(0.0, atan2(d.y, d.x), dot(d, d) > 1e-12);
130 let src_u = (theta + PI) / TAU;
131 warped = sample_wrap_u_clamp_v(src_a, vec2<f32>(src_u, r));
132 }
133
134 let orig = textureLoad(src_a, p, 0);
135 let m = read_mask(gid.xy, dims, has_mask, mask_inv);
136 let out = apply_channel_blend(orig, warped, blend * m, mbm, skip);
137 store_dst(p, out);
138}
139
140// ============================================================================
141// 2. LensDistort (3DE Classic LD Model / Brown-Conrady / FusionRadial / FusionRadialDiv)
142// p[0] = (mode: 0=Distort 1=Undistort, k1, k2, anamorphic_squeeze)
143// p[1] = (curvature_x, curvature_y, p1_tangential, p2_tangential)
144// p[2] = (center_x, center_y, pixel_aspect, scale)
145// p[3] = (edges, is_division_model, output_distortion_map, blend)
146// p[4] = (has_mask, mask_invert, mbm, 0)
147// p[5] = process_skip (r, g, b, a)
148// ============================================================================
149fn eval_lens_distort_fwd(
150 p: vec2<f32>,
151 k1: f32,
152 k2: f32,
153 anam: f32,
154 cx: f32,
155 cy: f32,
156 p1: f32,
157 p2: f32,
158 is_div: bool,
159) -> vec2<f32> {
160 let x = p.x;
161 let y = p.y;
162 let x2 = x * x;
163 let y2 = y * y;
164 let r2 = x2 + y2;
165 let r4 = r2 * r2;
166 let inv_sq = 1.0 / max(anam, 0.05);
167 let dx_t = 2.0 * p1 * x * y + p2 * (r2 + 2.0 * x2);
168 let dy_t = p1 * (r2 + 2.0 * y2) + 2.0 * p2 * x * y;
169 if (is_div) {
170 let denom = max(1.0 + k1 * r2 + k2 * r4, 0.1);
171 return vec2<f32>(x / denom + dx_t, y / denom + dy_t);
172 }
173 let cxx = k1 * inv_sq;
174 let cxy = (k1 + cx) * inv_sq;
175 let cyx = k1 + cy;
176 let cyy = k1;
177 let sx = 1.0 + cxx * x2 + cxy * y2 + (k2 * inv_sq) * r4;
178 let sy = 1.0 + cyx * x2 + cyy * y2 + k2 * r4;
179 return vec2<f32>(x * sx + dx_t, y * sy + dy_t);
180}
181
182fn eval_lens_distort_inv(
183 dst_pt: vec2<f32>,
184 k1: f32,
185 k2: f32,
186 anam: f32,
187 cx: f32,
188 cy: f32,
189 p1: f32,
190 p2: f32,
191 is_div: bool,
192) -> vec2<f32> {
193 var cur = dst_pt;
194 let eps = 1e-4;
195 for (var iter = 0; iter < 10; iter = iter + 1) {
196 let f = eval_lens_distort_fwd(cur, k1, k2, anam, cx, cy, p1, p2, is_div) - dst_pt;
197 if (dot(f, f) < 1e-12) {
198 break;
199 }
200 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;
201 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;
202 let det = fx.x * fy.y - fy.x * fx.y;
203 if (abs(det) < 1e-8) {
204 break;
205 }
206 let step = vec2<f32>(
207 (fy.y * f.x - fy.x * f.y) / det,
208 (-fx.y * f.x + fx.x * f.y) / det,
209 );
210 // Clamp Newton step for stability under extreme barrel/pincushion coefficients.
211 let step_len = length(step);
212 let damped = select(step, step * (0.5 / step_len), step_len > 0.5);
213 cur = cur - damped;
214 }
215 return cur;
216}
217
218@compute @workgroup_size(8, 8)
219fn lens_distort_op(@builtin(global_invocation_id) gid: vec3<u32>) {
220 let dims = textureDimensions(dst_tex);
221 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
222 let p = vec2<i32>(gid.xy);
223 let fu = pix_to_fu(gid.xy, dims);
224
225 let mode = i32(u.p[0].x + 0.5); // 0 = Distort, 1 = Undistort
226 let k1 = u.p[0].y;
227 let k2 = u.p[0].z;
228 let anam = max(u.p[0].w, 0.05);
229 let curv_x = u.p[1].x;
230 let curv_y = u.p[1].y;
231 let p1 = u.p[1].z;
232 let p2 = u.p[1].w;
233 let center = u.p[2].xy;
234 let pixel_aspect = max(u.p[2].z, 1e-4);
235 let scale = max(u.p[2].w, 1e-4);
236 let edges = i32(u.p[3].x + 0.5);
237 let is_div = u.p[3].y > 0.5;
238 let out_map = u.p[3].z > 0.5;
239 let blend = clamp(u.p[3].w, 0.0, 1.0);
240 let has_mask = u.p[4].x;
241 let mask_inv = u.p[4].y;
242 let mbm = u.p[4].z;
243 let skip = u.p[5];
244
245 let aspect_x = (f32(dims.x) / max(f32(dims.y), 1.0)) * pixel_aspect;
246 let norm_p = (fu - center) * vec2<f32>(aspect_x, 1.0) * (2.0 / scale);
247
248 let has_coeff = (abs(k1) + abs(k2) + abs(curv_x) + abs(curv_y) + abs(p1) + abs(p2)) > 1e-7;
249 var src_p = norm_p;
250 if (has_coeff) {
251 if (mode == 1) {
252 // Undistort: output is undistorted, sample distorted source directly.
253 src_p = eval_lens_distort_fwd(norm_p, k1, k2, anam, curv_x, curv_y, p1, p2, is_div);
254 } else {
255 // Distort: output is distorted, invert forward model to sample undistorted source.
256 src_p = eval_lens_distort_inv(norm_p, k1, k2, anam, curv_x, curv_y, p1, p2, is_div);
257 }
258 }
259
260 let src_fu = center + src_p * vec2<f32>(0.5 / aspect_x, 0.5);
261 var warped: vec4<f32>;
262 if (out_map) {
263 warped = vec4<f32>(src_fu.x, src_fu.y, 0.0, 1.0);
264 } else {
265 warped = sample_edges(src_a, src_fu, edges);
266 }
267
268 let orig = textureLoad(src_a, p, 0);
269 let m = read_mask(gid.xy, dims, has_mask, mask_inv);
270 let out = apply_channel_blend(orig, warped, blend * m, mbm, skip);
271 store_dst(p, out);
272}
273
274// ============================================================================
275// 3. TriangleMask (triangle SDF in pixels, box-filtered edge coverage; borders round the joins)
276// p[0] = (p1.x, p1.y, p2.x, p2.y)
277// p[1] = (p3.x, p3.y, soft_edge, border_width) soft edge is a host-side blur
278// p[2] = (solid, invert, level, has_prev_mask)
279// p[3] = (paint_mode, 0, 0, 0)
280// ============================================================================
281fn combine_mask_val(prev: f32, cur: f32, mode: i32) -> f32 {
282 if (mode == 1) { return clamp(prev + cur, 0.0, 1.0); }
283 if (mode == 2) { return clamp(prev - cur, 0.0, 1.0); }
284 if (mode == 3) { return prev * cur; }
285 if (mode == 4) { return min(prev, cur); }
286 if (mode == 5) { return max(prev, cur); }
287 if (mode == 6) { return mix(prev, 1.0 - prev, cur); }
288 if (mode == 7) { return select(prev, cur, cur > 0.002); }
289 if (mode == 8) { return 0.5 * (prev + cur); }
290 return clamp(prev + cur * (1.0 - prev), 0.0, 1.0);
291}
292
293fn sd_triangle_2d(p: vec2<f32>, a: vec2<f32>, b: vec2<f32>, c: vec2<f32>) -> f32 {
294 let e0 = b - a;
295 let e1 = c - b;
296 let e2 = a - c;
297 let v0 = p - a;
298 let v1 = p - b;
299 let v2 = p - c;
300 let pq0 = v0 - e0 * clamp(dot(v0, e0) / max(dot(e0, e0), 1e-12), 0.0, 1.0);
301 let pq1 = v1 - e1 * clamp(dot(v1, e1) / max(dot(e1, e1), 1e-12), 0.0, 1.0);
302 let pq2 = v2 - e2 * clamp(dot(v2, e2) / max(dot(e2, e2), 1e-12), 0.0, 1.0);
303 let area = e0.x * e2.y - e0.y * e2.x;
304 if (abs(area) < 1e-10) {
305 return sqrt(min(dot(pq0, pq0), min(dot(pq1, pq1), dot(pq2, pq2))));
306 }
307 let s = sign(area);
308 let d0 = vec2<f32>(dot(pq0, pq0), s * (v0.x * e0.y - v0.y * e0.x));
309 let d1 = vec2<f32>(dot(pq1, pq1), s * (v1.x * e1.y - v1.y * e1.x));
310 let d2 = vec2<f32>(dot(pq2, pq2), s * (v2.x * e2.y - v2.y * e2.x));
311 let d = min(min(d0, d1), d2);
312 return -sqrt(d.x) * sign(d.y);
313}
314
315/// Area of a unit pixel lying inside a straight boundary at signed distance `d` (px, positive
316/// outside) with unit normal `n`.
317fn pixel_coverage(d: f32, n: vec2<f32>) -> f32 {
318 let a = max(abs(n.x), abs(n.y));
319 let b = max(min(abs(n.x), abs(n.y)), 1e-6);
320 let x = clamp(0.5 * (a + b) - d, 0.0, a + b);
321 if (x < b) { return x * x / (2.0 * a * b); }
322 if (x > a) { return 1.0 - (a + b - x) * (a + b - x) / (2.0 * a * b); }
323 return (x - 0.5 * b) / a;
324}
325
326@compute @workgroup_size(8, 8)
327fn triangle_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) {
328 let dims = textureDimensions(dst_tex);
329 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
330 let p = vec2<i32>(gid.xy);
331 let level = u.p[2].z;
332 let has_prev = u.p[2].w > 0.5;
333 let paint_mode = i32(u.p[3].x + 0.5);
334 let inv_dims = vec2<f32>(1.0) / vec2<f32>(dims);
335
336 var a = 0.0;
337 if (level > 0.0) {
338 let px = vec2<f32>(dims);
339 let t0 = u.p[0].xy * px;
340 let t1 = u.p[0].zw * px;
341 let t2 = u.p[1].xy * px;
342 let c = vec2<f32>(f32(gid.x) + 0.5, f32(dims.y - gid.y) - 0.5);
343 let d = sd_triangle_2d(c, t0, t1, t2);
344 let e = vec2<f32>(0.25, 0.0);
345 let n = normalize(vec2<f32>(
346 sd_triangle_2d(c + e.xy, t0, t1, t2) - sd_triangle_2d(c - e.xy, t0, t1, t2),
347 sd_triangle_2d(c + e.yx, t0, t1, t2) - sd_triangle_2d(c - e.yx, t0, t1, t2),
348 ) + vec2<f32>(1e-9, 0.0));
349 let border_w = u.p[1].w;
350 let solid = u.p[2].x > 0.5;
351 let h = select(abs(border_w), border_w, solid) * 0.5 * px.x;
352 a = pixel_coverage(d - h, n);
353 if (!solid) {
354 a = a - pixel_coverage(d + h, n);
355 }
356 if (u.p[2].y > 0.5) {
357 a = 1.0 - a;
358 }
359 a = clamp(a * level, 0.0, 1.0);
360 }
361 if (has_prev) {
362 let uv = (vec2<f32>(gid.xy) + 0.5) * inv_dims;
363 let prev = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a;
364 a = combine_mask_val(prev, a, paint_mode);
365 }
366 store_dst(p, vec4<f32>(a, a, a, a));
367}
368
369// ============================================================================
370// 4. Trails per-step transform + RGBA gain
371// p[0] = (offset_x, offset_y, scale_x, scale_y)
372// p[1] = (angle_rad, pivot_x, pivot_y, 0)
373// p[2] = (gain_r, gain_g, gain_b, gain_a)
374// ============================================================================
375@compute @workgroup_size(8, 8)
376fn trails_step_op(@builtin(global_invocation_id) gid: vec3<u32>) {
377 let dims = textureDimensions(dst_tex);
378 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
379 let p = vec2<i32>(gid.xy);
380 let fu = pix_to_fu(gid.xy, dims);
381
382 let offset = u.p[0].xy;
383 let sx = select(u.p[0].z, 1e-4, abs(u.p[0].z) < 1e-4);
384 let sy = select(u.p[0].w, 1e-4, abs(u.p[0].w) < 1e-4);
385 let angle = u.p[1].x;
386 let pivot = u.p[1].yz;
387 let gain = u.p[2];
388
389 let aspect = f32(dims.x) / max(f32(dims.y), 1.0);
390 let d = fu - pivot - offset;
391 let d_iso = vec2<f32>(d.x * aspect, d.y);
392 let c = cos(angle);
393 let s = sin(angle);
394 let r_iso = vec2<f32>(d_iso.x * c + d_iso.y * s, -d_iso.x * s + d_iso.y * c);
395 let r = vec2<f32>(r_iso.x / aspect, r_iso.y);
396 let src_fu = pivot + vec2<f32>(r.x / sx, r.y / sy);
397
398 let sampled = sample_edges(src_a, src_fu, 0);
399 store_dst(p, sampled * gain);
400}
401
402// ============================================================================
403// 5. Trails final blend + EffectMask + ProcessRed/Green/Blue/Alpha
404// src_a = original current frame, src_b = accumulated trails result
405// p[0] = (blend, has_mask, mask_invert, mbm)
406// p[1] = process_skip (r, g, b, a)
407// ============================================================================
408@compute @workgroup_size(8, 8)
409fn trails_blend_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 p = vec2<i32>(gid.xy);
413 let orig = textureLoad(src_a, p, 0);
414 let eff_col = textureLoad(src_b, p, 0);
415 let blend = clamp(u.p[0].x, 0.0, 1.0);
416 let m = read_mask(gid.xy, dims, u.p[0].y, u.p[0].z);
417 let out = apply_channel_blend(orig, eff_col, blend * m, u.p[0].w, u.p[1]);
418 store_dst(p, out);
419}