aboutsummaryrefslogtreecommitdiff
path: root/layers/multi.cpp
blob: 4705b0ceb5d1d5a20713c3cbc4753ece376d81d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 * Vulkan
 *
 * Copyright (C) 2014 LunarG, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */

#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unordered_map>
#include "loader_platform.h"
#include "vk_dispatch_table_helper.h"
#include "vk_layer.h"
// The following is #included again to catch certain OS-specific functions
// being used:
#include "loader_platform.h"

static void initLayerTable(const VkBaseLayerObject *devw, VkLayerDispatchTable *pTable, const unsigned int layerNum);
static void initLayerInstanceTable(const VkBaseLayerObject *instw, VkLayerInstanceDispatchTable *pTable, const unsigned int layerNum);
/* Various dispatchable objects will use the same underlying dispatch table if they
 * are created from that "parent" object. Thus use pointer to dispatch table
 * as the key to table maps (tableMap1, tableInstanceMap1, tableMap2, tableInstanceMap2.
 *    Instance -> PhysicalDevice
 *    Device -> CmdBuffer or Queue
 * If use the object themselves as key to map then implies Create entrypoints have to be intercepted
 * and a new key inserted into map */
/******************************** Layer multi1 functions **************************/
static std::unordered_map<void *, VkLayerDispatchTable *> tableMap1;
static std::unordered_map<void *, VkLayerInstanceDispatchTable *> tableInstanceMap1;
static bool layer1_first_activated = false;

// Map lookup must be thread safe
static inline VkLayerDispatchTable *device_dispatch_table1(VkObject object)
{
    VkLayerDispatchTable *pDisp  = *(VkLayerDispatchTable **) object;
    std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap1.find((void *) pDisp);
    assert(it != tableMap1.end() && "Not able to find device dispatch entry");
    return it->second;
}

static inline VkLayerInstanceDispatchTable *instance_dispatch_table1(VkObject object)
{
    VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) object;
    std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap1.find((void *) pDisp);
    assert(it != tableInstanceMap1.end() && "Not able to find instance dispatch entry");
    return it->second;
}

static VkLayerDispatchTable *getLayer1Table(const VkBaseLayerObject *devw)
{
    VkLayerDispatchTable *pTable;
    assert(devw);
    VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) devw->baseObject;

    std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap1.find((void *) *ppDisp);
    if (it == tableMap1.end())
    {
        pTable =  new VkLayerDispatchTable;
        tableMap1[(void *) *ppDisp] = pTable;
        initLayerTable(devw, pTable, 1);
        return pTable;
    } else
    {
        return it->second;
    }
}
static VkLayerInstanceDispatchTable *getLayer1InstanceTable(const VkBaseLayerObject *instw)
{
    VkLayerInstanceDispatchTable *pTable;
    assert(instw);
    VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instw->baseObject;

    std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap1.find((void *) *ppDisp);
    if (it == tableInstanceMap1.end())
    {
        pTable =  new VkLayerInstanceDispatchTable;
        tableInstanceMap1[(void *) *ppDisp] = pTable;
        initLayerInstanceTable(instw, pTable, 1);
        return pTable;
    } else
    {
        return it->second;
    }
}
#ifdef __cplusplus
extern "C" {
#endif

/* hook DextroyDevice to remove tableMap entry */
VK_LAYER_EXPORT VkResult VKAPI multi1DestroyDevice(VkDevice device)
{
    VkLayerDispatchTable *pDisp = *(VkLayerDispatchTable **) device;
    VkResult res = device_dispatch_table1(device)->DestroyDevice(device);
    tableMap1.erase(pDisp);
    return res;
}

/* hook DestroyInstance to remove tableInstanceMap entry */
VK_LAYER_EXPORT VkResult VKAPI multi1DestroyInstance(VkInstance instance)
{
    VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) instance;
    VkResult res = instance_dispatch_table1(instance)->DestroyInstance(instance);
    tableInstanceMap1.erase(pDisp);
    return res;
}

VK_LAYER_EXPORT VkResult VKAPI multi1CreateSampler(VkDevice device, const VkSamplerCreateInfo* pCreateInfo, VkSampler* pSampler)
{
    VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;

    printf("At start of multi1 layer vkCreateSampler()\n");
    VkResult result = device_dispatch_table1(device)->CreateSampler(device, pCreateInfo, pSampler);
    printf("Completed multi1 layer vkCreateSampler()\n");
    return result;
}

