mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-21 23:10:43 +01:00
Frame-sliced recomposites filter a run of tiles once (#475)
A stack with a neighbourhood filter (blur, high pass, bevel, glow...) made a recomposite spread over frames many times slower than the same recomposite in one call: every chunk filtered its own surroundings again, and on a machine with many threads the filter's per-texel parallel steps cost more to hand out than to run on a region of a few tiles. - Neighbourhood filters split their per-texel work into tasks of at least 16384 texels. Each texel is computed on its own, so results do not change. - Session::update_for tells the compositor which dirty tiles follow the chunk in its block (up to 64). The compositor filters the chunk and those tiles at once and keeps the fields for the composites that follow, compared by everything the stack reads, and drops them at the end of the pass, after an edit, and under a live stroke. Results are identical to the one-call composite: tests cover cold, warm and partly evicted hot-layer caches, an edit mid-pass and a live stroke. perf::bench_sliced_filters_4k times both paths.
This commit is contained in:
1 parent
386630ee85
commit
1a742d3fa4
6 files changed
+324
-26
No files matched your search
@@ -13,7 +13,7 @@ use texturecraft_core::project::MAX_DEPTH;
|
||||
use texturecraft_core::{BakedMap, Channel, Layer, LayerId, LayerKind, Project, TextureSet};
|
||||
use texturecraft_texel::TileImage;
|
||||
|
||||
use crate::eval::{Inputs, PaintTarget};
|
||||
use crate::eval::{Fields, Inputs, PaintTarget};
|
||||
use crate::store::{BakedMaps, ImageStore, PixelStore};
|
||||
|
||||
/// Default byte budget of a [`BelowCache`], per nesting level it holds (see
|
||||
@@ -40,11 +40,47 @@ pub struct BelowCache {
|
||||
/// Tiles of the whole pass a caller splits into several composites (0: each composite is a
|
||||
/// pass). A pass larger than the cache records only what fits.
|
||||
pub pass: usize,
|
||||
/// Tiles the caller composites next in this pass, after those of the coming composite (empty: none, or a pass that is one
|
||||
/// composite). A stack with neighbourhood filters then filters the coming tiles and these at once and keeps the result for
|
||||
/// the composites that follow, instead of filtering each small chunk's surroundings again.
|
||||
pub ahead: Vec<(u32, u32)>,
|
||||
pub(crate) held: Option<HeldFields>,
|
||||
}
|
||||
|
||||
/// Neighbourhood-filter fields kept between the composites of one pass.
|
||||
pub(crate) struct HeldFields {
|
||||
/// Everything the fields read.
|
||||
pub key: Arc<StackKey>,
|
||||
/// The working size they were built at.
|
||||
pub size: (u32, u32),
|
||||
/// The tiles every layer's fields hold.
|
||||
pub stack_tiles: BTreeSet<(u32, u32)>,
|
||||
/// The tiles the hot layer's fields hold (the same tiles when the stack was not split).
|
||||
pub hot_tiles: BTreeSet<(u32, u32)>,
|
||||
pub fields: Arc<Fields>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for HeldFields {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("HeldFields").field("stack_tiles", &self.stack_tiles.len()).field("hot_tiles", &self.hot_tiles.len()).finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for BelowCache {
|
||||
fn default() -> Self {
|
||||
BelowCache { hot: None, key: None, tiles: HashMap::new(), order: VecDeque::new(), clock: 0, bytes: 0, budget: BELOW_BUDGET, levels: 1, pass: 0 }
|
||||
BelowCache {
|
||||
hot: None,
|
||||
key: None,
|
||||
tiles: HashMap::new(),
|
||||
order: VecDeque::new(),
|
||||
clock: 0,
|
||||
bytes: 0,
|
||||
budget: BELOW_BUDGET,
|
||||
levels: 1,
|
||||
pass: 0,
|
||||
ahead: Vec::new(),
|
||||
held: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,12 +220,19 @@ impl BelowCache {
|
||||
self.order.clear();
|
||||
self.bytes = 0;
|
||||
self.key = None;
|
||||
// Held fields tell the hot layer's tiles from the others'
|
||||
self.held = None;
|
||||
}
|
||||
|
||||
pub fn bytes(&self) -> usize {
|
||||
self.bytes
|
||||
}
|
||||
|
||||
/// Whether neighbourhood-filter fields are kept for the composites that follow (see [`BelowCache::ahead`]).
|
||||
pub fn holds_fields(&self) -> bool {
|
||||
self.held.is_some()
|
||||
}
|
||||
|
||||
/// The byte limit: the budget for the top level and as much again for a hot layer nested in
|
||||
/// folders (each tile then holds the stack at two or more levels).
|
||||
pub fn limit(&self) -> usize {
|
||||
|
||||
+91
-22
@@ -18,7 +18,7 @@ use texturecraft_geom::glam::{Vec2, Vec3};
|
||||
use texturecraft_geom::{Mesh, NO_TRI, UvGBuffer};
|
||||
use texturecraft_texel::{DilationMap, TILE, TILE_AREA, TileImage, from_u16, noise, to_u16, to_u16_signed};
|
||||
|
||||
use crate::below::{Band, BelowCache, Level};
|
||||
use crate::below::{Band, BelowCache, HeldFields, Level, StackKey};
|
||||
use crate::curvature_sizes::CurvatureSizes;
|
||||
use crate::pattern::{Coords, decal_uv, image_samples, procedural, world_noise};
|
||||
use crate::store::{BakedMaps, ImageStore, PixelStore, decode};
|
||||
@@ -434,15 +434,27 @@ fn gather(op: Op, orig: &[f32], cover: &[f32], comps: usize, (x0, y0, w, h): (u3
|
||||
/// the region it is computed in, so tiles composited in any grouping agree exactly.
|
||||
const FIX: f64 = (1u64 << 24) as f64;
|
||||
|
||||
/// Texels one parallel task of a neighbourhood filter handles at least. Compositing in frame slices filters regions of a few tiles: split per texel,
|
||||
/// handing that little work to every thread costs far more than the work itself. Each texel is computed on its own, so the split does not change results.
|
||||
const MIN_PARALLEL_TEXELS: usize = 1 << 14;
|
||||
|
||||
/// How many lines of `length` texels make a parallel task of at least [`MIN_PARALLEL_TEXELS`].
|
||||
fn lines_per_task(length: usize) -> usize {
|
||||
(MIN_PARALLEL_TEXELS / length.max(1)).max(1)
|
||||
}
|
||||
|
||||
/// Three running-sum box blurs (≈ Gaussian) of a `width × height` image along its rows, then its columns, each in parallel.
|
||||
pub(crate) fn blur(data: &mut [f32], width: usize, height: usize, radius: usize) {
|
||||
let to_fixed = |value: &f32| if value.is_finite() { (f64::from(value.clamp(-1e6, 1e6)) * FIX).round() as i64 } else { 0 };
|
||||
let mut fixed: Vec<i64> = data.par_iter().map(to_fixed).collect();
|
||||
let mut fixed: Vec<i64> = data.par_iter().with_min_len(MIN_PARALLEL_TEXELS).map(to_fixed).collect();
|
||||
blur_rows(&mut fixed, width, radius);
|
||||
|
||||
let mut columns = transpose(&fixed, width, height);
|
||||
blur_rows(&mut columns, height, radius);
|
||||
data.par_iter_mut().zip(transpose(&columns, height, width)).for_each(|(target, blurred)| *target = (blurred as f64 / FIX) as f32);
|
||||
data.par_iter_mut()
|
||||
.with_min_len(MIN_PARALLEL_TEXELS)
|
||||
.zip(transpose(&columns, height, width))
|
||||
.for_each(|(target, blurred)| *target = (blurred as f64 / FIX) as f32);
|
||||
}
|
||||
|
||||
/// The box blurs `blur` runs along each axis
|
||||
@@ -453,7 +465,7 @@ fn blur_rows(data: &mut [i64], w: usize, r: usize) {
|
||||
if w == 0 {
|
||||
return;
|
||||
}
|
||||
data.par_chunks_mut(w).for_each(|row| {
|
||||
data.par_chunks_mut(w).with_min_len(lines_per_task(w)).for_each(|row| {
|
||||
let mut tmp = vec![0i64; row.len()];
|
||||
for _ in 0..BLUR_BOX_PASSES {
|
||||
box_line(row, &mut tmp, r);
|
||||
@@ -481,7 +493,7 @@ fn box_line(src: &[i64], dst: &mut [i64], r: usize) {
|
||||
fn transpose(src: &[i64], w: usize, h: usize) -> Vec<i64> {
|
||||
let mut out = vec![0i64; src.len()];
|
||||
if h > 0 {
|
||||
out.par_chunks_mut(h).enumerate().for_each(|(x, col)| {
|
||||
out.par_chunks_mut(h).with_min_len(lines_per_task(h)).enumerate().for_each(|(x, col)| {
|
||||
for (y, d) in col.iter_mut().enumerate() {
|
||||
*d = src.get(y * w + x).copied().unwrap_or(0);
|
||||
}
|
||||
@@ -698,9 +710,10 @@ fn filter_region(
|
||||
blur(&mut weights, width, height, blur_radius);
|
||||
let mut data = orig.clone();
|
||||
for component in 0..comps {
|
||||
let mut sums: Vec<f32> = orig.par_chunks(comps).map(|pixel| pixel.get(component).copied().unwrap_or(0.0)).collect();
|
||||
let mut sums: Vec<f32> =
|
||||
orig.par_chunks(comps).with_min_len(MIN_PARALLEL_TEXELS).map(|pixel| pixel.get(component).copied().unwrap_or(0.0)).collect();
|
||||
blur(&mut sums, width, height, blur_radius);
|
||||
data.par_chunks_mut(comps).zip(sums.par_iter()).zip(weights.par_iter()).for_each(|((pixel, sum), weight)| {
|
||||
data.par_chunks_mut(comps).with_min_len(MIN_PARALLEL_TEXELS).zip(sums.par_iter()).zip(weights.par_iter()).for_each(|((pixel, sum), weight)| {
|
||||
// Content is premultiplied, so the mid value is too (coverage itself is kept)
|
||||
let cov = if comps == 4 { pixel.get(3).copied().unwrap_or(0.0) } else { 1.0 };
|
||||
if *weight > 1e-6
|
||||
@@ -749,7 +762,7 @@ fn filter_region(
|
||||
|
||||
/// Clamp filtered masks to 0..1, and turn filtered content back into straight values.
|
||||
fn unpremultiply(data: &mut [f32], comps: usize) {
|
||||
data.par_chunks_mut(comps).for_each(|px| match px {
|
||||
data.par_chunks_mut(comps).with_min_len(MIN_PARALLEL_TEXELS).for_each(|px| match px {
|
||||
[v] => *v = v.clamp(0.0, 1.0),
|
||||
[r, g, b, cov] => {
|
||||
*cov = cov.clamp(0.0, 1.0);
|
||||
@@ -1661,7 +1674,7 @@ pub fn composite_tiles(inp: &Inputs, out: &mut Composite, tiles: &[(u32, u32)])
|
||||
}
|
||||
|
||||
/// Tiles per side of the blocks a composite works through (bounds neighbourhood-filter memory).
|
||||
const BLOCK: u32 = 16;
|
||||
pub const BLOCK: u32 = 16;
|
||||
|
||||
/// [`composite_tiles`], serving everything but the hot layer from `below` where it can (and
|
||||
/// filling it). Results are identical to [`composite_tiles`].
|
||||
@@ -1681,14 +1694,50 @@ pub fn composite_tiles_cached(inp: &Inputs, out: &mut Composite, tiles: &[(u32,
|
||||
for t in tiles {
|
||||
blocks.entry((t.1 / BLOCK, t.0 / BLOCK)).or_default().push(*t);
|
||||
}
|
||||
for block in blocks.values() {
|
||||
let channels = set.channel_list();
|
||||
let hot = split.as_deref().and_then(<[Level]>::last);
|
||||
// Fields are only kept without a live stroke (its preview is not part of what a stack key compares)
|
||||
let holds = below.is_some() && inp.preview.is_none() && has_neighbourhood(inp, &set.stack);
|
||||
let key = if holds { StackKey::new(inp.project, inp.pixels, inp.baked, inp.images, inp.ts, &set.stack).map(Arc::new) } else { None };
|
||||
let size = (inp.gbuf.width, inp.gbuf.height);
|
||||
for (at, block) in &blocks {
|
||||
let lacks = |tile: &(u32, u32)| below.as_deref().zip(split.as_deref()).is_none_or(|(cache, _)| cache.tile(*tile).is_none());
|
||||
let fresh: Vec<(u32, u32)> = block.iter().copied().filter(lacks).collect();
|
||||
let held = below.as_deref().and_then(|cache| cache.held.as_ref()).filter(|held| {
|
||||
key.as_ref().is_some_and(|key| **key == *held.key)
|
||||
&& held.size == size
|
||||
&& fresh.iter().all(|tile| held.stack_tiles.contains(tile))
|
||||
&& block.iter().all(|tile| held.hot_tiles.contains(tile))
|
||||
});
|
||||
let ahead: Vec<(u32, u32)> = match (&key, below.as_deref()) {
|
||||
(Some(_), Some(cache)) if held.is_none() => {
|
||||
cache.ahead.iter().copied().filter(|tile| (tile.1 / BLOCK, tile.0 / BLOCK) == *at && !block.contains(tile)).collect()
|
||||
}
|
||||
_ => Vec::new(),
|
||||
};
|
||||
let fields: Arc<Fields> = match (held, &key) {
|
||||
(Some(held), _) => held.fields.clone(),
|
||||
(None, Some(key)) if !ahead.is_empty() => {
|
||||
// These tiles and the ones that follow them, filtered at once and kept
|
||||
let wide: Vec<(u32, u32)> = block.iter().chain(&ahead).copied().collect();
|
||||
let wide_fresh: Vec<(u32, u32)> = wide.iter().copied().filter(lacks).collect();
|
||||
let fields = Arc::new(block_fields(inp, &channels, &set.stack, hot, &wide_fresh, &wide));
|
||||
let hot_tiles: std::collections::BTreeSet<(u32, u32)> = wide.iter().copied().collect();
|
||||
let stack_tiles = if hot.is_some() && wide_fresh.len() < wide.len() { wide_fresh.into_iter().collect() } else { hot_tiles.clone() };
|
||||
if let Some(cache) = below.as_deref_mut() {
|
||||
cache.held = Some(HeldFields { key: key.clone(), size, stack_tiles, hot_tiles, fields: fields.clone() });
|
||||
}
|
||||
fields
|
||||
}
|
||||
_ => Arc::new(block_fields(inp, &channels, &set.stack, hot, &fresh, block)),
|
||||
};
|
||||
let quota = match below.as_deref() {
|
||||
Some(_) if fits => usize::MAX,
|
||||
Some(b) => b.limit().saturating_sub(b.bytes()) / tile_bytes,
|
||||
None => 0,
|
||||
};
|
||||
let cached = below.as_deref().zip(split.as_deref());
|
||||
let records = composite_block(inp, &set.stack, out, block, cached, quota);
|
||||
let records = composite_block(inp, &set.stack, out, block, cached, quota, &fields);
|
||||
if let Some(b) = below.as_deref_mut() {
|
||||
for t in block {
|
||||
b.touch(*t);
|
||||
@@ -1698,6 +1747,34 @@ pub fn composite_tiles_cached(inp: &Inputs, out: &mut Composite, tiles: &[(u32,
|
||||
}
|
||||
}
|
||||
}
|
||||
// Nothing follows in this pass: its fields are not kept past it
|
||||
if let Some(cache) = below.filter(|cache| cache.ahead.is_empty()) {
|
||||
cache.held = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a layer drawn by `stack` has a neighbourhood filter in its mask or content effects.
|
||||
fn has_neighbourhood(inp: &Inputs, stack: &[LayerId]) -> bool {
|
||||
let working_width = inp.gbuf.width;
|
||||
let mut order = Vec::new();
|
||||
draw_order(inp.project, stack, 0, &mut order);
|
||||
order.into_iter().any(|id| {
|
||||
let masked = layer_mask(inp.project, id).is_some_and(|mask| mask.enabled && neighbourhood(&mask.effects, working_width).is_some());
|
||||
masked || inp.project.content_layer(id).is_some_and(|layer| !layer.is_group() && neighbourhood(&layer.content_effects, working_width).is_some())
|
||||
})
|
||||
}
|
||||
|
||||
/// The neighbourhood fields compositing `tiles` reads: those of every layer of `stack` over the tiles the cache lacks (`fresh`),
|
||||
/// and off the path to the `hot` layer only there; the hot layer's over all of `tiles`.
|
||||
fn block_fields(inp: &Inputs, channels: &[Channel], stack: &[LayerId], hot: Option<&Level>, fresh: &[(u32, u32)], tiles: &[(u32, u32)]) -> Fields {
|
||||
match hot {
|
||||
Some(hot) if fresh.len() < tiles.len() => {
|
||||
let mut fields = fields_of(inp, channels, stack, fresh);
|
||||
fields.extend(fields_of(inp, channels, &[hot.at], tiles));
|
||||
fields
|
||||
}
|
||||
_ => fields_of(inp, channels, stack, tiles),
|
||||
}
|
||||
}
|
||||
|
||||
/// Rows of a tile composited as one parallel task.
|
||||
@@ -1719,6 +1796,7 @@ fn composite_block(
|
||||
tiles: &[(u32, u32)],
|
||||
below: Option<(&BelowCache, &[Level])>,
|
||||
quota: usize,
|
||||
fields: &Fields,
|
||||
) -> Vec<((u32, u32), Vec<Band>)> {
|
||||
let channels: Vec<Channel> = inp.project.texture_set(inp.ts).map(|s| s.channel_list()).unwrap_or_default();
|
||||
let (w, h) = (out.resolution, out.height);
|
||||
@@ -1726,15 +1804,6 @@ fn composite_block(
|
||||
let cached = |t: (u32, u32)| below.and_then(|(b, _)| b.tile(t));
|
||||
let fresh: Vec<(u32, u32)> = tiles.iter().copied().filter(|t| cached(*t).is_none()).collect();
|
||||
let recorded: std::collections::BTreeSet<(u32, u32)> = if split.is_some() { fresh.iter().copied().take(quota).collect() } else { Default::default() };
|
||||
// Off the path, layers are only evaluated on tiles the cache lacks.
|
||||
let fields = match split.and_then(<[Level]>::last) {
|
||||
Some(hot) if fresh.len() < tiles.len() => {
|
||||
let mut fields = fields_of(inp, &channels, stack, &fresh);
|
||||
fields.extend(fields_of(inp, &channels, &[hot.at], tiles));
|
||||
fields
|
||||
}
|
||||
_ => fields_of(inp, &channels, stack, tiles),
|
||||
};
|
||||
let lanes: Vec<Lane> = channels.iter().map(|c| Lane { ch: c.index(), n: c.components(), signed: c.kind() == ChannelKind::Signed }).collect();
|
||||
let stride: usize = lanes.iter().map(|l| l.n).sum();
|
||||
let nlev = split.map_or(0, <[Level]>::len);
|
||||
@@ -1747,8 +1816,8 @@ fn composite_block(
|
||||
.map(|&((tx, ty), band)| {
|
||||
let warm = cached((tx, ty)).and_then(|b| b.get(band as usize));
|
||||
let record = warm.is_none() && recorded.contains(&(tx, ty));
|
||||
let levels = split.map(|l| plan_levels(inp, l, &channels, tx, ty, &fields, warm.is_some()));
|
||||
let nodes = if levels.is_none() { plan(inp, stack, &channels, tx, ty, 0, Some(&fields)) } else { Vec::new() };
|
||||
let levels = split.map(|l| plan_levels(inp, l, &channels, tx, ty, fields, warm.is_some()));
|
||||
let nodes = if levels.is_none() { plan(inp, stack, &channels, tx, ty, 0, Some(fields)) } else { Vec::new() };
|
||||
let rows = (BAND * TILE) as usize;
|
||||
let mut accs = vec![defaults; nlev];
|
||||
let mut masks = vec![0.0f32; nlev];
|
||||
|
||||
@@ -20,8 +20,8 @@ pub mod thumb;
|
||||
pub use below::{BelowCache, StackKey};
|
||||
pub use display::{DisplayKind, tile_bytes};
|
||||
pub use eval::{
|
||||
Composite, Inputs, PaintTarget, Stencil, StrokePreview, all_tiles, composite_tiles, composite_tiles_cached, covered_tiles, effect_histogram, flatten_layer,
|
||||
mask_at, mask_image, pad_tiles,
|
||||
BLOCK, Composite, Inputs, PaintTarget, Stencil, StrokePreview, all_tiles, composite_tiles, composite_tiles_cached, covered_tiles, effect_histogram,
|
||||
flatten_layer, mask_at, mask_image, pad_tiles,
|
||||
};
|
||||
pub use store::{BakedMaps, ImageRes, ImageStore, PixelStore};
|
||||
pub use thumb::{THUMB_TEXELS, ThumbImage, ThumbTarget};
|
||||
@@ -573,3 +573,136 @@ fn gathering_filters_composite_identically_in_any_grouping_and_cached() {
|
||||
assert!(!check_cached(&f, 0, hot).is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
/// Composite every tile in chunks of `chunk`, telling the cache the `ahead` tiles that follow each chunk, as a pass spread over frames does.
|
||||
fn run_sliced(f: &Fixture, cache: &mut BelowCache, chunk: usize, ahead: usize) -> Composite {
|
||||
let all = all_tiles(RES, RES);
|
||||
let (images, baked) = (Default::default(), Default::default());
|
||||
let mut out = Composite::new(&f.project.texture_sets[0].channel_list(), RES);
|
||||
for (index, tiles) in all.chunks(chunk).enumerate() {
|
||||
let done = (index * chunk + tiles.len()).min(all.len());
|
||||
cache.ahead = all.iter().skip(done).take(ahead).copied().collect();
|
||||
composite_tiles_cached(&inputs(f, 0, &images, &baked), &mut out, tiles, Some(&mut *cache));
|
||||
assert_eq!(cache.holds_fields(), !cache.ahead.is_empty(), "fields are kept exactly while tiles follow (chunk {chunk}, ahead {ahead})");
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filter_fields_kept_across_the_chunks_of_a_pass_composite_exactly() {
|
||||
let mut f = fixture();
|
||||
let top = heavy_stack(&mut f);
|
||||
let blurred_fill = f.project.texture_sets[0].stack[1];
|
||||
let want = reference(&f, 0);
|
||||
for hot in [None, Some(top), Some(blurred_fill)] {
|
||||
// Short and long runs of kept tiles: chunks inside a run reuse it, chunks past its end filter again
|
||||
for (chunk, ahead) in [(1, 1), (1, 5), (3, 4), (5, 16), (2, 0)] {
|
||||
let mut cache = BelowCache::default();
|
||||
cache.set_hot(hot);
|
||||
assert!(run_sliced(&f, &mut cache, chunk, ahead) == want, "a cold sliced pass differs (hot {hot:?}, chunk {chunk}, ahead {ahead})");
|
||||
// The hot-layer cache is warm now: only the hot layer's fields are needed
|
||||
assert!(run_sliced(&f, &mut cache, chunk, ahead) == want, "a warm sliced pass differs (hot {hot:?}, chunk {chunk}, ahead {ahead})");
|
||||
}
|
||||
}
|
||||
|
||||
// A cache too small for the set drops tiles while a run of kept fields is in use: those tiles need every layer's fields again
|
||||
let mut full = BelowCache::default();
|
||||
full.set_hot(Some(top));
|
||||
run(&f, 0, &all_tiles(RES, RES), Some(&mut full));
|
||||
let tile_bytes = full.tile_bytes().unwrap();
|
||||
for tiles_held in [2, 5, 9] {
|
||||
for (chunk, ahead) in [(1, 5), (3, 4), (2, 16)] {
|
||||
let mut cache = BelowCache::default();
|
||||
cache.budget = tiles_held * tile_bytes;
|
||||
cache.set_hot(Some(top));
|
||||
for pass in 0..3 {
|
||||
assert!(run_sliced(&f, &mut cache, chunk, ahead) == want, "pass {pass} differs with {tiles_held} cached tiles (chunk {chunk}, ahead {ahead})");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kept_filter_fields_are_not_used_after_an_edit_or_under_a_live_stroke() {
|
||||
let mut f = fixture();
|
||||
let top = heavy_stack(&mut f);
|
||||
let all = all_tiles(RES, RES);
|
||||
let (first, rest) = all.split_at(3);
|
||||
let mut cache = BelowCache::default();
|
||||
cache.set_hot(Some(top));
|
||||
|
||||
// Fields are kept for the tiles after the first chunk…
|
||||
cache.ahead = rest.to_vec();
|
||||
let mut out = run(&f, 0, first, Some(&mut cache));
|
||||
assert!(cache.holds_fields());
|
||||
|
||||
// …then the blurred mask of the hot layer changes before they are composited
|
||||
let mask = f.project.layer_mut(top).unwrap().mask.as_mut().unwrap();
|
||||
mask.effects[1].kind = EffectKind::Filter(Filter::Blur { radius: 6.0 });
|
||||
let (images, baked) = (Default::default(), Default::default());
|
||||
cache.ahead = Vec::new();
|
||||
composite_tiles_cached(&inputs(&f, 0, &images, &baked), &mut out, &all, Some(&mut cache));
|
||||
assert!(out == reference(&f, 0), "fields kept from before an edit were used after it");
|
||||
assert!(!cache.holds_fields());
|
||||
|
||||
// A live stroke is not part of what kept fields are compared by: it composites without them
|
||||
let preview = stroke(top);
|
||||
cache.ahead = rest.to_vec();
|
||||
let mut live = run_with(&f, 0, first, Some(&mut cache), Some(&preview));
|
||||
assert!(!cache.holds_fields());
|
||||
let mut inp = inputs(&f, 0, &images, &baked);
|
||||
inp.preview = Some(&preview);
|
||||
composite_tiles_cached(&inp, &mut live, rest, Some(&mut cache));
|
||||
assert!(live == run_with(&f, 0, &all, None, Some(&preview)), "a live stroke over a sliced pass differs");
|
||||
}
|
||||
|
||||
/// Kept fields hold a layer off the hot path only over the tiles the cache lacked when they were built. A tile the cache drops
|
||||
/// while they are in use needs that layer's fields again.
|
||||
#[test]
|
||||
fn tiles_the_cache_drops_during_a_kept_run_are_filtered_again() {
|
||||
const SIZE: u32 = 512;
|
||||
let mesh = primitives::sphere(1.0, 32, 16);
|
||||
let gbuf = UvGBuffer::new(&mesh, 0, SIZE, SIZE);
|
||||
let mut project = Project::new("t", "builtin:sphere");
|
||||
project.texture_sets.push(TextureSet::new("A", 0, SIZE));
|
||||
let mut f = Fixture { project, mesh, gbuf, pixels: PixelStore::default() };
|
||||
add(&mut f, 0, fill(&[(Channel::BaseColor, procedural(ProceduralKind::Clouds, 0.1, 0.9))]), None);
|
||||
// A pattern with detail on every tile, so a tile composited without its blur shows
|
||||
let mut blurred = Mask::new(MaskBackground::Black);
|
||||
blurred.effects.push(effect(&mut f, EffectKind::Fill { source: procedural(ProceduralKind::Checker, 0.0, 1.0), projection: Projection::default() }));
|
||||
blurred.effects.push(effect(&mut f, EffectKind::Filter(Filter::Blur { radius: 40.0 })));
|
||||
add(&mut f, 0, fill(&[(Channel::Roughness, procedural(ProceduralKind::Grunge, 0.0, 1.0))]), Some(blurred));
|
||||
// A paint layer on top leaves the blurred layer in sight
|
||||
let hot = add(&mut f, 0, LayerKind::Paint, None);
|
||||
paint_rect_at(&mut f, hot, SIZE, (10, 10), (40, 40), 0.2);
|
||||
|
||||
let mut all = all_tiles(SIZE, SIZE);
|
||||
all.sort_by_key(|&(x, y)| (y, x));
|
||||
let channels = f.project.texture_sets[0].channel_list();
|
||||
let (images, baked) = (Default::default(), Default::default());
|
||||
let mut want = Composite::new(&channels, SIZE);
|
||||
composite_tiles(&inputs(&f, 0, &images, &baked), &mut want, &all);
|
||||
|
||||
// The cache holds the two bottom rows of tiles, and no more than that
|
||||
let rows = (SIZE / TILE) as usize;
|
||||
let (above, bottom) = all.split_at(all.len() - 2 * rows);
|
||||
let mut cache = BelowCache::default();
|
||||
cache.set_hot(Some(hot));
|
||||
let mut out = Composite::new(&channels, SIZE);
|
||||
composite_tiles_cached(&inputs(&f, 0, &images, &baked), &mut out, bottom, Some(&mut cache));
|
||||
assert_eq!(cache.len(), bottom.len());
|
||||
cache.budget = cache.bytes();
|
||||
|
||||
// One tile at a time with every later tile ahead: the first composite keeps fields for the whole set, the blurred layer's
|
||||
// only over the rows above. Recording those rows then drops the bottom ones from the cache before their turn
|
||||
let mut out = Composite::new(&channels, SIZE);
|
||||
for (index, tile) in all.iter().enumerate() {
|
||||
cache.ahead = all.iter().skip(index + 1).copied().collect();
|
||||
composite_tiles_cached(&inputs(&f, 0, &images, &baked), &mut out, &[*tile], Some(&mut cache));
|
||||
if index == 0 {
|
||||
assert!(cache.holds_fields());
|
||||
}
|
||||
}
|
||||
assert!(above.len() > bottom.len());
|
||||
assert!(out == want, "a tile dropped from the cache was composited with fields that lack the layers off the hot path");
|
||||
}
|
||||
@@ -817,6 +817,8 @@ impl Session {
|
||||
images: &self.images,
|
||||
bounds: self.mesh.bounds(),
|
||||
};
|
||||
// A pass spread over frames tells the compositor what comes next, so neighbourhood filters run once over a run of tiles
|
||||
cache.below.ahead = if budget_ms.is_finite() { tiles_ahead(&cache.dirty, &tiles) } else { Vec::new() };
|
||||
texturecraft_compose::composite_tiles_cached(&inputs, &mut cache.composite, &tiles, Some(&mut cache.below));
|
||||
// The composited tiles and the neighbours their padding reached change.
|
||||
let padded = pad_tiles(&mut cache.composite, &cache.dilation, &tiles);
|
||||
@@ -977,6 +979,19 @@ fn next_chunk(dirty: &mut BTreeSet<(u32, u32)>, n: usize) -> Vec<(u32, u32)> {
|
||||
}
|
||||
|
||||
/// Valid texture set resolutions: powers of two from 128 to 8192.
|
||||
/// Dirty tiles a neighbourhood filter runs over ahead of the chunk being composited (see [`tiles_ahead`]).
|
||||
const FILTER_AHEAD: usize = 64;
|
||||
|
||||
/// The dirty tiles composited after `chunk` in its 16 × 16 tile blocks, in the order [`next_chunk`] takes them, at most [`FILTER_AHEAD`].
|
||||
fn tiles_ahead(dirty: &BTreeSet<(u32, u32)>, chunk: &[(u32, u32)]) -> Vec<(u32, u32)> {
|
||||
let block = |&(x, y): &(u32, u32)| (y / texturecraft_compose::BLOCK, x / texturecraft_compose::BLOCK);
|
||||
let blocks: BTreeSet<(u32, u32)> = chunk.iter().map(block).collect();
|
||||
let mut ahead: Vec<(u32, u32)> = dirty.iter().copied().filter(|tile| blocks.contains(&block(tile))).collect();
|
||||
ahead.sort_by_key(|tile| (block(tile), tile.1, tile.0));
|
||||
ahead.truncate(FILTER_AHEAD);
|
||||
ahead
|
||||
}
|
||||
|
||||
pub fn check_resolution(r: u32) -> Result<u32> {
|
||||
if (128..=8192).contains(&r) && r.is_power_of_two() {
|
||||
Ok(r)
|
||||
|
||||
@@ -397,3 +397,41 @@ fn bench_layer_thumbs_4k() {
|
||||
}
|
||||
eprintln!(" every target once: {:.1} ms ({} pictures)", total.as_secs_f64() * 1e3, targets.len());
|
||||
}
|
||||
|
||||
/// A full recomposite in one call against the same one spread over frames, as the UI runs it, with and without a blurred mask
|
||||
/// in the stack: neighbourhood filters are what a frame slice pays for apart from its own tiles.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn bench_sliced_filters_4k() {
|
||||
for blur in [false, true] {
|
||||
let mut s = Session::new_project("sliced", primitives::sphere(1.0, 64, 32), "builtin:sphere", None, 4096).unwrap();
|
||||
s.viewport = Vec2::new(800.0, 800.0);
|
||||
let run = |s: &mut Session, id: &str, p: Value| execute(s, id, &p).unwrap();
|
||||
run(&mut s, "layer.add.fill", json!({"color": [0.6, 0.5, 0.4], "roughness": 0.6}));
|
||||
run(&mut s, "layer.add.fill", json!({"color": [0.1, 0.3, 0.6]}));
|
||||
run(&mut s, "mask.add.black", Value::Null);
|
||||
run(&mut s, "effect.add.generator", json!({"generator": "position", "level": 0.5}));
|
||||
if blur {
|
||||
run(&mut s, "effect.add.filter", json!({"filter": "blur", "radius": 8}));
|
||||
}
|
||||
s.update();
|
||||
|
||||
s.caches[0].invalidate_all();
|
||||
let t = Instant::now();
|
||||
s.update();
|
||||
let direct = t.elapsed();
|
||||
|
||||
s.caches[0].invalidate_all();
|
||||
let clock = Instant::now();
|
||||
let ms = || clock.elapsed().as_secs_f64() * 1000.0;
|
||||
let (mut frames, mut worst) = (0, Duration::ZERO);
|
||||
while s.pending_tiles() > 0 {
|
||||
let t = Instant::now();
|
||||
s.update_for(30.0, ms);
|
||||
worst = worst.max(t.elapsed());
|
||||
frames += 1;
|
||||
}
|
||||
let label = if blur { "blurred mask" } else { "no filter" };
|
||||
println!("[sliced 4096², {label}] one call {direct:.2?}; in 30 ms slices {frames} frames, worst {worst:.2?}, total {:.2?}", clock.elapsed());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user