Vulkan 内存对齐要求 [英] Vulkan memory alignment requirements
问题描述
我正在为 Vulkan 设备内存实现一个简单的内存管理器,并希望确保我了解内存的对齐要求以及如何满足这些要求.
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.
因此,假设我已经使用 vkAllocateMemory 分配了一个池"内存并希望将此池中的内存块子分配给各个资源(基于 VkMemoryRequirements 结构),以下伪代码是否能够分配具有正确大小和对齐要求的这段内存?
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?
- 使用RequiredSize 和RequiredAlignment 请求内存
- 遍历池中的块,寻找空闲且大小 > RequiredSize
- 如果当前块在内存中的偏移量不能被RequiredAlignment整除,找出对齐方式和余数之间的差异
- 如果当前块的大小减去差值小于RequiredSize,则跳到池中的下一个块
- 如果差值大于0,则插入大小等于差值的填充块,并调整当前未分配的块大小和偏移量
- 从当前未分配块的开头分配RequiredSize字节(现已对齐),相应地调整未分配块的Size和Offset
- 返回 vkDeviceMemory 句柄(池的)、大小和偏移量(新分配的块)
- 如果我们到达块列表的末尾,则该池无法分配内存
换句话说,我们是否只需要确保 Offset 是 RequiredAlignment 的倍数?
In other words, do we just need to make sure that Offset is a multiple of RequiredAlignment?
推荐答案
换句话说,我们是否只需要确保 Offset 是 RequiredAlignment 的倍数?
In other words, do we just need to make sure that Offset is a multiple of RequiredAlignment?
对于几乎足够的对齐.
在 vkBindbufferMemory 中的一个有效使用的要求是:
in vkBindbufferMemory one of the valid usage requirements is:
memoryOffset
必须 是从调用返回的 VkMemoryRequirements
结构的 alignment
成员的整数倍vkGetBufferMemoryRequirements
和 buffer
memoryOffset
must be an integer multiple of thealignment
member of theVkMemoryRequirements
structure returned from a call tovkGetBufferMemoryRequirements
withbuffer
and there is a parallel statement in the valid usage requirements of vkBindImageMemory:
memoryOffset
必须 是从调用返回的 VkMemoryRequirements
结构的 alignment
成员的整数倍vkGetImageMemoryRequirements
和 image
memoryOffset
must be an integer multiple of thealignment
member of theVkMemoryRequirements
structure returned from a call tovkGetImageMemoryRequirements
withimage
如果前一个块包含非线性资源,而当前块是线性资源,反之亦然,则对齐要求是 VkMemoryRequirements.alignment 和设备的 bufferImageGranularity 的最大值.这也需要检查内存块的末尾.
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.
但是您还需要考虑到必须在 VkMemoryRequirements
的 memoryTypeBits
标志中设置池的内存类型.
However you also need to take into account that the memory type of the pool must be set in the memoryTypeBits
flags of VkMemoryRequirements
.
这篇关于Vulkan 内存对齐要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!