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