Jon Ashburn | 79113cc | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Jon Ashburn | 79113cc | 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 | 1fec424 | 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 | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 28 | #include "loader_platform.h" |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 29 | #include "vk_dispatch_table_helper.h" |
| 30 | #include "vkLayer.h" |
Ian Elliott | 655cad7 | 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 | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 34 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 35 | static std::unordered_map<void *, VK_LAYER_DISPATCH_TABLE *> tableMap; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 36 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 37 | static VK_LAYER_DISPATCH_TABLE * initLayerTable(const VK_BASE_LAYER_OBJECT *gpuw) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 38 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 39 | VK_LAYER_DISPATCH_TABLE *pTable; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 40 | |
| 41 | assert(gpuw); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 42 | std::unordered_map<void *, VK_LAYER_DISPATCH_TABLE *>::const_iterator it = tableMap.find((void *) gpuw); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 43 | if (it == tableMap.end()) |
| 44 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 45 | pTable = new VK_LAYER_DISPATCH_TABLE; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 46 | tableMap[(void *) gpuw] = pTable; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 47 | } else |
| 48 | { |
| 49 | return it->second; |
| 50 | } |
Chia-I Wu | aa4121f | 2015-01-04 23:11:43 +0800 | [diff] [blame] | 51 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 52 | layer_initialize_dispatch_table(pTable, gpuw->pGPA, (VK_PHYSICAL_GPU) gpuw->nextObject); |
Chia-I Wu | aa4121f | 2015-01-04 23:11:43 +0800 | [diff] [blame] | 53 | |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 54 | return pTable; |
| 55 | } |
| 56 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 57 | VK_LAYER_EXPORT VK_RESULT VKAPI vkLayerExtension1(VK_DEVICE device) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 58 | { |
Courtney Goeltzenleuchter | d8e229c | 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 | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 64 | VK_LAYER_EXPORT VK_RESULT VKAPI vkGetExtensionSupport(VK_PHYSICAL_GPU gpu, const char* pExtName) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 65 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 66 | VK_RESULT result; |
| 67 | VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) gpu; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 68 | |
Jon Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 69 | /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */ |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 70 | if (!strncmp(pExtName, "vkLayerExtension1", strlen("vkLayerExtension1"))) |
Jon Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 71 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 72 | result = VK_SUCCESS; |
Jon Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 73 | } else if (!strncmp(pExtName, "Basic", strlen("Basic"))) |
| 74 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 75 | result = VK_SUCCESS; |
Jon Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 76 | } else if (!tableMap.empty() && (tableMap.find(gpuw) != tableMap.end())) |
| 77 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 78 | printf("At start of wrapped vkGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu); |
| 79 | VK_LAYER_DISPATCH_TABLE* pTable = tableMap[gpuw]; |
| 80 | result = pTable->GetExtensionSupport((VK_PHYSICAL_GPU)gpuw->nextObject, pExtName); |
| 81 | printf("Completed wrapped vkGetExtensionSupport() call w/ gpu: %p\n", (void*)gpu); |
Jon Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 82 | } else |
| 83 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 84 | result = VK_ERROR_INVALID_EXTENSION; |
Jon Ashburn | 5f3960e | 2015-04-02 12:06:28 -0600 | [diff] [blame] | 85 | } |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 86 | return result; |
| 87 | } |
| 88 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 89 | VK_LAYER_EXPORT VK_RESULT VKAPI vkCreateDevice(VK_PHYSICAL_GPU gpu, const VK_DEVICE_CREATE_INFO* pCreateInfo, VK_DEVICE* pDevice) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 90 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 91 | VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) gpu; |
| 92 | VK_LAYER_DISPATCH_TABLE* pTable = tableMap[gpuw]; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 93 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 94 | printf("At start of wrapped vkCreateDevice() call w/ gpu: %p\n", (void*)gpu); |
| 95 | VK_RESULT result = pTable->CreateDevice((VK_PHYSICAL_GPU)gpuw->nextObject, pCreateInfo, pDevice); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 96 | // create a mapping for the device object into the dispatch table |
| 97 | tableMap.emplace(*pDevice, pTable); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 98 | printf("Completed wrapped vkCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 99 | return result; |
| 100 | } |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 101 | VK_LAYER_EXPORT VK_RESULT VKAPI vkGetFormatInfo(VK_DEVICE device, VK_FORMAT format, VK_FORMAT_INFO_TYPE infoType, size_t* pDataSize, void* pData) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 102 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 103 | VK_LAYER_DISPATCH_TABLE* pTable = tableMap[device]; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 104 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 105 | printf("At start of wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device); |
| 106 | VK_RESULT result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData); |
| 107 | printf("Completed wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 108 | return result; |
| 109 | } |
| 110 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 111 | VK_LAYER_EXPORT VK_RESULT VKAPI vkEnumerateLayers(VK_PHYSICAL_GPU gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved) |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 112 | { |
| 113 | if (gpu != NULL) |
| 114 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 115 | VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) gpu; |
| 116 | VK_LAYER_DISPATCH_TABLE* pTable = initLayerTable(gpuw); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 117 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 118 | printf("At start of wrapped vkEnumerateLayers() call w/ gpu: %p\n", gpu); |
| 119 | VK_RESULT result = pTable->EnumerateLayers((VK_PHYSICAL_GPU)gpuw->nextObject, maxLayerCount, maxStringSize, pOutLayerCount, pOutLayers, pReserved); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 120 | return result; |
| 121 | } else |
| 122 | { |
| 123 | if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pReserved == NULL) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 124 | return VK_ERROR_INVALID_POINTER; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 125 | |
| 126 | // Example of a layer that is only compatible with Intel's GPUs |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 127 | VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT*) pReserved; |
| 128 | vkGetGpuInfoType fpGetGpuInfo; |
| 129 | VK_PHYSICAL_GPU_PROPERTIES gpuProps; |
| 130 | size_t dataSize = sizeof(VK_PHYSICAL_GPU_PROPERTIES); |
| 131 | fpGetGpuInfo = (vkGetGpuInfoType) gpuw->pGPA((VK_PHYSICAL_GPU) gpuw->nextObject, "vkGetGpuInfo"); |
| 132 | fpGetGpuInfo((VK_PHYSICAL_GPU) gpuw->nextObject, VK_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, &dataSize, &gpuProps); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 133 | if (gpuProps.vendorId == 0x8086) |
| 134 | { |
| 135 | *pOutLayerCount = 1; |
| 136 | strncpy((char *) pOutLayers[0], "Basic", maxStringSize); |
| 137 | } else |
| 138 | { |
| 139 | *pOutLayerCount = 0; |
| 140 | } |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 141 | return VK_SUCCESS; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 145 | VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VK_PHYSICAL_GPU gpu, const char* pName) |
Jon Ashburn | 79113cc | 2014-12-01 14:22:40 -0700 | [diff] [blame] | 146 | { |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 147 | if (gpu == NULL) |
| 148 | return NULL; |
Chia-I Wu | b665d94 | 2015-01-05 09:41:27 +0800 | [diff] [blame] | 149 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 150 | initLayerTable((const VK_BASE_LAYER_OBJECT *) gpu); |
Chia-I Wu | b665d94 | 2015-01-05 09:41:27 +0800 | [diff] [blame] | 151 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 152 | if (!strncmp("vkGetProcAddr", pName, sizeof("vkGetProcAddr"))) |
| 153 | return (void *) vkGetProcAddr; |
| 154 | else if (!strncmp("vkCreateDevice", pName, sizeof ("vkCreateDevice"))) |
| 155 | return (void *) vkCreateDevice; |
| 156 | else if (!strncmp("vkGetExtensionSupport", pName, sizeof ("vkGetExtensionSupport"))) |
| 157 | return (void *) vkGetExtensionSupport; |
| 158 | else if (!strncmp("vkEnumerateLayers", pName, sizeof ("vkEnumerateLayers"))) |
| 159 | return (void *) vkEnumerateLayers; |
| 160 | else if (!strncmp("vkGetFormatInfo", pName, sizeof ("vkGetFormatInfo"))) |
| 161 | return (void *) vkGetFormatInfo; |
| 162 | else if (!strncmp("vkLayerExtension1", pName, sizeof("vkLayerExtension1"))) |
| 163 | return (void *) vkLayerExtension1; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 164 | else { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 165 | VK_BASE_LAYER_OBJECT* gpuw = (VK_BASE_LAYER_OBJECT *) gpu; |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 166 | if (gpuw->pGPA == NULL) |
| 167 | return NULL; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 168 | return gpuw->pGPA((VK_PHYSICAL_GPU) gpuw->nextObject, pName); |
Jon Ashburn | 1fec424 | 2014-11-26 11:10:26 -0700 | [diff] [blame] | 169 | } |
| 170 | } |