M12.2: gpu compositor and gpu effects

New L3 crate effectcraft-gpu: a wgpu compute backend for the per-frame
compositing walk (layer transforms with the CPU's nearest/bilinear/bicubic
sampling and minification pre-filter, motion-blur accumulation, track
mattes, Preserve Transparency, layer style passes and knockout, all 38
blend modes as a WGSL port of color::blend, 8/16 bpc clamp+quantise,
colour-space conversions). 3D runs, adjustment and wireframe layers run on
the CPU between GPU steps. GPU effects with the CPU semantics: Gaussian,
Fast Box and Directional Blur, Glow, Levels, Curves, Hue/Saturation, Tint,
Fill, Gradient Ramp, Fractal Noise, Drop Shadow, Brightness & Contrast,
Exposure, Invert, Transform (registry gpu badge from effects::GPU_EFFECTS).

render: Backend (Cpu/Gpu/Auto) in RenderOpts, the Accelerator trait and
Renderer::accel, GPU effect chains in the effect stack, hooks for the GPU
walk. Project setting gpu_acceleration (Mercury GPU Acceleration / Software
Only), render.backend command, file.projectSettings renderer. Session and
Render Queue exports carry the accelerator. CLI --gpu and bench --gpu.

Tests compare GPU vs CPU renders (blend modes x bit depths, transforms,
masks, mattes, motion blur, colour management, styles, collapse,
adjustment fallback, every GPU effect) and skip without an adapter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Brandon Thomas
2026-10-02 10:59:08 -04:00
co-authored by Claude Opus 5.5
parent ab3b4cfb70
commit 485f562e19
30 changed files with 3625 additions and 62 deletions
+452
View File
@@ -0,0 +1,452 @@
// EffectCraft GPU compositor: shared bindings and helpers.
//
// Every kernel uses the same bind group: a uniform block of parameters, two input textures
// (premultiplied RGBA f32, read with textureLoad), one output storage texture and a read-only
// storage buffer of extra data (curve tables). Each helper mirrors a CPU function in
// effectcraft-raster / effectcraft-color operation for operation, so results match the CPU
// reference to float rounding.
struct Params {
u: array<vec4<u32>, 4>,
f: array<vec4<f32>, 12>,
}
@group(0) @binding(0) var<uniform> P: Params;
@group(0) @binding(1) var src: texture_2d<f32>;
@group(0) @binding(2) var aux: texture_2d<f32>;
@group(0) @binding(4) var<storage, read> data: array<f32>;
// ---------------------------------------------------------------- sampling (raster::Image)
fn tex_get(t: texture_2d<f32>, x: i32, y: i32) -> vec4<f32> {
let d = vec2<i32>(textureDimensions(t));
if (x < 0 || y < 0 || x >= d.x || y >= d.y) {
return vec4<f32>(0.0);
}
return textureLoad(t, vec2<i32>(x, y), 0);
}
fn tex_get_clamped(t: texture_2d<f32>, x: i32, y: i32) -> vec4<f32> {
let d = vec2<i32>(textureDimensions(t));
return textureLoad(t, vec2<i32>(clamp(x, 0, d.x - 1), clamp(y, 0, d.y - 1)), 0);
}
// Image::sample_bilinear (pixel centres at +0.5; transparent outside).
fn sample_bilinear(t: texture_2d<f32>, x: f32, y: f32) -> vec4<f32> {
let fx = x - 0.5;
let fy = y - 0.5;
let x0 = floor(fx);
let y0 = floor(fy);
let tx = fx - x0;
let ty = fy - y0;
let xi = i32(x0);
let yi = i32(y0);
let a = tex_get(t, xi, yi);
let b = tex_get(t, xi + 1, yi);
let c = tex_get(t, xi, yi + 1);
let d = tex_get(t, xi + 1, yi + 1);
let top = a + (b - a) * tx;
let bot = c + (d - c) * tx;
return top + (bot - top) * ty;
}
// Image::sample_bilinear_clamped (edge pixels repeat).
fn sample_bilinear_clamped(t: texture_2d<f32>, x: f32, y: f32) -> vec4<f32> {
let fx = x - 0.5;
let fy = y - 0.5;
let x0 = floor(fx);
let y0 = floor(fy);
let tx = fx - x0;
let ty = fy - y0;
let xi = i32(x0);
let yi = i32(y0);
let a = tex_get_clamped(t, xi, yi);
let b = tex_get_clamped(t, xi + 1, yi);
let c = tex_get_clamped(t, xi, yi + 1);
let d = tex_get_clamped(t, xi + 1, yi + 1);
let top = a + (b - a) * tx;
let bot = c + (d - c) * tx;
return top + (bot - top) * ty;
}
fn cubic_w(t: f32) -> vec4<f32> {
let t2 = t * t;
let t3 = t2 * t;
return vec4<f32>(-0.5 * t3 + t2 - 0.5 * t, 1.5 * t3 - 2.5 * t2 + 1.0, -1.5 * t3 + 2.0 * t2 + 0.5 * t, 0.5 * t3 - 0.5 * t2);
}
// Image::sample_bicubic (Catmull-Rom; negatives clamped after ringing).
fn sample_bicubic(t: texture_2d<f32>, x: f32, y: f32) -> vec4<f32> {
let fx = x - 0.5;
let fy = y - 0.5;
let x0 = floor(fx);
let y0 = floor(fy);
let wx = cubic_w(fx - x0);
let wy = cubic_w(fy - y0);
let xi = i32(x0);
let yi = i32(y0);
var o = vec4<f32>(0.0);
for (var j = 0; j < 4; j++) {
for (var i = 0; i < 4; i++) {
let p = tex_get(t, xi - 1 + i, yi - 1 + j);
o += p * (wx[i] * wy[j]);
}
}
return max(o, vec4<f32>(0.0));
}
// 0 nearest, 1 bilinear, 2 bicubic (raster::Sampling).
fn sample(t: texture_2d<f32>, sampling: u32, x: f32, y: f32) -> vec4<f32> {
if (sampling == 0u) {
return tex_get(t, i32(floor(x)), i32(floor(y)));
}
if (sampling == 2u) {
return sample_bicubic(t, x, y);
}
return sample_bilinear(t, x, y);
}
// raster::hash_noise.
fn hash_noise(x: u32, y: u32, seed: u32) -> f32 {
var h = (x * 0x8da6b343u) ^ (y * 0xd8163841u) ^ (seed * 0xcb1ab31fu);
h = h ^ (h >> 13u);
h = h * 0x5bd1e995u;
h = h ^ (h >> 15u);
return f32(h & 0x00ffffffu) / 16777216.0;
}
// powf for x >= 0 (0^y = 0 for y > 0; WGSL leaves pow(0, y) to the implementation).
fn powz(x: f32, y: f32) -> f32 {
if (x <= 0.0) {
return select(0.0, 1.0, y == 0.0);
}
return pow(x, y);
}
// Rust's f32::round (half away from zero; WGSL's round() is half to even).
fn round_away(x: f32) -> f32 {
let t = trunc(x);
if (abs(x - t) >= 0.5) {
return t + sign(x);
}
return t;
}
// f32::rem_euclid(1.0).
fn fract_euclid(x: f32) -> f32 {
return x - floor(x);
}
fn luminance(c: vec3<f32>) -> f32 {
return 0.2126 * c.x + 0.7152 * c.y + 0.0722 * c.z;
}
// ---------------------------------------------------------------- blend modes (color::blend)
// Mode numbers are indices into BlendMode::ALL.
fn burn(cb: f32, cs: f32) -> f32 {
if (cb >= 1.0) {
return 1.0;
}
if (cs <= 0.0) {
return 0.0;
}
return 1.0 - min((1.0 - cb) / cs, 1.0);
}
fn dodge(cb: f32, cs: f32) -> f32 {
if (cb <= 0.0) {
return 0.0;
}
if (cs >= 1.0) {
return 1.0;
}
return min(cb / (1.0 - cs), 1.0);
}
fn hard_light(cb: f32, cs: f32) -> f32 {
if (cs <= 0.5) {
return cb * 2.0 * cs;
}
let s = 2.0 * cs - 1.0;
return cb + s - cb * s;
}
fn soft_light(cb: f32, cs: f32) -> f32 {
if (cs <= 0.5) {
return cb - (1.0 - 2.0 * cs) * cb * (1.0 - cb);
}
var d: f32;
if (cb <= 0.25) {
d = ((16.0 * cb - 12.0) * cb + 4.0) * cb;
} else {
d = sqrt(max(cb, 0.0));
}
return cb + (2.0 * cs - 1.0) * (d - cb);
}
fn vivid(cb: f32, cs: f32) -> f32 {
if (cs <= 0.5) {
return burn(cb, 2.0 * cs);
}
return dodge(cb, 2.0 * (cs - 0.5));
}
fn separable(mode: u32, cb: f32, cs: f32) -> f32 {
switch mode {
case 3u: { return min(cb, cs); }
case 4u: { return cb * cs; }
case 5u: { return burn(cb, cs); }
case 6u: {
if (cs <= 0.0) {
return 0.0;
}
return max(1.0 - (1.0 - cb) / cs, 0.0);
}
case 7u: { return max(cb + cs - 1.0, 0.0); }
case 9u, 14u: { return cb + cs; }
case 10u: { return max(cb, cs); }
case 11u: { return cb + cs - cb * cs; }
case 12u: { return dodge(cb, cs); }
case 13u: {
if (cs >= 1.0) {
return 1.0;
}
return min(cb / (1.0 - cs), 1.0);
}
case 16u: { return hard_light(cs, cb); }
case 17u: { return soft_light(cb, cs); }
case 18u: { return hard_light(cb, cs); }
case 19u: { return clamp(cb + 2.0 * cs - 1.0, 0.0, 1.0); }
case 20u: { return vivid(cb, cs); }
case 21u: {
if (cs <= 0.5) {
return min(cb, 2.0 * cs);
}
return max(cb, 2.0 * cs - 1.0);
}
case 22u: {
if (cb + cs >= 1.0) {
return 1.0;
}
return 0.0;
}
case 23u, 24u: { return abs(cb - cs); }
case 25u: { return cb + cs - 2.0 * cb * cs; }
case 26u: { return max(cb - cs, 0.0); }
case 27u: {
if (cs <= 0.0) {
if (cb > 0.0) {
return 1.0;
}
return 0.0;
}
return cb / cs;
}
default: { return cs; }
}
}
fn blum(c: vec3<f32>) -> f32 {
return 0.3 * c.x + 0.59 * c.y + 0.11 * c.z;
}
fn clip_color(c: vec3<f32>) -> vec3<f32> {
let l = blum(c);
let n = min(min(c.x, c.y), c.z);
let x = max(max(c.x, c.y), c.z);
var o = c;
if (n < 0.0) {
o = l + (o - l) * l / max(l - n, 1e-9);
}
if (x > 1.0) {
o = l + (o - l) * (1.0 - l) / max(x - l, 1e-9);
}
return o;
}
fn set_lum(c: vec3<f32>, l: f32) -> vec3<f32> {
let d = l - blum(c);
return clip_color(c + d);
}
fn bsat(c: vec3<f32>) -> f32 {
return max(max(c.x, c.y), c.z) - min(min(c.x, c.y), c.z);
}
fn set_sat(c: vec3<f32>, s: f32) -> vec3<f32> {
let mx = max(max(c.x, c.y), c.z);
let mn = min(min(c.x, c.y), c.z);
if (mx - mn <= 1e-9) {
return vec3<f32>(0.0);
}
return (c - mn) * s / (mx - mn);
}
fn non_separable(mode: u32, cb: vec3<f32>, cs: vec3<f32>) -> vec3<f32> {
switch mode {
case 28u: { return set_lum(set_sat(cs, bsat(cb)), blum(cb)); }
case 29u: { return set_lum(set_sat(cb, bsat(cs)), blum(cb)); }
case 30u: { return set_lum(cs, blum(cb)); }
case 31u: { return set_lum(cb, blum(cs)); }
case 8u: {
if (luminance(cs) < luminance(cb)) {
return cs;
}
return cb;
}
case 15u: {
if (luminance(cs) > luminance(cb)) {
return cs;
}
return cb;
}
default: {
return vec3<f32>(separable(mode, cb.x, cs.x), separable(mode, cb.y, cs.y), separable(mode, cb.z, cs.z));
}
}
}
fn supports_hdr(mode: u32) -> bool {
switch mode {
case 0u, 1u, 2u, 3u, 4u, 7u, 8u, 9u, 10u, 11u, 14u, 15u, 23u, 24u, 26u, 27u, 32u, 33u, 34u, 35u, 36u, 37u: { return true; }
default: { return false; }
}
}
fn is_stencil(mode: u32) -> bool {
return mode >= 32u && mode <= 35u;
}
// color::blend_pixel.
fn blend_pixel(mode: u32, dst: vec4<f32>, src: vec4<f32>, noise: f32) -> vec4<f32> {
let sa = src.w;
let da = dst.w;
switch mode {
case 0u: {
let k = 1.0 - sa;
return vec4<f32>(src.xyz + dst.xyz * k, sa + da * k);
}
case 1u, 2u: {
if (sa <= 0.0 || noise >= sa) {
return dst;
}
let inv = 1.0 / sa;
return vec4<f32>(src.xyz * inv, 1.0);
}
case 32u: { return dst * sa; }
case 34u: { return dst * (1.0 - sa); }
case 33u: { return dst * luminance(src.xyz); }
case 35u: { return dst * (1.0 - luminance(src.xyz)); }
case 36u: { return vec4<f32>(src.xyz + dst.xyz * (1.0 - sa), min(sa + da, 1.0)); }
case 37u: { return vec4<f32>(src.xyz + dst.xyz * (1.0 - sa), sa + da - sa * da); }
default: {}
}
if (sa <= 0.0) {
return dst;
}
if (da <= 0.0) {
return src;
}
var cs = src.xyz / sa;
var cb = dst.xyz / da;
if (!supports_hdr(mode)) {
cs = clamp(cs, vec3<f32>(0.0), vec3<f32>(1.0));
cb = clamp(cb, vec3<f32>(0.0), vec3<f32>(1.0));
}
let b = non_separable(mode, cb, cs);
let ao = sa + da - sa * da;
return vec4<f32>((1.0 - da) * src.xyz + (1.0 - sa) * dst.xyz + sa * da * b, ao);
}
// ---------------------------------------------------------------- colour (color crate)
fn srgb_to_linear(v: f32) -> f32 {
if (v <= 0.04045) {
return v / 12.92;
}
return pow((v + 0.055) / 1.055, 2.4);
}
fn linear_to_srgb(v: f32) -> f32 {
if (v <= 0.0031308) {
return v * 12.92;
}
return 1.055 * pow(max(v, 0.0), 1.0 / 2.4) - 0.055;
}
fn rgb_to_hsl(c: vec3<f32>) -> vec3<f32> {
let mx = max(max(c.x, c.y), c.z);
let mn = min(min(c.x, c.y), c.z);
let l = (mx + mn) * 0.5;
if (abs(mx - mn) < 1e-7) {
return vec3<f32>(0.0, 0.0, l);
}
let d = mx - mn;
var s: f32;
if (l > 0.5) {
s = d / (2.0 - mx - mn);
} else {
s = d / (mx + mn);
}
var h: f32;
if (mx == c.x) {
h = ((c.y - c.z) / d + select(0.0, 6.0, c.y < c.z)) / 6.0;
} else if (mx == c.y) {
h = ((c.z - c.x) / d + 2.0) / 6.0;
} else {
h = ((c.x - c.y) / d + 4.0) / 6.0;
}
return vec3<f32>(h, s, l);
}
fn hsl_channel(t0: f32, p: f32, q: f32) -> f32 {
let t = fract_euclid(t0);
if (t < 1.0 / 6.0) {
return p + (q - p) * 6.0 * t;
}
if (t < 0.5) {
return q;
}
if (t < 2.0 / 3.0) {
return p + (q - p) * (2.0 / 3.0 - t) * 6.0;
}
return p;
}
fn hsl_to_rgb(h: f32, s: f32, l: f32) -> vec3<f32> {
if (s <= 0.0) {
return vec3<f32>(l);
}
var q: f32;
if (l < 0.5) {
q = l * (1.0 + s);
} else {
q = l + s - l * s;
}
let p = 2.0 * l - q;
return vec3<f32>(hsl_channel(h + 1.0 / 3.0, p, q), hsl_channel(h, p, q), hsl_channel(h - 1.0 / 3.0, p, q));
}
// ColorSpace transfer curves: 1 = sRGB (sRGB, Display P3), 2 = 2.4 power (Rec. 709 / 2020).
fn curve_decode(curve: u32, v: f32) -> f32 {
let a = abs(v);
var l = a;
if (curve == 1u) {
l = srgb_to_linear(a);
} else if (curve == 2u) {
l = powz(a, 2.4);
}
return select(l, -l, v < 0.0);
}
fn curve_encode(curve: u32, v: f32) -> f32 {
let a = abs(v);
var e = a;
if (curve == 1u) {
e = linear_to_srgb(a);
} else if (curve == 2u) {
e = powz(a, 1.0 / 2.4);
}
return select(e, -e, v < 0.0);
}
+19
View File
@@ -0,0 +1,19 @@
// Viewer display: premultiplied f32 → premultiplied RGBA8 (ui-egui's frames::to_color_image),
// written straight into a texture egui samples.
@group(0) @binding(1) var dsrc: texture_2d<f32>;
@group(0) @binding(3) var dout: texture_storage_2d<rgba8unorm, write>;
@compute @workgroup_size(16, 16)
fn display(@builtin(global_invocation_id) gid: vec3<u32>) {
let dims = vec2<i32>(textureDimensions(dout));
let p = vec2<i32>(gid.xy);
if (p.x >= dims.x || p.y >= dims.y) {
return;
}
let s = textureLoad(dsrc, p, 0);
let a = clamp(s.w, 0.0, 1.0);
// (v · 255 + 0.5) truncated, as the CPU conversion.
let q = floor(vec4<f32>(clamp(s.xyz, vec3<f32>(0.0), vec3<f32>(a)), a) * 255.0 + 0.5) / 255.0;
textureStore(dout, p, q);
}
+750
View File
@@ -0,0 +1,750 @@
// Compositing and effect kernels (rgba32float output). Parameter layouts are documented next to
// each entry point and mirrored by the Rust dispatch code (src/ops.rs, src/effects.rs).
@group(0) @binding(3) var out: texture_storage_2d<rgba32float, write>;
fn out_dims() -> vec2<i32> {
return vec2<i32>(textureDimensions(out));
}
// ---------------------------------------------------------------- compositing
// Warp `src` by the inverse matrix (output pixel → source pixel) and blend over `aux`
// (raster::composite_warp / accumulate_warp).
// u[0] = (mode, sampling, seed, flags: 1 accumulate, 2 clear outside bounds)
// f[0..3] = inverse rows; f[3] = (opacity, weight); f[4] = bounds (x0, y0, x1, y1)
@compute @workgroup_size(16, 16)
fn warp_blend(@builtin(global_invocation_id) gid: vec3<u32>) {
let dims = out_dims();
let x = i32(gid.x);
let y = i32(gid.y);
if (x >= dims.x || y >= dims.y) {
return;
}
let mode = P.u[0].x;
let sampling = P.u[0].y;
let seed = P.u[0].z;
let flags = P.u[0].w;
let b = P.f[4];
var d = textureLoad(aux, vec2<i32>(x, y), 0);
let fx = f32(x) + 0.5;
let fy = f32(y) + 0.5;
if (fx < b.x || fy < b.y || fx > b.z || fy > b.w) {
if ((flags & 2u) != 0u) {
d = vec4<f32>(0.0);
}
textureStore(out, vec2<i32>(x, y), d);
return;
}
let r0 = P.f[0];
let r1 = P.f[1];
let r2 = P.f[2];
var px = r0.x * fx + r0.y * fy + r0.z;
var py = r1.x * fx + r1.y * fy + r1.z;
let w = r2.x * fx + r2.y * fy + r2.z;
if (w != 1.0 && w != 0.0) {
px = px / w;
py = py / w;
}
let sz = vec2<f32>(textureDimensions(src));
let stencil = is_stencil(mode);
if (px < -1.0 || py < -1.0 || px > sz.x + 1.0 || py > sz.y + 1.0) {
if (stencil && (flags & 1u) == 0u) {
d = blend_pixel(mode, d, vec4<f32>(0.0), 0.5);
}
textureStore(out, vec2<i32>(x, y), d);
return;
}
var s = sample(src, sampling, px, py);
if ((flags & 1u) != 0u) {
if (s.w > 0.0) {
d = d + s * P.f[3].y;
}
textureStore(out, vec2<i32>(x, y), d);
return;
}
if (s.w <= 0.0 && !stencil) {
textureStore(out, vec2<i32>(x, y), d);
return;
}
let op = P.f[3].x;
if (op < 1.0) {
s = s * op;
}
var n = 0.5;
if (mode == 1u || mode == 2u) {
n = hash_noise(u32(x), u32(y), seed);
}
textureStore(out, vec2<i32>(x, y), blend_pixel(mode, d, s, n));
}
// Blend `src` (same size) onto `aux` (Image::blend_from).
// u[0] = (mode, _, seed); f[0].x = opacity
@compute @workgroup_size(16, 16)
fn blend_full(@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 mode = P.u[0].x;
let d = textureLoad(aux, p, 0);
var s = textureLoad(src, p, 0);
if (s.w <= 0.0 && !is_stencil(mode)) {
textureStore(out, p, d);
return;
}
s = s * P.f[0].x;
var n = 0.5;
if (mode == 1u || mode == 2u) {
n = hash_noise(gid.x, gid.y, P.u[0].z);
}
textureStore(out, p, blend_pixel(mode, d, s, n));
}
// Track matte: `src` × matte factor of `aux`. u[0].x = 0 alpha, 1 alpha inverted, 2 luma,
// 3 luma inverted.
@compute @workgroup_size(16, 16)
fn matte(@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 q = textureLoad(aux, p, 0);
var k: f32;
switch P.u[0].x {
case 0u: { k = q.w; }
case 1u: { k = 1.0 - q.w; }
case 2u: { k = luminance(q.xyz); }
default: { k = 1.0 - luminance(q.xyz); }
}
k = clamp(k, 0.0, 1.0);
textureStore(out, p, textureLoad(src, p, 0) * k);
}
// Preserve Transparency: `src` × alpha of `aux`.
@compute @workgroup_size(16, 16)
fn preserve(@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;
}
textureStore(out, p, textureLoad(src, p, 0) * textureLoad(aux, p, 0).w);
}
// Knockout: `src` × (1 − alpha of `aux`).
@compute @workgroup_size(16, 16)
fn knockout(@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 keep = 1.0 - clamp(textureLoad(aux, p, 0).w, 0.0, 1.0);
textureStore(out, p, textureLoad(src, p, 0) * keep);
}
// Styled layer finish: canvas `src` moves toward `aux` by the opacity, per enabled channel.
// u[0].xyz = R/G/B switches; f[0].x = opacity.
@compute @workgroup_size(16, 16)
fn channel_mix(@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 c = textureLoad(src, p, 0);
let t = textureLoad(aux, p, 0);
let op = P.f[0].x;
var v = t;
if (P.u[0].x == 0u) {
v.x = c.x;
}
if (P.u[0].y == 0u) {
v.y = c.y;
}
if (P.u[0].z == 0u) {
v.z = c.z;
}
textureStore(out, p, c + (v - c) * op);
}
// 8/16 bpc: clamp premultiplied pixels to 0..1 (colour ≤ alpha) and round. f[0].x = levels.
@compute @workgroup_size(16, 16)
fn quantize(@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 levels = P.f[0].x;
let inv = 1.0 / levels;
let s = textureLoad(src, p, 0);
let a = round_away(clamp(s.w, 0.0, 1.0) * levels) * inv;
var o = vec4<f32>(0.0, 0.0, 0.0, a);
o.x = round_away(clamp(s.x, 0.0, a) * levels) * inv;
o.y = round_away(clamp(s.y, 0.0, a) * levels) * inv;
o.z = round_away(clamp(s.z, 0.0, a) * levels) * inv;
textureStore(out, p, o);
}
// Colour conversion on straight colour (render::color::convert).
// u[0] = (decode curve, has matrix, encode curve); f[0..3] = matrix rows.
@compute @workgroup_size(16, 16)
fn convert(@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 s = textureLoad(src, p, 0);
let a = s.w;
if (a <= 0.0) {
textureStore(out, p, s);
return;
}
var c = s.xyz / a;
let dc = P.u[0].x;
if (dc != 0u) {
c = vec3<f32>(curve_decode(dc, c.x), curve_decode(dc, c.y), curve_decode(dc, c.z));
}
if (P.u[0].y != 0u) {
let m0 = P.f[0].xyz;
let m1 = P.f[1].xyz;
let m2 = P.f[2].xyz;
c = vec3<f32>(m0.x * c.x + m0.y * c.y + m0.z * c.z, m1.x * c.x + m1.y * c.y + m1.z * c.z, m2.x * c.x + m2.y * c.y + m2.z * c.z);
}
let ec = P.u[0].z;
if (ec != 0u) {
c = vec3<f32>(curve_encode(ec, c.x), curve_encode(ec, c.y), curve_encode(ec, c.z));
}
textureStore(out, p, vec4<f32>(c * a, a));
}
// Box-filter halving for minification (raster::warp::half).
@compute @workgroup_size(16, 16)
fn half(@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 sx = 2 * p.x;
let sy = 2 * p.y;
var acc = vec4<f32>(0.0);
acc += tex_get_clamped(src, sx, sy);
acc += tex_get_clamped(src, sx + 1, sy);
acc += tex_get_clamped(src, sx, sy + 1);
acc += tex_get_clamped(src, sx + 1, sy + 1);
textureStore(out, p, acc * 0.25);
}
// ---------------------------------------------------------------- blurs (raster::blur)
fn line_get(horizontal: bool, line: i32, i: i32, n: i32, repeat: bool) -> vec4<f32> {
var j = i;
if (i < 0 || i >= n) {
if (!repeat) {
return vec4<f32>(0.0);
}
j = clamp(i, 0, n - 1);
}
if (horizontal) {
return textureLoad(src, vec2<i32>(j, line), 0);
}
return textureLoad(src, vec2<i32>(line, j), 0);
}
// One box pass of radius r along rows (horizontal) or columns, as running sums over blocks of
// `block` pixels (each block restarts its sum, as the CPU's vertical pass does).
fn box_pass(gid: vec3<u32>, horizontal: bool) {
let dims = out_dims();
let r = i32(P.u[0].x);
let repeat = P.u[0].y != 0u;
let block = i32(P.u[0].z);
var lines = dims.x;
var n = dims.y;
if (horizontal) {
lines = dims.y;
n = dims.x;
}
let line = i32(gid.x);
let b0 = i32(gid.y) * block;
if (line >= lines || b0 >= n) {
return;
}
let norm = 1.0 / f32(2 * r + 1);
var acc = vec4<f32>(0.0);
for (var i = b0 - r; i <= b0 + r; i++) {
acc += line_get(horizontal, line, i, n, repeat);
}
let b1 = min(b0 + block, n);
for (var i = b0; i < b1; i++) {
if (horizontal) {
textureStore(out, vec2<i32>(i, line), acc * norm);
} else {
textureStore(out, vec2<i32>(line, i), acc * norm);
}
let add = line_get(horizontal, line, i + r + 1, n, repeat);
let sub = line_get(horizontal, line, i - r, n, repeat);
acc += add - sub;
}
}
// u[0] = (radius, repeat edges, block); dispatched over (line, block).
@compute @workgroup_size(64, 1)
fn box_h(@builtin(global_invocation_id) gid: vec3<u32>) {
box_pass(gid, true);
}
@compute @workgroup_size(64, 1)
fn box_v(@builtin(global_invocation_id) gid: vec3<u32>) {
box_pass(gid, false);
}
// Directional blur (raster::directional_blur). f[0] = (dx, dy, length, samples).
@compute @workgroup_size(16, 16)
fn directional(@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 dx = P.f[0].x;
let dy = P.f[0].y;
let len = P.f[0].z;
let n = i32(P.f[0].w);
var acc = vec4<f32>(0.0);
for (var i = 0; i < n; i++) {
let t = (f32(i) / f32(n - 1) - 0.5) * len;
acc += sample_bilinear(src, f32(p.x) + 0.5 + dx * t, f32(p.y) + 0.5 + dy * t);
}
textureStore(out, p, acc / f32(n));
}
// ---------------------------------------------------------------- Glow / Drop Shadow
// Glow bright pass. u[0].x = A & B colours; f[0].x = threshold; f[1] = colour A; f[2] = colour B.
@compute @workgroup_size(16, 16)
fn glow_bright(@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;
if (a <= 0.0) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
let thr = P.f[0].x;
let l = luminance(px.xyz / a);
let k = clamp((l - thr) / max(1.0 - thr, 1e-3), 0.0, 1.0);
if (P.u[0].x != 0u) {
let t = clamp(l, 0.0, 1.0);
let ca = P.f[1].xyz;
let cb = P.f[2].xyz;
let c = ca + (cb - ca) * t;
textureStore(out, p, vec4<f32>(c * k * a, k * a));
} else {
textureStore(out, p, px * k);
}
}
// Glow composite: original `src` with the blurred glow `aux`. u[0].x = behind; f[0].x = intensity.
@compute @workgroup_size(16, 16)
fn glow_combine(@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 o = textureLoad(src, p, 0);
let g = textureLoad(aux, p, 0) * P.f[0].x;
if (P.u[0].x != 0u) {
let k = 1.0 - o.w;
o = o + g * k;
} else {
o = vec4<f32>(o.xyz + g.xyz, min(o.w + g.w * (1.0 - o.w), 1.0));
}
o.w = clamp(o.w, 0.0, 1.0);
textureStore(out, p, o);
}
// Drop shadow silhouette. f[0] = shadow colour; f[1] = (opacity, dx, dy).
@compute @workgroup_size(16, 16)
fn shadow_make(@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 c = P.f[0];
let a = sample_bilinear(src, f32(p.x) + 0.5 - P.f[1].y, f32(p.y) + 0.5 - P.f[1].z).w * P.f[1].x * c.w;
textureStore(out, p, vec4<f32>(c.xyz * a, a));
}
// The layer over its shadow: `aux` (original) over `src` (shadow).
@compute @workgroup_size(16, 16)
fn shadow_combine(@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 s = textureLoad(src, p, 0);
let o = textureLoad(aux, p, 0);
textureStore(out, p, o + s * (1.0 - o.w));
}
// ---------------------------------------------------------------- per-pixel effects
fn mix3(a: vec3<f32>, b: vec3<f32>, t: f32) -> vec3<f32> {
return a + (b - a) * t;
}
// Curves: data = [n, xs.., ys.., ms.., lut (1025)] per channel at offsets f[c].
fn curve_exact(base: u32, x: f32) -> f32 {
let n = u32(data[base]);
let xs = base + 1u;
let ys = xs + n;
let ms = ys + n;
if (x <= data[xs]) {
return data[ys] + (x - data[xs]) * data[ms];
}
if (x >= data[xs + n - 1u]) {
return data[ys + n - 1u] + (x - data[xs + n - 1u]) * data[ms + n - 1u];
}
// partition_point(|v| v <= x) - 1, clamped to n - 2.
var k = 0u;
for (var i = 0u; i < n; i++) {
if (data[xs + i] <= x) {
k = i + 1u;
}
}
k = min(max(k, 1u) - 1u, n - 2u);
let h = data[xs + k + 1u] - data[xs + k];
let t = (x - data[xs + k]) / h;
let t2 = t * t;
let t3 = t * t * t;
let h00 = 2.0 * t3 - 3.0 * t2 + 1.0;
let h10 = t3 - 2.0 * t2 + t;
let h01 = -2.0 * t3 + 3.0 * t2;
let h11 = t3 - t2;
return h00 * data[ys + k] + h10 * h * data[ms + k] + h01 * data[ys + k + 1u] + h11 * h * data[ms + k + 1u];
}
fn curve_eval(base_f: f32, x: f32) -> f32 {
if (base_f < 0.0) {
return x;
}
let base = u32(base_f);
if (!(x >= 0.0 && x <= 1.0)) {
return curve_exact(base, x);
}
let n = u32(data[base]);
let lut = base + 1u + 3u * n;
let f = x * 1024.0;
let i = min(u32(f), 1023u);
let t = f - f32(i);
return data[lut + i] + (data[lut + i + 1u] - data[lut + i]) * t;
}
// Fractal Noise lattice (effects::generate::lattice / value_noise).
fn lattice(ix: i32, iy: i32, iz: i32, seed: u32) -> f32 {
var h = (bitcast<u32>(ix) * 0x27d4eb2du) ^ (bitcast<u32>(iy) * 0x165667b1u) ^ (bitcast<u32>(iz) * 0x9e3779b9u) ^ (seed * 0x85ebca6bu);
h = h ^ (h >> 15u);
h = h * 0x2c1b3c6du;
h = h ^ (h >> 12u);
h = h * 0x297a2d39u;
h = h ^ (h >> 15u);
return f32(h & 0xffffu) / 65535.0;
}
fn fade(t: f32) -> f32 {
return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
}
fn value_noise(x: f32, y: f32, z: f32, seed: u32) -> f32 {
let x0 = floor(x);
let y0 = floor(y);
let z0 = floor(z);
let fx = fade(x - x0);
let fy = fade(y - y0);
let fz = fade(z - z0);
let ix = i32(x0);
let iy = i32(y0);
let iz = i32(z0);
let c000 = lattice(ix, iy, iz, seed);
let c100 = lattice(ix + 1, iy, iz, seed);
let c010 = lattice(ix, iy + 1, iz, seed);
let c110 = lattice(ix + 1, iy + 1, iz, seed);
let c001 = lattice(ix, iy, iz + 1, seed);
let c101 = lattice(ix + 1, iy, iz + 1, seed);
let c011 = lattice(ix, iy + 1, iz + 1, seed);
let c111 = lattice(ix + 1, iy + 1, iz + 1, seed);
let l00 = c000 + (c100 - c000) * fx;
let l01 = c010 + (c110 - c010) * fx;
let a = l00 + (l01 - l00) * fy;
let l10 = c001 + (c101 - c001) * fx;
let l11 = c011 + (c111 - c011) * fx;
let b = l10 + (l11 - l10) * fy;
return a + (b - a) * fz;
}
// generate::put: blend a generated straight colour (alpha ga) over the pixel.
fn put(px: vec4<f32>, g: vec3<f32>, ga: f32, blend_orig: f32) -> vec4<f32> {
let g2 = vec4<f32>(g * ga, ga);
let k = 1.0 - blend_orig;
return px * blend_orig + g2 * k;
}
// Straight-colour map helper: skips transparent pixels (Image::map_straight).
fn straight(px: vec4<f32>) -> vec3<f32> {
return px.xyz / px.w;
}
// u[0].x = op:
// 1 Tint f[0] = black, f[1] = white, f[2].x = amount
// 2 Bright/Contrast f[0] = (brightness, k)
// 3 Hue/Saturation u[0].y = colorize; f[0] = (hue, sat, light); f[1] = (ch, cs, cl)
// 4 Levels f[0] = (ib, iw, g, ob); f[1] = (ow); u[0].y = clip
// 5 Exposure f[0] = (e, offset, gamma)
// 6 Invert u[0].y = channel; f[0].x = blend
// 7 Fill f[0] = colour; f[1] = (opacity); u[0].y = invert
// 8 Curves f[0] = data offsets (rgb, red, green, blue), f[1].x = alpha (−1 = none)
// 9 Gradient Ramp f[0] = (sx, sy, ex, ey); f[1] = start colour; f[2] = end colour;
// f[3] = (len2, scatter, blend); u[0] = (_, radial, seed)
// 10 Fractal Noise f[0] = (contrast, brightness, scale, frac); f[1] = (ox, oy, sr, cr);
// f[2] = (evo, blend, opacity); u[0] = (_, kind, n_oct, seed); u[1].x = invert
@compute @workgroup_size(16, 16)
fn pointwise(@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 px = textureLoad(src, p, 0);
let a = px.w;
let op = P.u[0].x;
switch op {
case 1u: {
if (a > 0.0) {
let c = straight(px);
let l = clamp(luminance(c), 0.0, 1.0);
let blk = P.f[0].xyz;
let wht = P.f[1].xyz;
let t = blk + (wht - blk) * l;
px = vec4<f32>(mix3(c, t, P.f[2].x) * a, a);
}
}
case 2u: {
if (a > 0.0) {
let c = straight(px);
let br = P.f[0].x;
let k = P.f[0].y;
px = vec4<f32>(max((c + br - 0.5) * k + 0.5, vec3<f32>(0.0)) * a, a);
}
}
case 3u: {
if (a > 0.0) {
let c = straight(px);
let hsl = rgb_to_hsl(c);
var h = hsl.x;
var s = hsl.y;
var l = hsl.z;
if (P.u[0].y != 0u) {
let cl = P.f[1].z;
var k = l;
if (cl > 0.0) {
k = 1.0 - l;
}
h = P.f[1].x;
s = P.f[1].y;
l = clamp(l + cl * k, 0.0, 1.0);
} else {
let hue = P.f[0].x;
let sat = P.f[0].y;
let light = P.f[0].z;
var s2: f32;
if (sat >= 0.0) {
s2 = s + (1.0 - s) * sat * min(s, 1.0);
} else {
s2 = s * (1.0 + sat);
}
var l2: f32;
if (light >= 0.0) {
l2 = l + (1.0 - l) * light;
} else {
l2 = l * (1.0 + light);
}
h = fract_euclid(h + hue);
s = clamp(s2, 0.0, 1.0);
l = l2;
}
px = vec4<f32>(hsl_to_rgb(h, s, l) * a, a);
}
}
case 4u: {
if (a > 0.0) {
let c = straight(px);
let ib = P.f[0].x;
let iw = P.f[0].y;
let g = P.f[0].z;
let ob = P.f[0].w;
let ow = P.f[1].x;
var o = vec3<f32>(0.0);
for (var i = 0; i < 3; i++) {
var t = (c[i] - ib) / max(iw - ib, 1e-6);
if (P.u[0].y != 0u) {
t = clamp(t, 0.0, 1.0);
}
t = powz(max(t, 0.0), 1.0 / g);
o[i] = ob + (ow - ob) * t;
}
px = vec4<f32>(o * a, a);
}
}
case 5u: {
if (a > 0.0) {
let c = straight(px);
var o = vec3<f32>(0.0);
for (var i = 0; i < 3; i++) {
let lin = srgb_to_linear(max(c[i], 0.0));
let v = powz(max(lin * P.f[0].x + P.f[0].y, 0.0), 1.0 / P.f[0].z);
o[i] = linear_to_srgb(v);
}
px = vec4<f32>(o * a, a);
}
}
case 6u: {
let ch = P.u[0].y;
let blend = P.f[0].x;
if (ch == 4u) {
let na = 1.0 - a;
var k = 0.0;
if (a > 0.0) {
k = na / a;
}
let inv = vec4<f32>(px.xyz * k, na);
px = px + (inv - px) * blend;
} else if (a > 0.0) {
let s = straight(px);
var o = s;
switch ch {
case 1u: { o.x = 1.0 - s.x; }
case 2u: { o.y = 1.0 - s.y; }
case 3u: { o.z = 1.0 - s.z; }
default: { o = 1.0 - s; }
}
px = vec4<f32>((s + (o - s) * blend) * a, a);
}
}
case 7u: {
let c = P.f[0];
let invert = P.u[0].y != 0u;
let opacity = P.f[1].x;
var fa = px.w;
if (invert) {
fa = 1.0 - px.w;
}
let f = c.xyz * fa;
let rgb = px.xyz + (f - px.xyz) * opacity;
var alpha = px.w;
if (invert) {
alpha = px.w + (fa - px.w) * opacity;
}
px = vec4<f32>(rgb, alpha);
}
case 8u: {
// effects::color2::map_ca: transparent pixels only change through the alpha curve.
if (a <= 0.0 && px.x == 0.0 && px.y == 0.0 && px.z == 0.0) {
let na = curve_eval(P.f[1].x, 0.0);
if (na > 0.0) {
px = vec4<f32>(0.0, 0.0, 0.0, clamp(na, 0.0, 1.0));
}
} else {
var c = vec3<f32>(0.0);
var aa = max(a, 0.0);
if (a > 1e-6) {
c = px.xyz / a;
aa = a;
}
var o = vec3<f32>(0.0);
for (var i = 0; i < 3; i++) {
o[i] = curve_eval(P.f[0][i + 1], curve_eval(P.f[0].x, c[i]));
}
let na = clamp(curve_eval(P.f[1].x, aa), 0.0, 1.0);
px = vec4<f32>(o * na, na);
}
}
case 9u: {
let s = P.f[0].xy;
let e = P.f[0].zw;
let d = e - s;
let v = vec2<f32>(f32(p.x) + 0.5, f32(p.y) + 0.5) - s;
let len2 = P.f[3].x;
var t: f32;
if (P.u[0].y != 0u) {
t = sqrt((v.x * v.x + v.y * v.y) / len2);
} else {
t = (v.x * d.x + v.y * d.y) / len2;
}
let scatter = P.f[3].y;
if (scatter > 0.0) {
t += (hash_noise(gid.x, gid.y, P.u[0].z) - 0.5) * scatter;
}
t = clamp(t, 0.0, 1.0);
let c0 = P.f[1].xyz;
let c1 = P.f[2].xyz;
px = put(px, c0 + (c1 - c0) * t, 1.0, P.f[3].z);
}
case 10u: {
let contrast = P.f[0].x;
let brightness = P.f[0].y;
let scale = P.f[0].z;
let frac = P.f[0].w;
let ox = P.f[1].x;
let oy = P.f[1].y;
let sr = P.f[1].z;
let cr = P.f[1].w;
let evo = P.f[2].x;
let kind = P.u[0].y;
let n_oct = P.u[0].z;
let seed = P.u[0].w;
let dx = f32(p.x) + 0.5 - ox;
let dy = f32(p.y) + 0.5 - oy;
let u = (dx * cr + dy * sr) / scale;
let v = (-dx * sr + dy * cr) / scale;
var sum = 0.0;
var amp = 1.0;
var norm = 0.0;
var f = 1.0;
for (var o = 0u; o < n_oct; o++) {
var w = 1.0;
if (o + 1u == n_oct && frac > 0.0) {
w = frac;
}
var n = value_noise(u * f, v * f, evo + f32(o) * 7.31, seed + o);
if (kind == 1u) {
n = 1.0 - abs(n * 2.0 - 1.0);
}
sum += n * amp * w;
norm += amp * w;
amp *= 0.5;
f *= 2.0;
}
var val = sum / max(norm, 1e-6);
val = clamp((val - 0.5) * contrast + 0.5 + brightness, 0.0, 1.0);
if (P.u[1].x != 0u) {
val = 1.0 - val;
}
px = put(px, vec3<f32>(val), P.f[2].z, P.f[2].y);
}
default: {}
}
textureStore(out, p, px);
}