From 15e74a9a178d12f074df8252993320383e24cdd6 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Tue, 24 Sep 2024 12:48:54 +0200 Subject: [PATCH] move command buffer allocation to separate module --- src/ptk_vk/command_buffers.c | 25 +++++++++++++++++++++++++ src/ptk_vk/command_buffers.h | 17 +++++++++++++++++ src/ptk_vk/init.c | 27 +++++---------------------- src/ptk_vk/init.h | 3 ++- 4 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 src/ptk_vk/command_buffers.c create mode 100644 src/ptk_vk/command_buffers.h diff --git a/src/ptk_vk/command_buffers.c b/src/ptk_vk/command_buffers.c new file mode 100644 index 0000000..9680f7d --- /dev/null +++ b/src/ptk_vk/command_buffers.c @@ -0,0 +1,25 @@ +#include + +#include +#include + +PTK_OPTION(VkCommandBufferList) vk_allocate_command_buffers(VkDevice dev, VkCommandPool command_pool) { + VkCommandBufferList ret = PTK_LIST_NEW(VkCommandBuffer, g_max_frames_in_flight); + + VK_TRY(PTK_OPTION_NONE(VkCommandBufferList), + vkAllocateCommandBuffers( + dev, + &(VkCommandBufferAllocateInfo){ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .pNext = NULL, + .commandPool = command_pool, + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = ret.allocated, + }, + ret.data + ) + ); + PTK_LIST_FILLED(ret); + + return PTK_OPTION_SOME(VkCommandBufferList, ret); +} diff --git a/src/ptk_vk/command_buffers.h b/src/ptk_vk/command_buffers.h new file mode 100644 index 0000000..c9236d6 --- /dev/null +++ b/src/ptk_vk/command_buffers.h @@ -0,0 +1,17 @@ +// Copyright (jacekpoz 2024). Licensed under the EUPL-1.2 or later. + +#ifndef PTK_PTK_VK_COMMAND_BUFFERS_H_ +#define PTK_PTK_VK_COMMAND_BUFFERS_H_ + +#include + +#include +#include + +PTK_LIST_DEFINE(VkCommandBuffer); +typedef PTK_LIST(VkCommandBuffer) VkCommandBufferList; +PTK_OPTION_DEFINE(VkCommandBufferList); + +PTK_OPTION(VkCommandBufferList) vk_allocate_command_buffers(VkDevice dev, VkCommandPool command_pool); + +#endif // PTK_PTK_VK_COMMAND_BUFFERS_H_ diff --git a/src/ptk_vk/init.c b/src/ptk_vk/init.c index 935ffc9..fe23ef8 100644 --- a/src/ptk_vk/init.c +++ b/src/ptk_vk/init.c @@ -100,27 +100,6 @@ bool vk_transfer_vertex_data(void) { return true; } -bool allocate_command_buffers(void) { - g_command_buffers = PTK_LIST_NEW(VkCommandBuffer, g_max_frames_in_flight); - - VK_TRY(false, - vkAllocateCommandBuffers( - g_dev, - &(VkCommandBufferAllocateInfo){ - .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, - .pNext = NULL, - .commandPool = m_command_pool, - .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, - .commandBufferCount = g_command_buffers.allocated, - }, - g_command_buffers.data - ) - ); - PTK_LIST_FILLED(g_command_buffers); - - return true; -} - bool vk_record_command_buffer(const VkCommandBuffer command_buffer, const uint32_t image_index) { VK_TRY(false, vkBeginCommandBuffer( @@ -375,11 +354,15 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const m_descriptor_sets = descriptor_sets_opt.value; - if (!allocate_command_buffers()) { + PTK_OPTION(VkCommandBufferList) command_buffers_opt = vk_allocate_command_buffers(g_dev, m_command_pool); + + if (!command_buffers_opt.exists) { PTK_ERR("failed allocating command buffers"); return false; } + g_command_buffers = command_buffers_opt.value; + if (!create_sync_objects()) { PTK_ERR("failed creating sync objects"); return false; diff --git a/src/ptk_vk/init.h b/src/ptk_vk/init.h index 1b167f9..c9f5d2c 100644 --- a/src/ptk_vk/init.h +++ b/src/ptk_vk/init.h @@ -10,7 +10,8 @@ #include #include -PTK_LIST_DEFINE(VkCommandBuffer); +#include + PTK_LIST_DEFINE(VkSemaphore); PTK_LIST_DEFINE(VkFence);