Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Merge main into #2368; adapt main's ui_env_light_gpu test (#2436) to the async compositor build

bflatasticcb2b83ae86fa
diff --cc crates/ui-egui/tests/it/ui_env_light_gpu.rs
index 000000000,171cc5728..e88f0a2a4
mode 000000,100644..100644
--- a/crates/ui-egui/tests/it/ui_env_light_gpu.rs
+++ b/crates/ui-egui/tests/it/ui_env_light_gpu.rs
@@@ -1,0 -1,79 +1,84 @@@
+ //! The Environment light through the app's GPU compositor (egui_kittest with wgpu): adding one
+ //! visibly changes the viewer's GPU frames (#2436). Skips without a GPU adapter (CI).
+
+ use effectcraft_engine::Session;
+ use effectcraft_engine::project::Renderer;
+ use effectcraft_ui_egui::EffectcraftApp;
+ use egui_kittest::Harness;
+ use serde_json::json;
+
++use crate::ui_gpu::wait_for_compositor;
++
+ fn settle(h: &mut Harness<'_, EffectcraftApp>) {
+ for _ in 0..600 {
+ h.step();
+ if h.state().frames.inflight() == 0 && h.state().frames.last_ms.lock().map(|v| *v > 0.0).unwrap_or(false) {
+ break;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(20));
+ }
+ for _ in 0..4 {
+ h.step();
+ }
+ }
+
+ /// egui_kittest's wgpu setup on the adapter the headless compositor picked, so the viewer's GPU
+ /// compositor attaches (see ui_gpu.rs).
+ fn same_adapter(name: String) -> eframe::egui_wgpu::WgpuSetup {
+ let mut setup = egui_kittest::wgpu::default_wgpu_setup();
+ if let eframe::egui_wgpu::WgpuSetup::CreateNew(c) = &mut setup {
+ let fallback = c.native_adapter_selector.take();
+ c.native_adapter_selector = Some(std::sync::Arc::new(move |adapters, surface| {
+ let named = |a: &&eframe::wgpu::Adapter| format!("{} ({:?})", a.get_info().name, a.get_info().backend) == name;
+ match (adapters.iter().find(named), &fallback) {
+ (Some(a), _) => Ok(a.clone()),
+ (None, Some(f)) => f(adapters, surface),
+ (None, None) => adapters.first().cloned().ok_or_else(|| "No adapter found".to_owned()),
+ }
+ }));
+ }
+ setup
+ }
+
+ /// A red Environment light at full intensity lights the scene on the GPU compositor too: the
+ /// viewer's GPU frames change when the light is added.
+ #[test]
+ fn environment_light_lights_the_scene_on_the_gpu() {
+ let Some(probe) = effectcraft_gpu::Gpu::headless() else {
+ eprintln!("no GPU adapter: skipping");
+ return;
+ };
+ let adapter = effectcraft_engine::render::Accelerator::name(&probe);
+ eprintln!("headless compositor adapter: {adapter}");
+ let gl = adapter.ends_with("(Gl)");
+ drop(probe);
- let mut s = Session::default();
- s.execute("file.openDemoProject", json!({})).unwrap();
- s.execute("comp.open", json!({"comp": "3D Showcase"})).unwrap();
- s.execute("time.set", json!({"time": 5.0})).unwrap();
- s.execute("comp.settings", json!({"renderer": "classic3D"})).unwrap();
+ let mut h = Harness::builder()
+ .with_size(egui::vec2(1400.0, 900.0))
+ .with_pixels_per_point(1.0)
+ .wgpu_setup(same_adapter(adapter))
- .build_eframe(|_| EffectcraftApp::new(s));
- settle(&mut h);
++ .build_eframe(|_| EffectcraftApp::new(Session::default()));
++ // The project opens once the compositor (built on a worker thread) is attached, so the
++ // frames compared below are GPU frames, not ones the CPU composited meanwhile (see ui_gpu.rs).
++ wait_for_compositor(&mut h);
+ if gl && h.state().gpu_adapter().is_none() {
+ eprintln!("egui-wgpu's GL device has WebGL2 limits: the viewer stays on the CPU, skipping");
+ return;
+ }
+ assert!(h.state().gpu_adapter().is_some(), "GPU compositor on egui-wgpu's device");
++ let s = &mut h.state_mut().session;
++ s.execute("file.openDemoProject", json!({})).unwrap();
++ s.execute("comp.open", json!({"comp": "3D Showcase"})).unwrap();
++ s.execute("time.set", json!({"time": 5.0})).unwrap();
++ s.execute("comp.settings", json!({"renderer": "classic3D"})).unwrap();
++ settle(&mut h);
+ let before = h.state_mut().viewer_pixels().expect("GPU frame before adding the light").pixels.clone();
+
+ h.state_mut().session.execute("layer.newLight", json!({"kind": "Environment", "color": [1.0, 0.0, 0.0], "intensity": 100.0})).unwrap();
+ assert_eq!(h.state().session.active_comp().unwrap().renderer, Renderer::Advanced3D);
+ settle(&mut h);
+ let after = h.state_mut().viewer_pixels().expect("GPU frame after adding the light").pixels.clone();
+
+ assert_eq!(before.len(), after.len());
+ let diff = before.iter().zip(&after).filter(|(a, b)| a != b).count();
+ assert!(diff > 500, "a red Environment light at 100 % changed only {diff} GPU pixels");
+ }
diff --cc crates/ui-egui/tests/it/ui_gpu.rs
index 52f89aa18,355c35bf5..643d99492
--- a/crates/ui-egui/tests/it/ui_gpu.rs
+++ b/crates/ui-egui/tests/it/ui_gpu.rs
@@@ -20,117 -19,6 +20,117 @@@ fn settle(h: &mut Harness<'_, Effectcra
}
}
+/// Step until the GPU compositor's worker has handed its result over (attached, or why not).
+/// The first step starts the build.
- fn wait_for_compositor(h: &mut Harness<'_, EffectcraftApp>) {
++pub(crate) fn wait_for_compositor(h: &mut Harness<'_, EffectcraftApp>) {
+ h.step();
+ for _ in 0..3000 {
+ if !h.state().gpu_initializing() {
+ return;
+ }
+ h.step();
+ std::thread::sleep(std::time::Duration::from_millis(20));
+ }
+ panic!("the GPU compositor's worker gave no result within a minute");
+}
+
+/// Whether wgpu finds any adapter at all (egui_kittest's wgpu harness needs one; CI runners
+/// without a GPU skip).
+fn any_adapter() -> bool {
+ let instance = eframe::wgpu::Instance::new(eframe::wgpu::InstanceDescriptor::new_without_display_handle());
+ !pollster::block_on(instance.enumerate_adapters(eframe::wgpu::Backends::all())).is_empty()
+}
+
+fn small_session() -> Session {
+ let mut s = Session::default();
+ let comp = s.execute("comp.new", json!({"name": "First", "width": 64, "height": 64, "duration": 1})).unwrap()["comp"].as_u64().unwrap();
+ s.execute("layer.newSolid", json!({"comp": comp, "color": "#ff0000"})).unwrap();
+ s
+}
+
+/// The window opens before the GPU compositor exists: its pipelines compile on a worker thread
+/// (tens of seconds on a cold shader cache), and until then the interface draws, says so, and
+/// composites on the CPU. The worker is held on a gate so the "preparing" state is observable.
+#[test]
+fn the_interface_draws_and_composites_on_the_cpu_while_the_gpu_compositor_is_built() {
+ if !any_adapter() {
+ eprintln!("no wgpu adapter: skipping");
+ return;
+ }
+ let (release, gate) = std::sync::mpsc::channel::<()>();
+ let s = small_session();
+ let mut h = Harness::builder().with_size(egui::vec2(1200.0, 800.0)).wgpu().build_eframe(move |_| {
+ let mut app = EffectcraftApp::new(s);
+ // The status line's 5 s expiry reads the app clock: a manual one makes the steps below
+ // stand for a known span of time.
+ app.use_manual_clock();
+ app.hooks.before_gpu_build = Some(Box::new(move || {
+ let _ = gate.recv();
+ }));
+ app
+ });
+ // The first frames paint, and a CPU frame reaches the viewer, while the worker still waits.
+ for _ in 0..600 {
+ h.step();
+ if h.state().is_ready() && !h.output().shapes.is_empty() && h.state().frames.last_ms.lock().map(|v| *v > 0.0).unwrap_or(false) {
+ break;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(10));
+ }
+ assert!(h.state().gpu_initializing(), "the compositor is still being built");
+ assert!(!h.output().shapes.is_empty(), "the interface draws before the compositor exists");
+ assert!(h.state().session.accel.is_none(), "nothing renders on a compositor that doesn't exist yet");
+ assert_eq!(h.state().ui.status, effectcraft_ui_egui::GPU_PREPARING, "the status line is honest about it");
+ assert_eq!(h.state().session.accel_note.as_deref(), Some(effectcraft_ui_egui::GPU_PREPARING));
+ assert!(h.state().frames.last_ms.lock().map(|v| *v > 0.0).unwrap_or(false), "a frame was composited on the CPU meanwhile");
+ assert!(h.state_mut().viewer_pixels().is_some(), "the viewer shows that frame");
+ // Other statuses clear after 5 s; this one stays for as long as the build takes (each step
+ // is a quarter of a second).
+ for _ in 0..30 {
+ h.state_mut().clock_mut().advance(0.25);
+ h.step();
+ }
+ assert!(h.state().gpu_initializing());
+ assert_eq!(h.state().ui.status, effectcraft_ui_egui::GPU_PREPARING, "the status line still says so 7.5 s later");
+ release.send(()).unwrap();
+ settle(&mut h);
+ assert!(!h.state().gpu_initializing());
+ assert_ne!(h.state().ui.status, effectcraft_ui_egui::GPU_PREPARING, "the preparing status goes away");
+ match h.state().gpu_adapter() {
+ Some(adapter) => {
+ eprintln!("compositor attached after the first frames: {adapter}");
+ assert!(h.state().session.accel.is_some());
+ assert!(h.state().session.accel_note.is_none());
+ }
+ // An adapter that can't run the compositor (software GL): the reason is recorded.
+ None => assert!(h.state().session.accel_note.as_deref().is_some_and(|n| n != effectcraft_ui_egui::GPU_PREPARING)),
+ }
+}
+
+/// A graphics backend that panics while the compositor's pipelines compile panics on the worker
+/// thread, not under the UI: the window keeps drawing on the CPU and says why.
+#[test]
+fn a_compositor_build_that_panics_leaves_the_viewer_on_the_cpu() {
+ if !any_adapter() {
+ eprintln!("no wgpu adapter: skipping");
+ return;
+ }
+ let s = small_session();
+ let mut h = Harness::builder().with_size(egui::vec2(1200.0, 800.0)).wgpu().build_eframe(move |_| {
+ let mut app = EffectcraftApp::new(s);
+ app.hooks.before_gpu_build = Some(Box::new(|| panic!("synthetic shader compiler panic")));
+ app
+ });
+ settle(&mut h);
+ assert!(!h.state().gpu_initializing());
+ assert!(h.state().gpu_adapter().is_none());
+ assert!(h.state().session.accel.is_none());
+ let note = h.state().session.accel_note.clone().unwrap_or_default();
+ assert!(note.contains("synthetic shader compiler panic"), "{note}");
+ assert!(!h.output().shapes.is_empty(), "the interface still draws");
+ assert!(h.state_mut().viewer_pixels().is_some(), "the viewer shows a CPU frame");
+}
+
/// egui_kittest's wgpu setup on the adapter the headless compositor picked (`name (Backend)`),
/// like the desktop app's window. Left alone, egui_kittest prefers a CPU adapter, which on
/// Windows is WARP on DirectX 12, where FXC fails to compile the compositor's kernels: the