Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 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 | #include <stdio.h> |
Jon Ashburn | e472239 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 25 | #include <stdbool.h> |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 26 | #include "vkLayer.h" |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 27 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 28 | static VkLayerDbgFunctionNode *g_pDbgFunctionHead = NULL; |
| 29 | static VkFlags g_reportFlags = (VkFlags) 0; |
| 30 | static VkLayerDbgAction g_debugAction = VK_DBG_LAYER_ACTION_LOG_MSG; |
Jon Ashburn | e472239 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 31 | static bool g_actionIsDefault = true; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 32 | static bool g_DEBUG_REPORT = false; |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 33 | static FILE *g_logFile = NULL; |
| 34 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 35 | static void enable_debug_report( |
| 36 | uint32_t extension_count, |
| 37 | const VkExtensionProperties* pEnabledExtensions) // layer or extension name to be enabled |
| 38 | { |
| 39 | for (uint32_t i = 0; i < extension_count; i++) { |
| 40 | /* TODO: Check other property fields */ |
| 41 | if (strcmp(pEnabledExtensions[i].name, DEBUG_REPORT_EXTENSION_NAME) == 0) { |
| 42 | g_DEBUG_REPORT = true; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | static VkResult layer_create_msg_callback( |
| 48 | VkInstance instance, |
| 49 | VkLayerInstanceDispatchTable* nextTable, |
| 50 | VkFlags msgFlags, |
| 51 | const PFN_vkDbgMsgCallback pfnMsgCallback, |
| 52 | void* pUserData, |
| 53 | VkDbgMsgCallback* pMsgCallback) |
| 54 | { |
| 55 | VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode*)malloc(sizeof(VkLayerDbgFunctionNode)); |
| 56 | if (!pNewDbgFuncNode) |
| 57 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 58 | VkResult result = nextTable->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback); |
| 59 | if (result == VK_SUCCESS) { |
| 60 | pNewDbgFuncNode->msgCallback = *pMsgCallback; |
| 61 | pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback; |
| 62 | pNewDbgFuncNode->msgFlags = msgFlags; |
| 63 | pNewDbgFuncNode->pUserData = pUserData; |
| 64 | pNewDbgFuncNode->pNext = g_pDbgFunctionHead; |
| 65 | g_pDbgFunctionHead = pNewDbgFuncNode; |
| 66 | } else { |
| 67 | free(pNewDbgFuncNode); |
| 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | static VkResult layer_destroy_msg_callback( |
| 73 | VkInstance instance, |
| 74 | VkLayerInstanceDispatchTable *nextTable, |
| 75 | VkDbgMsgCallback msg_callback) |
| 76 | { |
| 77 | VkLayerDbgFunctionNode *pTrav = g_pDbgFunctionHead; |
| 78 | VkLayerDbgFunctionNode *pPrev = pTrav; |
| 79 | |
| 80 | VkResult result = nextTable->DbgDestroyMsgCallback(instance, msg_callback); |
| 81 | |
| 82 | while (pTrav) { |
| 83 | if (pTrav->msgCallback == msg_callback) { |
| 84 | pPrev->pNext = pTrav->pNext; |
| 85 | if (g_pDbgFunctionHead == pTrav) |
| 86 | g_pDbgFunctionHead = pTrav->pNext; |
| 87 | free(pTrav); |
| 88 | break; |
| 89 | } |
| 90 | pPrev = pTrav; |
| 91 | pTrav = pTrav->pNext; |
| 92 | } |
| 93 | |
| 94 | return result; |
| 95 | } |
| 96 | |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 97 | // Utility function to handle reporting |
| 98 | // If callbacks are enabled, use them, otherwise use printf |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 99 | static void layerCbMsg( |
| 100 | VkFlags msgFlags, |
| 101 | VkObjectType objectType, |
| 102 | VkObject srcObject, |
| 103 | size_t location, |
| 104 | int32_t msgCode, |
| 105 | const char* pLayerPrefix, |
| 106 | const char* pMsg) |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 107 | { |
Courtney Goeltzenleuchter | d661a82 | 2015-02-22 16:15:32 -0700 | [diff] [blame] | 108 | if (g_logFile == NULL) { |
| 109 | g_logFile = stdout; |
| 110 | } |
| 111 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 112 | VkLayerDbgFunctionNode *pTrav = g_pDbgFunctionHead; |
| 113 | while (pTrav) { |
| 114 | if (pTrav->msgFlags & msgFlags) { |
| 115 | pTrav->pfnMsgCallback(msgFlags, |
| 116 | objectType, srcObject, |
| 117 | location, |
| 118 | msgCode, |
| 119 | pLayerPrefix, |
| 120 | pMsg, |
| 121 | (void *) pTrav->pUserData); |
| 122 | } |
| 123 | pTrav = pTrav->pNext; |
| 124 | } |
| 125 | #if 0 |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 126 | if (g_debugAction & (VK_DBG_LAYER_ACTION_LOG_MSG | VK_DBG_LAYER_ACTION_CALLBACK)) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 127 | if (msgFlags & VK_DBG_REPORT_ERROR_BIT) { |
| 128 | if (g_reportFlags <= VK_DBG_LAYER_LEVEL_ERROR) { |
| 129 | if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG) { |
| 130 | fprintf(g_logFile, "{%s}ERROR : %s\n", pLayerPrefix, pMsg); |
| 131 | fflush(g_logFile); |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 132 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 133 | if (g_debugAction & VK_DBG_LAYER_ACTION_CALLBACK) { |
| 134 | while (pTrav) { |
| 135 | pTrav->pfnMsgCallback(msgFlags, |
| 136 | objectType, srcObject, |
| 137 | location, |
| 138 | msgCode, |
| 139 | pLayerPrefix, |
| 140 | pMsg, |
| 141 | pTrav->pUserData); |
| 142 | pTrav = pTrav->pNext; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | switch (msgType) { |
| 148 | case VK_DBG_REPORT_ERROR_BIT: |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 149 | break; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 150 | case VK_DBG_MSG_WARNING: |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 151 | if (g_reportFlags <= VK_DBG_LAYER_LEVEL_WARN) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 152 | if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG) |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 153 | fprintf(g_logFile, "{%s}WARN : %s\n", pLayerPrefix, pMsg); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 154 | if (g_debugAction & VK_DBG_LAYER_ACTION_CALLBACK) |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 155 | while (pTrav) { |
| 156 | pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData); |
| 157 | pTrav = pTrav->pNext; |
| 158 | } |
| 159 | } |
| 160 | break; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 161 | case VK_DBG_MSG_PERF_WARNING: |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 162 | if (g_reportFlags <= VK_DBG_LAYER_LEVEL_PERF_WARN) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 163 | if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG) |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 164 | fprintf(g_logFile, "{%s}PERF_WARN : %s\n", pLayerPrefix, pMsg); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 165 | if (g_debugAction & VK_DBG_LAYER_ACTION_CALLBACK) |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 166 | while (pTrav) { |
| 167 | pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData); |
| 168 | pTrav = pTrav->pNext; |
| 169 | } |
| 170 | } |
| 171 | break; |
| 172 | default: |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 173 | if (g_reportFlags <= VK_DBG_LAYER_LEVEL_INFO) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 174 | if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG) |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 175 | fprintf(g_logFile, "{%s}INFO : %s\n", pLayerPrefix, pMsg); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 176 | if (g_debugAction & VK_DBG_LAYER_ACTION_CALLBACK) |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 177 | while (pTrav) { |
| 178 | pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData); |
| 179 | pTrav = pTrav->pNext; |
| 180 | } |
| 181 | } |
| 182 | break; |
| 183 | } |
| 184 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame^] | 185 | #endif |
Jon Ashburn | 21001f6 | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 186 | } |