FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
export: a frame planned as one CPU image does not take a pooled GPU renderer
Tommaso Maccagni13d8e73e8ef2
diff --git a/crates/export/src/pipeline.rs b/crates/export/src/pipeline.rsindex 306aef2fd..a2c4db220 100644--- a/crates/export/src/pipeline.rs+++ b/crates/export/src/pipeline.rs@@ -213,8 +213,12 @@ impl Pipeline {pub fn frame(&self, f: i64, sources: &dyn SourceProvider) -> (Vec<u8>, Vec<f32>) {let t = self.rate.tick_of(f);// The registered GPU renderer goes first; `None` (no adapter, a plan the GPU cannot- // draw, any internal error) falls back to the CPU reference renderer below.- let gpu = match self.renderer.as_ref() {+ // draw, any internal error) falls back to the CPU reference renderer below. A frame the+ // planner hands back as one CPU image anyway (an adjustment layer, a complex transition,+ // HDR) does not take a pooled renderer: it would render on the CPU inside the pool, as+ // many frames at a time as there are renderers instead of one per export thread.+ let pool = self.renderer.as_ref().filter(|_| !filmcraft_render::plan::is_cpu_frame(&self.project, self.seq, t));+ let gpu = match pool {Some(pool) => {let asked = web_time::Instant::now();pool.with(|r| {diff --git a/crates/export/src/tests.rs b/crates/export/src/tests.rsindex 656ddd9f5..ef7b182a1 100644--- a/crates/export/src/tests.rs+++ b/crates/export/src/tests.rs@@ -357,6 +357,36 @@ fn gpu_hook_scenarios() {}// at most one panic per pooled renderer (4): none is used again after it panickedassert!(PANICS.load(std::sync::atomic::Ordering::SeqCst) <= 4, "renderer reused after a panic: {}", PANICS.load(std::sync::atomic::Ordering::SeqCst));++ // 5. A frame the planner hands back as one CPU image (an adjustment layer over the clip from+ // frame 12) is rendered by the CPU without taking a pooled renderer: it counts as a+ // fallback, and the renderer's picture reaches only the frames before it.+ crate::reset_frame_renderers_for_tests();+ crate::register_frame_renderer(|| Some(Box::new(FakeRenderer) as Box<dyn FrameRenderer>));+ let (p, seq, m) = project();+ let mut p = Arc::unwrap_or_clone(p);+ let r = FrameRate::FPS_24;+ let adj = p.add_item("adj", Label::Iris, ItemKind::AdjustmentLayer { width: 320, height: 180, rate: r, duration: Tick(TICKS_PER_SECOND) }, None);+ let over = p.make_track_item(adj, TrackKind::Video, r.tick_of(12), TimeRange::new(Tick::ZERO, r.tick_of(12)), r).unwrap();+ let v2 = filmcraft_project::TrackId(p.alloc_id());+ let q = p.sequence_mut(seq).unwrap();+ q.video_tracks.push(filmcraft_project::Track::new(v2, TrackKind::Video, "Video 2".into()));+ q.video_tracks[1].items.push(over);+ let p = Arc::new(p);+ let mk24 = |path: &String, gpu: crate::GpuRendering| {+ let mut s = ExportSettings { format: Format::PngSequence, path: path.clone(), ..Default::default() };+ s.range = Some(TimeRange::new(Tick::ZERO, r.tick_of(24)));+ s.gpu_rendering = gpu;+ export(&p, seq, &s, &m, &Progress::default()).unwrap()+ };+ mk24(&tmp("adj-off.png"), crate::GpuRendering::Off);+ mk24(&tmp("adj-auto.png"), crate::GpuRendering::Auto);+ let frame = |name: &str, f: &str| std::fs::read(tmp(&format!("{name}{f}.png"))).unwrap();+ assert_ne!(frame("adj-off", "011"), frame("adj-auto", "011"), "a layered frame goes to the renderer");+ for f in ["012", "023"] {+ assert_eq!(frame("adj-off", f), frame("adj-auto", f), "frame {f}: an adjustment layer frame is the CPU's");+ }+ assert_eq!((gpu_render_stats().frames, gpu_render_stats().fallbacks), (12, 12), "{:?}", gpu_render_stats());crate::reset_frame_renderers_for_tests();}diff --git a/crates/render/src/plan.rs b/crates/render/src/plan.rsindex f986711c6..92b333cde 100644--- a/crates/render/src/plan.rs+++ b/crates/render/src/plan.rs@@ -191,12 +191,7 @@ fn gpu_chain<'a>(project: &Project, item: &'a TrackItem, opts: RenderOptions) ->pub fn plan_frame(project: &Project, seq_id: ItemId, t: Tick, opts: RenderOptions, sources: &dyn SourceProvider) -> FramePlan {let Some(seq) = project.sequence(seq_id) else { return FramePlan::Image(crate::Image::new(1, 1)) };let (w, h) = output_size(seq, opts.scale);- // HDR / wide-gamut sequences composite and convert on the CPU.- if !seq.settings.color.is_plain() {- return FramePlan::Image(crate::render_sequence(project, seq_id, t, opts, sources));- }- // Whole-frame fallback: adjustment layers or complex transitions anywhere at t.- if !layered_at(project, seq, t) {+ if whole_frame_on_cpu(project, seq, t) {return FramePlan::Image(crate::render_sequence(project, seq_id, t, opts, sources));}let mut layers = PlanStack::default();@@ -214,6 +209,20 @@ pub fn plan_frame(project: &Project, seq_id: ItemId, t: Tick, opts: RenderOptionlayers.finish(w, h)}+/// Whether [`plan_frame`] hands the frame at `t` back as one CPU image ([`FramePlan::Image`]). A+/// GPU renderer gains nothing on such a frame, so a caller with only a few of them (the export's+/// pool) can render it on the CPU without taking one.+pub fn is_cpu_frame(project: &Project, seq_id: ItemId, t: Tick) -> bool {+ project.sequence(seq_id).is_none_or(|seq| whole_frame_on_cpu(project, seq, t))+}++/// The frames [`plan_frame`] does not plan: an HDR / wide-gamut sequence composites and converts+/// on the CPU, and so does a frame with an adjustment layer or a complex transition anywhere at+/// `t` (whole-frame fallback).+fn whole_frame_on_cpu(project: &Project, seq: &Sequence, t: Tick) -> bool {+ !seq.settings.color.is_plain() || !layered_at(project, seq, t)+}+/// Whether the frame of `seq` at `t` can be planned as layers: no adjustment layer and no/// transition the compositor cannot mix itself is showing.fn layered_at(project: &Project, seq: &Sequence, t: Tick) -> bool {