diff --git a/crates/compose/src/eval.rs b/crates/compose/src/eval.rs index b8299438..a60beb6f 100644 --- a/crates/compose/src/eval.rs +++ b/crates/compose/src/eval.rs @@ -803,6 +803,22 @@ fn fields_of(inp: &Inputs, channels: &[Channel], list: &[LayerId], tiles: &[(u32 out } +/// Whether texture set `ts` has a layer with a neighbourhood filter (blur, sharpen, warp…) in +/// its mask or content: a composite then precomputes [`neighbourhood_fields`] at full +/// resolution, so even a [`composite_tiles_coarse`] is not much cheaper than the exact one. +/// The same layers as `neighbourhood_fields` looks at (it may count a hidden one). +pub fn has_neighbourhood_filters(project: &Project, ts: usize) -> bool { + let Ok(set) = project.texture_set(ts) else { return false }; + let mut order = Vec::new(); + draw_order(project, &set.stack, 0, &mut order); + let res = set.resolution; + order.iter().any(|id| { + let mask = layer_mask(project, *id).is_some_and(|m| m.enabled && neighbourhood(&m.effects, res).is_some()); + let content = project.content_layer(*id).is_some_and(|l| !l.is_group() && neighbourhood(&l.content_effects, res).is_some()); + mask || content + }) +} + /// Build the curvature detail sizes every visible generator of `layers` reads, once, before tiles composite in parallel. fn prepare_curvature_sizes(inp: &Inputs, layers: &[LayerId]) { let visible = layers.iter().filter(|id| inp.project.layers.get(id).is_some_and(|layer| layer.visible)); @@ -899,10 +915,89 @@ fn mask_plan<'a>(inp: &Inputs<'a>, id: LayerId, mask: &'a Mask, tx: u32, ty: u32 } fn plan<'a>(inp: &Inputs<'a>, list: &[LayerId], channels: &[Channel], tx: u32, ty: u32, depth: usize, fields: Option<&'a Fields>) -> Vec> { + plan_with(inp, list, channels, tx, ty, depth, fields, false) +} + +/// [`plan`], leaving out what an opaque layer above hides when `cull` (see [`cull_hidden`]). +/// Only for lists that are evaluated whole and not looked into (no anchor or stop layer reads). +#[allow(clippy::too_many_arguments)] +fn plan_with<'a>( + inp: &Inputs<'a>, + list: &[LayerId], + channels: &[Channel], + tx: u32, + ty: u32, + depth: usize, + fields: Option<&'a Fields>, + cull: bool, +) -> Vec> { if depth > texturecraft_core::project::MAX_DEPTH { return Vec::new(); } - list.iter().filter_map(|id| plan_node(inp, *id, channels, tx, ty, depth, fields, true)).collect() + let mut nodes: Vec> = list.iter().filter_map(|id| plan_node(inp, *id, channels, tx, ty, depth, fields, true, cull)).collect(); + if cull { + cull_hidden(&mut nodes, channels); + } + nodes +} + +/// Whether node `n` hides everything below it in channel `ch`: it replaces the stack there at +/// every texel (full opacity, normal blend, no mask or geometry or height mask, a fill that +/// covers the whole surface with no content effects), or is a folder at full opacity holding +/// such a layer. +fn occludes(n: &Node, ch: usize) -> bool { + if n.mask.is_some() || n.parts.is_some() || n.height_mask.enabled { + return false; + } + let Some(c) = n.chans.iter().find(|c| c.ch == ch) else { return false }; + if c.opacity < 1.0 { + return false; + } + match &n.content { + Content::Fill(f) => c.blend == BlendMode::Normal && !matches!(f.projection, Projection::Decal(_)) && c.effects.is_empty() && c.start.is_none(), + Content::Group(kids) => kids.iter().any(|k| occludes(k, ch)), + Content::Paint { .. } => false, + } +} + +/// Whether `n` or a layer in it masks with its height. +fn uses_height_mask(n: &Node) -> bool { + n.height_mask.enabled || matches!(&n.content, Content::Group(kids) if kids.iter().any(uses_height_mask)) +} + +/// Take channel `ch` out of `n` (and what it holds), dropping what then draws nothing. +fn strip_channel(n: &mut Node, ch: usize) { + n.chans.retain(|c| c.ch != ch); + if let Content::Group(kids) = &mut n.content { + for k in kids.iter_mut() { + strip_channel(k, ch); + } + kids.retain(|k| !k.chans.is_empty()); + } +} + +/// Leave out the layers (per channel) that a layer above them hides completely: a stack that +/// grows by applying material after material would otherwise evaluate every one of them. The +/// result is identical. Channels with anchor points or height masks below the hiding layer +/// are left alone (they read other layers' values), and so are those under a hiding folder +/// that records an anchor point: its inner stack starts from what lies below it, so an anchor +/// under the hiding layer inside it holds the layers it hides. +fn cull_hidden(nodes: &mut Vec, channels: &[Channel]) { + for c in channels { + let ch = c.index(); + let Some(k) = nodes.iter().rposition(|n| occludes(n, ch)) else { continue }; + if nodes.get(k).is_some_and(|n| n.has_anchor) { + continue; + } + let Some(below) = nodes.get_mut(..k) else { continue }; + if below.iter().any(|n| n.has_anchor || (*c == Channel::Height && uses_height_mask(n))) { + continue; + } + for n in below.iter_mut() { + strip_channel(n, ch); + } + } + nodes.retain(|n| !n.chans.is_empty() || n.has_anchor); } /// The plan of layer `id` at `depth` (None where it draws nothing); a folder's children are @@ -917,6 +1012,7 @@ fn plan_node<'a>( depth: usize, fields: Option<&'a Fields>, kids: bool, + cull: bool, ) -> Option> { let own: &'a Layer = inp.project.layers.get(&id)?; // Instances draw their source's content (a missing source draws nothing). @@ -943,7 +1039,9 @@ fn plan_node<'a>( let content = match &layer.kind { LayerKind::Paint => Content::Paint { preview: previews(inp, PaintTarget::Layer(layer.id)) }, LayerKind::Fill(f) => Content::Fill(f), - LayerKind::Group { children, .. } => Content::Group(if kids { plan(inp, children, channels, tx, ty, depth + 1, fields) } else { Vec::new() }), + LayerKind::Group { children, .. } => { + Content::Group(if kids { plan_with(inp, children, channels, tx, ty, depth + 1, fields, cull) } else { Vec::new() }) + } // `content_layer` already followed instance links. LayerKind::Instance { .. } => return None, }; @@ -990,8 +1088,8 @@ fn plan_levels<'a>(inp: &Inputs<'a>, levels: &[Level], channels: &[Channel], tx: .iter() .enumerate() .map(|(d, l)| LevelPlan { - below: if warm { Vec::new() } else { plan(inp, &l.below, channels, tx, ty, d, Some(fields)) }, - at: plan_node(inp, l.at, channels, tx, ty, d, Some(fields), d == last), + below: if warm { Vec::new() } else { plan_with(inp, &l.below, channels, tx, ty, d, Some(fields), true) }, + at: plan_node(inp, l.at, channels, tx, ty, d, Some(fields), d == last, false), above: plan(inp, &l.above, channels, tx, ty, d, Some(fields)), }) .collect() @@ -1660,6 +1758,35 @@ pub fn composite_tiles(inp: &Inputs, out: &mut Composite, tiles: &[(u32, u32)]) composite_tiles_cached(inp, out, tiles, None); } +/// The strides a coarse composite ([`composite_tiles_coarse`]) takes, coarsest first: texels +/// per block side, one evaluated per block. Each divides the rows of a band and a tile, so a +/// block never crosses bands or tiles. A big recomposite shows them in this order before the +/// exact composite. One level: a coarse pass costs about the same at any stride (writing, +/// padding and uploading the full-resolution tiles dominate once one texel in 64 is +/// evaluated; measured 4096², 3217 tiles: 139 ms at 16, 150 at 8, 254 at 4, 2.1 s exact), so +/// a second level would only add that cost again before the exact composite. +pub const COARSE_STRIDES: [u32; 1] = [8]; + +/// A quick look at `tiles`: [`composite_tiles`] evaluating one texel in every `stride` by `stride` +/// block (its first covered texel) and repeating it over the block's covered texels (a texel only +/// depends on its own position, so the evaluated ones are exact; the rest are as blocky as a +/// lower resolution), about `stride`² times fewer evaluations. A `stride` that does not divide a band and a tile composites exactly. A later +/// [`composite_tiles`] of the same tiles gives the exact result. +pub fn composite_tiles_coarse(inp: &Inputs, out: &mut Composite, tiles: &[(u32, u32)], stride: u32) { + let Ok(set) = inp.project.texture_set(inp.ts) else { return }; + if (inp.gbuf.width, inp.gbuf.height) != (out.resolution, out.height) { + return; + } + let step = if stride > 0 && BAND.is_multiple_of(stride) && TILE.is_multiple_of(stride) { stride } else { 1 }; + let mut blocks: BTreeMap<(u32, u32), Vec<(u32, u32)>> = BTreeMap::new(); + for t in tiles { + blocks.entry((t.1 / BLOCK, t.0 / BLOCK)).or_default().push(*t); + } + for block in blocks.values() { + composite_block(inp, &set.stack, out, block, None, 0, step); + } +} + /// Tiles per side of the blocks a composite works through (bounds neighbourhood-filter memory). const BLOCK: u32 = 16; @@ -1688,7 +1815,7 @@ pub fn composite_tiles_cached(inp: &Inputs, out: &mut Composite, tiles: &[(u32, 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, 1); if let Some(b) = below.as_deref_mut() { for t in block { b.touch(*t); @@ -1719,6 +1846,7 @@ fn composite_block( tiles: &[(u32, u32)], below: Option<(&BelowCache, &[Level])>, quota: usize, + step: u32, ) -> Vec<((u32, u32), Vec)> { let channels: Vec = inp.project.texture_set(inp.ts).map(|s| s.channel_list()).unwrap_or_default(); let (w, h) = (out.resolution, out.height); @@ -1748,7 +1876,7 @@ fn composite_block( 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 nodes = if levels.is_none() { plan_with(inp, stack, &channels, tx, ty, 0, Some(&fields), true) } else { Vec::new() }; let rows = (BAND * TILE) as usize; let mut accs = vec![defaults; nlev]; let mut masks = vec![0.0f32; nlev]; @@ -1772,11 +1900,17 @@ fn composite_block( let mut bufs: Vec> = channels.iter().map(|c| vec![0u16; rows * c.components()]).collect(); let mut anchors = Vec::new(); let mut any = false; + // Coarse: per `step`-wide block of the current block row, the value of its first + // covered texel (evaluated), repeated over the block's other covered texels. + let mut sampled: Vec> = vec![None; if step > 1 { (TILE / step) as usize } else { 0 }]; for ly in band * BAND..(band + 1) * BAND { let y = ty * TILE + ly; if y >= h { break; } + if step > 1 && ly % step == 0 { + sampled.fill(None); + } for lx in 0..TILE { let x = tx * TILE + lx; if x >= w { @@ -1788,7 +1922,12 @@ fn composite_block( let ob = ((ly - band * BAND) * TILE + lx) as usize; let covered = inp.gbuf.tri.get(gi).is_some_and(|t| *t != NO_TRI); let mut acc = defaults; - if covered { + let block = if step > 1 { sampled.get_mut((lx / step) as usize) } else { None }; + let repeated = block.as_ref().and_then(|b| **b); + if covered && let Some(a) = repeated { + any = true; + acc = a; + } else if covered { any = true; let mut tex = Texel::new(inp, x, y, o, stroke_at(preview_tile, o)); // Reuse one anchor buffer per band. @@ -1820,6 +1959,12 @@ fn composite_block( anchors = tex.anchors; anchors.clear(); } + if covered + && repeated.is_none() + && let Some(slot) = block + { + *slot = Some(acc); + } for (l, buf) in lanes.iter().zip(&mut bufs) { let v = acc.get(l.ch).copied().unwrap_or([0.0; 3]); for (k, x) in v.iter().take(l.n).enumerate() { diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index 46c3c6b5..0475fb8f 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -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, + COARSE_STRIDES, Composite, Inputs, PaintTarget, Stencil, StrokePreview, all_tiles, composite_tiles, composite_tiles_cached, composite_tiles_coarse, + covered_tiles, effect_histogram, flatten_layer, has_neighbourhood_filters, mask_at, mask_image, pad_tiles, }; pub use store::{BakedMaps, ImageRes, ImageStore, PixelStore}; pub use thumb::{THUMB_TEXELS, ThumbImage, ThumbTarget}; diff --git a/crates/compose/tests/cache_and_instances.rs b/crates/compose/tests/cache_and_instances.rs index d7df0258..50d125a6 100644 --- a/crates/compose/tests/cache_and_instances.rs +++ b/crates/compose/tests/cache_and_instances.rs @@ -573,3 +573,35 @@ fn gathering_filters_composite_identically_in_any_grouping_and_cached() { assert!(!check_cached(&f, 0, hot).is_empty()); } } + +/// A folder that hides the layers below it but records an anchor point inside (under its own +/// hiding layer) keeps them: its inner stack starts from them, so the anchor holds them. +#[test] +fn layers_under_a_hiding_folder_with_an_anchor_are_not_culled() { + let mut f = fixture(); + let only = |f: &mut Fixture, id: LayerId, c: Channel| f.project.layer_mut(id).unwrap().channels.retain(|x, _| *x == c); + let low = add(&mut f, 0, fill(&[(Channel::BaseColor, Source::Uniform([0.9; 3]))]), None); + only(&mut f, low, Channel::BaseColor); + let group = add(&mut f, 0, LayerKind::Group { children: vec![], collapsed: false }, None); + // Inside: a half-opacity fill recording anchor A, then an opaque fill hiding everything. + let anchored = put(&mut f, 0, Some(group), fill(&[(Channel::BaseColor, Source::Uniform([0.5; 3]))]), None); + only(&mut f, anchored, Channel::BaseColor); + blend(&mut f, anchored, BlendMode::Normal, 0.5); + let anchor = effect(&mut f, EffectKind::Anchor); + let anchor_id = anchor.id; + f.project.layer_mut(anchored).unwrap().content_effects.push(anchor); + let hiding = put(&mut f, 0, Some(group), fill(&[(Channel::BaseColor, Source::Uniform([0.1; 3]))]), None); + only(&mut f, hiding, Channel::BaseColor); + // Above the folder, roughness reads the base color held by A. + let reader = add(&mut f, 0, fill(&[(Channel::Roughness, Source::Anchor { anchor: anchor_id, channel: Channel::BaseColor })]), None); + only(&mut f, reader, Channel::Roughness); + let out = reference(&f, 0); + let rough = out.value(Channel::Roughness, 128, 128)[0]; + // A = 0.5 at half opacity over the low layer's 0.9. + assert!((rough - 0.7).abs() < 2e-3, "the anchor holds the layer under the folder: {rough}"); + assert!((out.value(Channel::BaseColor, 128, 128)[0] - 0.1).abs() < 2e-3); + // The same stack with an (unread) anchor on the low layer is never culled: identical. + let keep = effect(&mut f, EffectKind::Anchor); + f.project.layer_mut(low).unwrap().content_effects.push(keep); + assert!(reference(&f, 0) == out, "culling changed the composite"); +} diff --git a/crates/core/src/blend.rs b/crates/core/src/blend.rs index 5f165cdb..c7fa7e64 100644 --- a/crates/core/src/blend.rs +++ b/crates/core/src/blend.rs @@ -350,6 +350,11 @@ pub fn blend3(mode: BlendMode, a: [f32; 3], b: [f32; 3]) -> [f32; 3] { /// Mix `blended` over `a` by weight `w` (coverage × opacity × mask). pub fn mix3(a: [f32; 3], blended: [f32; 3], w: f32) -> [f32; 3] { + // A full-weight blend is exactly the blended value (`a + (b - a)` can be an ulp off it): what + // lies under it does not leak in, so skipping the layers it hides changes nothing at all. + if w >= 1.0 { + return blended; + } [a[0] + (blended[0] - a[0]) * w, a[1] + (blended[1] - a[1]) * w, a[2] + (blended[2] - a[2]) * w] } diff --git a/crates/engine/src/inspect.rs b/crates/engine/src/inspect.rs index 2db27946..603e7b17 100644 --- a/crates/engine/src/inspect.rs +++ b/crates/engine/src/inspect.rs @@ -193,5 +193,10 @@ pub fn session(s: &Session) -> Value { "history": { "undo": s.undo_label(), "redo": s.redo_label(), "depth": s.history_labels().len() }, "memoryBytes": s.memory().total(), "pendingTiles": s.pending_tiles(), + // Tiles with no look of the current composite yet, tiles shown from a coarse pass only + // (their exact composite pending), and the coarsest stride shown (0: none). + "dirtyTiles": s.caches.iter().map(|c| c.dirty.len()).sum::(), + "coarseTiles": s.caches.iter().map(|c| c.refine.len()).sum::(), + "coarsestStride": s.caches.iter().flat_map(|c| c.refine.values().copied()).max().unwrap_or(0), }) } diff --git a/crates/engine/src/session.rs b/crates/engine/src/session.rs index 64ebec69..6b721a79 100644 --- a/crates/engine/src/session.rs +++ b/crates/engine/src/session.rs @@ -4,7 +4,7 @@ use std::collections::{BTreeMap, BTreeSet}; use std::sync::Arc; use serde::{Deserialize, Serialize}; -use texturecraft_compose::{BakedMaps, BelowCache, Composite, PaintTarget, PixelStore, StackKey, StrokePreview, covered_tiles, pad_tiles}; +use texturecraft_compose::{BakedMaps, BelowCache, COARSE_STRIDES, Composite, PaintTarget, PixelStore, StackKey, StrokePreview, covered_tiles, pad_tiles}; use texturecraft_core::{Channel, Layer, LayerId, LayerKind, Project, TextureSet}; use texturecraft_geom::glam::Vec2; use texturecraft_geom::{Bvh, Mesh, OrbitCamera, UvGBuffer}; @@ -43,14 +43,24 @@ pub struct SetCache { /// Tiles to recomposite at low priority: what they show is already right (a committed /// stroke's live preview), but it was composited from the stroke, not the layer pixels. pub settle: BTreeSet<(u32, u32)>, + /// Tiles shown from a coarse pass ([`texturecraft_compose::composite_tiles_coarse`]) with + /// the stride they were composited at; they still need the finer ones and the exact one. A + /// big recomposite (a material applied) first looks right everywhere at a small part of the + /// cost, then sharpens level by level ([`texturecraft_compose::COARSE_STRIDES`]), each level + /// over every tile before the next, so the surface is always at one consistent level. + pub refine: BTreeMap<(u32, u32), u32>, + /// The dirty tiles go through a coarse pass: decided when a big recomposite starts and + /// kept until `dirty` drains, so its last few tiles do not go exact at the full price. + pub coarse_pass: bool, /// Tiles whose display textures must be re-uploaded. pub upload: BTreeSet<(u32, u32)>, /// Bumps on every composite change (thumbnails, exports, caches). pub revision: u64, /// Everything but the layer being edited, per tile (repaints evaluate only that layer). pub below: BelowCache, - /// Tiles per composite chunk that fit the last frame's budget (0: not measured yet). - pub chunk: usize, + /// Wall-clock milliseconds per tile of the last exact composite chunk (0: not measured + /// yet): the first chunk of a budgeted update is sized from it and that update's budget. + pub tile_ms: f64, } impl SetCache { @@ -69,10 +79,12 @@ impl SetCache { composite: Composite { block_scales: ts.block_scales(), ..Composite::with_size(&ts.channel_list(), w, h) }, dirty: all.clone(), settle: BTreeSet::new(), + refine: BTreeMap::new(), + coarse_pass: false, upload: all, revision: 0, below: BelowCache::default(), - chunk: 0, + tile_ms: 0.0, } } @@ -715,23 +727,60 @@ impl Session { carried: s.canvas.pickup.as_ref().map(|p| p.values.clone()).unwrap_or_default(), }) }); + // A coarse look first only when there is a frame budget to spread a big change over, and + // never under a live stroke (it keeps the exact, cached repaint of the hot layer). + let coarse_allowed = budget_ms.is_finite() && self.stroke.is_none(); let active = self.ts(); let order = std::iter::once(active).chain((0..self.caches.len()).filter(|ts| *ts != active)); let mut changed = false; for ts in order { let Some(cache) = self.caches.get_mut(ts) else { continue }; cache.below.set_hot(hot); - cache.below.pass = cache.dirty.len() + cache.settle.len(); + cache.below.pass = cache.dirty.len() + cache.settle.len() + cache.refine.len(); // Instances in sets of the stroke's size show the live stroke too. let preview = preview.as_ref().filter(|_| Some(cache.size) == stroke_size); - // Start at the size that fit the budget last time: with a heavy stack (smart - // materials at 8K) one tile per thread alone would take several frames' budget. - let mut chunk = if budget_ms.is_finite() && cache.chunk > 0 { cache.chunk } else { first }; + // Start at the size this budget fits at the last measured pace: with a heavy stack + // (smart materials at 8K) one tile per thread alone would take several frames' + // budget. Never above one tile per thread: work can turn heavy from one frame to the + // next. + let exact_chunk = + |cache: &SetCache| if budget_ms.is_finite() && cache.tile_ms > 0.0 { ((budget_ms / cache.tile_ms) as usize).clamp(1, first) } else { first }; + let mut chunk = exact_chunk(cache); + // A stack with neighbourhood filters computes them at full resolution even for a + // coarse composite, so it gains little from one: straight to the exact composite. + let may_coarse = coarse_allowed && !cache.dirty.is_empty() && !texturecraft_compose::has_neighbourhood_filters(&self.doc.project, ts); + // The stride of the previous chunk (1: exact), to restart the chunk size on a change. + let mut last_stride = 1; loop { if cache.dirty.is_empty() { - if cache.settle.is_empty() || clock() - start >= budget_ms.min(SETTLE_BUDGET_MS) { - break; + cache.coarse_pass = false; + } + // The work of this chunk: fresh dirty tiles (coarsest look or exact), the next + // level of the coarsest refining tiles, or settling tiles. + let (stride, refining, settling): (u32, Option, bool) = if !cache.dirty.is_empty() { + // A big change gets a coarse look first, for the whole of it. + if may_coarse && cache.dirty.len() > COARSE_MIN_TILES { + cache.coarse_pass = true; } + let coarse = may_coarse && cache.coarse_pass; + (if coarse { COARSE_STRIDES[0] } else { 1 }, None, false) + } else if let Some(at) = cache.refine.values().copied().max() { + (finer_stride(at), Some(at), false) + } else if !cache.settle.is_empty() && clock() - start < budget_ms.min(SETTLE_BUDGET_MS) { + (1, None, true) + } else { + break; + }; + // A level costs about its stride² less per texel than the exact composite: a + // new level starts at the exact pace, and the chunks grow from what it measures. + if stride != last_stride { + chunk = exact_chunk(cache); + last_stride = stride; + } + if let Some(at) = refining { + let mut level: BTreeSet<(u32, u32)> = cache.refine.iter().filter(|(_, s)| **s == at).map(|(t, _)| *t).collect(); + cache.dirty.extend(next_chunk(&mut level, chunk)); + } else if settling { // Small chunks: the settle slice of the budget is short. cache.dirty.extend(next_chunk(&mut cache.settle, chunk.min(first))); } @@ -739,6 +788,7 @@ impl Session { let tiles = next_chunk(&mut cache.dirty, chunk); for t in &tiles { cache.settle.remove(t); + cache.refine.remove(t); } let empty = BakedMaps::default(); let inputs = texturecraft_compose::Inputs { @@ -753,7 +803,12 @@ impl Session { images: &self.images, bounds: self.mesh.bounds(), }; - texturecraft_compose::composite_tiles_cached(&inputs, &mut cache.composite, &tiles, Some(&mut cache.below)); + if stride > 1 { + texturecraft_compose::composite_tiles_coarse(&inputs, &mut cache.composite, &tiles, stride); + cache.refine.extend(tiles.iter().map(|t| (*t, stride))); + } else { + 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); cache.upload.extend(tiles.iter().copied().chain(padded)); @@ -763,9 +818,8 @@ impl Session { let left = budget_ms - (now - start); // As many tiles as fit in what is left, at the pace of this chunk. let per_tile = (now - t0) / tiles.len().max(1) as f64; - if budget_ms.is_finite() && per_tile > 0.0 { - // Never above one tile per thread: work can turn heavy from one frame to the next. - cache.chunk = ((budget_ms / per_tile) as usize).clamp(1, first); + if budget_ms.is_finite() && per_tile > 0.0 && stride == 1 { + cache.tile_ms = per_tile; } if left <= 0.0 { return changed; @@ -782,7 +836,7 @@ impl Session { /// Tiles still waiting to be composited, over every texture set (settling ones included). pub fn pending_tiles(&self) -> usize { - self.caches.iter().map(|c| c.dirty.len() + c.settle.len()).sum() + self.caches.iter().map(|c| c.dirty.len() + c.settle.len() + c.refine.len()).sum() } /// The layer a stroke target belongs to (an effect's owner). @@ -894,6 +948,9 @@ impl Memory { /// Most tiles per composite chunk (a 1024² region). const CHUNK: usize = 256; +/// A recomposite of more tiles than this (a material applied, not a brush stroke) is shown from +/// a coarse pass first when it is spread over frames. +const COARSE_MIN_TILES: usize = 64; /// The most a budgeted update spends on settling tiles (what they show is already right). pub const SETTLE_BUDGET_MS: f64 = 6.0; @@ -912,6 +969,11 @@ fn next_chunk(dirty: &mut BTreeSet<(u32, u32)>, n: usize) -> Vec<(u32, u32)> { tiles } +/// The stride of the level after `stride` in [`COARSE_STRIDES`] (1: the exact composite). +fn finer_stride(stride: u32) -> u32 { + COARSE_STRIDES.iter().copied().find(|s| *s < stride).unwrap_or(1) +} + /// Valid texture set resolutions: powers of two from 128 to 8192. pub fn check_resolution(r: u32) -> Result { if (128..=8192).contains(&r) && r.is_power_of_two() { diff --git a/crates/engine/tests/it/instances.rs b/crates/engine/tests/it/instances.rs index b9f14bcd..f3a1b594 100644 --- a/crates/engine/tests/it/instances.rs +++ b/crates/engine/tests/it/instances.rs @@ -220,10 +220,12 @@ fn budgeted_updates_spread_work_and_end_identical() { s.caches[0].invalidate_all(); // A clock that is always over budget: one chunk per call. let mut calls = 0; + // Progress: a tile is two steps from done while dirty (coarse look, then exact), one when refining. + let steps = |s: &texturecraft_engine::Session| 2 * s.caches[0].dirty.len() + s.caches[0].refine.len() + s.caches[0].settle.len(); while s.pending_tiles() > 0 { - let before = s.pending_tiles(); + let before = steps(&s); s.update_for(0.0, || 1.0); - assert!(s.pending_tiles() < before); + assert!(steps(&s) < before); calls += 1; } assert!(calls > 1, "the work was spread over calls"); diff --git a/crates/engine/tests/session.rs b/crates/engine/tests/session.rs index 4dd04032..89f27beb 100644 --- a/crates/engine/tests/session.rs +++ b/crates/engine/tests/session.rs @@ -446,3 +446,166 @@ fn curvature_generators_take_a_mode_and_detail_sizes() { assert!(dirt.is_err(), "only curvature generators have a mode"); assert_eq!(execute(&mut state, "project.inspect", &Value::Null).unwrap().to_string(), inspect, "failed commands leave the project unchanged"); } + +/// A material applied over frames first looks right everywhere from a coarse pass (the sampled +/// texels are exact, the rest repeat them), then the tiles fill in to the exact composite. +#[test] +fn a_big_recomposite_shows_a_coarse_look_first_then_the_exact_one() { + let make = || { + let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:sphere", None, 2048).unwrap(); + s.viewport = texturecraft_engine::geom::glam::Vec2::new(400.0, 400.0); + s.update(); + execute(&mut s, "material.apply", &json!({"material": "steel_painted_worn"})).unwrap(); + s + }; + let mut exact = make(); + exact.update(); + assert_eq!(exact.pending_tiles(), 0, "an unbudgeted update goes straight to the exact composite"); + let exact = exact.caches[0].composite.clone(); + + let mut s = make(); + let t = std::cell::Cell::new(0.0); + let clock = || { + t.set(t.get() + 2.0); + t.get() + }; + let covered = s.caches[0].covered_tiles.len(); + assert!(covered > 100, "a big change: {covered} tiles"); + while !s.caches[0].dirty.is_empty() { + s.update_for(30.0, clock); + } + assert!(s.caches[0].refine.len() > 100 && s.pending_tiles() > 0, "everything has a coarse look, the exact one is pending"); + let coarse = s.caches[0].composite.clone(); + assert_ne!(coarse, exact, "the coarse look is not the exact one"); + // Where the coarse pass sampled (a block's first covered texel, its corner where covered), it + // is exact; the others repeat the sample of their block. + let c = Channel::BaseColor; + let stride = texturecraft_engine::compose::COARSE_STRIDES[0]; + let (mut sampled, mut repeated, mut checked) = (0, 0, 0); + for y in (0..2048).step_by(7) { + for x in (0..2048).step_by(5) { + let (ax, ay) = (x - x % stride, y - y % stride); + if x == ax && y == ay { + if coarse.value(c, x, y) == exact.value(c, x, y) { + sampled += 1; + } + checked += 1; + } else if coarse.value(c, x, y) == coarse.value(c, ax, ay) { + repeated += 1; + } + } + } + assert!(checked > 100 && sampled * 10 >= checked * 9, "sampled texels are exact: {sampled} of {checked}"); + assert!(repeated > 1000, "the others repeat their sample: {repeated}"); + while s.pending_tiles() > 0 { + s.update_for(30.0, clock); + } + assert_eq!(s.caches[0].composite, exact, "the refined composite is the exact one"); +} + +/// Layers an opaque layer above hides are not evaluated: a material applied over others +/// composites exactly like the same material applied alone, in the channels it sets. +#[test] +fn layers_hidden_by_an_opaque_material_are_left_out_without_changing_the_result() { + let make = |materials: &[&str]| { + let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:sphere", None, 512).unwrap(); + s.viewport = texturecraft_engine::geom::glam::Vec2::new(400.0, 400.0); + for m in materials { + execute(&mut s, "material.apply", &json!({"material": m})).unwrap(); + } + s.update(); + s.caches[0].composite.clone() + }; + let alone = make(&["gold_polished"]); + let stacked = make(&["steel_painted_worn", "copper", "gold_polished"]); + let underneath = make(&["steel_painted_worn", "copper"]); + assert_ne!(alone, underneath, "the materials differ"); + for c in [Channel::BaseColor, Channel::Roughness, Channel::Metallic] { + let (mut compared, mut differing) = (0, 0); + for y in (0..512).step_by(3) { + for x in (0..512).step_by(3) { + compared += 1; + if alone.value(c, x, y) != stacked.value(c, x, y) { + differing += 1; + } + } + } + assert!(compared > 1000 && differing == 0, "{c:?}: {differing} of {compared} texels differ"); + } +} + +/// A clock that advances 2 ms per reading, for budgeted updates. +fn ticking() -> impl Fn() -> f64 { + let t = std::cell::Cell::new(0.0); + move || { + t.set(t.get() + 2.0); + t.get() + } +} + +/// A big change goes through the coarse pass to its last tile (the decision is kept for the +/// whole pass): its tail does not go exact at the full price while the rest waits for detail. +#[test] +fn the_coarse_pass_covers_every_tile_of_a_big_change() { + let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:sphere", None, 1024).unwrap(); + s.update(); + execute(&mut s, "material.apply", &json!({"material": "steel_painted_worn"})).unwrap(); + let dirty = s.caches[0].dirty.len(); + assert!(dirty > 100, "a big change: {dirty} tiles"); + // A clock always over budget: one chunk per call. + let mut calls = 0; + while !s.caches[0].dirty.is_empty() { + s.update_for(0.0, || 1.0); + let c = &s.caches[0]; + assert_eq!(c.refine.len() + c.dirty.len(), dirty, "no tile went exact during the coarse pass"); + calls += 1; + } + assert!(calls > 1, "the pass was spread over calls"); + s.update_for(0.0, || 1.0); + assert!(!s.caches[0].coarse_pass, "the pass ended with its tiles"); + let clock = ticking(); + // A small change after it goes straight to the exact composite. + let some: Vec<(u32, u32)> = s.caches[0].covered_tiles.iter().copied().take(10).collect(); + s.update(); + s.caches[0].dirty.extend(some); + s.update_for(30.0, &clock); + assert!(s.caches[0].refine.is_empty() && s.caches[0].dirty.is_empty(), "a few tiles are composited exactly"); +} + +/// Under a live stroke the compositor keeps the exact (cached) repaint: no coarse pass. +#[test] +fn no_coarse_pass_while_a_stroke_is_live() { + let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:sphere", None, 1024).unwrap(); + s.update(); + s.begin_stroke_uv(texturecraft_engine::geom::glam::Vec2::new(0.5, 0.5), 1.0).unwrap(); + s.caches[0].invalidate_all(); + assert!(s.caches[0].dirty.len() > 100); + let clock = ticking(); + while !s.caches[0].dirty.is_empty() { + s.update_for(30.0, &clock); + assert!(s.caches[0].refine.is_empty(), "nothing went coarse under the stroke"); + } + s.end_stroke().unwrap(); +} + +/// A stack with a neighbourhood filter (blur) computes it at full resolution even for a coarse +/// look, which then saves little: such a stack goes straight to the exact composite. +#[test] +fn stacks_with_neighbourhood_filters_skip_the_coarse_pass() { + use texturecraft_engine::core::{Effect, EffectKind, Filter}; + let mut s = Session::new_project("t", primitives::sphere(1.0, 48, 24), "builtin:sphere", None, 1024).unwrap(); + s.update(); + execute(&mut s, "material.apply", &json!({"material": "copper"})).unwrap(); + s.update(); + assert!(!texturecraft_engine::compose::has_neighbourhood_filters(&s.doc.project, 0)); + let top = *s.doc.project.texture_sets[0].stack.last().unwrap(); + let id = s.doc.project.alloc_id(); + s.doc.project.layer_mut(top).unwrap().content_effects.push(Effect::new(id, EffectKind::Filter(Filter::Blur { radius: 8.0 }))); + assert!(texturecraft_engine::compose::has_neighbourhood_filters(&s.doc.project, 0)); + s.caches[0].invalidate_all(); + let clock = ticking(); + while !s.caches[0].dirty.is_empty() { + s.update_for(30.0, &clock); + assert!(s.caches[0].refine.is_empty(), "nothing went coarse"); + } +} diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rs index 944a8ecd..7811577b 100644 --- a/crates/ui-egui/src/gpu.rs +++ b/crates/ui-egui/src/gpu.rs @@ -93,11 +93,29 @@ pub struct GpuLink { pub adapter: String, /// Most tiles waiting to be composited since compositing last caught up (progress). composite_total: usize, + /// Consecutive frames without user input (see [`frame_budgets`]). + idle_frames: u32, bake_scene: BakeSceneLink, } -/// Compositing time per frame: a full recomposite of a large texture set spreads over frames. +/// The compositing and whole-frame budgets in milliseconds after `idle_frames` frames without +/// user input: the responsive pair while the user works, the longer pair once they have stopped. +fn frame_budgets(idle_frames: u32) -> (f64, f64) { + if idle_frames >= IDLE_FRAMES_BEFORE_BOOST { (IDLE_COMPOSITE_BUDGET_MS, IDLE_FRAME_BUDGET_MS) } else { (COMPOSITE_BUDGET_MS, FRAME_BUDGET_MS) } +} + +/// Compositing time per frame while the user is working: a full recomposite of a large texture +/// set spreads over frames. const COMPOSITE_BUDGET_MS: f64 = 30.0; +/// Compositing time per frame once the user has stopped touching the app: the compositor scales +/// with the machine's cores, so a longer slice finishes sooner on a fast PC (a frame then takes +/// longer, but nothing is being dragged or painted that would feel it). +const IDLE_COMPOSITE_BUDGET_MS: f64 = 100.0; +/// [`FRAME_BUDGET_MS`] while idle: the idle compositing slice plus the usual rest of the frame. +const IDLE_FRAME_BUDGET_MS: f64 = IDLE_COMPOSITE_BUDGET_MS + (FRAME_BUDGET_MS - COMPOSITE_BUDGET_MS); +/// Frames without input (about 0.2 s at 60 fps) before the longer idle slices start, so a short +/// pause between strokes or drags keeps the responsive budget. +const IDLE_FRAMES_BEFORE_BOOST: u32 = 12; /// The most a frame spends on the scene (with the work already done this frame, such as a /// stroke's commit): compositing and display conversion leave the rest for the next frames, so /// a large (8K) texture set never stalls one frame. @@ -109,6 +127,10 @@ const UPLOAD_CHUNK: usize = 64; /// Estimated GPU upload cost of one display tile (write_texture of one 64×64 tile of one /// display kind, measured at ~5.5 µs on a desktop GPU), counted against the frame budget. const UPLOAD_MS_PER_TILE: f64 = 0.006; +/// CPU cost of converting one display tile of one kind for upload ([`tile_bytes`], measured at +/// ~6 µs: 8192 in 50 ms on a 24-thread desktop): with [`UPLOAD_MS_PER_TILE`], what a tile still +/// waiting for upload will cost this frame. +const CONVERT_MS_PER_TILE: f64 = 0.006; pub fn init(rs: &egui_wgpu::RenderState, ctx: &egui::Context) -> GpuLink { let reset = Arc::new(AtomicBool::new(false)); @@ -140,6 +162,7 @@ pub fn init(rs: &egui_wgpu::RenderState, ctx: &egui::Context) -> GpuLink { aux: BTreeMap::new(), adapter: format!("{} ({:?})", info.name, info.backend), composite_total: 0, + idle_frames: 0, bake_scene: BakeSceneLink::default(), } } @@ -162,13 +185,28 @@ impl GpuLink { } /// Collect everything the GPU needs this frame, running the compositor first: `bake` is what bake mode's 3D view draws besides the low poly, `spent_ms` the time this frame already took - pub fn sync(&mut self, session: &mut Session, aux_map: Option, bake: Option, shift: u32, spent_ms: f64) -> SceneSync { + pub fn sync( + &mut self, + session: &mut Session, + aux_map: Option, + bake: Option, + shift: u32, + spent_ms: f64, + user_active: bool, + ) -> SceneSync { let start_ms = crate::now_ms(); let mut queued = 0usize; - let time_left_ms = |queued: usize| FRAME_BUDGET_MS - spent_ms - (crate::now_ms() - start_ms) - queued as f64 * UPLOAD_MS_PER_TILE; + self.idle_frames = if user_active { 0 } else { self.idle_frames.saturating_add(1) }; + let (composite_budget_ms, frame_budget_ms) = frame_budgets(self.idle_frames); + let time_left_ms = |queued: usize| frame_budget_ms - spent_ms - (crate::now_ms() - start_ms) - queued as f64 * UPLOAD_MS_PER_TILE; let pending = session.pending_tiles(); self.composite_total = if pending == 0 { 0 } else { self.composite_total.max(pending) }; - session.update_for(COMPOSITE_BUDGET_MS.min(time_left_ms(0)).max(MIN_SLICE_MS), crate::now_ms); + // Tiles composited earlier and still waiting to be uploaded go first: a big change's + // coarse look is composited in a frame or two, and showing it whole matters more than + // compositing on (otherwise it appears a chunk of tiles per frame). + let backlog = session.caches.iter().map(|c| c.upload.len()).sum::() * DisplayKind::ALL.len(); + let backlog_ms = backlog as f64 * (UPLOAD_MS_PER_TILE + CONVERT_MS_PER_TILE); + session.update_for(composite_budget_ms.min(time_left_ms(0) - backlog_ms).max(MIN_SLICE_MS), crate::now_ms); let mut out = SceneSync::default(); if self.lost.swap(false, Ordering::AcqRel) { @@ -410,3 +448,26 @@ impl CallbackTrait for ViewCallback { gpu.renderer.blit_prepared(pass, self.frame.view_id); } } + +#[cfg(test)] +mod tests { + use super::*; + + /// While the user is working (or just stopped) a frame keeps the responsive budgets. + #[test] + fn budgets_stay_short_while_the_user_is_active() { + assert_eq!(frame_budgets(0), (COMPOSITE_BUDGET_MS, FRAME_BUDGET_MS)); + assert_eq!(frame_budgets(IDLE_FRAMES_BEFORE_BOOST - 1), (COMPOSITE_BUDGET_MS, FRAME_BUDGET_MS)); + } + + /// After a pause the compositor gets the longer slice, and the frame budget grows with it so the + /// display uploads keep their usual share. + #[test] + fn budgets_grow_once_the_user_is_idle() { + let (composite, frame) = frame_budgets(IDLE_FRAMES_BEFORE_BOOST); + assert_eq!(composite, IDLE_COMPOSITE_BUDGET_MS); + assert!(composite > COMPOSITE_BUDGET_MS); + assert_eq!(frame - composite, FRAME_BUDGET_MS - COMPOSITE_BUDGET_MS, "the non-compositing share of a frame is unchanged"); + assert_eq!(frame_budgets(u32::MAX), (composite, frame), "stays boosted"); + } +} diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index f3087537..be6c87da 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -317,7 +317,11 @@ fn add_callback(app: &mut TextureCraftApp, ui: &Ui, rect: Rect, mut frame: Frame let scene = match (sync.0.take(), app.gpu.as_mut()) { (Some(aux), Some(gpu_link)) => { let sync_start_ms = crate::now_ms(); - let scene = gpu_link.sync(&mut app.session, aux, bake, u32::from(app.ui.prefs.viewport_texture.min(2)), f64::from(app.profiler.logic_ms)); + // Pointer, key and scroll input all count: the compositor then keeps frames short. + let user_active = + ui.ctx().input(|i| i.pointer.is_moving() || i.pointer.any_down() || !i.events.is_empty() || i.smooth_scroll_delta != egui::Vec2::ZERO); + let scene = + gpu_link.sync(&mut app.session, aux, bake, u32::from(app.ui.prefs.viewport_texture.min(2)), f64::from(app.profiler.logic_ms), user_active); let reports = gpu_link.take_reports(); app.profiler.sync_ms += (crate::now_ms() - sync_start_ms) as f32; gpu_report_toasts(app, reports);