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