Created vk function pointer loading helper function

This commit is contained in:
Charles Giessen 2020-03-07 21:57:49 -07:00
parent f389114811
commit 5ae586b559

View File

@ -6,6 +6,13 @@
namespace vkb { namespace vkb {
namespace detail { namespace detail {
template <typename T>
void get_inst_proc_addr (
T& out_ptr, const char* func_name, VkInstance instance, PFN_vkGetInstanceProcAddr ptr_vkGetInstanceProcAddr) {
out_ptr = reinterpret_cast<T> (ptr_vkGetInstanceProcAddr (instance, func_name));
}
// Helper for robustly executing the two-call pattern // Helper for robustly executing the two-call pattern
template <typename T, typename F, typename... Ts> template <typename T, typename F, typename... Ts>
auto get_vector (F&& f, Ts&&... ts) -> Expected<std::vector<T>, VkResult> { auto get_vector (F&& f, Ts&&... ts) -> Expected<std::vector<T>, VkResult> {
@ -68,6 +75,7 @@ VkResult create_debug_utils_messenger (VkInstance instance,
VkDebugUtilsMessageTypeFlagsEXT type, VkDebugUtilsMessageTypeFlagsEXT type,
VkDebugUtilsMessengerEXT* pDebugMessenger, VkDebugUtilsMessengerEXT* pDebugMessenger,
VkAllocationCallbacks* allocation_callbacks) { VkAllocationCallbacks* allocation_callbacks) {
if (debug_callback == nullptr) debug_callback = default_debug_callback; if (debug_callback == nullptr) debug_callback = default_debug_callback;
VkDebugUtilsMessengerCreateInfoEXT messengerCreateInfo = {}; VkDebugUtilsMessengerCreateInfoEXT messengerCreateInfo = {};
messengerCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; messengerCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
@ -76,12 +84,12 @@ VkResult create_debug_utils_messenger (VkInstance instance,
messengerCreateInfo.messageType = type; messengerCreateInfo.messageType = type;
messengerCreateInfo.pfnUserCallback = debug_callback; messengerCreateInfo.pfnUserCallback = debug_callback;
PFN_vkCreateDebugUtilsMessengerEXT createMessengerFunc;
detail::get_inst_proc_addr (
createMessengerFunc, "vkCreateDebugUtilsMessengerEXT", instance, vkGetInstanceProcAddr);
auto vkCreateDebugUtilsMessengerEXT_func = reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT> ( if (createMessengerFunc != nullptr) {
vkGetInstanceProcAddr (instance, "vkCreateDebugUtilsMessengerEXT")); return createMessengerFunc (instance, &messengerCreateInfo, allocation_callbacks, pDebugMessenger);
if (vkCreateDebugUtilsMessengerEXT_func != nullptr) {
return vkCreateDebugUtilsMessengerEXT_func (
instance, &messengerCreateInfo, allocation_callbacks, pDebugMessenger);
} else { } else {
return VK_ERROR_EXTENSION_NOT_PRESENT; return VK_ERROR_EXTENSION_NOT_PRESENT;
} }
@ -89,10 +97,11 @@ VkResult create_debug_utils_messenger (VkInstance instance,
void destroy_debug_utils_messenger ( void destroy_debug_utils_messenger (
VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, VkAllocationCallbacks* allocation_callbacks) { VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, VkAllocationCallbacks* allocation_callbacks) {
auto vkDestroyDebugUtilsMessengerEXT_func = reinterpret_cast<PFN_vkDestroyDebugUtilsMessengerEXT> ( PFN_vkDestroyDebugUtilsMessengerEXT deleteMessengerFunc;
vkGetInstanceProcAddr (instance, "vkDestroyDebugUtilsMessengerEXT")); detail::get_inst_proc_addr (
if (vkDestroyDebugUtilsMessengerEXT_func != nullptr) { deleteMessengerFunc, "vkDestroyDebugUtilsMessengerEXT", instance, vkGetInstanceProcAddr);
vkDestroyDebugUtilsMessengerEXT_func (instance, debugMessenger, allocation_callbacks); if (deleteMessengerFunc != nullptr) {
deleteMessengerFunc (instance, debugMessenger, allocation_callbacks);
} }
} }