vkQuake2 doxygen  1.0 dev
vk_validation.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 2018-2019 Krzysztof Kondrak
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 */
20 
21 #include "vk_local.h"
22 
23 static VkDebugUtilsMessengerEXT validationMessenger = VK_NULL_HANDLE;
24 extern FILE *vk_logfp;
25 
26 #define VK_FILELOG(msg, ...) if(vk_log->value && vk_logfp) { \
27  fprintf(vk_logfp, msg, __VA_ARGS__); \
28 }
29 
30 // layer message to string
31 static const char* msgToString(VkDebugUtilsMessageTypeFlagsEXT type)
32 {
33  int g = (type & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) != 0 ? 1 : 0;
34  int p = (type & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0 ? 1 : 0;
35  int v = (type & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) != 0 ? 1 : 0;
36 
37  if (g) return "";
38  if (p && !v) return "(performance)";
39  if (p && v) return "(performance and validation)";
40  if (v) return "(validation)";
41 
42  return "?";
43 }
44 
45 // validation layer callback function (VK_EXT_debug_utils)
46 static VKAPI_ATTR VkBool32 VKAPI_CALL debugUtilsCallback(VkDebugUtilsMessageSeverityFlagBitsEXT msgSeverity,
47  VkDebugUtilsMessageTypeFlagsEXT msgType,
48  const VkDebugUtilsMessengerCallbackDataEXT *callbackData,
49  void* userData)
50 {
51  switch (msgSeverity)
52  {
53  case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT:
54  ri.Con_Printf(PRINT_ALL, "VK_INFO: %s %s\n", callbackData->pMessage, msgToString(msgType));
55  VK_FILELOG("VK_INFO: %s %s\n", callbackData->pMessage, msgToString(msgType));
56  break;
57  case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT:
58  ri.Con_Printf(PRINT_ALL, "VK_VERBOSE: %s %s\n", callbackData->pMessage, msgToString(msgType));
59  VK_FILELOG("VK_VERBOSE: %s %s\n", callbackData->pMessage, msgToString(msgType));
60  break;
61  case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT:
62  ri.Con_Printf(PRINT_ALL, "VK_WARNING: %s %s\n", callbackData->pMessage, msgToString(msgType));
63  VK_FILELOG("VK_WARNING: %s %s\n", callbackData->pMessage, msgToString(msgType));
64  assert(!"Vulkan warning occured!");
65  break;
66  default:
67  ri.Con_Printf(PRINT_ALL, "VK_ERROR: %s %s\n", callbackData->pMessage, msgToString(msgType));
68  VK_FILELOG("VK_ERROR: %s %s\n", callbackData->pMessage, msgToString(msgType));
69  assert(!"Vulkan error occured!");
70  }
71  return VK_FALSE;
72 }
73 
75 {
76  VkDebugUtilsMessengerCreateInfoEXT callbackInfo = {
77  .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
78  .pNext = NULL,
79  .flags = 0,
80  .messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
81  VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT,
82  .messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
83  VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
84  VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT,
85  .pfnUserCallback = debugUtilsCallback,
86  .pUserData = NULL
87  };
88 
89  if( vk_validation->value > 1 )
90  {
91  callbackInfo.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
92  VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
93  }
94 
96  ri.Con_Printf(PRINT_ALL, "...Vulkan validation layers enabled\n");
97 }
98 
100 {
101  if( validationMessenger != VK_NULL_HANDLE )
102  {
104  validationMessenger = VK_NULL_HANDLE;
105  }
106 }
vk_local.h
vk_instance
VkInstance vk_instance
Definition: vk_common.c:46
ri
refimport_t ri
Definition: r_main.c:25
v
GLdouble v
Definition: qgl_win.c:143
validationMessenger
static VkDebugUtilsMessengerEXT validationMessenger
Definition: vk_validation.c:23
msgToString
static const char * msgToString(VkDebugUtilsMessageTypeFlagsEXT type)
Definition: vk_validation.c:31
VK_FILELOG
#define VK_FILELOG(msg,...)
Definition: vk_validation.c:26
type
GLenum type
Definition: qgl_win.c:72
vk_logfp
FILE * vk_logfp
Definition: vk_common.c:43
refimport_t::Con_Printf
void(* Con_Printf)(int print_level, char *str,...)
Definition: ref.h:202
PRINT_ALL
#define PRINT_ALL
Definition: qcommon.h:751
cvar_s::value
float value
Definition: q_shared.h:331
vk_validation
cvar_t * vk_validation
Definition: vk_rmain.c:90
qvkCreateDebugUtilsMessengerEXT
PFN_vkCreateDebugUtilsMessengerEXT qvkCreateDebugUtilsMessengerEXT
Definition: vk_common.c:166
QVk_CreateValidationLayers
void QVk_CreateValidationLayers()
Definition: vk_validation.c:74
NULL
#define NULL
Definition: q_shared.h:67
VK_VERIFY
#define VK_VERIFY(x)
Definition: vk_local.h:59
QVk_DestroyValidationLayers
void QVk_DestroyValidationLayers()
Definition: vk_validation.c:99
debugUtilsCallback
static VKAPI_ATTR VkBool32 VKAPI_CALL debugUtilsCallback(VkDebugUtilsMessageSeverityFlagBitsEXT msgSeverity, VkDebugUtilsMessageTypeFlagsEXT msgType, const VkDebugUtilsMessengerCallbackDataEXT *callbackData, void *userData)
Definition: vk_validation.c:46
qvkDestroyDebugUtilsMessengerEXT
PFN_vkDestroyDebugUtilsMessengerEXT qvkDestroyDebugUtilsMessengerEXT
Definition: vk_common.c:167