VK_LAYER_EXPORT VkResult VKAPI multi1CreateGraphicsPipeline(VkDevice device, const VkGraphicsPipelineCreateInfo* pCreateInfo,
                                                                VkPipeline* pPipeline)
{
    VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;

    printf("At start of multi1 layer vkCreateGraphicsPipeline()\n");
    VkResult result = device_dispatch_table1(device)->CreateGraphicsPipeline(device, pCreateInfo, pPipeline);
    printf("Completed multi1 layer vkCreateGraphicsPipeline()\n");
    return result;
}

VK_LAYER_EXPORT VkResult VKAPI multi1StorePipeline(VkDevice device, VkPipeline pipeline, size_t* pDataSize, void* pData)
{
    VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;

    printf("At start of multi1 layer vkStorePipeline()\n");
    VkResult result = device_dispatch_table1(device)->StorePipeline(device, pipeline, pDataSize, pData);
    printf("Completed multi1 layer vkStorePipeline()\n");
    return result;
}

VK_LAYER_EXPORT void * VKAPI multi1GetDeviceProcAddr(VkDevice device, const char* pName)
{
    VkBaseLayerObject* devw = (VkBaseLayerObject *) device;

    if (device == NULL)
        return NULL;



    if (!strcmp("vkGetDeviceProcAddr", pName)) {
        getLayer1Table(devw);
        return (void *) multi1GetDeviceProcAddr;
    }
    if (!strcmp("vkDestroyDevice", pName))
        return (void *) multi1DestroyDevice;
    if (!strcmp("vkCreateSampler", pName))
        return (void *) multi1CreateSampler;
    if (!strcmp("vkCreateGraphicsPipeline", pName))
        return (void *) multi1CreateGraphicsPipeline;
    if (!strcmp("vkStorePipeline", pName))
        return (void *) multi1StorePipeline;
    else {
        VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;
        VkLayerDispatchTable* pTable = device_dispatch_table1(device);
        if (pTable->GetDeviceProcAddr == NULL)
            return NULL;
        return pTable->GetDeviceProcAddr(device, pName);
    }
}

VK_LAYER_EXPORT void * VKAPI multi1GetInstanceProcAddr(VkInstance inst, const char* pName)
{
    VkBaseLayerObject* instw = (VkBaseLayerObject *) inst;

    if (inst == NULL)
        return NULL;



    if (!strcmp("vkGetInstanceProcAddr", pName)) {
        getLayer1InstanceTable(instw);
        return (void *) multi1GetInstanceProcAddr;
    }
    if (!strcmp("vkDestroyInstance", pName))
        return (void *) multi1DestroyInstance;
    if (!strcmp("GetGlobalExtensionProperties", pName))
        return (void*) vkGetGlobalExtensionProperties;
    if (!strcmp("GetGlobalExtensionCount", pName))
        return (void*) vkGetGlobalExtensionCount;
    else {
        VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) inst;
        VkLayerInstanceDispatchTable* pTable = instance_dispatch_table1(inst);
        if (pTable->GetInstanceProcAddr == NULL)
            return NULL;
        return pTable->GetInstanceProcAddr(inst, pName);
    }
}

/******************************** Layer multi2 functions **************************/
static std::unordered_map<void *, VkLayerDispatchTable *> tableMap2;
static std::unordered_map<void *, VkLayerInstanceDispatchTable *> tableInstanceMap2;
static bool layer2_first_activated = false;

// Map lookup must be thread safe
static inline VkLayerDispatchTable *device_dispatch_table2(VkObject object)
{
    VkLayerDispatchTable *pDisp  = *(VkLayerDispatchTable **) object;
    std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap2.find((void *) pDisp);
    assert(it != tableMap2.end() && "Not able to find device dispatch entry");
    return it->second;
}

static inline VkLayerInstanceDispatchTable *instance_dispatch_table2(VkObject object)
{
    VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) object;
    std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap2.find((void *) pDisp);
    assert(it != tableInstanceMap2.end() && "Not able to find instance dispatch entry");
    return it->second;
}

static VkLayerInstanceDispatchTable *getLayer2InstanceTable(const VkBaseLayerObject *instw)
{
    VkLayerInstanceDispatchTable *pTable;
    assert(instw);
    VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instw->baseObject;

    std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap2.find((void *) *ppDisp);
    if (it == tableInstanceMap2.end())
    {
        pTable =  new VkLayerInstanceDispatchTable;
        tableInstanceMap2[(void *) *ppDisp] = pTable;
        initLayerInstanceTable(instw, pTable, 2);
        return pTable;
    } else
    {
        return it->second;
    }
}

