Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015-2016 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2016 Valve Corporation |
| 3 | * Copyright (c) 2015-2016 LunarG, Inc. |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 4 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 8 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 10 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 16 | * |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 17 | * Author: Courtney Goeltzenleuchter <courtney@LunarG.com> |
| 18 | * Author: Tobin Ehlis <tobin@lunarg.com> |
| 19 | * |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef LAYER_LOGGING_H |
| 23 | #define LAYER_LOGGING_H |
| 24 | |
Mark Lobodzinski | b87f902 | 2016-05-24 16:04:56 -0600 | [diff] [blame] | 25 | #include "vk_loader_layer.h" |
Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 26 | #include "vk_layer_config.h" |
Tobin Ehlis | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 27 | #include "vk_layer_data.h" |
| 28 | #include "vk_layer_table.h" |
Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 29 | #include "vk_loader_platform.h" |
| 30 | #include "vulkan/vk_layer.h" |
Karl Schultz | d7f3754 | 2016-05-10 11:36:08 -0600 | [diff] [blame] | 31 | #include <cinttypes> |
Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 32 | #include <stdarg.h> |
| 33 | #include <stdbool.h> |
| 34 | #include <stdio.h> |
| 35 | #include <unordered_map> |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 36 | #include <vector> |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 37 | |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 38 | typedef struct _debug_report_data { |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 39 | VkLayerDbgFunctionNode *debug_callback_list; |
| 40 | VkLayerDbgFunctionNode *default_debug_callback_list; |
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 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 45 | template debug_report_data *get_my_data_ptr<debug_report_data>(void *data_key, |
| 46 | std::unordered_map<void *, debug_report_data *> &data_map); |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 47 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 48 | // Forward Declarations |
| 49 | static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags, |
| 50 | VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode, |
| 51 | const char *pLayerPrefix, const char *pMsg); |
| 52 | |
| 53 | // Add a debug message callback node structure to the specified callback linked list |
| 54 | static inline void AddDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head, |
| 55 | VkLayerDbgFunctionNode *new_node) { |
| 56 | |
| 57 | new_node->pNext = *list_head; |
| 58 | *list_head = new_node; |
| 59 | } |
| 60 | |
| 61 | // Remove specified debug message callback node structure from the specified callback linked list |
| 62 | static inline void RemoveDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head, |
| 63 | VkDebugReportCallbackEXT callback) { |
| 64 | VkLayerDbgFunctionNode *cur_callback = *list_head; |
| 65 | VkLayerDbgFunctionNode *prev_callback = cur_callback; |
| 66 | bool matched = false; |
Mark Lobodzinski | 05b04cd | 2016-11-10 09:12:57 -0700 | [diff] [blame] | 67 | VkFlags local_flags = 0; |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 68 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 69 | while (cur_callback) { |
| 70 | if (cur_callback->msgCallback == callback) { |
| 71 | matched = true; |
| 72 | prev_callback->pNext = cur_callback->pNext; |
| 73 | if (*list_head == cur_callback) { |
| 74 | *list_head = cur_callback->pNext; |
| 75 | } |
| 76 | debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT, |
| 77 | reinterpret_cast<uint64_t &>(cur_callback->msgCallback), 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, |
Mark Lobodzinski | 05b04cd | 2016-11-10 09:12:57 -0700 | [diff] [blame] | 78 | "DebugReport", "Destroyed callback\n"); |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 79 | } else { |
| 80 | matched = false; |
Mark Lobodzinski | 05b04cd | 2016-11-10 09:12:57 -0700 | [diff] [blame] | 81 | local_flags |= cur_callback->msgFlags; |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 82 | } |
| 83 | prev_callback = cur_callback; |
| 84 | cur_callback = cur_callback->pNext; |
| 85 | if (matched) { |
| 86 | free(prev_callback); |
| 87 | } |
| 88 | } |
Mark Lobodzinski | 05b04cd | 2016-11-10 09:12:57 -0700 | [diff] [blame] | 89 | debug_data->active_flags = local_flags; |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Removes all debug callback function nodes from the specified callback linked lists and frees their resources |
| 93 | static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) { |
| 94 | VkLayerDbgFunctionNode *current_callback = *list_head; |
| 95 | VkLayerDbgFunctionNode *prev_callback = current_callback; |
| 96 | |
| 97 | while (current_callback) { |
| 98 | prev_callback = current_callback->pNext; |
| 99 | debug_report_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT, |
| 100 | (uint64_t)current_callback->msgCallback, 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, "DebugReport", |
| 101 | "Debug Report callbacks not removed before DestroyInstance"); |
| 102 | free(current_callback); |
| 103 | current_callback = prev_callback; |
| 104 | } |
| 105 | *list_head = NULL; |
| 106 | } |
| 107 | |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 108 | // Utility function to handle reporting |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 109 | static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags, |
| 110 | VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode, |
| 111 | const char *pLayerPrefix, const char *pMsg) { |
Dustin Graves | 8f1eab9 | 2016-04-05 09:41:17 -0600 | [diff] [blame] | 112 | bool bail = false; |
Mark Lobodzinski | 55a197f | 2016-06-21 15:54:57 -0600 | [diff] [blame] | 113 | VkLayerDbgFunctionNode *pTrav = NULL; |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 114 | |
Mark Lobodzinski | 55a197f | 2016-06-21 15:54:57 -0600 | [diff] [blame] | 115 | if (debug_data->debug_callback_list != NULL) { |
| 116 | pTrav = debug_data->debug_callback_list; |
| 117 | } else { |
| 118 | pTrav = debug_data->default_debug_callback_list; |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 119 | } |
| 120 | |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 121 | while (pTrav) { |
| 122 | if (pTrav->msgFlags & msgFlags) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 123 | if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, pMsg, pTrav->pUserData)) { |
Courtney Goeltzenleuchter | 0664083 | 2015-09-04 13:52:24 -0600 | [diff] [blame] | 124 | bail = true; |
| 125 | } |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 126 | } |
| 127 | pTrav = pTrav->pNext; |
| 128 | } |
Courtney Goeltzenleuchter | 0664083 | 2015-09-04 13:52:24 -0600 | [diff] [blame] | 129 | |
| 130 | return bail; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 131 | } |
| 132 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 133 | static inline debug_report_data * |
| 134 | debug_report_create_instance(VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count, |
| 135 | const char *const *ppEnabledExtensions) // layer or extension name to be enabled |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 136 | { |
Mark Young | aa1aa3a | 2016-07-05 16:41:50 -0600 | [diff] [blame] | 137 | debug_report_data *debug_data = (debug_report_data *)malloc(sizeof(debug_report_data)); |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 138 | if (!debug_data) |
| 139 | return NULL; |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 140 | |
| 141 | memset(debug_data, 0, sizeof(debug_report_data)); |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 142 | for (uint32_t i = 0; i < extension_count; i++) { |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 143 | // TODO: Check other property fields |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 144 | if (strcmp(ppEnabledExtensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) { |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 145 | debug_data->g_DEBUG_REPORT = true; |
| 146 | } |
| 147 | } |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 148 | return debug_data; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 149 | } |
| 150 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 151 | static inline void layer_debug_report_destroy_instance(debug_report_data *debug_data) { |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 152 | if (debug_data) { |
| 153 | RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list); |
| 154 | RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list); |
| 155 | free(debug_data); |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 156 | } |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 157 | } |
| 158 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 159 | static inline debug_report_data *layer_debug_report_create_device(debug_report_data *instance_debug_data, VkDevice device) { |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 160 | // DEBUG_REPORT shares data between Instance and Device, |
| 161 | // so just return instance's data pointer |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 162 | return instance_debug_data; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 163 | } |
| 164 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 165 | static inline void layer_debug_report_destroy_device(VkDevice device) { |
| 166 | // Nothing to do since we're using instance data record |
| 167 | } |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 168 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 169 | static inline void layer_destroy_msg_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback, |
| 170 | const VkAllocationCallbacks *pAllocator) { |
| 171 | RemoveDebugMessageCallback(debug_data, &debug_data->debug_callback_list, callback); |
| 172 | RemoveDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback); |
| 173 | } |
| 174 | |
| 175 | static inline VkResult layer_create_msg_callback(debug_report_data *debug_data, bool default_callback, |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 176 | const VkDebugReportCallbackCreateInfoEXT *pCreateInfo, |
| 177 | const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pCallback) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 178 | VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode)); |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 179 | if (!pNewDbgFuncNode) |
| 180 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 181 | |
Tobin Ehlis | eb7715d | 2015-09-21 09:36:47 -0600 | [diff] [blame] | 182 | // Handle of 0 is logging_callback so use allocated Node address as unique handle |
Courtney Goeltzenleuchter | 05854bf | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 183 | if (!(*pCallback)) |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 184 | *pCallback = (VkDebugReportCallbackEXT)pNewDbgFuncNode; |
Courtney Goeltzenleuchter | 05854bf | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 185 | pNewDbgFuncNode->msgCallback = *pCallback; |
| 186 | pNewDbgFuncNode->pfnMsgCallback = pCreateInfo->pfnCallback; |
| 187 | pNewDbgFuncNode->msgFlags = pCreateInfo->flags; |
| 188 | pNewDbgFuncNode->pUserData = pCreateInfo->pUserData; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 189 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 190 | if (default_callback) { |
| 191 | AddDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode); |
Mark Lobodzinski | 2134fc3 | 2016-11-10 09:10:08 -0700 | [diff] [blame] | 192 | debug_data->active_flags |= pCreateInfo->flags; |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 193 | } else { |
| 194 | AddDebugMessageCallback(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode); |
Mark Lobodzinski | 2134fc3 | 2016-11-10 09:10:08 -0700 | [diff] [blame] | 195 | debug_data->active_flags = pCreateInfo->flags; |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 196 | } |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 197 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 198 | debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT, |
| 199 | (uint64_t)*pCallback, 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, "DebugReport", "Added callback"); |
Courtney Goeltzenleuchter | e45acec | 2015-06-14 12:03:26 -0600 | [diff] [blame] | 200 | return VK_SUCCESS; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 203 | static inline PFN_vkVoidFunction debug_report_get_instance_proc_addr(debug_report_data *debug_data, const char *funcName) { |
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 | |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 208 | if (!strcmp(funcName, "vkCreateDebugReportCallbackEXT")) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 209 | return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 210 | } |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 211 | if (!strcmp(funcName, "vkDestroyDebugReportCallbackEXT")) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 212 | return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 213 | } |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 214 | if (!strcmp(funcName, "vkDebugReportMessageEXT")) { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 215 | return (PFN_vkVoidFunction)vkDebugReportMessageEXT; |
Courtney Goeltzenleuchter | 822e8d7 | 2015-11-30 15:28:25 -0700 | [diff] [blame] | 216 | } |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 217 | return NULL; |
| 218 | } |
| 219 | |
Ian Elliott | ed6b5ac | 2016-04-28 09:08:13 -0600 | [diff] [blame] | 220 | // This utility (called at vkCreateInstance() time), looks at a pNext chain. |
| 221 | // It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It |
| 222 | // then allocates an array that can hold that many structs, as well as that |
| 223 | // many VkDebugReportCallbackEXT handles. It then copies each |
| 224 | // VkDebugReportCallbackCreateInfoEXT, and initializes each handle. |
| 225 | static VkResult layer_copy_tmp_callbacks(const void *pChain, uint32_t *num_callbacks, VkDebugReportCallbackCreateInfoEXT **infos, |
| 226 | VkDebugReportCallbackEXT **callbacks) { |
| 227 | uint32_t n = *num_callbacks = 0; |
| 228 | |
| 229 | const void *pNext = pChain; |
| 230 | while (pNext) { |
| 231 | // 1st, count the number VkDebugReportCallbackCreateInfoEXT: |
| 232 | if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) { |
| 233 | n++; |
| 234 | } |
| 235 | pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext; |
| 236 | } |
| 237 | if (n == 0) { |
| 238 | return VK_SUCCESS; |
| 239 | } |
| 240 | |
| 241 | // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT: |
| 242 | VkDebugReportCallbackCreateInfoEXT *pInfos = *infos = |
| 243 | ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT))); |
| 244 | if (!pInfos) { |
| 245 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 246 | } |
| 247 | // 3rd, allocate memory for a unique handle for each callback: |
| 248 | VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT))); |
| 249 | if (!pCallbacks) { |
| 250 | free(pInfos); |
| 251 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 252 | } |
| 253 | // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by |
| 254 | // vkDestroyInstance, and assign a unique handle to each callback (just |
| 255 | // use the address of the copied VkDebugReportCallbackCreateInfoEXT): |
| 256 | pNext = pChain; |
| 257 | while (pNext) { |
| 258 | if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) { |
| 259 | memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT)); |
| 260 | *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++; |
| 261 | } |
| 262 | pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext; |
| 263 | } |
| 264 | |
| 265 | *num_callbacks = n; |
| 266 | return VK_SUCCESS; |
| 267 | } |
| 268 | |
| 269 | // This utility frees the arrays allocated by layer_copy_tmp_callbacks() |
| 270 | static void layer_free_tmp_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) { |
| 271 | free(infos); |
| 272 | free(callbacks); |
| 273 | } |
| 274 | |
| 275 | // This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs |
| 276 | // that were copied by layer_copy_tmp_callbacks() |
| 277 | static VkResult layer_enable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks, |
| 278 | VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) { |
| 279 | VkResult rtn = VK_SUCCESS; |
| 280 | for (uint32_t i = 0; i < num_callbacks; i++) { |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 281 | rtn = layer_create_msg_callback(debug_data, false, &infos[i], NULL, &callbacks[i]); |
Ian Elliott | ed6b5ac | 2016-04-28 09:08:13 -0600 | [diff] [blame] | 282 | if (rtn != VK_SUCCESS) { |
| 283 | for (uint32_t j = 0; j < i; j++) { |
| 284 | layer_destroy_msg_callback(debug_data, callbacks[j], NULL); |
| 285 | } |
| 286 | return rtn; |
| 287 | } |
| 288 | } |
| 289 | return rtn; |
| 290 | } |
| 291 | |
| 292 | // This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs |
| 293 | // that were copied by layer_copy_tmp_callbacks() |
| 294 | static void layer_disable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks, |
| 295 | VkDebugReportCallbackEXT *callbacks) { |
| 296 | for (uint32_t i = 0; i < num_callbacks; i++) { |
| 297 | layer_destroy_msg_callback(debug_data, callbacks[i], NULL); |
| 298 | } |
| 299 | } |
| 300 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 301 | // Checks if the message will get logged. |
| 302 | // Allows layer to defer collecting & formating data if the |
| 303 | // message will be discarded. |
Dustin Graves | 8f1eab9 | 2016-04-05 09:41:17 -0600 | [diff] [blame] | 304 | static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msgFlags) { |
Courtney Goeltzenleuchter | 6a564a1 | 2015-09-18 16:30:24 -0600 | [diff] [blame] | 305 | if (!debug_data || !(debug_data->active_flags & msgFlags)) { |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 306 | // Message is not wanted |
Courtney Goeltzenleuchter | 6a564a1 | 2015-09-18 16:30:24 -0600 | [diff] [blame] | 307 | return false; |
| 308 | } |
| 309 | |
| 310 | return true; |
| 311 | } |
| 312 | |
Chris Forbes | 8a25bce | 2016-03-24 12:06:35 +1300 | [diff] [blame] | 313 | #ifdef WIN32 |
| 314 | static inline int vasprintf(char **strp, char const *fmt, va_list ap) { |
| 315 | *strp = nullptr; |
| 316 | int size = _vscprintf(fmt, ap); |
| 317 | if (size >= 0) { |
| 318 | *strp = (char *)malloc(size+1); |
| 319 | if (!*strp) { |
| 320 | return -1; |
| 321 | } |
| 322 | _vsnprintf(*strp, size+1, fmt, ap); |
| 323 | } |
| 324 | return size; |
| 325 | } |
| 326 | #endif |
| 327 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 328 | // Output log message via DEBUG_REPORT |
| 329 | // Takes format and variable arg list so that output string |
| 330 | // is only computed if a message needs to be logged |
Michael Lentine | 010f469 | 2015-11-03 16:19:46 -0800 | [diff] [blame] | 331 | #ifndef WIN32 |
Tobin Ehlis | 17978d8 | 2016-05-17 07:58:01 -0600 | [diff] [blame] | 332 | static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType, |
Dustin Graves | 8f1eab9 | 2016-04-05 09:41:17 -0600 | [diff] [blame] | 333 | uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format, ...) |
| 334 | __attribute__((format(printf, 8, 9))); |
Michael Lentine | 010f469 | 2015-11-03 16:19:46 -0800 | [diff] [blame] | 335 | #endif |
Tobin Ehlis | 17978d8 | 2016-05-17 07:58:01 -0600 | [diff] [blame] | 336 | static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType, |
Dustin Graves | 8f1eab9 | 2016-04-05 09:41:17 -0600 | [diff] [blame] | 337 | uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format, |
| 338 | ...) { |
Courtney Goeltzenleuchter | 61cb70a | 2015-06-26 15:14:50 -0600 | [diff] [blame] | 339 | if (!debug_data || !(debug_data->active_flags & msgFlags)) { |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 340 | // Message is not wanted |
Courtney Goeltzenleuchter | 0664083 | 2015-09-04 13:52:24 -0600 | [diff] [blame] | 341 | return false; |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 342 | } |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 343 | |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 344 | va_list argptr; |
| 345 | va_start(argptr, format); |
Chris Forbes | 8a25bce | 2016-03-24 12:06:35 +1300 | [diff] [blame] | 346 | char *str; |
Chris Forbes | ed6a1b3 | 2016-04-28 14:27:19 +1200 | [diff] [blame] | 347 | if (-1 == vasprintf(&str, format, argptr)) { |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 348 | // On failure, glibc vasprintf leaves str undefined |
Chris Forbes | ed6a1b3 | 2016-04-28 14:27:19 +1200 | [diff] [blame] | 349 | str = nullptr; |
| 350 | } |
Courtney Goeltzenleuchter | b9f0bf3 | 2015-06-14 09:50:18 -0600 | [diff] [blame] | 351 | va_end(argptr); |
Chris Forbes | ed6a1b3 | 2016-04-28 14:27:19 +1200 | [diff] [blame] | 352 | bool result = debug_report_log_msg(debug_data, msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, |
| 353 | str ? str : "Allocation failure"); |
Chris Forbes | 8a25bce | 2016-03-24 12:06:35 +1300 | [diff] [blame] | 354 | free(str); |
| 355 | return result; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 356 | } |
| 357 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 358 | static inline VKAPI_ATTR VkBool32 VKAPI_CALL log_callback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, |
| 359 | size_t location, int32_t msgCode, const char *pLayerPrefix, |
| 360 | const char *pMsg, void *pUserData) { |
Courtney Goeltzenleuchter | 2f25bb4 | 2015-06-14 11:29:24 -0600 | [diff] [blame] | 361 | char msg_flags[30]; |
Courtney Goeltzenleuchter | 7d8503b | 2015-06-11 15:58:51 -0600 | [diff] [blame] | 362 | |
Courtney Goeltzenleuchter | 2f25bb4 | 2015-06-14 11:29:24 -0600 | [diff] [blame] | 363 | print_msg_flags(msgFlags, msg_flags); |
| 364 | |
Mark Mueller | aab3650 | 2016-05-03 13:17:29 -0600 | [diff] [blame] | 365 | fprintf((FILE *)pUserData, "%s(%s): object: 0x%" PRIx64 " type: %d location: %lu msgCode: %d: %s\n", pLayerPrefix, msg_flags, |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 366 | srcObject, objType, (unsigned long)location, msgCode, pMsg); |
| 367 | fflush((FILE *)pUserData); |
Courtney Goeltzenleuchter | 0664083 | 2015-09-04 13:52:24 -0600 | [diff] [blame] | 368 | |
| 369 | return false; |
Courtney Goeltzenleuchter | 2f25bb4 | 2015-06-14 11:29:24 -0600 | [diff] [blame] | 370 | } |
Courtney Goeltzenleuchter | 5907ac4 | 2015-10-05 14:41:34 -0600 | [diff] [blame] | 371 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 372 | static inline VKAPI_ATTR VkBool32 VKAPI_CALL win32_debug_output_msg(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, |
| 373 | uint64_t srcObject, size_t location, int32_t msgCode, |
| 374 | const char *pLayerPrefix, const char *pMsg, void *pUserData) { |
Courtney Goeltzenleuchter | 5907ac4 | 2015-10-05 14:41:34 -0600 | [diff] [blame] | 375 | #ifdef WIN32 |
| 376 | char msg_flags[30]; |
| 377 | char buf[2048]; |
| 378 | |
| 379 | print_msg_flags(msgFlags, msg_flags); |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 380 | _snprintf(buf, sizeof(buf) - 1, |
| 381 | "%s (%s): object: 0x%" PRIxPTR " type: %d location: " PRINTF_SIZE_T_SPECIFIER " msgCode: %d: %s\n", pLayerPrefix, |
| 382 | msg_flags, (size_t)srcObject, objType, location, msgCode, pMsg); |
Courtney Goeltzenleuchter | 5907ac4 | 2015-10-05 14:41:34 -0600 | [diff] [blame] | 383 | |
| 384 | OutputDebugString(buf); |
| 385 | #endif |
| 386 | |
| 387 | return false; |
| 388 | } |
| 389 | |
Courtney Goeltzenleuchter | 2f25bb4 | 2015-06-14 11:29:24 -0600 | [diff] [blame] | 390 | #endif // LAYER_LOGGING_H |