mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-28 00:59:12 +01:00
Update gpu-alloc and remove the associated locking
This commit is contained in:
1 parent
e219f3b657
commit
5fccabf3b8
3 files changed
+53
-66
No files matched your search
Generated
+11
-11
@@ -476,7 +476,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "gfx-auxil"
|
||||
version = "0.5.0"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=477f17957a54d024f0077584b68e308376cb05c6#477f17957a54d024f0077584b68e308376cb05c6"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=59730cdeefdb00c61ba747dbcaa2087e7a5b53f6#59730cdeefdb00c61ba747dbcaa2087e7a5b53f6"
|
||||
dependencies = [
|
||||
"fxhash",
|
||||
"gfx-hal",
|
||||
@@ -486,7 +486,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "gfx-backend-dx11"
|
||||
version = "0.6.0"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=477f17957a54d024f0077584b68e308376cb05c6#477f17957a54d024f0077584b68e308376cb05c6"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=59730cdeefdb00c61ba747dbcaa2087e7a5b53f6#59730cdeefdb00c61ba747dbcaa2087e7a5b53f6"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bitflags",
|
||||
@@ -507,7 +507,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "gfx-backend-dx12"
|
||||
version = "0.6.2"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=477f17957a54d024f0077584b68e308376cb05c6#477f17957a54d024f0077584b68e308376cb05c6"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=59730cdeefdb00c61ba747dbcaa2087e7a5b53f6#59730cdeefdb00c61ba747dbcaa2087e7a5b53f6"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bit-set",
|
||||
@@ -527,7 +527,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "gfx-backend-empty"
|
||||
version = "0.6.0"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=477f17957a54d024f0077584b68e308376cb05c6#477f17957a54d024f0077584b68e308376cb05c6"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=59730cdeefdb00c61ba747dbcaa2087e7a5b53f6#59730cdeefdb00c61ba747dbcaa2087e7a5b53f6"
|
||||
dependencies = [
|
||||
"gfx-hal",
|
||||
"log",
|
||||
@@ -537,7 +537,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "gfx-backend-gl"
|
||||
version = "0.6.0"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=477f17957a54d024f0077584b68e308376cb05c6#477f17957a54d024f0077584b68e308376cb05c6"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=59730cdeefdb00c61ba747dbcaa2087e7a5b53f6#59730cdeefdb00c61ba747dbcaa2087e7a5b53f6"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bitflags",
|
||||
@@ -559,7 +559,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "gfx-backend-metal"
|
||||
version = "0.6.0"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=477f17957a54d024f0077584b68e308376cb05c6#477f17957a54d024f0077584b68e308376cb05c6"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=59730cdeefdb00c61ba747dbcaa2087e7a5b53f6#59730cdeefdb00c61ba747dbcaa2087e7a5b53f6"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bitflags",
|
||||
@@ -583,7 +583,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "gfx-backend-vulkan"
|
||||
version = "0.6.5"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=477f17957a54d024f0077584b68e308376cb05c6#477f17957a54d024f0077584b68e308376cb05c6"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=59730cdeefdb00c61ba747dbcaa2087e7a5b53f6#59730cdeefdb00c61ba747dbcaa2087e7a5b53f6"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"ash",
|
||||
@@ -603,7 +603,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "gfx-hal"
|
||||
version = "0.6.0"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=477f17957a54d024f0077584b68e308376cb05c6#477f17957a54d024f0077584b68e308376cb05c6"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=59730cdeefdb00c61ba747dbcaa2087e7a5b53f6#59730cdeefdb00c61ba747dbcaa2087e7a5b53f6"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"naga",
|
||||
@@ -626,7 +626,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "gpu-alloc"
|
||||
version = "0.2.1"
|
||||
source = "git+https://github.com/zakarumych/gpu-alloc?rev=15f4fe2cebbd0bf9ab446757a8e370e0adf4c502#15f4fe2cebbd0bf9ab446757a8e370e0adf4c502"
|
||||
source = "git+https://github.com/zakarumych/gpu-alloc?rev=915d0dad0db340a4d37d5abd597b8b796e7f35f6#915d0dad0db340a4d37d5abd597b8b796e7f35f6"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"gpu-alloc-types",
|
||||
@@ -636,7 +636,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "gpu-alloc-types"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/zakarumych/gpu-alloc?rev=15f4fe2cebbd0bf9ab446757a8e370e0adf4c502#15f4fe2cebbd0bf9ab446757a8e370e0adf4c502"
|
||||
source = "git+https://github.com/zakarumych/gpu-alloc?rev=915d0dad0db340a4d37d5abd597b8b796e7f35f6#915d0dad0db340a4d37d5abd597b8b796e7f35f6"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
@@ -1196,7 +1196,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "range-alloc"
|
||||
version = "0.1.1"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=477f17957a54d024f0077584b68e308376cb05c6#477f17957a54d024f0077584b68e308376cb05c6"
|
||||
source = "git+https://github.com/gfx-rs/gfx?rev=59730cdeefdb00c61ba747dbcaa2087e7a5b53f6#59730cdeefdb00c61ba747dbcaa2087e7a5b53f6"
|
||||
|
||||
[[package]]
|
||||
name = "raw-window-handle"
|
||||
|
||||
+11
-11
@@ -33,27 +33,27 @@ smallvec = "1"
|
||||
tracing = { version = "0.1", default-features = false, features = ["std"] }
|
||||
thiserror = "1"
|
||||
|
||||
gpu-alloc = { git = "https://github.com/zakarumych/gpu-alloc", rev = "15f4fe2cebbd0bf9ab446757a8e370e0adf4c502", features = ["tracing"] }
|
||||
gpu-alloc = { git = "https://github.com/zakarumych/gpu-alloc", rev = "915d0dad0db340a4d37d5abd597b8b796e7f35f6", features = ["tracing"] }
|
||||
gpu-descriptor = { git = "https://github.com/zakarumych/gpu-descriptor", rev = "aa092613889f03f8254d6f7278d08c655324c7c7", features = ["tracing"] }
|
||||
|
||||
hal = { package = "gfx-hal", git = "https://github.com/gfx-rs/gfx", rev = "477f17957a54d024f0077584b68e308376cb05c6" }
|
||||
gfx-backend-empty = { git = "https://github.com/gfx-rs/gfx", rev = "477f17957a54d024f0077584b68e308376cb05c6" }
|
||||
hal = { package = "gfx-hal", git = "https://github.com/gfx-rs/gfx", rev = "59730cdeefdb00c61ba747dbcaa2087e7a5b53f6" }
|
||||
gfx-backend-empty = { git = "https://github.com/gfx-rs/gfx", rev = "59730cdeefdb00c61ba747dbcaa2087e7a5b53f6" }
|
||||
|
||||
[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 = "477f17957a54d024f0077584b68e308376cb05c6", features = ["naga"] }
|
||||
gfx-backend-gl = { git = "https://github.com/gfx-rs/gfx", rev = "477f17957a54d024f0077584b68e308376cb05c6", features = ["naga"] }
|
||||
gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "59730cdeefdb00c61ba747dbcaa2087e7a5b53f6", features = ["naga"] }
|
||||
gfx-backend-gl = { git = "https://github.com/gfx-rs/gfx", rev = "59730cdeefdb00c61ba747dbcaa2087e7a5b53f6", 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 = "477f17957a54d024f0077584b68e308376cb05c6", features = ["naga"] }
|
||||
gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "477f17957a54d024f0077584b68e308376cb05c6", optional = true }
|
||||
gfx-backend-metal = { git = "https://github.com/gfx-rs/gfx", rev = "59730cdeefdb00c61ba747dbcaa2087e7a5b53f6", features = ["naga"] }
|
||||
gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "59730cdeefdb00c61ba747dbcaa2087e7a5b53f6", optional = true }
|
||||
|
||||
[target.'cfg(all(not(target_arch = "wasm32"), windows))'.dependencies]
|
||||
gfx-backend-dx12 = { git = "https://github.com/gfx-rs/gfx", rev = "477f17957a54d024f0077584b68e308376cb05c6" }
|
||||
gfx-backend-dx11 = { git = "https://github.com/gfx-rs/gfx", rev = "477f17957a54d024f0077584b68e308376cb05c6" }
|
||||
gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "477f17957a54d024f0077584b68e308376cb05c6", features = ["naga"] }
|
||||
gfx-backend-dx12 = { git = "https://github.com/gfx-rs/gfx", rev = "59730cdeefdb00c61ba747dbcaa2087e7a5b53f6" }
|
||||
gfx-backend-dx11 = { git = "https://github.com/gfx-rs/gfx", rev = "59730cdeefdb00c61ba747dbcaa2087e7a5b53f6" }
|
||||
gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "59730cdeefdb00c61ba747dbcaa2087e7a5b53f6", features = ["naga"] }
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
gfx-backend-gl = { git = "https://github.com/gfx-rs/gfx", rev = "477f17957a54d024f0077584b68e308376cb05c6", features = ["naga"] }
|
||||
gfx-backend-gl = { git = "https://github.com/gfx-rs/gfx", rev = "59730cdeefdb00c61ba747dbcaa2087e7a5b53f6", features = ["naga"] }
|
||||
|
||||
[dependencies.naga]
|
||||
git = "https://github.com/gfx-rs/naga"
|
||||
|
||||
@@ -3,22 +3,15 @@
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use super::DeviceError;
|
||||
use hal::device::Device;
|
||||
use parking_lot::Mutex;
|
||||
use std::{borrow::Cow, fmt, iter, ptr::NonNull, sync::Arc};
|
||||
use hal::device::Device as _;
|
||||
use std::{borrow::Cow, iter, ptr::NonNull};
|
||||
|
||||
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<Mutex<B::Memory>>>);
|
||||
pub struct MemoryAllocator<B: hal::Backend>(gpu_alloc::GpuAllocator<B::Memory>);
|
||||
#[derive(Debug)]
|
||||
pub struct MemoryBlock<B: hal::Backend>(gpu_alloc::MemoryBlock<B::Memory>);
|
||||
struct MemoryDevice<'a, B: hal::Backend>(&'a B::Device);
|
||||
|
||||
//TODO: https://github.com/zakarumych/gpu-alloc/issues/9
|
||||
impl<B: hal::Backend> fmt::Debug for MemoryAllocator<B> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "MemoryAllocator")
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: hal::Backend> MemoryAllocator<B> {
|
||||
pub fn new(mem_props: hal::adapter::MemoryProperties, limits: hal::Limits) -> Self {
|
||||
let mem_config = gpu_alloc::Config {
|
||||
@@ -100,19 +93,19 @@ impl<B: hal::Backend> MemoryBlock<B> {
|
||||
device: &B::Device,
|
||||
buffer: &mut B::Buffer,
|
||||
) -> Result<(), DeviceError> {
|
||||
let mem = self.0.memory();
|
||||
unsafe {
|
||||
let mem = self.0.memory().lock();
|
||||
device
|
||||
.bind_buffer_memory(&*mem, 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> {
|
||||
let mem = self.0.memory();
|
||||
unsafe {
|
||||
let mem = self.0.memory().lock();
|
||||
device
|
||||
.bind_image_memory(&*mem, self.0.offset(), image)
|
||||
.bind_image_memory(mem, self.0.offset(), image)
|
||||
.map_err(DeviceError::from_bind)
|
||||
}
|
||||
}
|
||||
@@ -187,10 +180,10 @@ impl<B: hal::Backend> MemoryBlock<B> {
|
||||
size: Option<wgt::BufferAddress>,
|
||||
) -> Result<(), DeviceError> {
|
||||
let segment = self.segment(inner_offset, size);
|
||||
let mem = self.0.memory();
|
||||
unsafe {
|
||||
let mem = self.0.memory().lock();
|
||||
device
|
||||
.flush_mapped_memory_ranges(iter::once((&*mem, segment)))
|
||||
.flush_mapped_memory_ranges(iter::once((mem, segment)))
|
||||
.or(Err(DeviceError::OutOfMemory))
|
||||
}
|
||||
}
|
||||
@@ -202,47 +195,44 @@ impl<B: hal::Backend> MemoryBlock<B> {
|
||||
size: Option<wgt::BufferAddress>,
|
||||
) -> Result<(), DeviceError> {
|
||||
let segment = self.segment(inner_offset, size);
|
||||
let mem = self.0.memory();
|
||||
unsafe {
|
||||
let mem = self.0.memory().lock();
|
||||
device
|
||||
.invalidate_mapped_memory_ranges(iter::once((&*mem, segment)))
|
||||
.invalidate_mapped_memory_ranges(iter::once((mem, segment)))
|
||||
.or(Err(DeviceError::OutOfMemory))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: hal::Backend> gpu_alloc::MemoryDevice<Arc<Mutex<B::Memory>>> for MemoryDevice<'_, B> {
|
||||
impl<B: hal::Backend> gpu_alloc::MemoryDevice<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<Mutex<B::Memory>>, gpu_alloc::OutOfMemory> {
|
||||
) -> Result<B::Memory, gpu_alloc::OutOfMemory> {
|
||||
assert!(flags.is_empty());
|
||||
|
||||
self.0
|
||||
.allocate_memory(hal::MemoryTypeId(memory_type as _), size)
|
||||
.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<Mutex<B::Memory>>) {
|
||||
let memory = Arc::try_unwrap(memory).expect("Memory must not be used anywhere");
|
||||
self.0.free_memory(memory.into_inner());
|
||||
unsafe fn deallocate_memory(&self, memory: B::Memory) {
|
||||
self.0.free_memory(memory);
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
|
||||
unsafe fn map_memory(
|
||||
&self,
|
||||
memory: &Arc<Mutex<B::Memory>>,
|
||||
memory: &mut B::Memory,
|
||||
offset: u64,
|
||||
size: u64,
|
||||
) -> Result<NonNull<u8>, gpu_alloc::DeviceMapError> {
|
||||
let mut mem = memory.lock();
|
||||
match self.0.map_memory(
|
||||
&mut *mem,
|
||||
memory,
|
||||
hal::memory::Segment {
|
||||
offset,
|
||||
size: Some(size),
|
||||
@@ -258,24 +248,22 @@ impl<B: hal::Backend> gpu_alloc::MemoryDevice<Arc<Mutex<B::Memory>>> for MemoryD
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
|
||||
unsafe fn unmap_memory(&self, memory: &Arc<Mutex<B::Memory>>) {
|
||||
let mut mem = memory.lock();
|
||||
self.0.unmap_memory(&mut *mem);
|
||||
unsafe fn unmap_memory(&self, memory: &mut B::Memory) {
|
||||
self.0.unmap_memory(memory);
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
|
||||
unsafe fn invalidate_memory_ranges(
|
||||
&self,
|
||||
ranges: &[gpu_alloc::MappedMemoryRange<'_, Arc<Mutex<B::Memory>>>],
|
||||
ranges: &[gpu_alloc::MappedMemoryRange<'_, B::Memory>],
|
||||
) -> Result<(), gpu_alloc::OutOfMemory> {
|
||||
let rs: Vec<_> = ranges.iter().map(|r| (r.memory.lock(), r)).collect();
|
||||
self.0
|
||||
.invalidate_mapped_memory_ranges(rs.iter().map(|range| {
|
||||
.invalidate_mapped_memory_ranges(ranges.iter().map(|r| {
|
||||
(
|
||||
&*range.0,
|
||||
r.memory,
|
||||
hal::memory::Segment {
|
||||
offset: range.1.offset,
|
||||
size: Some(range.1.size),
|
||||
offset: r.offset,
|
||||
size: Some(r.size),
|
||||
},
|
||||
)
|
||||
}))
|
||||
@@ -285,16 +273,15 @@ impl<B: hal::Backend> gpu_alloc::MemoryDevice<Arc<Mutex<B::Memory>>> for MemoryD
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
|
||||
unsafe fn flush_memory_ranges(
|
||||
&self,
|
||||
ranges: &[gpu_alloc::MappedMemoryRange<'_, Arc<Mutex<B::Memory>>>],
|
||||
ranges: &[gpu_alloc::MappedMemoryRange<'_, B::Memory>],
|
||||
) -> Result<(), gpu_alloc::OutOfMemory> {
|
||||
let rs: Vec<_> = ranges.iter().map(|r| (r.memory.lock(), r)).collect();
|
||||
self.0
|
||||
.flush_mapped_memory_ranges(rs.iter().map(|range| {
|
||||
.flush_mapped_memory_ranges(ranges.iter().map(|r| {
|
||||
(
|
||||
&*range.0,
|
||||
r.memory,
|
||||
hal::memory::Segment {
|
||||
offset: range.1.offset,
|
||||
size: Some(range.1.size),
|
||||
offset: r.offset,
|
||||
size: Some(r.size),
|
||||
},
|
||||
)
|
||||
}))
|
||||
|
||||
Reference in new issue
Block a user