From ce908c5f57c2103ac4d5eebb38e8a9ea549f0da3 Mon Sep 17 00:00:00 2001 From: TheArtist Date: Sat, 9 Jan 2021 19:53:13 +0200 Subject: [PATCH] Changed Arc to Arc> in GpuAllocator --- Cargo.lock | 23 +++++++------- wgpu-core/Cargo.toml | 20 ++++++------ wgpu-core/src/device/alloc.rs | 59 ++++++++++++++++++++--------------- wgpu-core/src/device/mod.rs | 14 ++++----- wgpu-core/src/device/queue.rs | 4 +-- wgpu-core/src/swap_chain.rs | 2 +- 6 files changed, 65 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7baac16a20..3535581b68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -441,7 +441,7 @@ dependencies = [ [[package]] name = "gfx-auxil" version = "0.5.0" -source = "git+https://github.com/gfx-rs/gfx?rev=40b6220a937a4c8663ce06aa8a7219cf131e60e2#40b6220a937a4c8663ce06aa8a7219cf131e60e2" +source = "git+https://github.com/gfx-rs/gfx?rev=6b3a1e36939473f0062232baf11e1deacd6605f4#6b3a1e36939473f0062232baf11e1deacd6605f4" dependencies = [ "fxhash", "gfx-hal", @@ -451,7 +451,7 @@ dependencies = [ [[package]] name = "gfx-backend-dx11" version = "0.6.0" -source = "git+https://github.com/gfx-rs/gfx?rev=40b6220a937a4c8663ce06aa8a7219cf131e60e2#40b6220a937a4c8663ce06aa8a7219cf131e60e2" +source = "git+https://github.com/gfx-rs/gfx?rev=6b3a1e36939473f0062232baf11e1deacd6605f4#6b3a1e36939473f0062232baf11e1deacd6605f4" dependencies = [ "arrayvec", "bitflags", @@ -472,7 +472,7 @@ dependencies = [ [[package]] name = "gfx-backend-dx12" version = "0.6.2" -source = "git+https://github.com/gfx-rs/gfx?rev=40b6220a937a4c8663ce06aa8a7219cf131e60e2#40b6220a937a4c8663ce06aa8a7219cf131e60e2" +source = "git+https://github.com/gfx-rs/gfx?rev=6b3a1e36939473f0062232baf11e1deacd6605f4#6b3a1e36939473f0062232baf11e1deacd6605f4" dependencies = [ "arrayvec", "bit-set", @@ -492,7 +492,7 @@ dependencies = [ [[package]] name = "gfx-backend-empty" version = "0.6.0" -source = "git+https://github.com/gfx-rs/gfx?rev=40b6220a937a4c8663ce06aa8a7219cf131e60e2#40b6220a937a4c8663ce06aa8a7219cf131e60e2" +source = "git+https://github.com/gfx-rs/gfx?rev=6b3a1e36939473f0062232baf11e1deacd6605f4#6b3a1e36939473f0062232baf11e1deacd6605f4" dependencies = [ "gfx-hal", "log", @@ -502,7 +502,7 @@ dependencies = [ [[package]] name = "gfx-backend-gl" version = "0.6.0" -source = "git+https://github.com/gfx-rs/gfx?rev=40b6220a937a4c8663ce06aa8a7219cf131e60e2#40b6220a937a4c8663ce06aa8a7219cf131e60e2" +source = "git+https://github.com/gfx-rs/gfx?rev=6b3a1e36939473f0062232baf11e1deacd6605f4#6b3a1e36939473f0062232baf11e1deacd6605f4" dependencies = [ "arrayvec", "bitflags", @@ -525,7 +525,7 @@ dependencies = [ [[package]] name = "gfx-backend-metal" version = "0.6.0" -source = "git+https://github.com/gfx-rs/gfx?rev=40b6220a937a4c8663ce06aa8a7219cf131e60e2#40b6220a937a4c8663ce06aa8a7219cf131e60e2" +source = "git+https://github.com/gfx-rs/gfx?rev=6b3a1e36939473f0062232baf11e1deacd6605f4#6b3a1e36939473f0062232baf11e1deacd6605f4" dependencies = [ "arrayvec", "bitflags", @@ -550,7 +550,7 @@ dependencies = [ [[package]] name = "gfx-backend-vulkan" version = "0.6.5" -source = "git+https://github.com/gfx-rs/gfx?rev=40b6220a937a4c8663ce06aa8a7219cf131e60e2#40b6220a937a4c8663ce06aa8a7219cf131e60e2" +source = "git+https://github.com/gfx-rs/gfx?rev=6b3a1e36939473f0062232baf11e1deacd6605f4#6b3a1e36939473f0062232baf11e1deacd6605f4" dependencies = [ "arrayvec", "ash", @@ -570,7 +570,7 @@ dependencies = [ [[package]] name = "gfx-hal" version = "0.6.0" -source = "git+https://github.com/gfx-rs/gfx?rev=40b6220a937a4c8663ce06aa8a7219cf131e60e2#40b6220a937a4c8663ce06aa8a7219cf131e60e2" +source = "git+https://github.com/gfx-rs/gfx?rev=6b3a1e36939473f0062232baf11e1deacd6605f4#6b3a1e36939473f0062232baf11e1deacd6605f4" dependencies = [ "bitflags", "naga", @@ -862,9 +862,8 @@ dependencies = [ [[package]] name = "metal" -version = "0.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c4e8a431536529327e28c9ba6992f2cb0c15d4222f0602a16e6d7695ff3bccf" +version = "0.20.1" +source = "git+https://github.com/gfx-rs/metal-rs?rev=ba08f5f98c70ab941020b8997936c9c75363b9aa#ba08f5f98c70ab941020b8997936c9c75363b9aa" dependencies = [ "bitflags", "block", @@ -1210,7 +1209,7 @@ dependencies = [ [[package]] name = "range-alloc" version = "0.1.1" -source = "git+https://github.com/gfx-rs/gfx?rev=40b6220a937a4c8663ce06aa8a7219cf131e60e2#40b6220a937a4c8663ce06aa8a7219cf131e60e2" +source = "git+https://github.com/gfx-rs/gfx?rev=6b3a1e36939473f0062232baf11e1deacd6605f4#6b3a1e36939473f0062232baf11e1deacd6605f4" [[package]] name = "raw-window-handle" diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index a761613c54..ce0a960c38 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -36,24 +36,24 @@ thiserror = "1" gpu-alloc = { git = "https://github.com/zakarumych/gpu-alloc", rev = "29e761f24edc50e28d238e723503b146d55d222e", features = ["tracing"] } gpu-descriptor = { git = "https://github.com/zakarumych/gpu-descriptor", rev = "df74fd8c7bea03149058a41aab0e4fe04077b266", features = ["tracing"] } -hal = { package = "gfx-hal", git = "https://github.com/gfx-rs/gfx", rev = "40b6220a937a4c8663ce06aa8a7219cf131e60e2" } -gfx-backend-empty = { git = "https://github.com/gfx-rs/gfx", rev = "40b6220a937a4c8663ce06aa8a7219cf131e60e2" } +hal = { package = "gfx-hal", git = "https://github.com/gfx-rs/gfx", rev = "6b3a1e36939473f0062232baf11e1deacd6605f4" } +gfx-backend-empty = { git = "https://github.com/gfx-rs/gfx", rev = "6b3a1e36939473f0062232baf11e1deacd6605f4" } [target.'cfg(all(not(target_arch = "wasm32"), all(unix, not(target_os = "ios"), not(target_os = "macos"))))'.dependencies] -gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "40b6220a937a4c8663ce06aa8a7219cf131e60e2", features = ["naga"] } -gfx-backend-gl = { git = "https://github.com/gfx-rs/gfx", rev = "40b6220a937a4c8663ce06aa8a7219cf131e60e2", features = ["naga"] } +gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "6b3a1e36939473f0062232baf11e1deacd6605f4", features = ["naga"] } +gfx-backend-gl = { git = "https://github.com/gfx-rs/gfx", rev = "6b3a1e36939473f0062232baf11e1deacd6605f4", features = ["naga"] } [target.'cfg(all(not(target_arch = "wasm32"), any(target_os = "ios", target_os = "macos")))'.dependencies] -gfx-backend-metal = { git = "https://github.com/gfx-rs/gfx", rev = "40b6220a937a4c8663ce06aa8a7219cf131e60e2", features = ["naga"] } -gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "40b6220a937a4c8663ce06aa8a7219cf131e60e2", optional = true } +gfx-backend-metal = { git = "https://github.com/gfx-rs/gfx", rev = "6b3a1e36939473f0062232baf11e1deacd6605f4", features = ["naga"] } +gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "6b3a1e36939473f0062232baf11e1deacd6605f4", optional = true } [target.'cfg(all(not(target_arch = "wasm32"), windows))'.dependencies] -gfx-backend-dx12 = { git = "https://github.com/gfx-rs/gfx", rev = "40b6220a937a4c8663ce06aa8a7219cf131e60e2" } -gfx-backend-dx11 = { git = "https://github.com/gfx-rs/gfx", rev = "40b6220a937a4c8663ce06aa8a7219cf131e60e2" } -gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "40b6220a937a4c8663ce06aa8a7219cf131e60e2", features = ["naga"] } +gfx-backend-dx12 = { git = "https://github.com/gfx-rs/gfx", rev = "6b3a1e36939473f0062232baf11e1deacd6605f4" } +gfx-backend-dx11 = { git = "https://github.com/gfx-rs/gfx", rev = "6b3a1e36939473f0062232baf11e1deacd6605f4" } +gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "6b3a1e36939473f0062232baf11e1deacd6605f4", features = ["naga"] } [target.'cfg(target_arch = "wasm32")'.dependencies] -gfx-backend-gl = { git = "https://github.com/gfx-rs/gfx", rev = "40b6220a937a4c8663ce06aa8a7219cf131e60e2", features = ["naga"] } +gfx-backend-gl = { git = "https://github.com/gfx-rs/gfx", rev = "6b3a1e36939473f0062232baf11e1deacd6605f4", features = ["naga"] } [dependencies.naga] git = "https://github.com/gfx-rs/naga" diff --git a/wgpu-core/src/device/alloc.rs b/wgpu-core/src/device/alloc.rs index 893830ace6..de5434ab4f 100644 --- a/wgpu-core/src/device/alloc.rs +++ b/wgpu-core/src/device/alloc.rs @@ -4,11 +4,12 @@ use super::DeviceError; use hal::device::Device; +use parking_lot::Mutex; use std::{borrow::Cow, fmt, iter, ptr::NonNull, sync::Arc}; -pub struct MemoryAllocator(gpu_alloc::GpuAllocator>); +pub struct MemoryAllocator(gpu_alloc::GpuAllocator>>); #[derive(Debug)] -pub struct MemoryBlock(gpu_alloc::MemoryBlock>); +pub struct MemoryBlock(gpu_alloc::MemoryBlock>>); struct MemoryDevice<'a, B: hal::Backend>(&'a B::Device); //TODO: https://github.com/zakarumych/gpu-alloc/issues/9 @@ -100,16 +101,18 @@ impl MemoryBlock { buffer: &mut B::Buffer, ) -> Result<(), DeviceError> { unsafe { + let mem = self.0.memory().lock(); device - .bind_buffer_memory(self.0.memory(), self.0.offset(), buffer) + .bind_buffer_memory(&*mem, self.0.offset(), buffer) .map_err(DeviceError::from_bind) } } pub fn bind_image(&self, device: &B::Device, image: &mut B::Image) -> Result<(), DeviceError> { unsafe { + let mem = self.0.memory().lock(); device - .bind_image_memory(self.0.memory(), self.0.offset(), image) + .bind_image_memory(&*mem, self.0.offset(), image) .map_err(DeviceError::from_bind) } } @@ -185,8 +188,9 @@ impl MemoryBlock { ) -> Result<(), DeviceError> { let segment = self.segment(inner_offset, size); unsafe { + let mem = self.0.memory().lock(); device - .flush_mapped_memory_ranges(iter::once((&**self.0.memory(), segment))) + .flush_mapped_memory_ranges(iter::once((&*mem, segment))) .or(Err(DeviceError::OutOfMemory)) } } @@ -199,44 +203,46 @@ impl MemoryBlock { ) -> Result<(), DeviceError> { let segment = self.segment(inner_offset, size); unsafe { + let mem = self.0.memory().lock(); device - .invalidate_mapped_memory_ranges(iter::once((&**self.0.memory(), segment))) + .invalidate_mapped_memory_ranges(iter::once((&*mem, segment))) .or(Err(DeviceError::OutOfMemory)) } } } -impl gpu_alloc::MemoryDevice> for MemoryDevice<'_, B> { +impl gpu_alloc::MemoryDevice>> for MemoryDevice<'_, B> { #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))] unsafe fn allocate_memory( &self, size: u64, memory_type: u32, flags: gpu_alloc::AllocationFlags, - ) -> Result, gpu_alloc::OutOfMemory> { + ) -> Result>, gpu_alloc::OutOfMemory> { assert!(flags.is_empty()); self.0 .allocate_memory(hal::MemoryTypeId(memory_type as _), size) - .map(Arc::new) + .map(|m| Arc::new(Mutex::new(m))) .map_err(|_| gpu_alloc::OutOfMemory::OutOfDeviceMemory) } #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))] - unsafe fn deallocate_memory(&self, memory: Arc) { + unsafe fn deallocate_memory(&self, memory: Arc>) { let memory = Arc::try_unwrap(memory).expect("Memory must not be used anywhere"); - self.0.free_memory(memory); + self.0.free_memory(memory.into_inner()); } #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))] unsafe fn map_memory( &self, - memory: &Arc, + memory: &Arc>, offset: u64, size: u64, ) -> Result, gpu_alloc::DeviceMapError> { + let mut mem = memory.lock(); match self.0.map_memory( - memory, + &mut *mem, hal::memory::Segment { offset, size: Some(size), @@ -252,22 +258,24 @@ impl gpu_alloc::MemoryDevice> for MemoryDevice<' } #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))] - unsafe fn unmap_memory(&self, memory: &Arc) { - self.0.unmap_memory(memory); + unsafe fn unmap_memory(&self, memory: &Arc>) { + let mut mem = memory.lock(); + self.0.unmap_memory(&mut *mem); } #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))] unsafe fn invalidate_memory_ranges( &self, - ranges: &[gpu_alloc::MappedMemoryRange<'_, Arc>], + ranges: &[gpu_alloc::MappedMemoryRange<'_, Arc>>], ) -> Result<(), gpu_alloc::OutOfMemory> { + let rs: Vec<_> = ranges.iter().map(|r| (r.memory.lock(), r)).collect(); self.0 - .invalidate_mapped_memory_ranges(ranges.iter().map(|range| { + .invalidate_mapped_memory_ranges(rs.iter().map(|range| { ( - &**range.memory, + &*range.0, hal::memory::Segment { - offset: range.offset, - size: Some(range.size), + offset: range.1.offset, + size: Some(range.1.size), }, ) })) @@ -277,15 +285,16 @@ impl gpu_alloc::MemoryDevice> for MemoryDevice<' #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))] unsafe fn flush_memory_ranges( &self, - ranges: &[gpu_alloc::MappedMemoryRange<'_, Arc>], + ranges: &[gpu_alloc::MappedMemoryRange<'_, Arc>>], ) -> Result<(), gpu_alloc::OutOfMemory> { + let rs: Vec<_> = ranges.iter().map(|r| (r.memory.lock(), r)).collect(); self.0 - .flush_mapped_memory_ranges(ranges.iter().map(|range| { + .flush_mapped_memory_ranges(rs.iter().map(|range| { ( - &**range.memory, + &*range.0, hal::memory::Segment { - offset: range.offset, - size: Some(range.size), + offset: range.1.offset, + size: Some(range.1.size), }, ) })) diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 5a13d9e241..ad5f72f878 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -1482,14 +1482,14 @@ impl Device { .into_iter() .flat_map(|(_, list)| list) .collect::>(); - let write = hal::pso::DescriptorSetWrite { - set: desc_set.raw(), - binding: start_binding, - array_offset: 0, - descriptors, - }; unsafe { - self.raw.write_descriptor_sets(iter::once(write)); + let write = hal::pso::DescriptorSetWrite { + set: desc_set.raw_mut(), + binding: start_binding, + array_offset: 0, + descriptors, + }; + self.raw.write_descriptor_set(write); } } diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 56def4f192..071748da5c 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -617,7 +617,7 @@ impl Global { } // now prepare the GPU submission - let fence = device + let mut fence = device .raw .create_fence(false) .or(Err(DeviceError::OutOfMemory))?; @@ -634,7 +634,7 @@ impl Global { }; unsafe { - device.queue_group.queues[0].submit(submission, Some(&fence)); + device.queue_group.queues[0].submit(submission, Some(&mut fence)); } fence }; diff --git a/wgpu-core/src/swap_chain.rs b/wgpu-core/src/swap_chain.rs index 3730cfa189..58bfdee385 100644 --- a/wgpu-core/src/swap_chain.rs +++ b/wgpu-core/src/swap_chain.rs @@ -273,7 +273,7 @@ impl Global { }; let sem = if sc.active_submission_index > device.last_completed_submission_index() { - Some(&sc.semaphore) + Some(&mut sc.semaphore) } else { None };