Jon Ashburn | 8d8dad0 | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Jon Ashburn | 8d8dad0 | 2014-12-01 14:22:40 -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 | */ |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <assert.h> |
| 27 | #include <unordered_map> |
Ian Elliott | 81ac44c | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 28 | #include "loader_platform.h" |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 29 | #include "vk_dispatch_table_helper.h" |
| 30 | #include "vkLayer.h" |
Ian Elliott | 20f0687 | 2015-02-12 17:08:34 -0700 | [diff] [blame] | 31 | // The following is #included again to catch certain OS-specific functions |
| 32 | // being used: |
| 33 | #include "loader_platform.h" |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 34 | |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 35 | static std::unordered_map<void *, VkLayerDispatchTable *> tableMap; |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 36 | |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 37 | static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *gpuw) |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 38 | { |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 39 | VkLayerDispatchTable *pTable; |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 40 | |
| 41 | assert(gpuw); |
Jon Ashburn | 630e44f | 2015-04-08 21:33:34 -0600 | [diff] [blame^] | 42 | std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) gpuw->baseObject); |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 43 | if (it == tableMap.end()) |
| 44 | { |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 45 | pTable = new VkLayerDispatchTable; |
Jon Ashburn | 630e44f | 2015-04-08 21:33:34 -0600 | [diff] [blame^] | 46 | tableMap[(void *) gpuw->baseObject] = pTable; |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 47 | } else |
| 48 | { |
| 49 | return it->second; |
| 50 | } |
Chia-I Wu | 0f65b1e | 2015-01-04 23:11:43 +0800 | [diff] [blame] | 51 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 52 | layer_initialize_dispatch_table(pTable, gpuw->pGPA, (VkPhysicalGpu) gpuw->nextObject); |
Chia-I Wu | 0f65b1e | 2015-01-04 23:11:43 +0800 | [diff] [blame] | 53 | |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 54 | return pTable; |
| 55 | } |
| 56 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 57 | VK_LAYER_EXPORT VkResult VKAPI vkLayerExtension1(VkDevice device) |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 58 | { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 59 | printf("In vkLayerExtension1() call w/ device: %p\n", (void*)device); |
| 60 | printf("vkLayerExtension1 returning SUCCESS\n"); |
| 61 | return VK_SUCCESS; |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 64 | VK_LAYER_EXPORT VkResult VKAPI vkGetExtensionSupport(VkPhysicalGpu gpu, const char* pExtName) |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 65 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 66 | VkResult result; |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 67 | |
Jon Ashburn | 2556635 | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 68 | /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */ |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 69 | if (!strncmp(pExtName, "vkLayerExtension1", strlen("vkLayerExtension1"))) |
Jon Ashburn | 2556635 | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 70 | { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 71 | result = VK_SUCCESS; |
Jon Ashburn | 2556635 | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 72 | } else if (!strncmp(pExtName, "Basic", strlen("Basic"))) |
| 73 | { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 74 | result = VK_SUCCESS; |
Jon Ashburn | 630e44f | 2015-04-08 21:33:34 -0600 | [diff] [blame^] | 75 | } else if (!tableMap.empty() && (tableMap.find(gpu) != tableMap.end())) |
Jon Ashburn | 2556635 | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 76 | { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 77 | printf("At start of wrapped vkGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu); |
Jon Ashburn | 630e44f | 2015-04-08 21:33:34 -0600 | [diff] [blame^] | 78 | VkLayerDispatchTable* pTable = tableMap[gpu]; |
| 79 | result = pTable->GetExtensionSupport(gpu, pExtName); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 80 | printf("Completed wrapped vkGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu); |
Jon Ashburn | 2556635 | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 81 | } else |
| 82 | { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 83 | result = VK_ERROR_INVALID_EXTENSION; |
Jon Ashburn | 2556635 | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 84 | } |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 85 | return result; |
| 86 | } |
| 87 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 88 | VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalGpu gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice) |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 89 | { |
Jon Ashburn | 630e44f | 2015-04-08 21:33:34 -0600 | [diff] [blame^] | 90 | VkLayerDispatchTable* pTable = tableMap[gpu]; |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 91 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 92 | printf("At start of wrapped vkCreateDevice() call w/ gpu: %p\n", (void*)gpu); |
Jon Ashburn | 630e44f | 2015-04-08 21:33:34 -0600 | [diff] [blame^] | 93 | VkResult result = pTable->CreateDevice(gpu, pCreateInfo, pDevice); |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 94 | // create a mapping for the device object into the dispatch table |
| 95 | tableMap.emplace(*pDevice, pTable); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 96 | printf("Completed wrapped vkCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice); |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 97 | return result; |
| 98 | } |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 99 | VK_LAYER_EXPORT VkResult VKAPI vkGetFormatInfo(VkDevice device, VkFormat format, VkFormatInfoType infoType, size_t* pDataSize, void* pData) |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 100 | { |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 101 | VkLayerDispatchTable* pTable = tableMap[device]; |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 102 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 103 | printf("At start of wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device); |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 104 | VkResult result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 105 | printf("Completed wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device); |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 106 | return result; |
| 107 | } |
| 108 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 109 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalGpu gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved) |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 110 | { |
| 111 | if (gpu != NULL) |
| 112 | { |
Jon Ashburn | 630e44f | 2015-04-08 21:33:34 -0600 | [diff] [blame^] | 113 | VkLayerDispatchTable* pTable = initLayerTable((const VkBaseLayerObject *) gpu); |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 114 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 115 | printf("At start of wrapped vkEnumerateLayers() call w/ gpu: %p\n", gpu); |
Jon Ashburn | 630e44f | 2015-04-08 21:33:34 -0600 | [diff] [blame^] | 116 | VkResult result = pTable->EnumerateLayers(gpu, maxLayerCount, maxStringSize, pOutLayerCount, pOutLayers, pReserved); |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 117 | return result; |
| 118 | } else |
| 119 | { |
| 120 | if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pReserved == NULL) |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 121 | return VK_ERROR_INVALID_POINTER; |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 122 | |
| 123 | // Example of a layer that is only compatible with Intel's GPUs |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 124 | VkBaseLayerObject* gpuw = (VkBaseLayerObject*) pReserved; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 125 | PFN_vkGetGpuInfo fpGetGpuInfo; |
| 126 | VkPhysicalGpuProperties gpuProps; |
| 127 | size_t dataSize = sizeof(VkPhysicalGpuProperties); |
| 128 | fpGetGpuInfo = (PFN_vkGetGpuInfo) gpuw->pGPA((VkPhysicalGpu) gpuw->nextObject, "vkGetGpuInfo"); |
| 129 | fpGetGpuInfo((VkPhysicalGpu) gpuw->nextObject, VK_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, &dataSize, &gpuProps); |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 130 | if (gpuProps.vendorId == 0x8086) |
| 131 | { |
| 132 | *pOutLayerCount = 1; |
| 133 | strncpy((char *) pOutLayers[0], "Basic", maxStringSize); |
| 134 | } else |
| 135 | { |
| 136 | *pOutLayerCount = 0; |
| 137 | } |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 138 | return VK_SUCCESS; |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 142 | VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalGpu gpu, const char* pName) |
Jon Ashburn | 8d8dad0 | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 143 | { |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 144 | if (gpu == NULL) |
| 145 | return NULL; |
Chia-I Wu | e9ae388 | 2015-01-05 09:41:27 +0800 | [diff] [blame] | 146 | |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 147 | initLayerTable((const VkBaseLayerObject *) gpu); |
Chia-I Wu | e9ae388 | 2015-01-05 09:41:27 +0800 | [diff] [blame] | 148 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 149 | if (!strncmp("vkGetProcAddr", pName, sizeof("vkGetProcAddr"))) |
| 150 | return (void *) vkGetProcAddr; |
| 151 | else if (!strncmp("vkCreateDevice", pName, sizeof ("vkCreateDevice"))) |
| 152 | return (void *) vkCreateDevice; |
| 153 | else if (!strncmp("vkGetExtensionSupport", pName, sizeof ("vkGetExtensionSupport"))) |
| 154 | return (void *) vkGetExtensionSupport; |
| 155 | else if (!strncmp("vkEnumerateLayers", pName, sizeof ("vkEnumerateLayers"))) |
| 156 | return (void *) vkEnumerateLayers; |
| 157 | else if (!strncmp("vkGetFormatInfo", pName, sizeof ("vkGetFormatInfo"))) |
| 158 | return (void *) vkGetFormatInfo; |
| 159 | else if (!strncmp("vkLayerExtension1", pName, sizeof("vkLayerExtension1"))) |
| 160 | return (void *) vkLayerExtension1; |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 161 | else { |
Jon Ashburn | 301c5f0 | 2015-04-06 10:58:22 -0600 | [diff] [blame] | 162 | VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu; |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 163 | if (gpuw->pGPA == NULL) |
| 164 | return NULL; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 165 | return gpuw->pGPA((VkPhysicalGpu) gpuw->nextObject, pName); |
Jon Ashburn | 227a942 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 166 | } |
| 167 | } |