From 5b8a1bc6f6490f4d97cfd9c98b9331be68ec668c Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Fri, 12 Aug 2022 16:35:48 -0700 Subject: [PATCH] Have `prepare_staging_buffer` take a raw hal Device. This helps the borrow checker understand that we can borrow `pending_writes`'s encoder and the raw Device at the same time. --- wgpu-core/src/device/queue.rs | 52 +++++++++++++++++------------------ 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 4bfcd3fa28a..cc6749fdaa1 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -217,30 +217,28 @@ impl PendingWrites { } } -impl super::Device { - fn prepare_staging_buffer( - &mut self, - size: wgt::BufferAddress, - ) -> Result<(StagingBuffer, *mut u8), DeviceError> { - profiling::scope!("prepare_staging_buffer"); - let stage_desc = hal::BufferDescriptor { - label: Some("(wgpu internal) Staging"), - size, - usage: hal::BufferUses::MAP_WRITE | hal::BufferUses::COPY_SRC, - memory_flags: hal::MemoryFlags::TRANSIENT, - }; - - let buffer = unsafe { self.raw.create_buffer(&stage_desc)? }; - let mapping = unsafe { self.raw.map_buffer(&buffer, 0..size) }?; - - let staging_buffer = StagingBuffer { - raw: buffer, - size, - is_coherent: mapping.is_coherent, - }; - - Ok((staging_buffer, mapping.ptr.as_ptr())) - } +fn prepare_staging_buffer( + device: &mut A::Device, + size: wgt::BufferAddress, +) -> Result<(StagingBuffer, *mut u8), DeviceError> { + profiling::scope!("prepare_staging_buffer"); + let stage_desc = hal::BufferDescriptor { + label: Some("(wgpu internal) Staging"), + size, + usage: hal::BufferUses::MAP_WRITE | hal::BufferUses::COPY_SRC, + memory_flags: hal::MemoryFlags::TRANSIENT, + }; + + let buffer = unsafe { device.create_buffer(&stage_desc)? }; + let mapping = unsafe { device.map_buffer(&buffer, 0..size) }?; + + let staging_buffer = StagingBuffer { + raw: buffer, + size, + is_coherent: mapping.is_coherent, + }; + + Ok((staging_buffer, mapping.ptr.as_ptr())) } impl StagingBuffer { @@ -324,7 +322,7 @@ impl Global { return Ok(()); } - let (staging_buffer, staging_buffer_ptr) = device.prepare_staging_buffer(data_size)?; + let (staging_buffer, staging_buffer_ptr) = prepare_staging_buffer(&mut device.raw, data_size)?; unsafe { profiling::scope!("copy"); @@ -356,7 +354,7 @@ impl Global { .map_err(|_| DeviceError::Invalid)?; let (staging_buffer, staging_buffer_ptr) = - device.prepare_staging_buffer(buffer_size.get())?; + prepare_staging_buffer(&mut device.raw, buffer_size.get())?; let fid = hub.staging_buffers.prepare(id_in); let id = fid.assign(staging_buffer, device_token); @@ -587,7 +585,7 @@ impl Global { let block_rows_in_copy = (size.depth_or_array_layers - 1) * block_rows_per_image + height_blocks; let stage_size = stage_bytes_per_row as u64 * block_rows_in_copy as u64; - let (staging_buffer, staging_buffer_ptr) = device.prepare_staging_buffer(stage_size)?; + let (staging_buffer, staging_buffer_ptr) = prepare_staging_buffer(&mut device.raw, stage_size)?; let dst = texture_guard.get_mut(destination.texture).unwrap(); if !dst.desc.usage.contains(wgt::TextureUsages::COPY_DST) {