diff --git a/crates/gpu/src/context.rs b/crates/gpu/src/context.rs index d074fe213..cce00bc7f 100644 --- a/crates/gpu/src/context.rs +++ b/crates/gpu/src/context.rs @@ -1207,17 +1207,25 @@ impl GpuContext { if !ticket.active() { return; } + let mapping = Arc::new(crate::readback::Mapping::new()); let b = buffer.clone(); + let callback_mapping = Arc::clone(&mapping); buffer.map_async(wgpu::MapMode::Read, .., move |r| { - if !ticket.active() { - b.unmap(); + if let Err(e) = r { + callback_mapping.failed(); + ticket.complete(Err(format!("GPU buffer mapping: {e}"))); return; } - let result = r.map_err(|e| format!("GPU buffer mapping: {e}")).and_then(|_| { - let view = b.get_mapped_range(..).map_err(|e| format!("GPU mapped range: {e}"))?; - crate::readback::packed(&view, row, stride, height) - }); - b.unmap(); + if !callback_mapping.mapped() { + return; + } + if !ticket.active() { + callback_mapping.unmap(|| b.unmap()); + return; + } + let result = + b.get_mapped_range(..).map_err(|e| format!("GPU mapped range: {e}")).and_then(|view| crate::readback::packed(&view, row, stride, height)); + callback_mapping.unmap(|| b.unmap()); ticket.complete(result); }); } @@ -1264,20 +1272,30 @@ impl GpuContext { if !ticket.active() { return Err("GPU readback cancelled".into()); } + let mapping = Arc::new(crate::readback::Mapping::new()); let b = buffer.clone(); + let callback_mapping = Arc::clone(&mapping); buffer.map_async(wgpu::MapMode::Read, .., move |r| { + if let Err(e) = r { + callback_mapping.failed(); + ticket.complete(Err(format!("GPU buffer mapping: {e}"))); + return; + } + if !callback_mapping.mapped() { + return; + } // Timed out or cancelled: nobody reads the mapping. if !ticket.active() { - b.unmap(); + callback_mapping.unmap(|| b.unmap()); return; } // Mapped: the waiting thread reads the rows and unmaps (an empty vector = mapped). - ticket.complete(r.map(|_| Vec::new()).map_err(|e| format!("GPU buffer mapping: {e}"))); + ticket.complete(Ok(Vec::new())); }); - // On any failure, unmap (harmless when not mapped; cancels a pending map) so a mapped - // buffer can never be handed out again. + // A timeout can cancel a pending map while its callback is being dispatched. The shared + // guard makes sure the callback and waiter cannot both unmap the same buffer. let fail = |e: String| { - buffer.unmap(); + mapping.unmap(|| buffer.unmap()); e }; self.wait_mapped(&rx, submission).map_err(fail)?.map_err(fail)?; @@ -1285,7 +1303,7 @@ impl GpuContext { let view = buffer.get_mapped_range(..).map_err(|e| format!("GPU mapped range: {e}")); view.and_then(|view| read(&view)) }; - buffer.unmap(); + mapping.unmap(|| buffer.unmap()); result } diff --git a/crates/gpu/src/readback.rs b/crates/gpu/src/readback.rs index 290ae08de..51d941640 100644 --- a/crates/gpu/src/readback.rs +++ b/crates/gpu/src/readback.rs @@ -4,6 +4,7 @@ use std::collections::BTreeMap; #[cfg(not(target_arch = "wasm32"))] use std::sync::Condvar; +use std::sync::atomic::{AtomicU8, Ordering}; use std::sync::{Arc, Mutex, Weak}; use std::time::Duration; @@ -92,6 +93,48 @@ pub(crate) struct Ticket { id: u64, } +/// Tracks one GPU buffer mapping shared by the callback and its waiter. A timeout can race a +/// late callback, so the buffer must be unmapped exactly once. A failed map is never unmapped. +pub(crate) struct Mapping(AtomicU8); + +impl Mapping { + const PENDING: u8 = 0; + const MAPPED: u8 = 1; + const FAILED: u8 = 2; + const FINISHED: u8 = 3; + + pub(crate) fn new() -> Self { + Self(AtomicU8::new(Self::PENDING)) + } + + /// Claim a successful map callback. False means a timeout already cancelled the map. + pub(crate) fn mapped(&self) -> bool { + self.0.compare_exchange(Self::PENDING, Self::MAPPED, Ordering::AcqRel, Ordering::Acquire).is_ok() + } + + /// Record a failed map so the waiter will not unmap a buffer that was never mapped. + pub(crate) fn failed(&self) { + let _ = self.0.compare_exchange(Self::PENDING, Self::FAILED, Ordering::AcqRel, Ordering::Acquire); + } + + /// Unmap once if mapping is pending (cancel it) or mapped (release it). + pub(crate) fn unmap(&self, unmap: impl FnOnce()) { + let mut unmap = Some(unmap); + loop { + let state = self.0.load(Ordering::Acquire); + if state != Self::PENDING && state != Self::MAPPED { + return; + } + if self.0.compare_exchange(state, Self::FINISHED, Ordering::AcqRel, Ordering::Acquire).is_ok() { + if let Some(unmap) = unmap.take() { + unmap(); + } + return; + } + } + } +} + /// User callbacks run outside locks and are isolated one at a time. On panic=abort /// (including the usual wasm build), Rust panics cannot be caught: the host must /// avoid panicking callbacks. This branch does not claim abort-mode recovery. @@ -402,6 +445,83 @@ mod tests { use super::*; use std::sync::atomic::{AtomicUsize, Ordering}; + #[test] + fn timeout_racing_late_map_callback_unmaps_once() { + let mapping = Arc::new(Mapping::new()); + let unmap_calls = Arc::new(AtomicUsize::new(0)); + let barrier = Arc::new(std::sync::Barrier::new(3)); + + let timeout_mapping = Arc::clone(&mapping); + let timeout_calls = Arc::clone(&unmap_calls); + let timeout_barrier = Arc::clone(&barrier); + let timeout = std::thread::spawn(move || { + timeout_barrier.wait(); + timeout_mapping.unmap(|| { + timeout_calls.fetch_add(1, Ordering::SeqCst); + }); + }); + + let callback_mapping = Arc::clone(&mapping); + let callback_calls = Arc::clone(&unmap_calls); + let callback_barrier = Arc::clone(&barrier); + let callback = std::thread::spawn(move || { + callback_barrier.wait(); + if callback_mapping.mapped() { + callback_mapping.unmap(|| { + callback_calls.fetch_add(1, Ordering::SeqCst); + }); + } + }); + + barrier.wait(); + timeout.join().unwrap(); + callback.join().unwrap(); + assert_eq!(unmap_calls.load(Ordering::SeqCst), 1); + } + + #[test] + fn timeout_before_map_callback_cancels_late_map() { + let mapping = Mapping::new(); + let unmap_calls = AtomicUsize::new(0); + + mapping.unmap(|| { + unmap_calls.fetch_add(1, Ordering::SeqCst); + }); + assert!(!mapping.mapped()); + mapping.unmap(|| { + unmap_calls.fetch_add(1, Ordering::SeqCst); + }); + + assert_eq!(unmap_calls.load(Ordering::SeqCst), 1); + } + + #[test] + fn mapped_callback_and_timeout_release_once() { + let mapping = Mapping::new(); + let unmap_calls = AtomicUsize::new(0); + assert!(mapping.mapped()); + + mapping.unmap(|| { + unmap_calls.fetch_add(1, Ordering::SeqCst); + }); + mapping.unmap(|| { + unmap_calls.fetch_add(1, Ordering::SeqCst); + }); + + assert_eq!(unmap_calls.load(Ordering::SeqCst), 1); + } + + #[test] + fn failed_map_is_not_unmapped() { + let mapping = Mapping::new(); + let unmap_calls = AtomicUsize::new(0); + mapping.failed(); + mapping.unmap(|| { + unmap_calls.fetch_add(1, Ordering::SeqCst); + }); + assert_eq!(unmap_calls.load(Ordering::SeqCst), 0); + } + #[test] fn early_rejection_reports_no_pixels_exactly_once() { let calls = Arc::new(AtomicUsize::new(0)); diff --git a/crates/gpu/src/shaders/fx_lut.wgsl b/crates/gpu/src/shaders/fx_lut.wgsl index 87966df7d..f9081f834 100644 --- a/crates/gpu/src/shaders/fx_lut.wgsl +++ b/crates/gpu/src/shaders/fx_lut.wgsl @@ -130,12 +130,15 @@ fn fxl_shape(at: u32, n: u32, x: f32, flip: bool) -> f32 { // partition_point(|p| p.x < x). var lo = 0u; var hi = n; - while (lo < hi) { - let mid = (lo + hi) / 2u; - if (data[at + mid * 2u + kx] < x) { - lo = mid + 1u; - } else { - hi = mid; + // Bound the partition search as for inverse 1D LUTs below. + for (var step = 0u; step < 32u; step++) { + if (lo < hi) { + let mid = lo + (hi - lo) / 2u; + if (data[at + mid * 2u + kx] < x) { + lo = mid + 1u; + } else { + hi = mid; + } } } let i = clamp(lo, 1u, n - 1u); @@ -156,13 +159,22 @@ fn fxl_shaper_at(s: u32, k: u32) -> vec2 { return vec2(at, u32(data[s + k])); } +// Evaluate independent shaper channels without dynamically writing vector lanes. +fn fxl_shaper_apply(sh: u32, c: vec3, flip: bool) -> vec3 { + let r = fxl_shaper_at(sh, 0u); + let g = fxl_shaper_at(sh, 1u); + let b = fxl_shaper_at(sh, 2u); + return vec3( + fxl_shape(r.x, r.y, c.x, flip), + fxl_shape(g.x, g.y, c.y, flip), + fxl_shape(b.x, b.y, c.z, flip) + ); +} + fn fxl_file_fwd(o: u32, sh: i32, c0: vec3, interp: u32) -> vec3 { var c = c0; if (sh >= 0) { - for (var k = 0u; k < 3u; k++) { - let a = fxl_shaper_at(u32(sh), k); - c[k] = fxl_shape(a.x, a.y, c[k], false); - } + c = fxl_shaper_apply(u32(sh), c, false); } return fxl_lut_apply(o, c, interp); } @@ -176,12 +188,16 @@ fn fxl_inv_1d(o: u32, i: u32, y: f32) -> f32 { // partition_point over the table's values. var lo = 0u; var hi = n; - while (lo < hi) { - let mid = (lo + hi) / 2u; - if (data[b1 + mid * 3u + i] < y) { - lo = mid + 1u; - } else { - hi = mid; + // A u32 interval needs at most 32 halvings. A fixed trip count avoids + // a divergent data-dependent loop on software Vulkan without changing lookup. + for (var step = 0u; step < 32u; step++) { + if (lo < hi) { + let mid = lo + (hi - lo) / 2u; + if (data[b1 + mid * 3u + i] < y) { + lo = mid + 1u; + } else { + hi = mid; + } } } let k = clamp(lo, 1u, n - 1u); @@ -205,10 +221,7 @@ fn fxl_file(o: u32, sh: i32, c: vec3, interp: u32, inverse: bool) -> vec3(fxl_inv_1d(o, 0u, c.x), fxl_inv_1d(o, 1u, c.y), fxl_inv_1d(o, 2u, c.z)); } if (sh >= 0) { - for (var k = 0u; k < 3u; k++) { - let a = fxl_shaper_at(u32(sh), k); - x[k] = fxl_shape(a.x, a.y, x[k], true); - } + x = fxl_shaper_apply(u32(sh), x, true); } return x; } @@ -444,12 +457,11 @@ fn fxl_run(c0: vec3) -> vec3 { case 1u, 2u: { let tf = u32(data[pc + 1u]); let g = data[pc + 2u]; - for (var i = 0u; i < 3u; i++) { - if (op == 1u) { - c[i] = fxl_decode(tf, g, c[i]); - } else { - c[i] = fxl_encode(tf, g, c[i]); - } + // Explicit components avoid llvmpipe's dynamically indexed vector writes. + if (op == 1u) { + c = vec3(fxl_decode(tf, g, c.x), fxl_decode(tf, g, c.y), fxl_decode(tf, g, c.z)); + } else { + c = vec3(fxl_encode(tf, g, c.x), fxl_encode(tf, g, c.y), fxl_encode(tf, g, c.z)); } pc += 3u; } diff --git a/crates/gpu/src/tests/fx_lut.rs b/crates/gpu/src/tests/fx_lut.rs index b74e90f09..acac88835 100644 --- a/crates/gpu/src/tests/fx_lut.rs +++ b/crates/gpu/src/tests/fx_lut.rs @@ -97,6 +97,35 @@ fn apply_color_lut() { effect_case("ec.utility.applylut", &[("lut", s("/nonexistent/grade.cube"))]); } +// Keep this separate so Nextest runs the inverse lookup in a fresh process/device, +// without the preceding 3D LUT submissions in `ocio_file_transform`. The combined +// test stays intact to cover ordering and shared-device state as well. +#[test] +fn inverse_1d_lut_on_a_fresh_device_matches_cpu() { + effect_direct("ec.color.ociofile", &[("file", s(&cube_1d())), ("direction", e(1))], false); +} + +// Isolate the first profile conversion that failed in hosted llvmpipe coverage. +// This retains the same oracle and tolerance as the broader profile test. +#[test] +fn profile_conversion_on_a_fresh_device_matches_cpu() { + effect_direct("ec.utility.colorprofileconverter", &[("inputProfile", e(1)), ("outputProfile", e(3)), ("intent", e(0))], false); +} + +// Unequal CSP channel point counts exercise shaper offsets on a fresh device. +#[test] +fn csp_shaper_on_a_fresh_device_matches_cpu() { + effect_direct("ec.color.ociofile", &[("file", s(&csp())), ("interpolation", e(1))], false); + effect_direct("ec.color.ociofile", &[("file", s(&csp())), ("direction", e(1))], false); +} + +// A 1D CSP takes the direct inverse-shaper path (flip=true), unlike a 3D CSP. +#[test] +fn inverse_1d_csp_shaper_on_a_fresh_device_matches_cpu() { + let lut = "CSPLUTV100\n1D\n3\n0 0.5 1\n0 0.3 1\n2\n0 1\n0 1\n4\n0 0.2 0.6 1\n0 0.4 0.7 1\n2\n0 0 0\n1 1 1\n"; + effect_direct("ec.color.ociofile", &[("file", s(lut)), ("direction", e(1))], false); +} + #[test] fn ocio_file_transform() { for interp in 1..4 {