Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Fix tests on FreeBSD with RADV (#194) (#194)

Goran Mekić2ad42d028a46
diff --git a/crates/engine/src/automate_cmds.rs b/crates/engine/src/automate_cmds.rs
index 4d86a0071..b15c9ed17 100644
--- a/crates/engine/src/automate_cmds.rs
+++ b/crates/engine/src/automate_cmds.rs
@@ -5,7 +5,7 @@
//! "Scripts" are PhotoCraft action scripts: JSON (`[[id, params], …]`, `{"steps": …}` or a
//! droplet) or a plain-text list with one `command.id {json params}` per line (`#` comments).
//! Droplets are JSON files (`.pcdroplet`) holding an action plus batch options; `photocraft-cli
-//! droplet <file> <inputs…>` runs one, and on macOS/Linux a `.command` shim makes it
+//! droplet <file> <inputs…>` runs one, and on Unix a `.command` shim makes it
//! double-clickable / drop-target-able from the shell.
//!
//! Script events (Start Application, New / Open / Save / Close Document, Print, Export) are
@@ -291,7 +291,7 @@ fn create_droplet(s: &mut Session, p: &Value) -> Result<Value> {
let droplet = json!({"photocraftDroplet": 1, "name": name, "action": {"name": name, "steps": steps_json}, "options": options});
let text = serde_json::to_string_pretty(&droplet).unwrap_or_default();
write_file(&path, text.as_bytes())?;
- let shim = p.get("shim").and_then(Value::as_bool).unwrap_or(cfg!(any(target_os = "macos", target_os = "linux")));
+ let shim = p.get("shim").and_then(Value::as_bool).unwrap_or(cfg!(unix));
let shim_path = if shim { Some(write_shim(&path)?) } else { None };
let _ = s;
Ok(json!({"path": path, "shim": shim_path, "steps": steps.len()}))
@@ -526,7 +526,7 @@ pub fn specs() -> Vec<CommandSpec> {
"file.automate.createDroplet",
"Create Droplet…",
&["File", "Automate"],
- r##"{"path":str (.pcdroplet),"steps":[[id,params]…] (the action),"name":str?,"output":folder?,"format":"same|png|jpg|…"?,"quality":0..12?,"shim":bool=true on macOS/Linux (writes <name>.command calling `photocraft-cli droplet`)} → {path, shim}"##,
+ r##"{"path":str (.pcdroplet),"steps":[[id,params]…] (the action),"name":str?,"output":folder?,"format":"same|png|jpg|…"?,"quality":0..12?,"shim":bool=true on Unix (writes <name>.command calling `photocraft-cli droplet`)} → {path, shim}"##,
native,
create_droplet
),
diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs
index 395dcfe6a..aab11bcb0 100644
--- a/crates/gpu/src/lib.rs
+++ b/crates/gpu/src/lib.rs
@@ -826,6 +826,11 @@ impl Compositor {
if region.is_empty() {
return Ok(stats);
}
+ if self.frame == 0 {
+ // The first submission after device creation is dropped on some drivers (RADV),
+ // which would render the first frame black; land it on an empty buffer instead.
+ queue.submit(std::iter::empty::<wgpu::CommandBuffer>());
+ }
self.frame += 1;
self.stamp += 1;
self.staged = 0;
diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs
index 1db79e480..33f76399a 100644
--- a/crates/gpu/tests/parity.rs
+++ b/crates/gpu/tests/parity.rs
@@ -9,6 +9,10 @@ use photocraft_gpu::{Compositor, render_to_vec};
const TOL: f32 = 1.0 / 255.0;
+/// Concurrent wgpu instances in one process segfault on some drivers (RADV), so the GPU tests
+/// take this lock and hold it until their device is dropped.
+static GPU_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
+
struct Gpu {
device: wgpu::Device,
queue: wgpu::Queue,
@@ -16,12 +20,14 @@ struct Gpu {
/// The same device with a simulated texture limit of [`PAGED_LIMIT`]: every check also runs
/// through layer pages and per-cell effect maps.
paged: Compositor,
+ _lock: std::sync::MutexGuard<'static, ()>,
}
/// Simulated texture limit (pages and chunks of 256 px).
const PAGED_LIMIT: u32 = 256;
fn gpu() -> Option<Gpu> {
+ let lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let instance = wgpu::Instance::default();
let adapter = match pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) {
Ok(a) => a,
@@ -46,14 +52,16 @@ fn gpu() -> Option<Gpu> {
};
let mut paged = Compositor::try_new_with_format(&device, wgpu::TextureFormat::Rgba32Float).ok()?;
paged.set_texture_limit(PAGED_LIMIT);
- Some(Gpu { device, queue, comp, paged })
+ Some(Gpu { device, queue, comp, paged, _lock: lock })
}
/// Any adapter and device, for the fallback-path tests (which don't need 32-bit float targets).
-fn any_device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
+/// The returned guard holds [`GPU_LOCK`] until the device is dropped.
+fn any_device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue, std::sync::MutexGuard<'static, ()>)> {
+ let lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let adapter = pollster::block_on(wgpu::Instance::default().request_adapter(&wgpu::RequestAdapterOptions::default())).ok()?;
let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).ok()?;
- Some((adapter, device, queue))
+ Some((adapter, device, queue, lock))
}
/// Deterministic pseudo-random values in 0..1.
@@ -134,6 +142,14 @@ fn check(g: &mut Gpu, doc: &Document, what: &str) {
diff_rect(g, doc, doc.bounds(), what).unwrap_or_else(|e| panic!("{e}"));
}
+#[test]
+fn first_frame_renders() {
+ // Regression: the first submission after device creation is dropped on some drivers
+ // (RADV), which used to make the very first render come back all zeroes.
+ let Some(mut g) = gpu() else { return };
+ check(&mut g, &base_doc(64, 48), "first frame");
+}
+
#[test]
fn blend_modes() {
let Some(mut g) = gpu() else { return };
@@ -1156,7 +1172,7 @@ fn rgba16f_fallback_path_renders() {
// Rgba16Float. Force that path here (even on a 32f-capable GPU) to prove it works end to end —
// pipeline creation, the accumulation texture, and the half-float readback — within display
// tolerance of the CPU reference. This is the path Intel-Vulkan / limited GPUs take.
- let Some((adapter, device, queue)) = any_device() else { return };
+ let Some((adapter, device, queue, _lock)) = any_device() else { return };
let mut comp = match Compositor::try_new_with_format(&device, wgpu::TextureFormat::Rgba16Float) {
Ok(c) => c,
Err(e) => return eprintln!("skipping: {e}"),
@@ -1186,7 +1202,7 @@ fn rgba16f_fallback_path_renders() {
fn unbuildable_pipelines_are_an_error_not_a_panic() {
// The app falls back to the CPU compositor on Err; a panic here would crash it (as FXC once
// did on D3D12). A depth format can't be a colour target, so its pipelines fail to build.
- let Some((adapter, device, _)) = any_device() else { return };
+ let Some((adapter, device, _, _lock)) = any_device() else { return };
let e = Compositor::try_new_with_format(&device, wgpu::TextureFormat::Depth32Float).err().expect("depth target must fail");
assert!(e.0.contains("pipelines"), "{e}");
// The format the app picks builds (effect maps the adapter can't render are left out, not fatal).