Scott Oliver
Scott Oliver

Reputation: 547

Vulkan memory alignment requirements

I'm implementing a naive memory manager for Vulkan device memory, and would like to make sure that I understand the alignment requirements for memory and how to satisfy them.

So, assuming that I've allocated a 'pool' of memory using vkAllocateMemory and wish to sub-allocate blocks of memory in this pool to individual resources (based on a VkMemoryRequirements struct), will the following pseudocode be able to allocate a section of this memory with the correct size and alignment requirements?

In other words, do we just need to make sure that Offset is a multiple of RequiredAlignment?

Upvotes: 3

Views: 6443

Answers (1)

ratchet freak
ratchet freak

Reputation: 48196

In other words, do we just need to make sure that Offset is a multiple of RequiredAlignment?

for alignment that is nearly sufficient.

in vkBindbufferMemory one of the valid usage requirements is:

memoryOffset must be an integer multiple of the alignment member of the VkMemoryRequirements structure returned from a call to vkGetBufferMemoryRequirements with buffer

and there is a parallel statement in the valid usage requirements of vkBindImageMemory:

memoryOffset must be an integer multiple of the alignment member of the VkMemoryRequirements structure returned from a call to vkGetImageMemoryRequirements with image

If the previous block contains a non-linear resource while the current one is linear or vice versa then the alignment requirement is the max of the VkMemoryRequirements.alignment and the device's bufferImageGranularity. This also needs to be check for the end of the memory block.

However you also need to take into account that the memory type of the pool must be set in the memoryTypeBits flags of VkMemoryRequirements .

Upvotes: 5

Related Questions