mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-20 09:05:51 +00:00
Merge remote-tracking branch 'upstream/main' into cache-defaults-3298
# Conflicts: # crates/ui-egui/src/frames.rs
This commit is contained in:
commit
ddff463c87
5 files changed
+431
-8
No files matched your search
@@ -610,15 +610,21 @@ struct ParallelClip {
|
||||
started: std::sync::atomic::AtomicUsize,
|
||||
/// Frames started by the time frame 1 was done.
|
||||
started_at_first: std::sync::atomic::AtomicUsize,
|
||||
/// Frames decoding now, and the most at a time.
|
||||
active: std::sync::atomic::AtomicUsize,
|
||||
most_active: std::sync::atomic::AtomicUsize,
|
||||
}
|
||||
|
||||
impl FootageSource for ParallelClip {
|
||||
fn frame(&self, _: ItemId, _: &effectcraft_engine::project::Footage, t: Tick) -> Option<Arc<effectcraft_engine::render::Image>> {
|
||||
self.started.fetch_add(1, Ordering::SeqCst);
|
||||
let active = self.active.fetch_add(1, Ordering::SeqCst) + 1;
|
||||
self.most_active.fetch_max(active, Ordering::SeqCst);
|
||||
(0..16).into_par_iter().for_each(|_| (0..4).into_par_iter().for_each(|_| std::thread::sleep(std::time::Duration::from_millis(1))));
|
||||
if effectcraft_engine::time::FrameRate::FPS_30.frame_at(t) == 1 {
|
||||
self.started_at_first.store(self.started.load(Ordering::SeqCst), Ordering::SeqCst);
|
||||
}
|
||||
self.active.fetch_sub(1, Ordering::SeqCst);
|
||||
Some(Arc::new(effectcraft_engine::render::Image::new(4, 4)))
|
||||
}
|
||||
}
|
||||
@@ -729,6 +735,166 @@ fn queued_frames_render_when_a_preview_starts_or_stops() {
|
||||
assert_eq!(f.cached_frames(&series).len(), 119);
|
||||
}
|
||||
|
||||
/// Waits for the lanes to be idle (after their last frame they give back their GPU slot) and
|
||||
/// checks every GPU slot was given back.
|
||||
fn wait_for_idle_lanes(f: &Frames) {
|
||||
for _ in 0..2000 {
|
||||
let q = f.queue.lock().unwrap();
|
||||
if q.idle_lanes.len() == f.lanes.len() {
|
||||
break;
|
||||
}
|
||||
drop(q);
|
||||
std::thread::sleep(std::time::Duration::from_millis(5));
|
||||
}
|
||||
let q = f.queue.lock().unwrap();
|
||||
assert_eq!(q.idle_lanes.len(), f.lanes.len(), "every lane is idle again");
|
||||
assert_eq!(q.gpu_lanes, 0, "every lane gave its GPU slot back");
|
||||
}
|
||||
|
||||
/// Plays a preview of `frames` frames of a [`parallel_clip`] as the viewer renders them (the
|
||||
/// project's renderer: `backend` Auto) or with `backend`, as if there were a GPU compositor
|
||||
/// (`gpu`), `retired` or with the project on Mercury Software Only (`!gpu_acceleration`), and
|
||||
/// waits for them: the frame cache and the most frames that rendered at a time.
|
||||
fn play_preview(gpu: bool, retired: bool, backend: effectcraft_engine::render::Backend, gpu_acceleration: bool) -> (Frames, usize) {
|
||||
use effectcraft_engine::time::FrameRate;
|
||||
let (f, footage, mut src, cid, series) = parallel_clip();
|
||||
Arc::make_mut(&mut src.project).settings.gpu_acceleration = gpu_acceleration;
|
||||
f.queue.lock().unwrap().fake_gpu = gpu;
|
||||
if retired {
|
||||
f.retire_gpu();
|
||||
}
|
||||
let opts = RenderOpts { backend, ..RenderOpts::default() };
|
||||
f.set_playhead(Some(Playhead { series, cur: 0, start: 0, end: 299, step: 1, looping: false }));
|
||||
let frames = 24;
|
||||
for frame in 1..=frames {
|
||||
f.request(&src, FrameKey { frame, ..series }, cid, FrameRate::FPS_30.tick_of(frame), opts);
|
||||
}
|
||||
wait_for_renders(&f);
|
||||
assert_eq!(f.cached_frames(&series).len(), frames as usize);
|
||||
wait_for_idle_lanes(&f);
|
||||
let most = footage.most_active.load(Ordering::SeqCst);
|
||||
(f, most)
|
||||
}
|
||||
|
||||
/// Every lane composited a playing preview's frame on the GPU at the same time, on the device
|
||||
/// the window draws with: the window froze for hundreds of milliseconds and the preview jumped
|
||||
/// ahead (#3345). A preview starts on one GPU lane (see [`Queue::ui_frame`]).
|
||||
#[test]
|
||||
fn a_preview_composites_on_a_few_lanes_at_a_time() {
|
||||
use effectcraft_engine::render::Backend;
|
||||
for backend in [Backend::Auto, Backend::Gpu] {
|
||||
let (_, most) = play_preview(true, false, backend, true);
|
||||
assert!(most <= 1, "{most} lanes composited on the GPU at once ({backend:?})");
|
||||
}
|
||||
}
|
||||
|
||||
/// Frames that render on the CPU use every lane: with no GPU compositor, with the GPU retired,
|
||||
/// and with Mercury Software Only (the project's renderer, or the CPU backend).
|
||||
#[test]
|
||||
fn frames_on_the_cpu_render_on_every_lane() {
|
||||
use effectcraft_engine::render::Backend;
|
||||
for (gpu, retired, backend, gpu_acceleration) in
|
||||
[(false, false, Backend::Auto, true), (true, true, Backend::Auto, true), (true, false, Backend::Auto, false), (true, false, Backend::Cpu, true)]
|
||||
{
|
||||
let (f, most) = play_preview(gpu, retired, backend, gpu_acceleration);
|
||||
if f.lanes.len() > 2 {
|
||||
assert!(most > 2, "frames on the CPU rendered on {most} lanes at a time ({gpu} {retired} {backend:?} {gpu_acceleration})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A queue of `lanes` lanes, `busy` of them compositing on the GPU.
|
||||
fn gpu_queue(lanes: usize, busy: usize) -> Queue {
|
||||
Queue { lanes, gpu_lanes: busy, gpu_limit: 1, ..Default::default() }
|
||||
}
|
||||
|
||||
/// Window frames every `gap` ms from `t` for `ms` ms; the time after the last.
|
||||
fn ui_frames(q: &mut Queue, t: web_time::Instant, gap: u64, ms: u64) -> web_time::Instant {
|
||||
let mut t = t;
|
||||
for _ in 0..ms / gap {
|
||||
t += std::time::Duration::from_millis(gap);
|
||||
q.ui_frame(Some(t));
|
||||
}
|
||||
t
|
||||
}
|
||||
|
||||
/// A GPU that keeps up with the window gets every lane, one more each half second the window
|
||||
/// stays smooth with every allowed lane compositing.
|
||||
#[test]
|
||||
fn a_smooth_window_gives_the_gpu_every_lane() {
|
||||
let mut q = gpu_queue(8, 0);
|
||||
let t = web_time::Instant::now();
|
||||
q.ui_frame(Some(t));
|
||||
// Nothing compositing: the limit isn't what holds the preview back.
|
||||
let t = ui_frames(&mut q, t, 16, 2000);
|
||||
assert_eq!(q.gpu_limit, 1);
|
||||
let mut t = t;
|
||||
for _ in 0..20 {
|
||||
q.gpu_lanes = q.gpu_limit;
|
||||
t = ui_frames(&mut q, t, 16, 520);
|
||||
}
|
||||
assert_eq!(q.gpu_limit, 8, "no more than the lanes");
|
||||
}
|
||||
|
||||
/// A window that stalls while lanes composite halves the limit, and the limit that stalled is
|
||||
/// tried again only after a long smooth spell; a window that is only slow keeps the limit.
|
||||
#[test]
|
||||
fn a_stalling_window_takes_lanes_from_the_gpu() {
|
||||
let mut q = gpu_queue(8, 4);
|
||||
q.gpu_limit = 4;
|
||||
let t = web_time::Instant::now();
|
||||
q.ui_frame(Some(t));
|
||||
let t = ui_frames(&mut q, t, 300, 300);
|
||||
assert_eq!((q.gpu_limit, q.gpu_stalled_at), (2, 4));
|
||||
let t = ui_frames(&mut q, t, 80, 5000);
|
||||
assert_eq!(q.gpu_limit, 2, "a slow (not stalling) window keeps the limit");
|
||||
q.gpu_lanes = 2;
|
||||
let t = ui_frames(&mut q, t, 16, 600);
|
||||
assert_eq!(q.gpu_limit, 3);
|
||||
q.gpu_lanes = 3;
|
||||
let t = ui_frames(&mut q, t, 16, 2000);
|
||||
assert_eq!(q.gpu_limit, 3, "4 stalled: it waits longer");
|
||||
ui_frames(&mut q, t, 16, 10_000);
|
||||
assert_eq!(q.gpu_limit, 4);
|
||||
// A stall with nothing compositing isn't the GPU's.
|
||||
let mut q = gpu_queue(8, 0);
|
||||
q.gpu_limit = 4;
|
||||
let t = web_time::Instant::now();
|
||||
q.ui_frame(Some(t));
|
||||
ui_frames(&mut q, t, 500, 500);
|
||||
assert_eq!(q.gpu_limit, 4);
|
||||
// Never below one lane; a stopped preview's pause is no stall.
|
||||
let mut q = gpu_queue(8, 1);
|
||||
let t = web_time::Instant::now();
|
||||
q.ui_frame(Some(t));
|
||||
ui_frames(&mut q, t, 400, 400);
|
||||
assert_eq!(q.gpu_limit, 1);
|
||||
q.gpu_limit = 2;
|
||||
q.ui_frame(None);
|
||||
q.ui_frame(Some(t + std::time::Duration::from_secs(5)));
|
||||
assert_eq!(q.gpu_limit, 2);
|
||||
}
|
||||
|
||||
/// A preview that stops while lanes composite: their frames finish, the queued ones move to the
|
||||
/// pool and render, the viewer's frame is not held up, and every GPU slot is given back.
|
||||
#[test]
|
||||
fn a_preview_stopping_mid_render_gives_the_gpu_slots_back() {
|
||||
use effectcraft_engine::time::FrameRate;
|
||||
let (f, _, src, cid, series) = parallel_clip();
|
||||
f.queue.lock().unwrap().fake_gpu = true;
|
||||
let opts = RenderOpts { backend: effectcraft_engine::render::Backend::Auto, ..RenderOpts::default() };
|
||||
f.set_playhead(Some(Playhead { series, cur: 0, start: 0, end: 299, step: 1, looping: false }));
|
||||
for frame in 1..=24 {
|
||||
f.request(&src, FrameKey { frame, ..series }, cid, FrameRate::FPS_30.tick_of(frame), opts);
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||
f.set_playhead(None);
|
||||
f.request_urgent(&src, FrameKey { frame: 100, ..series }, cid, FrameRate::FPS_30.tick_of(100), opts);
|
||||
wait_for_renders(&f);
|
||||
assert_eq!(f.cached_frames(&series).len(), 25);
|
||||
wait_for_idle_lanes(&f);
|
||||
}
|
||||
|
||||
/// The cache is full. A zoom (or preview-resolution) change left the frames of the old
|
||||
/// size to the plain least-recently-shown order, so the frames of the zoom the viewer is back
|
||||
/// at went before the ones of the abandoned zoom. The other size's frames go first, then the
|
||||
|
||||
Reference in new issue
Block a user