Move debug callback into header and allow adding user pointer

This commit is contained in:
Charles Giessen 2021-06-12 22:36:08 -06:00 committed by Charles Giessen
parent e511dfdbf9
commit 867376d01e
3 changed files with 32 additions and 20 deletions

View File

@ -16,7 +16,6 @@
#include "VkBootstrap.h"
#include <cstdio>
#include <cstring>
#if defined(_WIN32)
@ -257,6 +256,7 @@ VkResult create_debug_utils_messenger(VkInstance instance,
PFN_vkDebugUtilsMessengerCallbackEXT debug_callback,
VkDebugUtilsMessageSeverityFlagsEXT severity,
VkDebugUtilsMessageTypeFlagsEXT type,
void* user_data_pointer,
VkDebugUtilsMessengerEXT* pDebugMessenger,
VkAllocationCallbacks* allocation_callbacks) {
@ -267,6 +267,7 @@ VkResult create_debug_utils_messenger(VkInstance instance,
messengerCreateInfo.messageSeverity = severity;
messengerCreateInfo.messageType = type;
messengerCreateInfo.pfnUserCallback = debug_callback;
messengerCreateInfo.pUserData = user_data_pointer;
PFN_vkCreateDebugUtilsMessengerEXT createMessengerFunc;
detail::vulkan_functions().get_inst_proc_addr(createMessengerFunc, "vkCreateDebugUtilsMessengerEXT");
@ -289,17 +290,6 @@ void destroy_debug_utils_messenger(
}
}
VKAPI_ATTR VkBool32 VKAPI_CALL default_debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
void*) {
auto ms = to_string_message_severity(messageSeverity);
auto mt = to_string_message_type(messageType);
printf("[%s: %s]\n%s\n", ms, mt, pCallbackData->pMessage);
return VK_FALSE;
}
namespace detail {
bool check_layer_supported(std::vector<VkLayerProperties> const& available_layers, const char* layer_name) {
if (!layer_name) return false;
@ -674,6 +664,7 @@ detail::Result<Instance> InstanceBuilder::build() const {
messengerCreateInfo.messageSeverity = info.debug_message_severity;
messengerCreateInfo.messageType = info.debug_message_type;
messengerCreateInfo.pfnUserCallback = info.debug_callback;
messengerCreateInfo.pUserData = info.debug_user_data_pointer;
pNext_chain.push_back(reinterpret_cast<VkBaseOutStructure*>(&messengerCreateInfo));
}
@ -725,6 +716,7 @@ detail::Result<Instance> InstanceBuilder::build() const {
info.debug_callback,
info.debug_message_severity,
info.debug_message_type,
info.debug_user_data_pointer,
&instance.debug_messenger,
info.allocation_callbacks);
if (res != VK_SUCCESS) {
@ -795,6 +787,10 @@ InstanceBuilder& InstanceBuilder::set_debug_callback(PFN_vkDebugUtilsMessengerCa
info.debug_callback = callback;
return *this;
}
InstanceBuilder& InstanceBuilder::set_debug_callback_user_data_pointer(void* user_data_pointer) {
info.debug_user_data_pointer = user_data_pointer;
return *this;
}
InstanceBuilder& InstanceBuilder::set_headless(bool headless) {
info.headless_context = headless;
return *this;

View File

@ -17,6 +17,7 @@
#pragma once
#include <cassert>
#include <cstdio>
#include <vector>
#include <system_error>
@ -206,6 +207,22 @@ struct SystemInfo {
bool debug_utils_available = false;
};
// Forward declared - check VkBoostrap.cpp for implementations
const char* to_string_message_severity(VkDebugUtilsMessageSeverityFlagBitsEXT s);
const char* to_string_message_type(VkDebugUtilsMessageTypeFlagsEXT s);
// Default debug messenger
// Feel free to copy-paste it into your own code, change it as needed, then call `set_debug_callback()` to use that instead
inline VKAPI_ATTR VkBool32 VKAPI_CALL default_debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
void*) {
auto ms = to_string_message_severity(messageSeverity);
auto mt = to_string_message_type(messageType);
printf("[%s: %s]\n%s\n", ms, mt, pCallbackData->pMessage);
return VK_FALSE; // Applications must return false here
}
class InstanceBuilder;
class PhysicalDeviceSelector;
@ -270,6 +287,8 @@ class InstanceBuilder {
InstanceBuilder& use_default_debug_messenger();
// Provide a user defined debug callback.
InstanceBuilder& set_debug_callback(PFN_vkDebugUtilsMessengerCallbackEXT callback);
// Sets the void* to use in the debug messenger - only useful with a custom callback
InstanceBuilder& set_debug_callback_user_data_pointer(void* user_data_pointer);
// Set what message severity is needed to trigger the callback.
InstanceBuilder& set_debug_messenger_severity(VkDebugUtilsMessageSeverityFlagsEXT severity);
// Add a message severity to the list that triggers the callback.
@ -310,13 +329,14 @@ class InstanceBuilder {
VkInstanceCreateFlags flags = 0;
std::vector<VkBaseOutStructure*> pNext_elements;
// debug callback
PFN_vkDebugUtilsMessengerCallbackEXT debug_callback = nullptr;
// debug callback - use the default so it is not nullptr
PFN_vkDebugUtilsMessengerCallbackEXT debug_callback = default_debug_callback;
VkDebugUtilsMessageSeverityFlagsEXT debug_message_severity =
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
VkDebugUtilsMessageTypeFlagsEXT debug_message_type =
VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
void* debug_user_data_pointer = nullptr;
// validation features
std::vector<VkValidationCheckEXT> disabled_validation_checks;

View File

@ -3,11 +3,8 @@
#include <catch2/catch.hpp>
vkb::Instance get_instance(uint32_t minor_version = 0) {
auto instance_ret = vkb::InstanceBuilder()
.request_validation_layers()
.require_api_version(1, minor_version)
.use_default_debug_messenger()
.build();
auto instance_ret =
vkb::InstanceBuilder().request_validation_layers().require_api_version(1, minor_version).build();
REQUIRE(instance_ret.has_value());
return instance_ret.value();
}
@ -16,7 +13,6 @@ vkb::Instance get_headless_instance(uint32_t minor_version = 0) {
.request_validation_layers()
.require_api_version(1, minor_version)
.set_headless()
.use_default_debug_messenger()
.build();
REQUIRE(instance_ret.has_value());
return instance_ret.value();