Skip to content

Commit

Permalink
Vulkan: Add debug utills with define guards
Browse files Browse the repository at this point in the history
  • Loading branch information
JKSunny committed Feb 11, 2024
1 parent bda5b5a commit 82ffb3c
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 24 deletions.
63 changes: 54 additions & 9 deletions codemp/rd-vulkan/vk_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Vulkan validation layer debug callback
================
*/

#ifdef USE_DEBUG_REPORT
VKAPI_ATTR VkBool32 VKAPI_CALL vk_debug_callback(
VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT object_type,
uint64_t object, size_t location, int32_t message_code,
Expand All @@ -72,21 +73,65 @@ VKAPI_ATTR VkBool32 VKAPI_CALL vk_debug_callback(
vk_debug("debug callback: %s\n", message);
return VK_FALSE;
}
#endif

#ifdef USE_DEBUG_UTILS
VKAPI_ATTR VkBool32 VKAPI_CALL vk_debug_utils_messenger_callback (
VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
VkDebugUtilsMessageTypeFlagsEXT message_type,
const VkDebugUtilsMessengerCallbackDataEXT *callback_data,
void *user_data)
{
if (message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)
{
vk_debug("{%d} - {%s}: {%s}", callback_data->messageIdNumber, callback_data->pMessageIdName, callback_data->pMessage);
}
else if (message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
{
vk_debug("{%d} - {%s}: {%s}", callback_data->messageIdNumber, callback_data->pMessageIdName, callback_data->pMessage);
}
return VK_FALSE;

}
#endif

#ifdef USE_DEBUG_UTILS
void vk_create_debug_utils( VkDebugUtilsMessengerCreateInfoEXT &desc ) {
desc.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
desc.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
desc.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
desc.pfnUserCallback = vk_debug_utils_messenger_callback;
}
#endif

void vk_create_debug_callback(void)
{
Com_Printf("Create vulkan debug callback\n");

VkDebugReportCallbackCreateInfoEXT desc;
desc.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
desc.pNext = NULL;
desc.flags = VK_DEBUG_REPORT_WARNING_BIT_EXT |
VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
VK_DEBUG_REPORT_ERROR_BIT_EXT;
desc.pfnCallback = &vk_debug_callback;
desc.pUserData = NULL;
#ifdef USE_DEBUG_REPORT
{
VkDebugReportCallbackCreateInfoEXT desc;
desc.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
desc.pNext = NULL;
desc.flags = VK_DEBUG_REPORT_WARNING_BIT_EXT |
VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
VK_DEBUG_REPORT_ERROR_BIT_EXT;
desc.pfnCallback = &vk_debug_callback;
desc.pUserData = NULL;

VK_CHECK(qvkCreateDebugReportCallbackEXT(vk.instance, &desc, NULL, &vk.debug_callback));
}
#endif

#ifdef USE_DEBUG_UTILS
{
VkDebugUtilsMessengerCreateInfoEXT desc;
Com_Memset( &desc, 0, sizeof(VkDebugUtilsMessengerCreateInfoEXT) );
vk_create_debug_utils( desc );

VK_CHECK(qvkCreateDebugReportCallbackEXT(vk.instance, &desc, NULL, &vk.debug_callback));
VkResult result = qvkCreateDebugUtilsMessengerEXT(vk.instance, &desc, nullptr, &vk.debug_utils_messenger);
}
#endif

return;
}
Expand Down
10 changes: 8 additions & 2 deletions codemp/rd-vulkan/vk_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,14 @@ void vk_shutdown( void )
qvkDestroySurfaceKHR(vk.instance, vk.surface, NULL);

#ifdef USE_VK_VALIDATION
if (qvkDestroyDebugReportCallbackEXT && vk.debug_callback)
qvkDestroyDebugReportCallbackEXT(vk.instance, vk.debug_callback, NULL);
#ifdef USE_DEBUG_REPORT
if (qvkDestroyDebugReportCallbackEXT && vk.debug_callback)
qvkDestroyDebugReportCallbackEXT(vk.instance, vk.debug_callback, NULL);
#endif
#ifdef USE_DEBUG_UTILS
if (qvkDestroyDebugUtilsMessengerEXT && vk.debug_utils_messenger)
qvkDestroyDebugUtilsMessengerEXT(vk.instance, vk.debug_utils_messenger, NULL);
#endif
#endif

if (vk.instance != VK_NULL_HANDLE)
Expand Down
66 changes: 57 additions & 9 deletions codemp/rd-vulkan/vk_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ PFN_vkGetPhysicalDeviceSurfacePresentModesKHR qvkGetPhysicalDeviceSurfacePresent
PFN_vkGetPhysicalDeviceSurfaceSupportKHR qvkGetPhysicalDeviceSurfaceSupportKHR;

#ifdef USE_VK_VALIDATION
PFN_vkCreateDebugReportCallbackEXT qvkCreateDebugReportCallbackEXT;
PFN_vkDestroyDebugReportCallbackEXT qvkDestroyDebugReportCallbackEXT;
#ifdef USE_DEBUG_REPORT
PFN_vkCreateDebugReportCallbackEXT qvkCreateDebugReportCallbackEXT;
PFN_vkDestroyDebugReportCallbackEXT qvkDestroyDebugReportCallbackEXT;
#endif
#ifdef USE_DEBUG_UTILS
PFN_vkCreateDebugUtilsMessengerEXT qvkCreateDebugUtilsMessengerEXT;
PFN_vkDestroyDebugUtilsMessengerEXT qvkDestroyDebugUtilsMessengerEXT;
#endif
#endif

PFN_vkAllocateCommandBuffers qvkAllocateCommandBuffers;
Expand Down Expand Up @@ -171,8 +177,14 @@ static qboolean vk_used_instance_extension( const char *ext )
return qtrue;

#ifdef USE_VK_VALIDATION
if (Q_stricmp(ext, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0)
return qtrue;
#ifdef USE_DEBUG_REPORT
if (Q_stricmp(ext, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0)
return qtrue;
#endif
#ifdef USE_DEBUG_UTILS
if (Q_stricmp(ext, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0)
return qtrue;
#endif
#endif

if (Q_stricmp(ext, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0)
Expand Down Expand Up @@ -282,6 +294,14 @@ static void vk_create_instance( void )
desc.enabledLayerCount = 1;
desc.ppEnabledLayerNames = &validation_layer_name_lunarg;

#ifdef USE_DEBUG_UTILS
VkDebugUtilsMessengerCreateInfoEXT debug_utils_create_info;
Com_Memset( &debug_utils_create_info, 0, sizeof(VkDebugUtilsMessengerCreateInfoEXT) );
vk_create_debug_utils( debug_utils_create_info );

desc.pNext = &debug_utils_create_info;
#endif

result = qvkCreateInstance(&desc, NULL, &vk.instance);

if (result == VK_ERROR_LAYER_NOT_PRESENT) {
Expand All @@ -298,6 +318,9 @@ static void vk_create_instance( void )
// try without validation layer
desc.enabledLayerCount = 0;
desc.ppEnabledLayerNames = NULL;
#ifdef USE_DEBUG_UTILS
desc.pNext = NULL;
#endif

result = qvkCreateInstance(&desc, NULL, &vk.instance);
}
Expand Down Expand Up @@ -552,7 +575,7 @@ static qboolean vk_create_device( VkPhysicalDevice physical_device, int device_i
// create VkDevice
{
char *str;
const char *device_extension_list[4];
const char *device_extension_list[5];
uint32_t device_extension_count;
const char *ext, *end;
const float priority = 1.0;
Expand All @@ -566,6 +589,7 @@ static qboolean vk_create_device( VkPhysicalDevice physical_device, int device_i
qboolean dedicatedAllocation = qfalse;
qboolean memoryRequirements2 = qfalse;
qboolean debugMarker = qfalse;
qboolean toolingInfo = qfalse;
uint32_t i, len, count = 0;

VK_CHECK(qvkEnumerateDeviceExtensionProperties(physical_device, NULL, &count, NULL));
Expand All @@ -591,6 +615,9 @@ static qboolean vk_create_device( VkPhysicalDevice physical_device, int device_i
else if (strcmp(ext, VK_EXT_DEBUG_MARKER_EXTENSION_NAME) == 0) {
debugMarker = qtrue;
}
else if (strcmp(ext, VK_EXT_TOOLING_INFO_EXTENSION_NAME) == 0) {
toolingInfo = qtrue;
}

// add this device extension to glConfig
if (i != 0) {
Expand Down Expand Up @@ -634,6 +661,9 @@ static qboolean vk_create_device( VkPhysicalDevice physical_device, int device_i
vk.debugMarkers = qtrue;
}

if ( toolingInfo )
device_extension_list[device_extension_count++] = VK_EXT_TOOLING_INFO_EXTENSION_NAME;

qvkGetPhysicalDeviceFeatures(physical_device, &device_features);

if (device_features.fillModeNonSolid == VK_FALSE) {
Expand Down Expand Up @@ -755,8 +785,14 @@ void vk_init_library( void )
INIT_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfaceSupportKHR)

#ifdef USE_VK_VALIDATION
INIT_INSTANCE_FUNCTION_EXT(vkCreateDebugReportCallbackEXT)
INIT_INSTANCE_FUNCTION_EXT(vkDestroyDebugReportCallbackEXT)
#ifdef USE_DEBUG_REPORT
INIT_INSTANCE_FUNCTION_EXT(vkCreateDebugReportCallbackEXT)
INIT_INSTANCE_FUNCTION_EXT(vkDestroyDebugReportCallbackEXT)
#endif
#ifdef USE_DEBUG_UTILS
INIT_INSTANCE_FUNCTION_EXT(vkCreateDebugUtilsMessengerEXT)
INIT_INSTANCE_FUNCTION_EXT(vkDestroyDebugUtilsMessengerEXT)
#endif

vk_create_debug_callback();
#endif
Expand All @@ -783,8 +819,14 @@ void vk_init_library( void )
// Clear instance with a subset of vk_shutdown
qvkDestroySurfaceKHR(vk.instance, vk.surface, NULL);
#ifdef USE_VK_VALIDATION
#ifdef USE_DEBUG_REPORT
if (qvkDestroyDebugReportCallbackEXT && vk.debug_callback)
qvkDestroyDebugReportCallbackEXT(vk.instance, vk.debug_callback, NULL);
#endif
#ifdef USE_DEBUG_UTILS
if (qvkDestroyDebugUtilsMessengerEXT && vk.debug_utils_messenger)
qvkDestroyDebugUtilsMessengerEXT(vk.instance, vk.debug_utils_messenger, NULL);
#endif
#endif
qvkDestroyInstance(vk.instance, NULL);
vk_deinit_library();
Expand Down Expand Up @@ -964,8 +1006,14 @@ void vk_deinit_library( void )
qvkGetPhysicalDeviceSurfacePresentModesKHR = NULL;
qvkGetPhysicalDeviceSurfaceSupportKHR = NULL;
#ifdef USE_VK_VALIDATION
qvkCreateDebugReportCallbackEXT = NULL;
qvkDestroyDebugReportCallbackEXT = NULL;
#ifdef USE_DEBUG_REPORT
qvkCreateDebugReportCallbackEXT = NULL;
qvkDestroyDebugReportCallbackEXT = NULL;
#endif
#ifdef USE_DEBUG_UTILS
qvkCreateDebugUtilsMessengerEXT = NULL;
qvkDestroyDebugUtilsMessengerEXT = NULL;
#endif
#endif
qvkAllocateCommandBuffers = NULL;
qvkAllocateDescriptorSets = NULL;
Expand Down
28 changes: 24 additions & 4 deletions codemp/rd-vulkan/vk_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ along with this program; if not, see <http://www.gnu.org/licenses/>.
#if defined (_DEBUG)
#if defined (_WIN32)
#define USE_VK_VALIDATION
#define USE_DEBUG_REPORT
//#define USE_DEBUG_UTILS
#endif
#endif

Expand Down Expand Up @@ -292,8 +294,14 @@ extern PFN_vkGetPhysicalDeviceSurfaceFormatsKHR qvkGetPhysicalDeviceSurface
extern PFN_vkGetPhysicalDeviceSurfacePresentModesKHR qvkGetPhysicalDeviceSurfacePresentModesKHR;
extern PFN_vkGetPhysicalDeviceSurfaceSupportKHR qvkGetPhysicalDeviceSurfaceSupportKHR;
#ifdef USE_VK_VALIDATION
extern PFN_vkCreateDebugReportCallbackEXT qvkCreateDebugReportCallbackEXT;
extern PFN_vkDestroyDebugReportCallbackEXT qvkDestroyDebugReportCallbackEXT;
#ifdef USE_DEBUG_REPORT
extern PFN_vkCreateDebugReportCallbackEXT qvkCreateDebugReportCallbackEXT;
extern PFN_vkDestroyDebugReportCallbackEXT qvkDestroyDebugReportCallbackEXT;
#endif
#ifdef USE_DEBUG_UTILS
extern PFN_vkCreateDebugUtilsMessengerEXT qvkCreateDebugUtilsMessengerEXT;
extern PFN_vkDestroyDebugUtilsMessengerEXT qvkDestroyDebugUtilsMessengerEXT;
#endif
#endif
extern PFN_vkAllocateCommandBuffers qvkAllocateCommandBuffers;
extern PFN_vkAllocateDescriptorSets qvkAllocateDescriptorSets;
Expand Down Expand Up @@ -585,7 +593,12 @@ typedef struct {
char instance_extensions_string[MAX_STRING_CHARS];

#ifdef USE_VK_VALIDATION
VkDebugReportCallbackEXT debug_callback;
#ifdef USE_DEBUG_REPORT
VkDebugReportCallbackEXT debug_callback;
#endif
#ifdef USE_DEBUG_UTILS
VkDebugUtilsMessengerEXT debug_utils_messenger;
#endif
#endif

uint32_t queue_family_index;
Expand Down Expand Up @@ -1052,5 +1065,12 @@ void vk_set_object_name( uint64_t obj, const char *objName, VkDebugReportObject
}

void vk_debug( const char *msg, ... );
void vk_create_debug_callback( void );
void R_DebugGraphics( void );

#ifdef USE_VK_VALIDATION
void vk_create_debug_callback( void );

#ifdef USE_DEBUG_UTILS
void vk_create_debug_utils( VkDebugUtilsMessengerCreateInfoEXT &desc );
#endif
#endif

0 comments on commit 82ffb3c

Please sign in to comment.