GPUOpen-LibrariesAndSDKs / VulkanMemoryAllocator

Easy to integrate Vulkan memory allocation library
MIT License
2.56k stars 350 forks source link

Can I batch all allocations in one function call to VMA if I know my memory requirements upfront? #414

Closed IAmNotHanni closed 5 months ago

IAmNotHanni commented 5 months ago

Hello

I am currently working on a rendergraph (framegraph). As we know, one of the advantages of this is that all the resources which are required are (ideally) known upfront, so it allows to be proactive in resource management, especially memory allocation. Until now, during compilation of my rendergraph, I create all the buffers which are required for rendering one by one in a simple loop.

This means VMA only knows about the already existing allocations and about the allocation that it's making next, but it does not know about all allocations which are upcoming in total. Depending on game engine design, it could though, right? I am wondering: Could there be some benefit for memory allocation if VMA would know about the "whole picture" of memory which is required? This way, maybe VMA could better reason about which resource to put in which place of memory.

For example I could imagine something like vmaCreateBufferBatch:

VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateBufferBatch(
    VmaAllocator allocator, // Use only one allocator for all allocations
    const VkBufferCreateInfo* pBufferCreateInfos, // A pointer to the array of buffer create infos
    uint32_t pBufferCount, // The number of buffers to create
    const VmaAllocationCreateInfo* pAllocationCreateInfo, // A pointer to the allocation create infos
    VkBuffer* pBuffer, // ...
    VmaAllocation* pAllocation, // ...
    VmaAllocationInfo* pAllocationInfo // ...
)

Once my rendergraph would know which resources to allocate, I could do all allocations in one batch call and maybe VMA could maybe benefit from it in terms of deciding how to allocate memory.

What do you think?

best regards Johannes

adam-sawicki-a commented 5 months ago

Hello

Thanks for your question. This is a good topic to discuss. I even considered an API like this, although it was for resources that alias in the same place in memory, which you didn't mention.

Short answer is: No such feature exists or is planned for VMA, but you can use existing pieces of the library interface to achieve your goals.

With aliasing resources, you need to determine what can alias with what and how place them in memory (at which offset within the allocation) by yourself.

Without aliasing, I don't think there would be any performance or memory benefit in providing a function like you proposed to allocate a batch of multiple buffers over separate calls to vmaCreateBuffer, unless you mean the overhead of calling into the VMA library itself. For this, there is function vmaAllocateMemoryPages available that creates multiple allocation objects (all with the same offset, size, and memory type) at once, which is intended mostly to help with sparse binding/residency.

IAmNotHanni commented 5 months ago

Hello Thank you for this great answer! best regards Johannes