`media::develop` renders on the GPU when available (views, render_now, exports;
thumbnails stay on the CPU) and falls back to the CPU otherwise. `app.gpu
{enabled?}` toggles it at runtime, `LIGHTCRAFT_GPU=0` disables it for the process,
`ui.inspect` reports the adapter under perf.gpu.
Performance: dropped device buffers are recycled once their thread has submitted
(fresh 100-300 MB allocations per pass cost as much as the passes), Gaussian
passes ping-pong between two buffers and read all channels of a pixel at once,
and long box radii slide over longer chunks. LIGHTCRAFT_PROFILE prints real GPU
stage timings. render_bench prints CPU and GPU columns and the CPU/GPU difference.
24 MP ARW, loaded machine, min of 7: cold loupe 415 -> 32 ms, exposure drag
22-42 -> 3.9 ms, NR drag (1920) 301+ -> 18 ms, export render 1037+ -> 291 ms;
max 1 LSB difference. docs/gpu-pipeline.md describes the implementation.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
120 lines
4.3 KiB
Rust
120 lines
4.3 KiB
Rust
//! The LightCraft develop pipeline on the GPU (wgpu compute, WGSL kernels).
|
|
//!
|
|
//! The CPU pipeline (`lightcraft-pipeline`) is the reference: every kernel here is a port of a CPU
|
|
//! stage, both read the same resolved parameters ([`lightcraft_pipeline::Plan`],
|
|
//! [`lightcraft_pipeline::finish::FinishParams`]), and the equivalence tests (`tests/`) render the
|
|
//! same settings on both and bound the difference in 8-bit sRGB. Stages without a kernel run on the
|
|
//! CPU inside the same render (per-stage hybrid); see `docs/gpu-pipeline.md`.
|
|
//!
|
|
//! Use [`render`]: it returns `None` when the GPU is unavailable, disabled (`LIGHTCRAFT_GPU=0` or
|
|
//! [`set_enabled`]), or the render does not fit the device — callers then render on the CPU.
|
|
//! The browser build has no GPU path yet (WebGPU device creation is asynchronous): everything here
|
|
//! compiles to the CPU fallback on wasm32.
|
|
#![forbid(unsafe_code)]
|
|
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use lightcraft_develop::DevelopSettings;
|
|
use lightcraft_pipeline::{RenderRequest, Rendered, SourceInfo, StageCache};
|
|
use lightcraft_raster::Rgb32f;
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
mod ctx;
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
mod params;
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
mod render;
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub use render::GpuStages;
|
|
|
|
static ENABLED: AtomicBool = AtomicBool::new(true);
|
|
/// Set when a GPU render failed: the process stays on the CPU from then on.
|
|
static BROKEN: AtomicBool = AtomicBool::new(false);
|
|
|
|
/// Stop using the GPU for the rest of the process (after a device error).
|
|
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
|
|
fn mark_broken() {
|
|
BROKEN.store(true, Ordering::Relaxed);
|
|
}
|
|
|
|
/// Allow or forbid GPU rendering at runtime (a preference). `LIGHTCRAFT_GPU=0` forbids it for the
|
|
/// whole process regardless.
|
|
pub fn set_enabled(on: bool) {
|
|
ENABLED.store(on, Ordering::Relaxed);
|
|
}
|
|
|
|
/// Whether GPU rendering is allowed (environment, preference, no earlier failure).
|
|
pub fn enabled() -> bool {
|
|
ENABLED.load(Ordering::Relaxed) && !BROKEN.load(Ordering::Relaxed) && !env_disabled()
|
|
}
|
|
|
|
fn env_disabled() -> bool {
|
|
static OFF: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
|
*OFF.get_or_init(|| std::env::var("LIGHTCRAFT_GPU").is_ok_and(|v| matches!(v.trim(), "0" | "off" | "false" | "no")))
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub(crate) fn device() -> Option<&'static ctx::Gpu> {
|
|
static GPU: std::sync::OnceLock<Option<ctx::Gpu>> = std::sync::OnceLock::new();
|
|
if env_disabled() {
|
|
return None;
|
|
}
|
|
GPU.get_or_init(|| std::panic::catch_unwind(ctx::Gpu::new).ok().flatten()).as_ref()
|
|
}
|
|
|
|
/// Whether a usable GPU adapter exists and GPU rendering is enabled (creates the device on first
|
|
/// use).
|
|
pub fn available() -> bool {
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
{
|
|
enabled() && device().is_some()
|
|
}
|
|
#[cfg(target_arch = "wasm32")]
|
|
{
|
|
false
|
|
}
|
|
}
|
|
|
|
/// The adapter's name and backend, e.g. "Apple M4 Pro (Metal)".
|
|
pub fn adapter_name() -> Option<String> {
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
{
|
|
device().map(|g| format!("{} ({:?})", g.info.name, g.info.backend))
|
|
}
|
|
#[cfg(target_arch = "wasm32")]
|
|
{
|
|
None
|
|
}
|
|
}
|
|
|
|
/// Render `src` with `s` on the GPU, reusing the device-resident stages kept with `stages` (the
|
|
/// view's CPU stage cache). `None`: render on the CPU instead.
|
|
pub fn render(src: &Arc<Rgb32f>, info: &SourceInfo, s: &DevelopSettings, req: &RenderRequest, stages: Option<&StageCache>) -> Option<Rendered> {
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
{
|
|
if !enabled() {
|
|
return None;
|
|
}
|
|
let gpu = device()?;
|
|
let ext = stages.map(|c| c.extension::<GpuStages>());
|
|
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| render::render(gpu, src, info, s, req, ext.as_deref())));
|
|
match r {
|
|
// a device error during the render: its result is not trustworthy
|
|
Ok(_) if BROKEN.load(Ordering::Relaxed) => None,
|
|
Ok(r) => r,
|
|
Err(_) => {
|
|
log::error!("gpu: render failed; using the CPU pipeline from now on");
|
|
BROKEN.store(true, Ordering::Relaxed);
|
|
None
|
|
}
|
|
}
|
|
}
|
|
#[cfg(target_arch = "wasm32")]
|
|
{
|
|
let _ = (src, info, s, req, stages);
|
|
None
|
|
}
|
|
}
|