diff --git a/except.hpp b/except.hpp index ccff147..a4c0367 100644 --- a/except.hpp +++ b/except.hpp @@ -21,12 +21,46 @@ #include "./vk.hpp" #include +#include +#include + namespace cruft::vk { class error : public std::exception { public: static void try_code (VkResult); static void throw_code [[noreturn]] (VkResult); + + + /// invokes a supplied function with the given arguments and tests + /// that it returns indicating success using try_code. + template < + typename FuncT, + typename ...Args + > + static void + try_func (FuncT &&func, Args &&...args) + { + static_assert(std::is_same_v< + std::result_of_t, + VkResult + >); + + try_code (std::invoke (func, std::forward (args)...)); + } + + + /// requests a ValueT using a invokable FuncT with arguments + /// [Args..., &ValueT] and returns the result after testing for + /// success. + template + static ValueT + try_query (FuncT &&func, Args &&...args) + { + ValueT value; + try_func (func, std::forward (args)..., &value); + return value; + } }; diff --git a/fence.cpp b/fence.cpp index 8e14692..8db0cb4 100644 --- a/fence.cpp +++ b/fence.cpp @@ -41,7 +41,9 @@ fence::reset (const device &dev, fence *first, fence *last) { CHECK_LE (first, last); - vkResetFences (dev.id (), trunc_cast (last - first), &first->id ()); + cruft::vk::error::try_func ( + &vkResetFences, dev.id (), trunc_cast (last - first), &first->id () + ); } @@ -51,7 +53,9 @@ fence::wait (const device &d, fence *first, fence *last, uint64_t timeout) { CHECK_LE (first, last); - vkWaitForFences (d.id (), trunc_cast (last - first), &first->id (), VK_FALSE, timeout); + cruft::vk::error::try_func ( + &vkWaitForFences, d.id (), trunc_cast (last - first), &first->id (), VK_FALSE, timeout + ); } @@ -61,5 +65,7 @@ fence::wait_all (const device &d, fence *first, fence *last, uint64_t timeout) { CHECK_LE (first, last); - vkWaitForFences (d.id (), trunc_cast (last - first), &first->id (), VK_TRUE, timeout); + cruft::vk::error::try_func ( + &vkWaitForFences, d.id (), trunc_cast (last - first), &first->id (), VK_TRUE, timeout + ); } diff --git a/physical_device.cpp b/physical_device.cpp index 1fd792b..e828e7c 100644 --- a/physical_device.cpp +++ b/physical_device.cpp @@ -59,6 +59,19 @@ physical_device::features (void) const } +//----------------------------------------------------------------------------- +VkSurfaceCapabilitiesKHR +physical_device::surface_capabilities (VkSurfaceKHR surface) const +{ + VkSurfaceCapabilitiesKHR value; + error::try_func ( + &vkGetPhysicalDeviceSurfaceCapabilitiesKHR, id (), surface, &value + ); + + return value; +} + + /////////////////////////////////////////////////////////////////////////////// std::vector physical_device::queue_families (void) const diff --git a/physical_device.hpp b/physical_device.hpp index 0e12bf3..f943022 100644 --- a/physical_device.hpp +++ b/physical_device.hpp @@ -33,6 +33,7 @@ namespace cruft::vk { VkPhysicalDeviceProperties properties (void) const; VkPhysicalDeviceFeatures features (void) const; + VkSurfaceCapabilitiesKHR surface_capabilities (VkSurfaceKHR) const; std::vector queue_families (void) const; diff --git a/tools/hello.cpp b/tools/hello.cpp index 0ea1394..8810895 100644 --- a/tools/hello.cpp +++ b/tools/hello.cpp @@ -95,13 +95,17 @@ main (void) VkDebugReportCallbackEXT callback; VkDebugReportCallbackCreateInfoEXT debug_info {}; debug_info.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; - debug_info.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT; + debug_info.flags = VK_DEBUG_REPORT_INFORMATION_BIT_EXT | + VK_DEBUG_REPORT_WARNING_BIT_EXT | + VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT | + VK_DEBUG_REPORT_ERROR_BIT_EXT | + VK_DEBUG_REPORT_DEBUG_BIT_EXT; debug_info.pfnCallback = vk_debug_callback; auto func = (decltype(&vkCreateDebugReportCallbackEXT))vkGetInstanceProcAddr (instance.id (),"vkCreateDebugReportCallbackEXT"); - cruft::vk::error::try_code ( - func (instance.id (), &debug_info, nullptr, &callback) + cruft::vk::error::try_func ( + func, instance.id (), &debug_info, nullptr, &callback ); } @@ -117,7 +121,9 @@ main (void) std::vector surface_formats; std::vector present_modes; - vkGetPhysicalDeviceSurfaceCapabilitiesKHR (pdevice.id (), surface, &surface_capabilities); + cruft::vk::error::try_func ( + &vkGetPhysicalDeviceSurfaceCapabilitiesKHR, pdevice.id (), surface, &surface_capabilities + ); { uint32_t format_count = 0; cruft::vk::error::try_code ( @@ -150,7 +156,9 @@ main (void) graphics_queue_id = i; VkBool32 present_support = false; - vkGetPhysicalDeviceSurfaceSupportKHR (pdevice.id (), i, surface, &present_support); + cruft::vk::error::try_func ( + &vkGetPhysicalDeviceSurfaceSupportKHR, pdevice.id (), i, surface, &present_support + ); if (present_support) present_queue_id = i; } @@ -515,7 +523,7 @@ main (void) vkCmdEndRenderPass (command_buffers[i]); - vkEndCommandBuffer (command_buffers[i]); + cruft::vk::error::try_func (&vkEndCommandBuffer, command_buffers[i]); }; VkSemaphoreCreateInfo semaphore_info {}; @@ -560,7 +568,6 @@ main (void) dependency.srcSubpass = VK_SUBPASS_EXTERNAL; dependency.dstSubpass = 0; dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - dependency.srcAccessMask = 0; dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; render_pass_info.dependencyCount = 1; @@ -577,14 +584,14 @@ main (void) present_info.pImageIndices = &image_index; present_info.pResults = nullptr; - vkQueuePresentKHR (present_queue.id (), &present_info); + cruft::vk::error::try_func (&vkQueuePresentKHR, present_queue.id (), &present_info); LOG_INFO ("entering runloop"); while (!glfwWindowShouldClose (window)) { glfwPollEvents (); } - vkDeviceWaitIdle (ldevice.id ()); + cruft::vk::error::try_func (&vkDeviceWaitIdle, ldevice.id ()); LOG_INFO ("terminating glfw"); glfwDestroyWindow (window);