blob: fd286b4dd4c9de81c6f188b0bdce00bf411a5658 [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 *
24 */
25
26#include <string.h>
27#include <stdlib.h>
28#include <assert.h>
29#include <unordered_map>
Tobin Ehlis7a51d902015-07-03 10:34:49 -060030#include "vk_loader_platform.h"
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060031#include "vk_dispatch_table_helper.h"
Tobin Ehlis2d1d9702015-07-03 09:42:57 -060032#include "vk_layer.h"
Ian Elliott20f06872015-02-12 17:08:34 -070033// The following is #included again to catch certain OS-specific functions
34// being used:
Tobin Ehlis7a51d902015-07-03 10:34:49 -060035#include "vk_loader_platform.h"
Courtney Goeltzenleuchter142c9c22015-07-06 21:32:03 -060036#include "vk_layer_extension_utils.h"
Jon Ashburn8d8dad02014-12-01 14:22:40 -070037
Jon Ashburn1245cec2015-05-18 13:20:15 -060038static void initLayerTable(const VkBaseLayerObject *devw, VkLayerDispatchTable *pTable, const unsigned int layerNum);
Jon Ashburnd9564002015-05-07 10:27:37 -060039static void initLayerInstanceTable(const VkBaseLayerObject *instw, VkLayerInstanceDispatchTable *pTable, const unsigned int layerNum);
Jon Ashburnd25a78e2015-05-15 16:40:25 -060040/* Various dispatchable objects will use the same underlying dispatch table if they
41 * are created from that "parent" object. Thus use pointer to dispatch table
42 * as the key to table maps (tableMap1, tableInstanceMap1, tableMap2, tableInstanceMap2.
43 * Instance -> PhysicalDevice
44 * Device -> CmdBuffer or Queue
45 * If use the object themselves as key to map then implies Create entrypoints have to be intercepted
46 * and a new key inserted into map */
Jon Ashburn8d8dad02014-12-01 14:22:40 -070047/******************************** Layer multi1 functions **************************/
Jon Ashburn301c5f02015-04-06 10:58:22 -060048static std::unordered_map<void *, VkLayerDispatchTable *> tableMap1;
Jon Ashburnd9564002015-05-07 10:27:37 -060049static std::unordered_map<void *, VkLayerInstanceDispatchTable *> tableInstanceMap1;
Jon Ashburn8d8dad02014-12-01 14:22:40 -070050static bool layer1_first_activated = false;
51
Jon Ashburn5a10d212015-06-01 10:02:09 -060052// Map lookup must be thread safe
53static inline VkLayerDispatchTable *device_dispatch_table1(VkObject object)
54{
55 VkLayerDispatchTable *pDisp = *(VkLayerDispatchTable **) object;
56 std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap1.find((void *) pDisp);
57 assert(it != tableMap1.end() && "Not able to find device dispatch entry");
58 return it->second;
59}
60
61static inline VkLayerInstanceDispatchTable *instance_dispatch_table1(VkObject object)
62{
63 VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) object;
64 std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap1.find((void *) pDisp);
65 assert(it != tableInstanceMap1.end() && "Not able to find instance dispatch entry");
66 return it->second;
67}
68
Jon Ashburnd25a78e2015-05-15 16:40:25 -060069static VkLayerDispatchTable *getLayer1Table(const VkBaseLayerObject *devw)
Jon Ashburn8d8dad02014-12-01 14:22:40 -070070{
Jon Ashburn301c5f02015-04-06 10:58:22 -060071 VkLayerDispatchTable *pTable;
Jon Ashburnd25a78e2015-05-15 16:40:25 -060072 assert(devw);
73 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) devw->baseObject;
Jon Ashburn8d8dad02014-12-01 14:22:40 -070074
Jon Ashburnd25a78e2015-05-15 16:40:25 -060075 std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap1.find((void *) *ppDisp);
Jon Ashburn8d8dad02014-12-01 14:22:40 -070076 if (it == tableMap1.end())
77 {
Jon Ashburn301c5f02015-04-06 10:58:22 -060078 pTable = new VkLayerDispatchTable;
Jon Ashburnd25a78e2015-05-15 16:40:25 -060079 tableMap1[(void *) *ppDisp] = pTable;
80 initLayerTable(devw, pTable, 1);
Jon Ashburn8d8dad02014-12-01 14:22:40 -070081 return pTable;
82 } else
83 {
84 return it->second;
85 }
86}
Jon Ashburnd25a78e2015-05-15 16:40:25 -060087static VkLayerInstanceDispatchTable *getLayer1InstanceTable(const VkBaseLayerObject *instw)
Jon Ashburnd9564002015-05-07 10:27:37 -060088{
89 VkLayerInstanceDispatchTable *pTable;
Jon Ashburnd9564002015-05-07 10:27:37 -060090 assert(instw);
Jon Ashburnd25a78e2015-05-15 16:40:25 -060091 VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instw->baseObject;
92
93 std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap1.find((void *) *ppDisp);
Jon Ashburnd9564002015-05-07 10:27:37 -060094 if (it == tableInstanceMap1.end())
95 {
96 pTable = new VkLayerInstanceDispatchTable;
Jon Ashburnd25a78e2015-05-15 16:40:25 -060097 tableInstanceMap1[(void *) *ppDisp] = pTable;
Jon Ashburnd9564002015-05-07 10:27:37 -060098 initLayerInstanceTable(instw, pTable, 1);
99 return pTable;
100 } else
101 {
102 return it->second;
103 }
104}
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700105#ifdef __cplusplus
106extern "C" {
107#endif
108
Courtney Goeltzenleuchter142c9c22015-07-06 21:32:03 -0600109/* hook DestroyDevice to remove tableMap entry */
Jon Ashburn17f37372015-05-19 16:34:53 -0600110VK_LAYER_EXPORT VkResult VKAPI multi1DestroyDevice(VkDevice device)
111{
112 VkLayerDispatchTable *pDisp = *(VkLayerDispatchTable **) device;
Jon Ashburn5a10d212015-06-01 10:02:09 -0600113 VkResult res = device_dispatch_table1(device)->DestroyDevice(device);
Jon Ashburn17f37372015-05-19 16:34:53 -0600114 tableMap1.erase(pDisp);
115 return res;
116}
117
118/* hook DestroyInstance to remove tableInstanceMap entry */
119VK_LAYER_EXPORT VkResult VKAPI multi1DestroyInstance(VkInstance instance)
120{
121 VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) instance;
Jon Ashburn5a10d212015-06-01 10:02:09 -0600122 VkResult res = instance_dispatch_table1(instance)->DestroyInstance(instance);
Jon Ashburn17f37372015-05-19 16:34:53 -0600123 tableInstanceMap1.erase(pDisp);
124 return res;
125}
126
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600127VK_LAYER_EXPORT VkResult VKAPI multi1CreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo, VkSampler* pSampler)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700128{
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600129 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600130
131 printf("At start of multi1 layer vkCreateSampler()\n");
Jon Ashburn5a10d212015-06-01 10:02:09 -0600132 VkResult result = device_dispatch_table1(device)->CreateSampler(device, pCreateInfo, pSampler);
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600133 printf("Completed multi1 layer vkCreateSampler()\n");
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700134 return result;
135}
136
Jon Ashburn0d60d272015-07-09 15:02:25 -0600137VK_LAYER_EXPORT VkResult VKAPI multi1CreateGraphicsPipelines(
138 VkDevice device,
139 VkPipelineCache pipelineCache,
140 uint32_t count,
141 const VkGraphicsPipelineCreateInfo* pCreateInfos,
142 VkPipeline* pPipelines)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700143{
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600144 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700145
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600146 printf("At start of multi1 layer vkCreateGraphicsPipeline()\n");
Jon Ashburn0d60d272015-07-09 15:02:25 -0600147 VkResult result = device_dispatch_table1(device)->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pPipelines);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600148 printf("Completed multi1 layer vkCreateGraphicsPipeline()\n");
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700149 return result;
150}
151
Jon Ashburn1245cec2015-05-18 13:20:15 -0600152VK_LAYER_EXPORT void * VKAPI multi1GetDeviceProcAddr(VkDevice device, const char* pName)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700153{
Jon Ashburn1245cec2015-05-18 13:20:15 -0600154 VkBaseLayerObject* devw = (VkBaseLayerObject *) device;
Chia-I Wue9ae3882015-01-05 09:41:27 +0800155
Jon Ashburn1245cec2015-05-18 13:20:15 -0600156 if (device == NULL)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700157 return NULL;
Chia-I Wue9ae3882015-01-05 09:41:27 +0800158
Chia-I Wue9ae3882015-01-05 09:41:27 +0800159
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600160
161 if (!strcmp("vkGetDeviceProcAddr", pName)) {
162 getLayer1Table(devw);
Jon Ashburn7cb4e0e2015-05-19 10:05:54 -0600163 return (void *) multi1GetDeviceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600164 }
Jon Ashburn17f37372015-05-19 16:34:53 -0600165 if (!strcmp("vkDestroyDevice", pName))
166 return (void *) multi1DestroyDevice;
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600167 if (!strcmp("vkCreateSampler", pName))
168 return (void *) multi1CreateSampler;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600169 if (!strcmp("vkCreateGraphicsPipelines", pName))
170 return (void *) multi1CreateGraphicsPipelines;
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700171 else {
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600172 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;
Jon Ashburn5a10d212015-06-01 10:02:09 -0600173 VkLayerDispatchTable* pTable = device_dispatch_table1(device);
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600174 if (pTable->GetDeviceProcAddr == NULL)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700175 return NULL;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600176 return pTable->GetDeviceProcAddr(device, pName);
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600177 }
178}
179
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600180VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI multi1GetInstanceProcAddr(VkInstance inst, const char* pName)
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600181{
182 VkBaseLayerObject* instw = (VkBaseLayerObject *) inst;
183
184 if (inst == NULL)
185 return NULL;
186
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600187
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600188
189 if (!strcmp("vkGetInstanceProcAddr", pName)) {
190 getLayer1InstanceTable(instw);
Jon Ashburn7cb4e0e2015-05-19 10:05:54 -0600191 return (void *) multi1GetInstanceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600192 }
Jon Ashburn17f37372015-05-19 16:34:53 -0600193 if (!strcmp("vkDestroyInstance", pName))
194 return (void *) multi1DestroyInstance;
Tony Barbour426b9052015-06-24 16:06:58 -0600195 if (!strcmp("GetGlobalExtensionProperties", pName))
196 return (void*) vkGetGlobalExtensionProperties;
Courtney Goeltzenleuchter142c9c22015-07-06 21:32:03 -0600197 if (!strcmp("GetGlobalLayerProperties", pName))
198 return (void*) vkGetGlobalLayerProperties;
199 if (!strcmp("GetPhysicalDeviceExtensionProperties", pName))
200 return (void*) vkGetPhysicalDeviceExtensionProperties;
201 if (!strcmp("GetPhysicalDeviceLayerProperties", pName))
202 return (void*) vkGetPhysicalDeviceLayerProperties;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600203 else {
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600204 VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) inst;
Jon Ashburn5a10d212015-06-01 10:02:09 -0600205 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table1(inst);
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600206 if (pTable->GetInstanceProcAddr == NULL)
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600207 return NULL;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600208 return pTable->GetInstanceProcAddr(inst, pName);
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700209 }
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700210}
211
212/******************************** Layer multi2 functions **************************/
Jon Ashburn301c5f02015-04-06 10:58:22 -0600213static std::unordered_map<void *, VkLayerDispatchTable *> tableMap2;
Jon Ashburnd9564002015-05-07 10:27:37 -0600214static std::unordered_map<void *, VkLayerInstanceDispatchTable *> tableInstanceMap2;
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700215static bool layer2_first_activated = false;
216
Jon Ashburn5a10d212015-06-01 10:02:09 -0600217// Map lookup must be thread safe
218static inline VkLayerDispatchTable *device_dispatch_table2(VkObject object)
219{
220 VkLayerDispatchTable *pDisp = *(VkLayerDispatchTable **) object;
221 std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap2.find((void *) pDisp);
222 assert(it != tableMap2.end() && "Not able to find device dispatch entry");
223 return it->second;
224}
225
226static inline VkLayerInstanceDispatchTable *instance_dispatch_table2(VkObject object)
227{
228 VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) object;
229 std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap2.find((void *) pDisp);
230 assert(it != tableInstanceMap2.end() && "Not able to find instance dispatch entry");
231 return it->second;
232}
233
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600234static VkLayerInstanceDispatchTable *getLayer2InstanceTable(const VkBaseLayerObject *instw)
Jon Ashburnd9564002015-05-07 10:27:37 -0600235{
236 VkLayerInstanceDispatchTable *pTable;
Jon Ashburnd9564002015-05-07 10:27:37 -0600237 assert(instw);
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600238 VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instw->baseObject;
239
240 std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap2.find((void *) *ppDisp);
Jon Ashburnd9564002015-05-07 10:27:37 -0600241 if (it == tableInstanceMap2.end())
242 {
243 pTable = new VkLayerInstanceDispatchTable;
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600244 tableInstanceMap2[(void *) *ppDisp] = pTable;
Jon Ashburnd9564002015-05-07 10:27:37 -0600245 initLayerInstanceTable(instw, pTable, 2);
246 return pTable;
247 } else
248 {
249 return it->second;
250 }
251}
252
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600253static VkLayerDispatchTable *getLayer2Table(const VkBaseLayerObject *devw)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700254{
Jon Ashburn301c5f02015-04-06 10:58:22 -0600255 VkLayerDispatchTable *pTable;
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600256 assert(devw);
257 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) devw->baseObject;
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700258
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600259 std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap2.find((void *) *ppDisp);
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700260 if (it == tableMap2.end())
261 {
Jon Ashburn301c5f02015-04-06 10:58:22 -0600262 pTable = new VkLayerDispatchTable;
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600263 tableMap2[(void *) *ppDisp] = pTable;
264 initLayerTable(devw, pTable, 2);
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700265 return pTable;
266 } else
267 {
268 return it->second;
269 }
270}
271
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600272VK_LAYER_EXPORT VkResult VKAPI multi2EnumeratePhysicalDevices(
273 VkInstance instance,
274 uint32_t* pPhysicalDeviceCount,
275 VkPhysicalDevice* pPhysicalDevices)
276{
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600277 VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instance;
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600278
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600279 printf("At start of wrapped multi2 vkEnumeratePhysicalDevices()\n");
Jon Ashburn5a10d212015-06-01 10:02:09 -0600280 VkResult result = instance_dispatch_table2(instance)->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600281 printf("Completed multi2 layer vkEnumeratePhysicalDevices()\n");
282 return result;
283}
284
Jon Ashburn17f37372015-05-19 16:34:53 -0600285/* hook DextroyDevice to remove tableMap entry */
286VK_LAYER_EXPORT VkResult VKAPI multi2DestroyDevice(VkDevice device)
287{
288 VkLayerDispatchTable *pDisp = *(VkLayerDispatchTable **) device;
Jon Ashburn5a10d212015-06-01 10:02:09 -0600289 VkResult res = device_dispatch_table2(device)->DestroyDevice(device);
Jon Ashburn17f37372015-05-19 16:34:53 -0600290 tableMap2.erase(pDisp);
291 return res;
292}
293
294/* hook DestroyInstance to remove tableInstanceMap entry */
295VK_LAYER_EXPORT VkResult VKAPI multi2DestroyInstance(VkInstance instance)
296{
297 VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) instance;
Jon Ashburn5a10d212015-06-01 10:02:09 -0600298 VkResult res = instance_dispatch_table2(instance)->DestroyInstance(instance);
Jon Ashburn17f37372015-05-19 16:34:53 -0600299 tableInstanceMap2.erase(pDisp);
300 return res;
301}
302
Tony Barbour8205d902015-04-16 15:59:00 -0600303VK_LAYER_EXPORT VkResult VKAPI multi2CreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600304 VkDevice* pDevice)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700305{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600306 printf("At start of multi2 vkCreateDevice()\n");
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600307 VkResult result = device_dispatch_table2(*pDevice)->CreateDevice(gpu, pCreateInfo, pDevice);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600308 printf("Completed multi2 layer vkCreateDevice()\n");
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700309 return result;
310}
311
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600312VK_LAYER_EXPORT VkResult VKAPI multi2CreateCommandBuffer(VkDevice device, const VkCmdBufferCreateInfo* pCreateInfo,
313 VkCmdBuffer* pCmdBuffer)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700314{
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600315 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700316
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600317 printf("At start of multi2 layer vkCreateCommandBuffer()\n");
Jon Ashburn5a10d212015-06-01 10:02:09 -0600318 VkResult result = device_dispatch_table2(device)->CreateCommandBuffer(device, pCreateInfo, pCmdBuffer);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600319 printf("Completed multi2 layer vkCreateCommandBuffer()\n");
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700320 return result;
321}
322
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600323VK_LAYER_EXPORT VkResult VKAPI multi2BeginCommandBuffer(VkCmdBuffer cmdBuffer, const VkCmdBufferBeginInfo* pBeginInfo)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700324{
Jon Ashburnd25a78e2015-05-15 16:40:25 -0600325 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) cmdBuffer;
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700326
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600327 printf("At start of multi2 layer vkBeginCommandBuffer()\n");
Jon Ashburn5a10d212015-06-01 10:02:09 -0600328 VkResult result = device_dispatch_table2(cmdBuffer)->BeginCommandBuffer(cmdBuffer, pBeginInfo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600329 printf("Completed multi2 layer vkBeginCommandBuffer()\n");
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700330 return result;
331
332}
333
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600334VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI multi2GetDeviceProcAddr(VkDevice device, const char* pName)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700335{
Jon Ashburn1245cec2015-05-18 13:20:15 -0600336 VkBaseLayerObject* devw = (VkBaseLayerObject *) device;
Chia-I Wue9ae3882015-01-05 09:41:27 +0800337
Jon Ashburn1245cec2015-05-18 13:20:15 -0600338 if (device == NULL)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700339 return NULL;
Chia-I Wue9ae3882015-01-05 09:41:27 +0800340
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600341 if (!strcmp("vkGetDeviceProcAddr", pName)) {
342 getLayer2Table(devw);
Jon Ashburn7cb4e0e2015-05-19 10:05:54 -0600343 return (void *) multi2GetDeviceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600344 }
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600345 if (!strcmp("vkCreateDevice", pName))
346 return (void *) multi2CreateDevice;
Jon Ashburn17f37372015-05-19 16:34:53 -0600347 if (!strcmp("vkDestroyDevice", pName))
348 return (void *) multi2DestroyDevice;
Jon Ashburn1245cec2015-05-18 13:20:15 -0600349 if (!strcmp("vkCreateCommandBuffer", pName))
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600350 return (void *) multi2CreateCommandBuffer;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600351 else if (!strcmp("vkBeginCommandBuffer", pName))
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600352 return (void *) multi2BeginCommandBuffer;
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700353 else {
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600354 VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;
Jon Ashburn5a10d212015-06-01 10:02:09 -0600355 VkLayerDispatchTable* pTable = device_dispatch_table2(device);
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600356 if (pTable->GetDeviceProcAddr == NULL)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700357 return NULL;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600358 return pTable->GetDeviceProcAddr(device, pName);
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600359 }
360}
361
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600362VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI multi2GetInstanceProcAddr(VkInstance inst, const char* pName)
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600363{
364 VkBaseLayerObject* instw = (VkBaseLayerObject *) inst;
365
366 if (inst == NULL)
367 return NULL;
368
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600369 if (!strcmp("vkGetInstanceProcAddr", pName)) {
370 getLayer2InstanceTable(instw);
Jon Ashburn7cb4e0e2015-05-19 10:05:54 -0600371 return (void *) multi2GetInstanceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600372 }
Jon Ashburn2666e2f2015-05-15 15:09:35 -0600373 if (!strcmp("vkEnumeratePhysicalDevices", pName))
374 return (void *) multi2EnumeratePhysicalDevices;
Jon Ashburn17f37372015-05-19 16:34:53 -0600375 if (!strcmp("vkDestroyInstance", pName))
376 return (void *) multi2DestroyInstance;
Courtney Goeltzenleuchter142c9c22015-07-06 21:32:03 -0600377 if (!strcmp("GetGlobalExtensionProperties", pName))
Tony Barbour426b9052015-06-24 16:06:58 -0600378 return (void*) vkGetGlobalExtensionProperties;
Courtney Goeltzenleuchter142c9c22015-07-06 21:32:03 -0600379 if (!strcmp("GetGlobalLayerProperties", pName))
380 return (void*) vkGetGlobalLayerProperties;
381 if (!strcmp("GetPhysicalDeviceExtensionProperties", pName))
382 return (void*) vkGetPhysicalDeviceExtensionProperties;
383 if (!strcmp("GetPhysicalDeviceLayerProperties", pName))
384 return (void*) vkGetPhysicalDeviceLayerProperties;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600385 else {
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600386 VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) inst;
Jon Ashburn5a10d212015-06-01 10:02:09 -0600387 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table2(inst);
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600388 if (pTable->GetInstanceProcAddr == NULL)
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600389 return NULL;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600390 return pTable->GetInstanceProcAddr(inst, pName);
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700391 }
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700392}
393
394/********************************* Common functions ********************************/
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700395
Jon Ashburneb2728b2015-04-10 14:33:07 -0600396struct extProps {
397 uint32_t version;
398 const char * const name;
399};
400
Courtney Goeltzenleuchter142c9c22015-07-06 21:32:03 -0600401VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionProperties(
402 const char *pLayerName,
403 uint32_t *pCount,
404 VkExtensionProperties* pProperties)
405{
406 /* multi does not have any global extensions */
407 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
408}
409
410VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalLayerProperties(
411 uint32_t *pCount,
412 VkLayerProperties* pProperties)
413{
414 /* multi does not have any global layers */
415 return util_GetLayerProperties(0, NULL, pCount, pProperties);
416}
417
418#define MULTI_LAYER_ARRAY_SIZE 1
419static const VkLayerProperties multi_device_layers[MULTI_LAYER_ARRAY_SIZE] = {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600420 {
Courtney Goeltzenleuchter142c9c22015-07-06 21:32:03 -0600421 "Multi1",
422 VK_API_VERSION,
423 VK_MAKE_VERSION(0, 1, 0),
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600424 "Sample layer: multi",
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600425 }
Jon Ashburneb2728b2015-04-10 14:33:07 -0600426};
427
Courtney Goeltzenleuchter142c9c22015-07-06 21:32:03 -0600428VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionProperties(
429 VkPhysicalDevice physicalDevice,
430 const char* pLayerName,
431 uint32_t* pCount,
432 VkExtensionProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600433{
Courtney Goeltzenleuchter142c9c22015-07-06 21:32:03 -0600434 /* Multi does not have any physical device extensions */
435 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600436}
437
Courtney Goeltzenleuchter142c9c22015-07-06 21:32:03 -0600438VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceLayerProperties(
439 VkPhysicalDevice physicalDevice,
440 uint32_t* pCount,
441 VkLayerProperties* pProperties)
Jon Ashburneb2728b2015-04-10 14:33:07 -0600442{
Courtney Goeltzenleuchter142c9c22015-07-06 21:32:03 -0600443 return util_GetLayerProperties(MULTI_LAYER_ARRAY_SIZE, multi_device_layers,
444 pCount, pProperties);
Jon Ashburneb2728b2015-04-10 14:33:07 -0600445}
446
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600447VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice device, const char* pName)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700448{
Jon Ashburn1245cec2015-05-18 13:20:15 -0600449 // to find each layers GPA routine Loader will search via "<layerName>GetDeviceProcAddr"
450 if (!strcmp("multi1GetDeviceProcAddr", pName))
451 return (void *) multi1GetDeviceProcAddr;
452 else if (!strcmp("multi2GetDeviceProcAddr", pName))
453 return (void *) multi2GetDeviceProcAddr;
454 else if (!strcmp("vkGetDeviceProcAddr", pName))
455 return (void *) vkGetDeviceProcAddr;
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700456
457 // use first layer activated as GPA dispatch table activation happens in order
458 else if (layer1_first_activated)
Jon Ashburn1245cec2015-05-18 13:20:15 -0600459 return multi1GetDeviceProcAddr(device, pName);
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700460 else if (layer2_first_activated)
Jon Ashburn1245cec2015-05-18 13:20:15 -0600461 return multi2GetDeviceProcAddr(device, pName);
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700462 else
463 return NULL;
464
465}
466
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600467VK_LAYER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance inst, const char* pName)
468{
Jon Ashburn1245cec2015-05-18 13:20:15 -0600469 // to find each layers GPA routine Loader will search via "<layerName>GetInstanceProcAddr"
470 if (!strcmp("multi1GetInstanceProcAddr", pName))
Jon Ashburnd9564002015-05-07 10:27:37 -0600471 return (void *) multi1GetInstanceProcAddr;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600472 else if (!strcmp("multi2GetInstanceProcAddr", pName))
Jon Ashburnd9564002015-05-07 10:27:37 -0600473 return (void *) multi2GetInstanceProcAddr;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600474 else if (!strcmp("vkGetInstanceProcAddr", pName))
Jon Ashburn1245cec2015-05-18 13:20:15 -0600475 return (void *) vkGetInstanceProcAddr;
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600476
477 // use first layer activated as GPA dispatch table activation happens in order
478 else if (layer1_first_activated)
Jon Ashburnd9564002015-05-07 10:27:37 -0600479 return multi1GetInstanceProcAddr(inst, pName);
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600480 else if (layer2_first_activated)
Jon Ashburnd9564002015-05-07 10:27:37 -0600481 return multi2GetInstanceProcAddr(inst, pName);
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600482 else
483 return NULL;
484
485}
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700486#ifdef __cplusplus
487} //extern "C"
488#endif
489
Jon Ashburn1245cec2015-05-18 13:20:15 -0600490static void initLayerTable(const VkBaseLayerObject *devw, VkLayerDispatchTable *pTable, const unsigned int layerNum)
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700491{
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700492 if (layerNum == 2 && layer1_first_activated == false)
493 layer2_first_activated = true;
494 if (layerNum == 1 && layer2_first_activated == false)
495 layer1_first_activated = true;
496
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600497 layer_initialize_dispatch_table(pTable, devw);
Jon Ashburn8d8dad02014-12-01 14:22:40 -0700498}
Jon Ashburnd9564002015-05-07 10:27:37 -0600499
500static void initLayerInstanceTable(const VkBaseLayerObject *instw, VkLayerInstanceDispatchTable *pTable, const unsigned int layerNum)
501{
502 if (layerNum == 2 && layer1_first_activated == false)
503 layer2_first_activated = true;
504 if (layerNum == 1 && layer2_first_activated == false)
505 layer1_first_activated = true;
506
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600507 layer_init_instance_dispatch_table(pTable, instw);
Jon Ashburnd9564002015-05-07 10:27:37 -0600508}