Skip to content

Commit

Permalink
Replace deprecated debug reports extension with debug utils
Browse files Browse the repository at this point in the history
Fix validation layers not being enabled in debug builds
Refs KhronosGroup#1168
  • Loading branch information
SaschaWillems committed Oct 19, 2024
1 parent 6505639 commit b4f923f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
63 changes: 34 additions & 29 deletions samples/api/hello_triangle/hello_triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,28 @@
#include "platform/window.h"

#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
/// @brief A debug callback called from Vulkan validation layers.
static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT type,
uint64_t object, size_t location, int32_t message_code,
const char *layer_prefix, const char *message, void *user_data)
/// @brief A debug callback used to report messages from the validation layers. See instance creation for details on how this is set up
static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, VkDebugUtilsMessageTypeFlagsEXT message_type,
const VkDebugUtilsMessengerCallbackDataEXT *callback_data,
void *user_data)
{
if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT)
(void) user_data;

if (message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
{
LOGE("Validation Layer: Error: {}: {}", layer_prefix, message);
LOGE("{} Validation Layer: Error: {}: {}", callback_data->messageIdNumber, callback_data->pMessageIdName, callback_data->pMessage)
}
else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT)
else if (message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)
{
LOGE("Validation Layer: Warning: {}: {}", layer_prefix, message);
LOGE("{} Validation Layer: Warning: {}: {}", callback_data->messageIdNumber, callback_data->pMessageIdName, callback_data->pMessage)
}
else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT)
else if (message_type & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT)
{
LOGI("Validation Layer: Performance warning: {}: {}", layer_prefix, message);
LOGI("{} Validation Layer: Performance warning: {}: {}", callback_data->messageIdNumber, callback_data->pMessageIdName, callback_data->pMessage)
}
else
{
LOGI("Validation Layer: Information: {}: {}", layer_prefix, message);
LOGI("{} Validation Layer: Information: {}: {}", callback_data->messageIdNumber, callback_data->pMessageIdName, callback_data->pMessage)
}
return VK_FALSE;
}
Expand Down Expand Up @@ -176,22 +178,22 @@ void HelloTriangle::init_instance(Context &context,
std::vector<const char *> active_instance_extensions(required_instance_extensions);

#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
bool has_debug_report = false;
// Validation layers help finding wrong api usage, we enable them when explicitly requested or in debug builds
// For this we use the debug utils extension if it is supported
bool has_debug_utils = false;
for (const auto &ext : available_instance_extensions)
{
if (strcmp(ext.extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0)
if (strcmp(ext.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0)
{
has_debug_report = true;
has_debug_utils = true;
active_instance_extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
break;
}
}
if (has_debug_report)
if (!has_debug_utils)
{
active_instance_extensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
}
else
{
LOGW("{} is not available; disabling debug reporting", VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
LOGW("{} not supported or available", VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
LOGW("Make sure to compile the sample in debug mode and/or enable the validation layers");
}
#endif

Expand Down Expand Up @@ -238,7 +240,7 @@ void HelloTriangle::init_instance(Context &context,

std::vector<const char *> requested_validation_layers(required_validation_layers);

#ifdef VKB_VALIDATION_LAYERS
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
// Determine the optimal validation layers to enable that are necessary for useful debugging
std::vector<const char *> optimal_validation_layers = vkb::get_optimal_validation_layers(supported_validation_layers);
requested_validation_layers.insert(requested_validation_layers.end(), optimal_validation_layers.begin(), optimal_validation_layers.end());
Expand Down Expand Up @@ -270,13 +272,16 @@ void HelloTriangle::init_instance(Context &context,
instance_info.ppEnabledLayerNames = requested_validation_layers.data();

#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
VkDebugReportCallbackCreateInfoEXT debug_report_create_info = {VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT};
if (has_debug_report)
// Validation layers help finding wrong api usage, we enable them when explicitly requested or in debug builds
// For this we use the debug utils extension if it is supported
VkDebugUtilsMessengerCreateInfoEXT debug_utils_create_info = {VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT};
if (has_debug_utils)
{
debug_report_create_info.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
debug_report_create_info.pfnCallback = debug_callback;
debug_utils_create_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
debug_utils_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
debug_utils_create_info.pfnUserCallback = debug_callback;

instance_info.pNext = &debug_report_create_info;
instance_info.pNext = &debug_utils_create_info;
}
#endif

Expand All @@ -293,9 +298,9 @@ void HelloTriangle::init_instance(Context &context,
volkLoadInstance(context.instance);

#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
if (has_debug_report)
if (has_debug_utils)
{
VK_CHECK(vkCreateDebugReportCallbackEXT(context.instance, &debug_report_create_info, nullptr, &context.debug_callback));
VK_CHECK(vkCreateDebugUtilsMessengerEXT(context.instance, &debug_utils_create_info, nullptr, &context.debug_callback));
}
#endif
}
Expand Down Expand Up @@ -1062,7 +1067,7 @@ void HelloTriangle::teardown(Context &context)

if (context.debug_callback != VK_NULL_HANDLE)
{
vkDestroyDebugReportCallbackEXT(context.instance, context.debug_callback, nullptr);
vkDestroyDebugUtilsMessengerEXT(context.instance, context.debug_callback, nullptr);
context.debug_callback = VK_NULL_HANDLE;
}

Expand Down
4 changes: 2 additions & 2 deletions samples/api/hello_triangle/hello_triangle.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2018-2023, Arm Limited and Contributors
/* Copyright (c) 2018-2024, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -110,7 +110,7 @@ class HelloTriangle : public vkb::Application
VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;

/// The debug report callback.
VkDebugReportCallbackEXT debug_callback = VK_NULL_HANDLE;
VkDebugUtilsMessengerEXT debug_callback = VK_NULL_HANDLE;

/// A set of semaphores that can be reused.
std::vector<VkSemaphore> recycled_semaphores;
Expand Down

0 comments on commit b4f923f

Please sign in to comment.