Courtney Goeltzenleuchter | 41fa5b0 | 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> |
| 32 | #include <stdbool.h> |
| 33 | #include <unordered_map> |
| 34 | #include "vkLayer.h" |
Courtney Goeltzenleuchter | f1eb249 | 2015-06-11 16:01:11 -0600 | [diff] [blame] | 35 | #include "layer_data.h" |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 36 | #include "layers_table.h" |
| 37 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 38 | typedef struct _debug_report_data { |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 39 | VkLayerDbgFunctionNode *g_pDbgFunctionHead; |
| 40 | bool g_DEBUG_REPORT; |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 41 | } debug_report_data; |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 42 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 43 | template debug_report_data *get_my_data_ptr<debug_report_data>( |
Courtney Goeltzenleuchter | f1eb249 | 2015-06-11 16:01:11 -0600 | [diff] [blame] | 44 | void *data_key, |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 45 | std::unordered_map<void *, debug_report_data *> &data_map); |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 46 | |
| 47 | // Utility function to handle reporting |
| 48 | static void debug_report_log_msg( |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 49 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 50 | VkFlags msgFlags, |
| 51 | VkObjectType objectType, |
| 52 | VkObject srcObject, |
| 53 | size_t location, |
| 54 | int32_t msgCode, |
| 55 | const char* pLayerPrefix, |
| 56 | const char* pMsg) |
| 57 | { |
| 58 | VkLayerDbgFunctionNode *pTrav = debug_data->g_pDbgFunctionHead; |
| 59 | while (pTrav) { |
| 60 | if (pTrav->msgFlags & msgFlags) { |
| 61 | pTrav->pfnMsgCallback(msgFlags, |
| 62 | objectType, srcObject, |
| 63 | location, |
| 64 | msgCode, |
| 65 | pLayerPrefix, |
| 66 | pMsg, |
| 67 | (void *) pTrav->pUserData); |
| 68 | } |
| 69 | pTrav = pTrav->pNext; |
| 70 | } |
| 71 | } |
| 72 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 73 | static inline debug_report_data *debug_report_create_instance( |
| 74 | VkLayerInstanceDispatchTable *table, |
| 75 | VkInstance inst, |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 76 | uint32_t extension_count, |
| 77 | const VkExtensionProperties* pEnabledExtensions) // layer or extension name to be enabled |
| 78 | { |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 79 | debug_report_data *debug_data; |
| 80 | PFN_vkGetInstanceProcAddr gpa = table->GetInstanceProcAddr; |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 81 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 82 | table->DbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) gpa(inst, "vkDbgCreateMsgCallback"); |
| 83 | table->DbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) gpa(inst, "vkDbgDestroyMsgCallback"); |
| 84 | |
| 85 | debug_data = (debug_report_data *) malloc(sizeof(debug_report_data)); |
| 86 | if (!debug_data) return NULL; |
| 87 | |
| 88 | memset(debug_data, 0, sizeof(debug_report_data)); |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 89 | for (uint32_t i = 0; i < extension_count; i++) { |
| 90 | /* TODO: Check other property fields */ |
| 91 | if (strcmp(pEnabledExtensions[i].name, DEBUG_REPORT_EXTENSION_NAME) == 0) { |
| 92 | debug_data->g_DEBUG_REPORT = true; |
| 93 | } |
| 94 | } |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 95 | return debug_data; |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 96 | } |
| 97 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 98 | static inline void layer_debug_report_destroy_instance(debug_report_data *debug_data) |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 99 | { |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 100 | VkLayerDbgFunctionNode *pTrav = debug_data->g_pDbgFunctionHead; |
| 101 | VkLayerDbgFunctionNode *pTravNext; |
| 102 | |
| 103 | /* Clear out any leftover callbacks */ |
| 104 | while (pTrav) { |
| 105 | pTravNext = pTrav->pNext; |
| 106 | |
| 107 | debug_report_log_msg( |
| 108 | debug_data, VK_DBG_REPORT_WARN_BIT, |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 109 | VK_OBJECT_TYPE_MSG_CALLBACK, pTrav->msgCallback, |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 110 | 0, DEBUG_REPORT_CALLBACK_REF, |
| 111 | "DebugReport", |
| 112 | "Debug Report callbacks not removed before DestroyInstance"); |
| 113 | |
| 114 | free(pTrav); |
| 115 | pTrav = pTravNext; |
| 116 | } |
| 117 | debug_data->g_pDbgFunctionHead = NULL; |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 118 | |
| 119 | free(debug_data); |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 120 | } |
| 121 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 122 | static inline debug_report_data *layer_debug_report_create_device( |
| 123 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 124 | VkDevice device) |
| 125 | { |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 126 | return debug_data; |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static inline void layer_debug_report_destroy_device(VkDevice device) |
| 130 | { |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static inline VkResult layer_create_msg_callback( |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 134 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 135 | VkFlags msgFlags, |
| 136 | const PFN_vkDbgMsgCallback pfnMsgCallback, |
| 137 | void *pUserData, |
| 138 | VkDbgMsgCallback *pMsgCallback) |
| 139 | { |
| 140 | VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode*)malloc(sizeof(VkLayerDbgFunctionNode)); |
| 141 | if (!pNewDbgFuncNode) |
| 142 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 143 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 144 | pNewDbgFuncNode->msgCallback = *pMsgCallback; |
| 145 | pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback; |
| 146 | pNewDbgFuncNode->msgFlags = msgFlags; |
| 147 | pNewDbgFuncNode->pUserData = pUserData; |
| 148 | pNewDbgFuncNode->pNext = debug_data->g_pDbgFunctionHead; |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 149 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 150 | debug_data->g_pDbgFunctionHead = pNewDbgFuncNode; |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 151 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 152 | debug_report_log_msg( |
| 153 | debug_data, VK_DBG_REPORT_DEBUG_BIT, |
| 154 | VK_OBJECT_TYPE_MSG_CALLBACK, *pMsgCallback, |
| 155 | 0, DEBUG_REPORT_CALLBACK_REF, |
| 156 | "DebugReport", |
| 157 | "Added callback"); |
| 158 | return VK_SUCCESS; |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 159 | } |
| 160 | |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 161 | static void layer_destroy_msg_callback( |
| 162 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 163 | VkDbgMsgCallback msg_callback) |
| 164 | { |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 165 | VkLayerDbgFunctionNode *pTrav = debug_data->g_pDbgFunctionHead; |
| 166 | VkLayerDbgFunctionNode *pPrev = pTrav; |
| 167 | |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 168 | while (pTrav) { |
| 169 | if (pTrav->msgCallback == msg_callback) { |
| 170 | pPrev->pNext = pTrav->pNext; |
| 171 | if (debug_data->g_pDbgFunctionHead == pTrav) { |
| 172 | debug_data->g_pDbgFunctionHead = pTrav->pNext; |
| 173 | } |
| 174 | free(pTrav); |
| 175 | debug_report_log_msg( |
| 176 | debug_data, VK_DBG_REPORT_DEBUG_BIT, |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 177 | VK_OBJECT_TYPE_MSG_CALLBACK, pTrav->msgCallback, |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 178 | 0, DEBUG_REPORT_CALLBACK_REF, |
| 179 | "DebugReport", |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 180 | "Destroyed callback"); |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | pPrev = pTrav; |
| 184 | pTrav = pTrav->pNext; |
| 185 | } |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void* debug_report_get_instance_proc_addr( |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 189 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 190 | const char *funcName) |
| 191 | { |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 192 | if (!debug_data || !debug_data->g_DEBUG_REPORT) { |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | if (!strcmp(funcName, "vkDbgCreateMsgCallback")) { |
| 197 | return (void *) vkDbgCreateMsgCallback; |
| 198 | } |
| 199 | if (!strcmp(funcName, "vkDbgDestroyMsgCallback")) { |
| 200 | return (void *) vkDbgDestroyMsgCallback; |
| 201 | } |
| 202 | |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Devices, Queue, SwapChain and Command buffers all |
| 208 | * use the same device dispatch table. |
| 209 | */ |
| 210 | static void device_log_msg( |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 211 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 212 | VkFlags msgFlags, |
| 213 | VkObjectType objectType, |
| 214 | VkObject srcObject, |
| 215 | size_t location, |
| 216 | int32_t msgCode, |
| 217 | const char* pLayerPrefix, |
| 218 | const char* pMsg) |
| 219 | { |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 220 | debug_report_log_msg(debug_data, msgFlags, objectType, |
| 221 | srcObject, location, msgCode, |
| 222 | pLayerPrefix, pMsg); |
| 223 | } |
| 224 | |
| 225 | static void instance_log_msg( |
Courtney Goeltzenleuchter | 1f1fbbd | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 226 | debug_report_data *debug_data, |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 227 | VkFlags msgFlags, |
| 228 | VkObjectType objectType, |
| 229 | VkObject srcObject, |
| 230 | size_t location, |
| 231 | int32_t msgCode, |
| 232 | const char* pLayerPrefix, |
| 233 | const char* pMsg) |
| 234 | { |
Courtney Goeltzenleuchter | 41fa5b0 | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 235 | debug_report_log_msg(debug_data, msgFlags, objectType, |
| 236 | srcObject, location, msgCode, |
| 237 | pLayerPrefix, pMsg); |
| 238 | } |
| 239 | |
| 240 | #endif // LAYER_LOGGING_H |
| 241 | |