From 9a8229af7bf548d715b43c3d290259097b5f6f66 Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Wed, 13 Jul 2022 20:01:12 +0200 Subject: [PATCH] Record that the buffer is mapped when its size is zero. (#2877) * Record that the buffer is mapped when its size is zero. * Avoid internally trying to map a zero-sized buffer if mapped_at_creation is true. * Add an entry in the changelog. --- CHANGELOG.md | 1 + wgpu-core/src/device/life.rs | 5 +++++ wgpu-core/src/device/mod.rs | 21 +++++++++++++-------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc8603217..ff153844c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ Bottom level categories: - Fix bind group / pipeline deduplication not taking into account RenderBundle execution resetting these values by @shoebe [#2867](https://github.com/gfx-rs/wgpu/pull/2867) - Fix panics that occur when using `as_hal` functions when the hal generic type does not match the hub being looked up in by @i509VCB [#2871](https://github.com/gfx-rs/wgpu/pull/2871) - Add some validation in map_async by @nical in [#2876](https://github.com/gfx-rs/wgpu/pull/2876) +- Fix bugs when mapping/unmapping zero-sized buffers and ranges by @nical in [#2877](https://github.com/gfx-rs/wgpu/pull/2877) #### DX12 - `DownlevelCapabilities::default()` now returns the `ANISOTROPIC_FILTERING` flag set to true so DX12 lists `ANISOTROPIC_FILTERING` as true again by @cwfitzgerald in [#2851](https://github.com/gfx-rs/wgpu/pull/2851) diff --git a/wgpu-core/src/device/life.rs b/wgpu-core/src/device/life.rs index a1c2de61c..2cb570328 100644 --- a/wgpu-core/src/device/life.rs +++ b/wgpu-core/src/device/life.rs @@ -896,6 +896,11 @@ impl LifetimeTracker { } } } else { + buffer.map_state = resource::BufferMapState::Active { + ptr: std::ptr::NonNull::dangling(), + range: mapping.range, + host: mapping.op.host, + }; resource::BufferMapAsyncStatus::Success }; pending_callbacks.push((mapping.op, status)); diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index d132b7f41..cdf5b1978 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -3247,14 +3247,19 @@ impl Global { } else if desc.usage.contains(wgt::BufferUsages::MAP_WRITE) { // buffer is mappable, so we are just doing that at start let map_size = buffer.size; - let ptr = match map_buffer(&device.raw, &mut buffer, 0, map_size, HostMap::Write) { - Ok(ptr) => ptr, - Err(e) => { - let raw = buffer.raw.unwrap(); - device - .lock_life(&mut token) - .schedule_resource_destruction(queue::TempResource::Buffer(raw), !0); - break e.into(); + let ptr = if map_size == 0 { + std::ptr::NonNull::dangling() + } else { + match map_buffer(&device.raw, &mut buffer, 0, map_size, HostMap::Write) { + Ok(ptr) => ptr, + Err(e) => { + let raw = buffer.raw.unwrap(); + device.lock_life(&mut token).schedule_resource_destruction( + queue::TempResource::Buffer(raw), + !0, + ); + break e.into(); + } } }; buffer.map_state = resource::BufferMapState::Active {