Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions include/vk_mem_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,21 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBuffer(
VmaAllocation VMA_NULLABLE* VMA_NOT_NULL pAllocation,
VmaAllocationInfo* VMA_NULLABLE pAllocationInfo);

/** \brief Allocates memory for a buffer with additional minimum alignment.

Similar to vmaAllocateMemoryForBuffer() but provides additional parameter `minAlignment` which allows to specify custom,
minimum alignment to be used when placing the allocation inside a larger memory block.

See also vmaCreateBufferWithAlignment().
*/
VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBufferWithAlignment(
VmaAllocator VMA_NOT_NULL allocator,
VkBuffer VMA_NOT_NULL_NON_DISPATCHABLE buffer,
const VmaAllocationCreateInfo* VMA_NOT_NULL pCreateInfo,
VkDeviceSize minAlignment,
VmaAllocation VMA_NULLABLE* VMA_NOT_NULL pAllocation,
VmaAllocationInfo* VMA_NULLABLE pAllocationInfo);

/** \brief Allocates memory suitable for given `VkImage`.

\param allocator
Expand Down Expand Up @@ -16265,9 +16280,21 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBuffer(
VmaAllocation* pAllocation,
VmaAllocationInfo* pAllocationInfo)
{
VMA_ASSERT(allocator && buffer != VK_NULL_HANDLE && pCreateInfo && pAllocation);

VMA_DEBUG_LOG("vmaAllocateMemoryForBuffer");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this VMA_DEBUG_LOG() be removed? Could add a common internal implementation like AllocateMemoryForBuffer()? Inline the implementations?

Currently vmaCreateBuffer() and vmaCreateBufferWithAlignment() share an internal CreateBuffer() that has:

        // 2a. Include minAlignment
        vkMemReq.alignment = VMA_MAX(vkMemReq.alignment, minAlignment);

return vmaAllocateMemoryForBufferWithAlignment(allocator, buffer, pCreateInfo, 0, pAllocation, pAllocationInfo);
}

VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBufferWithAlignment(
VmaAllocator allocator,
VkBuffer buffer,
const VmaAllocationCreateInfo* pCreateInfo,
VkDeviceSize minAlignment,
VmaAllocation* pAllocation,
VmaAllocationInfo* pAllocationInfo)
{
VMA_ASSERT(allocator && buffer != VK_NULL_HANDLE && pCreateInfo && VmaIsPow2(minAlignment) && pAllocation);

VMA_DEBUG_LOG("vmaAllocateMemoryForBufferWithAlignment");

VMA_DEBUG_GLOBAL_MUTEX_LOCK

Expand All @@ -16278,6 +16305,9 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBuffer(
requiresDedicatedAllocation,
prefersDedicatedAllocation);

// Apply custom minimum alignment
vkMemReq.alignment = VMA_MAX(vkMemReq.alignment, minAlignment);

VkResult result = allocator->AllocateMemory(
vkMemReq,
requiresDedicatedAllocation,
Expand Down