Skip to content

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

fix(gpu): preflight compositor uniform buffer limits (#3931)

zenjabba95dbb01ad9ef
diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs
index 992e26dc3..cd2c77491 100644
--- a/crates/gpu/src/lib.rs
+++ b/crates/gpu/src/lib.rs
@@ -510,6 +510,31 @@ fn grid_rects(r: Rect, step: u32) -> Vec<Rect> {
out
}
+/// Number of rectangles `grid_rects` would produce, without allocating them during preflight.
+fn grid_rect_count(r: Rect, step: u32) -> Result<usize, Unsupported> {
+ if r.is_empty() {
+ return Ok(0);
+ }
+ let s = i64::from(step.max(1));
+ let axis = |lo: i32, hi: i32| usize::try_from((i64::from(hi) - 1).div_euclid(s) - i64::from(lo).div_euclid(s) + 1).ok();
+ axis(r.x0, r.x1).and_then(|x| axis(r.y0, r.y1).and_then(|y| x.checked_mul(y))).ok_or_else(|| Unsupported("GPU compositor chunk count is too large".into()))
+}
+
+/// Host length and GPU capacity for the chunk/pass uniform records, including their u32
+/// dynamic binding offsets. Decline unsupported stacks before an invalid buffer faults the device.
+fn uniform_buffer_len(chunks: usize, passes: usize, limit: u64) -> Result<usize, Unsupported> {
+ let too_large = || Unsupported("GPU compositor uniform buffer is too large".into());
+ let records = chunks.checked_add(passes).and_then(|n| u64::try_from(n).ok()).ok_or_else(too_large)?;
+ let bytes = records.checked_mul(STRIDE).ok_or_else(too_large)?;
+ if bytes > limit {
+ return Err(Unsupported(format!("GPU compositor uniform buffer requires {bytes} bytes; device limit is {limit}")));
+ }
+ if bytes.saturating_sub(STRIDE) > u64::from(u32::MAX) {
+ return Err(Unsupported("GPU compositor uniform offsets exceed u32".into()));
+ }
+ usize::try_from(bytes).map_err(|_| too_large())
+}
+
/// The reach up to which each distance field must be exact (the largest any program asks for).
fn field_reaches(progs: &[fx::MapProgram]) -> HashMap<FieldKind, i32> {
let mut want: HashMap<FieldKind, i32> = HashMap::new();
@@ -915,6 +940,14 @@ impl Compositor {
if region.is_empty() {
return Ok(stats);
}
+ let page = self.page;
+ let mut cells = grid_rects(region, page);
+ // Check every actual page cell before uploads, submissions or sink output. A later
+ // cell can need more chunk records than the first; a failure must leave the device healthy.
+ let buffer_limit = device.limits().max_buffer_size;
+ for part in &cells {
+ uniform_buffer_len(grid_rect_count(*part, self.chunk)?, plan.passes.len(), buffer_limit)?;
+ }
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.
@@ -942,8 +975,6 @@ impl Compositor {
// Page cells; every other frame in reverse, so a repeated full refresh of a document
// larger than the budget starts with the pages still resident.
- let page = self.page;
- let mut cells = grid_rects(region, page);
if self.frame.is_multiple_of(2) {
cells.reverse();
}
@@ -1168,7 +1199,8 @@ impl Compositor {
self.pool.push((t, v));
}
- // Uniforms: chunk records, then one record per pass.
+ // Uniforms: chunk records, then one record per pass. encode_scoped preflight checked
+ // the length, device capacity and dynamic offsets before any GPU work for this frame.
let op_base = chunks.len() as u64 * STRIDE;
let mut data = vec![0u8; (op_base + plan.passes.len() as u64 * STRIDE) as usize];
for (i, c) in chunks.iter().enumerate() {
@@ -2257,6 +2289,17 @@ pub fn render_to_vec_stats(
mod tests {
use super::*;
+ #[test]
+ fn uniform_buffer_sizes_are_checked_before_allocation() {
+ assert_eq!(uniform_buffer_len(1, 255, 65_536).unwrap(), 65_536);
+ assert!(uniform_buffer_len(1, 255, 65_535).is_err());
+ assert!(uniform_buffer_len(usize::MAX, 1, u64::MAX).is_err());
+ assert!(uniform_buffer_len(usize::MAX, 0, u64::MAX).is_err());
+ assert!(uniform_buffer_len(1, 16_777_216, u64::MAX).is_err(), "the last record offset cannot be represented by u32");
+ #[cfg(target_pointer_width = "64")]
+ assert_eq!(uniform_buffer_len(1, 16_777_215, u64::MAX).unwrap(), 4_294_967_296, "the last valid u32 dynamic offset is accepted");
+ }
+
#[test]
fn half_roundtrip() {
for v in [0.0f32, 1.0, 0.5, 0.25, 1.0 / 255.0, 0.333, 65504.0, -2.5, 1e-6] {
diff --git a/crates/gpu/tests/uniform_limits.rs b/crates/gpu/tests/uniform_limits.rs
new file mode 100644
index 000000000..aea8fb6b1
--- /dev/null
+++ b/crates/gpu/tests/uniform_limits.rs
@@ -0,0 +1,146 @@
+//! A deep stack that cannot fit the device's uniform buffer must fall back without panicking.
+
+use photocraft_color::{Color, ColorMode, SampleType};
+use photocraft_doc::{Document, Fill, Layer, LayerContent};
+use photocraft_geom::Size;
+use photocraft_gpu::{Compositor, DeviceHealth, plan, render_to_vec};
+
+static GPU_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
+
+fn fills(count: usize) -> Document {
+ fills_size(count, Size::new(1, 1))
+}
+
+fn fills_size(count: usize, size: Size) -> Document {
+ let mut doc = Document::new("Small deep stack", size, ColorMode::Rgb, SampleType::U8);
+ for _ in 0..count {
+ doc.layers.push(Layer::new("Translucent fill", LayerContent::Fill(Fill::Solid(Color::rgba(0.25, 0.5, 0.75, 0.5)))));
+ }
+ doc
+}
+
+fn constrained_device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
+ let instance = wgpu::Instance::default();
+ let adapter = match pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) {
+ Ok(adapter) => adapter,
+ Err(error) => {
+ eprintln!("skipping uniform-limit test: no GPU adapter ({error})");
+ return None;
+ }
+ };
+ let limits =
+ wgpu::Limits { max_buffer_size: 65_536, max_storage_buffer_binding_size: 65_536, max_uniform_buffer_binding_size: 65_536, ..Default::default() };
+ let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor { required_limits: limits, ..Default::default() }))
+ .expect("the supported reduced device limits must create a device");
+ assert_eq!(device.limits().max_buffer_size, 65_536);
+ Some((adapter, device, queue))
+}
+
+fn compositor(adapter: &wgpu::Adapter, device: &wgpu::Device) -> Compositor {
+ let mut compositor =
+ Compositor::try_new_with_format(device, Compositor::preferred_acc_format(adapter)).expect("the reduced-limit device supports the compositor");
+ compositor.set_texture_limit(256);
+ compositor
+}
+
+fn healthy_control(compositor: &mut Compositor, device: &wgpu::Device, queue: &wgpu::Queue) {
+ let small = fills(2);
+ let expected = photocraft_compose::flatten(&small);
+ let actual = render_to_vec(compositor, device, queue, &small, small.bounds()).expect("a fitting stack renders on the same device");
+ assert_eq!(actual.len(), 1);
+ for (cpu, gpu) in expected.px[0].iter().zip(actual[0]) {
+ assert!((cpu - gpu).abs() <= 1.0 / 255.0, "the healthy small-stack CPU control differs: {cpu} vs {gpu}");
+ }
+}
+
+#[test]
+fn a_deep_stack_above_the_device_buffer_limit_returns_an_error_without_panicking() {
+ let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
+ let Some((adapter, device, queue)) = constrained_device() else { return };
+ let mut compositor = compositor(&adapter, &device);
+ healthy_control(&mut compositor, &device, &queue);
+ let deep = fills(150);
+ eprintln!(
+ "uniform-limit reproduction: backend={:?}, buffer limit={}, page={}, passes={}",
+ adapter.get_info().backend,
+ device.limits().max_buffer_size,
+ compositor.page_size(),
+ plan(&deep).unwrap().passes.len()
+ );
+ let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| render_to_vec(&mut compositor, &device, &queue, &deep, deep.bounds())));
+ assert!(result.is_ok(), "rendering an oversized uniform buffer must return Unsupported, not panic");
+ assert!(result.unwrap().is_err(), "a stack above the device's buffer limit must return Unsupported");
+}
+
+#[test]
+fn an_oversized_stack_falls_back_without_faulting_the_watched_device() {
+ let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
+ let Some((adapter, device, queue)) = constrained_device() else { return };
+ let health = DeviceHealth::watch(&device);
+ let mut compositor = compositor(&adapter, &device);
+ compositor.set_health(health.clone());
+ healthy_control(&mut compositor, &device, &queue);
+ let deep = fills(150);
+ let fallback = match render_to_vec(&mut compositor, &device, &queue, &deep, deep.bounds()) {
+ Ok(_) => panic!("the oversized stack must return Unsupported for CPU fallback"),
+ Err(_) => photocraft_compose::flatten(&deep),
+ };
+ for (actual, expected) in fallback.px[0].iter().zip([0.25, 0.5, 0.75, 1.0]) {
+ assert!((actual - expected).abs() <= 1.0 / 255.0, "CPU fallback preserves the translucent-fill composite: {actual} vs {expected}");
+ }
+ let small = fills(2);
+ let next = render_to_vec(&mut compositor, &device, &queue, &small, small.bounds());
+ eprintln!("watched uniform-limit reproduction: errors={}, fault={:?}", health.errors(), health.fault());
+ eprintln!("next fitting stack after fallback: {next:?}");
+ assert!(health.is_ok(), "the unsupported stack must leave the device healthy: {:?}", health.fault());
+ assert_eq!(health.errors(), 0, "no invalid GPU operation is issued before falling back");
+ healthy_control(&mut compositor, &device, &queue);
+}
+
+#[test]
+fn the_exact_buffer_limit_still_renders_with_cpu_parity() {
+ let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
+ let Some((adapter, device, queue)) = constrained_device() else { return };
+ let health = DeviceHealth::watch(&device);
+ let mut compositor = compositor(&adapter, &device);
+ compositor.set_health(health.clone());
+ let boundary = fills(127);
+ assert_eq!(plan(&boundary).unwrap().passes.len(), 255, "one chunk plus255passes uses exactly65536bytes");
+ let expected = photocraft_compose::flatten(&boundary);
+ let actual = render_to_vec(&mut compositor, &device, &queue, &boundary, boundary.bounds()).expect("the exact supported buffer boundary is accepted");
+ assert_eq!(actual.len(), 1);
+ for (cpu, gpu) in expected.px[0].iter().zip(actual[0]) {
+ assert!((cpu - gpu).abs() <= 1.0 / 255.0, "the boundary render differs from CPU: {cpu} vs {gpu}");
+ }
+ assert_eq!(health.errors(), 0);
+ assert!(health.is_ok());
+}
+
+#[test]
+fn later_oversized_page_cells_are_rejected_before_render_or_encode_calls_the_sink() {
+ let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
+ let Some((adapter, device, queue)) = constrained_device() else { return };
+ let health = DeviceHealth::watch(&device);
+ let doc = fills_size(126, Size::new(4096, 2048));
+ let region = photocraft_geom::Rect::new(2047, 0, 4096, 2048);
+ assert_eq!(plan(&doc).unwrap().passes.len(), 253);
+ // Default pages are2048, chunks1024: the narrow first page has2chunks and fits;
+ // the later full page has4chunks and would exceed the same65536byte device limit.
+ for encode_only in [false, true] {
+ let mut compositor = Compositor::try_new_with_format(&device, Compositor::preferred_acc_format(&adapter)).unwrap();
+ compositor.set_health(health.clone());
+ assert_eq!(compositor.page_size(), 2048);
+ let mut calls = 0;
+ let mut sink = |_: &mut wgpu::CommandEncoder, _: photocraft_gpu::ChunkOut<'_>| calls += 1;
+ let result = if encode_only {
+ let mut encoder = device.create_command_encoder(&Default::default());
+ compositor.encode(&device, &queue, &mut encoder, &doc, region, &mut sink)
+ } else {
+ compositor.render(&device, &queue, &doc, region, &mut sink)
+ };
+ assert!(result.is_err(), "a later cell's uniform requirement must trigger CPU fallback");
+ assert_eq!(calls, 0, "preflight must reject the whole render before any partial sink output");
+ assert_eq!(health.errors(), 0);
+ assert!(health.is_ok());
+ }
+}