mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-19 16:45:28 +00:00
Merge disk-cache-frame-keys: second review round of steps 1 and 2
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
a1ea8c2282
7 files changed
+161
-15
No files matched your search
@@ -353,6 +353,8 @@ impl<'a> Renderer<'a> {
|
||||
return Some(b);
|
||||
}
|
||||
let (foot, files) = self.recorded(key, |r| {
|
||||
// (what the masks' expressions read, at shutter times too, goes to the buffer's files)
|
||||
let ctx = &r.with_reads(ctx);
|
||||
let mut foot = r.source(ctx, layer)?;
|
||||
let _ = masks::apply(ctx, layer, &mut foot, r.mask_blur(ctx, layer));
|
||||
Some(foot)
|
||||
|
||||
+27
-10
@@ -34,6 +34,17 @@ impl Shutter {
|
||||
}
|
||||
}
|
||||
|
||||
/// `files` (a content's) with the files of the buffers made of it that the layer cache kept:
|
||||
/// `files` itself when they are all its own.
|
||||
fn with_kept_files(files: Arc<crate::reads::ReadFiles>, kept: &[Arc<crate::reads::ReadFiles>]) -> Arc<crate::reads::ReadFiles> {
|
||||
if kept.iter().all(|f| Arc::ptr_eq(f, &files)) {
|
||||
return files;
|
||||
}
|
||||
let mut all = (*files).clone();
|
||||
kept.iter().for_each(|f| all.extend(f));
|
||||
Arc::new(all)
|
||||
}
|
||||
|
||||
impl<'a> Renderer<'a> {
|
||||
/// The layer with its styles: exterior passes and body (cached per pass).
|
||||
pub(crate) fn styled(&self, ctx: &EvalCtx, layer: &Layer, mut timing: Option<&mut LayerTiming>) -> Option<StyledLayer> {
|
||||
@@ -43,10 +54,22 @@ impl<'a> Renderer<'a> {
|
||||
}
|
||||
let modes = styles::pass_modes(ctx, layer);
|
||||
let key = ckey.map(|k| cache::styles_key(ctx, layer, k));
|
||||
if let Some((body, _)) = self.cached(key) {
|
||||
let passes: Option<Vec<_>> =
|
||||
modes.iter().enumerate().map(|(i, m)| self.cached(key.map(|k| cache::derive(k, i as u64 + 1))).map(|(b, _)| (b, *m))).collect();
|
||||
// (a cached body or pass may have been made of other files than the content: another
|
||||
// worker's, or one from disk; the styled layer lists the files of every buffer it holds)
|
||||
if let Some((body, body_files)) = self.cached(key) {
|
||||
let mut kept_files = vec![body_files];
|
||||
let passes: Option<Vec<_>> = modes
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, m)| {
|
||||
self.cached(key.map(|k| cache::derive(k, i as u64 + 1))).map(|(b, f)| {
|
||||
kept_files.push(f);
|
||||
(b, *m)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
if let Some(passes) = passes {
|
||||
let files = with_kept_files(files, &kept_files);
|
||||
return Some(StyledLayer { passes, body, content, key, ckey, plain: false, files });
|
||||
}
|
||||
}
|
||||
@@ -73,13 +96,7 @@ impl<'a> Renderer<'a> {
|
||||
(b, m)
|
||||
})
|
||||
.collect();
|
||||
let files = if kept_files.iter().all(|f| Arc::ptr_eq(f, &files)) {
|
||||
files
|
||||
} else {
|
||||
let mut all = (*files).clone();
|
||||
kept_files.iter().for_each(|f| all.extend(f));
|
||||
Arc::new(all)
|
||||
};
|
||||
let files = with_kept_files(files, &kept_files);
|
||||
Some(StyledLayer { passes, body, content, key, ckey, plain: false, files })
|
||||
}
|
||||
|
||||
|
||||
@@ -1066,6 +1066,8 @@ impl<'a> Renderer<'a> {
|
||||
return Some((b, key, files));
|
||||
}
|
||||
let (buf, files) = self.recorded(key, |r| {
|
||||
// (what expressions read while it is made, at shutter times too, goes to its files)
|
||||
let ctx = &r.with_reads(ctx);
|
||||
let buf = r.masked_source(ctx, layer)?;
|
||||
Some(r.apply_effects_timed(ctx, layer, buf, false, usize::MAX, timing.map(|t| &mut t.effects)))
|
||||
});
|
||||
@@ -1084,6 +1086,7 @@ impl<'a> Renderer<'a> {
|
||||
return Some(b);
|
||||
}
|
||||
let (buf, files) = self.recorded(key, |r| {
|
||||
let ctx = &r.with_reads(ctx);
|
||||
let mut buf = r.masked_source(ctx, layer)?;
|
||||
let origin = ctx.effect_bounds(layer).1;
|
||||
buf.rebase(origin);
|
||||
|
||||
@@ -482,6 +482,69 @@ impl crate::eval::ExprHost for ControllerRate {
|
||||
}
|
||||
}
|
||||
|
||||
/// An expression that reads a file (as `sampleImage` does, into [`EvalCtx::reads`]) and keeps
|
||||
/// the value.
|
||||
///
|
||||
/// [`EvalCtx::reads`]: crate::eval::EvalCtx::reads
|
||||
struct ReadsAFile;
|
||||
|
||||
impl crate::eval::ExprHost for ReadsAFile {
|
||||
fn eval(&self, ctx: &crate::eval::EvalCtx, _: &effectcraft_project::Layer, _: &effectcraft_project::Property, value: &Value) -> Result<Value, String> {
|
||||
if let Some(log) = ctx.reads {
|
||||
log.note(crate::reads::ReadFile::new("/sampled.png", crate::reads::FileStamp::Native { len: 3, modified: Some(1) }));
|
||||
}
|
||||
Ok(value.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// What expressions read while a cached buffer is made (a mask expression, which mask motion
|
||||
/// blur also runs at shutter times the cache key doesn't see) goes to that buffer's files, not
|
||||
/// only to the frame's (#3184): a later render served the buffer lists the file too. Content,
|
||||
/// a Time effect's input and an adjustment layer's footprint.
|
||||
#[test]
|
||||
fn what_expressions_read_goes_to_the_files_of_the_buffer_they_make() {
|
||||
use crate::reads::ReadLog;
|
||||
use crate::{NoFootage, RenderOpts, Renderer};
|
||||
let (mut p, cid, comp) = setup();
|
||||
for adjustment in [false, true] {
|
||||
let mut l = solid(&mut p, &comp, [1.0, 1.0, 1.0], 200, 100);
|
||||
l.switches.adjustment = adjustment;
|
||||
let mut next = p.next_id;
|
||||
let m = build::mask(&mut Ids(&mut next), "Mask 1", ShapePath::rect([50.0, 50.0], 40.0, 40.0), MaskMode::Add, [255, 255, 0]);
|
||||
p.next_id = next;
|
||||
l.props.sub_mut("masks").unwrap().children.push(m.into());
|
||||
l.props.prop_mut("masks/#1/path").unwrap().expr = Some(effectcraft_project::Expression { text: "sampled".into(), enabled: true });
|
||||
p.comp_mut(cid).unwrap().layers.push(l);
|
||||
}
|
||||
let cache = crate::LayerCache::default();
|
||||
let c = p.comp(cid).unwrap();
|
||||
let (content, adjustment) = (&c.layers[0], &c.layers[1]);
|
||||
// `make`, rendered once with the expressions (the files go to `log`) and then served from
|
||||
// the cache to a render without them: the files that render lists.
|
||||
let files_served = |what: &str, make: &dyn Fn(&Renderer, &crate::eval::EvalCtx) -> bool| {
|
||||
let log = ReadLog::new();
|
||||
let mut r = Renderer::new(&p, &NoFootage, RenderOpts::default());
|
||||
r.expr = Some(&ReadsAFile);
|
||||
r.cache = Some(&cache);
|
||||
r.reads = Some(&log);
|
||||
assert!(make(&r, &r.ctx(cid, c, Tick::ZERO)), "{what}");
|
||||
assert!(log.files().get("/sampled.png").is_some(), "{what}: the frame lists the file");
|
||||
let hits = cache.stats().hits;
|
||||
let served = ReadLog::new();
|
||||
r.expr = None;
|
||||
r.reads = Some(&served);
|
||||
assert!(make(&r, &r.ctx(cid, c, Tick::ZERO)), "{what}");
|
||||
assert!(cache.stats().hits > hits, "{what}: served from the cache");
|
||||
served.into_files()
|
||||
};
|
||||
let files = files_served("content", &|r, ctx| r.content_buf(ctx, content).is_some());
|
||||
assert!(files.get("/sampled.png").is_some(), "content: {files:?}");
|
||||
let files = files_served("input", &|r, ctx| r.layer_input(ctx, content, 0).is_some());
|
||||
assert!(files.get("/sampled.png").is_some(), "input: {files:?}");
|
||||
let files = files_served("footprint", &|r, ctx| r.adjustment_footprint(ctx, adjustment).is_some());
|
||||
assert!(files.get("/sampled.png").is_some(), "footprint: {files:?}");
|
||||
}
|
||||
|
||||
/// A Birth Rate expression reading another layer: changing that layer changes the history but
|
||||
/// not the current rate (0) nor this layer's properties, which is all the layer cache key sees.
|
||||
/// CC Particle World layers with expressions therefore aren't layer cached.
|
||||
|
||||
@@ -507,3 +507,34 @@ fn workers_styling_a_layer_at_once_share_its_buffers() {
|
||||
assert!(st.passes.iter().zip(&first.passes).all(|(a, b)| std::sync::Arc::ptr_eq(&a.0, &b.0)), "one set of passes");
|
||||
}
|
||||
}
|
||||
|
||||
/// A styled layer served from the cache lists the files of the body and passes it was served,
|
||||
/// not only its content's (#3184): one made of other files (another worker's, or from disk,
|
||||
/// whose list can't vouch for them) passes that on to the buffers made of it (flattened, in the
|
||||
/// blending space), which are then not kept on disk as complete.
|
||||
#[test]
|
||||
fn a_cached_styled_layer_lists_the_files_of_its_cached_buffers() {
|
||||
let (p, cid) = scene([1.0; 3], None, |p, c, l| add(p, c, l, "dropShadow"));
|
||||
let layer = &p.comp(cid).unwrap().layers[0];
|
||||
let styled = |cache: &crate::LayerCache| {
|
||||
let mut r = Renderer::new(&p, &NoFootage, RenderOpts::default());
|
||||
r.cache = Some(cache);
|
||||
let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap();
|
||||
r.styled_layer(&ctx, layer).unwrap()
|
||||
};
|
||||
let first = styled(&crate::LayerCache::default());
|
||||
assert!(first.files.is_complete() && !first.passes.is_empty());
|
||||
let key = first.key.unwrap();
|
||||
// Body and passes kept with a list that can't vouch for them; the content renders again.
|
||||
let cache = crate::LayerCache::default();
|
||||
let mut unsure = crate::reads::ReadFiles::default();
|
||||
unsure.mark_incomplete();
|
||||
let unsure = std::sync::Arc::new(unsure);
|
||||
cache.insert(key, first.body.clone(), unsure.clone());
|
||||
for (i, (b, _)) in first.passes.iter().enumerate() {
|
||||
cache.insert(crate::cache::derive(key, i as u64 + 1), b.clone(), unsure.clone());
|
||||
}
|
||||
let hit = styled(&cache);
|
||||
assert!(std::sync::Arc::ptr_eq(&hit.body, &first.body), "the cached body");
|
||||
assert!(!hit.files.is_complete(), "{:?}", hit.files);
|
||||
}
|
||||
@@ -644,15 +644,32 @@ fn align_index(j: Justify) -> u8 {
|
||||
/// it changes the text), a character drawn with a fallback face or with none (another installed
|
||||
/// font could take it), a font from memory.
|
||||
fn note_fonts(ctx: &EvalCtx, lay: &TextLayout) {
|
||||
note_layout_fonts(ctx, &lay.styles, &lay.layout);
|
||||
}
|
||||
|
||||
/// A character put in by Character Offset or Character Value (`ch`, in style `st`), outside a
|
||||
/// relayout: its outline and advance, with the font it is drawn with noted as [`note_fonts`] does
|
||||
/// (the run's face may not have it: a fallback face then draws it).
|
||||
fn substituted_glyph(ctx: &EvalCtx, st: &effectcraft_keyframe::CharStyle, ch: char) -> (BezPath, f64) {
|
||||
if ctx.reads.is_some() {
|
||||
// (the layout `char_glyph_style` makes, served from the layout cache there)
|
||||
let lay = effectcraft_text::layout_text(&ch.to_string(), &effectcraft_text::text_style(st), &effectcraft_text::ParagraphStyle::default());
|
||||
note_layout_fonts(ctx, std::slice::from_ref(st), &lay);
|
||||
}
|
||||
char_glyph_style(st, ch)
|
||||
}
|
||||
|
||||
/// [`note_fonts`] for `layout`, whose glyphs' runs index `styles`.
|
||||
fn note_layout_fonts(ctx: &EvalCtx, styles: &[effectcraft_keyframe::CharStyle], layout: &effectcraft_text::Layout) {
|
||||
use crate::reads::{FileStamp, ReadFile};
|
||||
use effectcraft_text::fonts;
|
||||
let Some(log) = ctx.reads else { return };
|
||||
let resolved: Vec<fonts::Resolved> = lay.styles.iter().map(|s| fonts::resolve(&s.font, &s.style)).collect();
|
||||
let resolved: Vec<fonts::Resolved> = styles.iter().map(|s| fonts::resolve(&s.font, &s.style)).collect();
|
||||
if resolved.iter().any(|r| r.missing) {
|
||||
log.mark_incomplete();
|
||||
}
|
||||
let mut faces: Vec<fonts::FaceId> = Vec::new();
|
||||
for g in &lay.layout.glyphs {
|
||||
for g in &layout.glyphs {
|
||||
if g.id == 0 || resolved.get(g.run).is_none_or(|r| r.face != g.face) {
|
||||
log.mark_incomplete();
|
||||
}
|
||||
@@ -828,7 +845,7 @@ pub fn text_geom(ctx: &EvalCtx, layer: &Layer) -> Option<TextGeom> {
|
||||
// Outline (substituted characters aligned in the original slot).
|
||||
let (outline, adv) = match subs[gi] {
|
||||
Some(c) => {
|
||||
let (p, a) = char_glyph_style(st, c);
|
||||
let (p, a) = substituted_glyph(ctx, st, c);
|
||||
let dx = match x.char_align {
|
||||
0 => 0.0,
|
||||
2 => g.advance - a,
|
||||
|
||||
@@ -749,16 +749,22 @@ fn text_notes_the_font_files_it_draws_with() {
|
||||
std::fs::write(&font, fonts::INTER_REGULAR).unwrap();
|
||||
fonts::add_source(&OneFile(font.clone()));
|
||||
let stamp = FileStamp::native(&font.to_string_lossy()).unwrap();
|
||||
let files_of = |family: &str| {
|
||||
// (`value`: a Character Value animator puts that character in for every one)
|
||||
let files_with = |family: &str, value: Option<char>| {
|
||||
let (mut p, cid, comp) = setup(200, 80);
|
||||
let l =
|
||||
let mut l =
|
||||
text_layer(&mut p, &comp, TextDoc { text: "Hi there\rtwo".into(), font: family.into(), style: "Regular".into(), size: 30.0, ..Default::default() });
|
||||
if let Some(c) = value {
|
||||
animator(&mut p, &mut l, &["characterValue"], &["range"]);
|
||||
set(&mut l, "text/animators/#1/properties/characterValue", Value::Scalar(c as u32 as f64));
|
||||
}
|
||||
p.comp_mut(cid).unwrap().layers.push(l);
|
||||
let log = ReadLog::new();
|
||||
let c = EvalCtx { reads: Some(&log), ..ctx(&p, cid, 0.0) };
|
||||
assert!(text_geom(&c, &p.comp(cid).unwrap().layers[0]).is_some());
|
||||
log.into_files()
|
||||
};
|
||||
let files_of = |family: &str| files_with(family, None);
|
||||
let files = files_of("Reads Test Sans");
|
||||
assert!(files.is_complete(), "{files:?}");
|
||||
assert_eq!(files.files().len(), 1, "{files:?}");
|
||||
@@ -768,5 +774,12 @@ fn text_notes_the_font_files_it_draws_with() {
|
||||
assert!(files.is_complete() && files.is_empty(), "{files:?}");
|
||||
// A family that isn't installed falls back: installing it would change the text.
|
||||
assert!(!files_of("No Such Family 3184").is_complete());
|
||||
// A substituted character the face has is drawn with it: the same one file.
|
||||
let files = files_with("Reads Test Sans", Some('B'));
|
||||
assert!(files.is_complete(), "{files:?}");
|
||||
assert_eq!(files.get(&font.to_string_lossy()), Some(stamp));
|
||||
// One it lacks (Inter has no CJK) is drawn with a fallback face, or none: another installed
|
||||
// font could take it, so the list can't vouch for the text.
|
||||
assert!(!files_with("Reads Test Sans", Some('\u{4e2d}')).is_complete());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
Reference in new issue
Block a user