Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame^] | 1 | /* |
| 2 | * Vulkan |
| 3 | * |
| 4 | * Copyright (C) 2015 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 <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <assert.h> |
| 27 | #include <unordered_map> |
| 28 | #include "loader_platform.h" |
| 29 | #include "vk_dispatch_table_helper.h" |
| 30 | #include "vkLayer.h" |
| 31 | // The following is #included again to catch certain OS-specific functions |
| 32 | // being used: |
| 33 | #include "loader_platform.h" |
| 34 | |
| 35 | #include "SPIRV/spirv.h" |
| 36 | |
| 37 | static std::unordered_map<void *, VkLayerDispatchTable *> tableMap; |
| 38 | |
| 39 | static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *gpuw) |
| 40 | { |
| 41 | VkLayerDispatchTable *pTable; |
| 42 | |
| 43 | assert(gpuw); |
| 44 | std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) gpuw->baseObject); |
| 45 | if (it == tableMap.end()) |
| 46 | { |
| 47 | pTable = new VkLayerDispatchTable; |
| 48 | tableMap[(void *) gpuw->baseObject] = pTable; |
| 49 | } else |
| 50 | { |
| 51 | return it->second; |
| 52 | } |
| 53 | |
| 54 | layer_initialize_dispatch_table(pTable, gpuw->pGPA, (VkPhysicalGpu) gpuw->nextObject); |
| 55 | |
| 56 | return pTable; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalGpu gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice) |
| 61 | { |
| 62 | VkLayerDispatchTable* pTable = tableMap[gpu]; |
| 63 | VkResult result = pTable->CreateDevice(gpu, pCreateInfo, pDevice); |
| 64 | // create a mapping for the device object into the dispatch table |
| 65 | tableMap.emplace(*pDevice, pTable); |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalGpu gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved) |
| 71 | { |
| 72 | if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pOutLayers[1] == NULL || pReserved == NULL) |
| 73 | return VK_ERROR_INVALID_POINTER; |
| 74 | |
| 75 | if (maxLayerCount < 1) |
| 76 | return VK_ERROR_INITIALIZATION_FAILED; |
| 77 | *pOutLayerCount = 1; |
| 78 | strncpy((char *) pOutLayers[0], "ShaderChecker", maxStringSize); |
| 79 | return VK_SUCCESS; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | struct extProps { |
| 84 | uint32_t version; |
| 85 | const char * const name; |
| 86 | }; |
| 87 | #define SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE 1 |
| 88 | static const struct extProps shaderCheckerExts[SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE] = { |
| 89 | // TODO what is the version? |
| 90 | 0x10, "ShaderChecker", |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo( |
| 95 | VkExtensionInfoType infoType, |
| 96 | uint32_t extensionIndex, |
| 97 | size_t* pDataSize, |
| 98 | void* pData) |
| 99 | { |
| 100 | VkResult result; |
| 101 | |
| 102 | /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */ |
| 103 | VkExtensionProperties *ext_props; |
| 104 | uint32_t *count; |
| 105 | |
| 106 | if (pDataSize == NULL) |
| 107 | return VK_ERROR_INVALID_POINTER; |
| 108 | |
| 109 | switch (infoType) { |
| 110 | case VK_EXTENSION_INFO_TYPE_COUNT: |
| 111 | *pDataSize = sizeof(uint32_t); |
| 112 | if (pData == NULL) |
| 113 | return VK_SUCCESS; |
| 114 | count = (uint32_t *) pData; |
| 115 | *count = SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE; |
| 116 | break; |
| 117 | case VK_EXTENSION_INFO_TYPE_PROPERTIES: |
| 118 | *pDataSize = sizeof(VkExtensionProperties); |
| 119 | if (pData == NULL) |
| 120 | return VK_SUCCESS; |
| 121 | if (extensionIndex >= SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE) |
| 122 | return VK_ERROR_INVALID_VALUE; |
| 123 | ext_props = (VkExtensionProperties *) pData; |
| 124 | ext_props->version = shaderCheckerExts[extensionIndex].version; |
| 125 | strncpy(ext_props->extName, shaderCheckerExts[extensionIndex].name, |
| 126 | VK_MAX_EXTENSION_NAME); |
| 127 | ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0'; |
| 128 | break; |
| 129 | default: |
| 130 | return VK_ERROR_INVALID_VALUE; |
| 131 | }; |
| 132 | |
| 133 | return VK_SUCCESS; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(VkDevice device, const VkShaderCreateInfo *pCreateInfo, |
| 138 | VkShader *pShader) |
| 139 | { |
| 140 | VkLayerDispatchTable* pTable = tableMap[(VkBaseLayerObject *)device]; |
| 141 | VkResult res = pTable->CreateShader(device, pCreateInfo, pShader); |
| 142 | return res; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalGpu gpu, const char* pName) |
| 147 | { |
| 148 | if (gpu == NULL) |
| 149 | return NULL; |
| 150 | |
| 151 | initLayerTable((const VkBaseLayerObject *) gpu); |
| 152 | |
| 153 | #define ADD_HOOK(fn) \ |
| 154 | if (!strncmp(#fn, pName, sizeof(#fn))) \ |
| 155 | return (void *) fn |
| 156 | |
| 157 | ADD_HOOK(vkGetProcAddr); |
| 158 | ADD_HOOK(vkEnumerateLayers); |
| 159 | ADD_HOOK(vkCreateDevice); |
| 160 | ADD_HOOK(vkCreateShader); |
| 161 | |
| 162 | VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu; |
| 163 | if (gpuw->pGPA == NULL) |
| 164 | return NULL; |
| 165 | return gpuw->pGPA((VkPhysicalGpu) gpuw->nextObject, pName); |
| 166 | } |