static VkLayerDispatchTable *getLayer2Table(const VkBaseLayerObject *devw)
{
    VkLayerDispatchTable *pTable;
    assert(devw);
    VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) devw->baseObject;

    std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap2.find((void *) *ppDisp);
    if (it == tableMap2.end())
    {
        pTable =  new VkLayerDispatchTable;
        tableMap2[(void *) *ppDisp] = pTable;
        initLayerTable(devw, pTable, 2);
        return pTable;
    } else
    {
        return it->second;
    }
}

VK_LAYER_EXPORT VkResult VKAPI multi2EnumeratePhysicalDevices(
                                            VkInstance instance,
                                            uint32_t* pPhysicalDeviceCount,
                                            VkPhysicalDevice* pPhysicalDevices)
{
    VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) instance;

    printf("At start of wrapped multi2 vkEnumeratePhysicalDevices()\n");
    VkResult result = instance_dispatch_table2(instance)->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
    printf("Completed multi2 layer vkEnumeratePhysicalDevices()\n");
    return result;
}

/* hook DextroyDevice to remove tableMap entry */
VK_LAYER_EXPORT VkResult VKAPI multi2DestroyDevice(VkDevice device)
{
    VkLayerDispatchTable *pDisp = *(VkLayerDispatchTable **) device;
    VkResult res = device_dispatch_table2(device)->DestroyDevice(device);
    tableMap2.erase(pDisp);
    return res;
}

/* hook DestroyInstance to remove tableInstanceMap entry */
VK_LAYER_EXPORT VkResult VKAPI multi2DestroyInstance(VkInstance instance)
{
    VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) instance;
    VkResult res = instance_dispatch_table2(instance)->DestroyInstance(instance);
    tableInstanceMap2.erase(pDisp);
    return res;
}

VK_LAYER_EXPORT VkResult VKAPI multi2CreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo,
                                                      VkDevice* pDevice)
{
    printf("At start of multi2 vkCreateDevice()\n");
    VkResult result = device_dispatch_table2(*pDevice)->CreateDevice(gpu, pCreateInfo, pDevice);
    printf("Completed multi2 layer vkCreateDevice()\n");
    return result;
}

VK_LAYER_EXPORT VkResult VKAPI multi2CreateCommandBuffer(VkDevice device, const VkCmdBufferCreateInfo* pCreateInfo,
                                                             VkCmdBuffer* pCmdBuffer)
{
    VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;

    printf("At start of multi2 layer vkCreateCommandBuffer()\n");
    VkResult result = device_dispatch_table2(device)->CreateCommandBuffer(device, pCreateInfo, pCmdBuffer);
    printf("Completed multi2 layer vkCreateCommandBuffer()\n");
    return result;
}

VK_LAYER_EXPORT VkResult VKAPI multi2BeginCommandBuffer(VkCmdBuffer cmdBuffer, const VkCmdBufferBeginInfo* pBeginInfo)
{
    VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) cmdBuffer;

    printf("At start of multi2 layer vkBeginCommandBuffer()\n");
    VkResult result = device_dispatch_table2(cmdBuffer)->BeginCommandBuffer(cmdBuffer, pBeginInfo);
    printf("Completed multi2 layer vkBeginCommandBuffer()\n");
    return result;

}

VK_LAYER_EXPORT void * VKAPI multi2GetDeviceProcAddr(VkDevice device, const char* pName)
{
    VkBaseLayerObject* devw = (VkBaseLayerObject *) device;

    if (device == NULL)
        return NULL;

    if (!strcmp("vkGetDeviceProcAddr", pName)) {
        getLayer2Table(devw);
        return (void *) multi2GetDeviceProcAddr;
    }
    if (!strcmp("vkCreateDevice", pName))
        return (void *) multi2CreateDevice;
    if (!strcmp("vkDestroyDevice", pName))
        return (void *) multi2DestroyDevice;
    if (!strcmp("vkCreateCommandBuffer", pName))
        return (void *) multi2CreateCommandBuffer;
    else if (!strcmp("vkBeginCommandBuffer", pName))
        return (void *) multi2BeginCommandBuffer;
    else {
        VkLayerDispatchTable **ppDisp = (VkLayerDispatchTable **) device;
        VkLayerDispatchTable* pTable = device_dispatch_table2(device);
        if (pTable->GetDeviceProcAddr == NULL)
            return NULL;
        return pTable->GetDeviceProcAddr(device, pName);
    }
}

