From e2e4651ecbefe2968c328ff0eb3c7dd0d947b4df Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Thu, 23 Oct 2025 17:05:58 -0400 Subject: [PATCH] fix(core): validate that buffer is unmapped in `write_buffer` calls --- CHANGELOG.md | 1 + cts_runner/test.lst | 3 ++- wgpu-core/src/command/transfer.rs | 5 ++++- wgpu-core/src/device/queue.rs | 3 +++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe12edeb3..86a3e2658 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -139,6 +139,7 @@ By @SupaMaggie70Incorporated in [#8206](https://github.com/gfx-rs/wgpu/pull/8206 - `wgpu_types::PollError` now always implements the `Error` trait. By @kpreid in [#8384](https://github.com/gfx-rs/wgpu/pull/8384). - The texture subresources used by the color attachments of a render pass are no longer allowed to overlap when accessed via different texture views. By @andyleiserson in [#8402](https://github.com/gfx-rs/wgpu/pull/8402). - Fixed a bug where the texture aspect was not passed through when calling `copy_texture_to_buffer` in WebGPU, causing the copy to fail for depth/stencil textures. By @Tim-Evans-Seequent in [#8445](https://github.com/gfx-rs/wgpu/pull/8445). +- Validate that buffers are unmapped in `write_buffer` calls. By @ErichDonGubler in [#8454](https://github.com/gfx-rs/wgpu/pull/8454). #### DX12 diff --git a/cts_runner/test.lst b/cts_runner/test.lst index 9c6386f9e..bc9cd5169 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -87,7 +87,8 @@ webgpu:api,validation,encoding,queries,general:occlusion_query,query_index:* webgpu:shader,validation,extension,dual_source_blending:blend_src_syntax_validation:* webgpu:api,validation,image_copy,buffer_texture_copies:depth_stencil_format,copy_usage_and_aspect:* webgpu:api,validation,image_copy,buffer_texture_copies:depth_stencil_format,copy_buffer_size:* -fails-if(dx12,vulkan,metal) webgpu:api,validation,queue,buffer_mapped:writeBuffer:* +// `vulkan` failure: https://github.com/gfx-rs/wgpu/issues/???? +fails-if(vulkan) webgpu:api,validation,queue,buffer_mapped:writeBuffer:* webgpu:api,validation,queue,buffer_mapped:copyBufferToBuffer:* webgpu:api,validation,queue,buffer_mapped:copyBufferToTexture:* webgpu:api,validation,queue,buffer_mapped:copyTextureToBuffer:* diff --git a/wgpu-core/src/command/transfer.rs b/wgpu-core/src/command/transfer.rs index adeee47a5..727fa611e 100644 --- a/wgpu-core/src/command/transfer.rs +++ b/wgpu-core/src/command/transfer.rs @@ -170,6 +170,8 @@ pub enum TransferError { }, #[error("Requested mip level {requested} does not exist (count: {count})")] InvalidMipLevel { requested: u32, count: u32 }, + #[error("Buffer is expected to be unmapped, but was not")] + BufferNotAvailable, } impl WebGpuError for TransferError { @@ -211,7 +213,8 @@ impl WebGpuError for TransferError { | Self::InvalidSampleCount { .. } | Self::SampleCountNotEqual { .. } | Self::InvalidMipLevel { .. } - | Self::SameSourceDestinationBuffer => return ErrorType::Validation, + | Self::SameSourceDestinationBuffer + | Self::BufferNotAvailable => return ErrorType::Validation, }; e.webgpu_error_type() } diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index db0bc9124..744e229c2 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -648,6 +648,9 @@ impl Queue { buffer_offset: u64, buffer_size: wgt::BufferSize, ) -> Result<(), TransferError> { + if !matches!(&*buffer.map_state.lock(), BufferMapState::Idle) { + return Err(TransferError::BufferNotAvailable); + } buffer.check_usage(wgt::BufferUsages::COPY_DST)?; if buffer_size.get() % wgt::COPY_BUFFER_ALIGNMENT != 0 { return Err(TransferError::UnalignedCopySize(buffer_size.get()));