Skip to content

Commit 1b51fcf

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

File tree

9 files changed

+249
-372
lines changed

9 files changed

+249
-372
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,18 @@ objc = "0.2.5"
207207
# Vulkan dependencies
208208
android_system_properties = "0.1.1"
209209
ash = "0.38"
210-
gpu-alloc = "0.6"
211210
gpu-descriptor = "0.3.2"
212211

213212
# DX12 dependencies
214-
gpu-allocator = { version = "0.27", default-features = false }
215213
range-alloc = "0.1"
216214
mach-dxcompiler-rs = { version = "0.1.4", default-features = false } # remember to increase max_shader_model if applicable
217215
windows-core = { version = "0.58", default-features = false }
218216

217+
# DX12 and Vulkan dependencies
218+
gpu-allocator = { version = "0.28.0", default-features = false, features = [
219+
"hashbrown",
220+
] }
221+
219222
# Gles dependencies
220223
khronos-egl = "6"
221224
glow = "0.16"

wgpu-hal/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ vulkan = [
9191
"dep:arrayvec",
9292
"dep:ash",
9393
"dep:bytemuck",
94-
"dep:gpu-alloc",
9594
"dep:gpu-descriptor",
9695
"dep:hashbrown",
9796
"dep:libc",
@@ -102,6 +101,7 @@ vulkan = [
102101
"dep:profiling",
103102
"dep:smallvec",
104103
"dep:windows",
104+
"gpu-allocator/vulkan",
105105
"windows/Win32",
106106
]
107107
gles = [
@@ -142,7 +142,7 @@ dx12 = [
142142
"dep:profiling",
143143
"dep:range-alloc",
144144
"dep:windows-core",
145-
"dep:gpu-allocator",
145+
"gpu-allocator/d3d12",
146146
"windows/Win32_Graphics_Direct3D_Fxc",
147147
"windows/Win32_Graphics_Direct3D_Dxc",
148148
"windows/Win32_Graphics_Direct3D",
@@ -221,9 +221,10 @@ glow = { workspace = true, optional = true }
221221
########################
222222

223223
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
224+
# Backend: Vulkan and Dx12
225+
gpu-allocator = { workspace = true, optional = true }
224226
# Backend: Vulkan
225227
ash = { workspace = true, optional = true }
226-
gpu-alloc = { workspace = true, optional = true }
227228
gpu-descriptor = { workspace = true, optional = true }
228229
smallvec = { workspace = true, optional = true, features = ["union"] }
229230
# Backend: GLES
@@ -250,7 +251,6 @@ windows-core = { workspace = true, optional = true }
250251
# Backend: Dx12
251252
bit-set = { workspace = true, optional = true }
252253
range-alloc = { workspace = true, optional = true }
253-
gpu-allocator = { workspace = true, optional = true, features = ["d3d12"] }
254254
once_cell = { workspace = true, optional = true }
255255
# backend: GLES
256256
glutin_wgl_sys = { workspace = true, optional = true }

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
@@ -2255,87 +2255,42 @@ impl super::Adapter {
22552255
signal_semaphores: Default::default(),
22562256
};
22572257

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

0 commit comments

Comments
 (0)