Overv / VulkanTutorial

Tutorial for the Vulkan graphics and compute API
https://vulkan-tutorial.com
Creative Commons Attribution Share Alike 4.0 International
3.15k stars 516 forks source link

Trying understand oldSwapChain #243

Closed liuhaowen478 closed 3 years ago

liuhaowen478 commented 3 years ago

I'm still trying to understand the oldSwapchain parameter. Actually, it would be great to see one in action. But my confusion is:

I'm guessing the idea is that we don't want to wait for the oldSwapChain to finish processing old images (they are invalid anyway) to create a new one. But in order to do that, we wouldn't be able to free old resources (they might still be in use) until we are done creating new things. In that case, what resources should we destroy and when should we be destroying them?

liuhaowen478 commented 3 years ago

Please correct me if I'm wrong but here is my current conclusion: using oldSwapChain, we still need to destroy everything.

Here is my recreateSwapChain utilizing this feature:

int width = 0, height = 0;
glfwGetFramebufferSize(window, &width, &height);
while (width == 0 || height == 0) {
    glfwGetFramebufferSize(window, &width, &height);
    glfwWaitEvents();
}

oldSwapChain = swapChain;
createSwapChain();
if (oldSwapChain != VK_NULL_HANDLE) {
    vkDestroySwapchainKHR(device, oldSwapChain, nullptr);
}

vkDeviceWaitIdle(device);
cleanupSwapChainDependents();
createImageViews();
createRenderPass();
createGraphicsPipeline();
createFramebuffers();
createUniformBuffers();
createCommandBuffers();

cleanupSwapChainDependes is exactly the same as in the tutorial except that it doesn't destroy swapChain, which is already destroyed before waitIdle.