From 4bb8fd5ac5c07e8756ae097e7f5d7f34697bc914 Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Tue, 1 Oct 2024 17:54:43 -0700 Subject: fix command buffer release issue --- src/vulkan_helper.cpp | 66 +++++++++--------------------------------------- src/vulkan_helper.h | 13 ---------- src/vulkan_swapchain.cpp | 2 +- src/vulkan_swapchain.h | 2 +- 4 files changed, 14 insertions(+), 69 deletions(-) diff --git a/src/vulkan_helper.cpp b/src/vulkan_helper.cpp index f39d867..567d414 100644 --- a/src/vulkan_helper.cpp +++ b/src/vulkan_helper.cpp @@ -278,69 +278,27 @@ CommandBuffer::CommandBuffer(VkDevice device, VK_NULL_HANDLE, &fence)); spdlog::debug("Created command buffer: 0x{:x}", (uint64_t)buffer); + spdlog::debug("Created fence: 0x{:x}", (uint64_t)fence); } -AsyncCommandBuffer::AsyncCommandBuffer(VkDevice device, - uint32_t queue_family_index, - VkQueue queue) - : device(device), queue(queue) -{ - VkCommandPoolCreateInfo pool_info = { - .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, - .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, - .queueFamilyIndex = queue_family_index, // TODO: query capabilities to find a proper queue index - }; - CHECK_VULKAN(vkCreateCommandPool( - device, - &pool_info, - VK_NULL_HANDLE, - &this->pool)); - - VkCommandBufferAllocateInfo buffer_info = { - .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, - .commandPool = this->pool, - .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, - .commandBufferCount = 1, - }; - CHECK_VULKAN(vkAllocateCommandBuffers( - device, - &buffer_info, - &buffer)); - - VkFenceCreateInfo fence_info = { - .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, - }; - CHECK_VULKAN(vkCreateFence( - device, - &fence_info, - VK_NULL_HANDLE, - &fence)); - spdlog::debug("Created async command buffer: 0x{:x}", (uint64_t)buffer); -} - -void AsyncCommandBuffer::destroy() { - vkDestroyFence(device, fence, VK_NULL_HANDLE); - vkFreeCommandBuffers(device, pool, 1, &buffer); - vkDestroyCommandPool(device, pool, VK_NULL_HANDLE); - spdlog::debug("Destroyed async command buffer: 0x{:x}", (uint64_t)buffer); -} - - void CommandBuffer::destroy() { - vkDestroyFence(device, fence, VK_NULL_HANDLE); - vkFreeCommandBuffers(device, pool, 1, &buffer); - vkDestroyCommandPool(device, pool, VK_NULL_HANDLE); - spdlog::debug("Destroyed command buffer: 0x{:x}", (uint64_t)buffer); + if (fence != VK_NULL_HANDLE) { + vkDestroyFence(device, fence, VK_NULL_HANDLE); + vkFreeCommandBuffers(device, pool, 1, &buffer); + vkDestroyCommandPool(device, pool, VK_NULL_HANDLE); + + spdlog::debug("Destroyed fence: 0x{:x}", (uint64_t)fence); + spdlog::debug("Destroyed command buffer: 0x{:x}", (uint64_t)buffer); + fence = VK_NULL_HANDLE; + buffer = VK_NULL_HANDLE; + pool = VK_NULL_HANDLE; + } } CommandBuffer Device::create_command_buffer() { return CommandBuffer(device, main_queue_family_index, graphics_queue); } -AsyncCommandBuffer Device::create_async_command_buffer() { - return AsyncCommandBuffer(device, main_queue_family_index, graphics_queue); -} - void CommandBuffer::begin(VkCommandBufferUsageFlags flags) { CHECK_VULKAN(vkResetFences(device, 1, &fence)); CHECK_VULKAN(vkResetCommandPool(device, pool, 0u)); diff --git a/src/vulkan_helper.h b/src/vulkan_helper.h index d8c9c32..6dbf0b7 100644 --- a/src/vulkan_helper.h +++ b/src/vulkan_helper.h @@ -69,18 +69,6 @@ struct CommandBuffer { ~CommandBuffer() { destroy(); } }; -struct AsyncCommandBuffer { - VkDevice device; - VkCommandPool pool; - VkCommandBuffer buffer; - VkFence fence; - VkQueue queue; - - AsyncCommandBuffer(VkDevice device, uint32_t queue_family_index, VkQueue queue); - void destroy(); - ~AsyncCommandBuffer() { destroy(); } -}; - struct Device { VkInstance instance; VkPhysicalDevice physical_device; @@ -123,7 +111,6 @@ struct Device { }); CommandBuffer create_command_buffer(); - AsyncCommandBuffer create_async_command_buffer(); }; } // namespace iris \ No newline at end of file diff --git a/src/vulkan_swapchain.cpp b/src/vulkan_swapchain.cpp index 21948c1..c38e9f6 100644 --- a/src/vulkan_swapchain.cpp +++ b/src/vulkan_swapchain.cpp @@ -154,7 +154,7 @@ Swapchain::Swapchain(GLFWwindow *window, , window(window) , width(width) , height(height) - , cmd_buf(device.create_async_command_buffer()) + , cmd_buf(device.create_command_buffer()) { if (!glfwVulkanSupported()) { spdlog::critical("GLFW failed to find Vulkan support"); diff --git a/src/vulkan_swapchain.h b/src/vulkan_swapchain.h index 1cd270c..381c5b0 100644 --- a/src/vulkan_swapchain.h +++ b/src/vulkan_swapchain.h @@ -28,7 +28,7 @@ struct Swapchain { VkFramebuffer framebuffers[SWAPCHAIN_IMAGE_COUNT]; VkSemaphore image_available_semaphores[SWAPCHAIN_IMAGE_COUNT]; VkSemaphore render_finished_semaphores[SWAPCHAIN_IMAGE_COUNT]; - AsyncCommandBuffer cmd_buf; + CommandBuffer cmd_buf; Texture2D upload_texture; -- cgit v1.2.3-70-g09d2