Files
effectcraft/crates/ui-egui/tests/ui_gpu.rs
T
Brandon ThomasandClaude Opus 5.5 af651a6f56 M12.2: viewer on gpu
The desktop viewer builds the GPU compositor on egui-wgpu's device and,
with Mercury GPU Acceleration, renders frames into RGBA8 textures that are
registered with egui-wgpu and drawn directly (no readback); the RAM preview
cache holds them under a video-memory budget. Pixels are read back only on
demand (Info panel, eyedroppers, histograms). Project Settings gets the
Video Rendering and Effects renderer choice. ui_gpu test: GPU viewer frame
matches the CPU one (skips without an adapter).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 11:06:05 -04:00

57 lines
2.4 KiB
Rust

//! The viewer on the GPU compositor (egui_kittest with wgpu): frames are drawn from GPU textures
//! and match the CPU viewer. Skips without a GPU adapter (CI). Set `EC_SNAPSHOT_DIR` to keep PNGs.
use effectcraft_engine::Session;
use effectcraft_ui_egui::EffectcraftApp;
use egui_kittest::Harness;
use serde_json::json;
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();
}
}
#[test]
fn viewer_draws_gpu_frames_that_match_the_cpu() {
if effectcraft_gpu::Gpu::headless().is_none() {
eprintln!("no GPU adapter: skipping");
return;
}
let mut s = Session::default();
s.execute("file.openDemoProject", json!({})).unwrap();
s.execute("time.set", json!({"time": 2.0})).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_pixels_per_point(1.0).wgpu().build_eframe(|_| EffectcraftApp::new(s));
settle(&mut h);
assert!(h.state().gpu_adapter().is_some(), "GPU compositor on egui-wgpu's device");
assert!(h.state().viewer_on_gpu(), "viewer frame comes from the GPU");
let gpu_px = h.state_mut().viewer_pixels().expect("read back the GPU frame");
let gpu_shot = h.render().expect("render");
// Mercury Software Only: the same frame from the CPU.
h.state_mut().session.execute("render.backend", json!({"backend": "cpu"})).unwrap();
settle(&mut h);
assert!(!h.state().viewer_on_gpu());
let cpu_px = h.state_mut().viewer_pixels().expect("CPU frame");
let cpu_shot = h.render().expect("render");
assert_eq!(gpu_px.size, cpu_px.size);
let mut worst = 0i32;
let mut over = 0usize;
for (a, b) in gpu_px.pixels.iter().zip(&cpu_px.pixels) {
let d = a.to_array().iter().zip(b.to_array()).map(|(x, y)| (*x as i32 - y as i32).abs()).max().unwrap_or(0);
worst = worst.max(d);
over += usize::from(d > 1);
}
assert!(over * 1000 <= gpu_px.pixels.len() * 3 && worst <= 4, "viewer GPU vs CPU: {over} pixels > 1/255, worst {worst}/255");
if let Ok(d) = std::env::var("EC_SNAPSHOT_DIR") {
gpu_shot.save(format!("{d}/ui_gpu_viewer.png")).unwrap();
cpu_shot.save(format!("{d}/ui_cpu_viewer.png")).unwrap();
}
}