mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-20 00:55:43 +00:00
Render: sampleImage and text fonts note the files they read
EvalCtx carries the render's read log. A sampleImage render records its footage reads, keeps them with its per-thread cached buffer and notes them in the evaluating render's log on every use. Text notes the font files it is drawn with, with the identity their bytes were read with; a family that isn't installed, a fallback or missing glyph, or a font from memory leaves the list incomplete. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d948c231c
commit
d04ccd5234
38 files changed
+298
-24
No files matched your search
@@ -309,6 +309,7 @@ impl<'a> Resolver<'a> {
|
||||
footage: self.ctx.footage,
|
||||
property_overrides: if same_comp { self.ctx.property_overrides } else { &[] },
|
||||
media_overrides: if same_comp { self.ctx.media_overrides } else { &[] },
|
||||
reads: self.ctx.reads,
|
||||
})
|
||||
}
|
||||
fn prop(&self, c: u64, l: u64, path: &str) -> Option<(&'a Layer, &'a Property)> {
|
||||
|
||||
@@ -172,6 +172,7 @@ fn evaluate_out(
|
||||
footage: ctx.footage,
|
||||
property_overrides: ctx.property_overrides,
|
||||
media_overrides: ctx.media_overrides,
|
||||
reads: ctx.reads,
|
||||
};
|
||||
let comp = ctx.comp_id.0;
|
||||
let path = layer.props.path_of(prop.uid).unwrap_or_else(|| format!("@{}", prop.uid));
|
||||
|
||||
@@ -4,13 +4,16 @@
|
||||
//! expression (its footage source; [`EvalCtx::footage`]): source → masks → effects for
|
||||
//! `postEffect = true`, the bare source otherwise, in layer space (before the transform). Buffers
|
||||
//! are cached per thread for the frame, so many expressions sampling the same layer at the same
|
||||
//! time render it once.
|
||||
//! time render it once. The source files a sampled render reads are kept with its buffer and
|
||||
//! noted in the evaluating render's log ([`EvalCtx::reads`]) on every use, so a frame whose
|
||||
//! expressions sample footage lists that footage too (#3184).
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use effectcraft_project::{ItemId, Layer};
|
||||
use effectcraft_render::reads::{ReadFiles, ReadLog};
|
||||
use effectcraft_render::{EvalCtx, NoFootage, RenderOpts, Renderer};
|
||||
use effectcraft_time::Tick;
|
||||
|
||||
@@ -26,7 +29,8 @@ struct Sampled {
|
||||
type Key = (usize, u64, u64, u64, bool, u64);
|
||||
|
||||
thread_local! {
|
||||
static CACHE: RefCell<HashMap<Key, Option<Sampled>>> = RefCell::new(HashMap::new());
|
||||
/// The sampled buffers and the source files each was made of.
|
||||
static CACHE: RefCell<HashMap<Key, (Option<Sampled>, Arc<ReadFiles>)>> = RefCell::new(HashMap::new());
|
||||
/// Nested sampleImage renders (a sampled layer whose expressions sample again).
|
||||
static DEPTH: RefCell<usize> = const { RefCell::new(0) };
|
||||
}
|
||||
@@ -48,7 +52,13 @@ fn render(ctx: EvalCtx, comp: ItemId, layer: &Layer, post: bool, t: f64) -> Opti
|
||||
return None;
|
||||
}
|
||||
let key: Key = (ctx.project as *const _ as usize, comp.0, layer.id.0, t.to_bits(), post, fingerprint(layer));
|
||||
if let Some(hit) = CACHE.with(|c| c.borrow().get(&key).cloned()) {
|
||||
let note = |files: &ReadFiles| {
|
||||
if let Some(log) = ctx.reads {
|
||||
log.note_all(files);
|
||||
}
|
||||
};
|
||||
if let Some((hit, files)) = CACHE.with(|c| c.borrow().get(&key).cloned()) {
|
||||
note(&files);
|
||||
return hit;
|
||||
}
|
||||
DEPTH.with(|d| *d.borrow_mut() += 1);
|
||||
@@ -60,20 +70,24 @@ fn render(ctx: EvalCtx, comp: ItemId, layer: &Layer, post: bool, t: f64) -> Opti
|
||||
}
|
||||
let _pop = Pop;
|
||||
let footage: &dyn effectcraft_render::FootageSource = ctx.footage.unwrap_or(&NoFootage);
|
||||
let log = ReadLog::new();
|
||||
let mut r = Renderer::new(ctx.project, footage, RenderOpts { motion_blur: false, ..Default::default() });
|
||||
r.expr = ctx.expr;
|
||||
r.reads = Some(&log);
|
||||
let ectx = r.eval_ctx(comp, Tick::from_seconds_f64(t))?;
|
||||
let out = if post {
|
||||
r.content_buf(&ectx, layer).map(|b| Sampled { img: Arc::new(b.img.clone()), offset: b.offset, scale: b.scale })
|
||||
} else {
|
||||
r.layer_source(&ectx, layer).map(|b| Sampled { img: Arc::new(b.img), offset: b.offset, scale: b.scale })
|
||||
};
|
||||
let files = Arc::new(log.into_files());
|
||||
note(&files);
|
||||
CACHE.with(|c| {
|
||||
let mut c = c.borrow_mut();
|
||||
if c.len() >= MAX_CACHE {
|
||||
c.clear();
|
||||
}
|
||||
c.insert(key, out.clone());
|
||||
c.insert(key, (out.clone(), files));
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
@@ -1326,6 +1326,7 @@ fn source_dimensions_follow_media_replacement() {
|
||||
footage: None,
|
||||
property_overrides: &[],
|
||||
media_overrides: &[media],
|
||||
reads: None,
|
||||
};
|
||||
assert_eq!(ctx.value(l, l.props.prop(ROT).unwrap()), Value::Scalar(130.0));
|
||||
}
|
||||
@@ -1548,6 +1549,7 @@ fn text_expression_selector_values() {
|
||||
footage: None,
|
||||
property_overrides: &[],
|
||||
media_overrides: &[],
|
||||
reads: None,
|
||||
};
|
||||
let doc = effectcraft_render::text::source_text(&ctx, l).unwrap();
|
||||
let lay = effectcraft_text::layout_doc(&doc);
|
||||
@@ -1600,6 +1602,43 @@ fn sample_image_reads_rendered_pixels() {
|
||||
assert_close!(f.num(&code("thisComp.layer(\"B\").sampleImage([-50, -50])"), 0.0), 0.0);
|
||||
}
|
||||
|
||||
/// The footage `sampleImage` renders is a file the evaluating render read (#3184): it is noted in
|
||||
/// the render's log, also when the sampled buffer comes from the per-thread cache.
|
||||
#[test]
|
||||
fn sample_image_notes_the_files_it_reads() {
|
||||
use effectcraft_render::reads::{FileStamp, ReadFile, ReadLog};
|
||||
/// A grey picture of any footage, noting `/clip.png` for every read.
|
||||
struct Clip;
|
||||
impl effectcraft_render::FootageSource for Clip {
|
||||
fn frame(&self, _: ItemId, f: &effectcraft_project::Footage, _: Tick) -> Option<std::sync::Arc<effectcraft_render::Image>> {
|
||||
let mut img = effectcraft_render::Image::new(f.width, f.height);
|
||||
img.data.fill([0.5, 0.5, 0.5, 1.0]);
|
||||
Some(std::sync::Arc::new(img))
|
||||
}
|
||||
fn recording(&self, reads: &ReadLog, f: &mut dyn FnMut()) {
|
||||
f();
|
||||
reads.note(ReadFile::new("/clip.png", FileStamp::Native { len: 3, modified: Some(1) }));
|
||||
}
|
||||
}
|
||||
let mut p = Project::default();
|
||||
let comp = Comp::new(20, 20, FrameRate::FPS_30, Tick::from_seconds_f64(1.0));
|
||||
let footage =
|
||||
effectcraft_project::Footage { path: "/clip.png".into(), width: 8, height: 8, has_video: true, frame_rate: FrameRate::FPS_30, ..Default::default() };
|
||||
let item = p.add_item("Clip", Label::Aqua, None, ItemKind::Footage(footage));
|
||||
let layer = build::layer(&mut p, &comp, "Clip", LayerSource::Footage { item }, (8, 8), None);
|
||||
let cid = p.add_item("C", Label::Sandstone, None, ItemKind::Comp(comp.into()));
|
||||
let c = p.comp(cid).unwrap();
|
||||
for pass in ["rendered", "cached"] {
|
||||
let log = ReadLog::new();
|
||||
let ctx = EvalCtx { footage: Some(&Clip), reads: Some(&log), ..EvalCtx::new(&p, cid, c, Tick::ZERO) };
|
||||
let px = crate::sample::sample_image(ctx, cid, &layer, [4.0, 4.0], [0.5, 0.5], false, 0.0).unwrap();
|
||||
assert!((px[0] - 0.5).abs() < 1e-3 && px[3] > 0.99, "{pass}: {px:?}");
|
||||
let files = log.into_files();
|
||||
assert_eq!(files.files().iter().map(|f| &*f.path).collect::<Vec<_>>(), ["/clip.png"], "{pass}");
|
||||
assert!(files.is_complete(), "{pass}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A huge sample point saturates the pixel coordinates; it must not overflow the loop bounds.
|
||||
#[test]
|
||||
fn sample_image_huge_point_is_transparent() {
|
||||
|
||||
Reference in new issue
Block a user