Traverse-Research / gpu-allocator

🦀 GPU memory allocator for Vulkan, DirectX 12 and Metal. Written in pure Rust
https://traverse.nl/
Apache License 2.0
381 stars 50 forks source link

Add support for growing the memory block sizes #254

Open nical opened 6 days ago

nical commented 6 days ago

Fixes #235.

This PR extends the AllocationsSizes configuration to define ranges of block sizes. The allocators start by allocating blocks with the minimum size and each new allocation for the same memory type doubles the block size.

This allows applications to scale memory usage more organically based on demand. While it is typically not an optimal choice for most games, it allows other types of apps that don't have well defined memory budgets to strike a good tradeoff. My own motivation comes from Firefox's WebGPU implementation. See #235 for more details.

While writing this I found out that the vulkan allocator was not taking the allocation_size parameter from its descritptor, so I fixed that along the way.

I, m not sure about how to expose this in the API. In its current state, this PR only adds to the current API and does not change anything for folks who want to stick with the current behavior (fixed block sizes).

Example:

const MB: u64 = 1024 * 1024;
// This configuration uses fixed memory block sizes.
let fixed = AllocationSizes::new(256 * MB, 64 * MB);
// This configuration starts with 8MB memory blocks
// and grows the block size of a given memory type each
// time a new allocation is needed, up to a limit of
// 256MB for device memory and 64MB for host memory.
let growing = AllocationSizes::new(8 * MB, 8 * MB)
    .with_max_device_memblock_size(256 * MB)
    .with_max_host_memblock_size(64 * MB);

But it looks a bit quirky. One might prefer to simply spell out the ranges when creating AllocationSizes. let me know what you prefer.