diff --git a/CHANGELOG.md b/CHANGELOG.md index 088a818ed..0ae654af5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -160,6 +160,7 @@ By @andyleiserson in [#9321](https://github.com/gfx-rs/wgpu/pull/9321). - Fix `SYNC-HAZARD-WRITE-AFTER-PRESENT` on Vulkan when a surface texture is presented without being rendered to. By @inner-daemons and @atlv24 in [#9361](https://github.com/gfx-rs/wgpu/pull/9361). - Fix incorrect checks for dynamic binding bounds when calling an encoder's `set_bind_group` in passes and bundles. By @ErichDonGubler in [#9308](https://github.com/gfx-rs/wgpu/pull/9308). +- Writes from `Queue::write_buffer` are now flushed by calls to `Buffer::map_async` for that same buffer, to prevent reading stale data. `on_submitted_work_done` also now flushes pending writes. By @andyleiserson in [#9307](https://github.com/gfx-rs/wgpu/pull/9307). #### naga diff --git a/cts_runner/test.lst b/cts_runner/test.lst index 8c3f0e5ef..9104b2530 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -12,6 +12,8 @@ unittests:* webgpu:api,operation,adapter,requestAdapter:* webgpu:api,operation,buffers,createBindGroup:buffer_binding_resource:* +webgpu:api,operation,buffers,map:mapAsync,read,* +webgpu:api,operation,buffers,map:mapAsync,write,* webgpu:api,operation,command_buffer,basic:* webgpu:api,operation,command_buffer,copyBufferToBuffer:* fails-if(vulkan) webgpu:api,operation,command_buffer,copyTextureToTexture:copy_depth_stencil:format="depth16unorm" diff --git a/tests/tests/wgpu-gpu/buffer.rs b/tests/tests/wgpu-gpu/buffer.rs index 598c43260..c1c592d02 100644 --- a/tests/tests/wgpu-gpu/buffer.rs +++ b/tests/tests/wgpu-gpu/buffer.rs @@ -6,6 +6,7 @@ pub fn all_tests(vec: &mut Vec) { vec.extend([ EMPTY_BUFFER, MAP_OFFSET, + MAP_WITHOUT_SUBMIT, MINIMUM_BUFFER_BINDING_SIZE_LAYOUT, MINIMUM_BUFFER_BINDING_SIZE_DISPATCH, CLEAR_OFFSET_OUTSIDE_RESOURCE_BOUNDS, @@ -184,6 +185,48 @@ static MAP_OFFSET: GpuTestConfiguration = GpuTestConfiguration::new() } }); +/// Mapping a buffer should see data previously written to the buffer, even if there was no +/// intervening submit. +/// +/// Regression test for [#5173](https://github.com/gfx-rs/wgpu/issues/5173). +#[gpu_test] +static MAP_WITHOUT_SUBMIT: GpuTestConfiguration = + GpuTestConfiguration::new().run_async(|ctx| async move { + let buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: 12, + usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: true, + }); + + { + let data = (0..12).map(|i| (i % 255) as u8).collect::>(); + let mut mapped = buffer.slice(0..12).get_mapped_range_mut().unwrap(); + assert!(mapped.len() == 12); + mapped.copy_from_slice(&data); + } + + buffer.unmap(); + + buffer + .slice(0..12) + .map_async(wgpu::MapMode::Read, Result::unwrap); + + ctx.async_poll(wgpu::PollType::wait_indefinitely()) + .await + .unwrap(); + + { + let mapped = buffer.slice(0..12).get_mapped_range().unwrap(); + assert!(mapped.len() == 12); + for (i, elt) in mapped.iter().enumerate() { + assert_eq!(*elt, (i % 255) as u8); + } + } + + buffer.unmap(); + }); + /// The WebGPU algorithm [validating shader binding][vsb] requires /// implementations to check that buffer bindings are large enough to /// hold the WGSL `storage` or `uniform` variables they're bound to. diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index f71be9a4b..602a1b107 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -348,9 +348,13 @@ pub(crate) struct EncoderInFlight { /// /// Instead, `Device::pending_writes` owns one of these values, which /// has its own hal command encoder and resource lists. The commands -/// accumulated here are automatically submitted to the queue the next -/// time the user submits a wgpu command buffer, ahead of the user's -/// commands. +/// accumulated here are automatically submitted to the queue at the +/// sooner of: +/// +/// 1. The user's next wgpu command buffer submission. (Pending writes +/// are inserted ahead of the user's commands.) +/// 2. The next `mapAsync` request for a buffer that has pending +/// writes. /// /// Important: /// When locking pending_writes be sure that tracker is not locked @@ -1243,6 +1247,24 @@ impl Queue { Ok(()) } + /// Flush `PendingWrites` if it contains a write to `buffer`. + pub fn flush_writes_for_buffer( + &self, + buffer: &Arc, + snatch_guard: SnatchGuard, + ) -> Result<(), BufferAccessError> { + let (submission, _index) = self.allocate_submission(snatch_guard); + + let pending_writes = self.pending_writes.lock(); + if !pending_writes.contains_buffer(buffer) { + return Ok(()); + } + + submission.submit(pending_writes)?; + + Ok(()) + } + fn flush_pending_writes(&self) -> Result, DeviceError> { let snatch_guard = self.device.snatchable_lock.read(); let (submission, submit_index) = self.allocate_submission(snatch_guard); diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 682c59b54..aa672dc26 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -629,9 +629,8 @@ impl Buffer { /// with the error, and the caller is responsible for calling the /// callback. /// - /// A return value of `Ok(0)` roughly means that no wait is necessary, - /// but it does not necessarily mean that the buffer has already been - /// mapped. + /// A return value of `Ok(0)` means that mapping does not need to wait on the queue, but + /// it does not mean that the buffer has already been mapped. fn try_map_async( self: &Arc, offset: wgt::BufferAddress, @@ -693,50 +692,79 @@ impl Buffer { return Err((op, e.into())); } - { + let submit_index = { let snatch_guard = device.snatchable_lock.read(); if let Err(e) = self.check_destroyed(&snatch_guard) { return Err((op, e.into())); } - } - { - let map_state = &mut *self.map_state.lock(); - *map_state = match *map_state { - BufferMapState::Init { .. } | BufferMapState::Active { .. } => { - return Err((op, BufferAccessError::AlreadyMapped)); - } - BufferMapState::Waiting(_) => { - return Err((op, BufferAccessError::MapAlreadyPending)); - } - BufferMapState::Idle => BufferMapState::Waiting(BufferPendingMapping { - range: offset..end_offset, - op, - _parent_buffer: self.clone(), - }), - }; - } + { + let map_state = &mut *self.map_state.lock(); + *map_state = match *map_state { + BufferMapState::Init { .. } | BufferMapState::Active { .. } => { + return Err((op, BufferAccessError::AlreadyMapped)); + } + BufferMapState::Waiting(_) => { + return Err((op, BufferAccessError::MapAlreadyPending)); + } + BufferMapState::Idle => BufferMapState::Waiting(BufferPendingMapping { + range: offset..end_offset, + op, + _parent_buffer: self.clone(), + }), + }; + } - // TODO: we are ignoring the transition here, I think we need to add a barrier - // at the end of the submission + if let Some(queue) = device.get_queue().as_ref() { + match queue.flush_writes_for_buffer(self, snatch_guard) { + Err(err) => { + let state = mem::replace(&mut *self.map_state.lock(), BufferMapState::Idle); + let BufferMapState::Waiting(BufferPendingMapping { op, .. }) = state else { + unreachable!(); + }; + return Err((op, err)); + } + Ok(()) => { + // Schedule the buffer map in the lifetime tracker. + // + // This call searches for use of the buffer by pending submissions. + // If we just flushed pending writes, that search is redundant; we + // already know that mapping needs to wait for the latest submission + // and could implement a special case to directly attach it to that + // submission. However, the queue is searched in reverse, so finding + // that the buffer is used by the latest submission will be fast. + Some(queue.lock_life().map(self).unwrap_or(0)) + } + } + } else { + None + } + }; + + // At this point, `submit_index` is: + // - `Some(index)`, if there is a submission the mapping operation must wait for. + // - `Some(0)`, if we have a queue and there is no submission to wait for. + // - `None`, if we don't have a queue. + // + // TODO(https://github.com/gfx-rs/wgpu/issues/9306): we are ignoring the transition + // here, I think we need to add a barrier at the end of the submission device .trackers .lock() .buffers .set_single(self, internal_use); - let submit_index = if let Some(queue) = device.get_queue() { - queue.lock_life().map(self).unwrap_or(0) // '0' means no wait is necessary + if let Some(index) = submit_index { + Ok(index) } else { + // We don't have a queue, so go ahead and map the buffer. // We can safely unwrap below since we just set the `map_state` to `BufferMapState::Waiting`. let (mut operation, status) = self.map(&device.snatchable_lock.read()).unwrap(); if let Some(callback) = operation.callback.take() { callback(status); } - 0 - }; - - Ok(submit_index) + Ok(0) + } } pub fn get_mapped_range( @@ -820,6 +848,7 @@ impl Buffer { } } /// This function returns [`None`] only if [`Self::map_state`] is not [`BufferMapState::Waiting`]. + /// Other errors are returned within `BufferMapPendingClosure`. #[must_use] pub(crate) fn map(&self, snatch_guard: &SnatchGuard) -> Option { // This _cannot_ be inlined into the match. If it is, the lock will be held