diff --git a/base/VulkanSwapChain.hpp b/base/VulkanSwapChain.hpp index c73adb20..324e1b19 100644 --- a/base/VulkanSwapChain.hpp +++ b/base/VulkanSwapChain.hpp @@ -120,7 +120,7 @@ public: #endif ) { - VkResult err; + VkResult err = VK_SUCCESS; // Create the os-specific surface #ifdef _WIN32 @@ -156,6 +156,10 @@ public: #endif #endif + if (err != VK_SUCCESS) { + vks::tools::exitFatal("Could not create surface!", "Fatal error"); + } + // Get available queue family properties uint32_t queueCount; vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueCount, NULL); @@ -224,13 +228,11 @@ public: // Get list of supported surface formats uint32_t formatCount; - err = fpGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, NULL); - assert(!err); + VK_CHECK_RESULT(fpGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, NULL)); assert(formatCount > 0); std::vector surfaceFormats(formatCount); - err = fpGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, surfaceFormats.data()); - assert(!err); + VK_CHECK_RESULT(fpGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, surfaceFormats.data())); // If the surface format list only includes one entry with VK_FORMAT_UNDEFINED, // there is no preferered format, so we assume VK_FORMAT_B8G8R8A8_UNORM @@ -299,24 +301,19 @@ public: */ void create(uint32_t *width, uint32_t *height, bool vsync = false) { - VkResult err; VkSwapchainKHR oldSwapchain = swapChain; // Get physical device surface properties and formats VkSurfaceCapabilitiesKHR surfCaps; - err = fpGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &surfCaps); - assert(!err); + VK_CHECK_RESULT(fpGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &surfCaps)); // Get available present modes uint32_t presentModeCount; - err = fpGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, NULL); - assert(!err); + VK_CHECK_RESULT(fpGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, NULL)); assert(presentModeCount > 0); std::vector presentModes(presentModeCount); - - err = fpGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, presentModes.data()); - assert(!err); + VK_CHECK_RESULT(fpGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, presentModes.data())); VkExtent2D swapchainExtent = {}; // If width (and height) equals the special value 0xFFFFFFFF, the size of the surface will be set by the swapchain @@ -422,8 +419,7 @@ public: swapchainCI.imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; } - err = fpCreateSwapchainKHR(device, &swapchainCI, nullptr, &swapChain); - assert(!err); + VK_CHECK_RESULT(fpCreateSwapchainKHR(device, &swapchainCI, nullptr, &swapChain)); // If an existing swap chain is re-created, destroy the old swap chain // This also cleans up all the presentable images @@ -435,14 +431,11 @@ public: } fpDestroySwapchainKHR(device, oldSwapchain, nullptr); } - - err = fpGetSwapchainImagesKHR(device, swapChain, &imageCount, NULL); - assert(!err); + VK_CHECK_RESULT(fpGetSwapchainImagesKHR(device, swapChain, &imageCount, NULL)); // Get the swap chain images images.resize(imageCount); - err = fpGetSwapchainImagesKHR(device, swapChain, &imageCount, images.data()); - assert(!err); + VK_CHECK_RESULT(fpGetSwapchainImagesKHR(device, swapChain, &imageCount, images.data())); // Get the swap chain buffers containing the image and imageview buffers.resize(imageCount); @@ -470,8 +463,7 @@ public: colorAttachmentView.image = buffers[i].image; - err = vkCreateImageView(device, &colorAttachmentView, nullptr, &buffers[i].view); - assert(!err); + VK_CHECK_RESULT(vkCreateImageView(device, &colorAttachmentView, nullptr, &buffers[i].view)); } } diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index 5c786bd1..d9234a1f 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -552,8 +552,15 @@ void VulkanExampleBase::getOverlayText(VulkanTextOverlay *textOverlay) void VulkanExampleBase::prepareFrame() { - // Acquire the next image from the swap chaing - VK_CHECK_RESULT(swapChain.acquireNextImage(semaphores.presentComplete, ¤tBuffer)); + // Acquire the next image from the swap chain + VkResult err = swapChain.acquireNextImage(semaphores.presentComplete, ¤tBuffer); + // Recreate the swapchain if it's no longer compatible with the surface (OUT_OF_DATE) or no longer optimal for presentation (SUBOPTIMAL) + if ((err == VK_ERROR_OUT_OF_DATE_KHR) || (err == VK_SUBOPTIMAL_KHR)) { + windowResize(); + } + else { + VK_CHECK_RESULT(err); + } } void VulkanExampleBase::submitFrame()