Changed Arc<B::Memory> to Arc<Mutex<B::Memory>> in GpuAllocator

This commit is contained in:
TheArtist
2021-01-09 19:53:13 +02:00
parent 8c00cbcc8e
commit ce908c5f57
6 changed files with 65 additions and 57 deletions

23
Cargo.lock generated
View File

@@ -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"

View File

@@ -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"

View File

@@ -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<B: hal::Backend>(gpu_alloc::GpuAllocator<Arc<B::Memory>>);
pub struct MemoryAllocator<B: hal::Backend>(gpu_alloc::GpuAllocator<Arc<Mutex<B::Memory>>>);
#[derive(Debug)]
pub struct MemoryBlock<B: hal::Backend>(gpu_alloc::MemoryBlock<Arc<B::Memory>>);
pub struct MemoryBlock<B: hal::Backend>(gpu_alloc::MemoryBlock<Arc<Mutex<B::Memory>>>);
struct MemoryDevice<'a, B: hal::Backend>(&'a B::Device);
//TODO: https://github.com/zakarumych/gpu-alloc/issues/9
@@ -100,16 +101,18 @@ impl<B: hal::Backend> MemoryBlock<B> {
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<B: hal::Backend> MemoryBlock<B> {
) -> 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<B: hal::Backend> MemoryBlock<B> {
) -> 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<B: hal::Backend> gpu_alloc::MemoryDevice<Arc<B::Memory>> for MemoryDevice<'_, B> {
impl<B: hal::Backend> gpu_alloc::MemoryDevice<Arc<Mutex<B::Memory>>> 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<Arc<B::Memory>, gpu_alloc::OutOfMemory> {
) -> Result<Arc<Mutex<B::Memory>>, 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<B::Memory>) {
unsafe fn deallocate_memory(&self, memory: Arc<Mutex<B::Memory>>) {
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<B::Memory>,
memory: &Arc<Mutex<B::Memory>>,
offset: u64,
size: u64,
) -> Result<NonNull<u8>, 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<B: hal::Backend> gpu_alloc::MemoryDevice<Arc<B::Memory>> for MemoryDevice<'
}
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
unsafe fn unmap_memory(&self, memory: &Arc<B::Memory>) {
self.0.unmap_memory(memory);
unsafe fn unmap_memory(&self, memory: &Arc<Mutex<B::Memory>>) {
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<B::Memory>>],
ranges: &[gpu_alloc::MappedMemoryRange<'_, Arc<Mutex<B::Memory>>>],
) -> 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<B: hal::Backend> gpu_alloc::MemoryDevice<Arc<B::Memory>> for MemoryDevice<'
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
unsafe fn flush_memory_ranges(
&self,
ranges: &[gpu_alloc::MappedMemoryRange<'_, Arc<B::Memory>>],
ranges: &[gpu_alloc::MappedMemoryRange<'_, Arc<Mutex<B::Memory>>>],
) -> 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),
},
)
}))

View File

@@ -1482,14 +1482,14 @@ impl<B: GfxBackend> Device<B> {
.into_iter()
.flat_map(|(_, list)| list)
.collect::<Vec<_>>();
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);
}
}

View File

@@ -617,7 +617,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}
// 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<G: GlobalIdentityHandlerFactory> Global<G> {
};
unsafe {
device.queue_group.queues[0].submit(submission, Some(&fence));
device.queue_group.queues[0].submit(submission, Some(&mut fence));
}
fence
};

View File

@@ -273,7 +273,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
};
let sem = if sc.active_submission_index > device.last_completed_submission_index() {
Some(&sc.semaphore)
Some(&mut sc.semaphore)
} else {
None
};