From 5b23f6fe609385771dc100b3833d4b5f308727f9 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 9 Nov 2026 01:23:16 +0100 Subject: [PATCH] #3334: Cryptomatte uploads only the matte, computed at source resolution The matte (or the Colors display's pixels) is made from the unscaled ID and coverage channels at source resolution, box-filtered below render scale 1, kept per footage frame, layer, selection, output and scale in a cache in the common memory budget, and uploaded to the GPU as one R32F plane. OpenEXR file identity checks are remembered for 1 s; Reload Footage forgets them. --- crates/effects/src/channel3d.rs | 287 ++++++++++++++++++---- crates/effects/src/channel3d/tests.rs | 175 ++++++++++++- crates/effects/src/lib.rs | 6 +- crates/engine/src/tests/crypto_pick.rs | 26 +- crates/gpu/src/fx_depth.rs | 95 +++++-- crates/gpu/src/fx_depth/matte_cache.rs | 166 +++++++++++++ crates/gpu/src/shaders/fx_depth.wgsl | 12 +- crates/gpu/src/tests.rs | 1 + crates/gpu/src/tests/crypto_bench.rs | 85 +++++++ crates/gpu/src/tests/fx_depth.rs | 115 ++++++++- crates/host/tests/it/cryptomatte_pick.rs | 9 +- crates/media/src/exr_index.rs | 10 +- crates/media/src/exr_planes.rs | 3 +- crates/media/src/exr_planes/tests.rs | 134 ++++++++++ crates/media/src/exr_read.rs | 86 ++++++- crates/media/src/lib.rs | 15 ++ crates/media/src/pool.rs | 10 +- crates/ui-egui/tests/it/ui_crypto_pick.rs | 20 +- 18 files changed, 1130 insertions(+), 125 deletions(-) create mode 100644 crates/gpu/src/fx_depth/matte_cache.rs create mode 100644 crates/gpu/src/tests/crypto_bench.rs diff --git a/crates/effects/src/channel3d.rs b/crates/effects/src/channel3d.rs index 146c98bdf..0b05cba76 100644 --- a/crates/effects/src/channel3d.rs +++ b/crates/effects/src/channel3d.rs @@ -13,7 +13,9 @@ //! 3D layer's render. //! //! Aux pixels are looked up nearest-neighbour (IDs must not be interpolated) at each buffer -//! pixel's layer position, so render scale and layer padding are respected. +//! pixel's layer position, so render scale and layer padding are respected. Cryptomatte makes its +//! finished matte from the unscaled channels first and box-filters only that below render scale 1 +//! ([`crypto_plane`]). use std::sync::Arc; @@ -550,45 +552,224 @@ fn crypto_colors(planes: &RankPlanes, i: usize, memo: &mut IdColorMemo) -> [f32; } /// Per aux pixel of Cryptomatte layer `layer`: the coverage-weighted ID colours (RGB) and the -/// selection's coverage (A), computed in parallel with the rank planes resolved once. The GPU -/// effect uploads this image and samples it per layer pixel. +/// selection's coverage (A), computed in parallel with the rank planes resolved once: the +/// per-pixel reduction the effect's planes ([`crypto_plane`]) are made of. pub fn crypto_aux_image(a: &AuxChannels, layer: &str, sel: &[u32]) -> Image { - crypto_aux_image_with(a, layer, sel, CryptoAux::ColorsAndMatte) -} - -/// What [`crypto_aux_image_with`] computes per aux pixel. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum CryptoAux { - /// The coverage-weighted ID colours (RGB) and the selection's coverage (A). - ColorsAndMatte, - /// Only the selection's coverage (A); the RGB stays 0. - MatteOnly, -} - -impl CryptoAux { - /// What the Cryptomatte effect reads for `display` and Matte Only: the ID colours are only - /// read by the Colors display (0) without Matte Only; Matte, Preview and Matte Only need the - /// matte alone. - pub fn for_display(display: u32, matte_only: bool) -> Self { - if display == 0 && !matte_only { Self::ColorsAndMatte } else { Self::MatteOnly } - } -} - -/// [`crypto_aux_image`]; with [`CryptoAux::MatteOnly`] the RGB stays 0 and only the matte (A) is -/// computed, for the displays that never read the ID colours. -pub fn crypto_aux_image_with(a: &AuxChannels, layer: &str, sel: &[u32], what: CryptoAux) -> Image { let planes = a.crypto_rank_planes(layer); let mut img = Image::new(a.width, a.height); img.data.par_iter_mut().enumerate().for_each_init(IdColorMemo::default, |memo, (i, px)| { - let c = match what { - CryptoAux::ColorsAndMatte => crypto_colors(&planes, i, memo), - CryptoAux::MatteOnly => [0.0; 3], - }; + let c = crypto_colors(&planes, i, memo); *px = [c[0], c[1], c[2], crypto_matte(&planes, sel, i)]; }); img } +/// The Colors display's pixel for coverage-weighted ID colours `c` and matte `m`: the colours, +/// the selection brighter, over alpha 1. The CPU effect and [`crypto_plane`] both make it here. +#[inline] +fn crypto_colors_px(c: [f32; 3], m: f32) -> Px { + let c = c.map(|v| v * (1.0 - 0.5 * m) + 0.5 * m); + [c[0], c[1], c[2], 1.0] +} + +/// What a finished Cryptomatte plane ([`crypto_plane`]) holds. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum CryptoOutput { + /// The selection's coverage, one value per pixel: what Matte, Preview and Matte Only read. + Matte, + /// The Colors display's finished pixel (RGBA, alpha 1). + Colors, +} + +impl CryptoOutput { + /// What the Cryptomatte effect reads for `display` and Matte Only: the ID colours only for + /// the Colors display (0) without Matte Only; Matte, Preview and Matte Only need the matte + /// alone. + pub fn for_display(display: u32, matte_only: bool) -> Self { + if display == 0 && !matte_only { Self::Colors } else { Self::Matte } + } + + /// Values per pixel. + pub fn channels(self) -> usize { + match self { + Self::Matte => 1, + Self::Colors => 4, + } + } +} + +/// A finished Cryptomatte output (#3334): the matte, or the Colors display's pixels, made from +/// the unscaled ID and coverage channels at source resolution and only then, below render +/// scale 1, box-filtered down. ID and coverage channels are never resampled: interpolating +/// hashed IDs makes IDs that don't exist, and picking one rank's pixel loses coverage at edges. +#[derive(Clone, Debug, PartialEq)] +pub struct CryptoPlane { + pub width: u32, + pub height: u32, + /// Plane pixels per layer pixel (as [`AuxChannels::scale`]). + pub scale: f64, + pub output: CryptoOutput, + /// [`CryptoOutput::channels`] values per pixel, row by row. + pub data: Vec, +} + +impl CryptoPlane { + /// The plane pixel at layer point (`lx`, `ly`), with [`AuxChannels::index_at`]'s arithmetic. + pub fn index_at(&self, lx: f64, ly: f64) -> Option { + let x = (lx * self.scale).floor(); + let y = (ly * self.scale).floor(); + if x < 0.0 || y < 0.0 || x >= self.width as f64 || y >= self.height as f64 { + return None; + } + Some(y as usize * self.width as usize + x as usize) + } + + /// The bytes its values take. + pub fn bytes(&self) -> usize { + self.data.len().saturating_mul(std::mem::size_of::()) + } + + /// The effect's pixel for buffer pixel `px` over plane pixel `k` (`None`: outside the aux + /// image, where the matte is 0 and Colors shows black). + fn shade(&self, px: Px, k: Option, display: u32, matte_only: bool, in_alpha: bool) -> Px { + match self.output { + CryptoOutput::Colors => { + let at = k.and_then(|k| k.checked_mul(4)).and_then(|i| self.data.get(i..i.saturating_add(4))); + at.and_then(|c| <[f32; 4]>::try_from(c).ok()).unwrap_or([0.0, 0.0, 0.0, 1.0]) + } + CryptoOutput::Matte => { + let m = k.and_then(|k| self.data.get(k)).copied().unwrap_or(0.0); + crypto_matte_px(px, m, display, matte_only, in_alpha) + } + } + } +} + +/// The effect's pixel for buffer pixel `px` and matte `m` in the displays that read the matte +/// alone (Matte, Preview, and every display with Matte Only). +#[inline] +fn crypto_matte_px(px: Px, m: f32, display: u32, matte_only: bool, in_alpha: bool) -> Px { + if matte_only { + return matte_only_px(m, in_alpha); + } + match display { + // Matte: the layer keyed by the selection (each pixel only reads itself, so no copy). + 1 => px.map(|v| v * m), + // Preview: the layer with the selection tinted. + _ => [px[0] * (1.0 - 0.5 * m) + 0.5 * m * px[3], px[1] * (1.0 - 0.5 * m) + 0.5 * m * px[3], px[2] * (1.0 - 0.5 * m), px[3]], + } +} + +/// The render scale a Cryptomatte plane is box-filtered to ([`crypto_plane`]): `None` at render +/// scale 1 and above, where the plane stays at source resolution and is looked up +/// nearest-neighbour, as every 3D Channel effect looks up its channels. +pub fn crypto_box_scale(render_scale: f64) -> Option { + (render_scale.is_finite() && render_scale > 0.0 && render_scale < 1.0 - 1e-9).then_some(render_scale) +} + +/// The most pixels a box-filtered plane may have (a footage item claiming a far larger size +/// than its file would otherwise allocate a huge one); beyond it the plane stays unscaled. +const MAX_BOX_PIXELS: usize = 1 << 27; + +/// Cryptomatte layer `layer`'s output for selection `sel` (hashes) at `render_scale` (#3334): +/// [`CryptoPlane`]. The matte (or the Colors pixel) is made per aux pixel from every rank's ID +/// and coverage; below render scale 1 the finished plane is box-filtered to the render's pixel +/// grid (coverage is linear, so the filtered matte is exactly the coverage of the larger pixel; +/// the Colors pixels are coloured first and filtered after). +pub fn crypto_plane(a: &AuxChannels, layer: &str, sel: &[u32], output: CryptoOutput, render_scale: f64) -> CryptoPlane { + let src = crypto_source_plane(a, layer, sel, output); + match crypto_box_scale(render_scale) { + Some(s) => crypto_box(src, s), + None => src, + } +} + +/// [`crypto_plane`] at source resolution: one value (or pixel) per aux pixel. +fn crypto_source_plane(a: &AuxChannels, layer: &str, sel: &[u32], output: CryptoOutput) -> CryptoPlane { + let planes = a.crypto_rank_planes(layer); + let n = (a.width as usize).saturating_mul(a.height as usize); + let data = match output { + CryptoOutput::Matte => (0..n).into_par_iter().map(|i| crypto_matte(&planes, sel, i)).collect(), + CryptoOutput::Colors => { + let mut d = vec![0.0f32; n.saturating_mul(4)]; + d.par_chunks_mut(4).enumerate().for_each_init(IdColorMemo::default, |memo, (i, px)| { + let c = crypto_colors_px(crypto_colors(&planes, i, memo), crypto_matte(&planes, sel, i)); + px.copy_from_slice(&c); + }); + d + } + }; + CryptoPlane { width: a.width, height: a.height, scale: a.scale, output, data } +} + +/// For each output pixel of one axis of a box filter by `k` layer pixels: the source pixels it +/// covers and how much of each (layer pixel `x` reads source pixel `⌊(x + ½) · src_scale⌋`, the +/// pixel render scale 1 shows there). +fn box_axis(layer_len: usize, src_len: usize, src_scale: f64, k: f64, out_len: usize) -> Vec> { + (0..out_len) + .map(|j| { + let (x0, x1) = (j as f64 * k, (j as f64 + 1.0) * k); + let last = (x1.ceil() as usize).min(layer_len); + (x0.floor() as usize..last) + .filter_map(|x| { + let w = x1.min(x as f64 + 1.0) - x0.max(x as f64); + let s = ((x as f64 + 0.5) * src_scale).floor(); + (w > 0.0 && s >= 0.0 && s < src_len as f64).then_some((s as usize, w)) + }) + .collect() + }) + .collect() +} + +/// `src` (at source resolution) box-filtered to render scale `s`: each pixel of the result is +/// the mean of the render-scale-1 pixels it covers (the layer's pixels outside the aux image +/// count as 0, as the effect shows them; the Colors display's alpha is 1 everywhere). +fn crypto_box(src: CryptoPlane, s: f64) -> CryptoPlane { + if !(src.scale.is_finite() && src.scale > 0.0) { + return src; + } + // (layer pixels per output pixel; a whole number when it is one within rounding, so a + // third resolution averages exactly three by three pixels) + let k = 1.0 / s; + let k = if (k - k.round()).abs() < 1e-9 * k { k.round() } else { k }; + let (lw, lh) = ((src.width as f64 / src.scale).ceil(), (src.height as f64 / src.scale).ceil()); + let (dw, dh) = ((lw / k * (1.0 - 1e-12)).ceil(), (lh / k * (1.0 - 1e-12)).ceil()); + let limit = (src.width as usize).saturating_mul(src.height as usize).saturating_mul(4).min(MAX_BOX_PIXELS); + let fits = |v: f64| v.is_finite() && v >= 0.0 && v <= MAX_BOX_PIXELS as f64; + // (the filter visits every layer pixel: bounded by the source's size too) + if !(fits(lw) && fits(lh) && fits(dw) && fits(dh)) || (dw * dh) as usize > limit || (lw * lh) as usize > limit { + return src; + } + let (lw, lh, dw, dh) = (lw as usize, lh as usize, dw as usize, dh as usize); + let ch = src.output.channels(); + let xs = box_axis(lw, src.width as usize, src.scale, k, dw); + let ys = box_axis(lh, src.height as usize, src.scale, k, dh); + let (sw, area) = (src.width as usize, k * k); + let mut data = vec![0.0f32; dw * dh * ch]; + data.par_chunks_mut((dw * ch).max(1)).zip(ys.par_iter()).for_each(|(row, ty)| { + for (dx, tx) in xs.iter().enumerate() { + for c in 0..ch { + let mut sum = 0.0f64; + for &(sy, wy) in ty { + for &(sx, wx) in tx { + let v = src.data.get((sy * sw + sx) * ch + c).copied().unwrap_or(0.0); + sum += wy * wx * v as f64; + } + } + if let Some(o) = row.get_mut(dx * ch + c) { + *o = (sum / area) as f32; + } + } + } + }); + if src.output == CryptoOutput::Colors { + for px in data.as_chunks_mut::<4>().0 { + px[3] = 1.0; + } + } + CryptoPlane { width: dw as u32, height: dh as u32, scale: s, output: src.output, data } +} + /// The hidden switch that keeps Matte Only as projects saved before #3127 render it: the matte in /// RGB and alpha. Fresh instances have it off (the matte in RGB over alpha 1, as After Effects' /// Cryptomatte plug-in shows Matte Only); `migrate::legacy_default` turns it on for older saves. @@ -606,36 +787,44 @@ fn cryptomatte(ctx: &EffectCtx, mut b: Buf) -> Buf { let Some(layer) = crypto_layer_named(&a, ctx.params.s("layerName"), ctx.params.e("layer") as usize) else { return b }; let manifest = a.manifest(&layer); let sel = selection_hashes(ctx.params.s("selection"), manifest); - let idx = index_map(&b, &a); let display = ctx.params.e("display"); let matte_only = ctx.params.b("matteOnly"); let in_alpha = ctx.params.b(MATTE_ONLY_IN_ALPHA); + // Below render scale 1: the finished matte (or Colors pixels) box-filtered down (#3334), + // looked up per buffer pixel; the ID channels are never resampled. + if crypto_box_scale(b.scale).is_some() { + let plane = crypto_plane(&a, &layer, &sel, CryptoOutput::for_display(display, matte_only), b.scale); + let idx = crypto_index_map(&b, &plane); + b.img.data.par_iter_mut().zip(idx.par_iter()).for_each(|(px, &k)| *px = plane.shade(*px, k, display, matte_only, in_alpha)); + return b; + } + let idx = index_map(&b, &a); let planes = a.crypto_rank_planes(&layer); b.img.data.par_iter_mut().zip(idx.par_iter()).for_each_init(IdColorMemo::default, |memo, (px, &k)| { let m = k.map_or(0.0, |k| crypto_matte(&planes, &sel, k)); - if matte_only { - *px = matte_only_px(m, in_alpha); - return; - } - *px = match display { + *px = if display == 0 && !matte_only { // Colors: coverage-weighted ID colours (the spec's preview), selection brighter. - 0 => { - let c = k.map_or([0.0; 3], |k| crypto_colors(&planes, k, memo)); - let c = c.map(|v| v * (1.0 - 0.5 * m) + 0.5 * m); - [c[0], c[1], c[2], 1.0] - } - // Matte: the layer keyed by the selection (each pixel only reads itself, so no copy). - 1 => px.map(|v| v * m), - // Preview: the layer with the selection tinted. - _ => { - let s = *px; - [s[0] * (1.0 - 0.5 * m) + 0.5 * m * s[3], s[1] * (1.0 - 0.5 * m) + 0.5 * m * s[3], s[2] * (1.0 - 0.5 * m), s[3]] - } + crypto_colors_px(k.map_or([0.0; 3], |k| crypto_colors(&planes, k, memo)), m) + } else { + crypto_matte_px(*px, m, display, matte_only, in_alpha) }; }); b } +/// The [`CryptoPlane`] pixel for each buffer pixel (None outside the aux image), looked up as +/// [`index_map`] looks up the aux. +fn crypto_index_map(b: &Buf, plane: &CryptoPlane) -> Vec> { + let (w, h) = (b.img.width as usize, b.img.height as usize); + (0..w * h) + .into_par_iter() + .map(|i| { + let [x, y] = aux_point(b.scale, b.offset, [(i % w) as f64 + 0.5, (i / w) as f64 + 0.5]); + plane.index_at(x, y) + }) + .collect() +} + // ---------------------------------------------------------------- EXtractoR /// EXtractoR's channel name for After Effects' `(copy)`: the effect input's own channel of the diff --git a/crates/effects/src/channel3d/tests.rs b/crates/effects/src/channel3d/tests.rs index 8e6f4f9a9..0892b96f9 100644 --- a/crates/effects/src/channel3d/tests.rs +++ b/crates/effects/src/channel3d/tests.rs @@ -223,31 +223,186 @@ fn crypto_aux_image_is_bit_exact_on_edge_values() { } } -/// Without the ID colours the matte (A) is bit-identical and the RGB is 0, on the edge values. +/// #3334: at render scale 1 the matte plane is the per-pixel reduction's matte bit for bit, and +/// the Colors plane is the Colors display's pixel made from it, on the edge values. #[test] -fn crypto_aux_image_without_colors_keeps_the_matte() { +fn the_source_resolution_planes_are_the_per_pixel_reduction() { let a = edge_aux(true); let sel = edge_selection(); let full = crypto_aux_image(&a, "CryptoObject", &sel); - let matte = crypto_aux_image_with(&a, "CryptoObject", &sel, CryptoAux::MatteOnly); - for (f, m) in full.data.iter().zip(&matte.data) { - assert_eq!(f[3].to_bits(), m[3].to_bits()); - assert_eq!(m[..3], [0.0; 3]); + let matte = crypto_plane(&a, "CryptoObject", &sel, CryptoOutput::Matte, 1.0); + let colors = crypto_plane(&a, "CryptoObject", &sel, CryptoOutput::Colors, 1.0); + assert_eq!((matte.width, matte.height, matte.scale), (a.width, a.height, a.scale)); + assert_eq!(matte.data.len(), 12, "one value per aux pixel"); + for (i, f) in full.data.iter().enumerate() { + assert_eq!(f[3].to_bits(), matte.data[i].to_bits(), "matte {i}"); + let want = crypto_colors_px([f[0], f[1], f[2]], f[3]); + assert_eq!(want.map(f32::to_bits), [0, 1, 2, 3].map(|c| colors.data[i * 4 + c].to_bits()), "colors {i}"); } } /// Only the Colors display without Matte Only reads the ID colours. #[test] -fn crypto_aux_for_display_reads_colors_only_for_the_colors_display() { - assert_eq!(CryptoAux::for_display(0, false), CryptoAux::ColorsAndMatte); - assert_eq!(CryptoAux::for_display(0, true), CryptoAux::MatteOnly); +fn crypto_output_reads_colors_only_for_the_colors_display() { + assert_eq!(CryptoOutput::for_display(0, false), CryptoOutput::Colors); + assert_eq!(CryptoOutput::for_display(0, true), CryptoOutput::Matte); for display in [1, 2, 3, u32::MAX] { for matte_only in [false, true] { - assert_eq!(CryptoAux::for_display(display, matte_only), CryptoAux::MatteOnly, "display {display}"); + assert_eq!(CryptoOutput::for_display(display, matte_only), CryptoOutput::Matte, "display {display}"); } } } +/// A 9×7 two-rank Cryptomatte layer (`aux_scale` aux pixels per layer pixel) whose objects +/// share their edge pixels: on rank 00 "cube" (60 %) or "sphere", on rank 01 the other one +/// (the remaining coverage) along the edges, so a pixel there belongs to two IDs. +fn shared_edges(aux_scale: f64) -> AuxChannels { + let (lw, lh) = (9usize, 7usize); + let (w, h) = ((lw as f64 * aux_scale) as usize, (lh as f64 * aux_scale) as usize); + let (cube, sphere) = (crypto_float(crypto_hash("cube")), crypto_float(crypto_hash("sphere"))); + let mut a = AuxChannels::new(w as u32, h as u32, aux_scale); + let mut ch: Vec> = vec![vec![0.0; w * h]; 4]; + for y in 0..h { + for x in 0..w { + let i = y * w + x; + let (lx, ly) = (x as f64 / aux_scale, y as f64 / aux_scale); + let d = lx + 0.6 * ly - 6.0; + let edge = d.abs() < 1.5; + let (first, second) = if d < 0.0 { (cube, sphere) } else { (sphere, cube) }; + let c = if edge { 0.6 + 0.1 * ((x + y) % 3) as f32 } else { 1.0 }; + ch[0][i] = first; + ch[1][i] = c; + ch[2][i] = if edge { second } else { 0.0 }; + ch[3][i] = if edge { 1.0 - c } else { 0.0 }; + } + } + a.channels = ["00.R", "00.G", "00.B", "00.A"].iter().zip(ch).map(|(n, v)| (format!("CryptoObject{n}"), v)).collect(); + a.manifests = vec![("CryptoObject".into(), vec![("cube".into(), crypto_hash("cube")), ("sphere".into(), crypto_hash("sphere"))])]; + a +} + +/// Run the Cryptomatte effect on a buffer of the 9×7 layer at render scale 1 / `k` over `a`. +fn crypto_run(a: &AuxChannels, vals: &[(&str, Value)], k: u32) -> Image { + let host = AuxHost(Arc::new(a.clone())); + let (w, h, scale) = (9u32.div_ceil(k), 7u32.div_ceil(k), 1.0 / k as f64); + let spec = crate::find("ec.3d.cryptomatte").unwrap(); + let mut params = crate::Params { values: spec.params.iter().map(|ps| (ps.id.to_string(), crate::default_value(ps, [9.0, 7.0]))).collect() }; + for (k, v) in vals { + params.values.insert(k.to_string(), v.clone()); + } + let ctx = EffectCtx { + params: ¶ms, + time: 0.0, + layer_size: [9.0, 7.0], + seed: 0, + adjustment: false, + env: EffectEnv { host: Some(&host), ..Default::default() }, + }; + (spec.render)(&ctx, Buf { img: Image::filled(w, h, [0.8, 0.6, 0.4, 1.0]), offset: [0.0; 2], scale }).img +} + +/// The full-resolution output box-filtered by `k`, summed in the plane's order (rows, then +/// columns), the pixels past the layer's edge 0. Alpha is 1: the outputs compared (Colors, and +/// Matte Only with the matte in RGB) are opaque. +fn boxed(full: &Image, k: u32) -> Image { + let (w, h) = (full.width.div_ceil(k), full.height.div_ceil(k)); + let mut out = Image::new(w, h); + for y in 0..h { + for x in 0..w { + let mut px = [0.0f32; 4]; + for (c, o) in px.iter_mut().enumerate() { + let mut sum = 0.0f64; + for sy in y * k..y * k + k { + for sx in x * k..x * k + k { + let v = if sx < full.width && sy < full.height { full.get(sx as i64, sy as i64)[c] } else { 0.0 }; + sum += v as f64; + } + } + *o = (sum / (k * k) as f64) as f32; + } + px[3] = 1.0; + out.set(x, y, px); + } + } + out +} + +/// #3334: below render scale 1 the output is the full-resolution output box-filtered down, bit +/// for bit, at half and third resolution, for the matte (Matte Only) and the Colors display, on +/// edges where two IDs share a pixel and with the aux at 1× and 2× the layer's resolution. +#[test] +fn reduced_resolution_is_the_full_resolution_output_box_filtered() { + for aux_scale in [1.0, 2.0] { + let a = shared_edges(aux_scale); + for vals in [ + vec![("selection", Value::Str("cube".into())), ("matteOnly", Value::Bool(true))], + vec![("selection", Value::Str("cube, sphere".into())), ("matteOnly", Value::Bool(true))], + vec![("selection", Value::Str("sphere".into())), ("display", Value::Enum(0)), ("matteOnly", Value::Bool(false))], + ] { + let full = crypto_run(&a, &vals, 1); + for k in [2u32, 3] { + let got = crypto_run(&a, &vals, k); + let want = boxed(&full, k); + assert_eq!((got.width, got.height), (want.width, want.height)); + for (i, (g, w)) in got.data.iter().zip(&want.data).enumerate() { + assert_eq!(g.map(f32::to_bits), w.map(f32::to_bits), "aux {aux_scale} {vals:?} 1/{k}: pixel {i}: {g:?} vs {w:?}"); + } + } + } + // The edges hold partial coverage: a filtered pixel there is neither 0 nor 1. + let half = crypto_run(&a, &[("selection", Value::Str("cube".into())), ("matteOnly", Value::Bool(true))], 2); + assert!(half.data.iter().any(|p| p[0] > 0.05 && p[0] < 0.95), "{:?}", half.data); + } +} + +/// #3334: the ID channels never go through a resampler. The selection is the ID an +/// interpolation of the two IDs would make where they meet (their values' mean: a 2 × 2 box or +/// a bilinear sample half way between them; and their bits' midpoint): no pixel of the layer +/// has it, so its matte is 0 at every render scale, in the effect and in the plane the GPU +/// uploads. (That the planes are the per-pixel reduction is +/// `the_source_resolution_planes_are_the_per_pixel_reduction`.) +#[test] +fn ids_are_never_resampled() { + let (cube, sphere) = (crypto_float(crypto_hash("cube")), crypto_float(crypto_hash("sphere"))); + let mid_bits = ((cube.to_bits() as u64 + sphere.to_bits() as u64) / 2) as u32; + let mean = (cube + sphere) * 0.5; + for made in [mid_bits, mean.to_bits()] { + assert!(made != cube.to_bits() && made != sphere.to_bits()); + for aux_scale in [1.0, 2.0] { + let a = shared_edges(aux_scale); + for k in [1u32, 2, 3, 4] { + let sel = Value::Str(format!("<{made:08x}>")); + let out = crypto_run(&a, &[("selection", sel), ("matteOnly", Value::Bool(true))], k); + assert!(out.data.iter().all(|p| p[0] == 0.0), "aux {aux_scale} 1/{k}: an ID no pixel has was selected"); + // (and the plane the GPU uploads: the matte alone) + let plane = crypto_plane(&a, "CryptoObject", &[made], CryptoOutput::Matte, 1.0 / k as f64); + assert!(plane.data.iter().all(|m| *m == 0.0)); + } + } + } +} + +/// #3334: a hostile render scale or aux scale never allocates a huge plane or panics. +#[test] +fn box_filter_survives_hostile_scales() { + let a = shared_edges(1.0); + for s in [0.0, -1.0, f64::NAN, f64::INFINITY, 1e-12, 1.0, 4.0] { + let p = crypto_plane(&a, "CryptoObject", &[crypto_hash("cube")], CryptoOutput::Matte, s); + assert!(p.data.len() <= 9 * 7, "scale {s}: {}", p.data.len()); + } + let mut tiny = shared_edges(1.0); + tiny.scale = 1e-9; + let p = crypto_plane(&tiny, "CryptoObject", &[crypto_hash("cube")], CryptoOutput::Matte, 0.5); + assert_eq!(p.data.len(), 9 * 7, "too large a layer: left unscaled"); + // A layer claiming 1000 × its file's size at a tiny render scale: few output pixels, but the + // filter would visit 6.3e7 layer pixels; left unscaled, at once. + tiny.scale = 1e-3; + let t = std::time::Instant::now(); + let p = crypto_plane(&tiny, "CryptoObject", &[crypto_hash("cube")], CryptoOutput::Matte, 1e-3); + assert_eq!(p.data.len(), 9 * 7); + assert!(t.elapsed() < std::time::Duration::from_secs(1)); +} + /// The CPU effect on a 20×10 layer over the 6×2 edge aux: pixels inside the aux area match /// the lookup by name bit for bit in every display mode, pixels outside it (no aux index) /// get an empty matte and no ID colour. diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs index a247c6820..9ec931483 100644 --- a/crates/effects/src/lib.rs +++ b/crates/effects/src/lib.rs @@ -116,9 +116,9 @@ pub use time_fx::{PixelMotion, TimewarpPlan, TwStep, pmb_plan, posterized_time, pub use ocio::color_stabilizer_maps; // GPU effects, part B (3D Channel, Immersive Video, colour management, simulation render passes). pub use channel3d::{ - CRYPTO_LAYERS, CryptoAux, EXTRACTOR_COPY, MATTE_ONLY_IN_ALPHA, aux_need, aux_needs, aux_point, crypto_aux_image, crypto_aux_image_with, - crypto_layer_choices, crypto_layer_menu, crypto_layer_name, crypto_layer_named, extractor_aux_channel, extractor_own_channel, id_color, is_extractor_copy, - matte_only_px, pixel_margin, reads_aux, selection_hashes, + CRYPTO_LAYERS, CryptoOutput, CryptoPlane, EXTRACTOR_COPY, MATTE_ONLY_IN_ALPHA, aux_need, aux_needs, aux_point, crypto_aux_image, crypto_box_scale, + crypto_layer_choices, crypto_layer_menu, crypto_layer_name, crypto_layer_named, crypto_plane, extractor_aux_channel, extractor_own_channel, id_color, + is_extractor_copy, matte_only_px, pixel_margin, reads_aux, selection_hashes, }; pub use ocio::{ColorOp, Straight, Tf, color_program}; pub use sim::{Acc, Blob, Bubble, Post, Shape, Sprite, SpritePlan, Tint, bubble_list, bubble_sample_bounds, drizzle_drops, mercury_blobs, sprite_plan}; diff --git a/crates/engine/src/tests/crypto_pick.rs b/crates/engine/src/tests/crypto_pick.rs index 82fe21e71..066b08e06 100644 --- a/crates/engine/src/tests/crypto_pick.rs +++ b/crates/engine/src/tests/crypto_pick.rs @@ -198,8 +198,17 @@ fn picks_on_a_precomp_of_a_3d_comp() { } /// The matte the rendered Cryptomatte effect gives the picked ID at the buffer pixel under -/// `{x, y}`: the pick has to name the ID the effect's own input buffer samples there. +/// `{x, y}`: the pick has to name the ID the effect's own input buffer samples there. Below one +/// buffer pixel per layer pixel (reduced resolution, a smaller proxy) the effect shows the matte +/// box-filtered over the pixel (#3334): the picked ID is then lit, partly where the pixel holds +/// other IDs too. fn rendered_matte_of_pick(s: &mut Session, x: f64, y: f64, scale: f64) -> (String, f32) { + let (name, m, _) = rendered_matte_and_scale(s, x, y, scale); + (name, m) +} + +/// [`rendered_matte_of_pick`], and whether the buffer shows the box-filtered matte. +fn rendered_matte_and_scale(s: &mut Session, x: f64, y: f64, scale: f64) -> (String, f32, bool) { let r = s.execute_checked("effect.pickCryptomatte", json!({"effect": "Cryptomatte", "x": x, "y": y, "scale": scale, "mode": "replace"})).unwrap(); let fx = s.active_comp().unwrap().layers[0].effects().unwrap().groups().position(|g| g.match_id == "ec.3d.cryptomatte").unwrap() + 1; s.execute_checked("prop.set", json!({"path": format!("effects/#{fx}/matteOnly"), "value": true})).unwrap(); @@ -219,7 +228,7 @@ fn rendered_matte_of_pick(s: &mut Session, x: f64, y: f64, scale: f64) -> (Strin let buf = s.renderer(effectcraft_render::RenderOpts { scale, ..Default::default() }).content_buf(&ctx, layer).unwrap(); let (px, py) = buf.to_px([x, y]); let m = buf.img.get(px.floor() as i64, py.floor() as i64)[0]; - (r["name"].as_str().unwrap_or("?").to_string(), m) + (r["name"].as_str().unwrap_or("?").to_string(), m, effectcraft_effects::crypto_box_scale(buf.scale).is_some()) } /// Between 3/4 and full resolution footage keeps its native pixels: at 0.8 the effect samples @@ -239,8 +248,8 @@ fn pick_on_a_proxy_matches_the_proxy_sized_buffer() { let mut s = session(); let item = *s.project.items.iter().find(|(_, i)| matches!(i.kind, effectcraft_project::ItemKind::Footage(_))).unwrap().0; s.execute("file.setProxy", json!({"item": item.0, "path": "/renders/crypto_proxy.exr"})).unwrap(); - let (name, m) = rendered_matte_of_pick(&mut s, 6.5, 1.5, 1.0); - assert!(m >= 0.5, "picked {name:?}, but the render gives it matte {m} there"); + let (name, m, boxed) = rendered_matte_and_scale(&mut s, 6.5, 1.5, 1.0); + assert!(boxed && m > 0.0, "picked {name:?}, but the render gives it matte {m} there"); assert_eq!(name, "left"); } @@ -281,8 +290,9 @@ fn pick_maps_pixel_centres_bit_exactly_as_the_effect() { s.execute("layer.addItem", json!({"item": item})).unwrap(); s.execute_checked("effect.apply", json!({"effect": "Cryptomatte"})).unwrap(); s.execute("file.setProxy", json!({"item": item, "path": "/renders/edge_proxy.exr"})).unwrap(); - let (name, m) = rendered_matte_of_pick(&mut s, 118.0, 5.0, 1.0); - assert_eq!((name.as_str(), m), ("left", 1.0)); + let (name, m, boxed) = rendered_matte_and_scale(&mut s, 118.0, 5.0, 1.0); + assert!(boxed && m > 0.0, "{m}"); + assert_eq!(name, "left"); } struct EdgeProbe(Footage, Footage); @@ -312,8 +322,8 @@ fn mismatches(s: &mut Session, scales: &[f64], xs: impl Iterator + C if s.execute("effect.cryptomatteAt", json!({"effect": "Cryptomatte", "x": x, "y": y, "scale": scale})).unwrap().is_null() { continue; } - let (name, m) = rendered_matte_of_pick(s, x, y, scale); - if m < 0.5 { + let (name, m, boxed) = rendered_matte_and_scale(s, x, y, scale); + if if boxed { m <= 0.0 } else { m < 0.5 } { bad.push(format!("scale {scale} ({x:.2}, {y}): picked {name:?}, matte {m}")); } } diff --git a/crates/gpu/src/fx_depth.rs b/crates/gpu/src/fx_depth.rs index 996015497..daef68a3f 100644 --- a/crates/gpu/src/fx_depth.rs +++ b/crates/gpu/src/fx_depth.rs @@ -6,12 +6,15 @@ //! extra texture at the aux resolution: the planes an effect reads are packed into its four //! channels (alpha = 1 marks "inside the aux image"). `fxd_lookup` resamples it nearest-neighbour //! onto the buffer grid with the CPU's exact index arithmetic (layer position × aux scale, -//! floored), and the effect kernels work on that buffer-sized plane. Cryptomatte packs a -//! per-aux-pixel reduction of its rank layers (selection coverage and the coverage-weighted ID -//! colours, measured on the CPU) instead of the raw ranks. Fog 3D's Gradient Layer is rendered -//! by the host on the CPU (it is another layer) and uploaded. +//! floored), and the effect kernels work on that buffer-sized plane. Cryptomatte uploads its +//! finished output instead of the raw ranks (#3334): the matte as one R32F plane (the Colors +//! display: its RGBA pixels), made on the CPU from the unscaled channels at source resolution, +//! box-filtered below render scale 1 and kept in [`matte_cache`]. Fog 3D's Gradient Layer is +//! rendered by the host on the CPU (it is another layer) and uploaded. -use effectcraft_effects::{Buf, EffectCtx}; +use std::sync::Arc; + +use effectcraft_effects::{Buf, CryptoOutput, CryptoPlane, EffectCtx}; use effectcraft_keyframe::Value; use effectcraft_raster::channels3d::BACKGROUND_DEPTH; use effectcraft_raster::{AuxChannels, Image}; @@ -19,6 +22,8 @@ use effectcraft_raster::{AuxChannels, Image}; use crate::context::{Enc, GpuImage, Params}; use crate::effects::{GBuf, gaussian_blur}; +pub(crate) mod matte_cache; + /// Compute entry points in `fx_depth.wgsl`. pub(crate) const KERNELS: &[&str] = &["fxd_lookup", "fxd_point", "fxd_dof_f", "fxd_dof_level", "fxd_avg"]; @@ -73,12 +78,12 @@ fn aux_texture(e: &mut Enc, a: &AuxChannels, planes: [Option<&[f32]>; 4]) -> Opt e.g.upload_image(&img) } -/// Resample an aux texture onto the buffer grid (nearest; `fill` outside the aux image), -/// sampling each buffer pixel at sub-pixel position `at`. -fn lookup(e: &mut Enc, b: &GBuf, a: &AuxChannels, tex: &GpuImage, at: [f64; 2], fill: [f32; 4]) -> GpuImage { +/// Resample an aux texture (`tex_scale` texels per layer pixel) onto the buffer grid (nearest; +/// `fill` outside the aux image), sampling each buffer pixel at sub-pixel position `at`. +fn lookup(e: &mut Enc, b: &GBuf, tex_scale: f64, tex: &GpuImage, at: [f64; 2], fill: [f32; 4]) -> GpuImage { let inv = 1.0 / b.scale.max(1e-9); let mut p = Params::default(); - p.f[0] = [b.offset[0] as f32, b.offset[1] as f32, inv as f32, a.scale as f32]; + p.f[0] = [b.offset[0] as f32, b.offset[1] as f32, inv as f32, tex_scale as f32]; p.f[1] = [at[0] as f32, at[1] as f32, 0.0, 0.0]; p.f[2] = fill; let out = e.scratch(b.img.width, b.img.height); @@ -116,14 +121,14 @@ fn channel_extract(e: &mut Enc, ctx: &EffectCtx, b: GBuf, a: &AuxChannels) -> Op p.u[0] = [0, mode, pr.b("clampOutput") as u32, pr.b("invertDepthMap") as u32]; p.f[0] = [pr.f("blackPoint") as f32, pr.f("whitePoint") as f32, 0.0, 0.0]; if !pr.b("antialias") { - let plane = lookup(e, &b, a, &tex, [0.5, 0.5], [0.0; 4]); + let plane = lookup(e, &b, a.scale, &tex, [0.5, 0.5], [0.0; 4]); return point(e, &p, b, &plane); } // Anti-alias: four sub-pixel samples, summed in order and scaled once (as the CPU does). let (w, h) = (b.img.width, b.img.height); let mut acc: Option = None; for s in [[0.25, 0.25], [0.75, 0.25], [0.25, 0.75], [0.75, 0.75]] { - let plane = lookup(e, &b, a, &tex, s, [0.0; 4]); + let plane = lookup(e, &b, a.scale, &tex, s, [0.0; 4]); let one = e.scratch(w, h); e.pixels("fxd_point", &p, &b.img, Some(&plane), &one, None); acc = Some(match acc { @@ -146,7 +151,7 @@ fn channel_extract(e: &mut Enc, ctx: &EffectCtx, b: GBuf, a: &AuxChannels) -> Op fn depth_plane(e: &mut Enc, b: &GBuf, a: &AuxChannels) -> Option { let z = a.depth()?; let tex = aux_texture(e, a, [Some(z), None, None, None])?; - Some(lookup(e, b, a, &tex, [0.5, 0.5], [BACKGROUND_DEPTH, 0.0, 0.0, 0.0])) + Some(lookup(e, b, a.scale, &tex, [0.5, 0.5], [BACKGROUND_DEPTH, 0.0, 0.0, 0.0])) } fn depth_matte(e: &mut Enc, ctx: &EffectCtx, b: GBuf, a: &AuxChannels) -> Option { @@ -236,7 +241,7 @@ fn id_matte(e: &mut Enc, ctx: &EffectCtx, b: GBuf, a: &AuxChannels) -> Option Option< let Some(ids) = id_plane(a, pr.e("channelType")) else { return Some(b) }; let max_id = ids.iter().cloned().fold(1.0f32, f32::max); let tex = aux_texture(e, a, [Some(ids), None, None, None])?; - let plane = lookup(e, &b, a, &tex, [0.5, 0.5], [0.0; 4]); + let plane = lookup(e, &b, a.scale, &tex, [0.5, 0.5], [0.0; 4]); let mut p = op(6); p.u[0][1] = pr.e("display"); p.f[0] = [pr.f("id").round() as f32, max_id, 0.0, 0.0]; @@ -266,25 +271,67 @@ fn identifier(e: &mut Enc, ctx: &EffectCtx, b: GBuf, a: &AuxChannels) -> Option< // ---------------------------------------------------------------- Cryptomatte -fn cryptomatte(e: &mut Enc, ctx: &EffectCtx, b: GBuf, a: &AuxChannels) -> Option { +fn cryptomatte(e: &mut Enc, ctx: &EffectCtx, b: GBuf, a: &Arc) -> Option { let pr = ctx.params; let Some(layer) = effectcraft_effects::crypto_layer_named(a, pr.s("layerName"), pr.e("layer") as usize) else { return Some(b) }; let manifest = a.manifest(&layer); let sel = effectcraft_effects::selection_hashes(pr.s("selection"), manifest); - // Per aux pixel: coverage-weighted ID colours (RGB) and the selection's coverage (A), on all - // cores with the rank planes resolved once. - // The ID colours are only read by the Colors display; Matte, Preview and Matte Only need the - // matte alone. + // The finished output, made on the CPU from the unscaled ID and coverage channels at source + // resolution and box-filtered below render scale 1 (#3334): the matte alone for Matte, + // Preview and Matte Only, the finished pixels for Colors. Kept per footage frame, layer, + // selection and output, so a frame shown again neither makes nor reads the ranks again. // `tests::fx_depth::cryptomatte_every_display_matches_the_cpu` guards this against the shader. - let what = effectcraft_effects::CryptoAux::for_display(pr.e("display"), pr.b("matteOnly")); - let img = effectcraft_effects::crypto_aux_image_with(a, &layer, &sel, what); - let tex = e.g.upload_image(&img)?; - let plane = lookup(e, &b, a, &tex, [0.5, 0.5], [0.0; 4]); + let output = CryptoOutput::for_display(pr.e("display"), pr.b("matteOnly")); + let finished = matte_cache::plane(a, &layer, &sel, output, b.scale); + let tex = upload_plane(e, &finished)?; + let plane = lookup(e, &b, finished.scale, &tex, [0.5, 0.5], [0.0; 4]); let mut p = op(7); p.u[0] = [7, pr.e("display"), pr.b("matteOnly") as u32, pr.b(effectcraft_effects::MATTE_ONLY_IN_ALPHA) as u32]; point(e, &p, b, &plane) } +/// Upload a finished Cryptomatte plane: the matte as one R32F channel (four bytes a pixel, not +/// the sixteen of a working image), the Colors display's pixels as RGBA. `None` when it does not +/// fit the device (the effect then runs on the CPU). +fn upload_plane(e: &Enc, p: &CryptoPlane) -> Option { + let g = e.g; + if !g.fits(p.width, p.height) { + return None; + } + let (format, bpp) = match p.output { + CryptoOutput::Matte => (wgpu::TextureFormat::R32Float, 4), + CryptoOutput::Colors => (wgpu::TextureFormat::Rgba32Float, 16), + }; + let n = (p.width as usize).checked_mul(p.height as usize)?.checked_mul(p.output.channels())?; + if p.data.len() != n { + return None; + } + let size = wgpu::Extent3d { width: p.width, height: p.height, depth_or_array_layers: 1 }; + let texture = g.device.create_texture(&wgpu::TextureDescriptor { + label: Some("Cryptomatte plane"), + size, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + g.queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + bytemuck::cast_slice(&p.data), + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(p.width.checked_mul(bpp)?), rows_per_image: Some(p.height) }, + size, + ); + #[cfg(test)] + PLANE_UPLOADS.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push((p.output, p.width, p.height, p.bytes())); + Some(GpuImage::new(texture, p.width, p.height)) +} + +/// The Cryptomatte planes uploaded so far (output, width, height, bytes; tests). +#[cfg(test)] +pub(crate) static PLANE_UPLOADS: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + // ---------------------------------------------------------------- EXtractoR fn extractor(e: &mut Enc, ctx: &EffectCtx, b: GBuf, a: Option<&AuxChannels>) -> Option { @@ -303,7 +350,7 @@ fn extractor(e: &mut Enc, ctx: &EffectCtx, b: GBuf, a: Option<&AuxChannels>) -> let plane = match a { Some(a) if src_ch.iter().any(Option::is_some) => { let tex = aux_texture(e, a, [src_ch[0], src_ch[1], src_ch[2], src_ch[3]])?; - Some(lookup(e, &b, a, &tex, [0.5, 0.5], [0.0; 4])) + Some(lookup(e, &b, a.scale, &tex, [0.5, 0.5], [0.0; 4])) } _ => None, }; diff --git a/crates/gpu/src/fx_depth/matte_cache.rs b/crates/gpu/src/fx_depth/matte_cache.rs new file mode 100644 index 000000000..093b54d1b --- /dev/null +++ b/crates/gpu/src/fx_depth/matte_cache.rs @@ -0,0 +1,166 @@ +//! Finished Cryptomatte planes (#3334), kept so a frame shown again neither reads the rank +//! channels again nor makes the matte again: per footage frame (the decoded channels the plane +//! was made of), Cryptomatte layer, selection, output and render scale. +//! +//! The frame is named by its channels' identity: the footage source hands out the same +//! [`AuxChannels`] while it keeps that frame's channels, and new ones once the frame changes or +//! the file is forgotten (Reload Footage), so a stale plane can't be found. The cache holds them +//! only weakly (it never keeps decoded channels alive); a plane whose channels are gone is the +//! first to go. +//! +//! The planes are in the common memory budget ([`effectcraft_render::memory`]), each worth what +//! making it took, and bounded by themselves as well (a render without a budget keeps no more +//! than [`MAX_ENTRIES`] planes and [`MAX_BYTES`]). + +use std::sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError, Weak}; + +use effectcraft_effects::{CryptoOutput, CryptoPlane}; +use effectcraft_raster::AuxChannels; +use effectcraft_render::memory::{self, Accountant, Candidate, MemoryHolder}; + +/// At most this many planes… +const MAX_ENTRIES: usize = 32; +/// …and bytes, whatever the common budget. +const MAX_BYTES: usize = 512 << 20; + +#[derive(Clone, Debug, PartialEq)] +struct Key { + layer: String, + sel: Vec, + output: CryptoOutput, + /// The render scale the plane is box-filtered to (its bits), `None` at source resolution. + scale: Option, +} + +struct Entry { + /// The channels it was made of (held weakly: the allocation stays while this lives, so its + /// address names these channels alone). + aux: Weak, + key: Key, + plane: Arc, + /// Last use ([`memory::tick`]) and what making it took (µs). + used: u64, + cost_us: u64, +} + +impl Entry { + fn of(&self, a: &Arc) -> bool { + std::ptr::eq(self.aux.as_ptr(), Arc::as_ptr(a)) + } + fn gone(&self) -> bool { + self.aux.strong_count() == 0 + } +} + +struct Cache { + entries: Vec, + /// Planes made so far (tests). + made: u64, +} + +static CACHE: Mutex = Mutex::new(Cache { entries: Vec::new(), made: 0 }); + +fn lock() -> MutexGuard<'static, Cache> { + CACHE.lock().unwrap_or_else(PoisonError::into_inner) +} + +/// In the common memory budget, registered once. +fn register() { + static HOLDER: OnceLock> = OnceLock::new(); + HOLDER.get_or_init(|| { + let h: Arc = Arc::new(Holder); + Accountant::global().register(&h); + h + }); +} + +/// Cryptomatte layer `layer`'s finished plane for selection `sel` (hashes) at `render_scale` +/// ([`effectcraft_effects::crypto_plane`]), kept or made now. +pub(crate) fn plane(a: &Arc, layer: &str, sel: &[u32], output: CryptoOutput, render_scale: f64) -> Arc { + register(); + let key = Key { layer: layer.to_string(), sel: sel.to_vec(), output, scale: effectcraft_effects::crypto_box_scale(render_scale).map(f64::to_bits) }; + { + let mut c = lock(); + let now = memory::tick(); + if let Some(e) = c.entries.iter_mut().find(|e| e.of(a) && e.key == key) { + e.used = now; + return e.plane.clone(); + } + } + let started = web_time::Instant::now(); + let made = Arc::new(effectcraft_effects::crypto_plane(a, layer, sel, output, render_scale)); + let cost_us = u64::try_from(started.elapsed().as_micros()).unwrap_or(u64::MAX).max(1); + let bytes = made.bytes(); + { + let mut c = lock(); + c.made += 1; + // (planes of channels that are gone can't be asked for again; another thread may have + // made this one meanwhile) + c.entries.retain(|e| !e.gone() && !(e.of(a) && e.key == key)); + c.entries.push(Entry { aux: Arc::downgrade(a), key, plane: made.clone(), used: memory::tick(), cost_us }); + loop { + let total: usize = c.entries.iter().map(|e| e.plane.bytes()).fold(0, usize::saturating_add); + if c.entries.len() <= 1 || (c.entries.len() <= MAX_ENTRIES && total <= MAX_BYTES) { + break; + } + let Some(i) = oldest(&c) else { break }; + c.entries.swap_remove(i); + } + } + Accountant::global().note_growth(bytes); + made +} + +/// The entry to drop next: one whose channels are gone, else the least recently used of those +/// no render holds now (dropping one in use would free nothing yet). +fn oldest(c: &Cache) -> Option { + c.entries + .iter() + .position(Entry::gone) + .or_else(|| c.entries.iter().enumerate().filter(|(_, e)| Arc::strong_count(&e.plane) == 1).min_by_key(|(_, e)| e.used).map(|(i, _)| i)) +} + +/// The planes in the common memory budget. +struct Holder; + +impl MemoryHolder for Holder { + fn name(&self) -> &'static str { + "Cryptomatte mattes" + } + fn bytes(&self) -> usize { + lock().entries.iter().map(|e| e.plane.bytes()).fold(0, usize::saturating_add) + } + fn oldest(&self) -> Option { + let c = lock(); + let e = c.entries.get(oldest(&c)?)?; + // (a plane of channels that are gone is worth nothing) + Some(if e.gone() { + Candidate { used: 0, bytes: e.plane.bytes(), cost_us: 0 } + } else { + Candidate { used: e.used, bytes: e.plane.bytes(), cost_us: e.cost_us } + }) + } + fn drop_oldest(&self) -> usize { + let mut c = lock(); + let Some(i) = oldest(&c) else { return 0 }; + c.entries.swap_remove(i).plane.bytes() + } +} + +/// Planes made so far (tests). +#[cfg(test)] +pub(crate) fn made() -> u64 { + lock().made +} + +/// Planes kept now (tests). +#[cfg(test)] +pub(crate) fn kept() -> usize { + lock().entries.len() +} + +/// The planes in the common memory budget (tests). +#[cfg(test)] +pub(crate) fn holder() -> Arc { + Arc::new(Holder) +} diff --git a/crates/gpu/src/shaders/fx_depth.wgsl b/crates/gpu/src/shaders/fx_depth.wgsl index 68fc78f9f..6d892fe9b 100644 --- a/crates/gpu/src/shaders/fx_depth.wgsl +++ b/crates/gpu/src/shaders/fx_depth.wgsl @@ -198,15 +198,17 @@ fn fxd_point(@builtin(global_invocation_id) gid: vec3) { } } } - // Cryptomatte (aux = (ID colours, selection coverage)). u[0] = (op, display, matte only, - // matte only in alpha). Matte Only is the matte in RGB over alpha 1, or in every channel - // for projects saved before #3127 (`effectcraft_effects::matte_only_px`). + // Cryptomatte (aux = the finished plane, #3334: the selection's coverage in x; for the + // Colors display, its finished pixel). u[0] = (op, display, matte only, matte only in + // alpha). Matte Only is the matte in RGB over alpha 1, or in every channel for projects + // saved before #3127 (`effectcraft_effects::matte_only_px`). Outside the aux image the + // plane is 0: no matte, and Colors shows black over alpha 1. case 7u: { - let m = q.w; + let m = q.x; if (P.u[0].z != 0u) { o = select(vec4(m, m, m, 1.0), vec4(m), P.u[0].w != 0u); } else if (P.u[0].y == 0u) { - o = vec4(q.xyz * (1.0 - 0.5 * m) + 0.5 * m, 1.0); + o = vec4(q.xyz, 1.0); } else if (P.u[0].y == 1u) { o = px * m; } else { diff --git a/crates/gpu/src/tests.rs b/crates/gpu/src/tests.rs index b88bac0be..40204762f 100644 --- a/crates/gpu/src/tests.rs +++ b/crates/gpu/src/tests.rs @@ -28,6 +28,7 @@ use crate::Gpu; mod adjust; mod adv3d; +mod crypto_bench; mod deferred; mod deferred_jobs; mod fx_color; diff --git a/crates/gpu/src/tests/crypto_bench.rs b/crates/gpu/src/tests/crypto_bench.rs new file mode 100644 index 000000000..050c92f10 --- /dev/null +++ b/crates/gpu/src/tests/crypto_bench.rs @@ -0,0 +1,85 @@ +//! #3334: what Cryptomatte costs on the GPU at 1920 × 1080 with three ranks (six ID and coverage +//! planes), at full and half resolution: the first frame and the same frame again. Ignored; run +//! with `cargo test -p effectcraft-gpu --release crypto_bench -- --ignored --nocapture`. + +use std::sync::Arc; + +use effectcraft_effects::EffectEnv; +use effectcraft_keyframe::Value; +use effectcraft_raster::channels3d::{crypto_float, crypto_hash}; +use effectcraft_raster::{AuxChannels, Image}; + +use crate::tests::fx_depth::AuxHost; +use crate::tests::gpu; + +const NAMES: [&str; 6] = ["floor", "wall", "chair", "table", "lamp", "cup"]; + +/// 1920 × 1080, three ranks: diagonal bands of objects whose edges share pixels. +fn aux_1080p() -> AuxChannels { + let (w, h) = (1920usize, 1080usize); + let ids: Vec = NAMES.iter().map(|n| crypto_float(crypto_hash(n))).collect(); + let mut a = AuxChannels::new(w as u32, h as u32, 1.0); + let mut planes = vec![vec![0.0f32; w * h]; 6]; + for y in 0..h { + for x in 0..w { + let i = y * w + x; + let band = (x + y / 2) / 97; + let edge = (x + y / 2) % 97 < 3; + let c = if edge { 0.6 } else { 1.0 }; + planes[0][i] = ids[band % 6]; + planes[1][i] = c; + planes[2][i] = ids[(band + 1) % 6]; + planes[3][i] = 1.0 - c; + planes[4][i] = ids[(band + 2) % 6]; + planes[5][i] = 0.0; + } + } + let names = ["00.R", "00.G", "00.B", "00.A", "01.R", "01.G"]; + a.channels = names.iter().zip(planes).map(|(n, p)| (format!("CryptoObject{n}"), p)).collect(); + a.manifests = vec![("CryptoObject".into(), NAMES.iter().map(|n| (n.to_string(), crypto_hash(n))).collect())]; + a +} + +#[test] +#[ignore] +fn crypto_bench() { + let Some(g) = gpu() else { return }; + let host = AuxHost(Arc::new(aux_1080p())); + let spec = effectcraft_effects::find("ec.3d.cryptomatte").unwrap(); + let size = [1920.0, 1080.0]; + for (label, display, matte_only) in [("Matte", 1u32, false), ("Matte Only", 1, true), ("Colors", 0, false)] { + let mut params = + effectcraft_effects::Params { values: spec.params.iter().map(|ps| (ps.id.to_string(), effectcraft_effects::default_value(ps, size))).collect() }; + params.values.insert("selection".into(), Value::Str("chair, lamp".into())); + params.values.insert("display".into(), Value::Enum(display)); + params.values.insert("matteOnly".into(), Value::Bool(matte_only)); + for scale in [1.0, 0.5] { + let (w, h) = ((size[0] * scale) as u32, (size[1] * scale) as u32); + let buf = effectcraft_effects::Buf { img: Image::filled(w, h, [0.5, 0.4, 0.3, 1.0]), offset: [0.0; 2], scale }; + let ctx = || effectcraft_effects::EffectCtx { + params: ¶ms, + time: 0.0, + layer_size: size, + seed: 0, + adjustment: false, + env: EffectEnv { host: Some(&host), ..Default::default() }, + }; + // The effect alone on a buffer already on the GPU, until its result is done (a 1 × 1 + // readback waits for it): no upload of the layer, no readback of the frame. + let mut e = crate::context::Enc::new(&g.ctx); + let input = g.ctx.upload_image(&buf.img).unwrap(); + let mut run = || { + let t = std::time::Instant::now(); + let gbuf = crate::effects::GBuf { img: input.clone(), offset: buf.offset, scale }; + let out = crate::fx_depth::apply(&mut e, "ec.3d.cryptomatte", &ctx(), gbuf).expect("the GPU runs the effect"); + let one = e.crop(&out.img, 0, 0, 1, 1); + e.download(&one).expect("a readback"); + t.elapsed().as_secs_f64() * 1000.0 + }; + let first = run(); + let mut warm: Vec = (0..20).map(|_| run()).collect(); + warm.sort_by(f64::total_cmp); + eprintln!("crypto_bench {label} scale {scale}: first frame {first:.2} ms, same frame again {:.2} ms (median of 20)", warm[10]); + } + } +} diff --git a/crates/gpu/src/tests/fx_depth.rs b/crates/gpu/src/tests/fx_depth.rs index f60f31e3e..1eb122d33 100644 --- a/crates/gpu/src/tests/fx_depth.rs +++ b/crates/gpu/src/tests/fx_depth.rs @@ -259,7 +259,7 @@ fn ids_and_cryptomatte() { } /// The GPU skips the ID colours for displays that do not read them -/// ([`effectcraft_effects::CryptoAux::for_display`]): every display × Matte Only must still match +/// ([`effectcraft_effects::CryptoOutput::for_display`]): every display × Matte Only must still match /// the CPU effect, so a shader that starts reading RGB in another mode fails here. Matte Only is /// checked both ways: the matte in RGB over alpha 1 (#3127) and, for older projects, in alpha too. #[test] @@ -279,6 +279,119 @@ fn cryptomatte_every_display_matches_the_cpu() { } } +/// Cryptomatte alone on a `scale` buffer of the 70 × 44 layer over `host`, on the GPU and the +/// CPU: (GPU, CPU, the Cryptomatte planes the GPU uploaded, the uploads counted by the context). +fn crypto_direct(host: &AuxHost, vals: &[(&str, Value)], scale: f64) -> Option<(Image, Image, Vec<(effectcraft_effects::CryptoOutput, u32, u32, usize)>, u64)> { + let g = gpu()?; + let spec = effectcraft_effects::find("ec.3d.cryptomatte").unwrap(); + let size = [70.0, 44.0]; + let mut params = + effectcraft_effects::Params { values: spec.params.iter().map(|ps| (ps.id.to_string(), effectcraft_effects::default_value(ps, size))).collect() }; + for (k, v) in vals { + params.values.insert(k.to_string(), v.clone()); + } + let (w, h) = ((size[0] * scale) as u32, (size[1] * scale) as u32); + let buf = effectcraft_effects::Buf { img: pattern(5, w, h), offset: [0.0; 2], scale }; + let ctx = || effectcraft_effects::EffectCtx { + params: ¶ms, + time: 0.0, + layer_size: size, + seed: 0, + adjustment: false, + env: EffectEnv { host: Some(host), ..Default::default() }, + }; + let cpu = (spec.render)(&ctx(), buf.clone()); + let planes = || crate::fx_depth::PLANE_UPLOADS.lock().unwrap().len(); + let (before, uploads) = (planes(), g.ctx.transfer_stats().uploads); + let out = effectcraft_render::Accelerator::effects(g, &[effectcraft_render::FxStep { spec, ctx: ctx() }], &buf, None).expect("the GPU runs the effect"); + let uploads = g.ctx.transfer_stats().uploads - uploads; + let uploaded = crate::fx_depth::PLANE_UPLOADS.lock().unwrap()[before..].to_vec(); + Some((out.img, cpu.img, uploaded, uploads)) +} + +/// #3334: at full resolution the GPU's Cryptomatte is bit-identical to the CPU effect (whose +/// full-resolution path is unchanged) in the displays whose output is the plane or one product +/// with it (Matte Only, Colors, Matte); the Preview tint within rounding. It uploads one plane: +/// the matte as R32F at source resolution (the Colors display its RGBA pixels), and nothing +/// else of the channels (the context's own uploads are the layer's buffer alone). +#[test] +fn cryptomatte_full_resolution_uploads_only_the_matte_and_is_bit_identical() { + use effectcraft_effects::CryptoOutput; + let host = AuxHost(Arc::new(aux_scene(70, 44, 1.0))); + for (vals, output, exact) in [ + (vec![("matteOnly", on())], CryptoOutput::Matte, true), + (vec![("display", e(0))], CryptoOutput::Colors, true), + (vec![("display", e(1))], CryptoOutput::Matte, true), + (vec![("display", e(2))], CryptoOutput::Matte, false), + ] { + let mut vals = vals; + vals.push(("selection", Value::Str("beta, delta".into()))); + let Some((gpu_img, cpu_img, planes, uploads)) = crypto_direct(&host, &vals, 1.0) else { return }; + let bytes = 70 * 44 * output.channels() * 4; + assert_eq!(planes, [(output, 70, 44, bytes)], "{vals:?}: one plane, at source resolution"); + assert_eq!(uploads, 1, "{vals:?}: the layer's buffer, no channel"); + if exact { + for (i, (g, c)) in gpu_img.data.iter().zip(&cpu_img.data).enumerate() { + assert_eq!(g.map(f32::to_bits), c.map(f32::to_bits), "{vals:?}: pixel {i}"); + } + } else { + let d = diff(&cpu_img, &gpu_img, 1e-6); + assert_eq!(d.over, 0, "{vals:?}: max {}, worst {:?}", d.max, d.worst); + } + } +} + +/// #3334: at half resolution the GPU uploads the box-filtered matte (a quarter of the pixels, +/// four bytes each) and shows what the CPU effect shows. +#[test] +fn cryptomatte_half_resolution_uploads_the_filtered_matte() { + use effectcraft_effects::CryptoOutput; + let host = AuxHost(Arc::new(aux_scene(70, 44, 1.0))); + let vals = [("selection", Value::Str("beta, delta".into())), ("matteOnly", on())]; + let Some((gpu_img, cpu_img, planes, uploads)) = crypto_direct(&host, &vals, 0.5) else { return }; + assert_eq!(planes, [(CryptoOutput::Matte, 35, 22, 35 * 22 * 4)]); + assert_eq!(uploads, 1); + for (i, (g, c)) in gpu_img.data.iter().zip(&cpu_img.data).enumerate() { + assert_eq!(g.map(f32::to_bits), c.map(f32::to_bits), "pixel {i}"); + } + assert!(gpu_img.data.iter().any(|p| p[0] > 0.0 && p[0] < 1.0), "partial coverage where objects meet"); +} + +/// #3334: the finished planes are kept per footage frame (its channels), layer, selection, +/// output and render scale: the same frame again makes none; another selection, output or +/// scale makes its own. Once the frame's channels are gone, its planes are the first the memory +/// accountant drops, at no cost. +#[test] +fn cryptomatte_planes_are_kept_per_frame_and_selection() { + use crate::fx_depth::matte_cache; + use effectcraft_effects::CryptoOutput; + crate::tests::hold_gpu_lock(); + let a = Arc::new(aux_scene(70, 44, 1.0)); + let sel = vec![crypto_hash("beta")]; + let made = matte_cache::made(); + let one = matte_cache::plane(&a, "CryptoObject", &sel, CryptoOutput::Matte, 1.0); + assert_eq!(matte_cache::made(), made + 1); + let again = matte_cache::plane(&a, "CryptoObject", &sel, CryptoOutput::Matte, 1.0); + assert!(Arc::ptr_eq(&one, &again) && matte_cache::made() == made + 1, "kept"); + matte_cache::plane(&a, "CryptoObject", &[crypto_hash("gamma")], CryptoOutput::Matte, 1.0); + matte_cache::plane(&a, "CryptoObject", &sel, CryptoOutput::Colors, 1.0); + matte_cache::plane(&a, "CryptoObject", &sel, CryptoOutput::Matte, 0.5); + assert_eq!(matte_cache::made(), made + 4, "one per selection, output and scale"); + // Another frame (other channels, equal values) makes its own. + let other = Arc::new(aux_scene(70, 44, 1.0)); + matte_cache::plane(&other, "CryptoObject", &sel, CryptoOutput::Matte, 1.0); + assert_eq!(matte_cache::made(), made + 5); + // The frame's channels go: its planes are worth nothing. + drop(other); + let holder = matte_cache::holder(); + let next = holder.oldest().unwrap(); + assert_eq!(next.cost_us, 0, "{next:?}"); + let kept = matte_cache::kept(); + assert_eq!(holder.drop_oldest(), 70 * 44 * 4); + assert_eq!(matte_cache::kept(), kept - 1); + assert!(Arc::ptr_eq(&one, &matte_cache::plane(&a, "CryptoObject", &sel, CryptoOutput::Matte, 1.0)), "the live frame's plane stays"); +} + #[test] fn extractor() { case("ec.3d.extractor", &[("red", Value::Str("Z".into())), ("green", Value::Str("UV.U".into())), ("whitePoint", n(1000.0))]); diff --git a/crates/host/tests/it/cryptomatte_pick.rs b/crates/host/tests/it/cryptomatte_pick.rs index 57b6e624b..8a674a962 100644 --- a/crates/host/tests/it/cryptomatte_pick.rs +++ b/crates/host/tests/it/cryptomatte_pick.rs @@ -73,8 +73,10 @@ fn picking_on_a_cryptomatte_exr_edits_the_selection_and_the_matte() { let _ = std::fs::remove_dir_all(std::path::Path::new(&path).parent().unwrap()); } -/// At half resolution a preview pixel shows the aux texel at its centre (layer x 3 for preview -/// pixel 1): `scale` makes the pick read that one, so what is clicked is what lights up. +/// At half resolution the pick reads the aux texel at the preview pixel's centre (layer x 3 for +/// preview pixel 1): `scale` makes the pick read that one, so what is clicked lights up. The +/// preview pixel shows the matte box-filtered over the two layer pixels it covers (#3334): Left's +/// x 2 and Right's x 3, half lit. #[test] fn a_pick_at_preview_resolution_reads_the_pixel_the_preview_shows() { let (mut s, path) = footage("half", 3); @@ -83,7 +85,8 @@ fn a_pick_at_preview_resolution_reads_the_pixel_the_preview_shows() { assert_eq!(r["name"], RIGHT, "{r}"); let opts = effectcraft_engine::render::RenderOpts { scale: 0.5, ..Default::default() }; // Matte Only: the matte in RGB over alpha 1 (#3127). - assert_eq!(s.render(s.active_comp_id().unwrap(), Tick::ZERO, opts).get(1, 0), [1.0; 4]); + // (0.5 at 8 bpc) + assert_eq!(s.render(s.active_comp_id().unwrap(), Tick::ZERO, opts).get(1, 0), [128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1.0]); // At full resolution the same point is Left's pixel. let r = s.execute_checked("effect.cryptomatteAt", json!({"effect": 1, "x": 2.5, "y": 1.5})).unwrap(); assert_eq!(r["name"], "Left"); diff --git a/crates/media/src/exr_index.rs b/crates/media/src/exr_index.rs index 21051166d..b642aba89 100644 --- a/crates/media/src/exr_index.rs +++ b/crates/media/src/exr_index.rs @@ -421,8 +421,9 @@ pub(crate) fn keep(path: &str, index: Arc) { } /// The index of the file at `path` (read from `source` the first time; kept until -/// [`forget`]). A file whose identity changed since (size, modification time) is indexed again; -/// a read checks it again on the file it opens. +/// [`forget`]). A file whose identity changed since (size, modification time; checked at most +/// once per [`crate::exr_read::STAMP_TTL`], #3334) is indexed again; a read checks it again on +/// the file it opens. pub(crate) fn get(path: &str, source: &ExrSource) -> Result, IndexError> { get_opened(path, source).map(|(ix, _)| ix) } @@ -445,6 +446,8 @@ pub(crate) fn get_opened(path: &str, source: &ExrSource) -> Result<(Arc Option> { lock().map.get(path).cloned() } -/// Drop the index of `path` (the file changed on disk). +/// Drop the index of `path` (the file changed on disk), and the identity last read of it. pub(crate) fn forget(path: &str) { + crate::exr_read::forget_stamp(std::path::Path::new(path)); let mut ix = lock(); if let Some(gone) = ix.map.remove(path) { ix.bytes = ix.bytes.saturating_sub(gone.bytes()); diff --git a/crates/media/src/exr_planes.rs b/crates/media/src/exr_planes.rs index e3607b15d..94b9cd0d0 100644 --- a/crates/media/src/exr_planes.rs +++ b/crates/media/src/exr_planes.rs @@ -910,7 +910,8 @@ fn decode(path: &str, source: &ExrSource, flight: &Arc, base: Option Vec { + use effectcraft_raster::channels3d::{crypto_float, crypto_hash}; + let (w, h) = size; + let (cube, sphere) = (crypto_float(crypto_hash("cube")), crypto_float(crypto_hash("sphere"))); + let mut ch: Vec> = vec![vec![0.0; w * h]; 4]; + for y in 0..h { + for x in 0..w { + let i = y * w + x; + let d = x as f64 + 0.7 * y as f64 - w as f64 * 0.6; + let edge = d.abs() < 1.5; + let (first, second) = if d < 0.0 { (cube, sphere) } else { (sphere, cube) }; + let c = if edge { 0.55 + 0.1 * ((x * 3 + y) % 3) as f32 } else { 1.0 }; + ch[0][i] = first; + ch[1][i] = c; + ch[2][i] = if edge { second } else { 0.0 }; + ch[3][i] = if edge { 1.0 - c } else { 0.0 }; + } + } + let mut list: Vec> = ["CryptoObject00.R", "CryptoObject00.G", "CryptoObject00.B", "CryptoObject00.A"] + .iter() + .zip(ch) + .map(|(n, v)| AnyChannel::new(*n, FlatSamples::F32(v))) + .collect(); + for c in ["R", "G", "B", "A"] { + list.push(AnyChannel::new(c, FlatSamples::F16(vec![f16::from_f32(0.5); w * h]))); + } + let encoding = Encoding { compression: Compression::ZIP1, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }; + write(vec![Layer::new(size, LayerAttributes::default(), encoding, AnyChannels::sort(list.into()))], size) +} + +/// The effects' view of a footage layer's channels (tests). +struct ChannelsHost(Arc); + +impl effectcraft_effects::EffectHost for ChannelsHost { + fn layer(&self, _: u64, _: bool) -> Option { + None + } + fn audio(&self, _: u64, _: f64, _: usize, _: u32) -> Option> { + None + } + fn aux(&self) -> Option> { + Some(self.0.clone()) + } +} + +/// #3334, on an OpenEXR written here and read through the pool: Cryptomatte at half resolution +/// is the full-resolution output box-filtered down (2 × 2 pixels averaged, bit for bit), along +/// edges where two IDs share a pixel; the channels the effect reads are the same set for every +/// request of the frame (the GPU keeps its matte by them). +#[test] +fn cryptomatte_at_half_resolution_is_the_full_resolution_matte_box_filtered() { + let size = (23usize, 17usize); + let file = Temp::new("crypto-half", &crypto_edges(size)); + let f = crate::probe(&file.0).unwrap(); + let pool = crate::MediaPool::new(); + let need = AuxNeed::All; + let aux = pool.aux_for(ItemId(1), &f, Tick::ZERO, &need).unwrap(); + assert!(Arc::ptr_eq(&aux, &pool.aux_for(ItemId(1), &f, Tick::ZERO, &need).unwrap()), "the same channels for the frame"); + assert_eq!(aux.crypto_rank_planes("CryptoObject").len(), 2); + let host = ChannelsHost(aux); + let spec = effectcraft_effects::find("ec.3d.cryptomatte").unwrap(); + let layer = [size.0 as f64, size.1 as f64]; + let mut params = + effectcraft_effects::Params { values: spec.params.iter().map(|ps| (ps.id.to_string(), effectcraft_effects::default_value(ps, layer))).collect() }; + params.values.insert("selection".into(), effectcraft_project::Value::Str("cube".into())); + params.values.insert("matteOnly".into(), effectcraft_project::Value::Bool(true)); + let run = |k: u32| { + let ctx = effectcraft_effects::EffectCtx { + params: ¶ms, + time: 0.0, + layer_size: layer, + seed: 0, + adjustment: false, + env: effectcraft_effects::EffectEnv { host: Some(&host), ..Default::default() }, + }; + let (w, h) = ((size.0 as u32).div_ceil(k), (size.1 as u32).div_ceil(k)); + (spec.render)(&ctx, effectcraft_effects::Buf { img: Image::filled(w, h, [1.0; 4]), offset: [0.0; 2], scale: 1.0 / k as f64 }).img + }; + let (full, half) = (run(1), run(2)); + let mut partial = 0; + for y in 0..half.height { + for x in 0..half.width { + let mut sum = 0.0f64; + for sy in y * 2..y * 2 + 2 { + for sx in x * 2..x * 2 + 2 { + if sx < full.width && sy < full.height { + sum += full.get(sx as i64, sy as i64)[0] as f64; + } + } + } + let m = (sum / 4.0) as f32; + assert_eq!(half.get(x as i64, y as i64), [m, m, m, 1.0], "pixel {x}, {y}"); + partial += usize::from(m > 0.0 && m < 1.0); + } + } + assert!(partial > 4, "edges: {partial}"); +} + +/// #3334: a warm frame checks its file's identity on disk at most once: repeated requests for +/// the channels within the stamp's lifetime use the identity read before, and Reload Footage +/// (forget) reads it again. +#[test] +fn a_warm_frame_checks_its_file_at_most_once() { + let dir = std::env::temp_dir().join(format!("ec-3334-stamps-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("crypto_0001.exr"); + std::fs::write(&file, crypto_edges((8, 8))).unwrap(); + let f = crate::probe(&file).unwrap(); + let pool = crate::MediaPool::new(); + let need = AuxNeed::All; + pool.aux_for(ItemId(1), &f, Tick::ZERO, &need).unwrap(); + let started = std::time::Instant::now(); + let before = crate::stamp_checks_under(&dir); + for _ in 0..8 { + pool.aux_for(ItemId(1), &f, Tick::ZERO, &need).unwrap(); + pool.aux(ItemId(1), &f, Tick::ZERO).unwrap(); + } + let checks = crate::stamp_checks_under(&dir) - before; + // (a slow machine may cross the stamp's lifetime once meanwhile) + let lifetimes = (started.elapsed().as_secs_f64() / crate::exr_read::STAMP_TTL.as_secs_f64()).ceil() as usize; + assert!(checks <= lifetimes.max(1), "{checks} identity checks for 16 requests"); + assert!(crate::exr_read::stamp_kept(&file)); + // Reload Footage: the identity read goes with the index; the file is read again at once. + pool.forget(&f.path); + assert!(!crate::exr_read::stamp_kept(&file), "forgotten"); + std::fs::write(&file, crypto_edges((8, 9))).unwrap(); + let again = pool.aux_for(ItemId(1), &f, Tick::ZERO, &need).unwrap(); + assert_eq!(again.height, 9, "the new file"); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/crates/media/src/exr_read.rs b/crates/media/src/exr_read.rs index d37da11ef..dcad343f8 100644 --- a/crates/media/src/exr_read.rs +++ b/crates/media/src/exr_read.rs @@ -49,15 +49,97 @@ pub(crate) struct Opened { pub(crate) stamp: FileStamp, } +/// How long a file's identity, once read, stands for it ([`recent_stamp`], #3334): every request +/// for a file's channels checked it, one or two `fs::metadata` calls each (1–7 ms on a network +/// share), several per frame. A file rewritten in place is seen at most this late; Reload Footage +/// forgets it at once ([`forget_stamp`]). +pub(crate) const STAMP_TTL: std::time::Duration = std::time::Duration::from_secs(1); + +/// At most this many files' identities are remembered (the oldest checks go first). +#[cfg(not(target_arch = "wasm32"))] +const STAMPS_KEPT: usize = 4096; + +#[cfg(not(target_arch = "wasm32"))] +type Stamps = std::collections::HashMap; + +#[cfg(not(target_arch = "wasm32"))] +fn stamps() -> std::sync::MutexGuard<'static, Stamps> { + static STAMPS: std::sync::OnceLock> = std::sync::OnceLock::new(); + STAMPS.get_or_init(Default::default).lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} + +/// [`recent_stamp`] in the browser: no clock there (and no files, only registered bytes). +#[cfg(target_arch = "wasm32")] +pub(crate) fn recent_stamp(path: &std::path::Path) -> std::io::Result { + std::fs::metadata(path).map(|m| FileStamp::of_metadata(&m)) +} + +/// The identity of the file at `path` (size and modification time): as read within the last +/// [`STAMP_TTL`], else read now. A file that can't be read is asked again every time. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn recent_stamp(path: &std::path::Path) -> std::io::Result { + let now = std::time::Instant::now(); + if let Some((stamp, at)) = stamps().get(path) + && now.saturating_duration_since(*at) < STAMP_TTL + { + return Ok(*stamp); + } + #[cfg(test)] + crate::count_stamp(path); + let stamp = std::fs::metadata(path).map(|m| FileStamp::of_metadata(&m))?; + note_stamp(path, stamp, now); + Ok(stamp) +} + +/// Remember `stamp` as the identity of the file at `path` read at `at` (a file just opened). +#[cfg(not(target_arch = "wasm32"))] +fn note_stamp(path: &std::path::Path, stamp: FileStamp, at: std::time::Instant) { + let mut s = stamps(); + if s.len() >= STAMPS_KEPT && !s.contains_key(path) { + s.retain(|_, (_, t)| at.saturating_duration_since(*t) < STAMP_TTL); + if s.len() >= STAMPS_KEPT { + s.clear(); + } + } + s.insert(path.to_path_buf(), (stamp, at)); +} + +/// Forget the identity read of the file at `path` (Reload Footage: the next request reads it). +pub(crate) fn forget_stamp(path: &std::path::Path) { + #[cfg(not(target_arch = "wasm32"))] + stamps().remove(path); + #[cfg(target_arch = "wasm32")] + let _ = path; +} + +/// Whether the identity of the file at `path` is remembered (tests). +#[cfg(test)] +pub(crate) fn stamp_kept(path: &std::path::Path) -> bool { + stamps().contains_key(path) +} + impl ExrSource { - /// Its identity now (a file's size and modification time), without opening it. + /// Its identity (a file's size and modification time), without opening it: a file's as read + /// within the last [`STAMP_TTL`] ([`recent_stamp`]); a localized file's as it was when its + /// verified copy was chosen (moments ago: [`recent_stamp`] too). pub(crate) fn stamp(&self) -> std::io::Result { match self { ExrSource::Bytes(_) => Ok(FileStamp::Virtual), - ExrSource::File(path) | ExrSource::Localized { original: path, .. } => std::fs::metadata(path).map(|m| FileStamp::of_metadata(&m)), + ExrSource::File(path) => recent_stamp(path), + ExrSource::Localized { stamp, .. } => Ok(*stamp), } } + /// The file was just opened and found to be `stamp`: later checks within [`STAMP_TTL`] use it. + pub(crate) fn opened_as(&self, stamp: FileStamp) { + #[cfg(not(target_arch = "wasm32"))] + if let ExrSource::File(path) = self { + note_stamp(path, stamp, std::time::Instant::now()); + } + #[cfg(target_arch = "wasm32")] + let _ = stamp; + } + /// Open it: a file only when it is a regular file (a project can name `/dev/zero` or a FIFO, /// #2134), with its identity from the open file. pub(crate) fn open(&self) -> std::io::Result { diff --git a/crates/media/src/lib.rs b/crates/media/src/lib.rs index d54dbd0d7..ecc5f8dcd 100644 --- a/crates/media/src/lib.rs +++ b/crates/media/src/lib.rs @@ -126,6 +126,21 @@ pub(crate) fn opens_under(dir: &std::path::Path) -> usize { OPENS.lock().unwrap_or_else(std::sync::PoisonError::into_inner).iter().filter(|p| p.starts_with(dir)).count() } +#[cfg(test)] +static STAMP_CHECKS: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + +/// Count a check of a file's identity on disk (tests, #3334). +#[cfg(test)] +pub(crate) fn count_stamp(path: &std::path::Path) { + STAMP_CHECKS.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(path.to_path_buf()); +} + +/// How many times the identity of files under `dir` was checked on disk so far (tests). +#[cfg(test)] +pub(crate) fn stamp_checks_under(dir: &std::path::Path) -> usize { + STAMP_CHECKS.lock().unwrap_or_else(std::sync::PoisonError::into_inner).iter().filter(|p| p.starts_with(dir)).count() +} + pub(crate) fn ext_of(path: &std::path::Path) -> String { path.extension().and_then(std::ffi::OsStr::to_str).unwrap_or("").to_ascii_lowercase() } diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index 6da73ec80..534ee8232 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -997,11 +997,11 @@ impl Inner { // copy once that is complete and the file unchanged (#3271). if crate::localize::is_network(&file) && crate::localize::status().is_some() { crate::localize::request(&file); - if let Ok(meta) = std::fs::metadata(&file) { - let stamp = FileStamp::of_metadata(&meta); - if let Some(copy) = crate::localize::local_copy(&file, &stamp) { - return ExrSource::Localized { original: file, copy, stamp }; - } + // (the identity as read within the last moment: one check per file and frame, #3334) + if let Ok(stamp) = crate::exr_read::recent_stamp(&file) + && let Some(copy) = crate::localize::local_copy(&file, &stamp) + { + return ExrSource::Localized { original: file, copy, stamp }; } } ExrSource::File(file) diff --git a/crates/ui-egui/tests/it/ui_crypto_pick.rs b/crates/ui-egui/tests/it/ui_crypto_pick.rs index 0d1767c7e..eaa0680fc 100644 --- a/crates/ui-egui/tests/it/ui_crypto_pick.rs +++ b/crates/ui-egui/tests/it/ui_crypto_pick.rs @@ -196,17 +196,12 @@ fn lit(h: &mut Harness<'_, EffectcraftApp>, name: &str, p: [f64; 2], scale: f64) img.get((p[0] * scale).floor() as i64, (p[1] * scale).floor() as i64)[0] } -/// Whether ID `got` (a cell name) has the largest coverage of the shown pixel at comp point `p` -/// (a downsampled or scaled layer blends the cells around it). +/// Whether ID `got` (a cell name) lights the shown pixel at comp point `p`. Below one buffer pixel +/// per layer pixel (a reduced resolution, a smaller proxy) the effect shows the matte +/// box-filtered over the layer pixels a shown pixel covers (#3334), so at a cell edge the ID the +/// hover reads under the cursor lights it partly, and need not be the one covering most of it. fn largest(h: &mut Harness<'_, EffectcraftApp>, got: &str, p: [f64; 2], scale: f64) -> bool { - let v = lit(h, got, p, scale); - if v >= 0.5 { - return true; - } - let (cx, cy) = ((p[0] / 37.0).floor() as i64, (p[1] / 29.0).floor() as i64); - let others: Vec = - (-2..=2).flat_map(|dx| (-2..=2).map(move |dy| (cx + dx, cy + dy))).filter(|c| c.0 >= 0 && c.1 >= 0).map(|c| format!("c{}_{}", c.0, c.1)).collect(); - v > 0.0 && others.iter().filter(|o| *o != got).all(|o| lit(h, o, p, scale) <= v + 1e-4) + lit(h, got, p, scale) > 0.0 } #[test] @@ -652,7 +647,10 @@ fn picks_on_the_gpu_viewer() { let sp = comp_to_screen(&h.ctx, [p[0] as f32, p[1] as f32]).unwrap(); let got = hover(&mut h, sp).trim_end_matches(" (selected)").to_string(); let q = to_comp(&h, sp); - assert!(got.starts_with('c') && lit(&mut h, &got, q, scale) >= 0.5, "{res:?}: at {q:?} hover {got:?} is not lit there"); + // (below Full the matte is box-filtered: an edge pixel is lit partly, #3334) + let lit_at = lit(&mut h, &got, q, scale); + let ok = if res == Resolution::Full { lit_at >= 0.5 } else { lit_at > 0.0 }; + assert!(got.starts_with('c') && ok, "{res:?}: at {q:?} hover {got:?} is not lit there ({lit_at})"); checked += 1; } }