blob: 313bb7290d0bb7d0f296c2e32688f89e1f9720b0 [file] [log] [blame]
David Pinedoeeca2a22015-06-18 17:03:14 -06001/*
2 * Vulkan
3 *
4 * Copyright (C) 2015 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#include <inttypes.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <assert.h>
30#include <unordered_map>
31#include <iostream>
32#include <algorithm>
33#include <list>
34#include <map>
35#include <vector>
36#include <fstream>
37
38using namespace std;
39
40#include "loader_platform.h"
41#include "vk_dispatch_table_helper.h"
42#include "vk_struct_string_helper_cpp.h"
43#include "layers_config.h"
44// The following is #included again to catch certain OS-specific functions
45// being used:
46#include "loader_platform.h"
47#include "layers_table.h"
48
David Pinedo8a4f5562015-06-19 13:48:18 -060049
50struct devExts {
51 bool wsi_lunarg_enabled;
52};
53static std::unordered_map<void *, struct devExts> deviceExtMap;
David Pinedoeeca2a22015-06-18 17:03:14 -060054static device_table_map screenshot_device_table_map;
55static instance_table_map screenshot_instance_table_map;
56
57static int globalLockInitialized = 0;
58static loader_platform_thread_mutex globalLock;
59
60// unordered map, associates a swap chain with a device, image extent, and format
61typedef struct
62{
63 VkDevice device;
64 VkExtent2D imageExtent;
65 VkFormat format;
66} SwapchainMapStruct;
67static unordered_map<VkSwapChainWSI, SwapchainMapStruct *> swapchainMap;
68
69// unordered map, associates an image with a device, image extent, and format
70typedef struct
71{
72 VkDevice device;
73 VkExtent2D imageExtent;
74 VkFormat format;
75} ImageMapStruct;
76static unordered_map<VkImage, ImageMapStruct *> imageMap;
77
78// unordered map, associates a device with a queue
79typedef struct
80{
81 VkQueue queue;
82 uint32_t queueNodeIndex;
83 uint32_t queueIndex;
84} DeviceMapStruct;
85static unordered_map<VkDevice, DeviceMapStruct *> deviceMap;
86
87// List of frames to we will get a screenshot of
88static vector<int> screenshotFrames;
89
90// Flag indicating we have queried _VK_SCREENSHOT env var
91static bool screenshotEnvQueried = false;
92
93static void init_screenshot()
94{
David Pinedoeeca2a22015-06-18 17:03:14 -060095 if (!globalLockInitialized)
96 {
97 // TODO/TBD: Need to delete this mutex sometime. How??? One
98 // suggestion is to call this during vkCreateInstance(), and then we
99 // can clean it up during vkDestroyInstance(). However, that requires
100 // that the layer have per-instance locks. We need to come back and
101 // address this soon.
102 loader_platform_thread_create_mutex(&globalLock);
103 globalLockInitialized = 1;
104 }
105}
106
107static void writePPM( const char *filename, VkImage image1)
108{
109 VkImage image2;
110 VkResult err;
111 int x, y;
112 const char *ptr;
113 VkDeviceMemory mem2;
114 VkCmdBuffer cmdBuffer;
115 VkDevice device = imageMap[image1]->device;
116 VkQueue queue = deviceMap[device]->queue;
117 int width = imageMap[image1]->imageExtent.width;
118 int height = imageMap[image1]->imageExtent.height;
119 VkFormat format = imageMap[image1]->format;
120 const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR, 0, 0};
121 VkSubresourceLayout sr_layout;
David Pinedoeeca2a22015-06-18 17:03:14 -0600122 const VkImageCreateInfo imgCreateInfo = {
123 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
124 NULL,
125 VK_IMAGE_TYPE_2D,
126 format,
127 {width, height, 1},
128 1,
129 1,
130 1,
131 VK_IMAGE_TILING_LINEAR,
132 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT|VK_IMAGE_USAGE_STORAGE_BIT),
133 0
134 };
135 VkMemoryAllocInfo memAllocInfo = {
136 VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
137 NULL,
138 0, // allocationSize, queried later
139 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
140 VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT |
Mark Lobodzinski99d272a2015-07-02 17:09:57 -0600141 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT)
David Pinedoeeca2a22015-06-18 17:03:14 -0600142 };
143 const VkCmdBufferCreateInfo createCommandBufferInfo = {
144 VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
145 NULL,
146 deviceMap[device]->queueNodeIndex,
147 0
148 };
149 const VkCmdBufferBeginInfo cmdBufferBeginInfo = {
150 VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
151 NULL,
152 VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
153 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
154 };
155 const VkImageCopy imageCopyRegion = {
156 {VK_IMAGE_ASPECT_COLOR, 0, 0},
157 {0, 0, 0},
158 {VK_IMAGE_ASPECT_COLOR, 0, 0},
159 {0, 0, 0},
160 {width, height, 1}
161 };
162 VkMemoryRequirements memRequirements;
David Pinedoeeca2a22015-06-18 17:03:14 -0600163 uint32_t num_allocations = 0;
164 size_t num_alloc_size = sizeof(num_allocations);
David Pinedoc3256662015-06-30 13:08:37 -0600165 VkLayerDispatchTable* pTableDevice = get_dispatch_table(screenshot_device_table_map, device);
166 VkLayerDispatchTable* pTableQueue = get_dispatch_table(screenshot_device_table_map, queue);
David Pinedoeeca2a22015-06-18 17:03:14 -0600167 VkLayerDispatchTable* pTableCmdBuffer;
168
169 if (imageMap.empty() || imageMap.find(image1) == imageMap.end())
170 return;
171
172 // The VkImage image1 we are going to dump may not be mappable,
173 // and/or it may have a tiling mode of optimal rather than linear.
174 // To make sure we have an image that we can map and read linearly, we:
175 // create image2 that is mappable and linear
176 // copy image1 to image2
177 // map image2
178 // read from image2's mapped memeory.
179
180 err = pTableDevice->CreateImage(device, &imgCreateInfo, &image2);
181 assert(!err);
182
Tony Barbour426b9052015-06-24 16:06:58 -0600183 err = pTableDevice->GetObjectMemoryRequirements(device,
David Pinedoeeca2a22015-06-18 17:03:14 -0600184 VK_OBJECT_TYPE_IMAGE, image2,
Tony Barbour426b9052015-06-24 16:06:58 -0600185 &memRequirements);
David Pinedoeeca2a22015-06-18 17:03:14 -0600186 assert(!err);
187
188 memAllocInfo.allocationSize = memRequirements.size;
189 err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2);
190 assert(!err);
191
192 err = pTableQueue->BindObjectMemory(device, VK_OBJECT_TYPE_IMAGE, image2, mem2, 0);
193 assert(!err);
194
195 err = pTableDevice->CreateCommandBuffer(device, &createCommandBufferInfo, &cmdBuffer);
196 assert(!err);
197
198 screenshot_device_table_map.emplace(cmdBuffer, pTableDevice);
199 pTableCmdBuffer = screenshot_device_table_map[cmdBuffer];
200
201 err = pTableCmdBuffer->BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo);
202 assert(!err);
203
204 pTableCmdBuffer->CmdCopyImage(cmdBuffer, image1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
205 image2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1, &imageCopyRegion);
206
207 err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer);
208 assert(!err);
209
210 err = pTableQueue->QueueSubmit(queue, 1, &cmdBuffer, VK_NULL_HANDLE);
211 assert(!err);
212
213 err = pTableQueue->QueueWaitIdle(queue);
214 assert(!err);
215
216 err = pTableDevice->DeviceWaitIdle(device);
217 assert(!err);
218
Tony Barbour426b9052015-06-24 16:06:58 -0600219 err = pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -0600220 assert(!err);
221
222 err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr );
223 assert(!err);
224
225 ptr += sr_layout.offset;
226
227 ofstream file(filename, ios::binary);
228
229 file << "P6\n";
230 file << width << "\n";
231 file << height << "\n";
232 file << 255 << "\n";
233
234 for (y = 0; y < height; y++) {
235 const unsigned int *row = (const unsigned int*) ptr;
236 if (format == VK_FORMAT_B8G8R8A8_UNORM)
237 {
238 for (x = 0; x < width; x++) {
239 unsigned int swapped;
240 swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16;
241 file.write((char *)&swapped, 3);
242 row++;
243 }
244 }
245 else if (format == VK_FORMAT_R8G8B8A8_UNORM)
246 {
247 for (x = 0; x < width; x++) {
248 file.write((char *)row, 3);
249 row++;
250 }
251 }
252 else
253 {
254 // TODO: add support for addition formats
255 printf("Unrecognized image format\n");
256 break;
257 }
258 ptr += sr_layout.rowPitch;
259 }
260 file.close();
261
262 // Clean up
263 err = pTableDevice->UnmapMemory(device, mem2);
264 assert(!err);
265 err = pTableDevice->FreeMemory(device, mem2);
266 assert(!err);
267 err = pTableDevice->DestroyObject(device, VK_OBJECT_TYPE_COMMAND_BUFFER, cmdBuffer);
268 assert(!err);
269}
270
271
272VkResult VKAPI vkCreateInstance(
273 const VkInstanceCreateInfo* pCreateInfo,
274 VkInstance* pInstance)
275{
276 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(screenshot_instance_table_map, *pInstance);
277 VkResult result = pTable->CreateInstance(pCreateInfo, pInstance);
278
279 if (result == VK_SUCCESS) {
280 init_screenshot();
281 }
282 return result;
283}
284
Courtney Goeltzenleuchterdb922a12015-06-23 08:50:27 -0600285// hook DestroyInstance to remove tableInstanceMap entry
286VK_LAYER_EXPORT VkResult VKAPI vkDestroyInstance(VkInstance instance)
287{
288 // Grab the key before the instance is destroyed.
289 dispatch_key key = get_dispatch_key(instance);
290 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(screenshot_instance_table_map, instance);
291 VkResult res = pTable->DestroyInstance(instance);
292
293 screenshot_instance_table_map.erase(key);
294 return res;
295}
296
David Pinedo8a4f5562015-06-19 13:48:18 -0600297static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
298{
299 uint32_t i, ext_idx;
300 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
301 deviceExtMap[pDisp].wsi_lunarg_enabled = false;
302 for (i = 0; i < pCreateInfo->extensionCount; i++) {
303 if (strcmp(pCreateInfo->pEnabledExtensions[i].name, VK_WSI_LUNARG_EXTENSION_NAME) == 0)
304 deviceExtMap[pDisp].wsi_lunarg_enabled = true;
305 }
306}
307
David Pinedoeeca2a22015-06-18 17:03:14 -0600308VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(
309 VkPhysicalDevice gpu,
310 const VkDeviceCreateInfo *pCreateInfo,
311 VkDevice *pDevice)
312{
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600313 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice);
314 VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pDevice);
David Pinedoeeca2a22015-06-18 17:03:14 -0600315
David Pinedo8a4f5562015-06-19 13:48:18 -0600316 if (result == VK_SUCCESS) {
317 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
318 }
319
David Pinedoeeca2a22015-06-18 17:03:14 -0600320 return result;
321}
322
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600323/* TODO: Probably need a DestroyDevice as well */
324
David Pinedo8a4f5562015-06-19 13:48:18 -0600325#define SCREENSHOT_LAYER_EXT_ARRAY_SIZE 2
326static const VkExtensionProperties ssExts[SCREENSHOT_LAYER_EXT_ARRAY_SIZE] = {
David Pinedoeeca2a22015-06-18 17:03:14 -0600327 {
328 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
329 "ScreenShot",
330 0x10,
331 "Layer: ScreenShot",
David Pinedo8a4f5562015-06-19 13:48:18 -0600332 }
333
David Pinedoeeca2a22015-06-18 17:03:14 -0600334};
Tony Barbour426b9052015-06-24 16:06:58 -0600335VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionCount(
336 uint32_t* pCount)
David Pinedoeeca2a22015-06-18 17:03:14 -0600337{
Tony Barbour426b9052015-06-24 16:06:58 -0600338 *pCount = SCREENSHOT_LAYER_EXT_ARRAY_SIZE;
David Pinedoeeca2a22015-06-18 17:03:14 -0600339 return VK_SUCCESS;
340}
341
Tony Barbour426b9052015-06-24 16:06:58 -0600342VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionProperties(
343 uint32_t extensionIndex,
344 VkExtensionProperties* pProperties)
David Pinedoeeca2a22015-06-18 17:03:14 -0600345{
Tony Barbour426b9052015-06-24 16:06:58 -0600346 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
347 if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE)
348 return VK_ERROR_INVALID_VALUE;
David Pinedoeeca2a22015-06-18 17:03:14 -0600349
Tony Barbour426b9052015-06-24 16:06:58 -0600350 memcpy(pProperties, &ssExts[extensionIndex], sizeof(VkExtensionProperties));
David Pinedoeeca2a22015-06-18 17:03:14 -0600351
Tony Barbour426b9052015-06-24 16:06:58 -0600352 return VK_SUCCESS;
353}
354VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionCount(
355 VkPhysicalDevice gpu,
356 uint32_t* pCount)
357{
358 *pCount = SCREENSHOT_LAYER_EXT_ARRAY_SIZE;
359 return VK_SUCCESS;
360}
361
362VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionProperties(
363 VkPhysicalDevice gpu,
364 uint32_t extensionIndex,
365 VkExtensionProperties* pProperties)
366{
367 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
368
369 if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE)
370 return VK_ERROR_INVALID_VALUE;
371 memcpy(pProperties, &ssExts[extensionIndex], sizeof(VkExtensionProperties));
David Pinedoeeca2a22015-06-18 17:03:14 -0600372
373 return VK_SUCCESS;
374}
375
376VK_LAYER_EXPORT VkResult VKAPI vkGetDeviceQueue(
377 VkDevice device,
378 uint32_t queueNodeIndex,
379 uint32_t queueIndex,
380 VkQueue *pQueue)
381{
382 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
383 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
384
385 loader_platform_thread_lock_mutex(&globalLock);
386 if (screenshotEnvQueried && screenshotFrames.empty()) {
387 // We are all done taking screenshots, so don't do anything else
388 loader_platform_thread_unlock_mutex(&globalLock);
389 return result;
390 }
391
392 if (result == VK_SUCCESS) {
393 screenshot_device_table_map.emplace(*pQueue, pTable);
394
395 // Create a mapping for the swapchain object into the dispatch table
396 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
397 deviceMapElem->queue = *pQueue;
398 deviceMapElem->queueNodeIndex = queueNodeIndex;
399 deviceMapElem->queueIndex = queueIndex;
400 deviceMap.insert(make_pair(device, deviceMapElem));
401 }
402 loader_platform_thread_unlock_mutex(&globalLock);
403 return result;
404}
405
406VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapChainWSI(
407 VkDevice device,
408 const VkSwapChainCreateInfoWSI *pCreateInfo,
409 VkSwapChainWSI *pSwapChain)
410{
411 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
412 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapChainWSI(device, pCreateInfo, pSwapChain);
413
414 loader_platform_thread_lock_mutex(&globalLock);
415 if (screenshotEnvQueried && screenshotFrames.empty()) {
416 // We are all done taking screenshots, so don't do anything else
417 loader_platform_thread_unlock_mutex(&globalLock);
418 return result;
419 }
420
421 if (result == VK_SUCCESS)
422 {
423 // Create a mapping for a swapchain to a device, image extent, and format
424 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
425 swapchainMapElem->device = device;
426 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
427 swapchainMapElem->format = pCreateInfo->imageFormat;
428 swapchainMap.insert(make_pair(*pSwapChain, swapchainMapElem));
429
430 // Create a mapping for the swapchain object into the dispatch table
431 screenshot_device_table_map.emplace(*pSwapChain, pTable);
432 }
433 loader_platform_thread_unlock_mutex(&globalLock);
434
435 return result;
436}
437
438VK_LAYER_EXPORT VkResult VKAPI vkGetSwapChainInfoWSI(
439 VkSwapChainWSI swapChain,
440 VkSwapChainInfoTypeWSI infoType,
441 size_t *pDataSize,
442 void *pData)
443{
444 VkResult result = get_dispatch_table(screenshot_device_table_map, swapChain)->GetSwapChainInfoWSI(swapChain, infoType, pDataSize, pData);
445
446 loader_platform_thread_lock_mutex(&globalLock);
447 if (screenshotEnvQueried && screenshotFrames.empty()) {
448 // We are all done taking screenshots, so don't do anything else
449 loader_platform_thread_unlock_mutex(&globalLock);
450 return result;
451 }
452
453 if (result == VK_SUCCESS &&
454 !swapchainMap.empty() && swapchainMap.find(swapChain) != swapchainMap.end())
455 {
456 VkSwapChainImageInfoWSI *swapChainImageInfo = (VkSwapChainImageInfoWSI *)pData;
457 for (int i=0; i<*pDataSize/sizeof(VkSwapChainImageInfoWSI); i++,swapChainImageInfo++)
458 {
459 // Create a mapping for an image to a device, image extent, and format
460 ImageMapStruct *imageMapElem = new ImageMapStruct;
461 imageMapElem->device = swapchainMap[swapChain]->device;
462 imageMapElem->imageExtent = swapchainMap[swapChain]->imageExtent;
463 imageMapElem->format = swapchainMap[swapChain]->format;
464 imageMap.insert(make_pair(swapChainImageInfo->image, imageMapElem));
465 }
466 }
467 loader_platform_thread_unlock_mutex(&globalLock);
468 return result;
469}
470
471VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentWSI(VkQueue queue, const VkPresentInfoWSI* pPresentInfo)
472{
473 static int frameNumber = 0;
474 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentWSI(queue, pPresentInfo);
475
476 loader_platform_thread_lock_mutex(&globalLock);
477
478 if (!screenshotEnvQueried)
479 {
480 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
481 if (_vk_screenshot && *_vk_screenshot)
482 {
483 string spec(_vk_screenshot), word;
484 size_t start = 0, comma = 0;
485
486 while (start < spec.size()) {
487 int frameToAdd;
488 comma = spec.find(',', start);
489 if (comma == string::npos)
490 word = string(spec, start);
491 else
492 word = string(spec, start, comma - start);
493 frameToAdd=atoi(word.c_str());
494 // Add the frame number to list, but only do it if the word started with a digit and if
495 // it's not already in the list
496 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
497 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
498 {
499 screenshotFrames.push_back(frameToAdd);
500 }
501 if (comma == string::npos)
502 break;
503 start = comma + 1;
504 }
505 }
506 screenshotEnvQueried = true;
507 }
508
509
510 if (result == VK_SUCCESS && !screenshotFrames.empty())
511 {
512 vector<int>::iterator it;
513 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
514 if (it != screenshotFrames.end())
515 {
516 string fileName;
517 fileName = to_string(frameNumber) + ".ppm";
518 writePPM(fileName.c_str(), pPresentInfo->image);
519 screenshotFrames.erase(it);
520
521 if (screenshotFrames.empty())
522 {
523 // Free all our maps since we are done with them.
524 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
525 {
526 SwapchainMapStruct *swapchainMapElem = it->second;
527 delete swapchainMapElem;
528 }
529 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
530 {
531 ImageMapStruct *imageMapElem = it->second;
532 delete imageMapElem;
533 }
534 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
535 {
536 DeviceMapStruct *deviceMapElem = it->second;
537 delete deviceMapElem;
538 }
539 swapchainMap.clear();
540 imageMap.clear();
541 deviceMap.clear();
542 }
543 }
544 }
545 frameNumber++;
546 loader_platform_thread_unlock_mutex(&globalLock);
547 return result;
548}
549
550VK_LAYER_EXPORT void* VKAPI vkGetDeviceProcAddr(
551 VkDevice dev,
552 const char *funcName)
553{
554 void *fptr;
555
556 if (dev == NULL) {
557 return NULL;
558 }
559
560 /* loader uses this to force layer initialization; device object is wrapped */
561 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
562 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
563 return (void *) vkGetDeviceProcAddr;
564 }
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600565 if (!strcmp(funcName, "vkCreateDevice"))
566 return (void*) vkCreateDevice;
567
David Pinedoeeca2a22015-06-18 17:03:14 -0600568 if (!strcmp(funcName, "vkGetDeviceQueue"))
569 return (void*) vkGetDeviceQueue;
David Pinedoeeca2a22015-06-18 17:03:14 -0600570
Tony Barbour426b9052015-06-24 16:06:58 -0600571 if (!strcmp(funcName, "vkGetGlobalExtensionCount"))
572 return (void*) vkGetGlobalExtensionCount;
573
574 if (!strcmp(funcName, "vkGetPhysicalDeviceExtensionCount"))
575 return (void*) vkGetPhysicalDeviceExtensionCount;
576
577 if (!strcmp(funcName, "vkGetGlobalExtensionProperties"))
578 return (void*) vkGetGlobalExtensionProperties;
579
580 if (!strcmp(funcName, "vkGetPhysicalDeviceExtensionProperties"))
581 return (void*) vkGetPhysicalDeviceExtensionProperties;
582
David Pinedo8a4f5562015-06-19 13:48:18 -0600583 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
584 if (deviceExtMap.size() == 0 || deviceExtMap[pDisp].wsi_lunarg_enabled)
585 {
586 if (!strcmp(funcName, "vkCreateSwapChainWSI"))
587 return (void*) vkCreateSwapChainWSI;
588 if (!strcmp(funcName, "vkGetSwapChainInfoWSI"))
589 return (void*) vkGetSwapChainInfoWSI;
590 if (!strcmp(funcName, "vkQueuePresentWSI"))
591 return (void*) vkQueuePresentWSI;
592 }
593
594 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600595 return NULL;
David Pinedo8a4f5562015-06-19 13:48:18 -0600596 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedoeeca2a22015-06-18 17:03:14 -0600597}
598
599VK_LAYER_EXPORT void* VKAPI vkGetInstanceProcAddr(
600 VkInstance instance,
601 const char *funcName)
602{
603 if (instance == NULL) {
604 return NULL;
605 }
606
607 /* loader uses this to force layer initialization; instance object is wrapped */
608 if (!strcmp(funcName, "vkGetInstanceProcAddr")) {
609 initInstanceTable(screenshot_instance_table_map, (const VkBaseLayerObject *) instance);
610 return (void *) vkGetInstanceProcAddr;
611 }
612
Courtney Goeltzenleuchter8633c332015-06-23 08:50:57 -0600613 if (!strcmp(funcName, "vkDestroyInstance"))
614 return (void *) vkDestroyInstance;
David Pinedoeeca2a22015-06-18 17:03:14 -0600615 if (!strcmp(funcName, "vkCreateInstance"))
616 return (void*) vkCreateInstance;
David Pinedoeeca2a22015-06-18 17:03:14 -0600617
618 if (get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr == NULL)
619 return NULL;
620 return get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr(instance, funcName);
621}