blob: 757c00c480cd54967516756d4915a1f95582c812 [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;
122 size_t data_size = sizeof(sr_layout);
123 const VkImageCreateInfo imgCreateInfo = {
124 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
125 NULL,
126 VK_IMAGE_TYPE_2D,
127 format,
128 {width, height, 1},
129 1,
130 1,
131 1,
132 VK_IMAGE_TILING_LINEAR,
133 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT|VK_IMAGE_USAGE_STORAGE_BIT),
134 0
135 };
136 VkMemoryAllocInfo memAllocInfo = {
137 VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
138 NULL,
139 0, // allocationSize, queried later
140 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
141 VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT |
142 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT |
Courtney Goeltzenleuchterb25c9b92015-06-18 17:01:41 -0600143 VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600144 };
145 const VkCmdBufferCreateInfo createCommandBufferInfo = {
146 VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
147 NULL,
148 deviceMap[device]->queueNodeIndex,
149 0
150 };
151 const VkCmdBufferBeginInfo cmdBufferBeginInfo = {
152 VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
153 NULL,
154 VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
155 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
156 };
157 const VkImageCopy imageCopyRegion = {
158 {VK_IMAGE_ASPECT_COLOR, 0, 0},
159 {0, 0, 0},
160 {VK_IMAGE_ASPECT_COLOR, 0, 0},
161 {0, 0, 0},
162 {width, height, 1}
163 };
164 VkMemoryRequirements memRequirements;
165 size_t memRequirementsSize = sizeof(memRequirements);
166 uint32_t num_allocations = 0;
167 size_t num_alloc_size = sizeof(num_allocations);
168 VkLayerDispatchTable* pTableDevice = screenshot_device_table_map[device];
169 VkLayerDispatchTable* pTableQueue = screenshot_device_table_map[queue];
170 VkLayerDispatchTable* pTableCmdBuffer;
171
172 if (imageMap.empty() || imageMap.find(image1) == imageMap.end())
173 return;
174
175 // The VkImage image1 we are going to dump may not be mappable,
176 // and/or it may have a tiling mode of optimal rather than linear.
177 // To make sure we have an image that we can map and read linearly, we:
178 // create image2 that is mappable and linear
179 // copy image1 to image2
180 // map image2
181 // read from image2's mapped memeory.
182
183 err = pTableDevice->CreateImage(device, &imgCreateInfo, &image2);
184 assert(!err);
185
186 err = pTableDevice->GetObjectInfo(device,
187 VK_OBJECT_TYPE_IMAGE, image2,
188 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
189 &memRequirementsSize, &memRequirements);
190 assert(!err);
191
192 memAllocInfo.allocationSize = memRequirements.size;
193 err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2);
194 assert(!err);
195
196 err = pTableQueue->BindObjectMemory(device, VK_OBJECT_TYPE_IMAGE, image2, mem2, 0);
197 assert(!err);
198
199 err = pTableDevice->CreateCommandBuffer(device, &createCommandBufferInfo, &cmdBuffer);
200 assert(!err);
201
202 screenshot_device_table_map.emplace(cmdBuffer, pTableDevice);
203 pTableCmdBuffer = screenshot_device_table_map[cmdBuffer];
204
205 err = pTableCmdBuffer->BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo);
206 assert(!err);
207
208 pTableCmdBuffer->CmdCopyImage(cmdBuffer, image1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
209 image2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1, &imageCopyRegion);
210
211 err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer);
212 assert(!err);
213
214 err = pTableQueue->QueueSubmit(queue, 1, &cmdBuffer, VK_NULL_HANDLE);
215 assert(!err);
216
217 err = pTableQueue->QueueWaitIdle(queue);
218 assert(!err);
219
220 err = pTableDevice->DeviceWaitIdle(device);
221 assert(!err);
222
223 err = pTableDevice->GetImageSubresourceInfo(device, image2, &sr,
224 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
225 &data_size, &sr_layout);
226 assert(!err);
227
228 err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr );
229 assert(!err);
230
231 ptr += sr_layout.offset;
232
233 ofstream file(filename, ios::binary);
234
235 file << "P6\n";
236 file << width << "\n";
237 file << height << "\n";
238 file << 255 << "\n";
239
240 for (y = 0; y < height; y++) {
241 const unsigned int *row = (const unsigned int*) ptr;
242 if (format == VK_FORMAT_B8G8R8A8_UNORM)
243 {
244 for (x = 0; x < width; x++) {
245 unsigned int swapped;
246 swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16;
247 file.write((char *)&swapped, 3);
248 row++;
249 }
250 }
251 else if (format == VK_FORMAT_R8G8B8A8_UNORM)
252 {
253 for (x = 0; x < width; x++) {
254 file.write((char *)row, 3);
255 row++;
256 }
257 }
258 else
259 {
260 // TODO: add support for addition formats
261 printf("Unrecognized image format\n");
262 break;
263 }
264 ptr += sr_layout.rowPitch;
265 }
266 file.close();
267
268 // Clean up
269 err = pTableDevice->UnmapMemory(device, mem2);
270 assert(!err);
271 err = pTableDevice->FreeMemory(device, mem2);
272 assert(!err);
273 err = pTableDevice->DestroyObject(device, VK_OBJECT_TYPE_COMMAND_BUFFER, cmdBuffer);
274 assert(!err);
275}
276
277
278VkResult VKAPI vkCreateInstance(
279 const VkInstanceCreateInfo* pCreateInfo,
280 VkInstance* pInstance)
281{
282 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(screenshot_instance_table_map, *pInstance);
283 VkResult result = pTable->CreateInstance(pCreateInfo, pInstance);
284
285 if (result == VK_SUCCESS) {
286 init_screenshot();
287 }
288 return result;
289}
290
Courtney Goeltzenleuchterdb922a12015-06-23 08:50:27 -0600291// hook DestroyInstance to remove tableInstanceMap entry
292VK_LAYER_EXPORT VkResult VKAPI vkDestroyInstance(VkInstance instance)
293{
294 // Grab the key before the instance is destroyed.
295 dispatch_key key = get_dispatch_key(instance);
296 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(screenshot_instance_table_map, instance);
297 VkResult res = pTable->DestroyInstance(instance);
298
299 screenshot_instance_table_map.erase(key);
300 return res;
301}
302
David Pinedo8a4f5562015-06-19 13:48:18 -0600303static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
304{
305 uint32_t i, ext_idx;
306 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
307 deviceExtMap[pDisp].wsi_lunarg_enabled = false;
308 for (i = 0; i < pCreateInfo->extensionCount; i++) {
309 if (strcmp(pCreateInfo->pEnabledExtensions[i].name, VK_WSI_LUNARG_EXTENSION_NAME) == 0)
310 deviceExtMap[pDisp].wsi_lunarg_enabled = true;
311 }
312}
313
David Pinedoeeca2a22015-06-18 17:03:14 -0600314VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(
315 VkPhysicalDevice gpu,
316 const VkDeviceCreateInfo *pCreateInfo,
317 VkDevice *pDevice)
318{
David Pinedoeeca2a22015-06-18 17:03:14 -0600319 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(screenshot_instance_table_map, gpu);
320 VkResult result = pInstanceTable->CreateDevice(gpu, pCreateInfo, pDevice);
321
David Pinedo8a4f5562015-06-19 13:48:18 -0600322 if (result == VK_SUCCESS) {
323 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
324 }
325
David Pinedoeeca2a22015-06-18 17:03:14 -0600326 return result;
327}
328
David Pinedo8a4f5562015-06-19 13:48:18 -0600329#define SCREENSHOT_LAYER_EXT_ARRAY_SIZE 2
330static const VkExtensionProperties ssExts[SCREENSHOT_LAYER_EXT_ARRAY_SIZE] = {
David Pinedoeeca2a22015-06-18 17:03:14 -0600331 {
332 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
333 "ScreenShot",
334 0x10,
335 "Layer: ScreenShot",
336 },
David Pinedo8a4f5562015-06-19 13:48:18 -0600337 {
338 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
339 VK_WSI_LUNARG_EXTENSION_NAME,
340 0x10,
341 "Layer: Screenshot",
342 }
343
David Pinedoeeca2a22015-06-18 17:03:14 -0600344};
345
346VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
347 VkExtensionInfoType infoType,
348 uint32_t extensionIndex,
349 size_t *pDataSize,
350 void *pData)
351{
352 // This entrypoint is NOT going to init its own dispatch table since loader calls here early
353 uint32_t *count;
354
355 if (pDataSize == NULL) {
356 return VK_ERROR_INVALID_POINTER;
357 }
358
359 switch (infoType) {
360 case VK_EXTENSION_INFO_TYPE_COUNT:
361 *pDataSize = sizeof(uint32_t);
362 if (pData == NULL) {
363 return VK_SUCCESS;
364 }
365 count = (uint32_t *) pData;
366 *count = SCREENSHOT_LAYER_EXT_ARRAY_SIZE;
367 break;
368 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
369 *pDataSize = sizeof(VkExtensionProperties);
370 if (pData == NULL) {
371 return VK_SUCCESS;
372 }
373 if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE) {
374 return VK_ERROR_INVALID_VALUE;
375 }
David Pinedo8a4f5562015-06-19 13:48:18 -0600376 memcpy((VkExtensionProperties *) pData, &ssExts[extensionIndex], sizeof(VkExtensionProperties));
David Pinedoeeca2a22015-06-18 17:03:14 -0600377 break;
378 default:
379 return VK_ERROR_INVALID_VALUE;
380 };
381
382 return VK_SUCCESS;
383}
384
385VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionInfo(
386 VkPhysicalDevice physical_device,
387 VkExtensionInfoType infoType,
388 uint32_t extensionIndex,
389 size_t *pDataSize,
390 void *pData)
391{
392 uint32_t *count;
393
394 if (pDataSize == NULL) {
395 return VK_ERROR_INVALID_POINTER;
396 }
397
398 switch (infoType) {
399 case VK_EXTENSION_INFO_TYPE_COUNT:
400 *pDataSize = sizeof(uint32_t);
401 if (pData == NULL) {
402 return VK_SUCCESS;
403 }
404 count = (uint32_t *) pData;
405 *count = SCREENSHOT_LAYER_EXT_ARRAY_SIZE;
406 break;
407 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
408 *pDataSize = sizeof(VkExtensionProperties);
409 if (pData == NULL) {
410 return VK_SUCCESS;
411 }
412 if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE) {
413 return VK_ERROR_INVALID_VALUE;
414 }
David Pinedo8a4f5562015-06-19 13:48:18 -0600415 memcpy((VkExtensionProperties *) pData, &ssExts[extensionIndex], sizeof(VkExtensionProperties));
David Pinedoeeca2a22015-06-18 17:03:14 -0600416 break;
417 default:
418 return VK_ERROR_INVALID_VALUE;
419 }
420
421 return VK_SUCCESS;
422}
423
424VK_LAYER_EXPORT VkResult VKAPI vkGetDeviceQueue(
425 VkDevice device,
426 uint32_t queueNodeIndex,
427 uint32_t queueIndex,
428 VkQueue *pQueue)
429{
430 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
431 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
432
433 loader_platform_thread_lock_mutex(&globalLock);
434 if (screenshotEnvQueried && screenshotFrames.empty()) {
435 // We are all done taking screenshots, so don't do anything else
436 loader_platform_thread_unlock_mutex(&globalLock);
437 return result;
438 }
439
440 if (result == VK_SUCCESS) {
441 screenshot_device_table_map.emplace(*pQueue, pTable);
442
443 // Create a mapping for the swapchain object into the dispatch table
444 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
445 deviceMapElem->queue = *pQueue;
446 deviceMapElem->queueNodeIndex = queueNodeIndex;
447 deviceMapElem->queueIndex = queueIndex;
448 deviceMap.insert(make_pair(device, deviceMapElem));
449 }
450 loader_platform_thread_unlock_mutex(&globalLock);
451 return result;
452}
453
454VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapChainWSI(
455 VkDevice device,
456 const VkSwapChainCreateInfoWSI *pCreateInfo,
457 VkSwapChainWSI *pSwapChain)
458{
459 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
460 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapChainWSI(device, pCreateInfo, pSwapChain);
461
462 loader_platform_thread_lock_mutex(&globalLock);
463 if (screenshotEnvQueried && screenshotFrames.empty()) {
464 // We are all done taking screenshots, so don't do anything else
465 loader_platform_thread_unlock_mutex(&globalLock);
466 return result;
467 }
468
469 if (result == VK_SUCCESS)
470 {
471 // Create a mapping for a swapchain to a device, image extent, and format
472 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
473 swapchainMapElem->device = device;
474 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
475 swapchainMapElem->format = pCreateInfo->imageFormat;
476 swapchainMap.insert(make_pair(*pSwapChain, swapchainMapElem));
477
478 // Create a mapping for the swapchain object into the dispatch table
479 screenshot_device_table_map.emplace(*pSwapChain, pTable);
480 }
481 loader_platform_thread_unlock_mutex(&globalLock);
482
483 return result;
484}
485
486VK_LAYER_EXPORT VkResult VKAPI vkGetSwapChainInfoWSI(
487 VkSwapChainWSI swapChain,
488 VkSwapChainInfoTypeWSI infoType,
489 size_t *pDataSize,
490 void *pData)
491{
492 VkResult result = get_dispatch_table(screenshot_device_table_map, swapChain)->GetSwapChainInfoWSI(swapChain, infoType, pDataSize, pData);
493
494 loader_platform_thread_lock_mutex(&globalLock);
495 if (screenshotEnvQueried && screenshotFrames.empty()) {
496 // We are all done taking screenshots, so don't do anything else
497 loader_platform_thread_unlock_mutex(&globalLock);
498 return result;
499 }
500
501 if (result == VK_SUCCESS &&
502 !swapchainMap.empty() && swapchainMap.find(swapChain) != swapchainMap.end())
503 {
504 VkSwapChainImageInfoWSI *swapChainImageInfo = (VkSwapChainImageInfoWSI *)pData;
505 for (int i=0; i<*pDataSize/sizeof(VkSwapChainImageInfoWSI); i++,swapChainImageInfo++)
506 {
507 // Create a mapping for an image to a device, image extent, and format
508 ImageMapStruct *imageMapElem = new ImageMapStruct;
509 imageMapElem->device = swapchainMap[swapChain]->device;
510 imageMapElem->imageExtent = swapchainMap[swapChain]->imageExtent;
511 imageMapElem->format = swapchainMap[swapChain]->format;
512 imageMap.insert(make_pair(swapChainImageInfo->image, imageMapElem));
513 }
514 }
515 loader_platform_thread_unlock_mutex(&globalLock);
516 return result;
517}
518
519VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentWSI(VkQueue queue, const VkPresentInfoWSI* pPresentInfo)
520{
521 static int frameNumber = 0;
522 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentWSI(queue, pPresentInfo);
523
524 loader_platform_thread_lock_mutex(&globalLock);
525
526 if (!screenshotEnvQueried)
527 {
528 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
529 if (_vk_screenshot && *_vk_screenshot)
530 {
531 string spec(_vk_screenshot), word;
532 size_t start = 0, comma = 0;
533
534 while (start < spec.size()) {
535 int frameToAdd;
536 comma = spec.find(',', start);
537 if (comma == string::npos)
538 word = string(spec, start);
539 else
540 word = string(spec, start, comma - start);
541 frameToAdd=atoi(word.c_str());
542 // Add the frame number to list, but only do it if the word started with a digit and if
543 // it's not already in the list
544 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
545 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
546 {
547 screenshotFrames.push_back(frameToAdd);
548 }
549 if (comma == string::npos)
550 break;
551 start = comma + 1;
552 }
553 }
554 screenshotEnvQueried = true;
555 }
556
557
558 if (result == VK_SUCCESS && !screenshotFrames.empty())
559 {
560 vector<int>::iterator it;
561 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
562 if (it != screenshotFrames.end())
563 {
564 string fileName;
565 fileName = to_string(frameNumber) + ".ppm";
566 writePPM(fileName.c_str(), pPresentInfo->image);
567 screenshotFrames.erase(it);
568
569 if (screenshotFrames.empty())
570 {
571 // Free all our maps since we are done with them.
572 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
573 {
574 SwapchainMapStruct *swapchainMapElem = it->second;
575 delete swapchainMapElem;
576 }
577 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
578 {
579 ImageMapStruct *imageMapElem = it->second;
580 delete imageMapElem;
581 }
582 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
583 {
584 DeviceMapStruct *deviceMapElem = it->second;
585 delete deviceMapElem;
586 }
587 swapchainMap.clear();
588 imageMap.clear();
589 deviceMap.clear();
590 }
591 }
592 }
593 frameNumber++;
594 loader_platform_thread_unlock_mutex(&globalLock);
595 return result;
596}
597
598VK_LAYER_EXPORT void* VKAPI vkGetDeviceProcAddr(
599 VkDevice dev,
600 const char *funcName)
601{
602 void *fptr;
603
604 if (dev == NULL) {
605 return NULL;
606 }
607
608 /* loader uses this to force layer initialization; device object is wrapped */
609 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
610 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
611 return (void *) vkGetDeviceProcAddr;
612 }
613 if (!strcmp(funcName, "vkGetDeviceQueue"))
614 return (void*) vkGetDeviceQueue;
David Pinedoeeca2a22015-06-18 17:03:14 -0600615
David Pinedo8a4f5562015-06-19 13:48:18 -0600616 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
617 if (deviceExtMap.size() == 0 || deviceExtMap[pDisp].wsi_lunarg_enabled)
618 {
619 if (!strcmp(funcName, "vkCreateSwapChainWSI"))
620 return (void*) vkCreateSwapChainWSI;
621 if (!strcmp(funcName, "vkGetSwapChainInfoWSI"))
622 return (void*) vkGetSwapChainInfoWSI;
623 if (!strcmp(funcName, "vkQueuePresentWSI"))
624 return (void*) vkQueuePresentWSI;
625 }
626
627 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600628 return NULL;
David Pinedo8a4f5562015-06-19 13:48:18 -0600629 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedoeeca2a22015-06-18 17:03:14 -0600630}
631
632VK_LAYER_EXPORT void* VKAPI vkGetInstanceProcAddr(
633 VkInstance instance,
634 const char *funcName)
635{
636 if (instance == NULL) {
637 return NULL;
638 }
639
640 /* loader uses this to force layer initialization; instance object is wrapped */
641 if (!strcmp(funcName, "vkGetInstanceProcAddr")) {
642 initInstanceTable(screenshot_instance_table_map, (const VkBaseLayerObject *) instance);
643 return (void *) vkGetInstanceProcAddr;
644 }
645
Courtney Goeltzenleuchter8633c332015-06-23 08:50:57 -0600646 if (!strcmp(funcName, "vkDestroyInstance"))
647 return (void *) vkDestroyInstance;
David Pinedoeeca2a22015-06-18 17:03:14 -0600648 if (!strcmp(funcName, "vkCreateInstance"))
649 return (void*) vkCreateInstance;
650 if (!strcmp(funcName, "vkCreateDevice"))
651 return (void*) vkCreateDevice;
652
653 if (get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr == NULL)
654 return NULL;
655 return get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr(instance, funcName);
656}