diff --git a/VulkanTest b/VulkanTest index c54f8a3..c0d2514 100755 Binary files a/VulkanTest and b/VulkanTest differ diff --git a/build/cve_renderer.o b/build/cve_renderer.o index dc26fb8..f25da6b 100644 Binary files a/build/cve_renderer.o and b/build/cve_renderer.o differ diff --git a/build/cve_swap_chain.o b/build/cve_swap_chain.o index f1f3b72..a34cc4d 100644 Binary files a/build/cve_swap_chain.o and b/build/cve_swap_chain.o differ diff --git a/build/first_app.o b/build/first_app.o index 93acf03..152d1bd 100644 Binary files a/build/first_app.o and b/build/first_app.o differ diff --git a/cve_renderer.cpp b/cve_renderer.cpp index 166d72d..66b109e 100644 --- a/cve_renderer.cpp +++ b/cve_renderer.cpp @@ -25,10 +25,11 @@ namespace cve { if (cveSwapChain == nullptr) { cveSwapChain = std::make_unique(cveDevice, extent); } else { - cveSwapChain = std::make_unique(cveDevice, extent, std::move(cveSwapChain)); - if (cveSwapChain->imageCount() != commandBuffers.size()) { - freeCommandBuffers(); - createCommandBuffers(); + std::shared_ptr oldSwapChain = std::move(cveSwapChain); + cveSwapChain = std::make_unique(cveDevice, extent, oldSwapChain); + + if (!oldSwapChain->compareSwapFormats(*cveSwapChain.get())) { + throw std::runtime_error("Swap chain image format/depth has changed."); } } @@ -36,7 +37,7 @@ namespace cve { } void CveRenderer::createCommandBuffers() { - commandBuffers.resize(cveSwapChain->imageCount()); + commandBuffers.resize(CveSwapChain::MAX_FRAMES_IN_FLIGHT); VkCommandBufferAllocateInfo allocInfo{}; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; @@ -99,6 +100,8 @@ namespace cve { } isFrameStarted = false; + + currentFrameIndex = (currentFrameIndex + 1) % CveSwapChain::MAX_FRAMES_IN_FLIGHT; } void CveRenderer::beginSwapChainRenderPass(VkCommandBuffer commandBuffer) { diff --git a/cve_renderer.hpp b/cve_renderer.hpp index 5a3e0d0..8cf1923 100644 --- a/cve_renderer.hpp +++ b/cve_renderer.hpp @@ -23,7 +23,12 @@ namespace cve { VkCommandBuffer getCurrentCommandBuffer() const { assert(isFrameStarted && "Cannot get command buffer when frame is not in progress"); - return commandBuffers[currentImageIndex]; + return commandBuffers[currentFrameIndex]; + } + + int getFrameIndex() const { + assert(isFrameStarted && "Cannot get frame index when frame is not started"); + return currentFrameIndex; } VkCommandBuffer beginFrame(); @@ -41,7 +46,8 @@ namespace cve { std::unique_ptr cveSwapChain; std::vector commandBuffers; - uint32_t currentImageIndex; + uint32_t currentImageIndex = 0; + int currentFrameIndex = 0; bool isFrameStarted = false; }; } diff --git a/cve_swap_chain.cpp b/cve_swap_chain.cpp index ec5f3ea..069748f 100644 --- a/cve_swap_chain.cpp +++ b/cve_swap_chain.cpp @@ -301,6 +301,7 @@ void CveSwapChain::createFramebuffers() { void CveSwapChain::createDepthResources() { VkFormat depthFormat = findDepthFormat(); + swapChainDepthFormat = depthFormat; VkExtent2D swapChainExtent = getSwapChainExtent(); depthImages.resize(imageCount()); diff --git a/cve_swap_chain.hpp b/cve_swap_chain.hpp index 346b530..df165d8 100644 --- a/cve_swap_chain.hpp +++ b/cve_swap_chain.hpp @@ -40,6 +40,11 @@ class CveSwapChain { VkResult acquireNextImage(uint32_t *imageIndex); VkResult submitCommandBuffers(const VkCommandBuffer *buffers, uint32_t *imageIndex); + bool compareSwapFormats(const CveSwapChain& swapChain) const { + return swapChain.swapChainDepthFormat == swapChainDepthFormat && + swapChain.swapChainImageFormat == swapChainImageFormat; + }; + private: void init(); void createSwapChain(); @@ -57,6 +62,7 @@ class CveSwapChain { VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities); VkFormat swapChainImageFormat; + VkFormat swapChainDepthFormat; VkExtent2D swapChainExtent; std::vector swapChainFramebuffers;