blob: 00adfa67a056c9d7ca7bcc8ab7a2adb3893ddc6c [file] [log] [blame]
Jon Ashburn8d8dad02014-12-01 14:22:40 -07001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Jon Ashburn8d8dad02014-12-01 14:22:40 -07003 *
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 Ashburn227a9422014-11-26 11:10:26 -070024#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
27#include <unordered_map>
Ian Elliott81ac44c2015-01-13 17:52:38 -070028#include "loader_platform.h"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060029#include "vk_dispatch_table_helper.h"
30#include "vkLayer.h"
Ian Elliott20f06872015-02-12 17:08:34 -070031// The following is #included again to catch certain OS-specific functions
32// being used:
33#include "loader_platform.h"
Jon Ashburn227a9422014-11-26 11:10:26 -070034
Jon Ashburn301c5f02015-04-06 10:58:22 -060035static std::unordered_map<void *, VkLayerDispatchTable *> tableMap;
Jon Ashburnd9564002015-05-07 10:27:37 -060036static std::unordered_map<void *, VkLayerInstanceDispatchTable *> tableInstanceMap;
37
Jon Ashburnd25a78e2015-05-15 16:40:25 -060038/* Various dispatchable objects will use the same underlying dispatch table if they
39 * are created from that "parent" object. Thus use pointer to dispatch table
40 * as the key to these table maps.
41 * Instance -> PhysicalDevice
42 * Device -> CmdBuffer or Queue
43 * If use the object themselves as key to map then implies Create entrypoints have to be intercepted
44 * and a new key inserted into map */
Jon Ashburnd9564002015-05-07 10:27:37 -060045static VkLayerInstanceDispatchTable * initLayerInstanceTable(const VkBaseLayerObject *instancew)
46{
47 VkLayerInstanceDispatchTable *pTable;
Jon Ashburnd9564002015-05-07 10:27:37 -060048 assert(instancew);
Jon Ashburnd25a78e2015-05-15 16:40:25 -060049 VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instancew->baseObject;
50
51 std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap.find((void *) *ppDisp);
Jon Ashburnd9564002015-05-07 10:27:37 -060052 if (it == tableInstanceMap.end())
53 {
54 pTable = new VkLayerInstanceDispatchTable;
Jon Ashburnd25a78e2015-05-15 16:40:25 -060055 tableInstanceMap[(void *) *ppDisp] = pTable;
Jon Ashburnd9564002015-05-07 10:27:37 -060056 } else
57 {
58 return it->second;
59 }
60
61 layer_init_instance_dispatch_table(pTable, (PFN_vkGetInstanceProcAddr) instancew->pGPA, (VkInstance) instancew->nextObject);
62
63 return pTable;
64}
Jon Ashburn227a9422014-11-26 11:10:26 -070065
Jon Ashburnd25a78e2015-05-15 16:40:25 -060066static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *devw)
Jon Ashburn227a9422014-11-26 11:10:26 -070067{
Jon Ashburn301c5f02015-04-06 10:58:22 -060068 VkLayerDispatchTable *pTable;
Jon Ashburnd25a78e2015-05-15 16:40:25 -060069 assert(devw);
70 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) (devw->baseObject);
Jon Ashburn227a9422014-11-26 11:10:26 -070071
Jon Ashburnd25a78e2015-05-15 16:40:25 -060072 std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) *ppDisp);
Jon Ashburn227a9422014-11-26 11:10:26 -070073 if (it == tableMap.end())
74 {
Jon Ashburn301c5f02015-04-06 10:58:22 -060075 pTable = new VkLayerDispatchTable;
Jon Ashburnd25a78e2015-05-15 16:40:25 -060076 tableMap[(void *) *ppDisp] = pTable;
Jon Ashburn227a9422014-11-26 11:10:26 -070077 } else
78 {
79 return it->second;
80 }
Chia-I Wu0f65b1e2015-01-04 23:11:43 +080081
Jon Ashburnd25a78e2015-05-15 16:40:25 -060082 layer_initialize_dispatch_table(pTable, (PFN_vkGetProcAddr) devw->pGPA, (VkPhysicalDevice) devw->nextObject);
Chia-I Wu0f65b1e2015-01-04 23:11:43 +080083
Jon Ashburn227a9422014-11-26 11:10:26 -070084 return pTable;
85}
86
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060087VK_LAYER_EXPORT VkResult VKAPI vkLayerExtension1(VkDevice device)
Jon Ashburn227a9422014-11-26 11:10:26 -070088{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060089 printf("In vkLayerExtension1() call w/ device: %p\n", (void*)device);
90 printf("vkLayerExtension1 returning SUCCESS\n");
91 return VK_SUCCESS;
Jon Ashburn227a9422014-11-26 11:10:26 -070092}
93
Jon Ashburneb2728b2015-04-10 14:33:07 -060094struct extProps {
95 uint32_t version;
96 const char * const name;
97};
98#define BASIC_LAYER_EXT_ARRAY_SIZE 2
99static const struct extProps basicExts[BASIC_LAYER_EXT_ARRAY_SIZE] = {
100 // TODO what is the version?
101 0x10, "Basic",
102 0x10, "vkLayerExtension1"
103};
104
105VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
106 VkExtensionInfoType infoType,
107 uint32_t extensionIndex,
108 size_t* pDataSize,
109 void* pData)
110{
Jon Ashburneb2728b2015-04-10 14:33:07 -0600111 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
112 VkExtensionProperties *ext_props;
113 uint32_t *count;
114
115 if (pDataSize == NULL)
116 return VK_ERROR_INVALID_POINTER;
117
118 switch (infoType) {
119 case VK_EXTENSION_INFO_TYPE_COUNT:
120 *pDataSize = sizeof(uint32_t);
121 if (pData == NULL)
122 return VK_SUCCESS;
123 count = (uint32_t *) pData;
124 *count = BASIC_LAYER_EXT_ARRAY_SIZE;
125 break;
126 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
127 *pDataSize = sizeof(VkExtensionProperties);
128 if (pData == NULL)
129 return VK_SUCCESS;
130 if (extensionIndex >= BASIC_LAYER_EXT_ARRAY_SIZE)
131 return VK_ERROR_INVALID_VALUE;
132 ext_props = (VkExtensionProperties *) pData;
133 ext_props->version = basicExts[extensionIndex].version;
134 strncpy(ext_props->extName, basicExts[extensionIndex].name,
135 VK_MAX_EXTENSION_NAME);
136 ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
137 break;
138 default:
139 return VK_ERROR_INVALID_VALUE;
140 };
141
142 return VK_SUCCESS;
143}
144
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600145VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
146 VkInstance instance,
147 uint32_t* pPhysicalDeviceCount,
148 VkPhysicalDevice* pPhysicalDevices)
149{
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600150 VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instance;
151 VkLayerInstanceDispatchTable *pInstTable = tableInstanceMap[*ppDisp];
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600152 printf("At start of wrapped vkEnumeratePhysicalDevices() call w/ inst: %p\n", (void*)instance);
153 VkResult result = pInstTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600154 printf("Completed wrapped vkEnumeratePhysicalDevices() call w/ count %u\n", *pPhysicalDeviceCount);
155 return result;
156}
157
Tony Barbour8205d902015-04-16 15:59:00 -0600158VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
Jon Ashburn227a9422014-11-26 11:10:26 -0700159{
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600160 VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) gpu;
161 VkLayerInstanceDispatchTable* pInstTable = tableInstanceMap[*ppDisp];
Jon Ashburn227a9422014-11-26 11:10:26 -0700162
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600163 printf("At start of wrapped vkCreateDevice() call w/ gpu: %p\n", (void*)gpu);
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600164 VkResult result = pInstTable->CreateDevice(gpu, pCreateInfo, pDevice);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600165 printf("Completed wrapped vkCreateDevice() call w/ pDevice, Device %p: %p\n", (void*)pDevice, (void *) *pDevice);
Jon Ashburn227a9422014-11-26 11:10:26 -0700166 return result;
167}
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600168
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600169VK_LAYER_EXPORT VkResult VKAPI vkGetFormatInfo(VkDevice device, VkFormat format, VkFormatInfoType infoType, size_t* pDataSize, void* pData)
Jon Ashburn227a9422014-11-26 11:10:26 -0700170{
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600171 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;
172 VkLayerDispatchTable* pTable = tableMap[*ppDisp];
Jon Ashburn227a9422014-11-26 11:10:26 -0700173
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600174 printf("At start of wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600175 VkResult result = pTable->GetFormatInfo(device, format, infoType, pDataSize, pData);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600176 printf("Completed wrapped vkGetFormatInfo() call w/ device: %p\n", (void*)device);
Jon Ashburn227a9422014-11-26 11:10:26 -0700177 return result;
178}
179
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -0600180VK_LAYER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice gpu, size_t maxStringSize, size_t* pLayerCount, char* const* pOutLayers, void* pReserved)
Jon Ashburn227a9422014-11-26 11:10:26 -0700181{
182 if (gpu != NULL)
183 {
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600184 VkLayerInstanceDispatchTable* pTable = initLayerInstanceTable((const VkBaseLayerObject *) gpu);
Jon Ashburn227a9422014-11-26 11:10:26 -0700185
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600186 printf("At start of wrapped vkEnumerateLayers() call w/ gpu: %p\n", gpu);
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -0600187 VkResult result = pTable->EnumerateLayers(gpu, maxStringSize, pLayerCount, pOutLayers, pReserved);
Jon Ashburn227a9422014-11-26 11:10:26 -0700188 return result;
189 } else
190 {
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -0600191 if (pLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pReserved == NULL)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600192 return VK_ERROR_INVALID_POINTER;
Jon Ashburn227a9422014-11-26 11:10:26 -0700193
194 // Example of a layer that is only compatible with Intel's GPUs
Jon Ashburn301c5f02015-04-06 10:58:22 -0600195 VkBaseLayerObject* gpuw = (VkBaseLayerObject*) pReserved;
Tony Barbour8205d902015-04-16 15:59:00 -0600196 PFN_vkGetPhysicalDeviceInfo fpGetGpuInfo;
197 VkPhysicalDeviceProperties gpuProps;
198 size_t dataSize = sizeof(VkPhysicalDeviceProperties);
199 fpGetGpuInfo = (PFN_vkGetPhysicalDeviceInfo) gpuw->pGPA((VkPhysicalDevice) gpuw->nextObject, "vkGetPhysicalDeviceInfo");
200 fpGetGpuInfo((VkPhysicalDevice) gpuw->nextObject, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES, &dataSize, &gpuProps);
Jon Ashburn227a9422014-11-26 11:10:26 -0700201 if (gpuProps.vendorId == 0x8086)
202 {
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -0600203 *pLayerCount = 1;
Jon Ashburn227a9422014-11-26 11:10:26 -0700204 strncpy((char *) pOutLayers[0], "Basic", maxStringSize);
205 } else
206 {
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -0600207 *pLayerCount = 0;
Jon Ashburn227a9422014-11-26 11:10:26 -0700208 }
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600209 return VK_SUCCESS;
Jon Ashburn227a9422014-11-26 11:10:26 -0700210 }
211}
212
Tony Barbour8205d902015-04-16 15:59:00 -0600213VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char* pName)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700214{
Jon Ashburn227a9422014-11-26 11:10:26 -0700215 if (gpu == NULL)
216 return NULL;
Chia-I Wue9ae3882015-01-05 09:41:27 +0800217
Jon Ashburn301c5f02015-04-06 10:58:22 -0600218 initLayerTable((const VkBaseLayerObject *) gpu);
Chia-I Wue9ae3882015-01-05 09:41:27 +0800219
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600220 if (!strcmp("vkGetProcAddr", pName))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600221 return (void *) vkGetProcAddr;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600222 if (!strcmp("vkCreateDevice", pName))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600223 return (void *) vkCreateDevice;
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600224 if (!strcmp("vkEnumeratePhysicalDevices", pName))
225 return (void*) vkEnumeratePhysicalDevices;
226 if (!strcmp("vkGetGlobalExtensionInfo", pName))
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600227 return (void*) vkGetGlobalExtensionInfo;
228 if (!strcmp("vkEnumerateLayers", pName))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600229 return (void *) vkEnumerateLayers;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600230 if (!strcmp("vkGetFormatInfo", pName))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600231 return (void *) vkGetFormatInfo;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600232 if (!strcmp("vkLayerExtension1", pName))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600233 return (void *) vkLayerExtension1;
Jon Ashburn227a9422014-11-26 11:10:26 -0700234 else {
Jon Ashburn301c5f02015-04-06 10:58:22 -0600235 VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;
Jon Ashburn227a9422014-11-26 11:10:26 -0700236 if (gpuw->pGPA == NULL)
237 return NULL;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600238 return gpuw->pGPA((VkObject) gpuw->nextObject, pName);
239 }
240}
241
242VK_LAYER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* pName)
243{
244 if (instance == NULL)
245 return NULL;
246
Jon Ashburnd9564002015-05-07 10:27:37 -0600247 initLayerInstanceTable((const VkBaseLayerObject *) instance);
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600248
249 if (!strcmp("vkGetInstanceProcAddr", pName))
250 return (void *) vkGetInstanceProcAddr;
251 if (!strcmp("vkGetProcAddr", pName))
252 return (void *) vkGetProcAddr;
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600253 if (!strcmp("vkEnumeratePhysicalDevices", pName))
254 return (void*) vkEnumeratePhysicalDevices;
255 if (!strcmp("vkGetGlobalExtensionInfo", pName))
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600256 return (void*) vkGetGlobalExtensionInfo;
257 if (!strcmp("vkCreateDevice", pName))
258 return (void *) vkCreateDevice;
259 if (!strcmp("vkEnumerateLayers", pName))
260 return (void *) vkEnumerateLayers;
261 else {
262 VkBaseLayerObject* instancew = (VkBaseLayerObject *) instance;
263 if (instancew->pGPA == NULL)
264 return NULL;
265 return instancew->pGPA((VkObject) instancew->nextObject, pName);
Jon Ashburn227a9422014-11-26 11:10:26 -0700266 }
267}