From 25559243ae09db2f278cff24bb6288c453468149 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 30 Nov 2021 21:00:33 +0900 Subject: [PATCH] [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. --- include/QF/Vulkan/buffer.h | 4 ++++ libs/video/renderer/vulkan/buffer.c | 9 +++++++++ libs/video/renderer/vulkan/vulkan_lighting.c | 8 +++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/include/QF/Vulkan/buffer.h b/include/QF/Vulkan/buffer.h index 712b14a96..f9cbbb0c9 100644 --- a/include/QF/Vulkan/buffer.h +++ b/include/QF/Vulkan/buffer.h @@ -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 diff --git a/libs/video/renderer/vulkan/buffer.c b/libs/video/renderer/vulkan/buffer.c index eb9da832c..063fe8c73 100644 --- a/libs/video/renderer/vulkan/buffer.c +++ b/libs/video/renderer/vulkan/buffer.c @@ -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; +} diff --git a/libs/video/renderer/vulkan/vulkan_lighting.c b/libs/video/renderer/vulkan/vulkan_lighting.c index ce13b7bed..755e7d919 100644 --- a/libs/video/renderer/vulkan/vulkan_lighting.c +++ b/libs/video/renderer/vulkan/vulkan_lighting.c @@ -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");