Files
aiongg c55c0b7ce8 gpu: build each compositor pipeline on first use (#15439)
Every compositor built all ~30 pipelines when created. On the Windows CI
runners (WARP, FXC) that took 3 to 7.5 s in every GPU test process, and a
test that draws one layer needs two or three of them.

The pipelines now sit in one table per compositor (shared with its
siblings), one entry per kernel, each built the first time a draw needs
it. try_new_with_format_threads(_classified), which the app uses for its
background build, still builds every pipeline up front in parallel
through the same table, so the app keeps its start-up error and its
cached deterministic-failure path. A pipeline that fails on first use
makes that render Unsupported (CPU fallback); a failed effect-map
pipeline turns layer effects to the CPU as before.

The #9173 rules hold: each kernel has its own vertex entry point, a
kernel's formats are built on one thread, and every build in the process
takes the same lock.

every_compositor_pipeline_builds builds all pipelines through the
threaded build, so FXC still compiles every shader on Windows CI. New
count test: one layer builds a small share of the pipelines, and a second
render or a sibling builds none.
2026-11-18 18:51:38 +00:00

103 lines
5.4 KiB
Rust

//! #6419: the compositor's pipelines are built on several threads, or one by one on first use
//! (#15439). A pipeline the device can't build is an `Err` from the build or the draw, not a panic
//! from wgpu's default error handler (error scopes are per thread). Skips when no GPU adapter
//! exists.
use std::sync::Arc;
use photocraft_color::{Color, ColorMode, SampleType};
use photocraft_doc::Document;
use photocraft_geom::Size;
use photocraft_gpu::{Compositor, render_to_vec};
use crate::GPU_LOCK;
fn device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
let instance = crate::instance();
let Ok(adapter) = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) else {
eprintln!("skipping: no adapter");
return None;
};
let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).ok()?;
Some((adapter, device, queue))
}
#[test]
fn a_pipeline_built_on_another_thread_fails_as_an_error() {
let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let Some((adapter, device, _queue)) = device() else { return };
// Never a render target, so every accumulation pipeline fails validation.
let Err(e) = Compositor::try_new_with_format_threads(&device, wgpu::TextureFormat::Rgb9e5Ufloat, 4) else {
panic!("a compositor accumulating into Rgb9e5Ufloat");
};
assert!(e.0.contains("couldn't build the compositor's pipelines"), "{e:?}");
// The device is still usable for a compositor it can build.
assert!(Compositor::try_new_with_format_threads(&device, Compositor::preferred_acc_format(&adapter), 4).is_ok());
}
/// A compositor whose pipelines fail to build on first use, on a device whose errors go to the
/// caller's handler: the draw is an `Err`, not a panic.
#[test]
fn a_compositor_with_failed_pipelines_draws_without_panicking() {
let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let Some((_adapter, device, queue)) = device() else { return };
// As the app does (`DeviceHealth::watch`): GPU errors go to a handler, not wgpu's default panic.
device.on_uncaptured_error(Arc::new(|_: wgpu::Error| {}));
let mut comp = Compositor::new_with_format(&device, wgpu::TextureFormat::Rgb9e5Ufloat);
let doc = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::rgba(0.5, 0.5, 0.5, 1.0));
// Drawing into an unrenderable format can't succeed, but must not panic.
assert!(render_to_vec(&mut comp, &device, &queue, &doc, doc.bounds()).is_err());
}
/// #9173: the threaded build builds the compositor's pipelines on several threads (#6419). On Direct3D 12 with FXC,
/// wgpu-hal caches each compiled shader as an `ID3DBlob` whose reference count isn't atomic
/// (gfx-rs/wgpu#10547), so two threads building pipelines from the same shader could free a blob
/// the cache still holds; dropping the device then aborted the process (`null reference
/// produced` in `IUnknown::drop`). Each thread now builds from shaders no other thread uses.
///
/// Stress test, ignored by default (it builds many compositors). Run it on WARP with FXC, e.g.
/// `WGPU_BACKEND=dx12 WGPU_DX12_COMPILER=fxc WGPU_ADAPTER_NAME="basic render"
/// PC_BUILD_ROUNDS=10 PC_BUILDS_PER_DEVICE=50
/// cargo test -p photocraft-gpu --test it parallel_compositor_builds -- --ignored`.
/// Windows' full page heap makes the race abort more reliably.
#[test]
#[ignore = "stress test: builds many compositors"]
fn parallel_compositor_builds_leave_the_shader_cache_intact() {
let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let var = |name: &str, default: u32| std::env::var(name).ok().and_then(|v| v.parse::<u32>().ok()).filter(|&n| n > 0).unwrap_or(default);
let (rounds, builds) = (var("PC_BUILD_ROUNDS", 4), var("PC_BUILDS_PER_DEVICE", 8));
for round in 0..rounds {
let instance = crate::instance();
let Ok(adapter) = pollster::block_on(wgpu::util::initialize_adapter_from_env_or_default(&instance, None)) else {
eprintln!("skipping: no adapter");
return;
};
if round == 0 {
let info = adapter.get_info();
eprintln!("shader cache stress: {:?} ({}), {rounds} devices x {builds} builds", info.backend, info.name);
}
let Ok((device, _queue)) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())) else {
eprintln!("skipping: no device");
return;
};
let format = Compositor::preferred_acc_format(&adapter);
// The first build compiles every shader; the rest are cache hits, so their threads take
// and release references to the cached shaders densely. The builds start together, so
// overlapping builds are exercised too (the process-wide lock).
std::thread::scope(|scope| {
let handles: Vec<_> = (0..builds)
.map(|_| {
scope.spawn(|| {
let built = Compositor::try_new_with_format_threads(&device, format, 4);
assert!(built.is_ok(), "round {round}: {:?}", built.err());
})
})
.collect();
for handle in handles {
handle.join().expect("compositor build thread panicked");
}
});
// Dropping the device releases every cached blob: a freed one aborts here.
}
}