Skip to content

Commit d74ecb3

Browse files
committed
hal/vulkan: Replace gpu-alloc by gpu-allocator
1 parent 0372d31 commit d74ecb3

File tree

9 files changed

+240
-371
lines changed

9 files changed

+240
-371
lines changed

Cargo.lock

Lines changed: 4 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ getrandom = "0.3"
120120
glam = "0.30"
121121
glob = "0.3"
122122
half = { version = "2.5", default-features = false } # We require 2.5 to have `Arbitrary` support.
123-
hashbrown = { version = "0.15", default-features = false, features = [
123+
hashbrown = { version = "0.15.2", default-features = false, features = [
124124
"default-hasher",
125125
"inline-more",
126126
] }
@@ -203,15 +203,19 @@ objc = "0.2.5"
203203
# Vulkan dependencies
204204
android_system_properties = "0.1.1"
205205
ash = "0.38"
206-
gpu-alloc = "0.6"
207206
gpu-descriptor = "0.3.2"
208207

209208
# DX12 dependencies
210-
gpu-allocator = { version = "0.27", default-features = false }
211209
range-alloc = "0.1"
212210
mach-dxcompiler-rs = { version = "0.1.4", default-features = false } # remember to increase max_shader_model if applicable
213211
windows-core = { version = "0.58", default-features = false }
214212

213+
# DX12 and Vulkan dependencies
214+
# # TODO: https://github.com/Traverse-Research/gpu-allocator/issues/281 put back a version
215+
gpu-allocator = { git = "https://github.com/Traverse-Research/gpu-allocator.git", rev = "673e4ecb503af4188e0ca576acd0dad681f22413", default-features = false, features = [
216+
"hashbrown",
217+
] }
218+
215219
# Gles dependencies
216220
khronos-egl = "6"
217221
glow = "0.16"

wgpu-hal/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ vulkan = [
9090
"dep:arrayvec",
9191
"dep:ash",
9292
"dep:bytemuck",
93-
"dep:gpu-alloc",
9493
"dep:gpu-descriptor",
9594
"dep:hashbrown",
9695
"dep:libc",
@@ -101,6 +100,7 @@ vulkan = [
101100
"dep:profiling",
102101
"dep:smallvec",
103102
"dep:windows",
103+
"gpu-allocator/vulkan",
104104
"windows/Win32",
105105
]
106106
gles = [
@@ -228,9 +228,10 @@ glow = { workspace = true, optional = true }
228228
########################
229229

230230
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
231+
# Backend: Vulkan and Dx12
232+
gpu-allocator = { workspace = true, optional = true }
231233
# Backend: Vulkan
232234
ash = { workspace = true, optional = true }
233-
gpu-alloc = { workspace = true, optional = true }
234235
gpu-descriptor = { workspace = true, optional = true }
235236
smallvec = { workspace = true, optional = true, features = ["union"] }
236237
# Backend: GLES
@@ -257,7 +258,6 @@ windows-core = { workspace = true, optional = true }
257258
# Backend: Dx12
258259
bit-set = { workspace = true, optional = true }
259260
range-alloc = { workspace = true, optional = true }
260-
gpu-allocator = { workspace = true, optional = true }
261261
# backend: GLES
262262
glutin_wgl_sys = { workspace = true, optional = true }
263263

wgpu-hal/src/dx12/suballocation.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl Allocator {
143143
allocations,
144144
blocks,
145145
total_allocated_bytes: upstream.total_allocated_bytes,
146-
total_reserved_bytes: upstream.total_reserved_bytes,
146+
total_reserved_bytes: upstream.total_capacity_bytes,
147147
}
148148
}
149149
}
@@ -621,37 +621,3 @@ impl<'a> DeviceAllocationContext<'a> {
621621
Ok(allocation_info)
622622
}
623623
}
624-
625-
impl From<gpu_allocator::AllocationError> for crate::DeviceError {
626-
fn from(result: gpu_allocator::AllocationError) -> Self {
627-
match result {
628-
gpu_allocator::AllocationError::OutOfMemory => Self::OutOfMemory,
629-
gpu_allocator::AllocationError::FailedToMap(e) => {
630-
log::error!("DX12 gpu-allocator: Failed to map: {e}");
631-
Self::Lost
632-
}
633-
gpu_allocator::AllocationError::NoCompatibleMemoryTypeFound => {
634-
log::error!("DX12 gpu-allocator: No Compatible Memory Type Found");
635-
Self::Lost
636-
}
637-
gpu_allocator::AllocationError::InvalidAllocationCreateDesc => {
638-
log::error!("DX12 gpu-allocator: Invalid Allocation Creation Description");
639-
Self::Lost
640-
}
641-
gpu_allocator::AllocationError::InvalidAllocatorCreateDesc(e) => {
642-
log::error!("DX12 gpu-allocator: Invalid Allocator Creation Description: {e}");
643-
Self::Lost
644-
}
645-
646-
gpu_allocator::AllocationError::Internal(e) => {
647-
log::error!("DX12 gpu-allocator: Internal Error: {e}");
648-
Self::Lost
649-
}
650-
gpu_allocator::AllocationError::BarrierLayoutNeedsDevice10
651-
| gpu_allocator::AllocationError::CastableFormatsRequiresEnhancedBarriers
652-
| gpu_allocator::AllocationError::CastableFormatsRequiresAtLeastDevice12 => {
653-
unreachable!()
654-
}
655-
}
656-
}
657-
}

wgpu-hal/src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,41 @@ pub enum DeviceError {
381381
Unexpected,
382382
}
383383

384+
#[cfg(any(dx12, vulkan))]
385+
impl From<gpu_allocator::AllocationError> for DeviceError {
386+
fn from(result: gpu_allocator::AllocationError) -> Self {
387+
match result {
388+
gpu_allocator::AllocationError::OutOfMemory => Self::OutOfMemory,
389+
gpu_allocator::AllocationError::FailedToMap(e) => {
390+
log::error!("DX12 gpu-allocator: Failed to map: {e}");
391+
Self::Lost
392+
}
393+
gpu_allocator::AllocationError::NoCompatibleMemoryTypeFound => {
394+
log::error!("DX12 gpu-allocator: No Compatible Memory Type Found");
395+
Self::Lost
396+
}
397+
gpu_allocator::AllocationError::InvalidAllocationCreateDesc => {
398+
log::error!("DX12 gpu-allocator: Invalid Allocation Creation Description");
399+
Self::Lost
400+
}
401+
gpu_allocator::AllocationError::InvalidAllocatorCreateDesc(e) => {
402+
log::error!("DX12 gpu-allocator: Invalid Allocator Creation Description: {e}");
403+
Self::Lost
404+
}
405+
406+
gpu_allocator::AllocationError::Internal(e) => {
407+
log::error!("DX12 gpu-allocator: Internal Error: {e}");
408+
Self::Lost
409+
}
410+
gpu_allocator::AllocationError::BarrierLayoutNeedsDevice10
411+
| gpu_allocator::AllocationError::CastableFormatsRequiresEnhancedBarriers
412+
| gpu_allocator::AllocationError::CastableFormatsRequiresAtLeastDevice12 => {
413+
unreachable!()
414+
}
415+
}
416+
}
417+
}
418+
384419
#[allow(dead_code)] // may be unused on some platforms
385420
#[cold]
386421
fn hal_usage_error<T: fmt::Display>(txt: T) -> ! {

wgpu-hal/src/vulkan/adapter.rs

Lines changed: 35 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,87 +2227,42 @@ impl super::Adapter {
22272227
signal_semaphores: Default::default(),
22282228
};
22292229

2230-
let mem_allocator = {
2231-
let limits = self.phd_capabilities.properties.limits;
2232-
2233-
// Note: the parameters here are not set in stone nor where they picked with
2234-
// strong confidence.
2235-
// `final_free_list_chunk` should be bigger than starting_free_list_chunk if
2236-
// we want the behavior of starting with smaller block sizes and using larger
2237-
// ones only after we observe that the small ones aren't enough, which I think
2238-
// is a good "I don't know what the workload is going to be like" approach.
2239-
//
2240-
// For reference, `VMA`, and `gpu_allocator` both start with 256 MB blocks
2241-
// (then VMA doubles the block size each time it needs a new block).
2242-
// At some point it would be good to experiment with real workloads
2243-
//
2244-
// TODO(#5925): The plan is to switch the Vulkan backend from `gpu_alloc` to
2245-
// `gpu_allocator` which has a different (simpler) set of configuration options.
2246-
//
2247-
// TODO: These parameters should take hardware capabilities into account.
2248-
let mb = 1024 * 1024;
2249-
let perf_cfg = gpu_alloc::Config {
2250-
starting_free_list_chunk: 128 * mb,
2251-
final_free_list_chunk: 512 * mb,
2252-
minimal_buddy_size: 1,
2253-
initial_buddy_dedicated_size: 8 * mb,
2254-
dedicated_threshold: 32 * mb,
2255-
preferred_dedicated_threshold: mb,
2256-
transient_dedicated_threshold: 128 * mb,
2257-
};
2258-
let mem_usage_cfg = gpu_alloc::Config {
2259-
starting_free_list_chunk: 8 * mb,
2260-
final_free_list_chunk: 64 * mb,
2261-
minimal_buddy_size: 1,
2262-
initial_buddy_dedicated_size: 8 * mb,
2263-
dedicated_threshold: 8 * mb,
2264-
preferred_dedicated_threshold: mb,
2265-
transient_dedicated_threshold: 16 * mb,
2266-
};
2267-
let config = match memory_hints {
2268-
wgt::MemoryHints::Performance => perf_cfg,
2269-
wgt::MemoryHints::MemoryUsage => mem_usage_cfg,
2270-
wgt::MemoryHints::Manual {
2271-
suballocated_device_memory_block_size,
2272-
} => gpu_alloc::Config {
2273-
starting_free_list_chunk: suballocated_device_memory_block_size.start,
2274-
final_free_list_chunk: suballocated_device_memory_block_size.end,
2275-
initial_buddy_dedicated_size: suballocated_device_memory_block_size.start,
2276-
..perf_cfg
2277-
},
2278-
};
2279-
2280-
let max_memory_allocation_size =
2281-
if let Some(maintenance_3) = self.phd_capabilities.maintenance_3 {
2282-
maintenance_3.max_memory_allocation_size
2283-
} else {
2284-
u64::MAX
2285-
};
2286-
let properties = gpu_alloc::DeviceProperties {
2287-
max_memory_allocation_count: limits.max_memory_allocation_count,
2288-
max_memory_allocation_size,
2289-
non_coherent_atom_size: limits.non_coherent_atom_size,
2290-
memory_types: memory_types
2291-
.iter()
2292-
.map(|memory_type| gpu_alloc::MemoryType {
2293-
props: gpu_alloc::MemoryPropertyFlags::from_bits_truncate(
2294-
memory_type.property_flags.as_raw() as u8,
2295-
),
2296-
heap: memory_type.heap_index,
2297-
})
2298-
.collect(),
2299-
memory_heaps: mem_properties
2300-
.memory_heaps_as_slice()
2301-
.iter()
2302-
.map(|&memory_heap| gpu_alloc::MemoryHeap {
2303-
size: memory_heap.size,
2304-
})
2305-
.collect(),
2306-
buffer_device_address: enabled_extensions
2307-
.contains(&khr::buffer_device_address::NAME),
2308-
};
2309-
gpu_alloc::GpuAllocator::new(config, properties)
2230+
// TODO: the allocator's configuration should take hardware capability into
2231+
// account.
2232+
const MB: u64 = 1024 * 1024;
2233+
let allocation_sizes = match memory_hints {
2234+
wgt::MemoryHints::Performance => gpu_allocator::AllocationSizes::new(128 * MB, 64 * MB)
2235+
.with_max_device_memblock_size(256 * MB)
2236+
.with_max_host_memblock_size(128 * MB),
2237+
wgt::MemoryHints::MemoryUsage => gpu_allocator::AllocationSizes::new(8 * MB, 4 * MB)
2238+
.with_max_device_memblock_size(64 * MB)
2239+
.with_max_host_memblock_size(32 * MB),
2240+
wgt::MemoryHints::Manual {
2241+
suballocated_device_memory_block_size,
2242+
} => {
2243+
// TODO: Would it be useful to expose the host size in memory hints
2244+
// instead of always using half of the device size?
2245+
let device_size = suballocated_device_memory_block_size;
2246+
let host_size = device_size.start / 2..device_size.end / 2;
2247+
2248+
gpu_allocator::AllocationSizes::new(device_size.start, host_size.start)
2249+
.with_max_device_memblock_size(device_size.end)
2250+
.with_max_host_memblock_size(host_size.end)
2251+
}
23102252
};
2253+
2254+
let buffer_device_address = enabled_extensions.contains(&khr::buffer_device_address::NAME);
2255+
2256+
let mem_allocator =
2257+
gpu_allocator::vulkan::Allocator::new(&gpu_allocator::vulkan::AllocatorCreateDesc {
2258+
instance: self.instance.raw.clone(),
2259+
device: shared.raw.clone(),
2260+
physical_device: self.raw,
2261+
debug_settings: Default::default(),
2262+
buffer_device_address,
2263+
allocation_sizes,
2264+
})?;
2265+
23112266
let desc_allocator = gpu_descriptor::DescriptorAllocator::new(
23122267
if let Some(di) = self.phd_capabilities.descriptor_indexing {
23132268
di.max_update_after_bind_descriptors_in_all_pools

0 commit comments

Comments
 (0)