Merge origin/main (M13.22 GPU effects part A) into M13.23: both GPU effect lists (236), docs recounted

This commit is contained in:
Brandon Thomas
2026-10-04 00:17:37 -04:00
33 changed files with 3570 additions and 179 deletions
+21
View File
@@ -255,3 +255,24 @@ fn wire(@builtin(global_invocation_id) id: vec3<u32>) {
let p = table[id.x];
textureStore(out, vec2<i32>(i32(p.x), i32(p.y)), vec4<f32>(1.0));
}
// A layer rendered on its own (color_in, finished) hidden where the run's main scene is nearer
// (`adv::draw_run`): depth = its depth, src = the main scene's colour, radius = the main
// scene's depth. Pixels it didn't draw (BIG) are never hidden, as ∞ compares on the CPU.
// P.u0 = (width, height)
@compute @workgroup_size(16, 16)
fn occlude(@builtin(global_invocation_id) id: vec3<u32>) {
let w = P.u0.x;
if (id.x >= w || id.y >= P.u0.y) {
return;
}
let i = id.y * w + id.x;
let q = vec2<i32>(i32(id.x), i32(id.y));
var p = textureLoad(color_in, q, 0);
let z = depth[i];
let mz = radius[i];
if (z < BIG * 0.5 && mz < z - 0.01 * max(abs(z), 1.0)) {
p *= 1.0 - clamp(src[i].w, 0.0, 1.0);
}
textureStore(out, q, p);
}
+231
View File
@@ -0,0 +1,231 @@
// GPU effects, the CC light family: see src/fx_light.rs. CC Light Rays, CC Light Burst 2.5,
// CC Light Sweep and CC Light Wipe, each mirroring the CPU effect operation for operation.
// Coordinates are pixel centres relative to the effect's centre, so f32 keeps its precision.
fn flt_inside(p: vec2<i32>) -> bool {
let d = out_dims();
return p.x < d.x && p.y < d.y;
}
fn flt_rel(p: vec2<i32>, c: vec2<f32>) -> vec2<f32> {
return vec2<f32>(f32(p.x) + 0.5, f32(p.y) + 0.5) - c;
}
// util::smoothstep.
fn flt_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);
}
// util::unpremul's colour.
fn flt_unpremul(p: vec4<f32>) -> vec3<f32> {
if (p.w > 1e-6) {
return p.xyz / p.w;
}
return vec3<f32>(0.0);
}
// ---------------------------------------------------------------- CC Light Rays (generate2::light_rays)
// u[0] = (square, colour from source, transfer mode); f[0] = (cx, cy, k, gain);
// f[1] = (radius, outer radius + 1e-3); f[2] = colour.
fn flt_ray_mask(q: vec2<f32>) -> f32 {
let d = abs(q);
var r = length(d);
if (P.u[0].x != 0u) {
r = max(d.x, d.y);
}
return 1.0 - flt_smoothstep(P.f[1].x, P.f[1].y, r);
}
@compute @workgroup_size(16, 16)
fn flt_rays(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!flt_inside(p)) {
return;
}
let c = P.f[0].xy;
let rel = flt_rel(p, c);
var acc = vec4<f32>(0.0);
for (var i = 0u; i < 32u; i++) {
let s = 1.0 - P.f[0].z * f32(i) / 32.0;
let q = rel * s;
let m = flt_ray_mask(q);
if (m <= 0.0) {
continue;
}
acc += sample_bilinear(src, c.x + q.x, c.y + q.y) * m;
}
var ray = acc / 32.0 * P.f[0].w;
if (P.u[0].y == 0u) {
let l = luminance(flt_unpremul(ray)) * ray.w;
ray = vec4<f32>(P.f[2].xyz * l, ray.w);
}
let la = min(ray.w, 1.0);
let px = textureLoad(src, p, 0);
var o: vec4<f32>;
switch P.u[0].z {
case 1u: {
o = vec4<f32>(max(px.xyz, ray.xyz), max(px.w, la));
}
case 2u: {
let r4 = vec4<f32>(ray.xyz, la);
o = px + r4 - px * r4;
}
case 3u: {
o = vec4<f32>(ray.xyz, la);
}
default: {
let a = clamp(la, 0.0, 1.0);
o = vec4<f32>(px.xyz + ray.xyz, clamp(px.w + a * (1.0 - px.w), 0.0, 1.0));
}
}
textureStore(out, p, o);
}
// ---------------------------------------------------------------- CC Light Burst 2.5 (generate2::light_burst)
// u[0] = (burst, set colour); f[0] = (cx, cy, ray length k, intensity); f[1] = colour.
@compute @workgroup_size(16, 16)
fn flt_burst(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!flt_inside(p)) {
return;
}
let c = P.f[0].xy;
let rel = flt_rel(p, c);
let mode = P.u[0].x;
var acc = vec4<f32>(0.0);
var wsum = 0.0;
var best = vec4<f32>(0.0);
var best_l = -1.0;
for (var i = 0u; i < 32u; i++) {
let f = f32(i) / 32.0;
let s = 1.0 - P.f[0].z * f;
let q = rel * s;
let v = sample_bilinear(src, c.x + q.x, c.y + q.y);
if (mode == 0u) {
let l = luminance(v.xyz) + v.w * 1e-3;
if (l > best_l) {
best_l = l;
best = v;
}
} else if (mode == 1u) {
let w = 1.0 - f;
acc += v * w;
wsum += w;
} else {
acc += v;
wsum += 1.0;
}
}
var o = best;
if (mode != 0u) {
o = acc / max(wsum, 1e-6);
}
if (P.u[0].y != 0u) {
let a = max(o.w, 0.0);
let l = luminance(flt_unpremul(o)) * a;
o = vec4<f32>(P.f[1].xyz * l, a);
}
let k = P.f[0].w;
textureStore(out, p, vec4<f32>(o.xyz * k, clamp(o.w * k, 0.0, 1.0)));
}
// ---------------------------------------------------------------- CC Light Sweep (generate2::light_sweep)
// aux = the (softened) alpha plane in x. u[0] = (shape, reception mode); f[0] = (cx, cy, nx, ny);
// f[1] = (half width, sweep, edge intensity); f[2] = light colour.
@compute @workgroup_size(16, 16)
fn flt_sweep(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!flt_inside(p)) {
return;
}
let px = textureLoad(src, p, 0);
let rel = flt_rel(p, P.f[0].xy);
let d = abs(rel.x * P.f[0].z + rel.y * P.f[0].w);
let half = P.f[1].x;
let mode = P.u[0].y;
var band: f32;
switch P.u[0].x {
case 0u: {
band = max(1.0 - d / half, 0.0);
}
case 2u: {
band = clamp(half - d + 0.5, 0.0, 1.0);
}
default: {
band = flt_smoothstep(half, 0.0, d);
}
}
if (band <= 0.0) {
textureStore(out, p, select(px, vec4<f32>(0.0), mode == 2u));
return;
}
let gx = tex_get_clamped(aux, p.x + 1, p.y).x - tex_get_clamped(aux, p.x - 1, p.y).x;
let gy = tex_get_clamped(aux, p.x, p.y + 1).x - tex_get_clamped(aux, p.x, p.y - 1).x;
let edge = min(sqrt(gx * gx + gy * gy) * 2.0, 1.0);
let light = band * P.f[1].y + band * edge * P.f[1].z;
let lc = P.f[2].xyz;
let a = px.w;
var o: vec4<f32>;
if (mode == 1u) {
let la = min(band * P.f[1].y, 1.0);
o = vec4<f32>(px.xyz + lc * light, min(a + la * (1.0 - a), 1.0));
} else if (mode == 2u) {
let la = min(light * a, 1.0);
o = vec4<f32>(lc * la, la);
} else {
o = vec4<f32>(px.xyz + lc * light * a, a);
}
textureStore(out, p, o);
}
// ---------------------------------------------------------------- CC Light Wipe (transition::light_wipe)
// u[0] = (shape, colour from source, reverse); f[0] = (cx, cy, sin, cos);
// f[1] = (edge, band, intensity × ramp); f[2] = colour.
@compute @workgroup_size(16, 16)
fn flt_wipe(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!flt_inside(p)) {
return;
}
var px = textureLoad(src, p, 0);
let d = flt_rel(p, P.f[0].xy);
let s = P.f[0].z;
let co = P.f[0].w;
let q = vec2<f32>(d.x * co + d.y * s, -d.x * s + d.y * co);
var m: f32;
switch P.u[0].x {
case 0u: {
m = abs(q.x);
}
case 2u: {
m = max(abs(q.x), abs(q.y));
}
default: {
m = length(q);
}
}
let edge = P.f[1].x;
let visible = select(m > edge, m < edge, P.u[0].z != 0u);
if (!visible) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
let z = (m - edge) / P.f[1].y;
let g = exp(-(z * z)) * P.f[1].z;
if (g > 0.0) {
var lc = P.f[2].xyz;
if (P.u[0].y != 0u) {
lc = flt_unpremul(px);
}
px = vec4<f32>(px.xyz + lc * g * px.w, px.w);
}
textureStore(out, p, px);
}
+59
View File
@@ -0,0 +1,59 @@
// GPU effects, Numbers and Timecode: see src/fx_text.rs. The glyph coverage is rasterised on
// the CPU (aux: fill in x, stroke ring in y, opacity applied); this kernel composites it as
// textfx::draw_plan does: the layer or transparency, Timecode's box, then fill and stroke.
// u[0] = (display options, keep the layer, box); f[0] = fill colour; f[1] = stroke colour;
// f[2] = box (x0, x1, y0, y1); f[3] = (box colour, box opacity).
fn ftx_over(dst: vec4<f32>, s: vec4<f32>) -> vec4<f32> {
return s + dst * (1.0 - s.w);
}
// textfx::tint.
fn ftx_tint(c: vec4<f32>, a0: f32) -> vec4<f32> {
let a = clamp(a0 * c.w, 0.0, 1.0);
return vec4<f32>(c.xyz * a, a);
}
@compute @workgroup_size(16, 16)
fn ftx_text(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
let dims = out_dims();
if (p.x >= dims.x || p.y >= dims.y) {
return;
}
var o = vec4<f32>(0.0);
if (P.u[0].y != 0u) {
o = textureLoad(src, p, 0);
}
if (P.u[0].z != 0u) {
let r = P.f[2];
let fy = f32(p.y) + 0.5;
let cy = clamp(fy - r.z + 0.5, 0.0, 1.0) * clamp(r.w - fy + 0.5, 0.0, 1.0);
if (cy > 0.0) {
let fx = f32(p.x) + 0.5;
let cx = clamp(fx - r.x + 0.5, 0.0, 1.0) * clamp(r.y - fx + 0.5, 0.0, 1.0);
let k = cx * cy * P.f[3].w;
if (k > 0.0) {
o = ftx_over(o, vec4<f32>(P.f[3].xyz * k, k));
}
}
}
let cov = textureLoad(aux, p, 0);
let fill = ftx_tint(P.f[0], cov.x);
let stroke = ftx_tint(P.f[1], cov.y);
switch P.u[0].x {
case 1u: {
o = ftx_over(o, stroke);
}
case 2u: {
o = ftx_over(ftx_over(o, stroke), fill);
}
case 3u: {
o = ftx_over(ftx_over(o, fill), stroke);
}
default: {
o = ftx_over(o, fill);
}
}
textureStore(out, p, o);
}
+176
View File
@@ -0,0 +1,176 @@
// GPU effects, time: see src/fx_time.rs. The frames come from the effect host (fetched and
// placed on one grid on the CPU, as Echo's); these kernels combine them as the CPU effects do.
fn ftm_inside(p: vec2<i32>) -> bool {
let d = out_dims();
return p.x < d.x && p.y < d.y;
}
// util::unpremul: (straight colour, alpha).
fn ftm_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));
}
// Weighted sum, one frame per pass (time_fx::average): src + aux × f[0].x.
@compute @workgroup_size(16, 16)
fn ftm_acc(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftm_inside(p)) {
return;
}
textureStore(out, p, textureLoad(src, p, 0) + textureLoad(aux, p, 0) * P.f[0].x);
}
// Cross-fade (raster::flow::mix): src + (aux − src) × f[0].x.
@compute @workgroup_size(16, 16)
fn ftm_mix(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftm_inside(p)) {
return;
}
let a = textureLoad(src, p, 0);
textureStore(out, p, a + (textureLoad(aux, p, 0) - a) * P.f[0].x);
}
// ---------------------------------------------------------------- Time Difference (time_fx::time_difference)
// src = the layer now, aux = the target. u[0] = (absolute difference, alpha channel);
// f[0].x = contrast gain.
@compute @workgroup_size(16, 16)
fn ftm_difference(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftm_inside(p)) {
return;
}
let cu = ftm_unpremul(textureLoad(src, p, 0));
let tu = ftm_unpremul(textureLoad(aux, p, 0));
let c = cu.xyz;
let d = tu.xyz;
let ca = cu.w;
let da = tu.w;
let gain = P.f[0].x;
let diff = c - d;
var rgb = clamp(0.5 + diff * 0.5 * gain, vec3<f32>(0.0), vec3<f32>(1.0));
if (P.u[0].x != 0u) {
rgb = clamp(abs(diff) * gain, vec3<f32>(0.0), vec3<f32>(1.0));
}
let mx = max(max(rgb.x, rgb.y), rgb.z);
let light = (mx + min(min(rgb.x, rgb.y), rgb.z)) * 0.5;
var a: f32;
switch P.u[0].y {
case 1u: {
a = da;
}
case 2u: {
a = (ca + da) * 0.5;
}
case 3u: {
a = max(ca, da);
}
case 4u: {
a = 1.0;
}
case 5u: {
a = light;
}
case 6u: {
a = mx;
}
case 7u: {
a = abs(ca - da) * gain;
}
case 8u: {
a = clamp(abs(ca - da) * gain, 0.0, 1.0);
rgb = vec3<f32>(1.0);
}
default: {
a = ca;
}
}
a = clamp(a, 0.0, 1.0);
textureStore(out, p, vec4<f32>(rgb * a, a));
}
// ---------------------------------------------------------------- Time Displacement (time_fx::time_displacement)
// One pass per sampled time: pixels whose frame index (data, u32 per pixel, row length u[0].y)
// is u[0].x take frame aux at (x + dx, y + dy) (u[0].zw, i32), the others keep src.
@compute @workgroup_size(16, 16)
fn ftm_displace(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftm_inside(p)) {
return;
}
let k = bitcast<u32>(data[u32(p.y) * P.u[0].y + u32(p.x)]);
if (k == P.u[0].x) {
textureStore(out, p, tex_get(aux, p.x + bitcast<i32>(P.u[0].z), p.y + bitcast<i32>(P.u[0].w)));
} else {
textureStore(out, p, textureLoad(src, p, 0));
}
}
// ---------------------------------------------------------------- Timewarp Pixel Motion (time_fx::timewarp_interpolate)
// src = frame a, aux = frame b (both source-cropped); data = the motion vectors (x, y pairs).
// u[0] = (columns, rows, extreme filtering, build from one image); f[0] = (vector spacing,
// fraction w, error threshold).
// raster::flow::Flow::at.
fn ftm_flow_at(x: f32, y: f32) -> vec2<f32> {
let cols = P.u[0].x;
let rows = P.u[0].y;
if (cols == 0u || rows == 0u) {
return vec2<f32>(0.0);
}
let b = P.f[0].x;
let fx = clamp(x / b - 0.5, 0.0, f32(cols - 1u));
let fy = clamp(y / b - 0.5, 0.0, f32(rows - 1u));
let x0 = u32(floor(fx));
let y0 = u32(floor(fy));
let x1 = min(x0 + 1u, cols - 1u);
let y1 = min(y0 + 1u, rows - 1u);
let tx = fx - f32(x0);
let ty = fy - f32(y0);
let g00 = vec2<f32>(data[(y0 * cols + x0) * 2u], data[(y0 * cols + x0) * 2u + 1u]);
let g10 = vec2<f32>(data[(y0 * cols + x1) * 2u], data[(y0 * cols + x1) * 2u + 1u]);
let g01 = vec2<f32>(data[(y1 * cols + x0) * 2u], data[(y1 * cols + x0) * 2u + 1u]);
let g11 = vec2<f32>(data[(y1 * cols + x1) * 2u], data[(y1 * cols + x1) * 2u + 1u]);
let top = g00 + (g10 - g00) * tx;
let bot = g01 + (g11 - g01) * tx;
return top + (bot - top) * ty;
}
fn ftm_sample(t: texture_2d<f32>, x: f32, y: f32) -> vec4<f32> {
if (P.u[0].z != 0u) {
return sample_bicubic(t, x, y);
}
return sample_bilinear_clamped(t, x, y);
}
@compute @workgroup_size(16, 16)
fn ftm_motion(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftm_inside(p)) {
return;
}
let cx = f32(p.x) + 0.5;
let cy = f32(p.y) + 0.5;
let f = ftm_flow_at(cx, cy);
let w = P.f[0].y;
var pa = ftm_sample(src, cx - w * f.x, cy - w * f.y);
var pb = ftm_sample(aux, cx + (1.0 - w) * f.x, cy + (1.0 - w) * f.y);
if (P.u[0].w != 0u) {
textureStore(out, p, select(pb, pa, w < 0.5));
return;
}
let d = abs(pa - pb);
let err = max(max(d.x, d.y), max(d.z, d.w));
let thr = P.f[0].z;
if (thr > 0.0 && err > thr && thr < 1.0) {
pa = textureLoad(src, p, 0);
pb = textureLoad(aux, p, 0);
}
textureStore(out, p, pa + (pb - pa) * w);
}
+883
View File
@@ -0,0 +1,883 @@
// GPU effects, transitions and perspective: see src/fx_transition.rs. Block Dissolve, the CC
// transitions (Glass Wipe, Grid Wipe, Image Wipe, Jaws, Line Sweep, Radial ScaleWipe, Scale
// Wipe, Twister, WarpoMatic), Radial Shadow, CC Bender, CC Blobbylize, CC Cylinder, CC Sphere,
// CC Spotlight, CC Environment and 3D Glasses, each mirroring the CPU effect operation for
// operation. Coordinates are pixel centres relative to the effect's centre where it has one.
fn ftr_inside(p: vec2<i32>) -> bool {
let d = out_dims();
return p.x < d.x && p.y < d.y;
}
fn ftr_centre(p: vec2<i32>) -> vec2<f32> {
return vec2<f32>(f32(p.x) + 0.5, f32(p.y) + 0.5);
}
// util::smoothstep.
fn ftr_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);
}
// util::unpremul's colour.
fn ftr_unpremul(p: vec4<f32>) -> vec3<f32> {
if (p.w > 1e-6) {
return p.xyz / p.w;
}
return vec3<f32>(0.0);
}
// The third image, copied into `data` as RGBA f32 rows: P.u[3] = (row stride, width, height).
fn ftr_row(x: i32, y: i32) -> vec4<f32> {
if (x < 0 || y < 0 || x >= i32(P.u[3].y) || y >= i32(P.u[3].z)) {
return vec4<f32>(0.0);
}
let i = (u32(y) * P.u[3].x + u32(x)) * 4u;
return vec4<f32>(data[i], data[i + 1u], data[i + 2u], data[i + 3u]);
}
fn ftr_row_clamped(x: i32, y: i32) -> vec4<f32> {
return ftr_row(clamp(x, 0, i32(P.u[3].y) - 1), clamp(y, 0, i32(P.u[3].z) - 1));
}
// Image::sample_bilinear on the third image.
fn ftr_row_bilinear(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 = ftr_row(xi, yi);
let b = ftr_row(xi + 1, yi);
let c = ftr_row(xi, yi + 1);
let d = ftr_row(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 on the third image.
fn ftr_row_bilinear_clamped(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 = ftr_row_clamped(xi, yi);
let b = ftr_row_clamped(xi + 1, yi);
let c = ftr_row_clamped(xi, yi + 1);
let d = ftr_row_clamped(xi + 1, yi + 1);
let top = a + (b - a) * tx;
let bot = c + (d - c) * tx;
return top + (bot - top) * ty;
}
// Central differences of the aux plane (x), edges clamped (transition2::grad).
fn ftr_grad(p: vec2<i32>) -> vec2<f32> {
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;
return vec2<f32>(gx, gy);
}
// transition2::wipe_t.
fn ftr_wipe_t(g: f32, c: f32, w0: f32) -> f32 {
let w = max(w0, 1e-3);
let edge = c * (1.0 + 2.0 * w) - w;
return 1.0 - ftr_smoothstep(edge - w, edge + w, g);
}
// ---------------------------------------------------------------- planes
// A plane (x) from the source: u[0].x = util::Src index (Red, Green, Blue, Alpha, Luminance,
// Hue, Lightness, Saturation, Full, Half, Off) times alpha except for Alpha itself
// (transition2::prop_plane, CC Blobbylize); 11 = CC Environment's relief.
@compute @workgroup_size(16, 16)
fn ftr_plane(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let px = textureLoad(src, p, 0);
let c = ftr_unpremul(px);
let a = max(px.w, 0.0);
var v: f32;
switch P.u[0].x {
case 0u: {
v = c.x;
}
case 1u: {
v = c.y;
}
case 2u: {
v = c.z;
}
case 3u: {
v = a;
}
case 4u: {
v = luminance(c);
}
case 5u: {
v = rgb_to_hsl(c).x;
}
case 6u: {
v = rgb_to_hsl(c).z;
}
case 7u: {
v = rgb_to_hsl(c).y;
}
case 8u: {
v = 1.0;
}
case 9u: {
v = 0.5;
}
case 11u: {
v = luminance(c) * 0.5 + 0.5;
}
default: {
v = 0.0;
}
}
if (P.u[0].x != 3u) {
v *= a;
}
textureStore(out, p, vec4<f32>(v, 0.0, 0.0, 0.0));
}
// src × aux.x (Image::mul_mask).
@compute @workgroup_size(16, 16)
fn ftr_mul(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
textureStore(out, p, textureLoad(src, p, 0) * textureLoad(aux, p, 0).x);
}
// aux + src × (1 − aux.a): src composited under aux.
@compute @workgroup_size(16, 16)
fn ftr_under(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let o = textureLoad(aux, p, 0);
textureStore(out, p, o + textureLoad(src, p, 0) * (1.0 - o.w));
}
// ---------------------------------------------------------------- Block Dissolve (transition::block_dissolve)
// The kept-block mask (x). u[0] = (sub-samples per axis, seed, column table length);
// f[0].x = completion; data = block columns (width × n, u32) then block rows (height × n).
@compute @workgroup_size(16, 16)
fn ftr_blocks(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let n = P.u[0].x;
var hits = 0u;
for (var j = 0u; j < n; j++) {
let by = bitcast<u32>(data[P.u[0].z + u32(p.y) * n + j]);
for (var i = 0u; i < n; i++) {
let bx = bitcast<u32>(data[u32(p.x) * n + i]);
if (hash_noise(bx, by, P.u[0].y) >= P.f[0].x) {
hits += 1u;
}
}
}
textureStore(out, p, vec4<f32>(f32(hits) / f32(n * n), 0.0, 0.0, 0.0));
}
// ---------------------------------------------------------------- CC Grid Wipe (transition::grid_wipe)
// u[0] = (shape, reverse); f[0] = (cx, cy, sin, cos); f[1] = (cell size, max distance,
// completion, border).
fn ftr_metric(shape: u32, d: vec2<f32>) -> f32 {
switch shape {
case 0u: {
return abs(d.x);
}
case 2u: {
return max(abs(d.x), abs(d.y));
}
default: {
return length(d);
}
}
}
@compute @workgroup_size(16, 16)
fn ftr_grid(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let px = textureLoad(src, p, 0);
let d = ftr_centre(p) - P.f[0].xy;
let s = P.f[0].z;
let co = P.f[0].w;
let q = vec2<f32>(d.x * co + d.y * s, -d.x * s + d.y * co);
let cs = P.f[1].x;
let cc = (floor(q / cs) + 0.5) * cs;
var o = clamp(ftr_metric(P.u[0].x, cc) / P.f[1].y, 0.0, 1.0);
if (P.u[0].y != 0u) {
o = 1.0 - o;
}
let spread = 0.25;
let pr = clamp((P.f[1].z * (1.0 + spread) - o) / spread, 0.0, 1.0);
if (pr <= 0.0) {
textureStore(out, p, px);
return;
}
let half = cs * 0.5 * (1.0 - pr);
let m = max(abs(q.x - cc.x), abs(q.y - cc.y));
var k = select(0.0, 1.0, m < half);
if (P.f[1].w > 0.0) {
k = clamp((half - m) / P.f[1].w + 0.5, 0.0, 1.0);
}
textureStore(out, p, px * k);
}
// ---------------------------------------------------------------- CC Radial ScaleWipe / CC Scale Wipe (transition.rs)
// Radial: u[0].x = reverse; f[0] = (cx, cy, max distance, completion).
@compute @workgroup_size(16, 16)
fn ftr_radial_scale(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let c = P.f[0].xy;
let maxd = P.f[0].z;
let done = P.f[0].w;
let d = ftr_centre(p) - c;
let r = length(d);
var o = vec4<f32>(0.0);
if (P.u[0].x != 0u) {
let lim = (1.0 - done) * maxd;
if (!(r >= lim || lim <= 1e-9)) {
let k = maxd / lim;
o = sample_bilinear(src, c.x + d.x * k, c.y + d.y * k);
}
} else {
let hole = done * maxd;
if (!(r <= hole || maxd - hole <= 1e-9)) {
let rs = (r - hole) * maxd / (maxd - hole);
o = sample_bilinear(src, c.x + d.x / r * rs, c.y + d.y / r * rs);
}
}
textureStore(out, p, o);
}
// Scale Wipe: f[0] = (cx, cy, direction x, direction y); f[1].x = stretch.
@compute @workgroup_size(16, 16)
fn ftr_scale(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let f = ftr_centre(p);
let dir = P.f[0].zw;
let u = dot(f - P.f[0].xy, dir);
var q = f;
if (u > 0.0) {
let back = u - u / (1.0 + P.f[1].x);
q = f - dir * back;
}
textureStore(out, p, sample_bilinear_clamped(src, q.x, q.y));
}
// ---------------------------------------------------------------- CC Glass Wipe (transition2::glass_wipe)
// aux = the gradient plane (x); data = the layer to reveal (rows). u[0].x = has reveal;
// f[0] = (completion, band half-width, displacement).
@compute @workgroup_size(16, 16)
fn ftr_glass(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let t = ftr_wipe_t(textureLoad(aux, p, 0).x, P.f[0].x, P.f[0].y);
let band = 4.0 * t * (1.0 - t);
let g = ftr_grad(p);
let f = ftr_centre(p);
let o = g * P.f[0].z * band * 10.0;
var a = textureLoad(src, p, 0);
var r = vec4<f32>(0.0);
if (band > 1e-4) {
a = sample_bilinear(src, f.x + o.x, f.y + o.y);
if (P.u[0].x != 0u) {
r = ftr_row_bilinear(f.x - o.x, f.y - o.y);
}
} else if (P.u[0].x != 0u) {
r = ftr_row(p.x, p.y);
}
textureStore(out, p, a + (r - a) * t);
}
// ---------------------------------------------------------------- CC Image Wipe (transition2::image_wipe)
// aux = the gradient plane (x). u[0].x = inverse; f[0] = (completion, band half-width).
@compute @workgroup_size(16, 16)
fn ftr_image_wipe(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
var g = textureLoad(aux, p, 0).x;
if (P.u[0].x != 0u) {
g = 1.0 - g;
}
let k = 1.0 - ftr_wipe_t(g, P.f[0].x, P.f[0].y);
textureStore(out, p, textureLoad(src, p, 0) * k);
}
// ---------------------------------------------------------------- CC Jaws (transition2::jaws)
// u[0].x = shape; f[0] = (cx, cy, sin, cos); f[1] = (tooth width, amplitude, travel).
@compute @workgroup_size(16, 16)
fn ftr_jaws(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let c = P.f[0].xy;
let sn = P.f[0].z;
let cs = P.f[0].w;
let width = P.f[1].x;
let amp = P.f[1].y;
let dd = P.f[1].z;
let d = ftr_centre(p) - c;
let u = d.x * cs + d.y * sn;
let v = -d.x * sn + d.y * cs;
let ph = fract_euclid(u / width);
var f: f32;
switch P.u[0].x {
case 0u: {
f = amp * (1.0 - 4.0 * abs(ph - 0.5));
}
case 1u: {
f = amp * clamp((1.0 - 4.0 * abs(ph - 0.5)) * 2.0, -1.0, 1.0);
}
case 2u: {
f = select(-amp, amp, ph < 0.5);
}
default: {
f = amp * sin(ph * 6.283185307179586);
}
}
var vs = 0.0;
var hit = true;
if (v + dd < f) {
vs = v + dd;
} else if (v - dd >= f) {
vs = v - dd;
} else {
hit = false;
}
var o = vec4<f32>(0.0);
if (hit) {
o = sample_bilinear(src, u * cs - vs * sn + c.x, u * sn + vs * cs + c.y);
}
textureStore(out, p, o);
}
// ---------------------------------------------------------------- CC Line Sweep (transition2::line_sweep)
// u[0] = (flip, slant ≥ 0); f[0] = (sin, cos, v0, u0); f[1] = (thickness, |slant|, strips,
// total travel); f[2].x = completion.
@compute @workgroup_size(16, 16)
fn ftr_line_sweep(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let f = ftr_centre(p);
let sn = P.f[0].x;
let cs = P.f[0].y;
let u = f.x * cs + f.y * sn;
let v = -f.x * sn + f.y * cs;
let nstr = P.f[1].z;
var k = floor((v - P.f[0].z) / P.f[1].x);
if (P.u[0].x != 0u) {
k = nstr - 1.0 - k;
}
let stagger = select(nstr - 1.0 - k, k, P.u[0].y != 0u);
let front = P.f[0].w + P.f[2].x * P.f[1].w - P.f[1].y * stagger;
textureStore(out, p, select(textureLoad(src, p, 0), vec4<f32>(0.0), u < front));
}
// ---------------------------------------------------------------- CC Twister (transition2::twister)
// aux = the backside layer (u[0].x = has one); u[0].y = shading; f[0] = (cx, cy, sin, cos);
// f[1] = (half diagonal, completion).
@compute @workgroup_size(16, 16)
fn ftr_twister(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let c = P.f[0].xy;
let sn = P.f[0].z;
let cs = P.f[0].w;
let d = ftr_centre(p) - c;
let u = d.x * cs + d.y * sn;
let v = -d.x * sn + d.y * cs;
let s01 = clamp((u / P.f[1].x) * 0.5 + 0.5, 0.0, 1.0);
let th = 3.141592653589793 * clamp(P.f[1].y * 2.0 - s01, 0.0, 1.0);
let k = cos(th);
if (abs(k) < 1e-4) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
let vs = v / k;
var o: vec4<f32>;
if (k > 0.0 || P.u[0].x == 0u) {
o = sample_bilinear(src, u * cs - vs * sn + c.x, u * sn + vs * cs + c.y);
} else {
o = sample_bilinear(aux, u * cs + vs * sn + c.x, u * sn - vs * cs + c.y);
}
if (P.u[0].y != 0u) {
let l = 0.4 + 0.6 * abs(k);
o = vec4<f32>(o.xyz * l, o.w);
}
textureStore(out, p, o);
}
// ---------------------------------------------------------------- CC WarpoMatic (transition2::warpomatic)
// Reactor "Contrast Differences": src = the plane, aux = its wide mean.
@compute @workgroup_size(16, 16)
fn ftr_react_contrast(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let a = textureLoad(src, p, 0).x;
let b = textureLoad(aux, p, 0).x;
textureStore(out, p, vec4<f32>(min(abs(a - b) * 4.0, 1.0), 0.0, 0.0, 0.0));
}
// Reactor "Local Differences": aux = the plane; its gradient magnitude.
@compute @workgroup_size(16, 16)
fn ftr_react_local(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let g = ftr_grad(p);
textureStore(out, p, vec4<f32>(min(sqrt(g.x * g.x + g.y * g.y) * 8.0, 1.0), 0.0, 0.0, 0.0));
}
// aux = the reactor plane (x); data = the layer to reveal (rows). u[0] = (warp direction,
// has reveal); f[0] = (completion, blend span, warp amount).
@compute @workgroup_size(16, 16)
fn ftr_warpo(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let q = ftr_wipe_t(textureLoad(aux, p, 0).x, P.f[0].x, P.f[0].y);
let g = ftr_grad(p);
let f = ftr_centre(p);
let amt = P.f[0].z;
let o = g * amt * q;
var i = g * amt * (1.0 - q);
if (P.u[0].x == 1u) {
i = -i;
} else if (P.u[0].x == 2u) {
i = vec2<f32>(-i.y, i.x);
}
var a = vec4<f32>(0.0);
if (q < 1.0) {
a = sample_bilinear(src, f.x + o.x, f.y + o.y);
}
var inc = vec4<f32>(0.0);
if (P.u[0].y != 0u && q > 0.0) {
inc = ftr_row_bilinear(f.x + i.x, f.y + i.y);
}
textureStore(out, p, a + (inc - a) * q);
}
// ---------------------------------------------------------------- Radial Shadow (perspective::radial_shadow)
// The shadow before its blur. u[0].x = glass edge; f[0] = (light x, y, projection k,
// opacity × colour alpha); f[1] = (colour, colour influence).
@compute @workgroup_size(16, 16)
fn ftr_rshadow(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let l = P.f[0].xy;
let q = ftr_centre(p);
let s = sample_bilinear(src, l.x + (q.x - l.x) / P.f[0].z, l.y + (q.y - l.y) / P.f[0].z);
let sc = ftr_unpremul(s);
let sa = max(s.w, 0.0);
var a = sa;
if (P.u[0].x != 0u) {
a = clamp(sa * (1.0 - sa) * 4.0, 0.0, 1.0);
}
a *= P.f[0].w;
let col = P.f[1].xyz;
let c = col + (sc - col) * P.f[1].w;
textureStore(out, p, vec4<f32>(c * a, a));
}
// ---------------------------------------------------------------- CC Bender (distort3::cc_bender)
// u[0].x = style; f[0] = (base x, y, axis x, y); f[1] = (axis length, normal x, y, amount ×
// reference).
@compute @workgroup_size(16, 16)
fn ftr_bender(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let f = ftr_centre(p);
let r = f - P.f[0].xy;
let t = clamp(dot(r, P.f[0].zw) / P.f[1].x, 0.0, 1.0);
let pi = 3.141592653589793;
var b: f32;
switch P.u[0].x {
case 1u: {
b = 0.5 * sin(pi * t) * t;
}
case 2u: {
b = 0.5 * t;
}
case 3u: {
b = 0.25 * (1.0 - cos(pi * t)) - 0.1 * sin(2.0 * pi * t);
}
default: {
b = 0.5 * t * t;
}
}
let d = P.f[1].w * b;
textureStore(out, p, sample_bilinear(src, f.x - P.f[1].y * d, f.y - P.f[1].z * d));
}
// ---------------------------------------------------------------- CC Blobbylize (distort3::cc_blobbylize)
// aux = the blurred blob plane (x). f[0] = (depth, cut away, intensity, ambient);
// f[1] = (diffuse, specular, shininess, metal); f[2] = (light x, y, point light height,
// point light); f[3] = distant light direction; f[4] = light colour.
@compute @workgroup_size(16, 16)
fn ftr_blob(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let depth = P.f[0].x;
let cut = P.f[0].y;
let hv = textureLoad(aux, p, 0).x;
let g = ftr_grad(p);
let f = ftr_centre(p);
let s = sample_bilinear(src, f.x - g.x * depth * 2.0, f.y - g.y * depth * 2.0);
let c = ftr_unpremul(s);
let a = max(s.w, 0.0);
let m = clamp((hv - cut) / max((1.0 - cut) * 0.5, 1e-3), 0.0, 1.0);
let alpha = max(a, s.w) * m;
if (alpha <= 0.0) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
let n = normalize(vec3<f32>(-g.x * depth, -g.y * depth, 1.0));
var l = P.f[3].xyz;
if (P.f[2].w != 0.0) {
l = normalize(vec3<f32>(P.f[2].x - f32(p.x), P.f[2].y - f32(p.y), P.f[2].z));
}
let intensity = P.f[0].z;
let ndl = max(dot(n, l), 0.0);
let h3 = normalize(vec3<f32>(l.x, l.y, l.z + 1.0));
let sp = powz(max(dot(n, h3), 0.0), P.f[1].z) * P.f[1].y;
let lc = P.f[4].xyz;
let metal = P.f[1].w;
let lit = (P.f[0].w + P.f[1].x * ndl * intensity) * lc;
let spec_col = (1.0 - metal) * lc + metal * c * lc;
let o = c * lit + (sp * intensity) * spec_col;
textureStore(out, p, vec4<f32>(o * alpha, alpha));
}
// 0.5 + atan2(x, z) / 2π, kept on the side of the ±π seam that the sign of `x` puts it on (a
// GPU's approximate atan2 can land just across it, which wraps to the opposite edge of the
// texture). x = ±0 takes the CPU's −π (its zero is negative there).
fn ftr_lon_u(x: f32, z: f32) -> f32 {
let u = 0.5 + atan2(x, z) / 6.283185307179586;
if (x <= 0.0) {
return clamp(u, 0.0, 0.5);
}
return clamp(u, 0.5, 0.99999994);
}
// ---------------------------------------------------------------- CC Cylinder / CC Sphere (perspective.rs)
// Shading (perspective::Shading): f[3] = (light direction, intensity); f[4] = (light colour,
// ambient); f[5] = (diffuse, specular, shininess); f[6] = half vector. The layer rectangle in
// f[1] = (x0, y0, width, height).
fn ftr_shade(px: vec4<f32>, n: vec3<f32>) -> vec4<f32> {
let c = ftr_unpremul(px);
let a = max(px.w, 0.0);
if (a <= 0.0) {
return vec4<f32>(0.0);
}
let nl = max(dot(n, P.f[3].xyz), 0.0);
var spec = 0.0;
if (nl > 0.0) {
spec = powz(max(dot(n, P.f[6].xyz), 0.0), P.f[5].z) * P.f[5].y;
}
let lc = P.f[4].xyz * P.f[3].w;
let o = c * (P.f[4].w + P.f[5].x * nl * lc) + spec * lc;
return vec4<f32>(o * a, a);
}
// perspective::texel: the layer as a texture at (u, v), wrapping horizontally.
fn ftr_texel(u0: f32, v: f32) -> vec4<f32> {
let u = fract_euclid(u0);
if (v < 0.0 || v > 1.0) {
return vec4<f32>(0.0);
}
let r = P.f[1];
return sample_bilinear_clamped(src, r.x + u * r.z, r.y + v * r.w);
}
fn ftr_over(top: vec4<f32>, bottom: vec4<f32>) -> vec4<f32> {
return top + bottom * (1.0 - top.w);
}
fn ftr_faces(mode: u32, front: vec4<f32>, back: vec4<f32>) -> vec4<f32> {
if (mode == 1u) {
return front;
}
if (mode == 2u) {
return back;
}
return ftr_over(front, back);
}
// Cylinder: u[0].x = render; f[0] = (cx, cy, radius, layer height); f[2].x = rotation.
@compute @workgroup_size(16, 16)
fn ftr_cylinder(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let d = ftr_centre(p) - P.f[0].xy;
let v = d.y / P.f[0].w + 0.5;
let s = d.x / P.f[0].z;
if (abs(s) >= 1.0) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
let tau = 6.283185307179586;
let th = asin(s);
let cz = cos(th);
let spin = P.f[2].x;
let front = ftr_shade(ftr_texel((th + spin) / tau + 0.5, v), vec3<f32>(s, 0.0, cz));
let back = ftr_shade(ftr_texel((3.141592653589793 - th + spin) / tau + 0.5, v), vec3<f32>(-s, 0.0, cz));
textureStore(out, p, ftr_faces(P.u[0].x, front, back));
}
// Sphere: u[0].x = render; f[0] = (cx, cy, radius); f[7..10] = the rotation matrix rows.
fn ftr_sphere_tex(q: vec3<f32>) -> vec4<f32> {
let m0 = P.f[7].xyz;
let m1 = P.f[8].xyz;
let m2 = P.f[9].xyz;
let t = m0 * q.x + m1 * q.y + m2 * q.z;
let lat = asin(clamp(t.y, -1.0, 1.0));
return ftr_texel(ftr_lon_u(t.x, t.z), lat / 3.141592653589793 + 0.5);
}
@compute @workgroup_size(16, 16)
fn ftr_sphere(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let sxy = (ftr_centre(p) - P.f[0].xy) / P.f[0].z;
let d2 = sxy.x * sxy.x + sxy.y * sxy.y;
if (d2 >= 1.0) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
let z = sqrt(1.0 - d2);
let front = ftr_shade(ftr_sphere_tex(vec3<f32>(sxy, z)), vec3<f32>(sxy, z));
let back = ftr_shade(ftr_sphere_tex(vec3<f32>(sxy, -z)), vec3<f32>(-sxy, z));
textureStore(out, p, ftr_faces(P.u[0].x, front, back));
}
// ---------------------------------------------------------------- CC Spotlight (perspective::spotlight)
// u[0].x = light only; f[0] = (from x, y, height, intensity); f[1] = (axis, inner angle);
// f[2] = (colour, cone angle).
@compute @workgroup_size(16, 16)
fn ftr_spot(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let d = ftr_centre(p) - P.f[0].xy;
let v = normalize(vec3<f32>(d, -P.f[0].z));
let axis = P.f[1].xyz;
// acos(v · axis), well conditioned near the axis.
let ang = atan2(length(cross(v, axis)), dot(v, axis));
let m = (1.0 - ftr_smoothstep(P.f[1].w, P.f[2].w, ang)) * P.f[0].w;
let px = textureLoad(src, p, 0);
if (P.u[0].x != 0u) {
let a = clamp(m, 0.0, 1.0);
textureStore(out, p, vec4<f32>(P.f[2].xyz * a, a));
} else {
textureStore(out, p, vec4<f32>(px.xyz * (m * P.f[2].xyz), px.w));
}
}
// ---------------------------------------------------------------- CC Environment (perspective2::cc_environment)
// aux = the relief height map (x); data = the environment image (rows). u[0].x = mapping;
// f[0] = (1 / buffer scale, height, environment scale); f[1] = (environment offset, size).
fn ftr_env_uv(mapping: u32, r: vec3<f32>) -> vec2<f32> {
if (mapping == 1u) {
let m = 2.0 * max(sqrt(r.x * r.x + r.y * r.y + (r.z + 1.0) * (r.z + 1.0)), 1e-9);
return vec2<f32>(0.5 + r.x / m, 0.5 + r.y / m);
}
if (mapping == 2u) {
let ax = abs(r.x);
let ay = abs(r.y);
let az = abs(r.z);
var cr: vec4<f32>;
if (ax >= ay && ax >= az) {
if (r.x > 0.0) {
cr = vec4<f32>(2.0, 1.0, -r.z / ax, r.y / ax);
} else {
cr = vec4<f32>(0.0, 1.0, r.z / ax, r.y / ax);
}
} else if (ay >= az) {
if (r.y < 0.0) {
cr = vec4<f32>(1.0, 0.0, r.x / ay, -r.z / ay);
} else {
cr = vec4<f32>(1.0, 2.0, r.x / ay, r.z / ay);
}
} else if (r.z > 0.0) {
cr = vec4<f32>(1.0, 1.0, r.x / az, r.y / az);
} else {
cr = vec4<f32>(1.0, 3.0, r.x / az, -r.y / az);
}
return vec2<f32>((cr.x + (cr.z + 1.0) * 0.5) / 3.0, (cr.y + (cr.w + 1.0) * 0.5) / 4.0);
}
let lat = asin(clamp(r.y, -1.0, 1.0));
return vec2<f32>(ftr_lon_u(r.x, r.z), 0.5 + lat / 3.141592653589793);
}
@compute @workgroup_size(16, 16)
fn ftr_env(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let a = textureLoad(src, p, 0).w;
if (a <= 0.0) {
textureStore(out, p, vec4<f32>(0.0));
return;
}
let g = ftr_grad(p) * P.f[0].x;
let height = P.f[0].y;
let n = normalize(vec3<f32>(-g.x * height, -g.y * height, 1.0));
let d = -n.z;
let r = vec3<f32>(-2.0 * d * n.x, -2.0 * d * n.y, -(-1.0 - 2.0 * d * n.z));
let uv = ftr_env_uv(P.u[0].x, r);
let es = P.f[0].z;
let ex = fract_euclid(uv.x) * P.f[1].z * es + P.f[1].x;
let ey = clamp(uv.y, 0.0, 1.0) * P.f[1].w * es + P.f[1].y;
let env = ftr_row_bilinear_clamped(ex, ey);
let ec = env.xyz * (1.0 / max(env.w, 1e-3));
textureStore(out, p, vec4<f32>(ec * a, a));
}
// ---------------------------------------------------------------- 3D Glasses (perspective2::glasses_3d)
// Translate by f[0].xy (perspective2::shift).
@compute @workgroup_size(16, 16)
fn ftr_shift(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let f = ftr_centre(p) - P.f[0].xy;
textureStore(out, p, sample_bilinear(src, f.x, f.y));
}
// src = left view, aux = right view. u[0].x = 3D view; f[0] = (width, height, balance).
@compute @workgroup_size(16, 16)
fn ftr_glasses(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
if (!ftr_inside(p)) {
return;
}
let f = ftr_centre(p);
let w = P.f[0].x;
let h = P.f[0].y;
let k = P.f[0].z;
let view = P.u[0].x;
var o: vec4<f32>;
if (view == 0u) {
if (f.x < w * 0.5) {
o = sample_bilinear(src, f.x * 2.0, f.y);
} else {
o = sample_bilinear(aux, (f.x - w * 0.5) * 2.0, f.y);
}
} else if (view == 1u) {
if (f.y < h * 0.5) {
o = sample_bilinear(src, f.x, f.y * 2.0);
} else {
o = sample_bilinear(aux, f.x, (f.y - h * 0.5) * 2.0);
}
} else if (view == 2u) {
if (p.y % 2 == 0) {
o = textureLoad(src, p, 0);
} else {
o = textureLoad(aux, p, 0);
}
} else {
let lp = textureLoad(src, p, 0);
let rp = textureLoad(aux, p, 0);
let lc = ftr_unpremul(lp);
let rc = ftr_unpremul(rp);
let a = max(max(lp.w, 0.0), max(rp.w, 0.0));
let ll = luminance(lc);
let rl = luminance(rc);
var c: vec3<f32>;
switch view {
case 3u: {
c = vec3<f32>(ll, rl, 0.0);
}
case 4u: {
c = vec3<f32>(ll, 0.0, rl);
}
case 5u: {
c = vec3<f32>(lc.x + (ll - lc.x) * k, rc.y, rc.z);
}
case 6u: {
c = vec3<f32>(ll * (1.0 - 0.5 * k) + lc.x * 0.5 * k, rl, 0.0);
}
case 7u: {
c = vec3<f32>(ll * (1.0 - 0.5 * k) + lc.x * 0.5 * k, 0.0, rl);
}
default: {
c = abs(lc - rc);
}
}
o = vec4<f32>(c * a, a);
}
textureStore(out, p, o);
}
+32
View File
@@ -0,0 +1,32 @@
// Environment Light Background layers (`three_d::compose::draw_sky`): the layer's
// equirectangular image looked up by each pixel's view ray through the camera, composited over
// the canvas. src = the canvas, aux = the layer's buffer. f[0] = (forward, zoom); f[1] =
// (right, orthographic); f[2] = (down, environment rotation); f[3] = (x offset, y offset,
// 1 / scale, opacity) with the offsets ROI / scale − view size / 2; f[4] = (image width, height).
@compute @workgroup_size(16, 16)
fn adv_sky(@builtin(global_invocation_id) gid: vec3<u32>) {
let p = vec2<i32>(gid.xy);
let dims = out_dims();
if (p.x >= dims.x || p.y >= dims.y) {
return;
}
let fwd = P.f[0].xyz;
var dir = fwd;
if (P.f[1].w == 0.0) {
let cx = (f32(p.x) + 0.5) * P.f[3].z + P.f[3].x;
let cy = (f32(p.y) + 0.5) * P.f[3].z + P.f[3].y;
dir = normalize(fwd * P.f[0].w + P.f[1].xyz * cx + P.f[2].xyz * cy);
}
// shade::equirect_uv.
let pi = 3.141592653589793;
var u = 0.5 + (atan2(dir.x, dir.z) + P.f[2].w) / (2.0 * pi);
let v = acos(clamp(-dir.y, -1.0, 1.0)) / pi;
u = u - floor(u);
let iw = P.f[4].x;
let ih = P.f[4].y;
let s = sample_bilinear(aux, clamp(u * iw, 0.5, iw - 0.5), clamp(v * ih, 0.5, ih - 0.5));
let op = P.f[3].w;
let d = textureLoad(src, p, 0);
textureStore(out, p, s * op + d * (1.0 - s.w * op));
}