except: add try_func and try_query

This commit is contained in:
Danny Robson 2017-09-06 12:12:10 +10:00
parent dc7fa60e15
commit 83e7621b40
5 changed files with 73 additions and 12 deletions

View File

@ -21,12 +21,46 @@
#include "./vk.hpp"
#include <exception>
#include <functional>
#include <type_traits>
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<FuncT(Args...)>,
VkResult
>);
try_code (std::invoke (func, std::forward<Args> (args)...));
}
/// requests a ValueT using a invokable FuncT with arguments
/// [Args..., &ValueT] and returns the result after testing for
/// success.
template <typename ValueT, typename FuncT, typename ...Args>
static ValueT
try_query (FuncT &&func, Args &&...args)
{
ValueT value;
try_func (func, std::forward<Args> (args)..., &value);
return value;
}
};

View File

@ -41,7 +41,9 @@ fence::reset (const device &dev, fence *first, fence *last)
{
CHECK_LE (first, last);
vkResetFences (dev.id (), trunc_cast<uint32_t> (last - first), &first->id ());
cruft::vk::error::try_func (
&vkResetFences, dev.id (), trunc_cast<uint32_t> (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<uint32_t> (last - first), &first->id (), VK_FALSE, timeout);
cruft::vk::error::try_func (
&vkWaitForFences, d.id (), trunc_cast<uint32_t> (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<uint32_t> (last - first), &first->id (), VK_TRUE, timeout);
cruft::vk::error::try_func (
&vkWaitForFences, d.id (), trunc_cast<uint32_t> (last - first), &first->id (), VK_TRUE, timeout
);
}

View File

@ -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<VkQueueFamilyProperties>
physical_device::queue_families (void) const

View File

@ -33,6 +33,7 @@ namespace cruft::vk {
VkPhysicalDeviceProperties properties (void) const;
VkPhysicalDeviceFeatures features (void) const;
VkSurfaceCapabilitiesKHR surface_capabilities (VkSurfaceKHR) const;
std::vector<VkQueueFamilyProperties> queue_families (void) const;

View File

@ -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<VkSurfaceFormatKHR> surface_formats;
std::vector<VkPresentModeKHR> 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);