From c205773d928be8f277f5ba0738d7222cfb2d8284 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Tue, 17 Nov 2026 03:27:27 +0100 Subject: [PATCH] Caching: memory is counted where it is held, and whole layers stay whole under a viewer region (#3301 review) - The memory accountant is offered only layer buffers and preview frames nobody else holds, so what it counts as freed is freed; the frame cache's recycled buffers count and go first. - A Cryptomatte plane over the matte cache's own limit is kept only where the common budget trims it. - Disk cache entries read back take their buffers fallibly: memory the system can't give is a miss. - The Layer panel, sampleImage and effect inputs read whole footage frames under a viewer region of interest. - A 16K Cryptomatte source at half resolution is box-filtered, not looked up nearest-neighbour. - The System Compatibility Report shows the cache budget in force. - A Project panel thumbnail render never starts under a key held back from an earlier state. - Convert Expression to Keyframes makes no keys on a property that can't be animated. --- crates/effects/src/channel3d.rs | 23 +++++++++--- crates/effects/src/channel3d/tests.rs | 14 ++++++++ crates/engine/src/cmd/app_more.rs | 4 +-- crates/engine/src/cmd/prop.rs | 11 ++++-- crates/engine/src/tests/aux_footage.rs | 24 +++++++++++++ crates/engine/src/tests/m145.rs | 14 ++++++++ crates/gpu/src/fx_depth/matte_cache.rs | 18 +++++++++- crates/gpu/src/tests/fx_depth.rs | 20 +++++++++++ crates/media/src/pool.rs | 48 +++++++++++++++++++++++-- crates/render/src/cache.rs | 31 ++++++++++++++-- crates/render/src/disk_cache.rs | 47 +++++++++++++++++------- crates/render/src/pipeline.rs | 3 +- crates/render/src/tests/footage_size.rs | 4 +++ crates/ui-egui/src/frames.rs | 22 ++++++++---- crates/ui-egui/src/frames/tests.rs | 26 ++++++++++++++ crates/ui-egui/src/panels/project.rs | 48 +++++++++++++++++++++---- 16 files changed, 315 insertions(+), 42 deletions(-) diff --git a/crates/effects/src/channel3d.rs b/crates/effects/src/channel3d.rs index 0b05cba76..b2d821cb4 100644 --- a/crates/effects/src/channel3d.rs +++ b/crates/effects/src/channel3d.rs @@ -725,6 +725,13 @@ fn box_axis(layer_len: usize, src_len: usize, src_scale: f64, k: f64, out_len: u /// 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 { + crypto_box_within(src, s, MAX_BOX_PIXELS) +} + +/// [`crypto_box`] with a result of at most `max_out` pixels. The filter's work, a visit of every +/// layer pixel, is bounded by the source's size alone, which is in memory already: a 16K source +/// at half resolution is filtered, not looked up nearest-neighbour. +fn crypto_box_within(src: CryptoPlane, s: f64, max_out: usize) -> CryptoPlane { if !(src.scale.is_finite() && src.scale > 0.0) { return src; } @@ -734,18 +741,24 @@ fn crypto_box(src: CryptoPlane, s: f64) -> CryptoPlane { 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 { + // (the filter visits every layer pixel: at most four per source pixel; a footage item + // claiming a far larger size than its file is left unscaled) + let work = (src.width as usize).saturating_mul(src.height as usize).saturating_mul(4); + let fits = |v: f64| v.is_finite() && v >= 0.0 && v <= work as f64; + if !(fits(lw) && fits(lh) && fits(dw) && fits(dh)) || (dw * dh) as usize > work.min(max_out) || (lw * lh) as usize > work { 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 n = dw.saturating_mul(dh).saturating_mul(ch); + let mut data = Vec::new(); + if data.try_reserve_exact(n).is_err() { + return src; + } + data.resize(n, 0.0f32); 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 { diff --git a/crates/effects/src/channel3d/tests.rs b/crates/effects/src/channel3d/tests.rs index 0892b96f9..c1aba5265 100644 --- a/crates/effects/src/channel3d/tests.rs +++ b/crates/effects/src/channel3d/tests.rs @@ -403,6 +403,20 @@ fn box_filter_survives_hostile_scales() { assert!(t.elapsed() < std::time::Duration::from_secs(1)); } +/// #3301 review: a source whose layer has more pixels than a filtered plane may have (a 16K +/// file at half resolution) is still filtered when the result fits: alternating selected and +/// unselected rows give 0.5, not the 0 of a nearest-neighbour lookup of every second row. +#[test] +fn a_large_source_is_filtered_when_its_result_fits() { + let (w, h) = (8usize, 8usize); + let data = (0..w * h).map(|i| if (i / w) % 2 == 0 { 1.0 } else { 0.0 }).collect(); + let src = CryptoPlane { width: w as u32, height: h as u32, scale: 1.0, output: CryptoOutput::Matte, data }; + // (at most 16 output pixels: the 4 × 4 result fits, the 64 layer pixels don't) + let p = crypto_box_within(src, 0.5, 16); + assert_eq!((p.width, p.height), (4, 4), "filtered to half resolution"); + assert!(p.data.iter().all(|m| (m - 0.5).abs() < 1e-6), "{:?}", p.data); +} + /// 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/engine/src/cmd/app_more.rs b/crates/engine/src/cmd/app_more.rs index be52fb232..faec31877 100644 --- a/crates/engine/src/cmd/app_more.rs +++ b/crates/engine/src/cmd/app_more.rs @@ -424,8 +424,8 @@ pub fn system_report(s: &Session) -> Value { let gb = |b: u64| format!("{:.1} GB", b as f64 / (1u64 << 30) as f64); let export: Vec<&str> = s.exporter.as_ref().map(|e| e.formats().into_iter().map(|f| f.label()).collect()).unwrap_or_default(); let gpu = s.accel.as_ref().map(|a| a.name()); - let read = mem.or(s.sys_memory); - let budget = s.prefs.memory_budget(read, read.is_some_and(|m| m.still_low(s.memory_low))); + // (the budget in force: from the reading the session applied, not this fresh one) + let budget = s.memory_budget(); let mut issues = vec![]; if gpu.is_none() { issues.push(match &s.accel_note { diff --git a/crates/engine/src/cmd/prop.rs b/crates/engine/src/cmd/prop.rs index eca458de8..3a1cf38c3 100644 --- a/crates/engine/src/cmd/prop.rs +++ b/crates/engine/src/cmd/prop.rs @@ -1448,8 +1448,11 @@ fn convert_expr_to_keys(s: &mut Session, p: &Value) -> Result { if p.get("path").is_none() && p.get("prop").is_none() && p.get("layer").is_none() { let cid = comp_id(s, p)?; let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; - let sel: Vec<(LayerId, Uid)> = - selected_leaf_props(s).into_iter().filter(|(l, u)| comp.layer(*l).and_then(|l| l.props.find(*u)).is_some_and(Property::has_expression)).collect(); + // (a property that can't be animated is left out, as Add Keyframe leaves it out) + let sel: Vec<(LayerId, Uid)> = selected_leaf_props(s) + .into_iter() + .filter(|(l, u)| comp.layer(*l).and_then(|l| l.props.find(*u)).is_some_and(|pr| pr.has_expression() && !pr.static_only)) + .collect(); if sel.is_empty() { return Err(bad(CMD, "no `path` given and no selected property has an expression")); } @@ -1497,6 +1500,10 @@ fn convert_one_expr(s: &mut Session, cid: ItemId, lid: LayerId, uid: Uid) -> Res let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone(); let layer = comp.layer(lid).ok_or(EngineError::NoComp)?.clone(); let pr = layer.props.find(uid).ok_or_else(|| bad("prop.convertExpressionToKeyframes", "no property"))?.clone(); + // (an expression an older document left on a property that can't be animated: no keys) + if pr.static_only { + return Err(bad("prop.convertExpressionToKeyframes", format!("{} can't be animated", pr.name))); + } let fd = comp.frame_duration(); check_convertible(fd.0, &layer)?; let mut keys = vec![]; diff --git a/crates/engine/src/tests/aux_footage.rs b/crates/engine/src/tests/aux_footage.rs index 5dde55da9..069ff7013 100644 --- a/crates/engine/src/tests/aux_footage.rs +++ b/crates/engine/src/tests/aux_footage.rs @@ -297,6 +297,30 @@ fn a_channel_selections_old_expression_can_be_removed() { assert!(expr(&s).is_none(), "removed"); } +/// #3301 review: Convert Expression to Keyframes makes no keys on a channel selection that has an +/// expression from an older document: named, it is refused; in a selection, it is left out. +#[test] +fn a_channel_selections_old_expression_is_not_converted_to_keys() { + let rec = Arc::new(Recorder::default()); + let mut s = session(&rec); + let item = import(&mut s, "/renders/old-expr-keys.exr"); + let l = add_layer(&mut s, &item); + s.execute_checked("effect.apply", json!({"effect": "EXtractoR", "layers": [l]})).unwrap(); + let cid = s.active_comp_id().unwrap(); + let red = "effects/#1/red"; + { + let p = Arc::make_mut(&mut s.project); + let pr = p.comp_mut(cid).unwrap().layer_mut(effectcraft_project::LayerId(l)).unwrap().props.prop_mut(red).unwrap(); + pr.expr = Some(effectcraft_project::Expression { text: "'Z'".into(), enabled: true }); + } + let keys = |s: &Session| s.active_comp().unwrap().layer(effectcraft_project::LayerId(l)).unwrap().props.prop(red).unwrap().keys.len(); + assert!(s.execute_checked("prop.convertExpressionToKeyframes", json!({"layer": l, "path": red})).is_err(), "named: refused"); + assert_eq!(keys(&s), 0); + s.execute_checked("prop.select", json!({"layer": l, "path": red})).unwrap(); + let _ = s.execute_checked("prop.convertExpressionToKeyframes", json!({})); + assert_eq!(keys(&s), 0, "selected: left out"); +} + /// An EXtractoR layer of `path` in a fresh session; the session and the layer. fn extractor_layer(rec: &Arc, path: &str) -> (Session, u64) { let mut s = session(rec); diff --git a/crates/engine/src/tests/m145.rs b/crates/engine/src/tests/m145.rs index 07e2a947f..c394a92cf 100644 --- a/crates/engine/src/tests/m145.rs +++ b/crates/engine/src/tests/m145.rs @@ -231,6 +231,20 @@ fn low_memory_budgets_hold_until_the_system_recovers() { assert!(!s.memory_budget().reduced, "recovered"); } +/// #3301 review: the System Compatibility Report shows the cache budget in force, the one the +/// session applied from its last reading, not one worked out from a fresh reading. +#[test] +fn the_report_shows_the_budget_in_force() { + use crate::sysinfo::SysMemory; + let mut s = Session::default(); + // (a reading no machine running the test has: 3 GiB, of which 0.1 GiB is free, low) + let applied = s.take_memory_reading(Some(SysMemory { total: 3 << 30, available: 100 << 20 })); + let report = crate::cmd::app_more::system_report(&s); + let gb = format!("{:.1} GB", applied.bytes as f64 / (1u64 << 30) as f64); + assert_eq!(report["caches"]["budget"], gb.as_str(), "{report}"); + assert_eq!(report["caches"]["reduced"], applied.reduced); +} + /// The memory readings over a low-memory episode: the budget is halved once, stays halved while /// the system has not recovered (a reading between the low line and twice it), and the toast /// shows once; recovered, the budget is whole again, and the next episode toasts again. diff --git a/crates/gpu/src/fx_depth/matte_cache.rs b/crates/gpu/src/fx_depth/matte_cache.rs index a81f10553..28dc200d3 100644 --- a/crates/gpu/src/fx_depth/matte_cache.rs +++ b/crates/gpu/src/fx_depth/matte_cache.rs @@ -77,6 +77,11 @@ fn register() { /// 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 { + plane_within(a, layer, sel, output, render_scale, MAX_BYTES, Accountant::global().budget()) +} + +/// [`plane`] with at most `max_bytes` kept, and the common `budget` (`usize::MAX`: none). +fn plane_within(a: &Arc, layer: &str, sel: &[u32], output: CryptoOutput, render_scale: f64, max_bytes: usize, budget: usize) -> 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) }; { @@ -97,10 +102,15 @@ pub(crate) fn plane(a: &Arc, layer: &str, sel: &[u32], output: Cryp // (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)); + // A plane over MAX_BYTES on its own (a 16K Colors plane) is kept only where the common + // budget trims it: a render without a budget hands it out and keeps nothing. + if bytes > max_bytes && (budget == usize::MAX || bytes > budget) { + return made; + } 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) { + if c.entries.len() <= 1 || (c.entries.len() <= MAX_ENTRIES && total <= max_bytes) { break; } let Some(i) = oldest(&c) else { break }; @@ -143,6 +153,12 @@ impl MemoryHolder for Holder { } } +/// [`plane_within`] (tests). +#[cfg(test)] +pub(crate) fn plane_held_to(a: &Arc, layer: &str, sel: &[u32], output: CryptoOutput, max_bytes: usize, budget: usize) -> Arc { + plane_within(a, layer, sel, output, 1.0, max_bytes, budget) +} + /// Planes made so far (tests). #[cfg(test)] pub(crate) fn made() -> u64 { diff --git a/crates/gpu/src/tests/fx_depth.rs b/crates/gpu/src/tests/fx_depth.rs index 1eb122d33..9e53d10a3 100644 --- a/crates/gpu/src/tests/fx_depth.rs +++ b/crates/gpu/src/tests/fx_depth.rs @@ -392,6 +392,26 @@ fn cryptomatte_planes_are_kept_per_frame_and_selection() { assert!(Arc::ptr_eq(&one, &matte_cache::plane(&a, "CryptoObject", &sel, CryptoOutput::Matte, 1.0)), "the live frame's plane stays"); } +/// #3301 review: a plane over the cache's own byte limit is not kept by a render without a +/// common budget (nothing would trim it); with a budget that holds it, it is kept and the budget +/// trims it. +#[test] +fn a_plane_over_the_byte_limit_is_kept_only_under_a_budget() { + use crate::fx_depth::matte_cache; + use effectcraft_effects::CryptoOutput; + crate::tests::hold_gpu_lock(); + let sel = vec![crypto_hash("beta")]; + let a = Arc::new(aux_scene(70, 44, 1.0)); + // (one Matte plane: 70 × 44 × 4 bytes, over a 100-byte limit) + let first = matte_cache::plane_held_to(&a, "CryptoObject", &sel, CryptoOutput::Matte, 100, usize::MAX); + let again = matte_cache::plane_held_to(&a, "CryptoObject", &sel, CryptoOutput::Matte, 100, usize::MAX); + assert!(!Arc::ptr_eq(&first, &again), "no budget: an oversized plane is not kept"); + let b = Arc::new(aux_scene(70, 44, 1.0)); + let first = matte_cache::plane_held_to(&b, "CryptoObject", &sel, CryptoOutput::Matte, 100, 1 << 30); + let again = matte_cache::plane_held_to(&b, "CryptoObject", &sel, CryptoOutput::Matte, 100, 1 << 30); + assert!(Arc::ptr_eq(&first, &again), "a budget that holds it: kept"); +} + #[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/media/src/pool.rs b/crates/media/src/pool.rs index 7b3644722..c20e546d2 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -214,6 +214,14 @@ impl Lru { self.remove(&k); had.saturating_sub(self.bytes) } + /// The bytes of the recycled buffers ([`Lru::spare`]). + fn spare_bytes(&self) -> usize { + self.spare.iter().map(|b| b.len().saturating_mul(std::mem::size_of::())).fold(0, usize::saturating_add) + } + /// Let one recycled buffer go; the bytes freed. + fn drop_spare(&mut self) -> usize { + self.spare.pop().map_or(0, |b| b.len().saturating_mul(std::mem::size_of::()).max(1)) + } /// A recycled buffer of exactly `len` pixels, or an empty Vec. fn take_spare(&mut self, len: usize) -> Vec { match self.spare.iter().position(|b| b.len() == len) { @@ -364,9 +372,10 @@ impl CacheState { } } - /// The bytes the frame cache and the whole stills beside it hold (the memory holder's). + /// The bytes the frame cache, its recycled buffers and the whole stills beside it hold (the + /// memory holder's). fn held_bytes(&self) -> usize { - self.lru.bytes.saturating_add(self.recent_bytes()) + self.lru.bytes.saturating_add(self.lru.spare_bytes()).saturating_add(self.recent_bytes()) } /// The bytes of the whole stills [`CacheState::recent`] holds. @@ -1730,10 +1739,14 @@ impl effectcraft_render::memory::MemoryHolder for Inner { } /// The older of the least recently used frame and the oldest whole still kept beside the /// frames (#3271: whichever was used last goes first); a whole still is kept for a moment - /// only, to resample other sizes, so it is offered as worth nothing. + /// only, to resample other sizes, so it is offered as worth nothing. Recycled buffers of + /// frames dropped go before either, at no cost. fn oldest(&self) -> Option { use effectcraft_render::memory::Candidate; let c = lock(&self.cache); + if let Some(b) = c.lru.spare.last() { + return Some(Candidate { used: 0, bytes: b.len().saturating_mul(std::mem::size_of::()).max(1), cost_us: 0, protected: false }); + } let frame = c.lru.oldest().and_then(|(k, _)| c.lru.map.get(k)).map(|e| Candidate { used: e.used, bytes: e.bytes, cost_us: e.cost_us, protected: false }); let still = c.recent.front().map(|r| Candidate { used: r.used, bytes: r.bytes.max(1), cost_us: 0, protected: false }); @@ -1744,6 +1757,9 @@ impl effectcraft_render::memory::MemoryHolder for Inner { } fn drop_oldest(&self) -> usize { let mut c = lock(&self.cache); + if !c.lru.spare.is_empty() { + return c.lru.drop_spare(); + } let frame = c.lru.oldest().map(|(_, t)| t); let still = c.recent.front().map(|r| r.used); match (frame, still) { @@ -2141,6 +2157,32 @@ mod tests { assert!(c.take_spare(17).is_empty()); } + /// #3301 review: the recycled buffers are in the memory budget: counted, and the first the + /// accountant lets go, at no cost, before any cached frame. + #[test] + fn recycled_buffers_are_held_bytes_and_go_first() { + let pool = MediaPool::unregistered(usize::MAX); + let holder = pool.memory_holder(); + let px = 256 * std::mem::size_of::(); + { + let mut c = lock(&pool.inner.cache); + let k = |i| Key { path: "a".into(), frame: i, interp: 0, matte: [0; 3], size: (0, 0), nominal: (0, 0), rows: None, q8: false }; + for i in 0..3 { + c.lru.insert(k(i), Pixels::F32(Arc::new(Image::new(16, 16))), Arc::default(), usize::MAX); + } + c.lru.spare.push(vec![[0.0; 4]; 256]); + c.lru.spare.push(vec![[0.0; 4]; 256]); + } + let frames = 3 * (px + 64); + assert_eq!(holder.bytes(), frames + 2 * px, "frames and recycled buffers"); + let next = holder.oldest().unwrap(); + assert_eq!((next.bytes, next.cost_us), (px, 0), "a recycled buffer first, worth nothing"); + assert_eq!(holder.drop_oldest(), px); + assert_eq!(holder.drop_oldest(), px); + assert_eq!(holder.bytes(), frames, "the frames stay until the buffers are gone"); + assert_eq!(holder.drop_oldest(), px + 64, "then the oldest frame"); + } + /// A frame kept as 8-bit levels is its own entry beside the float one, counted at 4 bytes /// a pixel. #[test] diff --git a/crates/render/src/cache.rs b/crates/render/src/cache.rs index 814a4f619..06ed04b75 100644 --- a/crates/render/src/cache.rs +++ b/crates/render/src/cache.rs @@ -90,6 +90,12 @@ impl Inner { self.order.first_key_value().map(|(_, k)| *k) } + /// The least recently used entry nobody else holds (a render using its buffer): what the + /// memory accountant may drop to free memory now. + fn oldest_free_key(&self) -> Option { + self.order.values().copied().find(|k| self.map.get(k).is_some_and(|e| Arc::strong_count(&e.buf) == 1)) + } + /// Mark the entry under `key` as used at `now` (the clock), for `order` and the accountant. fn touch(&mut self, key: u64, now: u64) { let Some(e) = self.map.get_mut(&key) else { return }; @@ -1060,15 +1066,16 @@ impl crate::memory::MemoryHolder for LayerCache { fn bytes(&self) -> usize { self.inner.lock().map(|g| g.bytes).unwrap_or(0) } - /// Its least recently used buffer, with what rendering it took. + /// Its least recently used buffer nobody else holds (dropping one a render holds would free + /// nothing yet), with what rendering it took. fn oldest(&self) -> Option { let g = self.inner.lock().ok()?; - let e = g.map.get(&g.oldest_key()?)?; + let e = g.map.get(&g.oldest_free_key()?)?; Some(crate::memory::Candidate { used: e.used, bytes: e.bytes, cost_us: e.cost_us, protected: false }) } fn drop_oldest(&self) -> usize { let Ok(mut g) = self.inner.lock() else { return 0 }; - match g.oldest_key() { + match g.oldest_free_key() { Some(k) => g.evict(k), None => 0, } @@ -1086,6 +1093,24 @@ mod shared_insert_tests { use effectcraft_effects::Buf; use effectcraft_raster::Image; + /// #3301 review: the memory accountant is offered only buffers nobody else holds; dropping + /// one a render still holds would free nothing, and its bytes would be counted as freed. + #[test] + fn a_buffer_held_elsewhere_is_not_offered_to_the_accountant() { + use crate::memory::MemoryHolder; + let cache = LayerCache::default(); + let buf = || Arc::new(Buf { img: Image::new(4, 4), offset: [0.0; 2], scale: 1.0 }); + let held = cache.insert(1, buf(), Default::default()).0; + drop(cache.insert(2, buf(), Default::default())); + let bytes = cache.bytes(); + let freed = cache.drop_oldest(); + assert!(freed > 0 && cache.get(1).is_some() && cache.get(2).is_none(), "the free one goes, the held one stays"); + assert_eq!(cache.bytes(), bytes - freed); + assert!(cache.oldest().is_none() && cache.drop_oldest() == 0, "only a held buffer left"); + drop(held); + assert!(cache.drop_oldest() > 0, "let go: offered"); + } + /// Two renders of one layer kept under one key: both use the buffer kept first. #[test] fn the_buffer_kept_first_is_shared() { diff --git a/crates/render/src/disk_cache.rs b/crates/render/src/disk_cache.rs index c3f6aa9f5..50b472305 100644 --- a/crates/render/src/disk_cache.rs +++ b/crates/render/src/disk_cache.rs @@ -1727,7 +1727,19 @@ fn decompress_checked(p: &[u8], expected: Option) -> Option> { return None; } // A block that ends short of the size it states is damaged: callers index by that size. - lz4_flex::decompress(p.get(4..)?, size).ok().filter(|raw| raw.len() == size) + // (memory the system can't give is a miss, not an abort) + let mut raw = zeroed(size, 0u8)?; + let got = lz4_flex::block::decompress_into(p.get(4..)?, &mut raw).ok()?; + (got == size).then_some(raw) +} + +/// `n` copies of `v`, or `None` when the system can't give the memory: a cache entry read back +/// then counts as a miss. +fn zeroed(n: usize, v: T) -> Option> { + let mut out = Vec::new(); + out.try_reserve_exact(n).ok()?; + out.resize(n, v); + Some(out) } fn decode_frame(p: &[u8]) -> Option { @@ -1774,7 +1786,7 @@ fn decode_frame_f16(p: &[u8]) -> Option { let h = u32::from_le_bytes(p[4..8].try_into().ok()?); let n = (w as usize).checked_mul(h as usize)?; let planes = decompress_checked(&p[8..], Some(n.checked_mul(8)?))?; - let mut rgba = vec![0u16; n * 4]; + let mut rgba = zeroed(n.checked_mul(4)?, 0u16)?; for (i, px) in rgba.as_chunks_mut::<4>().0.iter_mut().enumerate() { for (c, v) in px.iter_mut().enumerate() { *v = u16::from_le_bytes([planes[c * n + i], planes[(4 + c) * n + i]]); @@ -1848,22 +1860,24 @@ fn decode_plane(p: &[u8], expect: (u32, u32)) -> Option<(String, Vec)> { if u32::from_le_bytes(body.get(..4)?.try_into().ok()?) as usize != want { return None; } - let s = lz4_flex::decompress_size_prepended(body).ok()?; - if s.len() != want { - return None; - } - let plane = if width == 2 { + // (every buffer taken fallibly: a plane the system can't give memory for is a miss) + let s = decompress_checked(body, Some(want))?; + let mut plane = zeroed(n, 0.0f32)?; + if width == 2 { let (lo, hi) = s.split_at_checked(n)?; - let halves: Vec = lo.iter().zip(hi).map(|(l, h)| half::f16::from_bits(u16::from_le_bytes([*l, *h]))).collect(); - let mut plane = vec![0.0; n]; + let mut halves = zeroed(n, half::f16::ZERO)?; + for ((x, l), h) in halves.iter_mut().zip(lo).zip(hi) { + *x = half::f16::from_bits(u16::from_le_bytes([*l, *h])); + } half::slice::HalfFloatSliceExt::convert_to_f32_slice(halves.as_slice(), &mut plane); - plane } else { let (b0, rest) = s.split_at_checked(n)?; let (b1, rest) = rest.split_at_checked(n)?; let (b2, b3) = rest.split_at_checked(n)?; - b0.iter().zip(b1).zip(b2).zip(b3).map(|(((a, b), c), d)| f32::from_bits(u32::from_le_bytes([*a, *b, *c, *d]))).collect() - }; + for (o, (((a, b), c), d)) in plane.iter_mut().zip(b0.iter().zip(b1).zip(b2).zip(b3)) { + *o = f32::from_bits(u32::from_le_bytes([*a, *b, *c, *d])); + } + } Some((name, plane)) } @@ -2033,6 +2047,15 @@ pub fn frame_key(content: u128, frame: i64, scale_milli: u32, view: u64) -> u128 mod tests { use super::*; + /// #3301 review: buffers for entries read back are taken fallibly; memory the system can't + /// give is a miss, not an abort. + #[test] + fn memory_that_cannot_be_had_is_a_miss() { + assert!(zeroed(usize::MAX, 0u8).is_none()); + assert!(zeroed(usize::MAX / 2, 0.0f32).is_none()); + assert_eq!(zeroed(3, 7u16), Some(vec![7; 3])); + } + #[cfg(unix)] #[test] fn a_cache_folder_in_the_temp_folder_is_made_private_and_never_a_link() { diff --git a/crates/render/src/pipeline.rs b/crates/render/src/pipeline.rs index 2a2b4dd82..36e08878a 100644 --- a/crates/render/src/pipeline.rs +++ b/crates/render/src/pipeline.rs @@ -1238,7 +1238,8 @@ impl<'a> Renderer<'a> { /// declare its margin, or anything in the comp that reads layers' pixels (an effect's layer /// parameter, an expression). pub(crate) fn footage_rows(&self, ctx: &EvalCtx, layer: &Layer, f: &Footage) -> Option<(i64, i64)> { - let roi = self.opts.roi.filter(|_| self.top)?; + // (a caller that needs the whole layer: the Layer panel, sampleImage, effect inputs) + let roi = self.opts.roi.filter(|_| self.top && !self.whole_raster)?; // (a collapsed precomp's layers are placed through the outer layers too, and an // adjustment layer's footprint is keyed whole: the whole frame there) if self.outer.is_some() || self.collapse3d.is_some() || self.depth > 0 || layer.switches.adjustment { diff --git a/crates/render/src/tests/footage_size.rs b/crates/render/src/tests/footage_size.rs index 49c2a23ec..1346609b7 100644 --- a/crates/render/src/tests/footage_size.rs +++ b/crates/render/src/tests/footage_size.rs @@ -219,6 +219,10 @@ fn a_footage_layer_read_by_rows_is_not_the_shared_picture() { let Some(ItemKind::Footage(f)) = p.item(item).map(|it| &it.kind) else { panic!("footage") }; assert!(region.footage_rows(&ctx, &comp.layers[0], f).is_some(), "read by rows"); assert!(region.footage_picture(&ctx, &comp.layers[0]).is_none(), "a part of the frame is never shared"); + // #3301 review: a caller that needs the whole layer (Layer panel, sampleImage, effect inputs) + // reads the whole frame under a region too. + let whole_layer = Renderer { whole_raster: true, ..region }; + assert!(whole_layer.footage_rows(&ctx, &comp.layers[0], f).is_none(), "whole layer: the whole frame"); } /// #3271 review: a render's channel reads (an effect's EXR channels) are called off with the diff --git a/crates/ui-egui/src/frames.rs b/crates/ui-egui/src/frames.rs index 985bfee8b..ea4d7dbc0 100644 --- a/crates/ui-egui/src/frames.rs +++ b/crates/ui-egui/src/frames.rs @@ -172,6 +172,15 @@ impl FrameImage { fn is_gpu(&self) -> bool { matches!(self, FrameImage::Gpu(_)) } + /// Held outside the cache too (a viewer showing it): dropping the cache's entry frees + /// nothing yet. + fn held_elsewhere(&self) -> bool { + match self { + FrameImage::Cpu(i) => Arc::strong_count(i) > 1, + FrameImage::CpuF16(i) => Arc::strong_count(i) > 1, + FrameImage::Gpu(i) => Arc::strong_count(i) > 1, + } + } } /// A viewer frame: its comp's content, the frame, scale, view and render options. `revision` is @@ -364,9 +373,10 @@ impl Cache { } farthest.map(|(_, i)| i) } - /// [`Cache::victim`] among the frames in RAM (a GPU frame's bytes are video memory). - fn cpu_victim(&self) -> Option { - self.victim_of(|k| self.map.get(k).is_some_and(|c| !c.img.is_gpu())) + /// [`Cache::victim`] among the frames in RAM (a GPU frame's bytes are video memory) nobody + /// else holds: what the memory accountant may drop to free RAM now. + fn free_victim(&self) -> Option { + self.victim_of(|k| self.map.get(k).is_some_and(|c| !c.img.is_gpu() && !c.img.held_elsewhere())) } /// Video memory: drop the oldest GPU frames past their own budget, frames of another scale /// than the one on screen first (a zoom or resolution change). @@ -456,7 +466,7 @@ impl Cache { /// The preview frames (in RAM; GPU frames have their own budget) in the common memory budget /// (#3271, #3298): each frame is worth what rendering it took, per byte. The frame offered is -/// the one the cache would evict next ([`Cache::cpu_victim`]: it spares the frames a playing +/// the one the cache would evict next ([`Cache::free_victim`]: it spares the frames a playing /// preview shows next). struct PreviewMemory(Arc>); @@ -477,7 +487,7 @@ impl effectcraft_engine::render::memory::MemoryHolder for PreviewMemory { fn oldest(&self) -> Option { use effectcraft_engine::render::memory::{Candidate, DEFAULT_PREVIEW_US_PER_MB, default_cost}; let c = self.0.lock().ok()?; - let k = c.order.get(c.cpu_victim()?)?; + let k = c.order.get(c.free_victim()?)?; let f = c.map.get(k)?; let bytes = f.img.bytes(); let protected = c.playhead.as_ref().is_some_and(|p| p.ahead(k).is_some()); @@ -486,7 +496,7 @@ impl effectcraft_engine::render::memory::MemoryHolder for PreviewMemory { } fn drop_oldest(&self) -> usize { let Ok(mut c) = self.0.lock() else { return 0 }; - let Some(old) = c.cpu_victim().and_then(|i| c.order.remove(i)) else { return 0 }; + let Some(old) = c.free_victim().and_then(|i| c.order.remove(i)) else { return 0 }; let bytes = c.map.get(&old).map_or(0, |f| f.img.bytes()); c.remove(&old); bytes diff --git a/crates/ui-egui/src/frames/tests.rs b/crates/ui-egui/src/frames/tests.rs index 229d618cc..6632a70cc 100644 --- a/crates/ui-egui/src/frames/tests.rs +++ b/crates/ui-egui/src/frames/tests.rs @@ -3218,6 +3218,32 @@ fn the_accountant_trims_only_frames_in_ram() { assert_eq!(memory.bytes(), 16); } +/// #3301 review: a frame someone still holds (the viewer showing it, from `Frames::get`) is not +/// offered to the memory accountant: dropping the cache's entry would free nothing, and its bytes +/// would be counted as freed. +#[test] +fn a_frame_held_elsewhere_is_not_offered_to_the_accountant() { + use effectcraft_engine::render::memory::MemoryHolder; + let frames = Frames::default(); + { + let mut c = frames.cache.lock().unwrap(); + c.insert(key(1, 0, 0), img()); + c.insert(key(1, 1, 0), img()); + } + let shown = frames.get(&key(1, 0, 0)).unwrap(); + // (the other one shown since and let go: the held frame is the least recently shown) + drop(frames.get(&key(1, 1, 0))); + let memory = PreviewMemory(frames.cache.clone()); + assert_eq!(memory.drop_oldest(), 16, "the frame nobody holds goes"); + { + let c = frames.cache.lock().unwrap(); + assert!(c.map.contains_key(&key(1, 0, 0)) && !c.map.contains_key(&key(1, 1, 0)), "the shown frame stays"); + } + assert!(memory.oldest().is_none() && memory.drop_oldest() == 0, "only a held frame left"); + drop(shown); + assert_eq!(memory.drop_oldest(), 16, "let go: offered"); +} + /// #3301: frames a playing preview shows next are protected: other caches give way first, even /// for items worth more, until the caches are more than the bounded overage over; past it the /// preview frames go too. A frame rendered quickly still costs a typical render. diff --git a/crates/ui-egui/src/panels/project.rs b/crates/ui-egui/src/panels/project.rs index edbb4cd73..57d2c60fa 100644 --- a/crates/ui-egui/src/panels/project.rs +++ b/crates/ui-egui/src/panels/project.rs @@ -145,8 +145,10 @@ pub fn forget_thumbnails(ctx: &egui::Context) { /// The content key of item `it`'s thumbnail at `t` ([`effectcraft_engine::Session::thumbnail_key`]), /// worked out again only when the project's revision, the footage reloads (Reload Footage or a /// watched file that changed: the project's revision stays), the preferences (Switches Affect -/// Nested Comps and real-time shadows change what a comp shows) or the time changed. -fn thumbnail_key(app: &EffectcraftApp, ctx: &egui::Context, it: &Item, t: effectcraft_engine::time::Tick) -> u128 { +/// Nested Comps and real-time shadows change what a comp shows) or the time changed. `false` +/// with it: the key is of an earlier state (held back while edits come fast), so no render of +/// the state now may be made under it. +fn thumbnail_key(app: &EffectcraftApp, ctx: &egui::Context, it: &Item, t: effectcraft_engine::time::Tick) -> (u128, bool) { let id = egui::Id::unique(("proj-thumb-key", it.id.0)); let rev = app.session.revision; let reloads = app.session.footage_reloads.load(std::sync::atomic::Ordering::Relaxed); @@ -154,18 +156,18 @@ fn thumbnail_key(app: &EffectcraftApp, ctx: &egui::Context, it: &Item, t: effect let now = ctx.input(|i| i.time); if let Some((r, n, pr, at, k, when)) = ctx.data(|d| d.get_temp::(id)) { if r == rev && n == reloads && pr == prefs && at == t.0 { - return k; + return (k, true); } // (the key hashes the comp's content: while edits come fast, at most every 0.25 s) if now - when < THUMB_KEY_EVERY { ctx.request_repaint_after(std::time::Duration::from_secs_f64(THUMB_KEY_EVERY)); - return k; + return (k, false); } } // (no key: the revision stands in, as before) let k = app.session.thumbnail_key(it.id, t, THUMB_SIDE).unwrap_or(u128::from(rev) | (1 << 127)); ctx.data_mut(|d| d.insert_temp::(id, (rev, reloads, prefs, t.0, k, now))); - k + (k, true) } /// A thumbnail's content key as last worked out: (project revision, footage reloads, preferences @@ -187,7 +189,7 @@ fn thumbnail(app: &EffectcraftApp, ctx: &egui::Context, it: &Item) -> Option = ctx.data(|d| d.get_temp(key)); let mut tex = cached.as_ref().map(|Thumb(_, t)| t.clone()); let have = cached.as_ref().map(|Thumb(r, _)| *r); @@ -218,7 +220,9 @@ fn thumbnail(app: &EffectcraftApp, ctx: &egui::Context, it: &Item) -> Option(pkey)).is_none(), "a closed project's render is awaited"); } + /// #3301 review: while edits come fast the thumbnail's key is held back (it is of an + /// earlier state); no render of the state now is started under it, so it can never be kept + /// as the earlier state's picture. The thumbnail shown stays. + #[test] + fn no_thumbnail_render_starts_under_a_held_back_key() { + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + let comp = ItemId( + app.session.execute("comp.new", serde_json::json!({"name": "C", "width": 8, "height": 6, "duration": 1})).unwrap()["comp"].as_u64().unwrap(), + ); + let ctx = egui::Context::default(); + let it = app.session.project.item(comp).unwrap().clone(); + let (key, pkey, kkey) = + (egui::Id::unique(("proj-thumb", comp.0)), egui::Id::unique(("proj-thumb-pending", comp.0)), egui::Id::unique(("proj-thumb-key", comp.0))); + let tex = ctx.load_texture("t", egui::ColorImage::from_rgba_unmultiplied([1, 1], &[255; 4]), egui::TextureOptions::LINEAR); + // A key worked out just now for an earlier revision, and a thumbnail of another state. + let now = ctx.input(|i| i.time); + let (rev, prefs) = (app.session.revision, app.session.prefs_revision); + ctx.data_mut(|d| { + d.insert_temp::(kkey, (rev.wrapping_sub(1), 0, prefs, 0, 7, now)); + d.insert_temp(key, Thumb(5, tex)); + }); + assert!(thumbnail(&app, &ctx, &it).is_some(), "the thumbnail shown stays"); + assert!(ctx.data(|d| d.get_temp::(pkey)).is_none(), "no render under a held-back key"); + // Worked out again (the key is current): the render starts. + ctx.data_mut(|d| d.remove::(kkey)); + thumbnail(&app, &ctx, &it); + assert!(ctx.data(|d| d.get_temp::(pkey)).is_some(), "a current key: rendered"); + app.session.execute("file.closeProject", serde_json::json!({})).unwrap(); + } + /// Long names show in full: the Name column fits them (it stopped at 260 points with "…") /// up to the panel's width less the Label column and, while optional columns show, room for /// them; a width the user dragged wins.