[vulkan] Add function to compute next offset

When allocating memory for multiple objects that have alignment
requirements, it gets tedious keeping track of the offset and the
alignment. This is a simple function for walking the offset respecting
size and alignment requirements, and doubles as a size calculator.
This commit is contained in:
Bill Currie 2021-11-30 21:00:33 +09:00
parent 8d58dee4b1
commit 25559243ae
3 changed files with 18 additions and 3 deletions

View file

@ -45,4 +45,8 @@ VkBufferView QFV_CreateBufferView (struct qfv_device_s *device,
VkBuffer buffer, VkFormat format,
VkDeviceSize offset, VkDeviceSize size);
VkDeviceSize QFV_NextOffset (VkDeviceSize current, VkDeviceSize count,
const VkMemoryRequirements *requirements)
__attribute__((pure));
#endif//__QF_Vulkan_buffer_h

View file

@ -162,3 +162,12 @@ QFV_CreateBufferView (qfv_device_t *device, VkBuffer buffer, VkFormat format,
dfunc->vkCreateBufferView (dev, &createInfo, 0, &view);
return view;
}
VkDeviceSize
QFV_NextOffset (VkDeviceSize current, VkDeviceSize count,
const VkMemoryRequirements *requirements)
{
VkDeviceSize align = requirements->alignment - 1;
VkDeviceSize size = (requirements->size + align) & ~align;
return ((current + align) & ~align) + count * size;
}

View file

@ -327,8 +327,8 @@ Vulkan_Lighting_Init (vulkan_ctx_t *ctx)
dfunc->vkGetBufferMemoryRequirements (device->dev, lbuffers->a[0],
&requirements);
lctx->light_memory = QFV_AllocBufferMemory (device, lbuffers->a[0],
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
frames * requirements.size, 0);
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
QFV_NextOffset (0, frames, &requirements), 0);
QFV_duSetObjectName (device, VK_OBJECT_TYPE_DEVICE_MEMORY,
lctx->light_memory, "memory:lighting");
@ -359,6 +359,7 @@ Vulkan_Lighting_Init (vulkan_ctx_t *ctx)
lights);
__auto_type shadow_set = QFV_AllocateDescriptorSet (device, shadow_pool,
shadow);
VkDeviceSize light_offset = 0;
for (size_t i = 0; i < frames; i++) {
__auto_type lframe = &lctx->frames.a[i];
@ -382,7 +383,8 @@ Vulkan_Lighting_Init (vulkan_ctx_t *ctx)
lframe->light_buffer = lbuffers->a[i];
QFV_BindBufferMemory (device, lbuffers->a[i], lctx->light_memory,
i * requirements.size);
light_offset);
light_offset = QFV_NextOffset (light_offset, 1, &requirements);
QFV_duSetObjectName (device, VK_OBJECT_TYPE_COMMAND_BUFFER,
lframe->cmd, "cmd:lighting");