Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

feat(effects): multi-pass packages - the Gaussian blur separable, glows drawn at a quarter of the layer

Jareer7a5e61744a94
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index a63a5961..9cd228d0 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -148,7 +148,7 @@ and for the CPU path's history.
flowchart TD
pkg["package folder<br/>effect.toml + effect.wgsl"]
cat["Catalogue<br/>(concat-effects::catalogue)<br/>parsed, validated, kept"]
- pass["ShaderPass<br/>package id, compiled source,<br/>params bytes laid out to the struct,<br/>values by key, intensity, LUT"]
+ pass["ShaderPass<br/>package id, compiled source,<br/>params bytes laid out to the struct,<br/>values by key, intensity, LUT,<br/>stages before the last"]
gpu["GPU: the shader runs<br/>params as a uniform buffer"]
cpu["CPU: kernels.rs<br/>a kernel per known package,<br/>else untreated, said once"]
@@ -170,6 +170,19 @@ GPU over a 512-pixel picture against a three-second timeout
that fails; a pipeline the driver refuses at draw time is caught in an
error scope and the pass skipped, never an uncaptured error.
+A format 2 package may draw in several passes. Each `[[wgsl.pass]]` names a
+picture, `fn <target>(uv)` draws it, and every pass after it - `effect`, the
+last, among them - reads it through `<target>_at(uv)` and measures it with
+`<target>_texel()`; the pictures are bound in group 0 after the layer, and a
+pass that reads a picture not drawn before it is refused at load. A
+picture's `shrink` is two expressions over the knobs, rounded down to a
+power of two up to 64, so a blur is drawn across and down at a fraction of
+the layer's pixels (`concat.gaussian-blur`, `concat.glow`). The pass carries
+its stages (`concat_core::Stage`); `WgpuCompositor::run_stages` draws them
+in one submission into pictures claimed from the pool and handed on from
+one package to the next of a layer's stack, and only the last pass mixes by
+intensity.
+
## 4. Decoding, caching and scheduling
Export decodes every frame once, in order, with one decoder per clip. Everything
@@ -367,9 +380,8 @@ time to present a token.
fast path at its per-frame step (`concat-host/src/enhance.rs`, the
`enhancer.enhance` call), never as a second feature. Frame interpolation
does not fit that step: it changes the frame count and the encoder's rate.
-- A package's `[[wgsl.pass]]` list (`target`, `size` over `WIDTH` and
- `HEIGHT`; `concat-effects/src/manifest.rs`) is parsed and never read:
- `run_passes` (`concat-render/src/gpu.rs`) runs one pass per applied effect
- at the source's size, with no named intermediates. Wiring it up is what a
- GPU upscaler package (FSR 1.0, Anime4K, both MIT with WGSL ports) needs to
- write a larger picture than it reads.
+- A package's passes draw pictures at most the layer's size
+ (`[[wgsl.pass]]` `shrink`), and the last draws the layer's size: a GPU
+ upscaler package (FSR 1.0, Anime4K, both MIT with WGSL ports) needs a
+ pass that writes a larger picture than it reads, and a layer whose size
+ can change on its way through its effects.
diff --git a/src/crates/concat-core/src/lib.rs b/src/crates/concat-core/src/lib.rs
index 9f3509e9..0c2fbced 100644
--- a/src/crates/concat-core/src/lib.rs
+++ b/src/crates/concat-core/src/lib.rs
@@ -26,6 +26,6 @@ pub mod timeline;
pub use frame::Frame;
pub use retime::SpeedCurve;
-pub use shader::{Lut, RevealMap, ShaderPass, TransitionPass};
+pub use shader::{Lut, RevealMap, ShaderPass, Stage, TransitionPass};
pub use time::{FrameRate, Rational, TimeRange};
pub use timeline::{Blend, Clip, ClipId, Project, Timeline, Track, TrackId, TrackKind};
diff --git a/src/crates/concat-core/src/shader.rs b/src/crates/concat-core/src/shader.rs
index a05e7700..3e022767 100644
--- a/src/crates/concat-core/src/shader.rs
+++ b/src/crates/concat-core/src/shader.rs
@@ -12,6 +12,12 @@
//! uniform buffer as it is, because the catalogue already laid the bytes
//! out the way the shader's `Params` struct wants them.
//!
+//! A package may draw in several passes - a blur across and then down, a
+//! glow at a quarter of the layer's pixels - and then the pass carries its
+//! [`Stage`]s: the passes before the last, each drawing a picture of its own
+//! that the ones after it read. They share the module, the uniforms and the
+//! intensity; only the last mixes by it.
+//!
//! The parameters are uniforms, never strings: a knob with keys is worth
//! something different each frame, and the catalogue resolves it to the
//! value for the frame and writes the buffer. The same resolved values
@@ -56,6 +62,10 @@ pub struct ShaderPass {
/// so the call is harmless for a pass over anything that is not a
/// title. See [`RevealMap`].
pub reveal_map: Option<Arc<RevealMap>>,
+ /// The passes drawn before the last, in order, each into a picture of
+ /// its own that every pass after it may read: empty for a package drawn
+ /// in one pass, which is most of them. The last pass is `fs_main`.
+ pub stages: Vec<Stage>,
}
impl ShaderPass {
@@ -64,6 +74,28 @@ impl ShaderPass {
pub const MIN_PARAMS: usize = 16;
}
+/// One pass of a package drawn in several, before its last: which entry
+/// point of the module draws it, and how much smaller than the layer the
+/// picture it draws is.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct Stage {
+ /// The fragment entry point that draws it.
+ pub entry: String,
+ /// How many times smaller than the layer the picture is, across and
+ /// down: a power of two, 1 the layer's own size.
+ pub shrink: [u32; 2],
+}
+
+impl Stage {
+ /// The size of the picture the stage draws over a layer `width` by
+ /// `height`: the layer's divided by the shrink, rounded up, never
+ /// nothing.
+ pub fn size(&self, width: u32, height: u32) -> (u32, u32) {
+ let [across, down] = self.shrink.map(|shrink| shrink.max(1));
+ (width.div_ceil(across).max(1), height.div_ceil(down).max(1))
+ }
+}
+
/// One two-input transition combine, as the compositor runs it.
///
/// Where a [`ShaderPass`] changes one layer, a transition reads two - the
@@ -300,6 +332,21 @@ mod tests {
assert_eq!(map.gray[9 * 10 + 9], 0);
}
+ /// A stage's picture is the layer's size shrunk and rounded up, never
+ /// less than a pixel, and a shrink of nothing is none.
+ #[test]
+ fn a_stage_draws_the_layer_shrunk_and_rounded_up() {
+ let stage = |shrink: [u32; 2]| Stage {
+ entry: "fs_small".to_owned(),
+ shrink,
+ };
+ assert_eq!(stage([1, 1]).size(1920, 1080), (1920, 1080));
+ assert_eq!(stage([4, 1]).size(1920, 1080), (480, 1080));
+ assert_eq!(stage([16, 16]).size(1921, 1080), (121, 68));
+ assert_eq!(stage([64, 64]).size(8, 8), (1, 1));
+ assert_eq!(stage([0, 2]).size(10, 10), (10, 5));
+ }
+
#[test]
fn a_single_word_reveals_at_the_very_start() {
let map = RevealMap::from_rects(4, 4, &[(0, 0, 2, 2)]);
diff --git a/src/crates/concat-effects/packages/concat.bloom-pulse/effect.toml b/src/crates/concat-effects/packages/concat.bloom-pulse/effect.toml
index a5382a2d..6474423e 100644
--- a/src/crates/concat-effects/packages/concat.bloom-pulse/effect.toml
+++ b/src/crates/concat-effects/packages/concat.bloom-pulse/effect.toml
@@ -1,9 +1,12 @@
+format = 2
+
[effect]
id = "concat.bloom-pulse"
name = "Bloom Pulse"
kind = "effect"
category = "Light"
order = 72
+version = 2
intensity = "amount"
description = "A soft bloom over the highlights that breathes in and out."
@@ -24,8 +27,15 @@ max = 5
default = 2
step = 0.1
-[ffmpeg]
-chain = "split[blma{index}][blmb{index}];[blmb{index}]gblur=sigma=14[blmg{index}];[blma{index}][blmg{index}]blend=all_mode=screen:all_opacity={fixed(amount / 100, 3)}"
-
[wgsl]
entry = "effect.wgsl"
+
+# The bloom is the highlights' light spread by a Gaussian, drawn across and
+# then down at a quarter of the layer's pixels, three of which it is wide.
+[[wgsl.pass]]
+target = "across"
+shrink = ["4", "1"]
+
+[[wgsl.pass]]
+target = "down"
+shrink = ["4", "4"]
diff --git a/src/crates/concat-effects/packages/concat.bloom-pulse/effect.wgsl b/src/crates/concat-effects/packages/concat.bloom-pulse/effect.wgsl
index fb9e9d95..1f714a3a 100644
--- a/src/crates/concat-effects/packages/concat.bloom-pulse/effect.wgsl
+++ b/src/crates/concat-effects/packages/concat.bloom-pulse/effect.wgsl
@@ -1,8 +1,79 @@
struct Params { amount: f32, speed: f32 }
+// The highlights' light - each pixel weighed by how far its display level
+// is past 0.55, the knee of the bloom - spread by a Gaussian of twelve of
+// the layer's pixels, across and then down at a quarter of them, and
+// screened back over the picture by the amount as it breathes. The weight
+// is taken to the 2.4th power in light, so a flat field blooms by the same
+// share of its display level it always did; the screen is taken in the
+// display encoding, a level past white carried on past it.
+
+const SIGMA: f32 = 12.0;
+
+/// How far the taps reach, in sigmas.
+const REACH: f32 = 3.0;
+
+fn weight(d: f32, s: f32) -> f32 {
+ return exp(-d * d / (2.0 * s * s));
+}
+
+/// The light of the layer's highlights at `uv`, premultiplied.
+fn bright_at(uv: vec2<f32>) -> vec4<f32> {
+ let c = sample(uv);
+ let bright = pow(smoothstep(0.55, 1.0, luma(to_display(c.rgb))), 2.4);
+ return vec4<f32>(c.rgb * c.a * bright, c.a);
+}
+
+/// The highlights weighed along their row about `uv`, two pixels to a fetch.
+fn across(uv: vec2<f32>) -> vec4<f32> {
+ let width = frame.size.x;
+ let centre = uv.x * width;
+ let first = floor(centre - REACH * SIGMA - 0.5) + 0.5;
+ let pairs = i32(ceil(REACH * SIGMA)) + 1;
+ var sum = vec4<f32>(0.0);
+ var total = 0.0;
+ for (var i = 0; i < pairs; i++) {
+ let a = first + f32(2 * i);
+ let wa = weight(a - centre, SIGMA);
+ let wb = weight(a + 1.0 - centre, SIGMA);
+ let w = wa + wb;
+ sum += bright_at(vec2<f32>((a + wb / w) / width, uv.y)) * w;
+ total += w;
+ }
+ return sum / total;
+}
+
+/// `across` weighed down its column about `uv`, in its own pixels.
+fn down(uv: vec2<f32>) -> vec4<f32> {
+ let rows = 1.0 / across_texel().y;
+ let s = SIGMA * rows / frame.size.y;
+ let centre = uv.y * rows;
+ let first = floor(centre - REACH * s - 0.5) + 0.5;
+ let pairs = i32(ceil(REACH * s)) + 1;
+ var sum = vec4<f32>(0.0);
+ var total = 0.0;
+ for (var i = 0; i < pairs; i++) {
+ let a = first + f32(2 * i);
+ let wa = weight(a - centre, s);
+ let wb = weight(a + 1.0 - centre, s);
+ let w = wa + wb;
+ sum += across_at(vec2<f32>(uv.x, (a + wb / w) / rows)) * w;
+ total += w;
+ }
+ return sum / total;
+}
+
+/// `over` screened onto `base`, both display-encoded: light added the way
+/// two projectors add it, a level of `base` past white kept past it.
+fn screen(base: vec3<f32>, over: vec3<f32>) -> vec3<f32> {
+ let b = min(base, vec3<f32>(1.0));
+ return vec3<f32>(1.0) - (vec3<f32>(1.0) - b) * (vec3<f32>(1.0) - over) + (base - b);
+}
+
fn effect(uv: vec2<f32>) -> vec4<f32> {
let c = sample(uv);
let pulse = 0.6 + 0.4 * sin(frame.time * params.speed);
- let glow = halation(uv, c.rgb, 0.55, 12.0, vec3<f32>(1.0), params.amount * 0.01 * pulse);
- return vec4<f32>(glow, c.a);
+ let strength = params.amount * 0.01 * pulse;
+ let bloom = clamp(to_display(down_at(uv).rgb) * strength, vec3<f32>(0.0), vec3<f32>(1.0));
+ return vec4<f32>(from_display(screen(to_display(c.rgb), bloom)), c.a);
}
diff --git a/src/crates/concat-effects/packages/concat.bloom-pulse/fixtures.toml b/src/crates/concat-effects/packages/concat.bloom-pulse/fixtures.toml
index b72ff153..d7b794f3 100644
--- a/src/crates/concat-effects/packages/concat.bloom-pulse/fixtures.toml
+++ b/src/crates/concat-effects/packages/concat.bloom-pulse/fixtures.toml
@@ -1,13 +1,42 @@
-[[case]]
-name = "default"
-chain = "split[blma0][blmb0];[blmb0]gblur=sigma=14[blmg0];[blma0][blmg0]blend=all_mode=screen:all_opacity=0.500"
+# A flat picture blooms by its own highlights: nothing below the knee (a
+# display level of 0.55), a share of the level above it, screened over the
+# picture by the amount and the pulse - 0.6 at the timeline's start - and a
+# level past white stays where it was.
-[[case]]
-name = "min"
+[[probe]]
+name = "default, a grey below the knee"
+input = [0.18, 0.18, 0.18, 1]
+expect = [0.18, 0.18, 0.18, 1]
+
+[[probe]]
+name = "default, a bright grey"
+input = [0.8, 0.8, 0.8, 1]
+expect = [0.84673, 0.84673, 0.84673, 1]
+
+[[probe]]
+name = "default, a warm white"
+input = [1.0, 0.6, 0.2, 1]
+expect = [1.0, 0.65718, 0.2506, 1]
+
+[[probe]]
+name = "default, a highlight past white"
+input = [4.0, 1.0, 0.05, 1]
+expect = [4.0, 1.0, 0.079617, 1]
+
+[[probe]]
+name = "minimum, no bloom at all"
at = "min"
-chain = "split[blma0][blmb0];[blmb0]gblur=sigma=14[blmg0];[blma0][blmg0]blend=all_mode=screen:all_opacity=0.000"
+input = [1.0, 0.6, 0.2, 1]
+expect = [1.0, 0.6, 0.2, 1]
+
+[[probe]]
+name = "maximum, a bright grey"
+at = "max"
+input = [0.8, 0.8, 0.8, 1]
+expect = [0.89501, 0.89501, 0.89501, 1]
-[[case]]
-name = "max"
+[[probe]]
+name = "maximum, a highlight past white"
at = "max"
-chain = "split[blma0][blmb0];[blmb0]gblur=sigma=14[blmg0];[blma0][blmg0]blend=all_mode=screen:all_opacity=1.000"
+input = [4.0, 1.0, 0.05, 1]
+expect = [4.0, 1.0, 0.11753, 1]
diff --git a/src/crates/concat-effects/packages/concat.gaussian-blur/effect.toml b/src/crates/concat-effects/packages/concat.gaussian-blur/effect.toml
index c131746e..8c576152 100644
--- a/src/crates/concat-effects/packages/concat.gaussian-blur/effect.toml
+++ b/src/crates/concat-effects/packages/concat.gaussian-blur/effect.toml
@@ -1,9 +1,12 @@
+format = 2
+
[effect]
id = "concat.gaussian-blur"
name = "Gaussian Blur"
kind = "effect"
category = "Blur"
order = 20
+version = 2
intensity = "radius"
aliases = ["gaussian-blur"]
description = "A soft, even blur."
@@ -17,8 +20,18 @@ default = 10
step = 1
unit = "px"
-[ffmpeg]
-chain = "gblur=sigma={fixed(radius, 1)}"
-
[wgsl]
entry = "effect.wgsl"
+
+# Across the layer, then down what that drew, then `effect` spreads it back
+# over the layer. From a radius of six the first two are drawn at a power of
+# two fewer pixels, as many as keep the Gaussian three of them wide: it has
+# nothing finer in it for them to lose. Below six `effect` blurs down
+# `across` itself, and `down` is drawn at a sliver of the layer and unread.
+[[wgsl.pass]]
+target = "across"
+shrink = ["radius / 3", "1"]
+
+[[wgsl.pass]]
+target = "down"
+shrink = ["if(radius < 6, 64, radius / 3)", "if(radius < 6, 64, radius / 3)"]
diff --git a/src/crates/concat-effects/packages/concat.gaussian-blur/effect.wgsl b/src/crates/concat-effects/packages/concat.gaussian-blur/effect.wgsl
index 45c64b38..6255e460 100644
--- a/src/crates/concat-effects/packages/concat.gaussian-blur/effect.wgsl
+++ b/src/crates/concat-effects/packages/concat.gaussian-blur/effect.wgsl
@@ -1,20 +1,128 @@
struct Params { radius: f32 }
-// A Gaussian of sigma `radius` pixels, sampled on a 9×9 grid whose spacing
-// grows with the sigma; bilinear filtering fills the gaps at large radii.
-fn effect(uv: vec2<f32>) -> vec4<f32> {
- let t = texel();
- let sigma = max(params.radius, 0.5);
- let step = max(sigma / 2.5, 1.0);
+// A Gaussian of sigma `radius` of the layer's pixels, in three passes:
+// `across` weighs the layer along its rows, `down` weighs that down its
+// columns, and `effect` reads the result back over the layer. A 2-D
+// Gaussian is the product of the two, so this is the whole of it; the
+// manifest draws the first two at a power of two fewer pixels where the
+// radius is wide, which the last pass's bilinear reading fills back in.
+//
+// Each pass weighs the pixel centres within three sigmas by the Gaussian of
+// their distance, two at a time: one bilinear fetch placed between two
+// centres by their weights is both of them. Light is blurred premultiplied
+// by its alpha, so a transparent pixel's colour never bleeds into the
+// picture beside it, and `effect` divides it back out.
+
+/// How far the taps reach, in sigmas: all but 0.27 % of the Gaussian.
+const REACH: f32 = 3.0;
+
+fn sigma() -> f32 {
+ return max(params.radius, 0.5);
+}
+
+/// The Gaussian's weight at `d` pixels from its centre, of sigma `s`.
+fn weight(d: f32, s: f32) -> f32 {
+ return exp(-d * d / (2.0 * s * s));
+}
+
+/// Two pixels of the layer side by side on row `y`, the left one's centre
+/// at `x`, premultiplied and weighed `wa` and `wb`. A colour cannot be
+/// premultiplied once it is filtered, so the one fetch between them serves
+/// only where they are as opaque as each other - all of an opaque picture;
+/// elsewhere each is read on its own. The layer is read as the texture
+/// holds it, which is what `sample` hands a scene-linear package, without
+/// derivatives, so the reading may turn on what it reads.
+fn pair(x: f32, y: f32, wa: f32, wb: f32) -> vec4<f32> {
+ let w = wa + wb;
+ let both = textureSampleLevel(source, source_sampler, vec2<f32>(x + wb / w, y) / frame.size, 0.0);
+ if (abs(both.a - round(both.a)) < 1e-4) {
+ return vec4<f32>(both.rgb * both.a, both.a) * w;
+ }
+ let row = i32(floor(y));
+ let last = i32(frame.size.x) - 1;
+ let left = i32(floor(x));
+ let a = textureLoad(source, vec2<i32>(clamp(left, 0, last), row), 0);
+ let b = textureLoad(source, vec2<i32>(clamp(left + 1, 0, last), row), 0);
+ return vec4<f32>(a.rgb * a.a, a.a) * wa + vec4<f32>(b.rgb * b.a, b.a) * wb;
+}
+
+/// The layer weighed along its row about `uv`.
+fn across(uv: vec2<f32>) -> vec4<f32> {
+ let s = sigma();
+ let centre = uv.x * frame.size.x;
+ let row = uv.y * frame.size.y;
+ let first = floor(centre - REACH * s - 0.5) + 0.5;
+ let pairs = i32(ceil(REACH * s)) + 1;
var sum = vec4<f32>(0.0);
- var weight = 0.0;
- for (var y: i32 = -4; y <= 4; y++) {
- for (var x: i32 = -4; x <= 4; x++) {
- let d = vec2<f32>(f32(x), f32(y)) * step;
- let w = exp(-dot(d, d) / (2.0 * sigma * sigma));
- sum += sample(uv + d * t) * w;
- weight += w;
- }
+ var total = 0.0;
+ for (var i = 0; i < pairs; i++) {
+ let a = first + f32(2 * i);
+ let wa = weight(a - centre, s);
+ let wb = weight(a + 1.0 - centre, s);
+ sum += pair(a, row, wa, wb);
+ total += wa + wb;
+ }
+ return sum / total;
+}
+
+/// `across` weighed down its column about `uv`, in its own pixels.
+fn column(uv: vec2<f32>) -> vec4<f32> {
+ let rows = 1.0 / across_texel().y;
+ let s = sigma() * rows / frame.size.y;
+ let centre = uv.y * rows;
+ let first = floor(centre - REACH * s - 0.5) + 0.5;
+ let pairs = i32(ceil(REACH * s)) + 1;
+ var sum = vec4<f32>(0.0);
+ var total = 0.0;
+ for (var i = 0; i < pairs; i++) {
+ let a = first + f32(2 * i);
+ let wa = weight(a - centre, s);
+ let wb = weight(a + 1.0 - centre, s);
+ let w = wa + wb;
+ sum += across_at(vec2<f32>(uv.x, (a + wb / w) / rows)) * w;
+ total += w;
+ }
+ return sum / total;
+}
+
+fn down(uv: vec2<f32>) -> vec4<f32> {
+ return column(uv);
+}
+
+/// `down` read back over the layer. Between its pixel centres that is its
+/// bilinear filtering; past the outermost ones - the layer's last few
+/// pixels at an edge, when `down` has fewer - the blur goes on as it was
+/// going, its slope taken from the pixel in from the edge, where clamping
+/// would hold it flat.
+fn spread(uv: vec2<f32>) -> vec4<f32> {
+ let size = 1.0 / down_texel();
+ let at = uv * size - 0.5;
+ let inside = clamp(at, vec2<f32>(0.0), size - 1.0);
+ var blurred = down_at((inside + 0.5) / size);
+ let over = at - inside;
+ if (any(over != vec2<f32>(0.0))) {
+ let back = -sign(over);
+ let x = down_at((inside + vec2<f32>(back.x, 0.0) + 0.5) / size);
+ let y = down_at((inside + vec2<f32>(0.0, back.y) + 0.5) / size);
+ let xy = down_at((inside + back + 0.5) / size);
+ let d = abs(over);
+ blurred += (blurred - x) * d.x + (blurred - y) * d.y + (blurred - x - y + xy) * d.x * d.y;
+ }
+ return blurred;
+}
+
+/// Premultiplied light back to a straight colour and its alpha.
+fn straight(blurred: vec4<f32>) -> vec4<f32> {
+ let alpha = clamp(blurred.a, 0.0, 1.0);
+ return vec4<f32>(blurred.rgb / max(alpha, 1e-6), alpha);
+}
+
+/// Below a radius of six `across` keeps the layer's width (see the
+/// manifest), and blurring down it here spares drawing `down` the layer's
+/// size only to read it back; `down` is then a sliver nothing reads.
+fn effect(uv: vec2<f32>) -> vec4<f32> {
+ if (across_texel().x * frame.size.x < 1.5) {
+ return straight(column(uv));
}
- return sum / weight;
+ return straight(spread(uv));
}
diff --git a/src/crates/concat-effects/packages/concat.gaussian-blur/fixtures.toml b/src/crates/concat-effects/packages/concat.gaussian-blur/fixtures.toml
index d3f3d88c..b9e0c0e0 100644
--- a/src/crates/concat-effects/packages/concat.gaussian-blur/fixtures.toml
+++ b/src/crates/concat-effects/packages/concat.gaussian-blur/fixtures.toml
@@ -1,13 +1,37 @@
-[[case]]
-name = "default"
-chain = "gblur=sigma=10.0"
+# A flat picture blurs to itself at any radius, through every pass: what
+# these pin is that the light past white and the alpha come out as they went
+# in, at every size the passes' pictures take. How the blur spreads a
+# picture is held to a direct 2-D Gaussian in concat-render's suite.
-[[case]]
-name = "minimum"
+[[probe]]
+name = "default, a grey"
+input = [0.18, 0.18, 0.18, 1]
+expect = [0.18, 0.18, 0.18, 1]
+
+[[probe]]
+name = "default, a highlight past white"
+input = [4.0, 1.0, 0.05, 1]
+expect = [4.0, 1.0, 0.05, 1]
+
+[[probe]]
+name = "default, half transparent"
+input = [0.5, 0.2, 0.1, 0.5]
+expect = [0.5, 0.2, 0.1, 0.5]
+
+[[probe]]
+name = "minimum, a highlight past white"
at = "min"
-chain = "gblur=sigma=1.0"
+input = [4.0, 1.0, 0.05, 1]
+expect = [4.0, 1.0, 0.05, 1]
+
+[[probe]]
+name = "maximum, a highlight past white"
+at = "max"
+input = [4.0, 1.0, 0.05, 1]
+expect = [4.0, 1.0, 0.05, 1]
-[[case]]
-name = "maximum"
+[[probe]]
+name = "maximum, half transparent"
at = "max"
-chain = "gblur=sigma=50.0"
+input = [0.5, 0.2, 0.1, 0.5]
+expect = [0.5, 0.2, 0.1, 0.5]
diff --git a/src/crates/concat-effects/packages/concat.glow/effect.toml b/src/crates/concat-effects/packages/concat.glow/effect.toml
index 64c3c381..3173b0f6 100644
--- a/src/crates/concat-effects/packages/concat.glow/effect.toml
+++ b/src/crates/concat-effects/packages/concat.glow/effect.toml
@@ -1,9 +1,12 @@
+format = 2
+
[effect]
id = "concat.glow"
name = "Glow"
kind = "effect"
category = "Stylize"
order = 42
+version = 2
intensity = "amount"
aliases = ["glow"]
description = "A soft bloom around the bright parts of the picture."
@@ -17,8 +20,15 @@ default = 45
step = 1
unit = "%"
-[ffmpeg]
-chain = "split[glowa{index}][glowb{index}];[glowb{index}]gblur=sigma=18[glowg{index}];[glowa{index}][glowg{index}]blend=all_mode=screen:all_opacity={fixed(amount / 100, 2)}"
-
[wgsl]
entry = "effect.wgsl"
+
+# The glow is a wide Gaussian, drawn across and then down at a quarter of
+# the layer's pixels, four of which it is wide.
+[[wgsl.pass]]
+target = "across"
+shrink = ["4", "1"]
+
+[[wgsl.pass]]
+target = "down"
+shrink = ["4", "4"]
diff --git a/src/crates/concat-effects/packages/concat.glow/effect.wgsl b/src/crates/concat-effects/packages/concat.glow/effect.wgsl
index 91e9f6e2..86c1020a 100644
--- a/src/crates/concat-effects/packages/concat.glow/effect.wgsl
+++ b/src/crates/concat-effects/packages/concat.glow/effect.wgsl
@@ -1,21 +1,75 @@
struct Params { amount: f32 }
-// A wide soft blur, screened over the picture by the amount.
-fn effect(uv: vec2<f32>) -> vec4<f32> {
- let t = texel();
+// The picture's light spread wide - a Gaussian of sixteen of the layer's
+// pixels, across and then down, at a quarter of them - and screened back
+// over the picture by the amount. The screen is taken in the display
+// encoding the glow was drawn in, so on SDR it reads as it always did; a
+// level past white is carried on past it rather than bent back. The light
+// is spread premultiplied, so a transparent pixel adds none.
+
+const SIGMA: f32 = 16.0;
+
+/// How far the taps reach, in sigmas.
+const REACH: f32 = 3.0;
+
+fn weight(d: f32, s: f32) -> f32 {
+ return exp(-d * d / (2.0 * s * s));
+}
+
+fn layer_at(uv: vec2<f32>) -> vec4<f32> {
let c = sample(uv);
- let step = 6.0;
- var blur = vec3<f32>(0.0);
- var weight = 0.0;
- for (var y: i32 = -3; y <= 3; y++) {
- for (var x: i32 = -3; x <= 3; x++) {
- let d = vec2<f32>(f32(x), f32(y));
- let w = exp(-dot(d, d) / 8.0);
- blur += sample(uv + d * step * t).rgb * w;
- weight += w;
- }
+ return vec4<f32>(c.rgb * c.a, c.a);
+}
+
+/// The layer weighed along its row about `uv`, two pixels to a fetch.
+fn across(uv: vec2<f32>) -> vec4<f32> {
+ let width = frame.size.x;
+ let centre = uv.x * width;
+ let first = floor(centre - REACH * SIGMA - 0.5) + 0.5;
+ let pairs = i32(ceil(REACH * SIGMA)) + 1;
+ var sum = vec4<f32>(0.0);
+ var total = 0.0;
+ for (var i = 0; i < pairs; i++) {
+ let a = first + f32(2 * i);
+ let wa = weight(a - centre, SIGMA);
+ let wb = weight(a + 1.0 - centre, SIGMA);
+ let w = wa + wb;
+ sum += layer_at(vec2<f32>((a + wb / w) / width, uv.y)) * w;
+ total += w;
}
- blur /= weight;
- let screen = vec3<f32>(1.0) - (vec3<f32>(1.0) - c.rgb) * (vec3<f32>(1.0) - blur);
- return vec4<f32>(mix(c.rgb, screen, params.amount / 100.0), c.a);
+ return sum / total;
+}
+
+/// `across` weighed down its column about `uv`, in its own pixels.
+fn down(uv: vec2<f32>) -> vec4<f32> {
+ let rows = 1.0 / across_texel().y;
+ let s = SIGMA * rows / frame.size.y;
+ let centre = uv.y * rows;
+ let first = floor(centre - REACH * s - 0.5) + 0.5;
+ let pairs = i32(ceil(REACH * s)) + 1;
+ var sum = vec4<f32>(0.0);
+ var total = 0.0;
+ for (var i = 0; i < pairs; i++) {
+ let a = first + f32(2 * i);
+ let wa = weight(a - centre, s);
+ let wb = weight(a + 1.0 - centre, s);
+ let w = wa + wb;
+ sum += across_at(vec2<f32>(uv.x, (a + wb / w) / rows)) * w;
+ total += w;
+ }
+ return sum / total;
+}
+
+/// `over` screened onto `base`, both display-encoded: light added the way
+/// two projectors add it, a level of `base` past white kept past it.
+fn screen(base: vec3<f32>, over: vec3<f32>) -> vec3<f32> {
+ let b = min(base, vec3<f32>(1.0));
+ return vec3<f32>(1.0) - (vec3<f32>(1.0) - b) * (vec3<f32>(1.0) - over) + (base - b);
+}
+
+fn effect(uv: vec2<f32>) -> vec4<f32> {
+ let c = sample(uv);
+ let base = to_display(c.rgb);
+ let glowing = screen(base, to_display(down_at(uv).rgb));
+ return vec4<f32>(from_display(mix(base, glowing, params.amount / 100.0)), c.a);
}
diff --git a/src/crates/concat-effects/packages/concat.glow/fixtures.toml b/src/crates/concat-effects/packages/concat.glow/fixtures.toml
index 76db5024..36fab9f1 100644
--- a/src/crates/concat-effects/packages/concat.glow/fixtures.toml
+++ b/src/crates/concat-effects/packages/concat.glow/fixtures.toml
@@ -1,13 +1,36 @@
-[[case]]
-name = "default"
-chain = "split[glowa0][glowb0];[glowb0]gblur=sigma=18[glowg0];[glowa0][glowg0]blend=all_mode=screen:all_opacity=0.45"
+# A flat picture's glow is the picture itself, screened over it by the
+# amount in the display encoding: a grey is lifted by a share of what lies
+# between it and white, and a level past white stays where it was.
-[[case]]
-name = "minimum"
+[[probe]]
+name = "default, a grey"
+input = [0.18, 0.18, 0.18, 1]
+expect = [0.29569, 0.29569, 0.29569, 1]
+
+[[probe]]
+name = "default, an orange"
+input = [0.8, 0.3, 0.05, 1]
+expect = [0.87887, 0.44406, 0.097502, 1]
+
+[[probe]]
+name = "default, a highlight past white"
+input = [4.0, 1.0, 0.05, 1]
+expect = [4.0, 1.0, 0.097502, 1]
+
+[[probe]]
+name = "minimum, a grey"
at = "min"
-chain = "split[glowa0][glowb0];[glowb0]gblur=sigma=18[glowg0];[glowa0][glowg0]blend=all_mode=screen:all_opacity=0.10"
+input = [0.18, 0.18, 0.18, 1]
+expect = [0.20285, 0.20285, 0.20285, 1]
+
+[[probe]]
+name = "maximum, a grey"
+at = "max"
+input = [0.18, 0.18, 0.18, 1]
+expect = [0.4844, 0.4844, 0.4844, 1]
-[[case]]
-name = "maximum"
+[[probe]]
+name = "maximum, a highlight past white"
at = "max"
-chain = "split[glowa0][glowb0];[glowb0]gblur=sigma=18[glowg0];[glowa0][glowg0]blend=all_mode=screen:all_opacity=1.00"
+input = [4.0, 1.0, 0.05, 1]
+expect = [4.0, 1.0, 0.18196, 1]
diff --git a/src/crates/concat-effects/src/lib.rs b/src/crates/concat-effects/src/lib.rs
index deed0356..9fff1ec4 100644
--- a/src/crates/concat-effects/src/lib.rs
+++ b/src/crates/concat-effects/src/lib.rs
@@ -360,12 +360,12 @@ mod tests {
fn stacked_effects_join_with_commas_in_applied_order() {
let catalogue = Catalogue::builtin();
assert_eq!(
- catalogue.video_chain(&[applied("gaussian-blur", &[]), applied("black-white", &[])]),
- "gblur=sigma=10.0,hue=s=0"
+ catalogue.video_chain(&[applied("box-blur", &[]), applied("black-white", &[])]),
+ "boxblur=6:1,hue=s=0"
);
assert_eq!(
- catalogue.video_chain(&[applied("black-white", &[]), applied("gaussian-blur", &[])]),
- "hue=s=0,gblur=sigma=10.0"
+ catalogue.video_chain(&[applied("black-white", &[]), applied("box-blur", &[])]),
+ "hue=s=0,boxblur=6:1"
);
}
@@ -388,9 +388,10 @@ mod tests {
#[test]
fn stacking_one_labelled_effect_twice_keeps_its_graph_labels_distinct() {
- let chain = Catalogue::builtin().video_chain(&[applied("glow", &[]), applied("glow", &[])]);
- assert!(chain.contains("[glowa0]"), "was: {chain}");
- assert!(chain.contains("[glowa1]"), "was: {chain}");
+ let chain = Catalogue::builtin()
+ .video_chain(&[applied("concat.bokeh", &[]), applied("concat.bokeh", &[])]);
+ assert!(chain.contains("[bka0]"), "was: {chain}");
+ assert!(chain.contains("[bka1]"), "was: {chain}");
}
#[test]
diff --git a/src/crates/concat-effects/src/manifest.rs b/src/crates/concat-effects/src/manifest.rs
index 32b15080..0656095b 100644
--- a/src/crates/concat-effects/src/manifest.rs
+++ b/src/crates/concat-effects/src/manifest.rs
@@ -256,7 +256,10 @@ pub struct Ffmpeg {
pub struct Wgsl {
/// The shader file, beside the manifest.
pub entry: String,
- /// Render passes, in order. Empty means one pass to the output.
+ /// The passes drawn before `effect`, in order, each into a picture of
+ /// its own (see [`Pass`]); `effect` is always the last, drawing the
+ /// result. Empty - most packages - is `effect` alone. A format 2
+ /// setting.
#[serde(default, rename = "pass")]
pub passes: Vec<Pass>,
/// What a format 2 shader works in; see [`Space`].
@@ -366,20 +369,34 @@ pub const XFADE_NAMES: &[&str] = &[
"revealdown",
];
-/// One render pass of a WGSL package.
+/// One `[[wgsl.pass]]`: a pass drawn before `effect`, into a picture of its
+/// own that every pass after it reads.
#[derive(Deserialize, Clone, PartialEq, Debug)]
#[serde(deny_unknown_fields)]
pub struct Pass {
- /// The texture the pass writes, readable by later passes. Absent for
- /// the final pass, which writes the output.
+ /// The picture the pass draws, by name. The shader's
+ /// `fn <target>(uv: vec2<f32>) -> vec4<f32>` draws it, and the passes
+ /// after it read it through `<target>_at(uv)`.
+ pub target: String,
+ /// How many times smaller than the layer the picture is, across and
+ /// down: two expressions over the knobs, each rounded down to a power
+ /// of two from 1 to [`MAX_SHRINK`]. Absent is the layer's own size.
#[serde(default)]
- pub target: Option<String>,
- /// The target's size as an expression over `WIDTH` and `HEIGHT`; absent
- /// means the output size.
- #[serde(default)]
- pub size: Option<String>,
+ pub shrink: Option<[String; 2]>,
}
+/// The most passes a package may draw before `effect`: each is a picture
+/// bound beside the layer, and a pass may bind sixteen.
+pub const MAX_PASSES: usize = 8;
+
+/// The most times smaller than the layer a pass's picture may be, across
+/// or down.
+pub const MAX_SHRINK: u32 = 64;
+
+/// Names a pass's picture may not have: the last pass's function, and the
+/// entry point that draws it.
+const TAKEN_TARGETS: &[&str] = &["effect", "main"];
+
fn is_ident(text: &str) -> bool {
let mut chars = text.chars();
matches!(chars.next(), Some(c) if c.is_ascii_lowercase() || c == '_')
@@ -552,6 +569,9 @@ impl Manifest {
is handed the gamma-encoded 0..1 picture already"
)));
}
+ if let Some(wgsl) = &self.wgsl {
+ self.validate_passes(&wgsl.passes)?;
+ }
// The picture is drawn on the GPU, in light, and nowhere else:
// a chain would run on eight bits the shader never sees.
if self.scene_linear() && self.effect.kind.is_visual() && self.ffmpeg.is_some() {
@@ -570,6 +590,44 @@ impl Manifest {
self.format >= SCENE_LINEAR
}
+ /// The passes before `effect` name their pictures once each, with
+ /// names a shader can spell and the host has not taken, a few of them,
+ /// and only from format 2. What their functions read, and what their
+ /// shrinks say, is the shader's to check (`crate::shader`).
+ fn validate_passes(&self, passes: &[Pass]) -> Result<(), Error> {
+ if passes.is_empty() {
+ return Ok(());
+ }
+ if !self.scene_linear() {
+ return Err(self.invalid(format!("[[wgsl.pass]] is a format {SCENE_LINEAR} setting")));
+ }
+ if passes.len() > MAX_PASSES {
+ return Err(self.invalid(format!(
+ "{} passes before `effect`; a package may draw {MAX_PASSES}",
+ passes.len()
+ )));
+ }
+ let mut seen = std::collections::HashSet::new();
+ for pass in passes {
+ let target = pass.target.as_str();
+ if !is_ident(target) || !target.starts_with(|c: char| c.is_ascii_lowercase()) {
+ return Err(self.invalid(format!(
+ "pass target `{target}` must be a lower-case letter, then letters, digits \
+ and underscores"
+ )));
+ }
+ if TAKEN_TARGETS.contains(&target) {
+ return Err(self.invalid(format!(
+ "pass target `{target}` is taken: `effect` is the last pass"
+ )));
+ }
+ if !seen.insert(target) {
+ return Err(self.invalid(format!("pass target `{target}` is drawn twice")));
+ }
+ }
+ Ok(())
+ }
+
/// A `[card]` table sets only knobs the package has, within their
/// ranges, and places the card by what kind of package it is.
fn validate_card(&self, card: &CardSettings) -> Result<(), Error> {
@@ -815,6 +873,51 @@ mod tests {
Manifest::parse(&format!("format = 2\n{audio}")).expect("audio keeps its chain");
}
+ /// Passes before `effect` are a format 2 setting; each names a picture
+ /// once, a name a shader can spell and the host has not taken, and a
+ /// package draws a few of them.
+ #[test]
+ fn passes_name_their_pictures_once_from_format_2() {
+ let shader = GOOD.replace(
+ "[ffmpeg]\n chain = \"gblur=sigma={fixed(radius, 1)}\"",
+ "[wgsl]\n entry = \"effect.wgsl\"",
+ );
+ let with = |passes: &str| format!("format = 2\n{shader}\n{passes}");
+ let pass = |target: &str| format!("[[wgsl.pass]]\ntarget = \"{target}\"\n");
+ let manifest = Manifest::parse(&with(&format!(
+ "{}shrink = [\"radius / 3\", \"1\"]\n{}",
+ pass("across"),
+ pass("down")
+ )))
+ .expect("parses");
+ let passes = manifest.wgsl.expect("a shader").passes;
+ assert_eq!(passes.len(), 2);
+ assert_eq!(
+ passes[0].shrink,
+ Some(["radius / 3".to_owned(), "1".to_owned()])
+ );
+ assert_eq!(passes[1].shrink, None);
+
+ rejects(
+ &format!("format = 1\n{shader}\n{}", pass("across")),
+ "format 2 setting",
+ );
+ for name in ["Across", "2x", "_x", "a-b", ""] {
+ rejects(&with(&pass(name)), "must be a lower-case letter");
+ }
+ for name in ["effect", "main"] {
+ rejects(&with(&pass(name)), "is taken");
+ }
+ rejects(&with(&format!("{}{}", pass("x"), pass("x"))), "drawn twice");
+ let many: String = (0..=MAX_PASSES).map(|n| pass(&format!("p{n}"))).collect();
+ rejects(&with(&many), "a package may draw 8");
+ rejects(&with(&format!("{}shrink = [\"2\"]\n", pass("x"))), "length");
+ rejects(
+ &with(&format!("{}size = \"WIDTH / 2\"\n", pass("x"))),
+ "unknown field",
+ );
+ }
+
#[test]
fn a_transition_manifest_parses() {
let manifest = Manifest::parse(GOOD_TRANSITION).expect("parses");
diff --git a/src/crates/concat-effects/src/shader.rs b/src/crates/concat-effects/src/shader.rs
index d2abb45d..db4727f1 100644
--- a/src/crates/concat-effects/src/shader.rs
+++ b/src/crates/concat-effects/src/shader.rs
@@ -31,13 +31,23 @@
//! offset of every field, which is how a clip's settings become the bytes
//! of a uniform buffer without a package having to say anything about
//! layout.
-
-use std::collections::BTreeMap;
+//!
+//! From format 2 a package may draw in several passes: each
+//! `[[wgsl.pass]]` names a picture, `fn <target>(uv)` draws it, and the
+//! passes after it - `effect`, the last, among them - read it through
+//! `<target>_at(uv)`. The host binds each picture beside the layer, gives
+//! every pass an entry point of its own (`fs_<target>`, then `fs_main`),
+//! and refuses at load a pass that reads a picture not yet drawn. The
+//! pictures hold what the passes return, untouched: only the last pass's
+//! colour is taken back into light and mixed by intensity.
+
+use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::sync::Arc;
-use concat_core::{Lut, RevealMap, ShaderPass, TransitionPass};
+use concat_core::{Lut, RevealMap, ShaderPass, Stage, TransitionPass};
-use crate::manifest::{Manifest, Param, ParamType, Space};
+use crate::expr::{Expr, Value};
+use crate::manifest::{MAX_SHRINK, Manifest, Param, ParamType, Pass, Space};
/// What an effect's shader binds: the layer at group 0, the host's frame
/// block and the package's own parameters at group 1, its table at group 2
@@ -990,22 +1000,29 @@ pub struct Shader {
source: Arc<str>,
slots: Vec<Slot>,
span: usize,
+ /// The passes before `effect`, for a package drawn in several.
+ stages: Vec<StageRule>,
}
impl Shader {
/// Stitches `body` into the host's contract - the edition the
- /// manifest's format asks for - checks it, and reads the `Params`
- /// struct for where each of the manifest's parameters lands. Every
- /// declared parameter must be a field of the struct; a field the
- /// manifest does not declare is allowed and stays zero.
+ /// manifest's format asks for, with its passes - checks it, and reads
+ /// the `Params` struct for where each of the manifest's parameters
+ /// lands. Every declared parameter must be a field of the struct; a
+ /// field the manifest does not declare is allowed and stays zero.
pub fn compile(manifest: &Manifest, body: &str) -> Result<Shader, String> {
let (source, slots, span) = stitch(manifest, body, Entry::Effect)?;
+ let passes = manifest
+ .wgsl
+ .as_ref()
+ .map_or(&[][..], |wgsl| wgsl.passes.as_slice());
Ok(Shader {
package: manifest.effect.id.clone(),
key: pipeline_key(&manifest.effect.id, manifest.effect.version, &source),
source,
slots,
span,
+ stages: StageRule::of(manifest, passes)?,
})
}
@@ -1022,8 +1039,8 @@ impl Shader {
}
/// A pass over a layer with these values: the uniform buffer written
- /// from them, and the values themselves for a renderer that reads by
- /// name.
+ /// from them, the values themselves for a renderer that reads by name,
+ /// and the stages before the last, their pictures sized from them.
pub fn pass(
&self,
values: &BTreeMap<String, f64>,
@@ -1041,6 +1058,7 @@ impl Shader {
intensity,
lut,
reveal_map,
+ stages: self.stages.iter().map(|rule| rule.stage(values)).collect(),
}
}
}
@@ -1069,16 +1087,23 @@ impl Bound {
}
}
+/// Where the first pass's picture is bound in group 0, after the layer and
+/// its sampler; the next pass's is bound after it, and so on.
+const FIRST_PICTURE: u32 = 2;
+
/// What the host provides at `(group, binding)` for `entry`, and so the
-/// only thing a package may declare there: an effect's layer at (0,0)-(0,1)
-/// and its reveal map at group 3; a transition's two pictures at (0,0)-(0,3)
-/// and nothing at group 3; the frame block and parameters at group 1 and
-/// the look-up table at group 2 for both. A slot outside this, or one
-/// declared as something else, is a pipeline the device cannot build.
-fn provided(entry: Entry, group: u32, binding: u32) -> Option<Bound> {
+/// only thing a package may declare there: an effect's layer at (0,0)-(0,1),
+/// the pictures of its `pictures` passes after it, and its reveal map at
+/// group 3; a transition's two pictures at (0,0)-(0,3) and nothing at group
+/// 3; the frame block and parameters at group 1 and the look-up table at
+/// group 2 for both. A slot outside this, or one declared as something
+/// else, is a pipeline the device cannot build.
+fn provided(entry: Entry, pictures: usize, group: u32, binding: u32) -> Option<Bound> {
+ let passes = FIRST_PICTURE..FIRST_PICTURE + pictures as u32;
match (entry, group, binding) {
(_, 0, 0) => Some(Bound::Picture),
(_, 0, 1) => Some(Bound::Sampler),
+ (Entry::Effect, 0, binding) if passes.contains(&binding) => Some(Bound::Picture),
(Entry::Transition, 0, 2) => Some(Bound::Picture),
(Entry::Transition, 0, 3) => Some(Bound::Sampler),
(_, 1, 0) | (_, 1, 1) => Some(Bound::Uniform),
@@ -1119,12 +1144,12 @@ fn declared(module: &naga::Module, global: &naga::GlobalVariable) -> Option<Boun
/// hangs the device for every process on the machine; a binding the host
/// does not know is one it cannot serve. Caught at load, where a broken
/// package is a load error and not a black frame.
-fn budget(module: &naga::Module, entry: Entry) -> Result<(), String> {
+fn budget(module: &naga::Module, entry: Entry, pictures: usize) -> Result<(), String> {
for (_, global) in module.global_variables.iter() {
let Some(binding) = &global.binding else {
continue;
};
- let Some(wanted) = provided(entry, binding.group, binding.binding) else {
+ let Some(wanted) = provided(entry, pictures, binding.group, binding.binding) else {
return Err(format!(
"the shader binds @group({}) @binding({}), which the host does not provide",
binding.group, binding.binding
@@ -1194,6 +1219,262 @@ fn breaks(block: &naga::Block) -> bool {
})
}
+/// What the host stitches for a package's passes: each pass's picture bound
+/// in group 0 after the layer, read through `<target>_at` and measured by
+/// `<target>_texel`, and the entry point that draws it. Nothing for a
+/// package drawn in one pass.
+fn passes_host(passes: &[Pass]) -> String {
+ use std::fmt::Write as _;
+ let mut host = String::new();
+ if passes.is_empty() {
+ return host;
+ }
+ host.push_str("\n// ── the passes' pictures; see concat-effects/src/shader.rs ──\n");
+ for (index, pass) in passes.iter().enumerate() {
+ let target = &pass.target;
+ let binding = FIRST_PICTURE + index as u32;
+ write!(
+ host,
+ r#"
+@group(0) @binding({binding}) var {target}_picture: texture_2d<f32>;
+
+/// The picture the `{target}` pass drew, at `uv`: what its function
+/// returned, straight, bilinearly filtered between its pixels.
+fn {target}_at(uv: vec2<f32>) -> vec4<f32> {{
+ return textureSampleLevel({target}_picture, source_sampler, uv, 0.0);
+}}
+
+/// One pixel of the `{target}` pass's picture, as a fraction of it.
+fn {target}_texel() -> vec2<f32> {{
+ return vec2<f32>(1.0) / vec2<f32>(textureDimensions({target}_picture));
+}}
+
+@fragment
+fn fs_{target}(in: VsOut) -> @location(0) vec4<f32> {{
+ return {target}(in.uv);
+}}
+"#
+ )
+ .expect("a String takes every write");
+ }
+ host
+}
+
+/// Every pass reads only pictures drawn before it, and every picture is
+/// read by a pass after it. A pass reading its own picture, or one not
+/// drawn yet, would read a blank the host binds in its place; a picture no
+/// pass reads is work for nothing. What a pass reads is every picture its
+/// function, or anything that calls, touches.
+fn read_in_order(module: &naga::Module, passes: &[Pass]) -> Result<(), String> {
+ if passes.is_empty() {
+ return Ok(());
+ }
+ let pictures: HashMap<naga::Handle<naga::GlobalVariable>, usize> = module
+ .global_variables
+ .iter()
+ .filter_map(|(handle, global)| {
+ let binding = global.binding.as_ref()?;
+ let index = binding.binding.checked_sub(FIRST_PICTURE)? as usize;
+ (binding.group == 0 && index < passes.len()).then_some((handle, index))
+ })
+ .collect();
+ let mut unread: BTreeSet<usize> = (0..passes.len()).collect();
+ let drawers = passes
+ .iter()
+ .map(|pass| pass.target.as_str())
+ .chain([Entry::Effect.name()]);
+ for (index, name) in drawers.enumerate() {
+ let Some((function, _)) = module
+ .functions
+ .iter()
+ .find(|(_, function)| function.name.as_deref() == Some(name))
+ else {
+ return Err(format!(
+ "the shader declares no `fn {name}(uv: vec2<f32>) -> vec4<f32>`"
+ ));
+ };
+ let read = pictures_read(module, function, &pictures);
+ if let Some(&later) = read.range(index..).next() {
+ return Err(if later == index {
+ format!("the pass drawing `{name}` reads its own picture")
+ } else {
+ format!(
+ "the pass drawing `{name}` reads `{}`, which is drawn after it",
+ passes[later].target
+ )
+ });
+ }
+ unread.retain(|picture| !read.contains(picture));
+ }
+ match unread.first() {
+ Some(&picture) => Err(format!(
+ "the pass `{}` draws a picture no pass after it reads",
+ passes[picture].target
+ )),
+ None => Ok(()),
+ }
+}
+
+/// The pass pictures `function` reads: every one whose texture it, or any
+/// function it calls, names.
+fn pictures_read(
+ module: &naga::Module,
+ function: naga::Handle<naga::Function>,
+ pictures: &HashMap<naga::Handle<naga::GlobalVariable>, usize>,
+) -> BTreeSet<usize> {
+ let mut read = BTreeSet::new();
+ let mut seen = std::collections::HashSet::new();
+ let mut waiting = vec![function];
+ while let Some(handle) = waiting.pop() {
+ if !seen.insert(handle) {
+ continue;
+ }
+ let function = &module.functions[handle];
+ for (_, expression) in function.expressions.iter() {
+ if let naga::Expression::GlobalVariable(global) = expression
+ && let Some(&picture) = pictures.get(global)
+ {
+ read.insert(picture);
+ }
+ }
+ calls(&function.body, &mut waiting);
+ }
+ read
+}
+
+/// Every function `block` calls, at any depth, pushed onto `into`.
+fn calls(block: &naga::Block, into: &mut Vec<naga::Handle<naga::Function>>) {
+ for statement in block.iter() {
+ match statement {
+ naga::Statement::Call { function, .. } => into.push(*function),
+ naga::Statement::Block(inner) => calls(inner, into),
+ naga::Statement::If { accept, reject, .. } => {
+ calls(accept, into);
+ calls(reject, into);
+ }
+ naga::Statement::Switch { cases, .. } => {
+ for case in cases {
+ calls(&case.body, into);
+ }
+ }
+ naga::Statement::Loop {
+ body, continuing, ..
+ } => {
+ calls(body, into);
+ calls(continuing, into);
+ }
+ _ => {}
+ }
+ }
+}
+
+/// A pass before `effect`, as the shader keeps it: the entry point that
+/// draws it, and the expressions over the knobs its shrink is worked out
+/// from, across and down.
+#[derive(Clone, Debug)]
+struct StageRule {
+ entry: String,
+ shrink: [Expr; 2],
+}
+
+impl StageRule {
+ /// The rules for `passes`, their shrinks parsed and read only for the
+ /// knobs `manifest` declares, and worked out at every knob's default,
+ /// minimum and maximum, so an expression that fails is a load error
+ /// and never a silently full-sized picture.
+ fn of(manifest: &Manifest, passes: &[Pass]) -> Result<Vec<StageRule>, String> {
+ let knobs: Vec<&str> = manifest
+ .params
+ .iter()
+ .filter(|param| param.kind != ParamType::Point)
+ .map(|param| param.key.as_str())
+ .collect();
+ let rules = passes
+ .iter()
+ .map(|pass| {
+ let parse = |text: &str| {
+ let expr = Expr::parse(text).map_err(|error| {
+ format!("pass `{}`: shrink `{text}`: {error}", pass.target)
+ })?;
+ let mut names = Vec::new();
+ expr.names(&mut names);
+ match names.iter().find(|name| !knobs.contains(&name.as_str())) {
+ Some(name) => Err(format!(
+ "pass `{}`: shrink `{text}` reads `{name}`, which is not a knob",
+ pass.target
+ )),
+ None => Ok(expr),
+ }
+ };
+ let [across, down] = match &pass.shrink {
+ Some([across, down]) => [across.as_str(), down.as_str()],
+ None => ["1", "1"],
+ };
+ Ok(StageRule {
+ entry: format!("fs_{}", pass.target),
+ shrink: [parse(across)?, parse(down)?],
+ })
+ })
+ .collect::<Result<Vec<StageRule>, String>>()?;
+ for (at, pick) in [
+ (
+ "default",
+ (|param: &Param| param.default) as fn(&Param) -> f64,
+ ),
+ ("minimum", |param: &Param| param.min),
+ ("maximum", |param: &Param| param.max),
+ ] {
+ let values: BTreeMap<String, f64> = manifest
+ .params
+ .iter()
+ .map(|param| (param.key.clone(), pick(param)))
+ .collect();
+ for (rule, pass) in rules.iter().zip(passes) {
+ for expr in &rule.shrink {
+ shrink_of(expr, &values).map_err(|error| {
+ format!(
+ "pass `{}`: shrink at every knob's {at}: {error}",
+ pass.target
+ )
+ })?;
+ }
+ }
+ }
+ Ok(rules)
+ }
+
+ /// The stage for these resolved knob values.
+ fn stage(&self, values: &BTreeMap<String, f64>) -> Stage {
+ Stage {
+ entry: self.entry.clone(),
+ shrink: self
+ .shrink
+ .each_ref()
+ .map(|expr| shrink_of(expr, values).unwrap_or(1)),
+ }
+ }
+}
+
+/// A shrink worked out from the knobs, rounded down to a power of two from
+/// 1 to [`MAX_SHRINK`]: a few sizes a layer's pictures can be, so a knob
+/// that rides does not ask the pool for a new size every frame. Below 2, or
+/// not a number at all, is the layer's own size.
+fn shrink_of(expr: &Expr, values: &BTreeMap<String, f64>) -> Result<u32, String> {
+ let env: BTreeMap<String, Value> = values
+ .iter()
+ .map(|(key, value)| (key.clone(), Value::Float(*value)))
+ .collect();
+ let shrink = match expr.eval(&env).map_err(|error| error.to_string())? {
+ Value::Int(whole) => whole as f64,
+ Value::Float(real) => real,
+ Value::Text(_) => return Err("text where a number was needed".to_owned()),
+ };
+ if shrink.is_nan() || shrink < 2.0 {
+ return Ok(1);
+ }
+ Ok(1 << shrink.min(f64::from(MAX_SHRINK)).log2().floor() as u32)
+}
+
/// What a compiled pipeline is cached under: the package's id and version,
/// and a fingerprint of the stitched source, so a shader edited in place
/// without a version bump still gets a pipeline of its own rather than
@@ -1247,6 +1528,10 @@ fn stitch(
body: &str,
entry: Entry,
) -> Result<(Arc<str>, Vec<Slot>, usize), String> {
+ let passes: &[Pass] = match (entry, &manifest.wgsl) {
+ (Entry::Effect, Some(wgsl)) => &wgsl.passes,
+ _ => &[],
+ };
let declares_params = body
.split("struct")
.skip(1)
@@ -1254,7 +1539,16 @@ fn stitch(
if !body.contains(&format!("fn {}", entry.name())) {
return Err(format!("the shader declares no `{}`", entry.signature()));
}
- let host = Contract::of(manifest).host(entry);
+ if let Some(pass) = passes
+ .iter()
+ .find(|pass| !body.contains(&format!("fn {}", pass.target)))
+ {
+ return Err(format!(
+ "the shader declares no `fn {}(uv: vec2<f32>) -> vec4<f32>` to draw the pass `{}`",
+ pass.target, pass.target
+ ));
+ }
+ let host = Contract::of(manifest).host(entry) + &passes_host(passes);
let mut source = String::with_capacity(host.len() + body.len() + 64);
if !declares_params {
// A package with no knobs still has to bind something.
@@ -1283,7 +1577,8 @@ fn stitch(
{
return Err(format!("the shader declares no `{}`", entry.signature()));
}
- budget(&module, entry)?;
+ budget(&module, entry, passes.len())?;
+ read_in_order(&module, passes)?;
let (members, span) = module
.types
@@ -1728,6 +2023,159 @@ fn transition(uv: vec2<f32>, progress: f32) -> vec4<f32> {
assert!(message.contains("does not provide"), "{message}");
}
+ /// A format 2 manifest of a package with a `radius` knob from 0 to 300,
+ /// drawing `passes` before `effect`.
+ fn in_passes(passes: &str) -> Manifest {
+ Manifest::parse(&format!(
+ "format = 2\n[effect]\nid = \"test.passes\"\nname = \"Passes\"\nkind = \"effect\"\n\
+ [[param]]\nkey = \"radius\"\nlabel = \"Radius\"\nmax = 300\ndefault = 10\n\
+ [wgsl]\nentry = \"effect.wgsl\"\n{passes}"
+ ))
+ .expect("a valid manifest")
+ }
+
+ const TWO_PASSES: &str = "[[wgsl.pass]]\ntarget = \"across\"\nshrink = [\"radius / 3\", \"1\"]\n\
+ [[wgsl.pass]]\ntarget = \"down\"\nshrink = [\"radius / 3\", \"radius / 3\"]\n";
+
+ const THROUGH_BOTH: &str = "struct Params { radius: f32 }\n\
+ fn across(uv: vec2<f32>) -> vec4<f32> { return sample(uv); }\n\
+ fn down(uv: vec2<f32>) -> vec4<f32> { return across_at(uv) * across_texel().x; }\n\
+ fn effect(uv: vec2<f32>) -> vec4<f32> { return down_at(uv); }";
+
+ /// Each pass's picture is bound after the layer and read through its
+ /// own functions, each pass has an entry point of its own, and the
+ /// pass carries the stages with their pictures' shrinks worked out
+ /// from the knobs: rounded down to a power of two, at most 64.
+ #[test]
+ fn a_package_drawn_in_passes_binds_and_sizes_each_picture() {
+ let manifest = in_passes(TWO_PASSES);
+ let shader = Shader::compile(&manifest, THROUGH_BOTH).expect("compiles");
+ for line in [
+ "@group(0) @binding(2) var across_picture: texture_2d<f32>;",
+ "@group(0) @binding(3) var down_picture: texture_2d<f32>;",
+ "fn across_at(uv: vec2<f32>) -> vec4<f32>",
+ "fn down_texel() -> vec2<f32>",
+ "fn fs_across(in: VsOut)",
+ "fn fs_down(in: VsOut)",
+ "fn fs_main(in: VsOut)",
+ ] {
+ assert!(shader.source().contains(line), "no `{line}`");
+ }
+ let stages = |radius: f64| {
+ let values = BTreeMap::from([("radius".to_owned(), radius)]);
+ shader
+ .pass(&values, &manifest.params, 1.0, None, None)
+ .stages
+ .iter()
+ .map(|stage| (stage.entry.clone(), stage.shrink))
+ .collect::<Vec<_>>()
+ };
+ assert_eq!(
+ stages(10.0),
+ [
+ ("fs_across".to_owned(), [2, 1]),
+ ("fs_down".to_owned(), [2, 2])
+ ]
+ );
+ for (radius, shrink) in [(0.0, 1), (5.9, 1), (6.0, 2), (48.0, 16), (300.0, 64)] {
+ assert_eq!(stages(radius)[1].1, [shrink, shrink], "radius {radius}");
+ }
+ assert_eq!(stages(f64::NAN)[1].1, [1, 1], "not a number");
+
+ let single = Shader::compile(
+ &in_passes(""),
+ "struct Params { radius: f32 }\nfn effect(uv: vec2<f32>) -> vec4<f32> { return sample(uv); }",
+ )
+ .expect("compiles");
+ assert!(
+ single
+ .pass(&BTreeMap::new(), &[], 1.0, None, None)
+ .stages
+ .is_empty(),
+ "a package drawn in one pass has no stages"
+ );
+ }
+
+ /// A pass reads only the pictures drawn before it - through a helper as
+ /// much as directly - and every picture is read by a pass after it.
+ #[test]
+ fn a_pass_reads_only_what_was_drawn_before_it() {
+ let manifest = in_passes(TWO_PASSES);
+ let refused = |body: &str| {
+ Shader::compile(
+ &manifest,
+ &format!("struct Params {{ radius: f32 }}\n{body}"),
+ )
+ .expect_err("refused")
+ };
+ let own = refused(
+ "fn across(uv: vec2<f32>) -> vec4<f32> { return across_at(uv); }\n\
+ fn down(uv: vec2<f32>) -> vec4<f32> { return across_at(uv); }\n\
+ fn effect(uv: vec2<f32>) -> vec4<f32> { return down_at(uv); }",
+ );
+ assert!(own.contains("`across` reads its own picture"), "{own}");
+ let later = refused(
+ "fn peek(uv: vec2<f32>) -> vec2<f32> { return down_texel(); }\n\
+ fn across(uv: vec2<f32>) -> vec4<f32> { return sample(uv + peek(uv)); }\n\
+ fn down(uv: vec2<f32>) -> vec4<f32> { return across_at(uv); }\n\
+ fn effect(uv: vec2<f32>) -> vec4<f32> { return down_at(uv); }",
+ );
+ assert!(
+ later.contains("`across` reads `down`, which is drawn after it"),
+ "{later}"
+ );
+ let unread = refused(
+ "fn across(uv: vec2<f32>) -> vec4<f32> { return sample(uv); }\n\
+ fn down(uv: vec2<f32>) -> vec4<f32> { return sample(uv); }\n\
+ fn effect(uv: vec2<f32>) -> vec4<f32> { return down_at(uv); }",
+ );
+ assert!(
+ unread.contains("`across` draws a picture no pass after it reads"),
+ "{unread}"
+ );
+ let missing = refused(
+ "fn across(uv: vec2<f32>) -> vec4<f32> { return sample(uv); }\n\
+ fn effect(uv: vec2<f32>) -> vec4<f32> { return across_at(uv); }",
+ );
+ assert!(missing.contains("to draw the pass `down`"), "{missing}");
+ let bound = Shader::compile(
+ &in_passes("[[wgsl.pass]]\ntarget = \"across\"\n"),
+ "struct Params { radius: f32 }\n\
+ @group(0) @binding(3) var more: texture_2d<f32>;\n\
+ fn across(uv: vec2<f32>) -> vec4<f32> { return sample(uv); }\n\
+ fn effect(uv: vec2<f32>) -> vec4<f32> { return across_at(uv) + textureSampleLevel(more, source_sampler, uv, 0.0); }",
+ )
+ .expect_err("a binding past the pictures");
+ assert!(bound.contains("@group(0) @binding(3)"), "{bound}");
+ }
+
+ /// A shrink is an expression over the package's knobs that comes to a
+ /// number at every knob's default and bounds, or the package does not
+ /// load.
+ #[test]
+ fn a_shrink_is_a_number_worked_out_from_the_knobs() {
+ let with = |shrink: &str| {
+ Shader::compile(
+ &in_passes(&format!(
+ "[[wgsl.pass]]\ntarget = \"across\"\nshrink = [\"{shrink}\", \"1\"]\n"
+ )),
+ "struct Params { radius: f32 }\n\
+ fn across(uv: vec2<f32>) -> vec4<f32> { return sample(uv); }\n\
+ fn effect(uv: vec2<f32>) -> vec4<f32> { return across_at(uv); }",
+ )
+ };
+ with("max(radius / 4, 1)").expect("a knob and numbers");
+ let unknown = with("WIDTH / 4").expect_err("the layer's size is not a knob");
+ assert!(
+ unknown.contains("reads `WIDTH`, which is not a knob"),
+ "{unknown}"
+ );
+ let text = with("fixed(radius, 1)").expect_err("text");
+ assert!(text.contains("text where a number was needed"), "{text}");
+ let broken = with("radius /").expect_err("half an expression");
+ assert!(broken.contains("shrink `radius /`"), "{broken}");
+ }
+
/// The pipeline key follows the source, so a shader edited without a
/// version bump does not keep a stale pipeline.
#[test]
diff --git a/src/crates/concat-host/src/cards.rs b/src/crates/concat-host/src/cards.rs
index 7754bcfd..61d529c6 100644
--- a/src/crates/concat-host/src/cards.rs
+++ b/src/crates/concat-host/src/cards.rs
@@ -196,6 +196,13 @@ fn fingerprint(drawing: &Drawing, moment: f64) -> u64 {
feed(&pass.params);
feed(&pass.intensity.to_le_bytes());
feed(&pass.lut.as_ref().map_or(0, |lut| lut.id).to_le_bytes());
+ // A package drawn in several passes: the size of each picture is
+ // the manifest's, not the shader's, so it is fed on its own.
+ for stage in &pass.stages {
+ feed(stage.entry.as_bytes());
+ feed(&stage.shrink[0].to_le_bytes());
+ feed(&stage.shrink[1].to_le_bytes());
+ }
};
match drawing {
Drawing::Picture {
@@ -546,6 +553,22 @@ mod tests {
.to_string_lossy()
.starts_with(&*dir.join("a.gain-").to_string_lossy())
);
+
+ // A package drawn in passes: the same shader with its picture drawn
+ // at another size is another card.
+ let in_passes = |shrink: &str| {
+ let manifest = format!(
+ "{}[[wgsl.pass]]\ntarget = \"half\"\nshrink = [\"{shrink}\", \"{shrink}\"]\n",
+ manifest(2.0)
+ );
+ let body = "struct Params { gain: f32 }\n\
+ fn half(uv: vec2<f32>) -> vec4<f32> { return sample(uv) * params.gain; }\n\
+ fn effect(uv: vec2<f32>) -> vec4<f32> { return half_at(uv); }";
+ let package = Package::from_sources(&manifest, None, Some(body)).expect("loads");
+ Card::of(catalogue, &package, dir).expect("a card").path
+ };
+ assert_eq!(in_passes("2"), in_passes("2"));
+ assert_ne!(in_passes("2"), in_passes("4"), "another size of picture");
}
/// Pruning sweeps away a card no package draws any more and leaves the
diff --git a/src/crates/concat-perf/src/main.rs b/src/crates/concat-perf/src/main.rs
index b0089fb9..ddb15c2b 100644
--- a/src/crates/concat-perf/src/main.rs
+++ b/src/crates/concat-perf/src/main.rs
@@ -84,6 +84,7 @@ fn main() {
if let Some(measure) = compose_gpu() {
results.push(measure);
}
+ results.extend(blur_4k());
results.push(export(&media));
}
println!();
@@ -896,6 +897,52 @@ fn compose_gpu() -> Option<Measure> {
})
}
+/// A 4K picture through the Gaussian blur at its default radius and at its
+/// widest, drawn on the GPU and kept there as the monitor's frame is, and
+/// the same picture untreated, so the blur's own share can be read off.
+fn blur_4k() -> Vec<Measure> {
+ let Some(mut gpu) = concat_render::WgpuCompositor::new() else {
+ return Vec::new();
+ };
+ let picture = Arc::new(grainy(3840, 2160, 3));
+ let frame_of = |gpu: &mut concat_render::WgpuCompositor, radius: Option<f64>| {
+ let mut layer = PlannedLayer::picture(concat_render::detached_clip(), Arc::clone(&picture));
+ if let Some(radius) = radius {
+ let mut blur = AppliedFilter::new("concat.gaussian-blur");
+ blur.params.insert("radius".to_owned(), radius);
+ layer.effects =
+ concat_effects::Catalogue::builtin().shader_passes_at(&[blur], 0.0, None);
+ }
+ let plan = FramePlan {
+ layers: vec![layer],
+ ..FramePlan::empty(3840, 2160)
+ };
+ gpu.render_texture(&plan);
+ gpu.finish();
+ let rounds = 20;
+ let started = Instant::now();
+ for _ in 0..rounds {
+ gpu.render_texture(&plan);
+ gpu.finish();
+ }
+ started.elapsed().as_secs_f64() * 1e3 / f64::from(rounds)
+ };
+ let plain = frame_of(&mut gpu, None);
+ [
+ ("Gaussian blur over 4K, radius 10, on the GPU", 10.0),
+ ("Gaussian blur over 4K, radius 50, on the GPU", 50.0),
+ ]
+ .into_iter()
+ .map(|(name, radius)| Measure {
+ name,
+ value: frame_of(&mut gpu, Some(radius)),
+ unit: "ms",
+ budget: Budget::AtMost(33.0),
+ note: format!("the whole frame, kept on the GPU; {plain:.1} ms untreated"),
+ })
+ .collect()
+}
+
/// A real export of a three-second cut through the session, as the
/// window runs it: frames a second at 720p.
fn export(media: &Media) -> Measure {
diff --git a/src/crates/concat-render/src/gpu.rs b/src/crates/concat-render/src/gpu.rs
index f106dd01..887c27bc 100644
--- a/src/crates/concat-render/src/gpu.rs
+++ b/src/crates/concat-render/src/gpu.rs
@@ -330,6 +330,33 @@ struct CompiledShader {
frame: wgpu::Buffer,
params: wgpu::Buffer,
bind_group: wgpu::BindGroup,
+ /// For a package drawn in several passes, the pipeline of each stage
+ /// before the last, in the pass's order; `pipeline` draws the last.
+ stages: Vec<wgpu::RenderPipeline>,
+}
+
+/// A 2D float texture a pass samples, at `binding` of its group.
+fn picture_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
+ wgpu::BindGroupLayoutEntry {
+ binding,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Texture {
+ sample_type: wgpu::TextureSampleType::Float { filterable: true },
+ view_dimension: wgpu::TextureViewDimension::D2,
+ multisampled: false,
+ },
+ count: None,
+ }
+}
+
+/// A filtering sampler, at `binding` of its group.
+fn sampler_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
+ wgpu::BindGroupLayoutEntry {
+ binding,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
+ count: None,
+ }
}
/// One draw of a composite: the pooled texture drawn, how it meets the
@@ -437,6 +464,13 @@ pub struct WgpuCompositor {
/// Group 0 of a transition: the outgoing and incoming pictures, each a
/// texture and its sampler.
transition_layout: wgpu::BindGroupLayout,
+ /// Group 0 of a package drawn in several passes, by how many pictures
+ /// its passes draw: the layer and its sampler, then each picture; see
+ /// [`WgpuCompositor::pictures_layout`].
+ pictures_layouts: HashMap<usize, wgpu::BindGroupLayout>,
+ /// What a pass binds in place of a picture not drawn yet - its own
+ /// among them: one transparent pixel.
+ blank: wgpu::TextureView,
/// Uploaded tables by their id, the identity among them; see `lut_group`.
luts: HashMap<u64, wgpu::BindGroup>,
/// The composite each cached LUT was last used in; see [`LUT_CACHE`].
@@ -616,28 +650,12 @@ impl WgpuCompositor {
});
// Group 0 of a transition: two pictures, each a texture and a
// sampler - the outgoing at 0/1, the incoming at 2/3.
- let texture_entry = |binding: u32| wgpu::BindGroupLayoutEntry {
- binding,
- visibility: wgpu::ShaderStages::FRAGMENT,
- ty: wgpu::BindingType::Texture {
- sample_type: wgpu::TextureSampleType::Float { filterable: true },
- view_dimension: wgpu::TextureViewDimension::D2,
- multisampled: false,
- },
- count: None,
- };
- let sampler_entry = |binding: u32| wgpu::BindGroupLayoutEntry {
- binding,
- visibility: wgpu::ShaderStages::FRAGMENT,
- ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
- count: None,
- };
let transition_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("concat transition inputs"),
entries: &[
- texture_entry(0),
+ picture_entry(0),
sampler_entry(1),
- texture_entry(2),
+ picture_entry(2),
sampler_entry(3),
],
});
@@ -883,6 +901,25 @@ impl WgpuCompositor {
mapped_at_creation: false,
});
+ // Never written: wgpu clears a texture before its first use, so it
+ // reads as nothing at all.
+ let blank = device
+ .create_texture(&wgpu::TextureDescriptor {
+ label: Some("concat blank picture"),
+ size: wgpu::Extent3d {
+ width: 1,
+ height: 1,
+ depth_or_array_layers: 1,
+ },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: WORK,
+ usage: wgpu::TextureUsages::TEXTURE_BINDING,
+ view_formats: &[],
+ })
+ .create_view(&wgpu::TextureViewDescriptor::default());
+
Self {
device,
queue,
@@ -899,6 +936,8 @@ impl WgpuCompositor {
lut_layout,
reveal_layout,
transition_layout,
+ pictures_layouts: HashMap::new(),
+ blank,
luts: HashMap::new(),
luts_drawn: HashMap::new(),
reveals: HashMap::new(),
@@ -936,6 +975,16 @@ impl WgpuCompositor {
self.dead
}
+ /// Waits until the device has done everything asked of it so far: how
+ /// a timing knows that a texture [`WgpuCompositor::render_texture`]
+ /// handed back is drawn, without reading it back. False when the
+ /// device did not answer.
+ pub fn finish(&self) -> bool {
+ self.device
+ .poll(wgpu::PollType::wait_indefinitely())
+ .is_ok()
+ }
+
/// The next presentable texture for this output size.
fn presentable(&mut self, width: u32, height: u32) -> wgpu::Texture {
let stale = self
@@ -2014,6 +2063,13 @@ impl WgpuCompositor {
if self.shaders.contains_key(&pass.key) || self.refused.contains(&pass.key) {
return;
}
+ // A package drawn in one pass binds its layer as the pool made it;
+ // one drawn in several binds its passes' pictures beside it.
+ let inputs = if pass.stages.is_empty() {
+ self.bind_layout.clone()
+ } else {
+ self.pictures_layout(pass.stages.len())
+ };
let scope = self.device.push_error_scope(wgpu::ErrorFilter::Validation);
let module = self
.device
@@ -2026,42 +2082,49 @@ impl WgpuCompositor {
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some(&pass.key),
bind_group_layouts: &[
- Some(&self.bind_layout),
+ Some(&inputs),
Some(&self.uniform_layout),
Some(&self.lut_layout),
Some(&self.reveal_layout),
],
immediate_size: 0,
});
- let pipeline = self
- .device
- .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
- label: Some(&pass.key),
- layout: Some(&layout),
- vertex: wgpu::VertexState {
- module: &module,
- entry_point: Some("vs_main"),
- compilation_options: Default::default(),
- buffers: &[],
- },
- fragment: Some(wgpu::FragmentState {
- module: &module,
- entry_point: Some("fs_main"),
- compilation_options: Default::default(),
- targets: &[Some(wgpu::ColorTargetState {
- format: WORK,
- // A pass replaces: mixing by intensity is the
- // shader's own last line.
- blend: None,
- write_mask: wgpu::ColorWrites::ALL,
- })],
- }),
- primitive: wgpu::PrimitiveState::default(),
- depth_stencil: None,
- multisample: wgpu::MultisampleState::default(),
- multiview_mask: None,
- cache: None,
- });
+ let pipeline_of = |entry: &str| {
+ self.device
+ .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some(&pass.key),
+ layout: Some(&layout),
+ vertex: wgpu::VertexState {
+ module: &module,
+ entry_point: Some("vs_main"),
+ compilation_options: Default::default(),
+ buffers: &[],
+ },
+ fragment: Some(wgpu::FragmentState {
+ module: &module,
+ entry_point: Some(entry),
+ compilation_options: Default::default(),
+ targets: &[Some(wgpu::ColorTargetState {
+ format: WORK,
+ // A pass replaces: mixing by intensity is the
+ // shader's own last line.
+ blend: None,
+ write_mask: wgpu::ColorWrites::ALL,
+ })],
+ }),
+ primitive: wgpu::PrimitiveState::default(),
+ depth_stencil: None,
+ multisample: wgpu::MultisampleState::default(),
+ multiview_mask: None,
+ cache: None,
+ })
+ };
+ let pipeline = pipeline_of("fs_main");
+ let stages = pass
+ .stages
+ .iter()
+ .map(|stage| pipeline_of(&stage.entry))
+ .collect();
if let Some(error) = pollster::block_on(scope.pop()) {
log::error!(
"pass {}: the driver refused its pipeline: {error}",
@@ -2105,10 +2168,32 @@ impl WgpuCompositor {
frame,
params,
bind_group,
+ stages,
},
);
}
+ /// Group 0 of a package whose passes draw `pictures` pictures: the
+ /// layer and its sampler where a single pass binds them, then each
+ /// picture, in the passes' order. Made once for each count.
+ fn pictures_layout(&mut self, pictures: usize) -> wgpu::BindGroupLayout {
+ if let Some(layout) = self.pictures_layouts.get(&pictures) {
+ return layout.clone();
+ }
+ let entries: Vec<wgpu::BindGroupLayoutEntry> = [picture_entry(0), sampler_entry(1)]
+ .into_iter()
+ .chain((0..pictures as u32).map(|picture| picture_entry(2 + picture)))
+ .collect();
+ let layout = self
+ .device
+ .create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("concat pass pictures"),
+ entries: &entries,
+ });
+ self.pictures_layouts.insert(pictures, layout.clone());
+ layout
+ }
+
/// The compiled pipeline for a transition, built the first time its key is
/// seen. Mirrors [`WgpuCompositor::shader`] but binds two input pictures at
/// group 0 and lets the shader own the blend.
@@ -2203,6 +2288,7 @@ impl WgpuCompositor {
frame,
params,
bind_group,
+ stages: Vec::new(),
},
);
}
@@ -2500,10 +2586,16 @@ impl WgpuCompositor {
/// turns between two pooled textures of the same size - each reads the
/// one the pass before it drew and draws into the other - so a stack of
/// effects costs two textures, not one a pass: at 8K a pass's texture
- /// is 133 MB. `source` is only ever read, so a cached frame stays as it
+ /// is 265 MB. `source` is only ever read, so a cached frame stays as it
/// was. Each pass is its own submission, which is what makes drawing
/// into the texture two passes back safe, and keeps the uniforms a pass
/// wrote the ones it reads.
+ ///
+ /// A package drawn in several passes draws its stages into pictures of
+ /// their own first, in the same submission (see [`Self::run_stages`]).
+ /// Those are claimed from the pool the first time a size is wanted and
+ /// handed from one package to the next, so a stack of blurs costs one
+ /// set of them.
fn run_passes(
&mut self,
width: u32,
@@ -2516,6 +2608,7 @@ impl WgpuCompositor {
let mut current = source;
let mut turns: [Option<usize>; 2] = [None, None];
let mut drawn = 0;
+ let mut spare: HashMap<(u32, u32), Vec<usize>> = HashMap::new();
for pass in passes {
// A pass the driver refused does not take a turn, so the next
// one never draws into the texture it reads.
@@ -2531,9 +2624,23 @@ impl WgpuCompositor {
let lut_id = self.lut_group(pass.lut.as_deref());
let reveal_id = self.reveal_group(pass.reveal_map.as_deref());
// A pass the driver refused leaves the picture as it was.
- let Some(shader) = self.shaders.get(&pass.key) else {
+ if !self.shaders.contains_key(&pass.key) {
continue;
- };
+ }
+ if !pass.stages.is_empty() {
+ self.run_stages(
+ (width, height),
+ (current, target),
+ pass,
+ [time, time - clip_start],
+ (lut_id, reveal_id),
+ &mut spare,
+ );
+ current = target;
+ drawn += 1;
+ continue;
+ }
+ let shader = &self.shaders[&pass.key];
let lut_group = &self.luts[&lut_id];
let reveal_group = &self.reveals[&reveal_id];
let frame_block: [f32; 6] = [
@@ -2590,6 +2697,133 @@ impl WgpuCompositor {
current
}
+ /// One package drawn in several passes, over the pooled texture `layer`,
+ /// `width` by `height`, and into the pooled texture `into` of the same
+ /// size: each stage into a picture of its own - from `spare` where one
+ /// of its size is free, else claimed - then the last pass into `into`,
+ /// all in one submission. Every pass binds the layer and the pictures
+ /// drawn before it, a blank for the rest, its own among them. `time` is
+ /// the timeline's seconds and `clip_time` the clip's; `lut_id` and
+ /// `reveal_id` name the tables every pass binds. The pictures go back
+ /// to `spare` for the next package.
+ fn run_stages(
+ &mut self,
+ (width, height): (u32, u32),
+ (layer, into): (usize, usize),
+ pass: &ShaderPass,
+ [time, clip_time]: [f32; 2],
+ (lut_id, reveal_id): (u64, u64),
+ spare: &mut HashMap<(u32, u32), Vec<usize>>,
+ ) {
+ let pictures: Vec<((u32, u32), usize)> = pass
+ .stages
+ .iter()
+ .map(|stage| {
+ let size = stage.size(width, height);
+ let index = spare
+ .get_mut(&size)
+ .and_then(Vec::pop)
+ .unwrap_or_else(|| self.claim(size.0, size.1));
+ (size, index)
+ })
+ .collect();
+ let shader = &self.shaders[&pass.key];
+ // Every pass of the package reads the same frame block: the layer's
+ // size - what a knob in pixels is measured against - whatever the
+ // size of the picture it draws.
+ let frame_block: [f32; 6] = [
+ width as f32,
+ height as f32,
+ time,
+ pass.intensity,
+ clip_time,
+ 0.0,
+ ];
+ let frame_bytes: Vec<u8> = frame_block.iter().flat_map(|v| v.to_le_bytes()).collect();
+ self.queue.write_buffer(&shader.frame, 0, &frame_bytes);
+ let mut params = pass.params.clone();
+ params.resize(shader.params.size() as usize, 0);
+ self.queue.write_buffer(&shader.params, 0, &params);
+
+ let view_of = |size: (u32, u32), index: usize| {
+ self.pool[&size][index]
+ .texture
+ .create_view(&wgpu::TextureViewDescriptor::default())
+ };
+ let layer_view = view_of((width, height), layer);
+ let into_view = view_of((width, height), into);
+ let picture_views: Vec<wgpu::TextureView> = pictures
+ .iter()
+ .map(|&(size, index)| view_of(size, index))
+ .collect();
+ let layout = &self.pictures_layouts[&pictures.len()];
+ let mut encoder = self
+ .device
+ .create_command_encoder(&wgpu::CommandEncoderDescriptor {
+ label: Some("concat passes"),
+ });
+ for stage in 0..=pictures.len() {
+ let entries: Vec<wgpu::BindGroupEntry> =
+ [
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::TextureView(&layer_view),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: wgpu::BindingResource::Sampler(&self.sampler),
+ },
+ ]
+ .into_iter()
+ .chain(picture_views.iter().enumerate().map(|(picture, view)| {
+ wgpu::BindGroupEntry {
+ binding: 2 + picture as u32,
+ resource: wgpu::BindingResource::TextureView(if picture < stage {
+ view
+ } else {
+ &self.blank
+ }),
+ }
+ }))
+ .collect();
+ let inputs = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("concat pass pictures"),
+ layout,
+ entries: &entries,
+ });
+ let (pipeline, view) = match shader.stages.get(stage) {
+ Some(pipeline) => (pipeline, &picture_views[stage]),
+ None => (&shader.pipeline, &into_view),
+ };
+ let mut render = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ label: Some("concat pass"),
+ color_attachments: &[Some(wgpu::RenderPassColorAttachment {
+ view,
+ depth_slice: None,
+ resolve_target: None,
+ ops: wgpu::Operations {
+ load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
+ store: wgpu::StoreOp::Store,
+ },
+ })],
+ depth_stencil_attachment: None,
+ timestamp_writes: None,
+ occlusion_query_set: None,
+ multiview_mask: None,
+ });
+ render.set_pipeline(pipeline);
+ render.set_bind_group(0, &inputs, &[]);
+ render.set_bind_group(1, &shader.bind_group, &[]);
+ render.set_bind_group(2, &self.luts[&lut_id], &[]);
+ render.set_bind_group(3, &self.reveals[&reveal_id], &[]);
+ render.draw(0..3, 0..1);
+ }
+ self.queue.submit([encoder.finish()]);
+ for (size, index) in pictures {
+ spare.entry(size).or_default().push(index);
+ }
+ }
+
/// The six vertices of one layer's quad: the fitted picture scaled,
/// turned about its centre and moved as the geometry says, the forward
/// form of the CPU path's inverse map, sampling `uvs` through the
diff --git a/src/crates/concat-render/src/gpu/tests.rs b/src/crates/concat-render/src/gpu/tests.rs
index adf11ef7..d8c05f36 100644
--- a/src/crates/concat-render/src/gpu/tests.rs
+++ b/src/crates/concat-render/src/gpu/tests.rs
@@ -1051,3 +1051,307 @@ fn every_look_carries_a_highlight_past_white() {
}
assert!(clipped.is_empty(), "\n{}", clipped.join("\n"));
}
+
+/// `pixels`, straight RGBA in the working space and row by row, written
+/// into a pooled texture `width` by `height` as half floats: a picture
+/// with the light past white and the alpha an eight-bit frame cannot hold.
+fn written(gpu: &mut WgpuCompositor, (width, height): (u32, u32), pixels: &[[f32; 4]]) -> usize {
+ let index = gpu.claim(width, height);
+ let texels: Vec<u8> = pixels
+ .iter()
+ .flatten()
+ .flat_map(|channel| half::f16::from_f32(*channel).to_le_bytes())
+ .collect();
+ gpu.queue.write_texture(
+ wgpu::TexelCopyTextureInfo {
+ texture: &gpu.pool[&(width, height)][index].texture,
+ mip_level: 0,
+ origin: wgpu::Origin3d::ZERO,
+ aspect: wgpu::TextureAspect::All,
+ },
+ &texels,
+ wgpu::TexelCopyBufferLayout {
+ offset: 0,
+ bytes_per_row: Some(width * WORK_BYTES as u32),
+ rows_per_image: Some(height),
+ },
+ wgpu::Extent3d {
+ width,
+ height,
+ depth_or_array_layers: 1,
+ },
+ );
+ index
+}
+
+/// The pooled texture at `index`, `width` by `height`, read back whole in
+/// the half floats it holds.
+fn read_floats(gpu: &WgpuCompositor, (width, height): (u32, u32), index: usize) -> Vec<[f32; 4]> {
+ let row = width as usize * WORK_BYTES as usize;
+ let padded = row.div_ceil(ROW_ALIGN) * ROW_ALIGN;
+ let buffer = gpu.device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("test float readback"),
+ size: (padded * height as usize) as u64,
+ usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
+ mapped_at_creation: false,
+ });
+ let mut encoder = gpu
+ .device
+ .create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
+ encoder.copy_texture_to_buffer(
+ gpu.pool[&(width, height)][index].texture.as_image_copy(),
+ wgpu::TexelCopyBufferInfo {
+ buffer: &buffer,
+ layout: wgpu::TexelCopyBufferLayout {
+ offset: 0,
+ bytes_per_row: Some(padded as u32),
+ rows_per_image: Some(height),
+ },
+ },
+ wgpu::Extent3d {
+ width,
+ height,
+ depth_or_array_layers: 1,
+ },
+ );
+ gpu.queue.submit([encoder.finish()]);
+ let slice = buffer.slice(..);
+ slice.map_async(wgpu::MapMode::Read, |_| {});
+ gpu.device
+ .poll(wgpu::PollType::wait_indefinitely())
+ .expect("the device answers");
+ let data = slice.get_mapped_range().expect("mapped");
+ (0..height as usize)
+ .flat_map(|y| data[y * padded..y * padded + row].chunks_exact(8))
+ .map(|texel| {
+ let channel = |at: usize| half::f16::from_le_bytes([texel[at], texel[at + 1]]).to_f32();
+ [channel(0), channel(2), channel(4), channel(6)]
+ })
+ .collect()
+}
+
+/// `passes` over `picture`, `size` pixels, as a layer's effects run: the
+/// last picture drawn, read back in floats.
+fn treated(
+ gpu: &mut WgpuCompositor,
+ size: (u32, u32),
+ picture: &[[f32; 4]],
+ passes: &[ShaderPass],
+) -> Vec<[f32; 4]> {
+ gpu.used.values_mut().for_each(|used| *used = 0);
+ gpu.composites += 1;
+ let source = written(gpu, size, picture);
+ let drawn = gpu.run_passes(size.0, size.1, source, passes, 0.0, 0.0);
+ let out = read_floats(gpu, size, drawn);
+ gpu.retire();
+ out
+}
+
+/// A pass of a format 2 package drawn in several passes: `body` as its
+/// shader, `passes` its `[[wgsl.pass]]` tables, `params` its knobs, at
+/// `intensity`.
+fn in_passes(body: &str, passes: &str, params: &str, intensity: f32) -> ShaderPass {
+ let manifest = concat_effects::Manifest::parse(&format!(
+ "format = 2\n[effect]\nid = \"test.passes\"\nname = \"Test\"\nkind = \"effect\"\n{params}\n[wgsl]\nentry = \"effect.wgsl\"\n{passes}"
+ ))
+ .expect("a manifest");
+ let shader = concat_effects::Shader::compile(&manifest, body).expect("compiles");
+ shader.pass(&BTreeMap::new(), &manifest.params, intensity, None, None)
+}
+
+/// The picture `picture`, `size` pixels, blurred by a Gaussian of `sigma`
+/// pixels summed directly over both axes at `(x, y)`, out to five sigmas,
+/// the edge clamped as the GPU's sampler clamps it: premultiplied colour,
+/// then alpha, in doubles.
+fn gaussian_at(
+ picture: &[[f32; 4]],
+ (width, height): (u32, u32),
+ (x, y): (i64, i64),
+ sigma: f64,
+) -> [f64; 4] {
+ let reach = (5.0 * sigma).ceil() as i64;
+ let (mut sum, mut total) = ([0.0f64; 4], 0.0f64);
+ for dy in -reach..=reach {
+ let row = (y + dy).clamp(0, i64::from(height) - 1);
+ for dx in -reach..=reach {
+ let column = (x + dx).clamp(0, i64::from(width) - 1);
+ let weight = (-((dx * dx + dy * dy) as f64) / (2.0 * sigma * sigma)).exp();
+ let [r, g, b, a] = picture[(row * i64::from(width) + column) as usize].map(f64::from);
+ for (into, value) in sum.iter_mut().zip([r * a, g * a, b * a, a]) {
+ *into += weight * value;
+ }
+ total += weight;
+ }
+ }
+ sum.map(|value| value / total)
+}
+
+/// The Gaussian blur - across and then down, at a power of two fewer
+/// pixels where its radius is wide, and read back over the layer - is the
+/// 2-D Gaussian summed directly, at every radius from its least to its
+/// most: over a picture with a gradient, a checkerboard, dots of light six
+/// times white, a hard edge past white, a half-transparent strip, and a
+/// transparent band whose colour must not bleed into anything.
+#[test]
+fn the_gaussian_blur_is_a_direct_gaussian() {
+ const PEAK: f64 = 6.0;
+ let Some(mut gpu) = gpu() else { return };
+ let blur = concat_effects::Catalogue::builtin()
+ .get("concat.gaussian-blur")
+ .expect("a built-in");
+ let size = (192, 108);
+ let picture: Vec<[f32; 4]> = (0..size.1)
+ .flat_map(|y| (0..size.0).map(move |x| (x, y)))
+ .map(|(x, y)| {
+ let checker = if (x / 6 + y / 6) % 2 == 0 { 0.1 } else { 0.6 };
+ let mut rgb = [
+ 0.02 + 0.9 * x as f32 / size.0 as f32,
+ checker,
+ 0.05 + 0.5 * y as f32 / size.1 as f32,
+ ];
+ if x >= 150 {
+ rgb = [1.5, 0.05, 0.05];
+ }
+ if x % 40 / 2 == 10 && y % 30 / 2 == 7 {
+ rgb = [PEAK as f32; 3];
+ }
+ match y {
+ 80..90 => [5.0, 5.0, 5.0, 0.0],
+ _ if x < 20 => [rgb[0], rgb[1], rgb[2], 0.5],
+ _ => [rgb[0], rgb[1], rgb[2], 1.0],
+ }
+ })
+ .collect();
+ // Below a radius of six the last pass blurs down `across` itself, and
+ // `down` is a sliver nothing reads.
+ for (radius, across, down) in [
+ (1.0, 1, 64),
+ (4.0, 1, 64),
+ (10.0, 2, 2),
+ (24.0, 8, 8),
+ (50.0, 16, 16),
+ ] {
+ let pass = blur
+ .pass(&BTreeMap::from([("radius".to_owned(), radius)]), None)
+ .expect("a shader");
+ assert_eq!(
+ pass.stages
+ .iter()
+ .map(|stage| stage.shrink)
+ .collect::<Vec<_>>(),
+ [[across, 1], [down, down]],
+ "radius {radius}"
+ );
+ let got = treated(&mut gpu, size, &picture, &[pass]);
+ // Every pixel where the sum is quick, a grid of them - the edges
+ // among them - where it is not.
+ let stride = (radius / 4.0).max(1.0) as usize;
+ let along = |length: u32| {
+ (0..length as i64)
+ .step_by(stride)
+ .chain([i64::from(length) - 1])
+ .collect::<Vec<_>>()
+ };
+ let (mut worst, mut at, mut squares, mut count) = (0.0f64, (0, 0), 0.0f64, 0);
+ for &y in &along(size.1) {
+ for &x in &along(size.0) {
+ let want = gaussian_at(&picture, size, (x, y), radius);
+ let [r, g, b, a] = got[(y * i64::from(size.0) + x) as usize].map(f64::from);
+ for (got, want) in [r * a, g * a, b * a, a].into_iter().zip(want) {
+ let error = (got - want).abs();
+ squares += error * error;
+ count += 1;
+ if error > worst {
+ (worst, at) = (error, (x, y));
+ }
+ }
+ }
+ }
+ let rms = (squares / f64::from(count)).sqrt();
+ eprintln!("radius {radius}: worst {worst:.5} at {at:?}, rms {rms:.6}");
+ // Half floats, the fetch between two pixels, and the sum cut at
+ // three sigmas each leave a trace; together they come to a quarter
+ // of a percent of the brightest light at worst.
+ assert!(
+ worst < 0.0025 * PEAK,
+ "radius {radius}: {worst} off at {at:?}"
+ );
+ assert!(rms < 0.0005 * PEAK, "radius {radius}: rms {rms}");
+ }
+}
+
+/// A pass's picture is the layer's size divided by its shrink, rounded
+/// up, and a later pass measures it through `<target>_texel`.
+#[test]
+fn a_pass_draws_its_picture_at_its_shrunk_size() {
+ let Some(mut gpu) = gpu() else { return };
+ let pass = in_passes(
+ "fn small(uv: vec2<f32>) -> vec4<f32> { return sample(uv); }\n\
+ fn effect(uv: vec2<f32>) -> vec4<f32> { let seen = small_at(uv); return vec4<f32>(1.0 / small_texel(), seen.b, 1.0); }",
+ "[[wgsl.pass]]\ntarget = \"small\"\nshrink = [\"4\", \"2\"]\n",
+ "",
+ 1.0,
+ );
+ for (side, want) in [(8, [2.0, 4.0]), (10, [3.0, 5.0])] {
+ let got = gpu
+ .probe(std::slice::from_ref(&pass), [0.1, 0.2, 0.3, 1.0], side, 0.0)
+ .expect("reads back");
+ assert!(
+ near(got, [want[0], want[1], 0.3, 1.0], 0.004),
+ "{side}: {got:?}"
+ );
+ }
+}
+
+/// A pass's picture holds what its function returned - past white and
+/// below zero, nothing clipped or mixed - and only the last pass mixes
+/// by intensity: a quarter of the way from 0.2 to the picture, once.
+#[test]
+fn only_the_last_pass_mixes_by_intensity() {
+ let Some(mut gpu) = gpu() else { return };
+ let at = |intensity: f32| {
+ in_passes(
+ "fn light(uv: vec2<f32>) -> vec4<f32> { return vec4<f32>(6.0, -0.5, 1.0, 1.0); }\n\
+ fn effect(uv: vec2<f32>) -> vec4<f32> { return light_at(uv); }",
+ "[[wgsl.pass]]\ntarget = \"light\"\nshrink = [\"2\", \"2\"]\n",
+ "",
+ intensity,
+ )
+ };
+ let whole = gpu
+ .probe(&[at(1.0)], [0.2, 0.2, 0.2, 1.0], 8, 0.0)
+ .expect("reads back");
+ assert!(near(whole, [6.0, -0.5, 1.0, 1.0], 0.004), "{whole:?}");
+ let quarter = gpu
+ .probe(&[at(0.25)], [0.2, 0.2, 0.2, 1.0], 8, 0.0)
+ .expect("reads back");
+ assert!(near(quarter, [1.65, 0.025, 0.4, 1.0], 0.004), "{quarter:?}");
+}
+
+/// A stack of packages drawn in passes hands its pictures from one package
+/// to the next: five blurs claim no more textures than two, and draw a
+/// flat picture as it was.
+#[test]
+fn a_stack_of_blurs_shares_its_pictures() {
+ let Some(mut gpu) = gpu() else { return };
+ let blur = concat_effects::Catalogue::builtin()
+ .get("concat.gaussian-blur")
+ .expect("a built-in")
+ .trial_pass()
+ .expect("a shader");
+ let colour = [0.3, 2.0, 0.05, 1.0];
+ let two = gpu
+ .probe(&vec![blur.clone(); 2], colour, 64, 0.0)
+ .expect("reads back");
+ let after_two = gpu.pool_textures;
+ let five = gpu
+ .probe(&vec![blur; 5], colour, 64, 0.0)
+ .expect("reads back");
+ assert_eq!(gpu.pool_textures, after_two, "five blurs claimed more");
+ for got in [two, five] {
+ assert!(near(got, colour, 0.004), "{got:?}");
+ }
+ // At the default radius, ten: across at half the width, then down at
+ // half of both.
+ assert!(gpu.pool.contains_key(&(32, 64)) && gpu.pool.contains_key(&(32, 32)));
+}
diff --git a/src/crates/concat-render/src/kernels.rs b/src/crates/concat-render/src/kernels.rs
index 7ed0d520..e0683cc3 100644
--- a/src/crates/concat-render/src/kernels.rs
+++ b/src/crates/concat-render/src/kernels.rs
@@ -28,7 +28,6 @@ pub const KERNELS: &[&str] = &[
"concat.hue-shift",
"concat.vignette",
"concat.box-blur",
- "concat.gaussian-blur",
];
/// Whether the CPU has a kernel for the package.
@@ -87,13 +86,6 @@ pub fn run(pass: &ShaderPass, picture: &Frame, seconds: f32) -> Option<Frame> {
let step = (knob("radius") / 4.0).max(0.5);
gathered(picture, step, |_, _| 1.0)
}
- "concat.gaussian-blur" => {
- let sigma = knob("radius").max(0.5);
- let step = (sigma / 2.5).max(1.0);
- gathered(picture, step, move |dx, dy| {
- (-(dx * dx + dy * dy) / (2.0 * sigma * sigma)).exp()
- })
- }
_ => return None,
})
}
@@ -154,7 +146,7 @@ fn per_point(picture: &Frame, f: impl Fn([f32; 4], f32, f32) -> [f32; 4]) -> Fra
out
}
-/// The blurs' 9×9 gather: every pixel becomes the weighted mean of the
+/// The box blur's 9×9 gather: every pixel becomes the weighted mean of the
/// samples `step` pixels apart around it, `weight` of the offset in
/// pixels, sampled bilinearly with the edge clamped the way the GPU's
/// sampler does.
@@ -206,6 +198,7 @@ mod tests {
intensity: 1.0,
lut: None,
reveal_map: None,
+ stages: Vec::new(),
}
}
@@ -259,11 +252,9 @@ mod tests {
#[test]
fn a_blur_of_a_flat_picture_is_the_picture_and_the_edges_stay_put() {
let flat = solid([90, 120, 30, 255]);
- for package in ["concat.box-blur", "concat.gaussian-blur"] {
- let out = run(&pass(package, &[("radius", 8.0)]), &flat, 0.0).expect("kernel");
- assert_eq!(out.pixel(0, 0), Some([90, 120, 30, 255]), "{package}");
- assert_eq!(out.pixel(3, 3), Some([90, 120, 30, 255]), "{package}");
- }
+ let out = run(&pass("concat.box-blur", &[("radius", 8.0)]), &flat, 0.0).expect("kernel");
+ assert_eq!(out.pixel(0, 0), Some([90, 120, 30, 255]));
+ assert_eq!(out.pixel(3, 3), Some([90, 120, 30, 255]));
// A negative radius is the floor, not a panic.
let out = run(&pass("concat.box-blur", &[("radius", -9.0)]), &flat, 0.0).expect("kernel");
assert_eq!(out.pixel(1, 1), Some([90, 120, 30, 255]));
diff --git a/src/crates/concat-render/src/reference.rs b/src/crates/concat-render/src/reference.rs
index bc2376f5..5439b3c5 100644
--- a/src/crates/concat-render/src/reference.rs
+++ b/src/crates/concat-render/src/reference.rs
@@ -753,6 +753,7 @@ mod tests {
intensity: 1.0,
lut: None,
reveal_map: None,
+ stages: Vec::new(),
};
let mut frame_plan = plan(8, 8, vec![red, blue]);
frame_plan.treatments = vec![PlannedTreatment {