Files
photocraft/crates/gpu/src/context.rs
T
Khawar Zamman WaniandFredApps 1089932ae9 Add a headless GPU backend for composition and flat export (#10842)
* Add headless GPU backend foundation for composition and flat export (issue #8062 PR 1)

* Fix headless GPU readback bounds, API compliance, and error preservation (#8062)

* Fix GPU readback bounds, context thread spawn error, and test parity (#8062)

* Fix BandRenderer Send bound, PNG reborrows, and clippy lints (#8062)

* Fix effect shadow test construction and remove dead code wrappers (#8062)

* Use short reborrows for Headless renderer without lifetime escaping (#8062)

* Fix clippy collapsible_if and ensure multi-band geometry in flat export tests (#8062)

* Fix automation Rect import, add renderer diagnostics, and harden flat export retries (#8062)

* Fix GPU feature test APIs and native target gates

* Verify unsupported GPU effects before testing band seams

* Use compositor buffer ownership API after upstream memory accounting change

* Update export renderer test helper for owned compositor buffers

* Fix headless GPU backend review findings (#10842)

- release a closed document's GPU caches through a BandRenderer hook
- bound the monitored readback wait and report a stalled GPU as device loss
- reject 32-bit float documents before band readback so exports fall back to CPU
- keep previews at 8-bit RGBA and report the palette/Duotone renderer bypass
- retain renderer diagnostics when encoding fails
- strengthen renderer, save-path and GPU diagnostics tests

* Add GPU close-hook and 8-bit preview regression tests (#10842)

* fix(io): correct renderer binding mutability

* Fix remaining headless GPU review findings (#10842)

- release a document's GPU caches when file.close, file.closeAll or
  file.closeOthers removes it through a command
- honor cancellation while band readback waits, polling in 100 ms slices
- fault the device after a readback timeout so later exports skip it
- report single-layer, CMYK ink-mixing, ORA and PDF CPU paths to a
  requested renderer
- keep fallback warnings in their export instead of leaking them into the
  next one
- requeue renderer diagnostics when a renderer-aware save's final write
  fails
- write MCP/serve diagnostics without panicking on a broken stderr
- document the actual colour-space contract and drop F32 from the GPU
  depth list

* Use question mark for cancellable readback errors

* Keep GPU export diagnostics with their attempt and stop bands on cancel

Encode native .pcraft bytes before draining renderer diagnostics, and
requeue export warnings when a workspace write fails, so callers still
report the adapter or fallback reason with the error.

Move a failed GPU attempt's queued diagnostics into its CPU retry result
instead of leaving them for a later successful export to report.

Stop GPU band readback at the next band after cancellation, and check the
interrupt before each CpuRenderer sink delivery.

Band readback uploads half-precision textures, so reject U16 alongside
F32: their samples cannot survive the upload. Tighten the 8-bit CPU/GPU
parity checks to 1/255.

* Keep headless export warnings when a workspace write fails (#10842)

A capability-scoped save encodes the export, then writes it; when the write
fails, the export warnings (folded together with renderer diagnostics) were
pushed back through `BandRenderer::push_diagnostic`, whose default is a no-op.
A custom renderer that overrides only `drain_diagnostics` therefore lost the
adapter or fallback reason that belonged with the error.

Keep them in `Headless` instead and return them from the next
`drain_diagnostics`, alongside the renderer's own queue. The regression uses a
renderer without a `push_diagnostic` override and checks the warnings survive
the failed write and drain exactly once.

Also name the actual source of the depth limit in the headless GPU matrix:
layer content is uploaded as half-precision textures, not band readback.

* Stop CPU bands and CPU retries on cancellation (#10912)

The CPU retry after a retryable GPU error composes through
`FlatSource::bands`, whose shared `emit` sink did not poll the export
interrupt: a cancel was only observed after the whole composite finished.
Check it once per band, as `CpuRenderer` does, for both RGB and CMYK ink
bands. The flat and streamed-PNG retry branches also check before starting
the full CPU composite again, so a cancelled export returns
`IoError::Cancelled` instead of re-rendering the document.

Tests: band-sink cancellation for RGB and CMYK inks, a renderer call counter
proving a second export reaches the renderer, and a pending `wait_slices`
poll observing cancellation set mid-wait.

* Merge origin/main into GPU foundation (#10842)

* Keep GPU-path diagnostics on every failure path, honour cancel in native copy

- save: export warnings of a failed local write stay in Headless pending diagnostics
- render: preview fallback warnings are returned, drained, and added to the doc.render reply
- io: renderer-neutral fallback wording; native single-layer copy checks cancellation per band
- gpu: gate wait_slices tests off wasm32 (#10842)

---------

Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
2026-11-05 04:53:25 +01:00

228 lines
8.5 KiB
Rust

//! Native windowless wgpu context creation for headless composition and export.
use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use crate::health::DeviceHealth;
use crate::{Compositor, Unsupported};
/// Errors encountered when creating or inspecting a native GPU context.
#[derive(Debug, thiserror::Error)]
pub enum GpuContextError {
#[error("no suitable GPU adapter found: {0}")]
NoAdapter(String),
#[error("failed to request GPU device: {0}")]
RequestDevice(String),
#[error("failed to initialize GPU compositor: {0}")]
Compositor(String),
#[error("GPU device was lost: {0}")]
DeviceLost(String),
}
impl From<Unsupported> for GpuContextError {
fn from(e: Unsupported) -> Self {
GpuContextError::Compositor(e.0)
}
}
/// Configuration options for creating a [`NativeContext`].
#[derive(Clone, Debug)]
pub struct NativeContextOptions {
/// Desired adapter power tier. Default is [`wgpu::PowerPreference::HighPerformance`]
/// to prefer discrete/dedicated hardware GPUs.
pub power_preference: wgpu::PowerPreference,
/// Force software/fallback adapter selection (e.g. WARP/llvmpipe).
pub force_fallback_adapter: bool,
/// Allowed backend flags. Default is all available native backends.
pub backends: wgpu::Backends,
/// Bounded timeout for adapter/device initialization. When set, requests exceeding
/// this duration return [`GpuContextError::NoAdapter`]. Default is 10 seconds.
///
/// # Limitation
/// If initialization times out, the background initialization thread cannot be forcibly terminated
/// in standard Rust and continues detached until wgpu completes or fails. Callers should avoid
/// repeatedly requesting initialization in tight loops when timeouts occur to prevent thread and
/// resource leaks.
pub timeout: Option<Duration>,
}
impl Default for NativeContextOptions {
fn default() -> Self {
Self {
power_preference: wgpu::PowerPreference::HighPerformance,
force_fallback_adapter: false,
backends: wgpu::Backends::all(),
timeout: Some(Duration::from_secs(10)),
}
}
}
/// A headless, windowless GPU context managing a [`wgpu::Device`], [`wgpu::Queue`],
/// and device health monitor.
#[derive(Clone)]
pub struct NativeContext {
device: Arc<wgpu::Device>,
queue: Arc<wgpu::Queue>,
adapter_info: wgpu::AdapterInfo,
health: DeviceHealth,
acc_format: wgpu::TextureFormat,
limits: wgpu::Limits,
}
impl fmt::Debug for NativeContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("NativeContext")
.field("adapter", &self.adapter_info.name)
.field("backend", &self.adapter_info.backend)
.field("device_type", &self.adapter_info.device_type)
.field("acc_format", &self.acc_format)
.field("healthy", &self.is_healthy())
.finish()
}
}
impl NativeContext {
/// Create a windowless GPU context using default high-performance options.
pub fn new() -> Result<Self, GpuContextError> {
Self::request(&NativeContextOptions::default())
}
/// Request an adapter and device matching `options`.
pub fn request(options: &NativeContextOptions) -> Result<Self, GpuContextError> {
let opts = options.clone();
let init_work = move || -> Result<Self, GpuContextError> {
let instance =
wgpu::Instance::new(wgpu::InstanceDescriptor { backends: opts.backends, ..wgpu::InstanceDescriptor::new_without_display_handle_from_env() });
let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: opts.power_preference,
force_fallback_adapter: opts.force_fallback_adapter,
compatible_surface: None,
..Default::default()
}))
.or_else(|e| {
if !opts.force_fallback_adapter {
pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::None,
force_fallback_adapter: true,
compatible_surface: None,
..Default::default()
}))
} else {
Err(e)
}
})
.map_err(|e| GpuContextError::NoAdapter(format!("request_adapter failed: {e}")))?;
let info = adapter.get_info();
log::info!("GPU native context: selected adapter '{}' ({:?}, {:?})", info.name, info.backend, info.device_type);
let limits = adapter.limits();
let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
label: Some("pc_headless_gpu"),
required_features: wgpu::Features::empty(),
required_limits: limits.clone(),
..Default::default()
}))
.map_err(|e| GpuContextError::RequestDevice(format!("request_device failed for '{}': {e}", info.name)))?;
let health = DeviceHealth::watch(&device);
let acc_format = Compositor::preferred_acc_format(&adapter);
Ok(Self { device: Arc::new(device), queue: Arc::new(queue), adapter_info: info, health, acc_format, limits })
};
if let Some(timeout) = options.timeout {
let (tx, rx) = std::sync::mpsc::channel();
let thread_res = std::thread::Builder::new().name("pc_gpu_init".into()).spawn(move || {
let res = init_work();
let _ = tx.send(res);
});
match thread_res {
Ok(_handle) => match rx.recv_timeout(timeout) {
Ok(res) => res,
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
Err(GpuContextError::NoAdapter(format!("timed out after {timeout:?} requesting GPU adapter/device")))
}
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => Err(GpuContextError::NoAdapter("GPU initialization thread disconnected".into())),
},
Err(e) => Err(GpuContextError::NoAdapter(format!("failed to spawn GPU initialization thread: {e}"))),
}
} else {
init_work()
}
}
/// Build a new [`Compositor`] wired to this context's device and health flag.
pub fn create_compositor(&self) -> Result<Compositor, GpuContextError> {
if !self.is_healthy() {
let fault = self.health.fault().map_or_else(|| "GPU device lost".into(), |f| f.to_string());
return Err(GpuContextError::DeviceLost(fault));
}
let mut comp = Compositor::try_new_with_format(&self.device, self.acc_format)?;
comp.set_health(self.health.clone());
Ok(comp)
}
/// Whether this adapter is a CPU software rasterizer (e.g. llvmpipe, Lavapipe, WARP).
pub fn is_software(&self) -> bool {
self.adapter_info.device_type == wgpu::DeviceType::Cpu
}
/// Human-readable adapter name reported by the driver.
pub fn adapter_name(&self) -> &str {
&self.adapter_info.name
}
/// Backend graphics API in use (Vulkan, Metal, Dx12, etc.).
pub fn backend(&self) -> wgpu::Backend {
self.adapter_info.backend
}
/// The underlying [`wgpu::Device`].
pub fn device(&self) -> &wgpu::Device {
&self.device
}
/// Arc clone of the device handle.
pub fn device_arc(&self) -> Arc<wgpu::Device> {
self.device.clone()
}
/// The underlying [`wgpu::Queue`].
pub fn queue(&self) -> &wgpu::Queue {
&self.queue
}
/// Arc clone of the queue handle.
pub fn queue_arc(&self) -> Arc<wgpu::Queue> {
self.queue.clone()
}
/// Shared device health monitor.
pub fn health(&self) -> &DeviceHealth {
&self.health
}
/// Preferred accumulation texture format for this adapter (`Rgba32Float` or `Rgba16Float`).
pub fn acc_format(&self) -> wgpu::TextureFormat {
self.acc_format
}
/// Effective device limits.
pub fn limits(&self) -> &wgpu::Limits {
&self.limits
}
/// Maximum 2D texture side dimension supported by this device.
pub fn max_texture_dimension_2d(&self) -> u32 {
self.limits.max_texture_dimension_2d
}
/// Whether the device is healthy and has not reported a lost device or uncaptured error.
pub fn is_healthy(&self) -> bool {
self.health.is_ok()
}
}