Document proper swapchain recreation practice

Make the example not leak the old swapchain handle upon recreation.
vkb::destroy_swapchain doesn't null out the old handle.
This commit is contained in:
Charles Giessen 2020-06-08 16:59:31 -06:00
parent d169f3e32d
commit bbe3971a04
5 changed files with 16 additions and 19 deletions

View File

@ -247,7 +247,9 @@ if !(swap_ret){
// If it failed to create a swapchain, the old swapchain handle is invalid. // If it failed to create a swapchain, the old swapchain handle is invalid.
vkb_swapchain.swapchain = VK_NULL_HANDLE; vkb_swapchain.swapchain = VK_NULL_HANDLE;
} }
// Note that this is the same vkb::Swapchain which was fed into vkb::SwapchainBuilder // Even though we recycled the previous swapchain, we need to free its resources.
vkb::destroy_swapchain(vkb_swapchain);
// Get the new swapchain and place it in our variable
vkb_swapchain = swap_ret.value(); vkb_swapchain = swap_ret.value();
``` ```

View File

@ -73,17 +73,12 @@ int device_initialization (Init& init) {
int create_swapchain (Init& init) { int create_swapchain (Init& init) {
vkb::SwapchainBuilder swapchain_builder{ init.device }; vkb::SwapchainBuilder swapchain_builder{ init.device };
auto swap_ret = swapchain_builder auto swap_ret = swapchain_builder.set_old_swapchain (init.swapchain).build ();
.set_old_swapchain (init.swapchain)
.build ();
static int count = 0;
count++;
std::cout << count << '\n';
if (!swap_ret) { if (!swap_ret) {
std::cout << swap_ret.error ().message () << " " << swap_ret.vk_result () << "\n"; std::cout << swap_ret.error ().message () << " " << swap_ret.vk_result () << "\n";
init.swapchain.swapchain = VK_NULL_HANDLE; return -1;
return -1;
} }
vkb::destroy_swapchain(init.swapchain);
init.swapchain = swap_ret.value (); init.swapchain = swap_ret.value ();
return 0; return 0;
} }
@ -439,13 +434,13 @@ int recreate_swapchain (Init& init, RenderData& data) {
vkDestroyCommandPool (init.device.device, data.command_pool, nullptr); vkDestroyCommandPool (init.device.device, data.command_pool, nullptr);
for (auto framebuffer : data.framebuffers) { for (auto framebuffer : data.framebuffers) {
vkDestroyFramebuffer (init.device.device, framebuffer, nullptr); vkDestroyFramebuffer (init.device.device, framebuffer, nullptr);
} }
init.swapchain.destroy_image_views (data.swapchain_image_views); init.swapchain.destroy_image_views (data.swapchain_image_views);
if (0 != create_swapchain (init)) return -1; if (0 != create_swapchain (init)) return -1;
if (0 != create_framebuffers (init, data)) return -1; if (0 != create_framebuffers (init, data)) return -1;
if (0 != create_command_pool (init, data)) return -1; if (0 != create_command_pool (init, data)) return -1;
if (0 != create_command_buffers (init, data)) return -1; if (0 != create_command_buffers (init, data)) return -1;

View File

@ -1292,10 +1292,10 @@ VkExtent2D find_extent (
} }
} // namespace detail } // namespace detail
void destroy_swapchain (Swapchain& swapchain) { void destroy_swapchain (Swapchain const& swapchain) {
if (swapchain.device != VK_NULL_HANDLE && swapchain.swapchain != VK_NULL_HANDLE) { if (swapchain.device != VK_NULL_HANDLE && swapchain.swapchain != VK_NULL_HANDLE) {
vkDestroySwapchainKHR (swapchain.device, swapchain.swapchain, swapchain.allocation_callbacks); vkDestroySwapchainKHR (swapchain.device, swapchain.swapchain, swapchain.allocation_callbacks);
swapchain.swapchain = VK_NULL_HANDLE;
} }
} }

View File

@ -528,7 +528,7 @@ struct Swapchain {
void destroy_image_views (std::vector<VkImageView> const& image_views); void destroy_image_views (std::vector<VkImageView> const& image_views);
}; };
void destroy_swapchain (Swapchain & swapchain); void destroy_swapchain (Swapchain const& swapchain);
class SwapchainBuilder { class SwapchainBuilder {
public: public:

View File

@ -15,7 +15,7 @@ GLFWwindow* create_window_glfw (const char * window_name = "", bool resize = tru
glfwWindowHint (GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint (GLFW_CLIENT_API, GLFW_NO_API);
if (!resize) glfwWindowHint (GLFW_RESIZABLE, GLFW_FALSE); if (!resize) glfwWindowHint (GLFW_RESIZABLE, GLFW_FALSE);
return glfwCreateWindow (640, 480, window_name, NULL, NULL); return glfwCreateWindow (1024,1024, window_name, NULL, NULL);
} }
void destroy_window_glfw (GLFWwindow* window) { void destroy_window_glfw (GLFWwindow* window) {
glfwDestroyWindow (window); glfwDestroyWindow (window);