From 9796ad78b7f3d84ba0605ab5a7c34890ce5a6e2d Mon Sep 17 00:00:00 2001 From: asuessenbach Date: Mon, 8 Dec 2025 13:53:57 +0100 Subject: [PATCH 1/2] Correctly handle vk::Result::eOutOfDate from vk::raii::SwapchainKHR::acquireNextImage and vk::raii::Queue::presentKHR --- attachments/17_swap_chain_recreation.cpp | 45 ++++++++++-------------- attachments/CMakeLists.txt | 5 ++- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/attachments/17_swap_chain_recreation.cpp b/attachments/17_swap_chain_recreation.cpp index cbe6b2dd..956d7477 100644 --- a/attachments/17_swap_chain_recreation.cpp +++ b/attachments/17_swap_chain_recreation.cpp @@ -514,13 +514,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } @@ -537,35 +542,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/CMakeLists.txt b/attachments/CMakeLists.txt index 113fe20b..1261ca30 100644 --- a/attachments/CMakeLists.txt +++ b/attachments/CMakeLists.txt @@ -14,7 +14,7 @@ endif() find_package (glfw3 REQUIRED) find_package (glm REQUIRED) -find_package (Vulkan REQUIRED) +find_package (Vulkan 1.4.335 REQUIRED) # Require Vulkan SDK version 1.4.335 or higher find_package (tinyobjloader REQUIRED) find_package (tinygltf REQUIRED) find_package (KTX REQUIRED) @@ -139,6 +139,9 @@ function (add_chapter CHAPTER_NAME) target_compile_definitions(${CHAPTER_NAME} PRIVATE USE_CPP20_MODULES=1) endif() + # Define VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS to treat VK_ERROR_OUT_OF_DATE_KHR as a success code + target_compile_definitions(${CHAPTER_NAME} PRIVATE "VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS" ) + if(WIN32) if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*") set_target_properties(${CHAPTER_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${CHAPTER_NAME}") From 15321571d8c89354bdf96a1d7cda4569ec4e9ed1 Mon Sep 17 00:00:00 2001 From: asuessenbach Date: Thu, 18 Dec 2025 10:36:29 +0100 Subject: [PATCH 2/2] Carry changes to all other chapters. --- attachments/18_vertex_input.cpp | 47 ++++++++----------- attachments/19_vertex_buffer.cpp | 45 ++++++++---------- attachments/20_staging_buffer.cpp | 45 ++++++++---------- attachments/21_index_buffer.cpp | 45 ++++++++---------- attachments/22_descriptor_layout.cpp | 45 ++++++++---------- attachments/23_descriptor_sets.cpp | 45 ++++++++---------- attachments/24_texture_image.cpp | 45 ++++++++---------- attachments/25_sampler.cpp | 45 ++++++++---------- attachments/26_texture_mapping.cpp | 45 ++++++++---------- attachments/27_depth_buffering.cpp | 45 ++++++++---------- attachments/28_model_loading.cpp | 45 ++++++++---------- attachments/29_mipmapping.cpp | 45 ++++++++---------- attachments/30_multisampling.cpp | 45 ++++++++---------- attachments/31_compute_shader.cpp | 36 +++++---------- attachments/32_ecosystem_utilities.cpp | 45 ++++++++---------- attachments/33_vulkan_profiles.cpp | 64 +++++++++++--------------- attachments/34_android.cpp | 62 +++++++++++-------------- attachments/35_gltf_ktx.cpp | 45 ++++++++---------- attachments/36_multiple_objects.cpp | 45 ++++++++---------- attachments/37_multithreading.cpp | 16 +++++-- attachments/38_ray_tracing.cpp | 14 ++++-- 21 files changed, 394 insertions(+), 520 deletions(-) diff --git a/attachments/18_vertex_input.cpp b/attachments/18_vertex_input.cpp index ae6dc48a..13499d08 100644 --- a/attachments/18_vertex_input.cpp +++ b/attachments/18_vertex_input.cpp @@ -533,13 +533,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } @@ -556,37 +561,25 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } - frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; + frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } [[nodiscard]] vk::raii::ShaderModule createShaderModule(const std::vector &code) const diff --git a/attachments/19_vertex_buffer.cpp b/attachments/19_vertex_buffer.cpp index 15989885..2d6ee716 100644 --- a/attachments/19_vertex_buffer.cpp +++ b/attachments/19_vertex_buffer.cpp @@ -569,13 +569,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } @@ -592,35 +597,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/20_staging_buffer.cpp b/attachments/20_staging_buffer.cpp index 2c53030d..a5692211 100644 --- a/attachments/20_staging_buffer.cpp +++ b/attachments/20_staging_buffer.cpp @@ -589,13 +589,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } @@ -612,35 +617,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/21_index_buffer.cpp b/attachments/21_index_buffer.cpp index 0b8f65ad..0a1f204c 100644 --- a/attachments/21_index_buffer.cpp +++ b/attachments/21_index_buffer.cpp @@ -615,13 +615,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } @@ -638,35 +643,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/22_descriptor_layout.cpp b/attachments/22_descriptor_layout.cpp index f5fd719f..d3a036f0 100644 --- a/attachments/22_descriptor_layout.cpp +++ b/attachments/22_descriptor_layout.cpp @@ -674,13 +674,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -698,35 +703,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/23_descriptor_sets.cpp b/attachments/23_descriptor_sets.cpp index 86941754..42398448 100644 --- a/attachments/23_descriptor_sets.cpp +++ b/attachments/23_descriptor_sets.cpp @@ -702,13 +702,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -726,35 +731,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/24_texture_image.cpp b/attachments/24_texture_image.cpp index 7816c45b..75beff7a 100644 --- a/attachments/24_texture_image.cpp +++ b/attachments/24_texture_image.cpp @@ -811,13 +811,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -835,35 +840,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/25_sampler.cpp b/attachments/25_sampler.cpp index eee63145..087d5638 100644 --- a/attachments/25_sampler.cpp +++ b/attachments/25_sampler.cpp @@ -847,13 +847,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -871,35 +876,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/26_texture_mapping.cpp b/attachments/26_texture_mapping.cpp index d51cc788..34ba6a1c 100644 --- a/attachments/26_texture_mapping.cpp +++ b/attachments/26_texture_mapping.cpp @@ -912,13 +912,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -936,35 +941,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/27_depth_buffering.cpp b/attachments/27_depth_buffering.cpp index b7d60670..5fe024fc 100644 --- a/attachments/27_depth_buffering.cpp +++ b/attachments/27_depth_buffering.cpp @@ -1013,13 +1013,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -1037,35 +1042,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/28_model_loading.cpp b/attachments/28_model_loading.cpp index 448ac1c8..6a2bae30 100644 --- a/attachments/28_model_loading.cpp +++ b/attachments/28_model_loading.cpp @@ -1069,13 +1069,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -1093,35 +1098,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/29_mipmapping.cpp b/attachments/29_mipmapping.cpp index d796e68a..76e6b173 100644 --- a/attachments/29_mipmapping.cpp +++ b/attachments/29_mipmapping.cpp @@ -1139,13 +1139,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -1163,35 +1168,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/30_multisampling.cpp b/attachments/30_multisampling.cpp index 7ee65259..68526539 100644 --- a/attachments/30_multisampling.cpp +++ b/attachments/30_multisampling.cpp @@ -1202,13 +1202,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -1226,35 +1231,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/31_compute_shader.cpp b/attachments/31_compute_shader.cpp index 6b38a90a..75f08643 100644 --- a/attachments/31_compute_shader.cpp +++ b/attachments/31_compute_shader.cpp @@ -413,7 +413,7 @@ class ComputeShaderApplication .format = swapChainSurfaceFormat.format, .components = {vk::ComponentSwizzle::eIdentity, vk::ComponentSwizzle::eIdentity, vk::ComponentSwizzle::eIdentity, vk::ComponentSwizzle::eIdentity}, .subresourceRange = {vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1}}; - for (auto& image : swapChainImages) + for (auto &image : swapChainImages) { imageViewCreateInfo.image = image; swapChainImageViews.emplace_back(device, imageViewCreateInfo); @@ -811,7 +811,7 @@ class ComputeShaderApplication void drawFrame() { auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, nullptr, *inFlightFences[frameIndex]); - auto fenceResult = device.waitForFences(*inFlightFences[frameIndex], vk::True, UINT64_MAX); + auto fenceResult = device.waitForFences(*inFlightFences[frameIndex], vk::True, UINT64_MAX); if (fenceResult != vk::Result::eSuccess) { throw std::runtime_error("failed to wait for fence!"); @@ -884,7 +884,7 @@ class ComputeShaderApplication if (result != vk::Result::eSuccess) { throw std::runtime_error("failed to wait for semaphore!"); - } + } vk::PresentInfoKHR presentInfo{ .waitSemaphoreCount = 0, // No binary semaphores needed @@ -893,30 +893,18 @@ class ComputeShaderApplication .pSwapchains = &*swapChain, .pImageIndices = &imageIndex}; - try + result = queue.presentKHR(presentInfo); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - result = queue.presentKHR(presentInfo); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; diff --git a/attachments/32_ecosystem_utilities.cpp b/attachments/32_ecosystem_utilities.cpp index 22a67e5b..849326a6 100644 --- a/attachments/32_ecosystem_utilities.cpp +++ b/attachments/32_ecosystem_utilities.cpp @@ -1593,13 +1593,18 @@ class HelloTriangleApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -1656,35 +1661,23 @@ class HelloTriangleApplication queue.submit(submitInfo, *inFlightFences[frameIndex]); } - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/33_vulkan_profiles.cpp b/attachments/33_vulkan_profiles.cpp index a0627897..d8bc9a5f 100644 --- a/attachments/33_vulkan_profiles.cpp +++ b/attachments/33_vulkan_profiles.cpp @@ -1554,23 +1554,28 @@ class HelloTriangleApplication void drawFrame() { - vk::Result result = device.waitForFences({*inFlightFences[frameIndex]}, VK_TRUE, UINT64_MAX); - if (result != vk::Result::eSuccess) + vk::Result fenceResult = device.waitForFences({*inFlightFences[frameIndex]}, VK_TRUE, UINT64_MAX); + if (fenceResult != vk::Result::eSuccess) { throw std::runtime_error("failed to wait for fence!"); } - uint32_t imageIndex; - try - { - auto [result, idx] = swapChain.acquireNextImage(UINT64_MAX, *imageAvailableSemaphores[frameIndex]); - imageIndex = idx; - } - catch (vk::OutOfDateKHRError &) + auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *imageAvailableSemaphores[frameIndex], nullptr); + + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. + if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) + { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); + throw std::runtime_error("failed to acquire swap chain image!"); + } updateUniformBuffer(frameIndex); @@ -1590,36 +1595,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*presentCompleteSemaphore[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*presentCompleteSemaphore[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{ - .waitSemaphoreCount = 1, - .pWaitSemaphores = &*presentCompleteSemaphore[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - auto result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/34_android.cpp b/attachments/34_android.cpp index ce26e3ca..89a73ae7 100644 --- a/attachments/34_android.cpp +++ b/attachments/34_android.cpp @@ -1317,17 +1317,22 @@ class HelloTriangleApplication { static_cast(device.waitForFences({*inFlightFences[frameIndex]}, VK_TRUE, UINT64_MAX)); - uint32_t imageIndex; - try - { - auto [result, idx] = swapChain.acquireNextImage(UINT64_MAX, *imageAvailableSemaphores[frameIndex]); - imageIndex = idx; - } - catch (vk::OutOfDateKHRError &) + auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *imageAvailableSemaphores[frameIndex], nullptr); + + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. + if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) + { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); + throw std::runtime_error("failed to acquire swap chain image!"); + } // Update uniform buffer with current transformation updateUniformBuffer(frameIndex); @@ -1348,38 +1353,23 @@ class HelloTriangleApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{ - .waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - - vk::Result result = queue.presentKHR(presentInfoKHR); - - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("Failed to present swap chain image"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; diff --git a/attachments/35_gltf_ktx.cpp b/attachments/35_gltf_ktx.cpp index 48b68d2c..d4441e0a 100644 --- a/attachments/35_gltf_ktx.cpp +++ b/attachments/35_gltf_ktx.cpp @@ -1402,13 +1402,18 @@ class VulkanApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -1426,35 +1431,23 @@ class VulkanApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/36_multiple_objects.cpp b/attachments/36_multiple_objects.cpp index f0a62176..b35ef44e 100644 --- a/attachments/36_multiple_objects.cpp +++ b/attachments/36_multiple_objects.cpp @@ -1535,13 +1535,18 @@ class VulkanApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } @@ -1561,35 +1566,23 @@ class VulkanApplication .pSignalSemaphores = &*renderFinishedSemaphores[imageIndex]}; queue.submit(submitInfo, *inFlightFences[frameIndex]); - try + const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, + .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], + .swapchainCount = 1, + .pSwapchains = &*swapChain, + .pImageIndices = &imageIndex}; + result = queue.presentKHR(presentInfoKHR); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { - const vk::PresentInfoKHR presentInfoKHR{.waitSemaphoreCount = 1, - .pWaitSemaphores = &*renderFinishedSemaphores[imageIndex], - .swapchainCount = 1, - .pSwapchains = &*swapChain, - .pImageIndices = &imageIndex}; - result = queue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) - { - framebufferResized = false; - recreateSwapChain(); - } - else if (result != vk::Result::eSuccess) - { - throw std::runtime_error("failed to present swap chain image!"); - } + framebufferResized = false; + recreateSwapChain(); } - catch (const vk::SystemError &e) + else { - if (e.code().value() == static_cast(vk::Result::eErrorOutOfDateKHR)) - { - recreateSwapChain(); - return; - } - else - { - throw; - } + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; } diff --git a/attachments/37_multithreading.cpp b/attachments/37_multithreading.cpp index 90c60ff5..3ea2d09c 100644 --- a/attachments/37_multithreading.cpp +++ b/attachments/37_multithreading.cpp @@ -1201,13 +1201,19 @@ class MultithreadedApplication // Acquire the next image auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *imageAvailableSemaphores[frameIndex], nullptr); + + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. else if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } @@ -1326,15 +1332,17 @@ class MultithreadedApplication .pImageIndices = &imageIndex}; result = queue.presentKHR(presentInfo); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { framebufferResized = false; recreateSwapChain(); - return; } - else if (result != vk::Result::eSuccess) + else { - throw std::runtime_error("failed to present swap chain image!"); + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } // Move to the next frame diff --git a/attachments/38_ray_tracing.cpp b/attachments/38_ray_tracing.cpp index 112e436c..170cbefa 100644 --- a/attachments/38_ray_tracing.cpp +++ b/attachments/38_ray_tracing.cpp @@ -1832,13 +1832,18 @@ class VulkanRaytracingApplication auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. if (result == vk::Result::eErrorOutOfDateKHR) { recreateSwapChain(); return; } + // On other success codes than eSuccess and eSuboptimalKHR we just throw an exception. + // On any error code, aquireNextImage already threw an exception. if (result != vk::Result::eSuccess && result != vk::Result::eSuboptimalKHR) { + assert(result == vk::Result::eTimeout || result == vk::Result::eNotReady); throw std::runtime_error("failed to acquire swap chain image!"); } updateUniformBuffer(frameIndex); @@ -1866,14 +1871,17 @@ class VulkanRaytracingApplication .pSwapchains = &*swapChain, .pImageIndices = &imageIndex}; result = presentQueue.presentKHR(presentInfoKHR); - if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR || framebufferResized) + // Due to VULKAN_HPP_HANDLE_ERROR_OUT_OF_DATE_AS_SUCCESS being defined, eErrorOutOfDateKHR can be checked as a result + // here and does not need to be caught by an exception. + if ((result == vk::Result::eSuboptimalKHR) || (result == vk::Result::eErrorOutOfDateKHR) || framebufferResized) { framebufferResized = false; recreateSwapChain(); } - else if (result != vk::Result::eSuccess) + else { - throw std::runtime_error("failed to present swap chain image!"); + // There are no other success codes than eSuccess; on any error code, presentKHR already threw an exception. + assert(result == vk::Result::eSuccess); } frameIndex = (frameIndex + 1) % MAX_FRAMES_IN_FLIGHT; }