From 6185c081c1a6ec13b54eab6a12ff72814cf3addb Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Tue, 1 Oct 2024 17:08:41 -0700 Subject: Fix vulkan validation error --- src/vulkan_swapchain.cpp | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) (limited to 'src/vulkan_swapchain.cpp') diff --git a/src/vulkan_swapchain.cpp b/src/vulkan_swapchain.cpp index 7916a3b..21948c1 100644 --- a/src/vulkan_swapchain.cpp +++ b/src/vulkan_swapchain.cpp @@ -8,8 +8,8 @@ #include #include +#include #include -#include #include #include @@ -77,8 +77,7 @@ void Swapchain::resize(uint32_t new_width, uint32_t new_height) { &image_count, nullptr)); if (image_count > SWAPCHAIN_IMAGE_COUNT) { - // TODO throw an exception - std::cerr << "Swapchain image count is greater than expected" << std::endl; + spdlog::critical("Swapchain image count is greater than expected"); abort(); } CHECK_VULKAN(vkGetSwapchainImagesKHR( @@ -134,6 +133,9 @@ void Swapchain::resize(uint32_t new_width, uint32_t new_height) { &framebuffers[i])); } + if (upload_texture) { + upload_texture->release(); + } upload_texture = device.create_texture( {width, height}, VK_FORMAT_B8G8R8A8_UNORM, @@ -155,8 +157,7 @@ Swapchain::Swapchain(GLFWwindow *window, , cmd_buf(device.create_async_command_buffer()) { if (!glfwVulkanSupported()) { - std::cerr << "GLFW failed to find Vulkan support" << std::endl; - // TODO throw an exception + spdlog::critical("GLFW failed to find Vulkan support"); abort(); } @@ -176,8 +177,7 @@ Swapchain::Swapchain(GLFWwindow *window, surface, &supported)); if (supported != VK_TRUE) { - // TODO throw an exception - std::cerr << "Surface does not support presentation" << std::endl; + spdlog::critical("Surface does not support presentation"); abort(); } @@ -188,8 +188,7 @@ Swapchain::Swapchain(GLFWwindow *window, &surface_capabilities)); if (surface_capabilities.maxImageCount < SWAPCHAIN_IMAGE_COUNT) { - // TODO throw an exception - std::cerr << "Surface does not support enough images" << std::endl; + spdlog::critical("Surface does not support enough images"); abort(); } } @@ -280,6 +279,7 @@ Swapchain::Swapchain(GLFWwindow *window, }; VkDescriptorPoolCreateInfo pool_info = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, + .flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, .maxSets = 1, .poolSizeCount = 1, .pPoolSizes = &pool_size, @@ -317,11 +317,17 @@ Swapchain::Swapchain(GLFWwindow *window, } void Swapchain::start_frame() { + spdlog::trace("Starting frame {}", frame_index); VkCommandBufferBeginInfo begin_info = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, }; + + if (vkGetFenceStatus(device.device, cmd_buf.fence) == VK_NOT_READY) { + vkWaitForFences(device.device, 1, &cmd_buf.fence, VK_TRUE, 2e6); + } CHECK_VULKAN(vkBeginCommandBuffer(cmd_buf.buffer, &begin_info)); + vkResetFences(device.device, 1, &cmd_buf.fence); } void Swapchain::display(Texture2D& texture) { @@ -331,7 +337,7 @@ void Swapchain::display(Texture2D& texture) { UINT64_MAX, image_available_semaphores[semaphore_index], VK_NULL_HANDLE, - &frame_index); + &semaphore_index); switch (present_result) { case VK_ERROR_OUT_OF_DATE_KHR: case VK_SUBOPTIMAL_KHR: @@ -355,7 +361,7 @@ void Swapchain::display(Texture2D& texture) { .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .image = swapchain_images[frame_index], + .image = swapchain_images[semaphore_index], .subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .levelCount = 1, @@ -417,7 +423,7 @@ void Swapchain::display(Texture2D& texture) { cmd_buf.buffer, texture->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, - swapchain_images[frame_index], + swapchain_images[semaphore_index], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, @@ -456,7 +462,7 @@ void Swapchain::display(Texture2D& texture) { VkRenderPassBeginInfo render_pass_begin_info = { .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, .renderPass = render_pass, - .framebuffer = framebuffers[frame_index], + .framebuffer = framebuffers[semaphore_index], .renderArea = { .offset = {0, 0}, .extent = {width, height}, @@ -488,7 +494,7 @@ void Swapchain::display(Texture2D& texture) { cmd_buf.queue, 1, &submit_info, - VK_NULL_HANDLE)); + cmd_buf.fence)); } // Present the image to the swapchain @@ -498,7 +504,7 @@ void Swapchain::display(Texture2D& texture) { .pWaitSemaphores = &render_finished_semaphores[semaphore_index], .swapchainCount = 1, .pSwapchains = &swapchain, - .pImageIndices = &frame_index, + .pImageIndices = &semaphore_index, }; present_result = vkQueuePresentKHR(device.graphics_queue, &present_info); @@ -514,10 +520,12 @@ void Swapchain::display(Texture2D& texture) { // Rotate the semaphore index semaphore_index = (semaphore_index + 1) % SWAPCHAIN_IMAGE_COUNT; + frame_index += 1; } -Swapchain::~Swapchain() { - ImGui_ImplVulkan_Shutdown(); +void Swapchain::release() { + upload_texture->release(); + cmd_buf.destroy(); for (uint32_t i = 0; i < SWAPCHAIN_IMAGE_COUNT; i++) { vkDestroyFramebuffer(device.device, framebuffers[i], nullptr); @@ -530,8 +538,6 @@ Swapchain::~Swapchain() { vkDestroySwapchainKHR(device.device, swapchain, nullptr); vkDestroyRenderPass(device.device, render_pass, nullptr); vkDestroySurfaceKHR(device.instance, surface, nullptr); - - upload_texture.reset(); } } // namespace iris \ No newline at end of file -- cgit v1.2.3-70-g09d2