Skip to content

Commit ff5648a

Browse files
committed
hal/vulkan: Replace gpu-alloc by gpu-allocator
1 parent 4853133 commit ff5648a

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
@@ -2235,87 +2235,42 @@ impl super::Adapter {
22352235
signal_semaphores: Default::default(),
22362236
};
22372237

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

0 commit comments

Comments
 (0)