Merge main into M13.11 (bokeh kernel_spans/blur_levels, Essential Graphics as_type controls with mirrors and links)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Brandon Thomas
2026-10-03 09:54:37 -04:00
co-authored by Claude Opus 5.5
146 changed files with 19761 additions and 676 deletions
+867
View File
@@ -0,0 +1,867 @@
// GPU effects (keying, matte and channel family): see src/fx_key.rs. Each entry point mirrors
// the CPU effect in effectcraft-effects operation for operation. Every name here is prefixed
// `fxk_` (the family files share one module).
// util::unpremul: (straight colour, alpha); colour 0 when alpha ≤ 1e-6.
fn fxk_straight(px: vec4<f32>) -> vec3<f32> {
if (px.w > 1e-6) {
return px.xyz / px.w;
}
return vec3<f32>(0.0);
}
fn fxk_premul(c: vec3<f32>, a: f32) -> vec4<f32> {
return vec4<f32>(c * a, a);
}
// Pixel `p` of the third image, copied into `data` as RGBA f32 rows of P.u[3].x pixels.
fn fxk_rows(p: vec2<i32>) -> vec4<f32> {
let i = (u32(p.y) * P.u[3].x + u32(p.x)) * 4u;
return vec4<f32>(data[i], data[i + 1u], data[i + 2u], data[i + 3u]);
}
// keying::to_lab.
fn fxk_cbrt(t: f32) -> f32 {
return sign(t) * pow(abs(t), 1.0 / 3.0);
}
fn fxk_lab_f(t: f32) -> f32 {
if (t > 0.008856) {
return fxk_cbrt(t);
}
return 7.787 * t + 16.0 / 116.0;
}
// keying::range_components.
fn fxk_range_components(space: u32, c: vec3<f32>) -> vec3<f32> {
if (space == 1u) {
let y = 0.299 * c.x + 0.587 * c.y + 0.114 * c.z;
let u = -0.14713 * c.x - 0.28886 * c.y + 0.436 * c.z;
let v = 0.615 * c.x - 0.51499 * c.y - 0.10001 * c.z;
return vec3<f32>(y * 255.0, u * 255.0 + 128.0, v * 255.0 + 128.0);
}
if (space == 2u) {
return c * 255.0;
}
let cl = clamp(c, vec3<f32>(0.0), vec3<f32>(1.0));
let l = vec3<f32>(srgb_to_linear(cl.x), srgb_to_linear(cl.y), srgb_to_linear(cl.z));
let x = (0.4124 * l.x + 0.3576 * l.y + 0.1805 * l.z) / 0.95047;
let yy = 0.2126 * l.x + 0.7152 * l.y + 0.0722 * l.z;
let z = (0.0193 * l.x + 0.1192 * l.y + 0.9505 * l.z) / 1.08883;
let fx = fxk_lab_f(x);
let fy = fxk_lab_f(yy);
let fz = fxk_lab_f(z);
return vec3<f32>((116.0 * fy - 16.0) * 2.55, 500.0 * (fx - fy) + 128.0, 200.0 * (fy - fz) + 128.0);
}
// keying::matte_levels.
fn fxk_matte_levels(v: f32, in_b: f32, in_w: f32, gamma: f32, out_b: f32, out_w: f32) -> f32 {
var t = clamp((v - in_b / 255.0) / max((in_w - in_b) / 255.0, 1e-4), 0.0, 1.0);
t = powz(t, 1.0 / max(gamma, 0.01));
return clamp(out_b / 255.0 + t * (out_w - out_b) / 255.0, 0.0, 1.0);
}
// keying::secondary.
fn fxk_secondary(c: vec3<f32>, o1: u32, o2: u32, bal: f32) -> f32 {
var hi = c[o2];
var lo = c[o1];
if (c[o1] > c[o2]) {
hi = c[o1];
lo = c[o2];
}
return bal * hi + (1.0 - bal) * lo;
}
// channel::arithmetic's 8-bit quantisation.
fn fxk_q(v: f32) -> u32 {
return u32(round_away(clamp(v, 0.0, 1.0) * 255.0));
}
// channel2::extract.
fn fxk_extract(px: vec4<f32>, ch: u32, inv: bool) -> vec4<f32> {
let c = fxk_straight(px);
let a = max(px.w, 0.0);
var g: f32;
switch ch {
case 0u: {
if (inv) {
return fxk_premul(1.0 - c, a);
}
return px;
}
case 1u: { g = luminance(c) * a; }
case 2u: { g = c.x * a; }
case 3u: { g = c.y * a; }
case 4u: { g = c.z * a; }
default: { g = a; }
}
if (inv) {
g = 1.0 - g;
}
return vec4<f32>(g, g, g, 1.0);
}
// channel2::arith.
fn fxk_arith(op: u32, a: f32, b: f32) -> f32 {
switch op {
case 0u: { return b; }
case 1u: { return a + b; }
case 2u: { return a - b; }
case 3u: { return a * b; }
case 4u: { return abs(a - b); }
case 5u: { return f32(fxk_q(a) & fxk_q(b)) / 255.0; }
case 6u: { return f32(fxk_q(a) | fxk_q(b)) / 255.0; }
case 7u: { return f32(fxk_q(a) ^ fxk_q(b)) / 255.0; }
case 8u, 11u: { return max(a, b); }
case 9u, 10u: { return min(a, b); }
case 12u: { return a + b - a * b; }
case 13u: {
if (a < 0.5) {
return 2.0 * a * b;
}
return 1.0 - 2.0 * (1.0 - a) * (1.0 - b);
}
default: {
if (b < 0.5) {
return 2.0 * a * b;
}
return 1.0 - 2.0 * (1.0 - a) * (1.0 - b);
}
}
}
// channel2::overflow.
fn fxk_overflow(op: u32, v: f32, how: u32) -> f32 {
if (how == 0u) {
return clamp(v, 0.0, 1.0);
}
if (how == 1u) {
let m = 1.0001;
let r = v - m * floor(v / m);
return min(select(r, 0.0, r >= m || r < 0.0), 1.0);
}
if (op == 1u) {
return v * 0.5;
}
if (op == 2u) {
return (v + 1.0) * 0.5;
}
return clamp(v, 0.0, 1.0);
}
// Per-pixel keying / matte / channel operations. u[0].x = op (see src/fx_key.rs for each
// effect's parameter layout):
// 1 Color Key matte 2 Luma Key matte 3 Color Range matte 4 Extract matte
// 5 apply a matte (aux.x; u[0].y = 0 final, 2 matte view, 3 Difference Matte's matte view,
// 4 Difference Matte's output)
// 6 Difference Matte matte (src and aux blurred)
// 7 Spill Suppressor 8 Advanced Spill Suppressor (aux = 1×1 excess sums when u[1].w)
// 9 Color Difference Key 10 Unmult 11 route channels (u[1] = sources)
// 12 Remove Color Matting 13 Arithmetic 14 Solid Composite 15 Set Matte
// 16 Channel Combiner 17 Blend 18 Calculations 19 Compound Arithmetic
// 20 Set Channels: channel u[0].y of the value image from aux; 21 Set Channels: premultiply
// 22 Screen Key matte 23 Screen Key output 24 alpha plane 25 Key Cleaner output
// 26 Matte Choker ramp 27 util::set_alpha (aux.x = alpha, data = fill)
// 28 matte view (alpha as opaque grey)
// Refine Soft / Hard Matte: 29 guide pack (luminance, alpha, products); 30 guided-filter
// coefficients from the box means (f[0].x = eps); 31 refined alpha (src = boxed coefficients,
// aux = pack, data = edge band); 32 edge band (src.y − aux.y > 1e-3); 33 choke / contrast /
// invert; 34 View Edge Region; 35 decontamination (aux.x = refined alpha, data = colour estimate)
@compute @workgroup_size(16, 16)
fn fxk_point(@builtin(global_invocation_id) gid: vec3<u32>) {
let dims = out_dims();
let p = vec2<i32>(gid.xy);
if (p.x >= dims.x || p.y >= dims.y) {
return;
}
let px = textureLoad(src, p, 0);
let c = fxk_straight(px);
let a = max(px.w, 0.0);
let u0 = P.u[0];
let u1 = P.u[1];
let f0 = P.f[0];
var o = px;
switch u0.x {
case 1u: {
let k = f0.xyz;
let d = max(max(abs(c.x - k.x), abs(c.y - k.y)), abs(c.z - k.z));
o = vec4<f32>(select(1.0, 0.0, d <= f0.w));
}
case 2u: {
let l = luminance(c);
let thr = f0.x;
let tol = f0.y;
var m: f32;
switch u0.y {
case 0u: {
if (tol > 0.0) {
m = clamp((thr + tol - l) / tol, 0.0, 1.0);
} else {
m = select(1.0, 0.0, l > thr);
}
}
case 2u: { m = select(1.0, 0.0, abs(l - thr) <= tol); }
case 3u: { m = select(0.0, 1.0, abs(l - thr) <= tol); }
default: {
if (tol > 0.0) {
m = clamp((l - (thr - tol)) / tol, 0.0, 1.0);
} else {
m = select(1.0, 0.0, l < thr);
}
}
}
o = vec4<f32>(m);
}
case 3u: {
let v = fxk_range_components(u0.y, c);
var out_d = 0.0;
for (var i = 0u; i < 3u; i++) {
out_d = max(max(out_d, P.f[0][i] - v[i]), v[i] - P.f[1][i]);
}
let fuzz = P.f[2].x;
var m = 1.0;
if (out_d <= 0.0) {
m = 0.0;
} else if (fuzz > 0.0) {
m = clamp(out_d / fuzz, 0.0, 1.0);
}
o = vec4<f32>(m);
}
case 4u: {
var v: f32;
switch u0.y {
case 1u: { v = c.x; }
case 2u: { v = c.y; }
case 3u: { v = c.z; }
case 4u: { v = a; }
default: { v = luminance(c); }
}
v = 255.0 * v;
let bp = f0.x;
let wp = f0.y;
let bs = f0.z;
let ws = f0.w;
var k = 1.0;
if (v < bp) {
k = select(0.0, clamp(1.0 - (bp - v) / bs, 0.0, 1.0), bs > 0.0);
} else if (v > wp) {
k = select(0.0, clamp(1.0 - (v - wp) / ws, 0.0, 1.0), ws > 0.0);
}
if (u0.z != 0u) {
k = 1.0 - k;
}
o = vec4<f32>(k);
}
case 5u: {
let m = textureLoad(aux, p, 0).x;
switch u0.y {
case 2u: {
let k = clamp(m, 0.0, 1.0);
let v = px.w * k;
o = vec4<f32>(v, v, v, 1.0);
}
case 3u: {
let v = m * px.w;
o = vec4<f32>(v, v, v, px.w);
}
case 4u: { o = px * m; }
default: { o = px * clamp(m, 0.0, 1.0); }
}
}
case 6u: {
let q = textureLoad(aux, p, 0);
let oc = fxk_straight(q);
let oa = max(q.w, 0.0);
let d = max(max(max(abs(c.x - oc.x), abs(c.y - oc.y)), abs(c.z - oc.z)), abs(a - oa));
var m: f32;
if (d <= f0.x) {
m = 0.0;
} else if (f0.y <= 1e-6) {
m = 1.0;
} else {
m = clamp((d - f0.x) / f0.y, 0.0, 1.0);
}
o = vec4<f32>(m);
}
case 7u: {
if (px.w <= 0.0) {
textureStore(out, p, px);
return;
}
var s = px.xyz / px.w;
if (u0.y == 1u) {
let g = (s.x + s.y + s.z) / 3.0;
let dir = f0.xyz;
let proj = (s.x - g) * dir.x + (s.y - g) * dir.y + (s.z - g) * dir.z;
if (proj > 0.0) {
s = s - proj * dir * f0.w;
}
} else {
let spill = max(s[u1.x] - max(s[u1.y], s[u1.z]), 0.0);
s[u1.x] = s[u1.x] - spill * f0.w;
}
o = vec4<f32>(s * px.w, px.w);
}
case 8u: {
if (px.w <= 0.0) {
textureStore(out, p, px);
return;
}
var pi = u1.x;
var o1 = u1.y;
var o2 = u1.z;
if (u1.w != 0u) {
let ex = textureLoad(aux, vec2<i32>(0, 0), 0);
if (ex.y >= ex.x && ex.y >= ex.z) {
pi = 1u;
o1 = 0u;
o2 = 2u;
} else if (ex.z >= ex.x) {
pi = 2u;
o1 = 0u;
o2 = 1u;
} else {
pi = 0u;
o1 = 1u;
o2 = 2u;
}
}
var s = px.xyz / px.w;
let amt = f0.x;
let spill_range = f0.y;
let luma = f0.z;
let tol = f0.w;
let desat = P.f[1].x;
let neutral = P.f[1].y;
let key_hue = P.f[1].z;
var w = 1.0;
if (tol < 1.0) {
let h = rgb_to_hsl(s).x;
let d0 = abs(h - key_hue);
let d = min(d0, 1.0 - d0);
w = clamp(1.0 - d / max(tol * 0.5, 1e-4), 0.0, 1.0);
}
let mx = max(s[o1], s[o2]);
let avg = 0.5 * (s[o1] + s[o2]);
let lim = mx + (avg - mx) * spill_range;
let spill = max(s[pi] - lim, 0.0) * amt * w;
s[pi] = s[pi] - spill;
let lw = vec3<f32>(0.2126, 0.7152, 0.0722);
let back = spill * lw[pi] * luma + spill * neutral / 3.0;
s = s + back;
if (desat > 0.0 && spill > 0.0) {
let l = luminance(s);
let k = min(desat * min(spill * 4.0, 1.0), 1.0);
s = s + (vec3<f32>(l) - s) * k;
}
o = vec4<f32>(s * px.w, px.w);
}
case 9u: {
var cc = c;
if (u0.z != 0u) {
let cl = clamp(cc, vec3<f32>(0.0), vec3<f32>(1.0));
cc = vec3<f32>(srgb_to_linear(cl.x), srgb_to_linear(cl.y), srgb_to_linear(cl.z));
}
let pa = P.f[1];
let pb = P.f[3];
let ua = 1.0 - clamp((cc[u1.x] - cc[u1.y]) / f0.x, 0.0, 1.0);
let ub = 1.0 - clamp((cc[u1.x] - cc[u1.z]) / f0.y, 0.0, 1.0);
let ca = fxk_matte_levels(ua, pa.x, pa.y, pa.z, pa.w, P.f[2].x);
let cb = fxk_matte_levels(ub, pb.x, pb.y, pb.z, pb.w, P.f[2].y);
let um = max(ua, ub);
let cm = fxk_matte_levels(max(ca, cb), f0.z, f0.w, P.f[2].z, 0.0, 255.0);
var view = u0.y;
if (view == 8u) {
let right = p.x * 2 >= dims.x;
let bottom = p.y * 2 >= dims.y;
if (!right && !bottom) {
view = 2u;
} else if (right && !bottom) {
view = 4u;
} else if (!right && bottom) {
view = 6u;
} else {
view = 7u;
}
}
var g = -1.0;
switch view {
case 1u: { g = ua; }
case 2u: { g = ca; }
case 3u: { g = ub; }
case 4u: { g = cb; }
case 5u: { g = um; }
case 6u: { g = cm; }
default: {}
}
if (g >= 0.0) {
let m = g * a;
o = vec4<f32>(m, m, m, 1.0);
} else {
o = px * cm;
}
}
case 10u: {
let white = u0.y != 0u;
var d: f32;
var start: f32;
if (white) {
d = 1.0 - min(min(c.x, c.y), c.z);
start = 1.0 - f0.x;
} else {
d = max(max(c.x, c.y), c.z);
start = f0.x;
}
d = clamp(d, 0.0, 1.0);
let width = f0.y * (1.0 - start);
var m: f32;
if (width > 1e-6) {
m = clamp((d - start) / width, 0.0, 1.0);
} else {
m = select(0.0, 1.0, d > start);
}
if (m <= 1e-6) {
o = vec4<f32>(0.0);
} else {
let bg = select(0.0, 1.0, white);
var oc = c;
if (u0.z != 0u) {
oc = (c - bg * (1.0 - m)) / m;
}
if (u0.w != 0u) {
oc = clamp(oc, vec3<f32>(0.0), vec3<f32>(1.0));
}
o = fxk_premul(oc, a * m);
}
}
case 11u: {
let v = vec4<f32>(dst_pick(u1.x, c, a), dst_pick(u1.y, c, a), dst_pick(u1.z, c, a), dst_pick(u1.w, c, a));
o = fxk_premul(v.xyz, clamp(v.w, 0.0, 1.0));
}
case 12u: {
if (px.w <= 1e-6) {
o = vec4<f32>(0.0);
} else {
for (var i = 0; i < 3; i++) {
let s = px[i] / px.w;
var v = max(s - f0[i] * (1.0 - px.w), 0.0);
if (u0.y != 0u) {
v = min(v, px.w);
}
o[i] = v;
}
}
}
case 13u: {
if (px.w <= 0.0) {
textureStore(out, p, px);
return;
}
let s = px.xyz / px.w;
var r = vec3<f32>(0.0);
for (var i = 0u; i < 3u; i++) {
let v = s[i];
let kv = f0[i];
let iv = fxk_q(v);
let ik = u32(round_away(kv * 255.0));
var x: f32;
switch u0.y {
case 0u: { x = f32(iv & ik) / 255.0; }
case 1u: { x = f32(iv | ik) / 255.0; }
case 2u: { x = f32(iv ^ ik) / 255.0; }
case 3u: { x = v + kv; }
case 4u: { x = v - kv; }
case 5u: { x = abs(v - kv); }
case 6u: { x = min(v, kv); }
case 7u: { x = max(v, kv); }
case 8u: { x = select(v, 0.0, v > kv); }
case 9u: { x = select(v, 0.0, v < kv); }
case 10u: { x = select(0.0, 1.0, v >= kv); }
case 11u: { x = v * kv; }
default: { x = 1.0 - (1.0 - v) * (1.0 - kv); }
}
if (u0.z != 0u) {
r[i] = clamp(x, 0.0, 1.0);
} else {
r[i] = max(x, 0.0);
}
}
o = vec4<f32>(r * px.w, px.w);
}
case 14u: {
var b = blend_pixel(u0.y, f0, px * P.f[1].x, 0.5);
b.w = clamp(b.w, 0.0, 1.0);
o = vec4<f32>(max(b.xyz, vec3<f32>(0.0)), b.w);
}
case 15u: {
var mc = c;
var ma = a;
if ((u0.z & 8u) == 0u) {
let q = textureLoad(aux, p, 0);
mc = fxk_straight(q);
ma = max(q.w, 0.0);
}
var m = dst_pick(u0.y, mc, ma);
if ((u0.z & 4u) != 0u) {
m = m * ma;
}
m = clamp(m, 0.0, 1.0);
let inv = (u0.z & 1u) != 0u;
if (inv) {
m = 1.0 - m;
}
let own_m = (u0.z & 8u) != 0u;
let is_alpha = (u0.z & 16u) != 0u;
var na = m;
if ((u0.z & 2u) != 0u && (!own_m || !is_alpha || inv)) {
na = a * m;
}
o = fxk_premul(c, clamp(na, 0.0, 1.0));
}
case 16u: {
let ch_from = u0.y;
let ch_to = u0.z;
let inv = (u0.w & 1u) != 0u;
let solid = (u0.w & 2u) != 0u;
let second = (u0.w & 4u) != 0u;
var sc = c;
var sa = a;
if (second) {
let q = textureLoad(aux, p, 0);
sc = fxk_straight(q);
sa = max(q.w, 0.0);
}
if (sa <= 0.0 && ch_from != 7u && !second) {
if (solid) {
o = fxk_premul(c, 1.0);
}
textureStore(out, p, o);
return;
}
var na = a;
var r: vec3<f32>;
if (ch_from <= 3u) {
switch ch_from {
case 0u: {
let hsl = rgb_to_hsl(sc);
r = vec3<f32>(hsl.x, hsl.z, hsl.y);
}
case 1u: { r = hsl_to_rgb(sc.x, sc.z, sc.y); }
case 2u: {
let y = 0.299 * sc.x + 0.587 * sc.y + 0.114 * sc.z;
r = vec3<f32>(y, 0.492 * (sc.z - y) + 0.5, 0.877 * (sc.x - y) + 0.5);
}
default: {
let y = sc.x;
let uu = sc.y - 0.5;
let vv = sc.z - 0.5;
let rr = y + vv / 0.877;
let bb = y + uu / 0.492;
let gg = (y - 0.299 * rr - 0.114 * bb) / 0.587;
r = vec3<f32>(rr, gg, bb);
}
}
if (inv) {
r = 1.0 - r;
}
} else {
var v: f32;
switch ch_from {
case 4u: { v = sc.x; }
case 5u: { v = sc.y; }
case 6u: { v = sc.z; }
case 7u: { v = sa; }
case 8u: { v = rgb_to_hsl(sc).z; }
case 9u: { v = rgb_to_hsl(sc).x; }
case 10u: { v = rgb_to_hsl(sc).y; }
case 11u: { v = luminance(sc); }
case 12u: { v = max(max(sc.x, sc.y), sc.z); }
default: { v = min(min(sc.x, sc.y), sc.z); }
}
if (inv) {
v = 1.0 - v;
}
switch ch_to {
case 0u: { r = vec3<f32>(v, 0.0, 0.0); }
case 1u: { r = vec3<f32>(0.0, v, 0.0); }
case 2u: { r = vec3<f32>(0.0, 0.0, v); }
case 3u: {
na = clamp(v, 0.0, 1.0);
r = vec3<f32>(1.0);
}
case 5u: { r = hsl_to_rgb(v, 1.0, 0.5); }
case 6u: { r = hsl_to_rgb(0.0, clamp(v, 0.0, 1.0), 0.5); }
case 8u: {
let hsl = rgb_to_hsl(c);
r = hsl_to_rgb(hsl.x, clamp(hsl.y * v, 0.0, 1.0), hsl.z);
}
default: { r = vec3<f32>(v); }
}
}
if (solid) {
na = 1.0;
}
o = fxk_premul(max(r, vec3<f32>(0.0)), na);
}
case 17u: {
let q = textureLoad(aux, p, 0);
var bl: vec4<f32>;
switch u0.y {
case 0u: { bl = q; }
case 1u, 2u: {
let oa = q.w;
if (px.w <= 0.0 || oa <= 0.0) {
bl = px;
} else {
let hsl = rgb_to_hsl(c);
let ohsl = rgb_to_hsl(fxk_straight(q));
var h2 = ohsl.x;
var s2 = ohsl.y;
if (u0.y == 2u) {
if (hsl.y > 1e-4) {
s2 = hsl.y;
} else {
h2 = 0.0;
s2 = 0.0;
}
}
let rgb = hsl_to_rgb(h2, s2, hsl.z);
bl = px + (fxk_premul(rgb, a) - px) * oa;
}
}
case 3u: { bl = vec4<f32>(min(px.xyz, q.xyz), px.w); }
default: { bl = max(px, q); }
}
o = bl + (px - bl) * f0.x;
}
case 18u: {
let flags = u1.x;
var r = fxk_extract(px, u0.y, (flags & 1u) != 0u);
if ((flags & 8u) != 0u) {
let s = fxk_extract(textureLoad(aux, p, 0), u0.z, (flags & 2u) != 0u) * f0.x;
r = blend_pixel(u0.w, r, s, 0.5);
}
if ((flags & 4u) != 0u && r.w > 1e-6) {
r = fxk_premul(fxk_straight(r), px.w);
}
r.w = clamp(r.w, 0.0, 1.0);
o = r;
}
case 19u: {
let q = textureLoad(aux, p, 0);
let oc = fxk_straight(q);
let oa = max(q.w, 0.0);
var rc = c;
var ra = a;
if (u0.z != 2u) {
for (var i = 0u; i < 3u; i++) {
rc[i] = fxk_overflow(u0.y, fxk_arith(u0.y, c[i], oc[i]), u0.w);
}
}
if (u0.z != 0u) {
ra = fxk_overflow(u0.y, fxk_arith(u0.y, a, oa), u0.w);
}
let res = fxk_premul(rc, ra);
o = res + (px - res) * f0.x;
}
case 20u: {
let q = textureLoad(aux, p, 0);
o = px;
o[u0.y] = dst_pick(u0.z, fxk_straight(q), max(q.w, 0.0));
}
case 21u: {
o = fxk_premul(px.xyz, clamp(px.w, 0.0, 1.0));
}
case 22u: {
let d = c[u1.x] - fxk_secondary(c, u1.y, u1.z, f0.z);
let t = clamp(d / f0.x * f0.y, 0.0, 1.0);
o = vec4<f32>(clamp((1.0 - t - f0.w) / max(P.f[1].x - f0.w, 1e-3), 0.0, 1.0));
}
case 23u: {
let k = clamp(textureLoad(aux, p, 0).x, 0.0, 1.0);
let alpha = a * k;
if (u0.y == 1u) {
o = vec4<f32>(alpha, alpha, alpha, 1.0);
} else if (u0.y == 2u) {
var v = 0.5;
if (alpha <= 1e-3) {
v = 0.0;
} else if (alpha >= 0.999) {
v = 1.0;
}
o = vec4<f32>(v, v, v, 1.0);
} else {
let screen = f0.xyz;
var fg = c;
if (!(u0.z != 0u || k <= 1e-4)) {
let s = 1.0 - k;
for (var i = 0; i < 3; i++) {
fg[i] = min(max(c[i] - s * screen[i], 0.0) / k, max(c[i], 1.0));
}
}
let spill = max(fg[u1.x] - fxk_secondary(fg, u1.y, u1.z, f0.w), 0.0);
fg[u1.x] = fg[u1.x] - spill * P.f[1].x;
o = fxk_premul(fg, alpha);
}
}
case 24u: {
o = vec4<f32>(px.w);
}
case 25u: {
var na = textureLoad(aux, p, 0).x;
if (u0.y != 0u) {
na = clamp((na - 0.5) * 1.5 + 0.5, 0.0, 1.0);
}
var cc = c;
let a0 = a;
if (P.u[3].y != 0u && a0 > 1e-4 && a0 < 0.999) {
let q = fxk_rows(p);
if (q.w > 1e-6) {
let ea = q.w;
if (ea > 1e-4) {
let t = f0.y * (1.0 - a0);
cc = cc + (q.xyz / ea - cc) * t;
}
}
}
na = clamp((na - 0.5) * f0.x + 0.5, 0.0, 1.0);
if (a0 <= 1e-6) {
na = 0.0;
}
o = fxk_premul(cc, na);
}
case 26u: {
o = vec4<f32>(clamp((px.x - f0.x) / f0.y + 0.5, 0.0, 1.0));
}
case 27u: {
let na = clamp(textureLoad(aux, p, 0).x, 0.0, 1.0);
var cc = c;
if (px.w <= 1e-4) {
cc = fxk_straight(fxk_rows(p));
}
o = fxk_premul(cc, na);
}
case 29u: {
let g = luminance(px.xyz);
o = vec4<f32>(g, px.w, g * px.w, g * g);
}
case 30u: {
let var_g = px.w - px.x * px.x;
let cov = px.z - px.x * px.y;
let ca = cov / (var_g + f0.x);
o = vec4<f32>(ca, px.y - ca * px.x, 0.0, 0.0);
}
case 31u: {
let q = textureLoad(aux, p, 0);
if (fxk_rows(p).x != 0.0) {
o = vec4<f32>(clamp(px.x * q.x + px.y, 0.0, 1.0));
} else {
o = vec4<f32>(q.y);
}
}
case 32u: {
o = vec4<f32>(select(0.0, 1.0, px.y - textureLoad(aux, p, 0).y > 1e-3));
}
case 33u: {
var v = px.x;
let choke = f0.x;
if (choke > 0.0) {
v = (v - choke) / (1.0 - choke);
} else if (choke < 0.0) {
v = v / (1.0 + choke);
}
if (abs(f0.y - 1.0) > 1e-6) {
v = (v - 0.5) * f0.y + 0.5;
}
v = clamp(v, 0.0, 1.0);
if (u0.y != 0u) {
v = 1.0 - v;
}
o = vec4<f32>(v);
}
case 34u: {
let v = px.x;
if (textureLoad(aux, p, 0).x != 0.0) {
o = vec4<f32>(0.5 + 0.5 * v, 0.5 * v, 0.5 * v, 1.0);
} else {
o = vec4<f32>(v, v, v, 1.0);
}
}
case 35u: {
let n = textureLoad(aux, p, 0).x;
let a0 = px.w;
var t = 0.0;
if (a0 > 1e-4 && a0 < 0.999) {
t = 1.0 - a0;
} else if (u0.w != 0u && n > 1e-4 && n < 0.999) {
t = 1.0 - n;
}
let strength = f0.x * t;
if (u0.y != 0u) {
let v = select(0.0, strength, u0.z != 0u);
o = vec4<f32>(v, v, v, 1.0);
} else if (P.u[3].y != 0u && strength > 0.0 && a0 > 1e-4) {
let q = fxk_rows(p);
if (q.w > 1e-4) {
let cc = c + (q.xyz / q.w - c) * strength;
o = fxk_premul(cc, a0);
}
}
}
case 28u: {
let v = clamp(px.w, 0.0, 1.0);
o = vec4<f32>(v, v, v, 1.0);
}
default: {}
}
textureStore(out, p, o);
}
// util::fit_layer: bilinear sample of the other layer (src) at the buffer coordinates in data
// (one per column, then one per row).
@compute @workgroup_size(16, 16)
fn fxk_fit(@builtin(global_invocation_id) gid: vec3<u32>) {
let dims = out_dims();
let p = vec2<i32>(gid.xy);
if (p.x >= dims.x || p.y >= dims.y) {
return;
}
let x = data[u32(p.x)];
let y = data[u32(dims.x) + u32(p.y)];
textureStore(out, p, sample_bilinear(src, x, y));
}
// Sum of 16×16 blocks; u[0].x = first pass: sum Advanced Spill Suppressor's per-channel excess
// of each primary over the other two (straight colour × alpha), else sum the values.
@compute @workgroup_size(16, 16)
fn fxk_reduce(@builtin(global_invocation_id) gid: vec3<u32>) {
let dims = out_dims();
let p = vec2<i32>(gid.xy);
if (p.x >= dims.x || p.y >= dims.y) {
return;
}
let sd = vec2<i32>(textureDimensions(src));
var acc = vec4<f32>(0.0);
for (var j = 0; j < 16; j++) {
for (var i = 0; i < 16; i++) {
let q = vec2<i32>(p.x * 16 + i, p.y * 16 + j);
if (q.x >= sd.x || q.y >= sd.y) {
continue;
}
let v = textureLoad(src, q, 0);
if (P.u[0].x != 0u) {
let c = fxk_straight(v);
let a = max(v.w, 0.0);
acc += vec4<f32>(
max(c.x - max(c.y, c.z), 0.0) * a,
max(c.y - max(c.x, c.z), 0.0) * a,
max(c.z - max(c.x, c.y), 0.0) * a,
0.0,
);
} else {
acc += v;
}
}
}
textureStore(out, p, acc);
}
+825
View File
@@ -0,0 +1,825 @@
// GPU effects (noise, blur and time family): see src/fx_noise.rs. Each entry point mirrors the CPU effect in
// effectcraft-effects operation for operation. Every name here is prefixed `fxn_` (the
// family files share one module).
const FXN_MAX: f32 = 3.40282347e38;
fn fxn_pixel(gid: vec3<u32>) -> vec2<i32> {
let dims = out_dims();
let p = vec2<i32>(gid.xy);
if (p.x >= dims.x || p.y >= dims.y) {
return vec2<i32>(-1);
}
return p;
}
// a / b rounded like the CPU's IEEE division (GPU division may be approximate): one
// residual correction with fma. Used where a threshold follows the quotient.
fn fxn_div4(a: vec4<f32>, b: vec4<f32>) -> vec4<f32> {
let q = a / b;
let r = fma(-q, b, a);
return fma(r, 1.0 / b, q);
}
// util::unpremul: (straight colour, alpha).
fn fxn_unpremul(p: vec4<f32>) -> vec4<f32> {
if (p.w > 1e-6) {
return vec4<f32>(p.xyz / p.w, p.w);
}
return vec4<f32>(0.0, 0.0, 0.0, max(p.w, 0.0));
}
// ---------------------------------------------------------------- Median family (noise::median_image)
// Quantise to the median's 512 levels (0..511). u[0].x = 1: straight colour with alpha 1
// (Median (Legacy) colour only).
@compute @workgroup_size(16, 16)
fn fxn_quant(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
var v = textureLoad(src, p, 0);
if (P.u[0].x == 1u) {
v = vec4<f32>(fxn_unpremul(v).xyz, 1.0);
}
let q = clamp(v, vec4<f32>(0.0), vec4<f32>(1.0)) * 511.0;
textureStore(out, p, vec4<f32>(round_away(q.x), round_away(q.y), round_away(q.z), round_away(q.w)));
}
// Per-channel median of the quantised levels over a (2r+1)² window (edges repeated): the
// smallest level m with more than half the window at or below m. u[0].x = r.
@compute @workgroup_size(16, 16)
fn fxn_median(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let r = i32(P.u[0].x);
let n = (2 * r + 1) * (2 * r + 1);
let half = f32(n / 2);
var lo = vec4<f32>(0.0);
var hi = vec4<f32>(511.0);
for (var it = 0; it < 9; it++) {
let mid = floor((lo + hi) * 0.5);
var cnt = vec4<f32>(0.0);
for (var dy = -r; dy <= r; dy++) {
for (var dx = -r; dx <= r; dx++) {
let q = tex_get_clamped(src, p.x + dx, p.y + dy);
cnt += select(vec4<f32>(0.0), vec4<f32>(1.0), q <= mid);
}
}
let above = cnt > vec4<f32>(half);
hi = select(hi, mid, above);
lo = select(mid + 1.0, lo, above);
}
textureStore(out, p, fxn_div4(lo, vec4<f32>(511.0)));
}
// noise::median_px.
fn fxn_median_px(o: vec4<f32>, m: vec4<f32>, on_alpha: bool) -> vec4<f32> {
if (on_alpha) {
let a = clamp(m.w, 0.0, 1.0);
return vec4<f32>(min(m.xyz, vec3<f32>(a)), a);
}
if (m.w <= 1e-4) {
return o;
}
return vec4<f32>(min(fxn_div4(m, vec4<f32>(m.w)).xyz, vec3<f32>(1.0)) * o.w, o.w);
}
// src = the layer, aux = the median. u[0] = (mode, operate on alpha): 0 Median, 1 Dust &
// Scratches (f[0].x = threshold), 2 Median (Legacy) with alpha, 3 Median (Legacy) colour only.
@compute @workgroup_size(16, 16)
fn fxn_median_out(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let o = textureLoad(src, p, 0);
let m = textureLoad(aux, p, 0);
let on_alpha = P.u[0].y != 0u;
var r = o;
switch P.u[0].x {
case 0u: {
r = fxn_median_px(o, m, on_alpha);
}
case 1u: {
let cand = fxn_median_px(o, m, on_alpha);
let d = abs(cand - o);
if (max(max(d.x, d.y), max(d.z, d.w)) > P.f[0].x) {
r = cand;
}
}
case 2u: {
r = m;
}
default: {
r = vec4<f32>(m.xyz * o.w, o.w);
}
}
textureStore(out, p, r);
}
// ---------------------------------------------------------------- Bilateral / Smart Blur (blur2.rs)
// blur2::edge_preserving. u[0] = (ceil r, stride, hard, largest d²); u[1].x = grey (Bilateral
// with Colorize off); f[0] = (2σs², 2σr², range).
@compute @workgroup_size(16, 16)
fn fxn_edge(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let ri = i32(P.u[0].x);
let stride = i32(P.u[0].y);
let hard = P.u[0].z != 0u;
let d2max = i32(P.u[0].w);
let sig_s2 = P.f[0].x;
let sig_r2 = P.f[0].y;
let range = P.f[0].z;
let c = textureLoad(src, p, 0);
let cl = luminance(c.xyz);
var acc = vec4<f32>(0.0);
var wsum = 0.0;
for (var dy = -ri; dy <= ri; dy += stride) {
for (var dx = -ri; dx <= ri; dx += stride) {
let d2 = dx * dx + dy * dy;
if (d2 > d2max) {
continue;
}
let q = tex_get_clamped(src, p.x + dx, p.y + dy);
var w = 0.0;
if (hard) {
w = select(0.0, 1.0, abs(luminance(q.xyz) - cl) <= range);
} else {
let d = q - c;
let dc = d.x * d.x + d.y * d.y + d.z * d.z + d.w * d.w;
w = exp(-f32(d2) / sig_s2 - dc / sig_r2);
}
if (w > 0.0) {
acc += q * w;
wsum += w;
}
}
}
var o = c;
if (wsum > 0.0) {
o = acc / wsum;
}
if (P.u[1].x != 0u && o.w > 0.0) {
let l = luminance(o.xyz / o.w);
o = vec4<f32>(vec3<f32>(l * o.w), o.w);
}
textureStore(out, p, o);
}
// Smart Blur's Edge Only / Overlay Edge (blur2::sobel on the blurred layer's premultiplied
// luminance). u[0].x = mode; f[0].x = edge threshold.
@compute @workgroup_size(16, 16)
fn fxn_smart_edges(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
var l: array<f32, 9>;
for (var j = 0; j < 3; j++) {
for (var i = 0; i < 3; i++) {
l[j * 3 + i] = luminance(tex_get_clamped(src, p.x + i - 1, p.y + j - 1).xyz);
}
}
let gx = l[2] + 2.0 * l[5] + l[8] - l[0] - 2.0 * l[3] - l[6];
let gy = l[6] + 2.0 * l[7] + l[8] - l[0] - 2.0 * l[1] - l[2];
let e = sqrt(gx * gx + gy * gy) * 0.25;
let px = textureLoad(src, p, 0);
let on = e > P.f[0].x;
var o = px;
if (P.u[0].x == 1u) {
o = vec4<f32>(vec3<f32>(select(0.0, 1.0, on) * px.w), px.w);
} else if (on) {
o = vec4<f32>(vec3<f32>(px.w), px.w);
}
textureStore(out, p, o);
}
// ---------------------------------------------------------------- Sharpen / Unsharp Mask (misc::unsharp_core)
// src = the layer, aux = its blur. f[0] = (amount, threshold in 0..255).
@compute @workgroup_size(16, 16)
fn fxn_unsharp(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
var o = textureLoad(src, p, 0);
let bl = textureLoad(aux, p, 0);
for (var c = 0; c < 3; c++) {
let d = o[c] - bl[c];
if (abs(d) * 255.0 >= P.f[0].y) {
o[c] = max(o[c] + d * P.f[0].x, 0.0);
}
}
textureStore(out, p, o);
}
// ---------------------------------------------------------------- planes
// Channel select: channel c of `src` becomes aux[c] (u[0].x = 0) or aux's alpha (1) where
// u[1][c] != 0.
@compute @workgroup_size(16, 16)
fn fxn_pick(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
var o = textureLoad(src, p, 0);
let a = textureLoad(aux, p, 0);
for (var c = 0; c < 4; c++) {
if (P.u[1][c] != 0u) {
o[c] = select(a[c], a.w, P.u[0].x == 1u);
}
}
textureStore(out, p, o);
}
// Channel Blur's result: src = blurred premultiplied channels, aux = the alpha blurred like
// each channel (alpha = the Alpha Blurriness result).
@compute @workgroup_size(16, 16)
fn fxn_cblur_out(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let v = textureLoad(src, p, 0);
let ba = textureLoad(aux, p, 0);
var s = vec3<f32>(0.0);
for (var c = 0; c < 3; c++) {
if (ba[c] > 1e-6) {
s[c] = v[c] / ba[c];
}
}
let a = clamp(ba.w, 0.0, 1.0);
textureStore(out, p, vec4<f32>(s * a, a));
}
// Straight colour planes with the raw alpha (Minimax).
@compute @workgroup_size(16, 16)
fn fxn_straight(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let v = textureLoad(src, p, 0);
textureStore(out, p, vec4<f32>(fxn_unpremul(v).xyz, v.w));
}
// Premultiply straight planes by the clamped alpha.
@compute @workgroup_size(16, 16)
fn fxn_premul(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let v = textureLoad(src, p, 0);
let a = clamp(v.w, 0.0, 1.0);
textureStore(out, p, vec4<f32>(v.xyz * a, a));
}
// util::morph_plane along one axis (edges repeated). u[0] = (r, vertical); u[1] = per-channel
// operation (0 min, 1 max, 2 keep).
@compute @workgroup_size(16, 16)
fn fxn_morph(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let r = i32(P.u[0].x);
let vertical = P.u[0].y != 0u;
let centre = textureLoad(src, p, 0);
var lo = centre;
var hi = centre;
for (var k = -r; k <= r; k++) {
var v: vec4<f32>;
if (vertical) {
v = tex_get_clamped(src, p.x, p.y + k);
} else {
v = tex_get_clamped(src, p.x + k, p.y);
}
lo = min(lo, v);
hi = max(hi, v);
}
var o = centre;
for (var c = 0; c < 4; c++) {
if (P.u[1][c] == 0u) {
o[c] = lo[c];
} else if (P.u[1][c] == 1u) {
o[c] = hi[c];
}
}
textureStore(out, p, o);
}
// Crop: out(p) = src(p + u[0].xy).
@compute @workgroup_size(16, 16)
fn fxn_crop(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
textureStore(out, p, textureLoad(src, p + vec2<i32>(P.u[0].xy), 0));
}
// ---------------------------------------------------------------- Compound Blur (blur2::compound_blur)
// The blur map (straight luminance × alpha, inverted with u[0].y) in every channel. u[0].x = 1:
// `src` is the Blur Layer sampled at the per-column / per-row coordinates in `data` (u[1].x =
// width); 0: `src` is the layer itself.
@compute @workgroup_size(16, 16)
fn fxn_cb_map(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
var v: vec4<f32>;
if (P.u[0].x == 1u) {
let w = P.u[1].x;
v = sample_bilinear(src, data[u32(p.x)], data[w + u32(p.y)]);
} else {
v = textureLoad(src, p, 0);
}
let s = fxn_unpremul(v);
var l = luminance(s.xyz) * s.w;
if (P.u[0].y != 0u) {
l = 1.0 - l;
}
textureStore(out, p, vec4<f32>(l));
}
// Writes the pixels whose blur level falls between levels k (src) and k + 1 (aux). The map is
// `data` (RGBA f32 rows of u[0].y pixels); u[0].x = k.
@compute @workgroup_size(16, 16)
fn fxn_cb_level(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let m = data[(u32(p.y) * P.u[0].y + u32(p.x)) * 4u];
let t = clamp(m, 0.0, 1.0) * 4.0;
let k = min(u32(floor(t)), 3u);
if (k != P.u[0].x) {
return;
}
let f = t - f32(k);
let a = textureLoad(src, p, 0);
let c = textureLoad(aux, p, 0);
textureStore(out, p, a + (c - a) * f);
}
// ---------------------------------------------------------------- Echo (time_fx::echo)
// acc (src) ⊕ frame (aux) × f[0].x. u[0] = (operator, first frame).
@compute @workgroup_size(16, 16)
fn fxn_echo(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let op = P.u[0].x;
var acc = textureLoad(src, p, 0);
if (P.u[0].y != 0u) {
acc = select(vec4<f32>(0.0), vec4<f32>(FXN_MAX), op == 2u);
}
let k = P.f[0].x;
let q = textureLoad(aux, p, 0) * k;
switch op {
case 1u: {
acc = max(acc, q);
}
case 2u: {
acc = min(acc, q);
}
case 3u: {
acc = acc + q - acc * q;
}
case 4u: {
acc += q * (1.0 - acc.w);
}
case 5u: {
acc = q + acc * (1.0 - q.w);
}
default: {
acc += q;
}
}
textureStore(out, p, acc);
}
// Echo's finish. u[0].x = operator; f[0].x = number of frames.
@compute @workgroup_size(16, 16)
fn fxn_echo_fin(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let op = P.u[0].x;
var acc = textureLoad(src, p, 0);
if (op == 6u) {
acc = acc / P.f[0].x;
}
if (op == 2u && acc.x == FXN_MAX) {
acc = vec4<f32>(0.0);
}
textureStore(out, p, vec4<f32>(max(acc.xyz, vec3<f32>(0.0)), clamp(acc.w, 0.0, 1.0)));
}
// ---------------------------------------------------------------- Fractal / Turbulent Noise (noise3.rs)
fn fxn_catmull(p0: f32, p1: f32, p2: f32, p3: f32, t: f32) -> f32 {
return 0.5 * (2.0 * p1 + (p2 - p0) * t + (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t * t + (3.0 * p1 - p0 - 3.0 * p2 + p3) * t * t * t);
}
fn fxn_slice(ix: i32, iy: i32, iz: i32, tx: f32, ty: f32, seed: u32, nt: u32) -> f32 {
if (nt == 0u) {
return lattice(ix, iy, iz, seed);
}
if (nt == 1u) {
let a = lattice(ix, iy, iz, seed);
let b = lattice(ix + 1, iy, iz, seed);
let c = lattice(ix, iy + 1, iz, seed);
let d = lattice(ix + 1, iy + 1, iz, seed);
let top = a + (b - a) * tx;
let bot = c + (d - c) * tx;
return top + (bot - top) * ty;
}
var rows: array<f32, 4>;
for (var j = 0; j < 4; j++) {
let y = iy + j - 1;
rows[j] = fxn_catmull(lattice(ix - 1, y, iz, seed), lattice(ix, y, iz, seed), lattice(ix + 1, y, iz, seed), lattice(ix + 2, y, iz, seed), tx);
}
return fxn_catmull(rows[0], rows[1], rows[2], rows[3], ty);
}
// noise3::typed_noise.
fn fxn_typed_noise(x: f32, y: f32, z: f32, seed: u32, nt: u32) -> f32 {
if (nt == 2u) {
return value_noise(x, y, z, seed);
}
let x0 = floor(x);
let y0 = floor(y);
let z0 = floor(z);
let tx = x - x0;
let ty = y - y0;
let fz = fade(z - z0);
let a = fxn_slice(i32(x0), i32(y0), i32(z0), tx, ty, seed, nt);
let b = fxn_slice(i32(x0), i32(y0), i32(z0) + 1, tx, ty, seed, nt);
return a + (b - a) * fz;
}
// noise3::shape.
fn fxn_shape(kind: u32, n: f32) -> f32 {
let s = n * 2.0 - 1.0;
switch kind {
case 1u: {
return 1.0 - s * s;
}
case 2u: {
return 1.0 - abs(s);
}
case 3u: {
let t = 1.0 - abs(s);
return t * t;
}
case 8u: {
let t = clamp(abs(s) / 0.12, 0.0, 1.0);
return 1.0 - t * t;
}
default: {
return n;
}
}
}
// Fractal::layer. `o` = octave data base index.
fn fxn_layer(u: f32, v: f32, o: u32, z: f32, seed: u32) -> f32 {
let nt = P.u[0].y;
if (P.u[1].y != 0u) {
let t = P.f[2].x;
let c = P.f[2].y;
let w = P.f[2].z;
let a = fxn_typed_noise(u, v, t + z, seed, nt);
let b = fxn_typed_noise(u, v, t - c + z, seed, nt);
return a * (1.0 - w) + b * w;
}
return fxn_typed_noise(u, v, data[o + 7u] + z, seed, nt);
}
// u[0] = (kind, noise type, octaves, seed); u[1] = (sub rotation, cycle, invert, overflow);
// u[2].x = blend mode (BlendMode::ALL index, 255 = None); f[0] = (sin, cos, size x, size y);
// f[1] = (contrast, brightness, opacity, blend with original); f[2] = (cycle t, cycle, t / cycle,
// norm); data = 10 floats per octave (noise3::FractalGpu).
@compute @workgroup_size(16, 16)
fn fxn_fractal(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let kind = P.u[0].x;
let n_oct = P.u[0].z;
let sr = P.f[0].x;
let cr = P.f[0].y;
let x = f32(p.x) + 0.5;
let y = f32(p.y) + 0.5;
var sum = 0.0;
var best = 0.0;
for (var oi = 0u; oi < n_oct; oi++) {
let o = oi * 10u;
let dx = x - data[o];
let dy = y - data[o + 1u];
var u = (dx * cr + dy * sr) / P.f[0].z;
var v = (-dx * sr + dy * cr) / P.f[0].w;
if (P.u[1].x != 0u) {
let s = data[o + 2u];
let c = data[o + 3u];
let u2 = u * c + v * s;
v = -u * s + v * c;
u = u2;
}
let f = data[o + 4u];
u = u * f;
v = v * f;
let z = data[o + 8u];
let seed = P.u[0].w + oi;
if (kind >= 4u && kind <= 6u) {
let k = data[o + 9u];
let a = fxn_layer(u * 0.5, v * 0.5, o, z + 3.1, seed ^ 0x55u) - 0.5;
let bb = fxn_layer(u * 0.5, v * 0.5, o, z + 5.7, seed ^ 0xaau) - 0.5;
if (kind == 5u) {
let s = sin(a * 3.0);
let c = cos(a * 3.0);
let u2 = u * c - v * s;
v = u * s + v * c;
u = u2;
} else {
u += a * 1.5 * k;
v += bb * 1.5 * k;
}
}
let n = fxn_shape(kind, fxn_layer(u, v, o, z, seed));
let amp = data[o + 5u];
if (kind == 7u) {
best = max(best, n * amp);
}
sum += n * amp * data[o + 6u];
}
var raw = sum / max(P.f[2].w, 1e-6);
if (kind == 7u) {
raw = best;
}
var val = (raw - 0.5) * P.f[1].x + 0.5 + P.f[1].y;
let ov = P.u[1].w;
if (ov == 0u) {
val = clamp(val, 0.0, 1.0);
} else if (ov == 1u) {
val = clamp(0.5 + 0.5 * tanh(2.0 * (val - 0.5)) / tanh(1.0), 0.0, 1.0);
} else if (ov == 2u) {
let t = val - 2.0 * floor(val / 2.0);
val = select(t, 2.0 - t, t > 1.0);
}
if (P.u[1].z != 0u) {
val = 1.0 - val;
}
let op = P.f[1].z;
let s = vec4<f32>(vec3<f32>(val * op), op);
let orig = textureLoad(src, p, 0);
var r = s;
if (P.u[2].x != 255u) {
r = blend_pixel(P.u[2].x, orig, s, 0.5);
}
let bo = P.f[1].w;
textureStore(out, p, orig * bo + r * (1.0 - bo));
}
// ---------------------------------------------------------------- Noise Alpha / Noise HLS (noise.rs)
// noise::phased_cycle with the phase's two seeds and smoothed fraction resolved on the CPU.
fn fxn_phased(x: u32, y: u32, sa: u32, sb: u32, t: f32) -> f32 {
let a = hash_noise(x, y, sa);
let b = hash_noise(x, y, sb);
return a + (b - a) * t;
}
// u[0] = (squared, original alpha mode, overflow, seed a); u[1].x = seed b; f[0] = (amount, t).
@compute @workgroup_size(16, 16)
fn fxn_noise_alpha(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let k = P.f[0].x;
var n = fxn_phased(u32(p.x), u32(p.y), P.u[0].w, P.u[1].x, P.f[0].y);
if (P.u[0].x != 0u) {
n = n * n;
}
let s = fxn_unpremul(textureLoad(src, p, 0));
let a = s.w;
var na: f32;
switch P.u[0].y {
case 0u: {
na = a + n * k;
}
case 1u: {
na = select(0.0, a + n * k, a > 0.0);
}
case 2u: {
na = a * (1.0 + (2.0 * n - 1.0) * k);
}
default: {
na = a + (2.0 * n - 1.0) * k * 4.0 * a * (1.0 - a);
}
}
if (P.u[0].z == 1u) {
let t = na - 2.0 * floor(na / 2.0);
na = select(t, 2.0 - t, t > 1.0);
} else if (P.u[0].z == 2u && !(na >= 0.0 && na <= 1.0)) {
na = na - floor(na);
}
na = clamp(na, 0.0, 1.0);
textureStore(out, p, vec4<f32>(s.xyz * na, na));
}
// Noise HLS: u[0] = (noise kind, seed); u[1], u[2].xy = per channel (hue, lightness,
// saturation) the phase's two seeds; f[0] = (hue, lightness, saturation, grain size); f[1].x =
// phase (Grain), f[1].y = smoothed fraction.
fn fxn_hls_seed(i: u32) -> u32 {
if (i < 4u) {
return P.u[1][i];
}
return P.u[2][i - 4u];
}
fn fxn_hls_n(x: u32, y: u32, k: u32) -> f32 {
let s = P.u[0].y + k * 7919u;
switch P.u[0].x {
case 2u: {
let gs = P.f[0].w;
return clamp((value_noise(f32(x) / gs, f32(y) / gs, P.f[1].x, s) - 0.5) * 3.0, -1.0, 1.0);
}
case 1u: {
let v = fxn_phased(x, y, fxn_hls_seed(2u * k), fxn_hls_seed(2u * k + 1u), P.f[1].y) * 2.0 - 1.0;
return v * abs(v);
}
default: {
return fxn_phased(x, y, fxn_hls_seed(2u * k), fxn_hls_seed(2u * k + 1u), P.f[1].y) * 2.0 - 1.0;
}
}
}
@compute @workgroup_size(16, 16)
fn fxn_noise_hls(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let px = textureLoad(src, p, 0);
let s0 = fxn_unpremul(px);
let a = s0.w;
if (a <= 0.0) {
textureStore(out, p, px);
return;
}
let x = u32(p.x);
let y = u32(p.y);
var hsl = rgb_to_hsl(s0.xyz);
if (P.f[0].x != 0.0) {
hsl.x = fract_euclid(hsl.x + fxn_hls_n(x, y, 0u) * P.f[0].x * 0.5);
}
if (P.f[0].y != 0.0) {
hsl.z = clamp(hsl.z + fxn_hls_n(x, y, 1u) * P.f[0].y, 0.0, 1.0);
}
if (P.f[0].z != 0.0) {
hsl.y = clamp(hsl.y + fxn_hls_n(x, y, 2u) * P.f[0].z, 0.0, 1.0);
}
let c = hsl_to_rgb(hsl.x, hsl.y, hsl.z);
textureStore(out, p, vec4<f32>(c * a, a));
}
// ---------------------------------------------------------------- Remove Grain (noise::remove_grain)
// The guide (util::guided_filter's g): the luminance of the premultiplied channels in every
// channel (Multichannel), or the channels themselves (u[0].x, Single Channel).
@compute @workgroup_size(16, 16)
fn fxn_gf_guide(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let v = textureLoad(src, p, 0);
if (P.u[0].x != 0u) {
textureStore(out, p, v);
} else {
textureStore(out, p, vec4<f32>(luminance(v.xyz)));
}
}
// Guided filter arithmetic, S = src, A = aux, D = `data` (RGBA f32 rows of u[0].y pixels):
// u[0].x = 0: S − A·D, 1: S − A·A, 2: S / (A + f[0].x), 3: A − S·D, 4: S·D + A, 5: S·A, 6: S·S.
@compute @workgroup_size(16, 16)
fn fxn_gf(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let s = textureLoad(src, p, 0);
let a = textureLoad(aux, p, 0);
var d = vec4<f32>(0.0);
let mode = P.u[0].x;
if (mode == 0u || mode == 3u || mode == 4u) {
let i = (u32(p.y) * P.u[0].y + u32(p.x)) * 4u;
d = vec4<f32>(data[i], data[i + 1u], data[i + 2u], data[i + 3u]);
}
var o: vec4<f32>;
switch mode {
case 0u: {
o = s - a * d;
}
case 1u: {
o = s - a * a;
}
case 2u: {
o = s / (a + P.f[0].x);
}
case 3u: {
o = a - s * d;
}
case 4u: {
o = s * d + a;
}
case 5u: {
o = s * a;
}
default: {
o = s * s;
}
}
textureStore(out, p, o);
}
// Texture: f + (o − f) × f[0].x on colour (src = filtered, aux = original).
@compute @workgroup_size(16, 16)
fn fxn_rg_texture(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let f = textureLoad(src, p, 0);
let o = textureLoad(aux, p, 0);
textureStore(out, p, vec4<f32>(f.xyz + (o.xyz - f.xyz) * P.f[0].x, f.w));
}
// Unsharp Mask on the planes: src = channels, aux = their blur; f[0] = (amount, threshold).
@compute @workgroup_size(16, 16)
fn fxn_rg_unsharp(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
var v = textureLoad(src, p, 0);
let l = textureLoad(aux, p, 0);
for (var c = 0; c < 3; c++) {
if (abs(v[c] - l[c]) > P.f[0].y) {
v[c] = v[c] + (v[c] - l[c]) * P.f[0].x;
}
}
textureStore(out, p, v);
}
// Remove Grain's result: colour clamped to 0..alpha (alpha from aux = the original); with
// u[0].x the Preview viewing mode (noise::preview_compose): the original (aux) outside the
// region f[0] = (x0, x1, y0, y1), the box outline in f[1] where u[0].y.
@compute @workgroup_size(16, 16)
fn fxn_rg_out(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = fxn_pixel(gid);
if (p.x < 0) {
return;
}
let o = textureLoad(aux, p, 0);
let a = o.w;
var v = vec4<f32>(clamp(textureLoad(src, p, 0).xyz, vec3<f32>(0.0), vec3<f32>(max(a, 0.0))), a);
if (P.u[0].x != 0u) {
let x = f32(p.x) + 0.5;
let y = f32(p.y) + 0.5;
let r = P.f[0];
let inside = x >= r.x && x < r.y && y >= r.z && y < r.w;
if (!inside) {
v = o;
}
if (P.u[0].y != 0u) {
let nx = abs(x - r.x) < 0.75 || abs(x - r.y) < 0.75;
let ny = abs(y - r.z) < 0.75 || abs(y - r.w) < 0.75;
if ((nx && y >= r.z - 0.75 && y < r.w + 0.75) || (ny && x >= r.x - 0.75 && x < r.y + 0.75)) {
v = vec4<f32>(P.f[1].xyz, 1.0);
}
}
}
textureStore(out, p, v);
}
+925
View File
@@ -0,0 +1,925 @@
// GPU effects (stylize and distort family): see src/fx_stylize.rs. Each entry point mirrors the CPU effect in
// effectcraft-effects operation for operation. Every name here is prefixed `fxs_` (the
// family files share one module). Coordinates are pixel centres (x + 0.5, y + 0.5) in buffer
// pixels, as in util::remap. Per-column / per-row tables in `data` hold coordinates the CPU
// computes in f64 (anything with a floor or a wrap), so tile and wrap boundaries match exactly.
const FXS_TAU: f32 = 6.2831855;
const FXS_PI: f32 = 3.1415927;
const FXS_HALF_PI: f32 = 1.5707964;
fn fxs_inside(p: vec2<i32>) -> bool {
let d = out_dims();
return p.x < d.x && p.y < d.y;
}
fn fxs_centre(p: vec2<i32>) -> vec2<f32> {
return vec2<f32>(f32(p.x) + 0.5, f32(p.y) + 0.5);
}
// util::unpremul: straight colour (zero below alpha 1e-6) and alpha (≥ 0).
fn fxs_unpremul(px: vec4<f32>) -> vec4<f32> {
let a = px.w;
if (a > 1e-6) {
return vec4<f32>(fxs_qdiv(px.xyz, a), a);
}
return vec4<f32>(0.0, 0.0, 0.0, max(a, 0.0));
}
// util::smoothstep.
fn fxs_smoothstep(e0: f32, e1: f32, x: f32) -> f32 {
if (abs(e1 - e0) < 1e-9) {
return select(1.0, 0.0, x < e0);
}
let t = clamp((x - e0) / (e1 - e0), 0.0, 1.0);
return t * t * (3.0 - 2.0 * t);
}
// x / a rounded like the CPU's IEEE division (GPU division may be off by an ulp, which flips
// thresholds on 8 / 16 bpc colours that sit exactly on them): one residual correction.
fn fxs_qdiv(x: vec3<f32>, a: f32) -> vec3<f32> {
let q = x / a;
let r = fma(-q, vec3<f32>(a), x);
return q + r / a;
}
fn fxs_rem2(i: i32) -> i32 {
return ((i % 2) + 2) % 2;
}
// ---------------------------------------------------------------- per-pixel colour effects
// u[0].x = mode:
// 1 Posterize (misc::posterize): f[0].x = levels − 1
// 2 Threshold (misc::threshold): f[0].x = level
// 3 CC Threshold: u[0].yz = (channel, invert); f[0].xy = (threshold, blend)
// 4 CC Threshold RGB: u[0].yzw = invert R, G, B; f[0] = (thresholds, blend)
// 5 Strobe Light on colour: u[0].y = operator; f[0] = (strobe colour, blend)
// 6 Strobe Light transparent (Image::scale_alpha): f[0].x = factor
// 7 CC Vignette: f[0] = (cx, cy, distance, amount); f[1].x = pin highlights
fn fxs_bin(v: f32, t: f32, inv: bool) -> f32 {
return select(0.0, 1.0, (v >= t) != inv);
}
fn fxs_strobe_op(op: u32, v: f32, k: f32) -> f32 {
switch op {
case 1u: { return v + k; }
case 2u: { return max(v - k, 0.0); }
case 3u: { return v * k; }
case 4u: { return abs(v - k); }
case 5u: { return 1.0 - (1.0 - v) * (1.0 - k); }
case 6u: { return max(v, k); }
case 7u: { return min(v, k); }
default: { return k; }
}
}
@compute @workgroup_size(16, 16)
fn fxs_point(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let px = textureLoad(src, p, 0);
let mode = P.u[0].x;
let a = px.w;
if (mode == 6u) {
textureStore(out, p, px * P.f[0].x);
return;
}
if (a <= 0.0) {
textureStore(out, p, px);
return;
}
switch mode {
case 1u: {
let lv = P.f[0].x;
let c = clamp(fxs_qdiv(px.xyz, a), vec3<f32>(0.0), vec3<f32>(1.0)) * lv;
let o = vec3<f32>(round_away(c.x), round_away(c.y), round_away(c.z)) / lv;
textureStore(out, p, vec4<f32>(o * a, a));
}
case 2u: {
let v = select(0.0, 1.0, luminance(fxs_qdiv(px.xyz, a)) >= P.f[0].x);
textureStore(out, p, vec4<f32>(vec3<f32>(v) * a, a));
}
case 3u: {
let u = fxs_unpremul(px);
let c = u.xyz;
let t = P.f[0].x;
let blend = P.f[0].y;
let inv = P.u[0].z != 0u;
var o = c;
var na = u.w;
switch P.u[0].y {
case 1u: {
o = vec3<f32>(fxs_bin(c.x, t, inv), fxs_bin(c.y, t, inv), fxs_bin(c.z, t, inv));
}
case 2u: {
o = vec3<f32>(fxs_bin(rgb_to_hsl(c).y, t, inv));
}
case 3u: {
na = fxs_bin(u.w, t, inv);
}
default: {
o = vec3<f32>(fxs_bin(luminance(c), t, inv));
}
}
o = o + (c - o) * blend;
na = na + (u.w - na) * blend;
textureStore(out, p, vec4<f32>(o * na, na));
}
case 4u: {
let c = fxs_qdiv(px.xyz, a);
let t = P.f[0].xyz;
let o = vec3<f32>(fxs_bin(c.x, t.x, P.u[0].y != 0u), fxs_bin(c.y, t.y, P.u[0].z != 0u), fxs_bin(c.z, t.z, P.u[0].w != 0u));
let r = o + (c - o) * P.f[0].w;
textureStore(out, p, vec4<f32>(r * a, a));
}
case 5u: {
let c = px.xyz / a;
let k = P.f[0].xyz;
let op = P.u[0].y;
let o = vec3<f32>(fxs_strobe_op(op, c.x, k.x), fxs_strobe_op(op, c.y, k.y), fxs_strobe_op(op, c.z, k.z));
let r = o + (c - o) * P.f[0].w;
textureStore(out, p, vec4<f32>(r * a, a));
}
case 7u: {
let u = fxs_unpremul(px);
let q = fxs_centre(p);
let dx = q.x - P.f[0].x;
let dy = q.y - P.f[0].y;
let r = sqrt(dx * dx + dy * dy) / P.f[0].z;
let f0 = 1.0 / (1.0 + r * r);
let f = f0 * f0;
var k = 1.0 - P.f[0].w * (1.0 - f);
let pin = P.f[1].x;
if (pin > 0.0) {
let l = clamp(luminance(u.xyz), 0.0, 1.0);
k += (1.0 - k) * pin * l;
}
textureStore(out, p, vec4<f32>(max(u.xyz * k, vec3<f32>(0.0)) * u.w, u.w));
}
default: {
textureStore(out, p, px);
}
}
}
// ---------------------------------------------------------------- inverse-mapped warps
// u[0].x = mode (all sample bilinear, transparent outside, unless noted):
// 1 Mirror (distort::mirror): f[0] = (cx, cy, nx, ny)
// 2 Spherize (distort::spherize): f[0] = (cx, cy, radius)
// 3 Polar Coordinates (distort::polar, edges repeated): u[0].y = to polar;
// f[0] = (w, h, cx, cy); f[1] = (rmax, interpolation)
// 4 CC Slant (distort3::cc_slant): f[0] = (floor x, floor y, 1 / height, slant); f[1].x = x scale
// 5 CC Smear (distort3::cc_smear): f[0] = (from x, from y, dx, dy); f[1] = (len², reach, radius)
// 6 CC Split (distort3::split_impl): f[0] = (ax, ay, ux, uy); f[1] = (len, split 1, split 2, reach)
// 7 Optics Compensation (distort2::optics_compensation): u[0].y = reverse;
// f[0] = (cx, cy, fit, f); f[1] = (reference radius, half FOV)
// 8 CC Kaleida (stylize2::kaleida, edges repeated): u[0].y = mirror;
// f[0] = (cx, cy, size, rotation); f[1].x = segment angle
// 9 Liquify (distort4::liquify): u[0].xy = (_, mesh nx, mesh ny); f[0] = (cell, mesh offset x, y,
// percentage); f[1] = (scale, buffer offset x, y); data = mesh offsets (x, y) per node
// 10 Twirl (Legacy) (distort3::twirl_legacy): f[0] = (cx, cy, radius, angle)
// 11 CC Ripple Pulse (distort3::cc_ripple_pulse): u[0].y = render bump; f[0] = (cx, cy, front,
// speed); f[1] = (time span, wavelength, height)
// 12 CC Power Pin, full perspective (distort3::cc_power_pin): f[2..5] = inverse of the
// square → quad matrix (rows); f[5] = source rect in layer pixels (x0, y0, w, h);
// f[6] = (scale, buffer offset x, y)
fn fxs_mesh_d(i: u32, j: u32) -> vec2<f32> {
let k = (j * P.u[0].y + i) * 2u;
return vec2<f32>(data[k], data[k + 1u]);
}
// distort3::cc_ripple_pulse's wave.
fn fxs_pulse(r: f32) -> f32 {
let front = P.f[0].z;
let speed = P.f[0].w;
let span = P.f[1].x;
let age = (front - r) / max(speed, 1e-9);
if (age < 0.0 || age > span) {
return 0.0;
}
let fade = 1.0 - age / span;
return sin(FXS_TAU * (r - front) / P.f[1].y) * fade;
}
// distort4::Mesh::at.
fn fxs_mesh_at(x: f32, y: f32) -> vec2<f32> {
let cell = P.f[0].x;
let nx = P.u[0].y;
let ny = P.u[0].z;
let fx = x / cell;
let fy = y / cell;
if (fx < 0.0 || fy < 0.0 || fx > f32(nx - 1u) || fy > f32(ny - 1u)) {
return vec2<f32>(0.0);
}
let x0 = min(u32(floor(fx)), nx - 2u);
let y0 = min(u32(floor(fy)), ny - 2u);
let x1 = min(x0 + 1u, nx - 1u);
let y1 = min(y0 + 1u, ny - 1u);
let tx = fx - f32(x0);
let ty = fy - f32(y0);
let a = fxs_mesh_d(x0, y0);
let b = fxs_mesh_d(x1, y0);
let c = fxs_mesh_d(x0, y1);
let d = fxs_mesh_d(x1, y1);
let top = a + (b - a) * tx;
let bot = c + (d - c) * tx;
return top + (bot - top) * ty;
}
@compute @workgroup_size(16, 16)
fn fxs_warp(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let q = fxs_centre(p);
let x = q.x;
let y = q.y;
var s = q;
var clamped = false;
switch P.u[0].x {
case 1u: {
let n = P.f[0].zw;
let d = (x - P.f[0].x) * n.x + (y - P.f[0].y) * n.y;
if (d > 0.0) {
s = vec2<f32>(x - 2.0 * d * n.x, y - 2.0 * d * n.y);
}
}
case 2u: {
let c = P.f[0].xy;
let r = P.f[0].z;
let dx = x - c.x;
let dy = y - c.y;
let d = sqrt(dx * dx + dy * dy);
if (d < r && d != 0.0) {
let nd = d / r;
let k = (asin(nd) / FXS_HALF_PI) / nd;
s = vec2<f32>(c.x + dx * k, c.y + dy * k);
}
}
case 3u: {
clamped = true;
let w = P.f[0].x;
let h = P.f[0].y;
let cx = P.f[0].z;
let cy = P.f[0].w;
let rmax = P.f[1].x;
var pp: vec2<f32>;
if (P.u[0].y != 0u) {
let dx = x - cx;
let dy = y - cy;
let a0 = atan2(dx, -dy);
let a = a0 - floor(a0 / FXS_TAU) * FXS_TAU;
let r = sqrt(dx * dx + dy * dy) / rmax;
pp = vec2<f32>(a / FXS_TAU * w, h - r * h);
} else {
let a = x / w * FXS_TAU;
let r = (h - y) / h * rmax;
pp = vec2<f32>(cx + r * sin(a), cy - r * cos(a));
}
let amt = P.f[1].y;
s = vec2<f32>(x + (pp.x - x) * amt, y + (pp.y - y) * amt);
}
case 4u: {
let fl = P.f[0].xy;
s = vec2<f32>(fl.x + (x - fl.x - (fl.y - y) * P.f[0].w) / P.f[1].x, fl.y + (y - fl.y) * P.f[0].z);
}
case 5u: {
let fr = P.f[0].xy;
let dx = P.f[0].z;
let dy = P.f[0].w;
let t = clamp(((x - fr.x) * dx + (y - fr.y) * dy) / P.f[1].x, 0.0, 1.0);
let px = fr.x + dx * t;
let py = fr.y + dy * t;
let d = sqrt((x - px) * (x - px) + (y - py) * (y - py));
let w0 = max(1.0 - d / P.f[1].z, 0.0);
let w = w0 * w0 * (3.0 - 2.0 * w0);
let k = P.f[1].y * w * t;
s = vec2<f32>(x - dx * k, y - dy * k);
}
case 6u: {
let a = P.f[0].xy;
let ux = P.f[0].z;
let uy = P.f[0].w;
let nx = -uy;
let ny = ux;
let len = P.f[1].x;
let reach = P.f[1].w;
let rx = x - a.x;
let ry = y - a.y;
let t = (rx * ux + ry * uy) / len;
let sd = rx * nx + ry * ny;
if (t >= 0.0 && t <= 1.0) {
let amt = select(P.f[1].z, P.f[1].y, sd >= 0.0);
let g = amt * reach * sin(FXS_PI * t);
let ad = abs(sd);
if (ad < g) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
let push = g * max(1.0 - (ad - g) / reach, 0.0);
let ns = select(-1.0, 1.0, sd >= 0.0) * (ad - push);
s = vec2<f32>(x + nx * (ns - sd), y + ny * (ns - sd));
}
}
case 7u: {
let c = P.f[0].xy;
let fit = P.f[0].z;
let f = P.f[0].w;
let r_ref = P.f[1].x;
let half = P.f[1].y;
let dx = x - c.x;
let dy = y - c.y;
let r = sqrt(dx * dx + dy * dy) * fit;
if (r >= 1e-9) {
var rs: f32;
if (P.u[0].y != 0u) {
rs = atan(r / f) / half * r_ref;
} else {
let th = r / r_ref * half;
if (th >= FXS_HALF_PI - 1e-4) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
rs = f * tan(th);
}
let k = rs / r * fit;
s = vec2<f32>(c.x + dx * k, c.y + dy * k);
}
}
case 8u: {
clamped = true;
let c = P.f[0].xy;
let rot = P.f[0].w;
let seg = P.f[1].x;
let dx = x - c.x;
let dy = y - c.y;
let r = sqrt(dx * dx + dy * dy) / P.f[0].z;
let a0 = atan2(dy, dx) - rot;
var a = a0 - floor(a0 / seg) * seg;
if (P.u[0].y != 0u && a > seg * 0.5) {
a = seg - a;
}
s = vec2<f32>(c.x + r * cos(a + rot), c.y + r * sin(a + rot));
}
case 9u: {
let scale = P.f[1].x;
let o = P.f[1].yz;
let lx = (x - o.x) / scale;
let ly = (y - o.y) / scale;
let d = fxs_mesh_at(lx - P.f[0].y, ly - P.f[0].z);
if (d.x == 0.0 && d.y == 0.0) {
textureStore(out, p, textureLoad(src, p, 0));
return;
}
let pct = P.f[0].w;
s = vec2<f32>((lx + d.x * pct) * scale + o.x, (ly + d.y * pct) * scale + o.y);
}
case 10u: {
let c = P.f[0].xy;
let r = P.f[0].z;
let dx = x - c.x;
let dy = y - c.y;
let d = sqrt(dx * dx + dy * dy);
if (d < r) {
let a = -P.f[0].w * (1.0 - d / r);
let sn = sin(a);
let co = cos(a);
s = vec2<f32>(c.x + dx * co - dy * sn, c.y + dx * sn + dy * co);
}
}
case 11u: {
let c = P.f[0].xy;
let dx = x - c.x;
let dy = y - c.y;
let r = sqrt(dx * dx + dy * dy);
let height = P.f[1].z;
let d = fxs_pulse(r) * height;
var u = vec2<f32>(0.0);
if (r > 1e-9) {
u = vec2<f32>(dx / r, dy / r);
}
var o = sample_bilinear(src, x - u.x * d, y - u.y * d);
if (P.u[0].y != 0u && d != 0.0) {
let slope = (fxs_pulse(r + 0.5) - fxs_pulse(r - 0.5)) * height;
let k = clamp(1.0 - slope * 0.5, 0.0, 2.0);
o = vec4<f32>(o.xyz * k, o.w);
}
textureStore(out, p, o);
return;
}
case 12u: {
let r0 = P.f[2];
let r1 = P.f[3];
let r2 = P.f[4];
var u = r0.x * x + r0.y * y + r0.z;
var v = r1.x * x + r1.y * y + r1.z;
let w = r2.x * x + r2.y * y + r2.z;
if (w != 1.0 && w != 0.0) {
u = u / w;
v = v / w;
}
if (u < -1e-9 || u > 1.0 + 1e-9 || v < -1e-9 || v > 1.0 + 1e-9) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
let rc = P.f[5];
let lx = rc.x + u * rc.z;
let ly = rc.y + v * rc.w;
let o = P.f[6];
s = vec2<f32>(lx * o.x + o.y, ly * o.x + o.z);
}
default: {}
}
if (clamped) {
textureStore(out, p, sample_bilinear_clamped(src, s.x, s.y));
} else {
textureStore(out, p, sample_bilinear(src, s.x, s.y));
}
}
// ---------------------------------------------------------------- CC Flo Motion (distort4::flo_motion)
// One knot's pull at layer point q: f[k] = (knot x, knot y, amount, _).
fn fxs_flo_knot(q: vec2<f32>, k: vec4<f32>) -> vec2<f32> {
if (k.z == 0.0) {
return q;
}
let d = q - k.xy;
let r = sqrt(d.x * d.x + d.y * d.y) / P.f[2].y;
let w = powz(1.0 - min(r, 1.0), P.f[2].x);
let s = max(1.0 - clamp(k.z, -4.0, 0.95) * w, 0.02);
return k.xy + d * s;
}
// u[0] = (supersampling, tile edges); f[0], f[1] = knots; f[2] = (falloff, radius, layer w,
// layer h); f[3] = (scale, buffer offset x, y)
@compute @workgroup_size(16, 16)
fn fxs_flomotion(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let ss = P.u[0].x;
let scale = P.f[3].x;
let off = P.f[3].yz;
let lw = P.f[2].z;
let lh = P.f[2].w;
var acc = vec4<f32>(0.0);
for (var sy = 0u; sy < ss; sy++) {
for (var sx = 0u; sx < ss; sx++) {
let px = f32(p.x) + (f32(sx) + 0.5) / f32(ss);
let py = f32(p.y) + (f32(sy) + 0.5) / f32(ss);
var q = vec2<f32>((px - off.x) / scale, (py - off.y) / scale);
q = fxs_flo_knot(q, P.f[0]);
q = fxs_flo_knot(q, P.f[1]);
if (P.u[0].y != 0u) {
q = vec2<f32>(q.x - floor(q.x / lw) * lw, q.y - floor(q.y / lh) * lh);
}
acc += sample_bilinear(src, q.x * scale + off.x, q.y * scale + off.y);
}
}
textureStore(out, p, acc / f32(ss * ss));
}
// ---------------------------------------------------------------- separable table warps
// The source point is (data[x], data[w + y]) (computed in f64 per column and per row), then
// mixed with the original: out = sample + (orig − sample) · blend (Offset, CC Tiler).
// u[0] = (buffer width, repeat edges); f[0].x = blend
@compute @workgroup_size(16, 16)
fn fxs_table(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let w = P.u[0].x;
let sx = data[u32(p.x)];
let sy = data[w + u32(p.y)];
var s: vec4<f32>;
if (P.u[0].y != 0u) {
s = sample_bilinear_clamped(src, sx, sy);
} else {
s = sample_bilinear(src, sx, sy);
}
let o = textureLoad(src, p, 0);
textureStore(out, p, s + (o - s) * P.f[0].x);
}
// CC Griddler (distort2::cc_griddler). data: per column (tile centre x, local x), per row
// (tile centre y, local y). u[0] = (buffer width, cut tiles); f[0] = (cos, sin, h scale, v scale);
// f[1].x = cut half size (0.45 · tile)
@compute @workgroup_size(16, 16)
fn fxs_griddler(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let w = P.u[0].x;
let x = u32(p.x);
let y = u32(p.y);
let tcx = data[2u * x];
let lx = data[2u * x + 1u];
let tcy = data[2u * w + 2u * y];
let ly = data[2u * w + 2u * y + 1u];
if (P.u[0].y != 0u && max(abs(lx), abs(ly)) > P.f[1].x) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
let cr = P.f[0].x;
let sr = P.f[0].y;
let rx = lx * cr + ly * sr;
let ry = -lx * sr + ly * cr;
textureStore(out, p, sample_bilinear(src, tcx + rx / P.f[0].z, tcy + ry / P.f[0].w));
}
// Motion Tile (stylize2::motion_tile) into a new output size. data: per output column (tile
// index, fraction) of tx, then per row of ty. u[0] = (output width, horizontal phase shift,
// mirror edges); f[0] = (phase, layer w · scale, layer h · scale); f[1].xy = source buffer offset
@compute @workgroup_size(16, 16)
fn fxs_motiontile(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let w = P.u[0].x;
let x = u32(p.x);
let y = u32(p.y);
let ixc = data[2u * x];
let fxc = data[2u * x + 1u];
let iyr = data[2u * w + 2u * y];
let fyr = data[2u * w + 2u * y + 1u];
let phase = P.f[0].x;
var i = ixc;
var fx = fxc;
var j = iyr;
var fy = fyr;
if (P.u[0].y != 0u) {
let t = fxc + phase * iyr;
i = ixc + floor(t);
fx = t - floor(t);
} else {
let t = fyr + phase * ixc;
j = iyr + floor(t);
fy = t - floor(t);
}
if (P.u[0].z != 0u) {
if (fxs_rem2(i32(i)) == 1) {
fx = 1.0 - fx;
}
if (fxs_rem2(i32(j)) == 1) {
fy = 1.0 - fy;
}
}
textureStore(out, p, sample_bilinear(src, fx * P.f[0].y + P.f[1].x, fy * P.f[0].z + P.f[1].y));
}
// CC RepeTile (stylize2::repetile) into a new output size. data: per output column (tile index,
// position in the tile), then per row. u[0] = (output width, tiling); f[0] = layer rect (x0, y0, w, h)
@compute @workgroup_size(16, 16)
fn fxs_repetile(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let w = P.u[0].x;
let x = u32(p.x);
let y = u32(p.y);
let ix = i32(data[2u * x]);
var fx = data[2u * x + 1u];
let iy = i32(data[2u * w + 2u * y]);
var fy = data[2u * w + 2u * y + 1u];
let odd = fxs_rem2(ix + iy) == 1;
var flip_x = false;
var flip_y = false;
switch P.u[0].y {
case 1u: {
flip_x = fxs_rem2(ix) == 1;
flip_y = fxs_rem2(iy) == 1;
}
case 2u: {
flip_x = odd;
}
case 3u: {
flip_y = odd;
}
case 4u: {
flip_x = odd;
flip_y = odd;
}
default: {}
}
let r = P.f[0];
if (flip_x) {
fx = r.z - fx;
}
if (flip_y) {
fy = r.w - fy;
}
textureStore(out, p, sample_bilinear(src, r.x + fx, r.y + fy));
}
// ---------------------------------------------------------------- Magnify (distort2::magnify)
// data: per column (dx, sx, floor(sx) + 0.5), then per row (dy, sy, floor(sy) + 0.5).
// u[0] = (buffer width, square, scaling, blend: 0 none, 1 normal, 2 blend mode);
// u[1] = (blend mode id, seed); f[0] = (size, feather, opacity, magnification); f[1].x = size − feather
@compute @workgroup_size(16, 16)
fn fxs_magnify(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let bw = P.u[0].x;
let x = u32(p.x);
let y = u32(p.y);
let dx = data[3u * x];
let sx = data[3u * x + 1u];
let nx = data[3u * x + 2u];
let dy = data[3u * bw + 3u * y];
let sy = data[3u * bw + 3u * y + 1u];
let ny = data[3u * bw + 3u * y + 2u];
var d: f32;
if (P.u[0].y != 0u) {
d = max(abs(dx), abs(dy));
} else {
d = sqrt(dx * dx + dy * dy);
}
let size = P.f[0].x;
let feather = P.f[0].y;
var w: f32;
if (feather > 0.01) {
w = 1.0 - fxs_smoothstep(P.f[1].x, size, d);
} else {
w = clamp(size - d + 0.5, 0.0, 1.0);
}
w = w * P.f[0].z;
let px = textureLoad(src, p, 0);
let blend = P.u[0].w;
if (w <= 0.0) {
if (blend == 0u) {
textureStore(out, p, vec4<f32>(0.0));
} else {
textureStore(out, p, px);
}
return;
}
var m: vec4<f32>;
switch P.u[0].z {
case 1u: {
m = sample_bicubic(src, sx, sy);
}
case 2u: {
let seed = P.u[1].y;
let k = max(1.0 - 1.0 / P.f[0].w, 0.0);
let j1 = (hash_noise(x, y ^ (1u << 20u), seed) - 0.5) * k;
let j2 = (hash_noise(x, y ^ (2u << 20u), seed) - 0.5) * k;
m = sample_bilinear(src, nx + j1, ny + j2);
}
default: {
m = sample_bilinear(src, nx, ny);
}
}
if (blend == 0u) {
textureStore(out, p, m * w);
} else if (blend == 1u) {
textureStore(out, p, px + (m - px) * w);
} else {
textureStore(out, p, blend_pixel(P.u[1].x, px, m * w, 0.5));
}
}
// ---------------------------------------------------------------- Scatter (stylize2::scatter)
// u[0] = (grain, seed); f[0].x = amount
@compute @workgroup_size(16, 16)
fn fxs_scatter(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let grain = P.u[0].x;
let seed = P.u[0].y;
let amt = P.f[0].x;
let x = u32(p.x);
let y = u32(p.y);
var dx = 0.0;
var dy = 0.0;
if (grain != 2u) {
dx = (hash_noise(x, y, seed) - 0.5) * 2.0 * amt;
}
if (grain != 1u) {
dy = (hash_noise(x, y, seed ^ 0x5bd1u) - 0.5) * 2.0 * amt;
}
textureStore(out, p, tex_get(src, i32(round_away(f32(p.x) + dx)), i32(round_away(f32(p.y) + dy))));
}
// ---------------------------------------------------------------- Brush Strokes (stylize2::brush_strokes)
// data: cell column per x (w), cell row per y (h), then per cell (dx, dy, length, jitter, samples,
// painted). u[0] = (buffer width, cell columns, paint surface, buffer height); f[0].x = blend
@compute @workgroup_size(16, 16)
fn fxs_brush(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let w = P.u[0].x;
let h = P.u[0].w;
let cx = u32(data[u32(p.x)]);
let cy = u32(data[w + u32(p.y)]);
let base = w + h + (cy * P.u[0].y + cx) * 6u;
let dx = data[base];
let dy = data[base + 1u];
let l = data[base + 2u];
let jit = data[base + 3u];
let n = u32(data[base + 4u]);
let m = data[base + 5u];
let q = fxs_centre(p);
let bx = q.x - dy * jit;
let by = q.y + dx * jit;
var acc = vec4<f32>(0.0);
for (var i = 0u; i < n; i++) {
var t = 0.0;
if (n != 1u) {
t = (f32(i) / f32(n - 1u) - 0.5) * l;
}
acc += sample_bilinear_clamped(src, bx + dx * t, by + dy * t);
}
let stroke = acc / f32(n);
let orig = textureLoad(src, p, 0);
let surface = P.u[0].z;
var base_px = orig;
if (surface == 1u) {
base_px = vec4<f32>(0.0);
} else if (surface == 2u) {
base_px = vec4<f32>(1.0);
} else if (surface == 3u) {
base_px = vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
var painted: vec4<f32>;
if (surface == 2u || surface == 3u) {
let k = 1.0 - stroke.w * m;
painted = stroke * m + base_px * k;
} else {
painted = base_px + (stroke - base_px) * m;
}
textureStore(out, p, painted + (orig - painted) * P.f[0].x);
}
// ---------------------------------------------------------------- Roughen Edges (stylize2::roughen_edges)
// noise::fbm.
fn fxs_fbm(u: f32, v: f32, z: f32, seed: u32, octaves0: f32) -> f32 {
let octaves = clamp(octaves0, 1.0, 20.0);
let n = u32(ceil(octaves));
let frac = octaves - floor(octaves);
var sum = 0.0;
var norm = 0.0;
var amp = 1.0;
var f = 1.0;
for (var o = 0u; o < n; o++) {
var w = 1.0;
if (o + 1u == n && frac > 0.0) {
w = frac;
}
sum += value_noise(u * f, v * f, z + f32(o) * 7.31, seed + o) * amp * w;
norm += amp * w;
amp *= 0.5;
f *= 2.0;
}
return sum / max(norm, 1e-6);
}
// aux = the (blurred) alpha in .w. u[0] = (seed, cycle evolution, coloured);
// f[0] = (offset x, offset y, scale · stretch x, scale · stretch y); f[1] = (frequency, evolution,
// cycle, cycle weight); f[2] = (octaves, influence, influence multiplier, sharpness);
// f[3] = (edge colour, sharpness multiplier)
@compute @workgroup_size(16, 16)
fn fxs_roughen(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let px = textureLoad(src, p, 0);
if (px.w <= 0.0) {
textureStore(out, p, px);
return;
}
let dv = textureLoad(aux, p, 0).w;
let fm = P.f[1].x;
let nx = (f32(p.x) + 0.5 - P.f[0].x) / P.f[0].z * fm;
let ny = (f32(p.y) + 0.5 - P.f[0].y) / P.f[0].w * fm;
let seed = P.u[0].x;
let evo = P.f[1].y;
let oct = P.f[2].x;
var n = fxs_fbm(nx, ny, evo, seed, oct);
if (P.u[0].y != 0u) {
n += (fxs_fbm(nx, ny, evo - P.f[1].z, seed, oct) - n) * P.f[1].w;
}
let infl = P.f[2].y;
let s = dv - 0.5 + (n - 0.5) * infl * P.f[2].z * 0.5;
let m = clamp(s * P.f[2].w * P.f[3].w * 4.0 + 0.5, 0.0, 1.0);
let u = fxs_unpremul(px);
var c = u.xyz;
if (P.u[0].z != 0u) {
let band = 1.0 - fxs_smoothstep(0.5, 0.9, dv + (n - 0.5) * infl * 0.5);
c = c + (P.f[3].xyz - c) * band;
}
let a = u.w * m;
textureStore(out, p, vec4<f32>(c * a, a));
}
// ---------------------------------------------------------------- Texturize (stylize2::texturize)
// The texture layer placed into the buffer (transition::place_layer) as luminance
// (Plane::luma). aux = the texture layer's buffer; data: per column x, then per row y in its
// pixels. u[0].x = buffer width
@compute @workgroup_size(16, 16)
fn fxs_tex_luma(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let w = P.u[0].x;
let t = sample_bilinear(aux, data[u32(p.x)], data[w + u32(p.y)]);
let l = luminance(fxs_unpremul(t).xyz);
textureStore(out, p, vec4<f32>(l));
}
// Relief shading. aux = the blurred texture luminance; f[0] = (light x, light y, contrast)
@compute @workgroup_size(16, 16)
fn fxs_texturize(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let gx = (tex_get_clamped(aux, p.x + 1, p.y).x - tex_get_clamped(aux, p.x - 1, p.y).x) * 0.5;
let gy = (tex_get_clamped(aux, p.x, p.y + 1).x - tex_get_clamped(aux, p.x, p.y - 1).x) * 0.5;
let shade = max(1.0 - P.f[0].z * 4.0 * (gx * P.f[0].x + gy * P.f[0].y), 0.0);
let px = textureLoad(src, p, 0);
textureStore(out, p, vec4<f32>(px.xyz * shade, px.w));
}
// ---------------------------------------------------------------- Glow (misc::glow) extras
// Bright pass with the Arbitrary Map: as `glow_bright` (same f[0] / u[0] layout, see
// kernels.wgsl) with the red, green and blue curves at data offsets f[1].xyz (−1 = identity).
@compute @workgroup_size(16, 16)
fn fxs_glow_map(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let px = textureLoad(src, p, 0);
let a = px.w;
if (a <= 0.0) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
let thr = P.f[0].x;
var l = luminance(px.xyz / a);
if (P.u[0].y != 0u) {
l = a;
}
let k = clamp((l - thr) / max(1.0 - thr, 1e-3), 0.0, 1.0);
let t = glow_ab_t(l);
let c = vec3<f32>(curve_eval(P.f[1].x, t), curve_eval(P.f[1].y, t), curve_eval(P.f[1].z, t));
textureStore(out, p, vec4<f32>(c * k * a, k * a));
}
// On Top with another Glow Operation: blend_pixel(mode, original, glow · intensity, 0).
// aux = the blurred glow; u[0].x = blend mode id; f[0].x = intensity
@compute @workgroup_size(16, 16)
fn fxs_glow_op(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
let g = textureLoad(aux, p, 0) * P.f[0].x;
var o = blend_pixel(P.u[0].x, textureLoad(src, p, 0), g, 0.0);
o.w = clamp(o.w, 0.0, 1.0);
textureStore(out, p, o);
}
// ---------------------------------------------------------------- accumulation (Transform motion blur)
// out = src + aux
@compute @workgroup_size(16, 16)
fn fxs_add(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
textureStore(out, p, textureLoad(src, p, 0) + textureLoad(aux, p, 0));
}
// out = src / f[0].x
@compute @workgroup_size(16, 16)
fn fxs_div(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!fxs_inside(p)) {
return;
}
textureStore(out, p, textureLoad(src, p, 0) / P.f[0].x);
}
+729
View File
@@ -0,0 +1,729 @@
// GPU effects (tonal and colour correction family): see src/fx_tone.rs. Each operation mirrors
// the CPU effect in effectcraft-effects operation for operation. Every name here is prefixed
// `fxt_` (the family files share one module).
fn fxt_straight(px: vec4<f32>) -> vec3<f32> {
if (px.w > 1e-6) {
return px.xyz / px.w;
}
return vec3<f32>(0.0);
}
fn fxt_mix(a: vec3<f32>, b: vec3<f32>, t: f32) -> vec3<f32> {
return a + (b - a) * t;
}
// f32::rem_euclid(360.0).
fn fxt_rem360(x: f32) -> f32 {
let r = x - 360.0 * floor(x / 360.0);
return select(r, 0.0, r >= 360.0);
}
// Levels (Individual Controls) channel `i` (data: 5 × (ib, iw, g, ob, ow, identity)).
fn fxt_lv(i: u32, v: f32, clip_b: bool, clip_w: bool) -> f32 {
let o = i * 6u;
if (data[o + 5u] != 0.0) {
return v;
}
var t = (v - data[o]) / max(data[o + 1u] - data[o], 1e-6);
if (clip_b) {
t = max(t, 0.0);
}
if (clip_w) {
t = min(t, 1.0);
}
return data[o + 3u] + (data[o + 4u] - data[o + 3u]) * powz(max(t, 0.0), 1.0 / max(data[o + 2u], 0.01));
}
// color_fx::level (data offset o: ib, iw, g, ob, ow, clip black, clip white).
fn fxt_level(o: u32, v: f32) -> f32 {
var t = (v - data[o]) / max(data[o + 1u] - data[o], 1e-6);
if (data[o + 5u] != 0.0) {
t = max(t, 0.0);
}
if (data[o + 6u] != 0.0) {
t = min(t, 1.0);
}
t = powz(max(t, 0.0), 1.0 / data[o + 2u]);
return data[o + 3u] + (data[o + 4u] - data[o + 3u]) * t;
}
fn fxt_level_ident(o: u32) -> bool {
return data[o] == 0.0 && data[o + 1u] == 1.0 && data[o + 2u] == 1.0 && data[o + 3u] == 0.0 && data[o + 4u] == 1.0;
}
fn fxt_adjust_sat(s: f32, sat: f32) -> f32 {
if (sat >= 0.0) {
return s + (1.0 - s) * sat * min(s, 1.0);
}
return s * (1.0 + sat);
}
fn fxt_adjust_light(l: f32, light: f32) -> f32 {
if (light >= 0.0) {
return l + (1.0 - l) * light;
}
return l * (1.0 + light);
}
// HueRange::weight for range `r` (data: start, end, fall start, fall end, hue, sat, light).
fn fxt_range_weight(r: u32, h: f32) -> f32 {
let o = r * 7u;
let width = fxt_rem360(data[o + 1u] - data[o]);
let d = fxt_rem360(h - data[o]);
if (d <= width) {
return 1.0;
}
let after = d - width;
let before = 360.0 - d;
var wa = 0.0;
if (data[o + 3u] > 0.0) {
wa = 1.0 - after / data[o + 3u];
}
var wb = 0.0;
if (data[o + 2u] > 0.0) {
wb = 1.0 - before / data[o + 2u];
}
return clamp(max(wa, wb), 0.0, 1.0);
}
// color3::colour_match.
fn fxt_colour_match(c: vec3<f32>, key: vec3<f32>, key_hue: f32, mode: u32, tol: f32, soft: f32) -> f32 {
var d: f32;
if (mode == 0u) {
d = sqrt(dot(c - key, c - key)) / sqrt(3.0);
} else if (mode == 1u) {
let hs = rgb_to_hsl(c);
let dh0 = abs(hs.x - key_hue);
let dh = min(dh0, 1.0 - dh0) * 2.0;
d = select(dh, 1.0, hs.y < 0.02);
} else {
let sa = max(c.x + c.y + c.z, 1e-6);
let sk = max(key.x + key.y + key.z, 1e-6);
let a = vec2<f32>(c.x / sa, c.y / sa);
let k = vec2<f32>(key.x / sk, key.y / sk);
d = sqrt(dot(a - k, a - k)) * 1.5;
}
if (d <= tol) {
return 1.0;
}
if (soft <= 1e-6) {
return 0.0;
}
return clamp(1.0 - (d - tol) / soft, 0.0, 1.0);
}
// color3::knee.
fn fxt_knee(v: f32, start: f32, limit: f32) -> f32 {
if (v <= start || limit <= start) {
return min(v, limit);
}
let r = limit - start;
return start + r * (1.0 - exp(-(v - start) / r));
}
// PS Arbitrary Map lookup (map in data, n entries; f[0].x = phase offset in entries).
fn fxt_lookup(v: f32, n: u32) -> f32 {
let nf = f32(n);
var x = clamp(v, 0.0, 1.0) * (nf - 1.0) + P.f[0].x;
x = x - nf * floor(x / nf);
let fl = floor(x);
let i0 = u32(fl) % n;
let i1 = select(i0, i0 + 1u, i0 + 1u < n);
let t = x - fl;
return data[i0] + (data[i1] - data[i0]) * t;
}
fn fxt_eq_look(table: u32, n: u32, v: f32) -> f32 {
let i = min(u32(round_away(clamp(v, 0.0, 1.0) * f32(n - 1u))), n - 1u);
return data[table * n + i];
}
// Per-pixel colour corrections. u[0].x = op (layouts in src/fx_tone.rs):
// 1 Levels (Individual Controls) 2 Gamma/Pedestal/Gain 3 Photo Filter 4 Change to Color
// 5 Leave Color 6 Change Color 7 Broadcast Colors 8 Color Balance (HLS) 9 CC Toner
// 10 CC Color Offset 11 CC Kernel 12 PS Arbitrary Map 13 Video Limiter
// 14 Hue/Saturation with colour ranges 15 Levels with channel controls
// 16 Auto Levels / Contrast / Color apply 17 Equalize apply 18 Invert (HLS / YIQ channels)
// 19 luminance plane 20 Shadow/Highlight (aux = (shadow base, highlight base))
// 21 pack src.x, aux.x 22 Shadow/Highlight clip remap 23 CC Color Neutralizer
// 24 Color Stabilizer (data: per channel n, then 5 × (x, y))
@compute @workgroup_size(16, 16)
fn fxt_point(@builtin(global_invocation_id) gid: vec3<u32>) {
let dims = out_dims();
let p = vec2<i32>(gid.xy);
if (p.x >= dims.x || p.y >= dims.y) {
return;
}
let px = textureLoad(src, p, 0);
let a = px.w;
let u0 = P.u[0];
let f0 = P.f[0];
// Image::map_straight: transparent pixels stay as they are.
let straight_skip = a <= 0.0;
let c = fxt_straight(px);
var o = px;
switch u0.x {
case 1u: {
let cb = u0.y != 0u;
let cw = u0.z != 0u;
// color2::map_ca.
if (a <= 0.0 && px.x == 0.0 && px.y == 0.0 && px.z == 0.0) {
let na = clamp(fxt_lv(4u, 0.0, cb, cw), 0.0, 1.0);
if (na > 0.0) {
o = vec4<f32>(0.0, 0.0, 0.0, clamp(na, 0.0, 1.0));
}
} else {
let aa = max(a, 0.0);
var nc: vec3<f32>;
for (var i = 0u; i < 3u; i++) {
nc[i] = fxt_lv(i + 1u, fxt_lv(0u, c[i], cb, cw), cb, cw);
}
let na = clamp(clamp(fxt_lv(4u, aa, cb, cw), 0.0, 1.0), 0.0, 1.0);
o = vec4<f32>(nc * na, na);
}
}
case 2u: {
if (straight_skip) {
break;
}
let stretch = P.f[3].x;
var r: vec3<f32>;
for (var i = 0; i < 3; i++) {
var v = c[i];
if (stretch > 0.0 && v > 0.0 && v < 1.0) {
let q = 1.0 - v;
v = v + stretch * 0.25 * v * (q * q * q * q);
}
let g = P.f[i];
r[i] = max(g.y + (g.z - g.y) * powz(max(v, 0.0), g.x), 0.0);
}
o = vec4<f32>(r * a, a);
}
case 3u: {
if (straight_skip) {
break;
}
var r = fxt_mix(c, c * f0.xyz, f0.w);
if (u0.y != 0u) {
let l0 = luminance(c);
let l1 = max(luminance(r), 1e-6);
r = r * l0 / l1;
}
o = vec4<f32>(r * a, a);
}
case 4u: {
if (straight_skip) {
break;
}
let fr = f0.xyz;
let tc = P.f[1].xyz;
let tol = P.f[2].xyz;
let soft = f0.w;
let hsl = rgb_to_hsl(c);
let d = vec3<f32>(abs(fract_euclid(hsl.x - fr.x + 0.5) - 0.5) * 2.0, abs(hsl.z - fr.z), abs(hsl.y - fr.y));
var k = 1.0;
for (var i = 0; i < 3; i++) {
let over = d[i] - tol[i];
if (over > 0.0) {
k = min(k, select(clamp(1.0 - over / soft, 0.0, 1.0), 0.0, soft <= 1e-6));
}
}
let ch_l = (u0.y & 1u) != 0u;
let ch_s = (u0.y & 2u) != 0u;
var r = c;
if ((u0.y & 8u) != 0u) {
r = vec3<f32>(k);
} else if (k > 0.0) {
var nh: f32;
var ns = hsl.y;
var nl = hsl.z;
if ((u0.y & 4u) != 0u) {
let dh = (fract_euclid(tc.x - fr.x + 0.5) - 0.5) * k;
nh = fract_euclid(hsl.x + dh);
if (ch_s) {
ns = hsl.y + (tc.y - fr.y) * k;
}
if (ch_l) {
nl = hsl.z + (tc.z - fr.z) * k;
}
} else {
let dh = (fract_euclid(tc.x - hsl.x + 0.5) - 0.5) * k;
nh = fract_euclid(hsl.x + dh);
if (ch_s) {
ns = hsl.y + (tc.y - hsl.y) * k;
}
if (ch_l) {
nl = hsl.z + (tc.z - hsl.z) * k;
}
}
r = hsl_to_rgb(nh, clamp(ns, 0.0, 1.0), clamp(nl, 0.0, 1.0));
}
o = vec4<f32>(r * a, a);
}
case 5u: {
if (straight_skip) {
break;
}
let keep = f0.xyz;
let amt = P.f[1].x;
let tol = P.f[1].y;
let soft = P.f[1].z;
var d: f32;
if (u0.y != 0u) {
let hsl = rgb_to_hsl(c);
d = select(abs(fract_euclid(hsl.x - f0.w + 0.5) - 0.5) * 2.0, 1.0, hsl.y < 0.02);
} else {
d = sqrt(dot(c - keep, c - keep)) / sqrt(3.0);
}
var keepk: f32;
if (tol >= 1.0 || d <= tol) {
keepk = 1.0;
} else if (soft <= 1e-6) {
keepk = 0.0;
} else {
keepk = clamp(1.0 - (d - tol) / soft, 0.0, 1.0);
}
let g = luminance(c);
let r = fxt_mix(c, vec3<f32>(g), amt * (1.0 - keepk));
o = vec4<f32>(r * a, a);
}
case 6u: {
if (a <= 0.0) {
break;
}
var m = fxt_colour_match(c, f0.xyz, f0.w, u0.y, P.f[2].x, P.f[2].y);
if ((u0.z & 2u) != 0u) {
m = 1.0 - m;
}
var r = c;
if ((u0.z & 1u) != 0u) {
r = vec3<f32>(m);
} else if (m > 0.0) {
let hsl = rgb_to_hsl(c);
let dh = P.f[1].x;
let dl = P.f[1].y;
let ds = P.f[1].z;
let h2 = fract_euclid(hsl.x + dh);
let s2 = clamp(hsl.y + ds * select(hsl.y, 1.0 - hsl.y, ds > 0.0), 0.0, 1.0);
let l2 = clamp(hsl.z + dl * select(hsl.z, 1.0 - hsl.z, dl > 0.0), 0.0, 1.0);
r = fxt_mix(c, hsl_to_rgb(h2, s2, l2), m);
}
o = vec4<f32>(r * a, a);
}
case 7u: {
if (a <= 0.0) {
break;
}
let pal = u0.y != 0u;
let y = 0.299 * c.x + 0.587 * c.y + 0.114 * c.z;
var ch: f32;
if (pal) {
let uu = 0.492 * (c.z - y);
let vv = 0.877 * (c.x - y);
ch = sqrt(uu * uu + vv * vv);
} else {
let ii = 0.596 * c.x - 0.274 * c.y - 0.322 * c.z;
let qq = 0.211 * c.x - 0.523 * c.y + 0.312 * c.z;
ch = sqrt(ii * ii + qq * qq);
}
let max_norm = f0.x;
let unsafe_px = y + ch > max_norm + 1e-5;
switch u0.z {
case 2u: {
if (unsafe_px) {
o = vec4<f32>(0.0);
}
}
case 3u: {
if (!unsafe_px) {
o = vec4<f32>(0.0);
}
}
case 1u: {
if (unsafe_px) {
let target_v = max(max_norm - y, 0.0);
let s = select(1.0, target_v / ch, ch > 1e-6);
var r = y + (c - y) * s;
if (y > max_norm) {
r = r * max_norm / y;
}
o = vec4<f32>(r * a, a);
}
}
default: {
if (unsafe_px) {
let k = max_norm / (y + ch);
o = vec4<f32>(c * k * a, a);
}
}
}
}
case 8u: {
if (straight_skip) {
break;
}
let hsl = rgb_to_hsl(c);
let dh = f0.x;
let dl = f0.y;
let ds = f0.z;
var l = hsl.z;
if (dl > 0.0) {
l = l + (1.0 - l) * dl;
} else {
l = l + l * dl;
}
var s = hsl.y;
if (ds > 0.0) {
s = s + (1.0 - s) * ds;
} else {
s = s + s * ds;
}
let r = hsl_to_rgb(hsl.x + dh, clamp(s, 0.0, 1.0), clamp(l, 0.0, 1.0));
o = vec4<f32>(r * a, a);
}
case 9u: {
if (straight_skip) {
break;
}
let n = u0.y;
let t = clamp(luminance(c), 0.0, 1.0) * f32(n);
let k = min(u32(floor(t)), n - 1u);
let tone = fxt_mix(P.f[1u + k].xyz, P.f[2u + k].xyz, t - f32(k));
let r = fxt_mix(tone, c, f0.x);
o = vec4<f32>(r * a, a);
}
case 10u: {
if (straight_skip) {
break;
}
var r = c;
for (var k = 0; k < 3; k++) {
if (f0[k] == 0.0) {
continue;
}
let x = c[k] + f0[k];
if (x <= 1.0) {
r[k] = x;
} else if (u0.y == 1u) {
r[k] = max(2.0 - x, 0.0);
} else if (u0.y == 2u) {
r[k] = select(1.0, 0.0, x - 1.0 < 0.5);
} else {
r[k] = fract_euclid(x);
}
}
o = vec4<f32>(r * a, a);
}
case 11u: {
if (a <= 0.0) {
break;
}
var acc = vec3<f32>(0.0);
for (var j = 0; j < 3; j++) {
for (var i = 0; i < 3; i++) {
let idx = j * 3 + i;
let w = P.f[idx / 4][idx % 4];
if (w == 0.0) {
continue;
}
let q = tex_get_clamped(src, p.x + i - 1, p.y + j - 1);
acc += fxt_straight(q) * w;
}
}
var v = acc * P.f[2].y + P.f[2].z;
if (u0.y != 0u) {
v = abs(v);
}
o = vec4<f32>(max(v, vec3<f32>(0.0)) * a, a);
}
case 12u: {
let n = u0.z;
let aa = max(a, 0.0);
let r = vec3<f32>(fxt_lookup(c.x, n), fxt_lookup(c.y, n), fxt_lookup(c.z, n));
var na = aa;
if (u0.y != 0u) {
na = fxt_lookup(aa, n);
}
o = vec4<f32>(r * na, na);
}
case 13u: {
if (a <= 0.0) {
break;
}
let level = f0.x;
let start = f0.y;
let y = luminance(c);
var r = c;
if (u0.y != 1u) {
let y2 = fxt_knee(max(y, 0.0), start, level);
r = r - y + y2;
}
if (u0.y != 0u) {
let yl = luminance(r);
var t = 1.0;
for (var i = 0; i < 3; i++) {
let v = r[i];
if (v > level && v - yl > 1e-6) {
t = min(t, (level - yl) / (v - yl));
}
if (v < 0.0 && yl - v > 1e-6) {
t = min(t, yl / (yl - v));
}
}
t = clamp(t, 0.0, 1.0);
r = yl + (r - yl) * t;
}
r = clamp(r, vec3<f32>(0.0), vec3<f32>(level));
if (u0.z != 0u && any(abs(c - r) > vec3<f32>(1e-4))) {
r = P.f[1].xyz;
}
o = vec4<f32>(r * a, a);
}
case 14u: {
if (straight_skip) {
break;
}
let hsl = rgb_to_hsl(c);
var h = hsl.x;
var s = hsl.y;
var l = hsl.z;
if (u0.y != 0u) {
let cl = P.f[1].z;
h = P.f[1].x;
s = P.f[1].y;
l = clamp(l + cl * select(l, 1.0 - l, cl > 0.0), 0.0, 1.0);
} else {
var s2 = fxt_adjust_sat(s, f0.y);
var l2 = fxt_adjust_light(l, f0.z);
var h2 = h + f0.x;
for (var r = 0u; r < u0.z; r++) {
let w = fxt_range_weight(r, hsl.x * 360.0) * min(hsl.y * 20.0, 1.0);
if (w > 0.0) {
let ro = r * 7u;
h2 += data[ro + 4u] * w;
s2 = fxt_adjust_sat(clamp(s2, 0.0, 1.0), data[ro + 5u] * w);
l2 = fxt_adjust_light(l2, data[ro + 6u] * w);
}
}
h = fract_euclid(h2);
s = clamp(s2, 0.0, 1.0);
l = l2;
}
o = vec4<f32>(hsl_to_rgb(h, s, l) * a, a);
}
case 15u: {
var r = c;
if (!straight_skip) {
for (var i = 0u; i < 3u; i++) {
r[i] = fxt_level(0u, c[i]);
}
}
// The master pass writes premultiplied values; the channel pass reads them back.
var q = px;
if (!straight_skip) {
q = vec4<f32>(r * a, a);
}
let qc = fxt_straight(q);
let qa = max(q.w, 0.0);
var any_ch = false;
for (var k = 0u; k < 4u; k++) {
if (!fxt_level_ident(7u + 7u * k)) {
any_ch = true;
}
}
if (!any_ch) {
o = q;
break;
}
var oc = qc;
for (var i = 0u; i < 3u; i++) {
let off = 7u + 7u * i;
if (!fxt_level_ident(off)) {
oc[i] = fxt_level(off, qc[i]);
}
}
var na = qa;
if (!fxt_level_ident(28u)) {
na = clamp(fxt_level(28u, qa), 0.0, 1.0);
}
o = vec4<f32>(oc * na, na);
}
case 16u: {
if (a <= 0.0 && px.x == 0.0 && px.y == 0.0 && px.z == 0.0) {
break;
}
let lo = f0.xyz;
let hi = P.f[1].xyz;
let g = P.f[2];
var r: vec3<f32>;
for (var k = 0; k < 3; k++) {
let st = clamp((c[k] - lo[k]) / (hi[k] - lo[k]), 0.0, 1.0);
r[k] = powz(st, g[k]);
}
r = fxt_mix(r, c, g.w);
let na = clamp(max(a, 0.0), 0.0, 1.0);
o = vec4<f32>(r * na, na);
}
case 17u: {
if (a <= 0.0 && px.x == 0.0 && px.y == 0.0 && px.z == 0.0) {
break;
}
let n = u0.z;
let amt = f0.x;
var r: vec3<f32>;
if (u0.y == 1u) {
let l = luminance(c);
let nl = fxt_eq_look(0u, n, l);
if (l > 1e-5) {
r = c * nl / l;
} else {
r = vec3<f32>(nl);
}
} else if (u0.y == 2u) {
r = vec3<f32>(fxt_eq_look(0u, n, c.x), fxt_eq_look(0u, n, c.y), fxt_eq_look(0u, n, c.z));
} else {
r = vec3<f32>(fxt_eq_look(0u, n, c.x), fxt_eq_look(1u, n, c.y), fxt_eq_look(2u, n, c.z));
}
r = fxt_mix(c, r, amt);
let na = clamp(max(a, 0.0), 0.0, 1.0);
o = vec4<f32>(r * na, na);
}
case 18u: {
if (a <= 0.0) {
break;
}
let ch = u0.y;
var r: vec3<f32>;
if (ch <= 7u) {
var hsl = rgb_to_hsl(c);
if (ch == 4u || ch == 5u) {
hsl.x = 1.0 - hsl.x;
}
if (ch == 4u || ch == 6u) {
hsl.z = 1.0 - hsl.z;
}
if (ch == 4u || ch == 7u) {
hsl.y = 1.0 - hsl.y;
}
r = hsl_to_rgb(hsl.x, hsl.y, hsl.z);
} else {
var q = vec3<f32>(
0.299 * c.x + 0.587 * c.y + 0.114 * c.z,
0.596 * c.x - 0.274 * c.y - 0.322 * c.z,
0.211 * c.x - 0.523 * c.y + 0.312 * c.z,
);
if (ch == 8u || ch == 9u) {
q.x = 1.0 - q.x;
}
if (ch == 8u || ch == 10u) {
q.y = -q.y;
}
if (ch == 8u || ch == 11u) {
q.z = -q.z;
}
r = max(
vec3<f32>(q.x + 0.956 * q.y + 0.621 * q.z, q.x - 0.272 * q.y - 0.647 * q.z, q.x - 1.106 * q.y + 1.703 * q.z),
vec3<f32>(0.0),
);
}
o = vec4<f32>((c + (r - c) * f0.x) * a, a);
}
case 19u: {
o = vec4<f32>(luminance(c));
}
case 20u: {
if (a <= 0.0) {
break;
}
let bases = textureLoad(aux, p, 0);
let s_amt = f0.x;
let h_amt = f0.y;
let ws0 = clamp(1.0 - bases.x / f0.z, 0.0, 1.0);
let wh0 = clamp((bases.y - (1.0 - f0.w)) / f0.w, 0.0, 1.0);
let ws = ws0 * ws0;
let wh = wh0 * wh0;
let gain = (1.0 + s_amt * ws * 2.0) * (1.0 - h_amt * wh * 0.5);
let l = luminance(c);
let nl = l * gain;
let sat = pow(max(gain, 1e-3), P.f[1].x);
var r = nl + (c - l) * sat;
let mc = P.f[1].y;
if (mc != 0.0) {
let shift = (0.5 + (nl - 0.5) * (1.0 + mc)) - nl;
r = r + shift;
}
r = fxt_mix(max(r, vec3<f32>(0.0)), c, P.f[1].z);
o = vec4<f32>(r * a, a);
}
case 21u: {
o = vec4<f32>(px.x, textureLoad(aux, p, 0).x, 0.0, 0.0);
}
case 22u: {
if (a <= 0.0 && px.x == 0.0 && px.y == 0.0 && px.z == 0.0) {
break;
}
let r = max(f0.x + (c - f0.y) * f0.z, vec3<f32>(0.0));
let na = clamp(max(a, 0.0), 0.0, 1.0);
o = vec4<f32>(r * na, na);
}
case 23u: {
if (a <= 0.0) {
break;
}
let l = clamp(luminance(c), 0.0, 1.0);
let ws = 1.0 - fxt_smoothstep(0.0, 0.5, l);
let wh = fxt_smoothstep(0.5, 1.0, l);
let wm = 1.0 - ws - wh;
let pin = P.f[3].x;
let keep = 1.0 - pin * clamp(1.0 - 4.0 * l * (1.0 - l), 0.0, 1.0);
let contrast = P.f[3].z;
let darks = P.f[4].x;
let brights = P.f[4].y;
var r: vec3<f32>;
for (var k = 0; k < 3; k++) {
let d = P.f[0][k] * ws + P.f[1][k] * wm + P.f[2][k] * wh;
var v = c[k] + d * keep;
v = 0.5 + (v - 0.5) * (1.0 + contrast);
v += darks * 0.25 * (1.0 - l) * (1.0 - l) + brights * 0.25 * l * l;
r[k] = max(v, 0.0);
}
r = fxt_mix(r, c, P.f[3].y);
o = vec4<f32>(r * a, a);
}
case 24u: {
if (a <= 0.0) {
break;
}
var r: vec3<f32>;
for (var k = 0u; k < 3u; k++) {
let base = k * 11u;
let n = u32(data[base]);
let v = c[k];
// slice::partition_point(x < v), clamped to 1..n-1.
var i = 0u;
while (i < n && data[base + 1u + 2u * i] < v) {
i++;
}
i = clamp(i, 1u, n - 1u);
let ax = data[base + 1u + 2u * (i - 1u)];
let ay = data[base + 2u + 2u * (i - 1u)];
let cx = data[base + 1u + 2u * i];
let cy = data[base + 2u + 2u * i];
r[k] = ay + (cy - ay) * (v - ax) / max(cx - ax, 1e-9);
}
o = vec4<f32>(r * a, a);
}
default: {}
}
textureStore(out, p, o);
}
// util::smoothstep.
fn fxt_smoothstep(e0: f32, e1: f32, x: f32) -> f32 {
if (abs(e1 - e0) < 1e-9) {
return select(1.0, 0.0, x < e0);
}
let t = clamp((x - e0) / (e1 - e0), 0.0, 1.0);
return t * t * (3.0 - 2.0 * t);
}
+115
View File
@@ -844,3 +844,118 @@ fn adjust_mix(@builtin(global_invocation_id) gid: vec3<u32>) {
let k = data[(gid.y * P.u[0].x + gid.x) * 4u + 3u] * P.f[0].x;
textureStore(out, p, c + (a - c) * k);
}
// ---------------------------------------------------------------- Classic 3D bokeh depth of field
// (render::three_d::bokeh: boost_highlights, row prefix sums, span gathers per blur level
// blended by each pixel's radius.)
// Highlight boost: f[0] = (gain, threshold, saturation).
@compute @workgroup_size(16, 16)
fn bokeh_boost(@builtin(global_invocation_id) gid: vec3<u32>) {
let dims = out_dims();
let p = vec2<i32>(gid.xy);
if (p.x >= dims.x || p.y >= dims.y) {
return;
}
var c = textureLoad(src, p, 0);
let gain = P.f[0].x;
let thr = clamp(P.f[0].y, 0.0, 1.0);
let satk = P.f[0].z;
if (c.w > 0.0 && gain > 0.0) {
let a = c.w;
let rgb = c.xyz / a;
let l = 0.2126 * rgb.x + 0.7152 * rgb.y + 0.0722 * rgb.z;
if (!(l < thr || (thr >= 1.0 && l < 1.0))) {
var over = 1.0;
if (thr < 1.0) {
over = clamp((l - thr) / (1.0 - thr), 0.0, 1.0);
}
let k = 1.0 + gain * 4.0 * max(over, 0.25);
let sat = 1.0 + satk;
let o = max(vec3<f32>(l) + (rgb - vec3<f32>(l)) * sat, vec3<f32>(0.0)) * k;
c = vec4<f32>(o * a, a);
}
}
textureStore(out, p, c);
}
// Row prefix sums: out is (w + 1) × h, out[x] = sum of src[0..x]. One invocation per row.
@compute @workgroup_size(64, 1)
fn bokeh_prefix(@builtin(global_invocation_id) gid: vec3<u32>) {
let dims = vec2<i32>(textureDimensions(src));
let y = i32(gid.x);
if (y >= dims.y) {
return;
}
var acc = vec4<f32>(0.0);
textureStore(out, vec2<i32>(0, y), acc);
for (var x = 0; x < dims.x; x = x + 1) {
acc = acc + textureLoad(src, vec2<i32>(x, y), 0);
textureStore(out, vec2<i32>(x + 1, y), acc);
}
}
fn bokeh_prefix_at(y: i32, t: f32) -> vec4<f32> {
let n = i32(textureDimensions(src).x) - 1;
if (t <= 0.0) {
return vec4<f32>(0.0);
}
if (t >= f32(n)) {
return textureLoad(src, vec2<i32>(n, y), 0);
}
let i = i32(floor(t));
let f = t - f32(i);
let a = textureLoad(src, vec2<i32>(i, y), 0);
let b = textureLoad(src, vec2<i32>(i + 1, y), 0);
return a + (b - a) * f;
}
// One blur level added into the accumulator `aux`: src = prefix sums; data = the radius map
// (w·h) then spans (dy, x0, x1, weight). u[0] = (span offset, span count, level, levels),
// u[1].x = 1 for the first level (no accumulator yet); f[0] = (1 / norm, lo, hi).
@compute @workgroup_size(16, 16)
fn bokeh_gather(@builtin(global_invocation_id) gid: vec3<u32>) {
let dims = out_dims();
let p = vec2<i32>(gid.xy);
if (p.x >= dims.x || p.y >= dims.y) {
return;
}
var acc = vec4<f32>(0.0);
if (P.u[1].x == 0u) {
acc = textureLoad(aux, p, 0);
}
let r = data[u32(p.y) * u32(dims.x) + u32(p.x)];
let k = P.u[0].z;
let n = P.u[0].w;
var wgt = 1.0;
if (n > 1u) {
let lo = P.f[0].y;
let hi = P.f[0].z;
let f = clamp((r - lo) / (hi - lo) * f32(n - 1u), 0.0, f32(n - 1u));
let kk = min(u32(floor(f)), n - 2u);
let t = f - f32(kk);
if (k == kk) {
wgt = 1.0 - t;
} else if (k == kk + 1u) {
wgt = t;
} else {
wgt = 0.0;
}
}
if (wgt > 0.0) {
var g = vec4<f32>(0.0);
let off = P.u[0].x;
for (var i = 0u; i < P.u[0].y; i = i + 1u) {
let s = off + i * 4u;
let sy = p.y + i32(data[s]);
if (sy < 0 || sy >= dims.y) {
continue;
}
let hi = bokeh_prefix_at(sy, f32(p.x) + data[s + 2u] + 0.5);
let lo = bokeh_prefix_at(sy, f32(p.x) + data[s + 1u] + 0.5);
g = g + (hi - lo) * data[s + 3u];
}
acc = acc + g * P.f[0].x * wgt;
}
textureStore(out, p, acc);
}