GPUOpen-LibrariesAndSDKs / VulkanMemoryAllocator

Easy to integrate Vulkan memory allocation library
MIT License
2.63k stars 359 forks source link

do not create a dummy buffer and destroy when call vmaFindMemoryTypeIndexForBufferInfo #419

Open papazhang66 opened 5 months ago

papazhang66 commented 5 months ago

we saw everytime call vmaFindMemoryTypeIndexForBufferInfo, it take quite a few cpu cycles to do the following

"// Must create a dummy buffer to query :( VkBuffer hBuffer = VK_NULL_HANDLE; res = funcs->vkCreateBuffer( hDev, pBufferCreateInfo, allocator->GetAllocationCallbacks(), &hBuffer); if(res == VK_SUCCESS) { VkMemoryRequirements memReq = {}; funcs->vkGetBufferMemoryRequirements(hDev, hBuffer, &memReq);

        res = allocator->FindMemoryTypeIndex(
            memReq.memoryTypeBits, pAllocationCreateInfo,
            VmaBufferImageUsage(*pBufferCreateInfo, allocator->m_UseKhrMaintenance5), pMemoryTypeIndex);

        funcs->vkDestroyBuffer(
            hDev, hBuffer, allocator->GetAllocationCallbacks());
    }"

according to the spec. "The memoryTypeBits member is identical for all VkBuffer objects created with the same value for the flags and usage members in the VkBufferCreateInfo structure and the handleTypes member of the VkExternalMemoryBufferCreateInfo structure passed to vkCreateBuffer. Further, if usage1 and usage2 of type VkBufferUsageFlags are such that the bits set in usage2 are a subset of the bits set in usage1, and they have the same flags and VkExternalMemoryBufferCreateInfo::handleTypes, then the bits set in memoryTypeBits returned for usage1 must be a subset of the bits set in memoryTypeBits returned for usage2, for all values of flags." it looks the returned memoryTypeBit only affected by flags and usage and external buffer type. can we create a cache to avoid everytime create a dummy buffer.

adam-sawicki-a commented 5 months ago

Thank you for this suggestion.

The problem doesn't occur if you use Vulkan 1.3 or VK_KHR_maintenance4 extension (and inform VMA library about it during library initialization), because then function vmaFindMemoryTypeIndexForBufferInfo can use a new Vulkan function vkGetDeviceBufferMemoryRequirements. Can you enable Vulkan 1.3 or this extension in your project?

Implementing a cache for recently used buffer/image creation parameters is a good idea that may be implemented in the future.

papazhang66 commented 4 months ago

currently we can not update the version to 1.3. since we have so many device running on vulkan 1.1. it will be perfect, if can fix this issue. thanks