VK_LAYER_EXPORT void * VKAPI multi2GetInstanceProcAddr(VkInstance inst, const char* pName)
{
    VkBaseLayerObject* instw = (VkBaseLayerObject *) inst;

    if (inst == NULL)
        return NULL;

    if (!strcmp("vkGetInstanceProcAddr", pName)) {
        getLayer2InstanceTable(instw);
        return (void *) multi2GetInstanceProcAddr;
    }
    if (!strcmp("vkEnumeratePhysicalDevices", pName))
        return (void *) multi2EnumeratePhysicalDevices;
    if (!strcmp("vkDestroyInstance", pName))
        return (void *) multi2DestroyInstance;
    else if (!strcmp("GetGlobalExtensionProperties", pName))
        return (void*) vkGetGlobalExtensionProperties;
    else if (!strcmp("GetGlobalExtensionCount", pName))
        return (void*) vkGetGlobalExtensionCount;
    else {
        VkLayerInstanceDispatchTable **ppDisp = (VkLayerInstanceDispatchTable **) inst;
        VkLayerInstanceDispatchTable* pTable = instance_dispatch_table2(inst);
        if (pTable->GetInstanceProcAddr == NULL)
            return NULL;
        return pTable->GetInstanceProcAddr(inst, pName);
    }
}

/********************************* Common functions ********************************/

struct extProps {
    uint32_t version;
    const char * const name;
};

#define MULTI_LAYER_EXT_ARRAY_SIZE 2
static const VkExtensionProperties multiExts[MULTI_LAYER_EXT_ARRAY_SIZE] = {
    {
        VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
        "multi1",
        0x10,
        "Sample layer: multi",
//        0,
//        NULL,
    },
    {
        VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
        "multi2",
        0x10,
        "Sample layer: multi",
//        0,
//        NULL,
    }
};

VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionCount(
        uint32_t*    pCount)
{
    *pCount = MULTI_LAYER_EXT_ARRAY_SIZE;
    return VK_SUCCESS;
}

VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionProperties(
        uint32_t extensionIndex,
        VkExtensionProperties*    pProperties)
{
    /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
    if (extensionIndex >= MULTI_LAYER_EXT_ARRAY_SIZE)
        return VK_ERROR_INVALID_VALUE;

    memcpy(pProperties, &multiExts[extensionIndex], sizeof(VkExtensionProperties));

    return VK_SUCCESS;
}

VK_LAYER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char* pName)
{
    // to find each layers GPA routine Loader will search via "<layerName>GetDeviceProcAddr"
    if (!strcmp("multi1GetDeviceProcAddr", pName))
        return (void *) multi1GetDeviceProcAddr;
    else if (!strcmp("multi2GetDeviceProcAddr", pName))
        return (void *) multi2GetDeviceProcAddr;
    else if (!strcmp("vkGetDeviceProcAddr", pName))
        return (void *) vkGetDeviceProcAddr;

    // use first layer activated as GPA dispatch table activation happens in order
    else if (layer1_first_activated)
        return multi1GetDeviceProcAddr(device, pName);
    else if (layer2_first_activated)
        return multi2GetDeviceProcAddr(device, pName);
    else
        return NULL;

}

VK_LAYER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance inst, const char* pName)
{
    // to find each layers GPA routine Loader will search via "<layerName>GetInstanceProcAddr"
    if (!strcmp("multi1GetInstanceProcAddr", pName))
        return (void *) multi1GetInstanceProcAddr;
    else if (!strcmp("multi2GetInstanceProcAddr", pName))
        return (void *) multi2GetInstanceProcAddr;
    else if (!strcmp("vkGetInstanceProcAddr", pName))
        return (void *) vkGetInstanceProcAddr;

    // use first layer activated as GPA dispatch table activation happens in order
    else if (layer1_first_activated)
        return multi1GetInstanceProcAddr(inst, pName);
    else if (layer2_first_activated)
        return multi2GetInstanceProcAddr(inst, pName);
    else
        return NULL;

}
#ifdef __cplusplus
}    //extern "C"
#endif

static void initLayerTable(const VkBaseLayerObject *devw, VkLayerDispatchTable *pTable, const unsigned int layerNum)
{
    if (layerNum == 2 && layer1_first_activated == false)
        layer2_first_activated = true;
    if (layerNum == 1 && layer2_first_activated == false)
        layer1_first_activated = true;

    layer_initialize_dispatch_table(pTable, devw);
}

static void initLayerInstanceTable(const VkBaseLayerObject *instw, VkLayerInstanceDispatchTable *pTable, const unsigned int layerNum)
{
    if (layerNum == 2 && layer1_first_activated == false)
        layer2_first_activated = true;
    if (layerNum == 1 && layer2_first_activated == false)
        layer1_first_activated = true;

    layer_init_instance_dispatch_table(pTable, instw);
}