Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Vulkan |
| 3 | * |
| 4 | * Copyright (C) 2014 LunarG, Inc. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included |
| 14 | * in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: |
| 25 | * Courtney Goeltzenleuchter <courtney@lunarg.com> |
| 26 | */ |
| 27 | |
| 28 | #ifndef LAYER_LOGGING_H |
| 29 | #define LAYER_LOGGING_H |
| 30 | |
| 31 | #include <stdio.h> |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 32 | #include <stdarg.h> |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 33 | #include <stdbool.h> |
| 34 | #include <unordered_map> |
Tobin Ehlis | 0c6f9ee | 2015-07-03 09:42:57 -0600 | [diff] [blame] | 35 | #include "vk_layer.h" |
Tobin Ehlis | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 36 | #include "vk_layer_data.h" |
| 37 | #include "vk_layer_table.h" |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 38 | |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 39 | typedef struct _debug_report_data { |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 40 | VkLayerDbgFunctionNode *g_pDbgFunctionHead; |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 41 | VkFlags active_flags; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 42 | bool g_DEBUG_REPORT; |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 43 | } debug_report_data; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 44 | |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 45 | template debug_report_data *get_my_data_ptr<debug_report_data>( |
Courtney Goeltzenleuchter | 7dcc6a7 | 2015-06-11 16:01:11 -0600 | [diff] [blame] | 46 | void *data_key, |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 47 | std::unordered_map<void *, debug_report_data *> &data_map); |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 48 | |
| 49 | // Utility function to handle reporting |
Courtney Goeltzenleuchter | 37d482d | 2015-06-14 11:28:24 -0600 | [diff] [blame] | 50 | static inline void debug_report_log_msg( |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 51 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 52 | VkFlags msgFlags, |
| 53 | VkObjectType objectType, |
| 54 | VkObject srcObject, |
| 55 | size_t location, |
| 56 | int32_t msgCode, |
| 57 | const char* pLayerPrefix, |
| 58 | const char* pMsg) |
| 59 | { |
| 60 | VkLayerDbgFunctionNode *pTrav = debug_data->g_pDbgFunctionHead; |
| 61 | while (pTrav) { |
| 62 | if (pTrav->msgFlags & msgFlags) { |
| 63 | pTrav->pfnMsgCallback(msgFlags, |
| 64 | objectType, srcObject, |
| 65 | location, |
| 66 | msgCode, |
| 67 | pLayerPrefix, |
| 68 | pMsg, |
| 69 | (void *) pTrav->pUserData); |
| 70 | } |
| 71 | pTrav = pTrav->pNext; |
| 72 | } |
| 73 | } |
| 74 | |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 75 | static inline debug_report_data *debug_report_create_instance( |
| 76 | VkLayerInstanceDispatchTable *table, |
| 77 | VkInstance inst, |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 78 | uint32_t extension_count, |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame^] | 79 | const char*const* ppEnabledExtensions) // layer or extension name to be enabled |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 80 | { |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 81 | debug_report_data *debug_data; |
| 82 | PFN_vkGetInstanceProcAddr gpa = table->GetInstanceProcAddr; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 83 | |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 84 | table->DbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) gpa(inst, "vkDbgCreateMsgCallback"); |
| 85 | table->DbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) gpa(inst, "vkDbgDestroyMsgCallback"); |
| 86 | |
| 87 | debug_data = (debug_report_data *) malloc(sizeof(debug_report_data)); |
| 88 | if (!debug_data) return NULL; |
| 89 | |
| 90 | memset(debug_data, 0, sizeof(debug_report_data)); |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 91 | for (uint32_t i = 0; i < extension_count; i++) { |
| 92 | /* TODO: Check other property fields */ |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame^] | 93 | if (strcmp(ppEnabledExtensions[i], DEBUG_REPORT_EXTENSION_NAME) == 0) { |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 94 | debug_data->g_DEBUG_REPORT = true; |
| 95 | } |
| 96 | } |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 97 | return debug_data; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 98 | } |
| 99 | |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 100 | static inline void layer_debug_report_destroy_instance(debug_report_data *debug_data) |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 101 | { |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 102 | VkLayerDbgFunctionNode *pTrav; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 103 | VkLayerDbgFunctionNode *pTravNext; |
| 104 | |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 105 | if (!debug_data) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | pTrav = debug_data->g_pDbgFunctionHead; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 110 | /* Clear out any leftover callbacks */ |
| 111 | while (pTrav) { |
| 112 | pTravNext = pTrav->pNext; |
| 113 | |
| 114 | debug_report_log_msg( |
| 115 | debug_data, VK_DBG_REPORT_WARN_BIT, |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 116 | VK_OBJECT_TYPE_MSG_CALLBACK, pTrav->msgCallback, |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 117 | 0, DEBUG_REPORT_CALLBACK_REF, |
| 118 | "DebugReport", |
| 119 | "Debug Report callbacks not removed before DestroyInstance"); |
| 120 | |
| 121 | free(pTrav); |
| 122 | pTrav = pTravNext; |
| 123 | } |
| 124 | debug_data->g_pDbgFunctionHead = NULL; |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 125 | |
| 126 | free(debug_data); |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 127 | } |
| 128 | |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 129 | static inline debug_report_data *layer_debug_report_create_device( |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 130 | debug_report_data *instance_debug_data, |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 131 | VkDevice device) |
| 132 | { |
Courtney Goeltzenleuchter | 2f25bb4 | 2015-06-14 11:29:24 -0600 | [diff] [blame] | 133 | /* DEBUG_REPORT shares data between Instance and Device, |
| 134 | * so just return instance's data pointer */ |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 135 | return instance_debug_data; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static inline void layer_debug_report_destroy_device(VkDevice device) |
| 139 | { |
Courtney Goeltzenleuchter | 2f25bb4 | 2015-06-14 11:29:24 -0600 | [diff] [blame] | 140 | /* Nothing to do since we're using instance data record */ |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static inline VkResult layer_create_msg_callback( |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 144 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 145 | VkFlags msgFlags, |
| 146 | const PFN_vkDbgMsgCallback pfnMsgCallback, |
| 147 | void *pUserData, |
| 148 | VkDbgMsgCallback *pMsgCallback) |
| 149 | { |
| 150 | VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode*)malloc(sizeof(VkLayerDbgFunctionNode)); |
| 151 | if (!pNewDbgFuncNode) |
| 152 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 153 | |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 154 | pNewDbgFuncNode->msgCallback = *pMsgCallback; |
| 155 | pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback; |
| 156 | pNewDbgFuncNode->msgFlags = msgFlags; |
| 157 | pNewDbgFuncNode->pUserData = pUserData; |
| 158 | pNewDbgFuncNode->pNext = debug_data->g_pDbgFunctionHead; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 159 | |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 160 | debug_data->g_pDbgFunctionHead = pNewDbgFuncNode; |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 161 | debug_data->active_flags |= msgFlags; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 162 | |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 163 | debug_report_log_msg( |
| 164 | debug_data, VK_DBG_REPORT_DEBUG_BIT, |
| 165 | VK_OBJECT_TYPE_MSG_CALLBACK, *pMsgCallback, |
| 166 | 0, DEBUG_REPORT_CALLBACK_REF, |
| 167 | "DebugReport", |
| 168 | "Added callback"); |
| 169 | return VK_SUCCESS; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 170 | } |
| 171 | |
Courtney Goeltzenleuchter | 37d482d | 2015-06-14 11:28:24 -0600 | [diff] [blame] | 172 | static inline void layer_destroy_msg_callback( |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 173 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 174 | VkDbgMsgCallback msg_callback) |
| 175 | { |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 176 | VkLayerDbgFunctionNode *pTrav = debug_data->g_pDbgFunctionHead; |
| 177 | VkLayerDbgFunctionNode *pPrev = pTrav; |
| 178 | |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 179 | debug_data->active_flags = 0; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 180 | while (pTrav) { |
| 181 | if (pTrav->msgCallback == msg_callback) { |
| 182 | pPrev->pNext = pTrav->pNext; |
| 183 | if (debug_data->g_pDbgFunctionHead == pTrav) { |
| 184 | debug_data->g_pDbgFunctionHead = pTrav->pNext; |
| 185 | } |
| 186 | free(pTrav); |
| 187 | debug_report_log_msg( |
| 188 | debug_data, VK_DBG_REPORT_DEBUG_BIT, |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 189 | VK_OBJECT_TYPE_MSG_CALLBACK, pTrav->msgCallback, |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 190 | 0, DEBUG_REPORT_NONE, |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 191 | "DebugReport", |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 192 | "Destroyed callback"); |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 193 | } |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 194 | debug_data->active_flags |= pTrav->msgFlags; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 195 | pPrev = pTrav; |
| 196 | pTrav = pTrav->pNext; |
| 197 | } |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 198 | } |
| 199 | |
Courtney Goeltzenleuchter | 37d482d | 2015-06-14 11:28:24 -0600 | [diff] [blame] | 200 | static inline void* debug_report_get_instance_proc_addr( |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 201 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 202 | const char *funcName) |
| 203 | { |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 204 | if (!debug_data || !debug_data->g_DEBUG_REPORT) { |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | if (!strcmp(funcName, "vkDbgCreateMsgCallback")) { |
| 209 | return (void *) vkDbgCreateMsgCallback; |
| 210 | } |
| 211 | if (!strcmp(funcName, "vkDbgDestroyMsgCallback")) { |
| 212 | return (void *) vkDbgDestroyMsgCallback; |
| 213 | } |
| 214 | |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | /* |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 219 | * Output log message via DEBUG_REPORT |
| 220 | * Takes format and variable arg list so that output string |
| 221 | * is only computed if a message needs to be logged |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 222 | */ |
Courtney Goeltzenleuchter | 37d482d | 2015-06-14 11:28:24 -0600 | [diff] [blame] | 223 | static inline void log_msg( |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 224 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 225 | VkFlags msgFlags, |
| 226 | VkObjectType objectType, |
| 227 | VkObject srcObject, |
| 228 | size_t location, |
| 229 | int32_t msgCode, |
| 230 | const char* pLayerPrefix, |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 231 | const char* format, |
| 232 | ...) |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 233 | { |
Courtney Goeltzenleuchter | 61cb70a | 2015-06-26 15:14:50 -0600 | [diff] [blame] | 234 | if (!debug_data || !(debug_data->active_flags & msgFlags)) { |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 235 | /* message is not wanted */ |
| 236 | return; |
| 237 | } |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 238 | |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 239 | char str[1024]; |
| 240 | va_list argptr; |
| 241 | va_start(argptr, format); |
| 242 | vsnprintf(str, 1024, format, argptr); |
| 243 | va_end(argptr); |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 244 | debug_report_log_msg(debug_data, msgFlags, objectType, |
| 245 | srcObject, location, msgCode, |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 246 | pLayerPrefix, str); |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 247 | } |
| 248 | |
Courtney Goeltzenleuchter | 2f25bb4 | 2015-06-14 11:29:24 -0600 | [diff] [blame] | 249 | static inline void VKAPI log_callback( |
| 250 | VkFlags msgFlags, |
| 251 | VkObjectType objType, |
| 252 | VkObject srcObject, |
| 253 | size_t location, |
| 254 | int32_t msgCode, |
| 255 | const char* pLayerPrefix, |
| 256 | const char* pMsg, |
| 257 | void* pUserData) |
| 258 | { |
| 259 | char msg_flags[30]; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 260 | |
Courtney Goeltzenleuchter | 2f25bb4 | 2015-06-14 11:29:24 -0600 | [diff] [blame] | 261 | print_msg_flags(msgFlags, msg_flags); |
| 262 | |
Courtney Goeltzenleuchter | 95f2c0d | 2015-06-16 17:01:30 -0600 | [diff] [blame] | 263 | fprintf((FILE *) pUserData, "%s(%s): object: 0x%p type: %d location: %zu msgCode: %d: %s\n", |
Courtney Goeltzenleuchter | 2f25bb4 | 2015-06-14 11:29:24 -0600 | [diff] [blame] | 264 | pLayerPrefix, msg_flags, srcObject, objType, location, msgCode, pMsg); |
| 265 | } |
| 266 | #endif // LAYER_LOGGING_H |