From 5617f0fd1783f2302370c8d73b9cee856e760d17 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:15:43 +0200 Subject: [PATCH] call `flush_mapped_ranges` when unmapping write-mapped buffers I'm not sure how things worked without this. --- wgpu-core/src/device/global.rs | 6 ++++-- wgpu-core/src/device/life.rs | 11 +++++++---- wgpu-core/src/device/mod.rs | 6 +++--- wgpu-core/src/device/resource.rs | 9 ++++++--- wgpu-core/src/resource.rs | 15 +++++++++++---- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 0aab087ad..1f70ee09e 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -2418,7 +2418,9 @@ impl Global { Ok((ptr, range_size)) } resource::BufferMapState::Active { - ref ptr, ref range, .. + ref mapping, + ref range, + .. } => { if offset < range.start { return Err(BufferAccessError::OutOfBoundsUnderrun { @@ -2437,7 +2439,7 @@ impl Global { let relative_offset = (offset - range.start) as isize; unsafe { Ok(( - NonNull::new_unchecked(ptr.as_ptr().offset(relative_offset)), + NonNull::new_unchecked(mapping.ptr.as_ptr().offset(relative_offset)), range_size, )) } diff --git a/wgpu-core/src/device/life.rs b/wgpu-core/src/device/life.rs index 1bb687d7e..7408c184d 100644 --- a/wgpu-core/src/device/life.rs +++ b/wgpu-core/src/device/life.rs @@ -391,10 +391,10 @@ impl LifetimeTracker { host, snatch_guard, ) { - Ok(ptr) => { + Ok(mapping) => { *buffer.map_state.lock() = resource::BufferMapState::Active { - ptr, - range: pending_mapping.range.start..pending_mapping.range.start + size, + mapping, + range: pending_mapping.range.clone(), host, }; Ok(()) @@ -406,7 +406,10 @@ impl LifetimeTracker { } } else { *buffer.map_state.lock() = resource::BufferMapState::Active { - ptr: std::ptr::NonNull::dangling(), + mapping: hal::BufferMapping { + ptr: std::ptr::NonNull::dangling(), + is_coherent: true, + }, range: pending_mapping.range, host: pending_mapping.op.host, }; diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index d33de22da..693d8f8c8 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -18,7 +18,7 @@ use std::os::raw::c_char; use thiserror::Error; use wgt::{BufferAddress, DeviceLostReason, TextureFormat}; -use std::{iter, num::NonZeroU32, ptr}; +use std::{iter, num::NonZeroU32}; pub mod any_device; pub(crate) mod bgl; @@ -307,7 +307,7 @@ fn map_buffer( size: BufferAddress, kind: HostMap, snatch_guard: &SnatchGuard, -) -> Result, BufferAccessError> { +) -> Result { let raw_buffer = buffer.try_raw(snatch_guard)?; let mapping = unsafe { raw.map_buffer(raw_buffer, offset..offset + size) @@ -360,7 +360,7 @@ fn map_buffer( } } - Ok(mapping.ptr) + Ok(mapping) } #[derive(Clone, Debug)] diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 621149404..96b4e9800 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -611,8 +611,11 @@ impl Device { } 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 = if map_size == 0 { - std::ptr::NonNull::dangling() + let mapping = if map_size == 0 { + hal::BufferMapping { + ptr: std::ptr::NonNull::dangling(), + is_coherent: true, + } } else { let snatch_guard: SnatchGuard = self.snatchable_lock.read(); map_buffer( @@ -625,7 +628,7 @@ impl Device { )? }; *buffer.map_state.lock() = resource::BufferMapState::Active { - ptr, + mapping, range: 0..map_size, host: HostMap::Write, }; diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 54f2b114e..0b9a12125 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -229,7 +229,7 @@ pub(crate) enum BufferMapState { Waiting(BufferPendingMapping), /// Mapped Active { - ptr: NonNull, + mapping: hal::BufferMapping, range: hal::MemoryRange, host: HostMap, }, @@ -669,13 +669,18 @@ impl Buffer { BufferMapState::Waiting(pending) => { return Ok(Some((pending.op, Err(BufferAccessError::MapAborted)))); } - BufferMapState::Active { ptr, range, host } => { + BufferMapState::Active { + mapping, + range, + host, + } => { + #[allow(clippy::collapsible_if)] if host == HostMap::Write { #[cfg(feature = "trace")] if let Some(ref mut trace) = *device.trace.lock() { let size = range.end - range.start; let data = trace.make_binary("bin", unsafe { - std::slice::from_raw_parts(ptr.as_ptr(), size as usize) + std::slice::from_raw_parts(mapping.ptr.as_ptr(), size as usize) }); trace.add(trace::Action::WriteBuffer { id: buffer_id, @@ -684,7 +689,9 @@ impl Buffer { queued: false, }); } - let _ = (ptr, range); + if !mapping.is_coherent { + unsafe { device.raw().flush_mapped_ranges(raw_buf, iter::once(range)) }; + } } unsafe { device.raw().unmap_buffer(raw_buf) }; }