blob: 19b1d4aa8073d534feb1fae0ee39f8710a216d6e [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
David Pinedo8a4f5562015-06-19 13:48:18 -0600291static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
292{
293 uint32_t i, ext_idx;
294 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
295 deviceExtMap[pDisp].wsi_lunarg_enabled = false;
296 for (i = 0; i < pCreateInfo->extensionCount; i++) {
297 if (strcmp(pCreateInfo->pEnabledExtensions[i].name, VK_WSI_LUNARG_EXTENSION_NAME) == 0)
298 deviceExtMap[pDisp].wsi_lunarg_enabled = true;
299 }
300}
301
David Pinedoeeca2a22015-06-18 17:03:14 -0600302VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(
303 VkPhysicalDevice gpu,
304 const VkDeviceCreateInfo *pCreateInfo,
305 VkDevice *pDevice)
306{
David Pinedoeeca2a22015-06-18 17:03:14 -0600307 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(screenshot_instance_table_map, gpu);
308 VkResult result = pInstanceTable->CreateDevice(gpu, pCreateInfo, pDevice);
309
David Pinedo8a4f5562015-06-19 13:48:18 -0600310 if (result == VK_SUCCESS) {
311 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
312 }
313
David Pinedoeeca2a22015-06-18 17:03:14 -0600314 if (screenshotEnvQueried && screenshotFrames.empty())
315 // We are all done taking screenshots, so don't do anything else
316 return result;
317
318 if (result == VK_SUCCESS) {
319 VkLayerDispatchTable *pTable = get_dispatch_table(screenshot_device_table_map, *pDevice);
320 screenshot_device_table_map.emplace(*pDevice, pTable);
321 }
322
323 return result;
324}
325
David Pinedo8a4f5562015-06-19 13:48:18 -0600326#define SCREENSHOT_LAYER_EXT_ARRAY_SIZE 2
327static const VkExtensionProperties ssExts[SCREENSHOT_LAYER_EXT_ARRAY_SIZE] = {
David Pinedoeeca2a22015-06-18 17:03:14 -0600328 {
329 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
330 "ScreenShot",
331 0x10,
332 "Layer: ScreenShot",
333 },
David Pinedo8a4f5562015-06-19 13:48:18 -0600334 {
335 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
336 VK_WSI_LUNARG_EXTENSION_NAME,
337 0x10,
338 "Layer: Screenshot",
339 }
340
David Pinedoeeca2a22015-06-18 17:03:14 -0600341};
342
343VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
344 VkExtensionInfoType infoType,
345 uint32_t extensionIndex,
346 size_t *pDataSize,
347 void *pData)
348{
349 // This entrypoint is NOT going to init its own dispatch table since loader calls here early
350 uint32_t *count;
351
352 if (pDataSize == NULL) {
353 return VK_ERROR_INVALID_POINTER;
354 }
355
356 switch (infoType) {
357 case VK_EXTENSION_INFO_TYPE_COUNT:
358 *pDataSize = sizeof(uint32_t);
359 if (pData == NULL) {
360 return VK_SUCCESS;
361 }
362 count = (uint32_t *) pData;
363 *count = SCREENSHOT_LAYER_EXT_ARRAY_SIZE;
364 break;
365 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
366 *pDataSize = sizeof(VkExtensionProperties);
367 if (pData == NULL) {
368 return VK_SUCCESS;
369 }
370 if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE) {
371 return VK_ERROR_INVALID_VALUE;
372 }
David Pinedo8a4f5562015-06-19 13:48:18 -0600373 memcpy((VkExtensionProperties *) pData, &ssExts[extensionIndex], sizeof(VkExtensionProperties));
David Pinedoeeca2a22015-06-18 17:03:14 -0600374 break;
375 default:
376 return VK_ERROR_INVALID_VALUE;
377 };
378
379 return VK_SUCCESS;
380}
381
382VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionInfo(
383 VkPhysicalDevice physical_device,
384 VkExtensionInfoType infoType,
385 uint32_t extensionIndex,
386 size_t *pDataSize,
387 void *pData)
388{
389 uint32_t *count;
390
391 if (pDataSize == NULL) {
392 return VK_ERROR_INVALID_POINTER;
393 }
394
395 switch (infoType) {
396 case VK_EXTENSION_INFO_TYPE_COUNT:
397 *pDataSize = sizeof(uint32_t);
398 if (pData == NULL) {
399 return VK_SUCCESS;
400 }
401 count = (uint32_t *) pData;
402 *count = SCREENSHOT_LAYER_EXT_ARRAY_SIZE;
403 break;
404 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
405 *pDataSize = sizeof(VkExtensionProperties);
406 if (pData == NULL) {
407 return VK_SUCCESS;
408 }
409 if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE) {
410 return VK_ERROR_INVALID_VALUE;
411 }
David Pinedo8a4f5562015-06-19 13:48:18 -0600412 memcpy((VkExtensionProperties *) pData, &ssExts[extensionIndex], sizeof(VkExtensionProperties));
David Pinedoeeca2a22015-06-18 17:03:14 -0600413 break;
414 default:
415 return VK_ERROR_INVALID_VALUE;
416 }
417
418 return VK_SUCCESS;
419}
420
421VK_LAYER_EXPORT VkResult VKAPI vkGetDeviceQueue(
422 VkDevice device,
423 uint32_t queueNodeIndex,
424 uint32_t queueIndex,
425 VkQueue *pQueue)
426{
427 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
428 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
429
430 loader_platform_thread_lock_mutex(&globalLock);
431 if (screenshotEnvQueried && screenshotFrames.empty()) {
432 // We are all done taking screenshots, so don't do anything else
433 loader_platform_thread_unlock_mutex(&globalLock);
434 return result;
435 }
436
437 if (result == VK_SUCCESS) {
438 screenshot_device_table_map.emplace(*pQueue, pTable);
439
440 // Create a mapping for the swapchain object into the dispatch table
441 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
442 deviceMapElem->queue = *pQueue;
443 deviceMapElem->queueNodeIndex = queueNodeIndex;
444 deviceMapElem->queueIndex = queueIndex;
445 deviceMap.insert(make_pair(device, deviceMapElem));
446 }
447 loader_platform_thread_unlock_mutex(&globalLock);
448 return result;
449}
450
451VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapChainWSI(
452 VkDevice device,
453 const VkSwapChainCreateInfoWSI *pCreateInfo,
454 VkSwapChainWSI *pSwapChain)
455{
456 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
457 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapChainWSI(device, pCreateInfo, pSwapChain);
458
459 loader_platform_thread_lock_mutex(&globalLock);
460 if (screenshotEnvQueried && screenshotFrames.empty()) {
461 // We are all done taking screenshots, so don't do anything else
462 loader_platform_thread_unlock_mutex(&globalLock);
463 return result;
464 }
465
466 if (result == VK_SUCCESS)
467 {
468 // Create a mapping for a swapchain to a device, image extent, and format
469 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
470 swapchainMapElem->device = device;
471 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
472 swapchainMapElem->format = pCreateInfo->imageFormat;
473 swapchainMap.insert(make_pair(*pSwapChain, swapchainMapElem));
474
475 // Create a mapping for the swapchain object into the dispatch table
476 screenshot_device_table_map.emplace(*pSwapChain, pTable);
477 }
478 loader_platform_thread_unlock_mutex(&globalLock);
479
480 return result;
481}
482
483VK_LAYER_EXPORT VkResult VKAPI vkGetSwapChainInfoWSI(
484 VkSwapChainWSI swapChain,
485 VkSwapChainInfoTypeWSI infoType,
486 size_t *pDataSize,
487 void *pData)
488{
489 VkResult result = get_dispatch_table(screenshot_device_table_map, swapChain)->GetSwapChainInfoWSI(swapChain, infoType, pDataSize, pData);
490
491 loader_platform_thread_lock_mutex(&globalLock);
492 if (screenshotEnvQueried && screenshotFrames.empty()) {
493 // We are all done taking screenshots, so don't do anything else
494 loader_platform_thread_unlock_mutex(&globalLock);
495 return result;
496 }
497
498 if (result == VK_SUCCESS &&
499 !swapchainMap.empty() && swapchainMap.find(swapChain) != swapchainMap.end())
500 {
501 VkSwapChainImageInfoWSI *swapChainImageInfo = (VkSwapChainImageInfoWSI *)pData;
502 for (int i=0; i<*pDataSize/sizeof(VkSwapChainImageInfoWSI); i++,swapChainImageInfo++)
503 {
504 // Create a mapping for an image to a device, image extent, and format
505 ImageMapStruct *imageMapElem = new ImageMapStruct;
506 imageMapElem->device = swapchainMap[swapChain]->device;
507 imageMapElem->imageExtent = swapchainMap[swapChain]->imageExtent;
508 imageMapElem->format = swapchainMap[swapChain]->format;
509 imageMap.insert(make_pair(swapChainImageInfo->image, imageMapElem));
510 }
511 }
512 loader_platform_thread_unlock_mutex(&globalLock);
513 return result;
514}
515
516VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentWSI(VkQueue queue, const VkPresentInfoWSI* pPresentInfo)
517{
518 static int frameNumber = 0;
519 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentWSI(queue, pPresentInfo);
520
521 loader_platform_thread_lock_mutex(&globalLock);
522
523 if (!screenshotEnvQueried)
524 {
525 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
526 if (_vk_screenshot && *_vk_screenshot)
527 {
528 string spec(_vk_screenshot), word;
529 size_t start = 0, comma = 0;
530
531 while (start < spec.size()) {
532 int frameToAdd;
533 comma = spec.find(',', start);
534 if (comma == string::npos)
535 word = string(spec, start);
536 else
537 word = string(spec, start, comma - start);
538 frameToAdd=atoi(word.c_str());
539 // Add the frame number to list, but only do it if the word started with a digit and if
540 // it's not already in the list
541 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
542 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
543 {
544 screenshotFrames.push_back(frameToAdd);
545 }
546 if (comma == string::npos)
547 break;
548 start = comma + 1;
549 }
550 }
551 screenshotEnvQueried = true;
552 }
553
554
555 if (result == VK_SUCCESS && !screenshotFrames.empty())
556 {
557 vector<int>::iterator it;
558 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
559 if (it != screenshotFrames.end())
560 {
561 string fileName;
562 fileName = to_string(frameNumber) + ".ppm";
563 writePPM(fileName.c_str(), pPresentInfo->image);
564 screenshotFrames.erase(it);
565
566 if (screenshotFrames.empty())
567 {
568 // Free all our maps since we are done with them.
569 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
570 {
571 SwapchainMapStruct *swapchainMapElem = it->second;
572 delete swapchainMapElem;
573 }
574 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
575 {
576 ImageMapStruct *imageMapElem = it->second;
577 delete imageMapElem;
578 }
579 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
580 {
581 DeviceMapStruct *deviceMapElem = it->second;
582 delete deviceMapElem;
583 }
584 swapchainMap.clear();
585 imageMap.clear();
586 deviceMap.clear();
587 }
588 }
589 }
590 frameNumber++;
591 loader_platform_thread_unlock_mutex(&globalLock);
592 return result;
593}
594
595VK_LAYER_EXPORT void* VKAPI vkGetDeviceProcAddr(
596 VkDevice dev,
597 const char *funcName)
598{
599 void *fptr;
600
601 if (dev == NULL) {
602 return NULL;
603 }
604
605 /* loader uses this to force layer initialization; device object is wrapped */
606 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
607 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
608 return (void *) vkGetDeviceProcAddr;
609 }
610 if (!strcmp(funcName, "vkGetDeviceQueue"))
611 return (void*) vkGetDeviceQueue;
David Pinedoeeca2a22015-06-18 17:03:14 -0600612
David Pinedo8a4f5562015-06-19 13:48:18 -0600613 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
614 if (deviceExtMap.size() == 0 || deviceExtMap[pDisp].wsi_lunarg_enabled)
615 {
616 if (!strcmp(funcName, "vkCreateSwapChainWSI"))
617 return (void*) vkCreateSwapChainWSI;
618 if (!strcmp(funcName, "vkGetSwapChainInfoWSI"))
619 return (void*) vkGetSwapChainInfoWSI;
620 if (!strcmp(funcName, "vkQueuePresentWSI"))
621 return (void*) vkQueuePresentWSI;
622 }
623
624 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600625 return NULL;
David Pinedo8a4f5562015-06-19 13:48:18 -0600626 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedoeeca2a22015-06-18 17:03:14 -0600627}
628
629VK_LAYER_EXPORT void* VKAPI vkGetInstanceProcAddr(
630 VkInstance instance,
631 const char *funcName)
632{
633 if (instance == NULL) {
634 return NULL;
635 }
636
637 /* loader uses this to force layer initialization; instance object is wrapped */
638 if (!strcmp(funcName, "vkGetInstanceProcAddr")) {
639 initInstanceTable(screenshot_instance_table_map, (const VkBaseLayerObject *) instance);
640 return (void *) vkGetInstanceProcAddr;
641 }
642
643 if (!strcmp(funcName, "vkCreateInstance"))
644 return (void*) vkCreateInstance;
645 if (!strcmp(funcName, "vkCreateDevice"))
646 return (void*) vkCreateDevice;
647
648 if (get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr == NULL)
649 return NULL;
650 return get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr(instance, funcName);
651}