diff --git a/samples/api/hello_triangle/hello_triangle.cpp b/samples/api/hello_triangle/hello_triangle.cpp index 33aec7d81..689124cdc 100644 --- a/samples/api/hello_triangle/hello_triangle.cpp +++ b/samples/api/hello_triangle/hello_triangle.cpp @@ -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; } @@ -176,22 +178,22 @@ void HelloTriangle::init_instance(Context &context, std::vector 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 @@ -238,7 +240,7 @@ void HelloTriangle::init_instance(Context &context, std::vector 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 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()); @@ -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 @@ -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 } @@ -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; } diff --git a/samples/api/hello_triangle/hello_triangle.h b/samples/api/hello_triangle/hello_triangle.h index a093769f4..0f3c62614 100644 --- a/samples/api/hello_triangle/hello_triangle.h +++ b/samples/api/hello_triangle/hello_triangle.h @@ -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 * @@ -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 recycled_semaphores;