From 3e11ff59bf3f9795d285ecc045014089640d7248 Mon Sep 17 00:00:00 2001 From: Sam <16504129+sagudev@users.noreply.github.com> Date: Wed, 2 Sep 2026 20:19:05 +0200 Subject: [PATCH] `Buffer::unmap` should not raise any validation errors (#10242) Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- CHANGELOG.md | 1 + deno_webgpu/buffer.rs | 2 +- player/src/lib.rs | 2 +- tests/tests/wgpu-gpu/resource_error.rs | 6 +--- wgpu-core-remote/src/global/device.rs | 8 ++---- wgpu-core/src/resource.rs | 39 ++++++++++++++++---------- wgpu/src/backend/wgpu_core.rs | 8 +----- 7 files changed, 32 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3078e94fc..695958777 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -131,6 +131,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). - Remove the never-constructed `CreateBlasError::InvalidAabbStride` variant. `create_blas` takes no stride, so it could never be produced; AABB stride is validated at build time as `BuildAccelerationStructureError::InvalidAabbStride`. By @mstampfli in [#9935](https://github.com/gfx-rs/wgpu/pull/9935). - Added `DownlevelFlags::LINEAR_INTERPOLATION`, indicating that the adapter supports `@interpolate(linear)`. It is absent on GLES/WebGL2, since GLSL ES has no `noperspective` qualifier. By @emilk in [#9972](https://github.com/gfx-rs/wgpu/pull/9972). +- `Buffer::unmap` will not raise any validation errors anymore per specification. By @sagudev in [#10242](https://github.com/gfx-rs/wgpu/pull/10242). #### naga diff --git a/deno_webgpu/buffer.rs b/deno_webgpu/buffer.rs index e6a99e295..d1e52cfd2 100644 --- a/deno_webgpu/buffer.rs +++ b/deno_webgpu/buffer.rs @@ -256,7 +256,7 @@ impl GPUBuffer { ab.detach(None); } - self.wgpu_buffer.unmap().map_err(BufferError::Access)?; + self.wgpu_buffer.unmap(); *self.map_state.borrow_mut() = "unmapped"; diff --git a/player/src/lib.rs b/player/src/lib.rs index 6acb73332..81e6a64e3 100644 --- a/player/src/lib.rs +++ b/player/src/lib.rs @@ -126,7 +126,7 @@ impl Player { } Action::DropBuffer(id) => { let buffer = self.buffers.remove(&id).expect("invalid buffer"); - let _ = buffer.unmap(); + buffer.unmap(); } Action::CreateTexture(id, desc) => { let texture = device.create_texture(&desc); diff --git a/tests/tests/wgpu-gpu/resource_error.rs b/tests/tests/wgpu-gpu/resource_error.rs index d752e745c..cc9932888 100644 --- a/tests/tests/wgpu-gpu/resource_error.rs +++ b/tests/tests/wgpu-gpu/resource_error.rs @@ -30,11 +30,7 @@ static BAD_BUFFER_MAP: GpuTestConfiguration = GpuTestConfiguration::new() || buffer.slice(..).map_async(wgpu::MapMode::Write, |_| {}), Some("Buffer with '' label is invalid"), ); - fail( - &ctx.device, - || buffer.unmap(), - Some("Buffer with '' label is invalid"), - ); + valid(&ctx.device, || buffer.unmap()); valid(&ctx.device, || buffer.destroy()); valid(&ctx.device, || buffer.destroy()); }); diff --git a/wgpu-core-remote/src/global/device.rs b/wgpu-core-remote/src/global/device.rs index 4348911c6..aec646d32 100644 --- a/wgpu-core-remote/src/global/device.rs +++ b/wgpu-core-remote/src/global/device.rs @@ -17,9 +17,7 @@ use wgpu_core::{ self, ProgrammableStageDescriptor, RenderPipelineVertexProcessor, ResolvedGeneralRenderPipelineDescriptor, }, - resource::{ - self, BufferAccessError, BufferAccessResult, BufferMapOperation, CreateBufferError, - }, + resource::{self, BufferAccessError, BufferMapOperation, CreateBufferError}, Label, LabelHelpers, SubmissionIndex, }; @@ -1191,12 +1189,12 @@ impl Global { buffer.get_mapped_range(offset, size) } - pub fn buffer_unmap(&self, buffer_id: id::BufferId) -> BufferAccessResult { + pub fn buffer_unmap(&self, buffer_id: id::BufferId) { let hub = self.hub.borrow(); let buffer = hub.buffers.get(buffer_id); - buffer.unmap() + buffer.unmap(); } pub fn device_on_uncaptured_error( diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index b9ec59990..42338305a 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -996,25 +996,35 @@ impl Buffer { } // Note: This must not be called while holding a lock. - pub fn unmap(self: &Arc) -> Result<(), BufferAccessError> { + pub fn unmap(self: &Arc) { profiling::scope!("unmap", "Buffer"); api_log!("Buffer::unmap {:?}", Arc::as_ptr(self)); - if let Some((mut operation, status)) = self.unmap_inner()? { + if let Some((mut operation, status)) = self.unmap_inner() { if let Some(callback) = operation.callback.take() { callback(status); } } - - Ok(()) } - fn unmap_inner(self: &Arc) -> Result, BufferAccessError> { + /// Per the WebGPU spec, unmap does not raise any errors + /// it just resolves any pending map_async calls with a MapAborted error. + /// It is okay to ignore errors because: + /// - if the buffer or device was invalid from the start it couldn't have been mapped via `map_async` anyway (no callback to resolve) + /// - if the device becomes invalid it calls the callback in `poll`/`maintain` + /// - if the buffer was destroyed (via `Buffer::destroy`) it was first unmapped + /// + /// + fn unmap_inner(self: &Arc) -> Option { let device = &self.device; - self.check_is_valid()?; - self.device.check_is_valid()?; + // We can stop here if the device is invalid because: + // - if the device was invalid from the start it couldn't have been mapped via `map_async` anyway + // - if the device becomes invalid it calls the callback in `poll`/`maintain` + self.device.check_is_valid().ok()?; let snatch_guard = device.snatchable_lock.read(); - self.check_destroyed(&snatch_guard)?; - let raw_buf = self.try_raw(&snatch_guard)?; + // We can stop here if the buffer is invalid or destroyed because: + // - if the device was invalid from the start it couldn't have been mapped via `map_async` anyway + // - if the buffer was destroyed (via `Buffer::destroy`) it was first unmapped + let raw_buf = self.try_raw(&snatch_guard).ok()?; let map_state = mem::replace(&mut *self.map_state.lock(), BufferMapState::Idle); match map_state { BufferMapState::Init { staging_buffer } => { @@ -1072,12 +1082,11 @@ impl Buffer { pending_writes.consume(staging_buffer); pending_writes.insert_buffer(self); } + None } - BufferMapState::Idle => { - return Err(BufferAccessError::NotMapped); - } + BufferMapState::Idle => None, BufferMapState::Waiting(pending) => { - return Ok(Some((pending.op, Err(BufferAccessError::MapAborted)))); + Some((pending.op, Err(BufferAccessError::MapAborted))) } BufferMapState::Active { mapping, @@ -1106,9 +1115,9 @@ impl Buffer { } } unsafe { device.raw().unmap_buffer(raw_buf) }; + None } } - Ok(None) } pub fn destroy(self: &Arc) { @@ -1127,7 +1136,7 @@ impl Buffer { return; }; - let _ = self.unmap(); + self.unmap(); let temp = { let mut snatch_guard = device.snatchable_lock.write(); diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index 72c33a145..aabcead70 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -1709,13 +1709,7 @@ impl dispatch::BufferInterface for CoreBuffer { } fn unmap(&self) { - match self.wgpu_buffer.unmap() { - Ok(()) => (), - Err(cause) => self - .wgpu_buffer - .device() - .handle_error_nolabel(cause, "Buffer::buffer_unmap"), - } + self.wgpu_buffer.unmap(); } fn destroy(&self) {