Jon Ashburn | d6badbc | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Jon Ashburn | d6badbc | 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 | a8aa837 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 25 | #include <stdbool.h> |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 26 | #include "vkLayer.h" |
Jon Ashburn | d6badbc | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 27 | |
Courtney Goeltzenleuchter | f579fa6 | 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 | a8aa837 | 2015-03-03 15:07:15 -0700 | [diff] [blame] | 31 | static bool g_actionIsDefault = true; |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 32 | static bool g_DEBUG_REPORT = false; |
Jon Ashburn | d6badbc | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 33 | static FILE *g_logFile = NULL; |
| 34 | |
Courtney Goeltzenleuchter | f579fa6 | 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; |
Courtney Goeltzenleuchter | d971b61 | 2015-06-17 20:51:59 -0600 | [diff] [blame^] | 65 | |
| 66 | /* TODO: This should be a per-instance resource */ |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 67 | g_pDbgFunctionHead = pNewDbgFuncNode; |
| 68 | } else { |
| 69 | free(pNewDbgFuncNode); |
| 70 | } |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | static VkResult layer_destroy_msg_callback( |
| 75 | VkInstance instance, |
| 76 | VkLayerInstanceDispatchTable *nextTable, |
| 77 | VkDbgMsgCallback msg_callback) |
| 78 | { |
| 79 | VkLayerDbgFunctionNode *pTrav = g_pDbgFunctionHead; |
| 80 | VkLayerDbgFunctionNode *pPrev = pTrav; |
| 81 | |
| 82 | VkResult result = nextTable->DbgDestroyMsgCallback(instance, msg_callback); |
| 83 | |
| 84 | while (pTrav) { |
| 85 | if (pTrav->msgCallback == msg_callback) { |
| 86 | pPrev->pNext = pTrav->pNext; |
| 87 | if (g_pDbgFunctionHead == pTrav) |
| 88 | g_pDbgFunctionHead = pTrav->pNext; |
| 89 | free(pTrav); |
| 90 | break; |
| 91 | } |
| 92 | pPrev = pTrav; |
| 93 | pTrav = pTrav->pNext; |
| 94 | } |
| 95 | |
| 96 | return result; |
| 97 | } |
| 98 | |
Courtney Goeltzenleuchter | d971b61 | 2015-06-17 20:51:59 -0600 | [diff] [blame^] | 99 | static void* msg_callback_get_proc_addr( |
| 100 | const char *funcName) |
| 101 | { |
| 102 | if (!g_DEBUG_REPORT) { |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | if (!strcmp(funcName, "vkDbgCreateMsgCallback")) { |
| 107 | return (void *) vkDbgCreateMsgCallback; |
| 108 | } |
| 109 | if (!strcmp(funcName, "vkDbgDestroyMsgCallback")) { |
| 110 | return (void *) vkDbgDestroyMsgCallback; |
| 111 | } |
| 112 | |
| 113 | return NULL; |
| 114 | } |
| 115 | |
Jon Ashburn | d6badbc | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 116 | // Utility function to handle reporting |
| 117 | // If callbacks are enabled, use them, otherwise use printf |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 118 | static void layerCbMsg( |
Courtney Goeltzenleuchter | d971b61 | 2015-06-17 20:51:59 -0600 | [diff] [blame^] | 119 | VkFlags msgFlags, |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 120 | VkObjectType objectType, |
| 121 | VkObject srcObject, |
| 122 | size_t location, |
| 123 | int32_t msgCode, |
| 124 | const char* pLayerPrefix, |
| 125 | const char* pMsg) |
Jon Ashburn | d6badbc | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 126 | { |
Courtney Goeltzenleuchter | ab5636c | 2015-02-22 16:15:32 -0700 | [diff] [blame] | 127 | if (g_logFile == NULL) { |
| 128 | g_logFile = stdout; |
| 129 | } |
| 130 | |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 131 | VkLayerDbgFunctionNode *pTrav = g_pDbgFunctionHead; |
| 132 | while (pTrav) { |
| 133 | if (pTrav->msgFlags & msgFlags) { |
| 134 | pTrav->pfnMsgCallback(msgFlags, |
| 135 | objectType, srcObject, |
| 136 | location, |
| 137 | msgCode, |
| 138 | pLayerPrefix, |
| 139 | pMsg, |
| 140 | (void *) pTrav->pUserData); |
| 141 | } |
| 142 | pTrav = pTrav->pNext; |
| 143 | } |
Jon Ashburn | d6badbc | 2015-02-16 08:26:50 -0700 | [diff] [blame] | 144 | } |