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