blob: f7927df343a1c98b2e1e63c9acbbd7761e07f947 [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 |
141 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT |
Courtney Goeltzenleuchterb25c9b92015-06-18 17:01:41 -0600142 VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600143 };
144 const VkCmdBufferCreateInfo createCommandBufferInfo = {
145 VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
146 NULL,
147 deviceMap[device]->queueNodeIndex,
148 0
149 };
150 const VkCmdBufferBeginInfo cmdBufferBeginInfo = {
151 VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
152 NULL,
153 VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
154 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
155 };
156 const VkImageCopy imageCopyRegion = {
157 {VK_IMAGE_ASPECT_COLOR, 0, 0},
158 {0, 0, 0},
159 {VK_IMAGE_ASPECT_COLOR, 0, 0},
160 {0, 0, 0},
161 {width, height, 1}
162 };
163 VkMemoryRequirements memRequirements;
David Pinedoeeca2a22015-06-18 17:03:14 -0600164 uint32_t num_allocations = 0;
165 size_t num_alloc_size = sizeof(num_allocations);
166 VkLayerDispatchTable* pTableDevice = screenshot_device_table_map[device];
167 VkLayerDispatchTable* pTableQueue = screenshot_device_table_map[queue];
168 VkLayerDispatchTable* pTableCmdBuffer;
169
170 if (imageMap.empty() || imageMap.find(image1) == imageMap.end())
171 return;
172
173 // The VkImage image1 we are going to dump may not be mappable,
174 // and/or it may have a tiling mode of optimal rather than linear.
175 // To make sure we have an image that we can map and read linearly, we:
176 // create image2 that is mappable and linear
177 // copy image1 to image2
178 // map image2
179 // read from image2's mapped memeory.
180
181 err = pTableDevice->CreateImage(device, &imgCreateInfo, &image2);
182 assert(!err);
183
Tony Barbour426b9052015-06-24 16:06:58 -0600184 err = pTableDevice->GetObjectMemoryRequirements(device,
David Pinedoeeca2a22015-06-18 17:03:14 -0600185 VK_OBJECT_TYPE_IMAGE, image2,
Tony Barbour426b9052015-06-24 16:06:58 -0600186 &memRequirements);
David Pinedoeeca2a22015-06-18 17:03:14 -0600187 assert(!err);
188
189 memAllocInfo.allocationSize = memRequirements.size;
190 err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2);
191 assert(!err);
192
193 err = pTableQueue->BindObjectMemory(device, VK_OBJECT_TYPE_IMAGE, image2, mem2, 0);
194 assert(!err);
195
196 err = pTableDevice->CreateCommandBuffer(device, &createCommandBufferInfo, &cmdBuffer);
197 assert(!err);
198
199 screenshot_device_table_map.emplace(cmdBuffer, pTableDevice);
200 pTableCmdBuffer = screenshot_device_table_map[cmdBuffer];
201
202 err = pTableCmdBuffer->BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo);
203 assert(!err);
204
205 pTableCmdBuffer->CmdCopyImage(cmdBuffer, image1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
206 image2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1, &imageCopyRegion);
207
208 err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer);
209 assert(!err);
210
211 err = pTableQueue->QueueSubmit(queue, 1, &cmdBuffer, VK_NULL_HANDLE);
212 assert(!err);
213
214 err = pTableQueue->QueueWaitIdle(queue);
215 assert(!err);
216
217 err = pTableDevice->DeviceWaitIdle(device);
218 assert(!err);
219
Tony Barbour426b9052015-06-24 16:06:58 -0600220 err = pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -0600221 assert(!err);
222
223 err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr );
224 assert(!err);
225
226 ptr += sr_layout.offset;
227
228 ofstream file(filename, ios::binary);
229
230 file << "P6\n";
231 file << width << "\n";
232 file << height << "\n";
233 file << 255 << "\n";
234
235 for (y = 0; y < height; y++) {
236 const unsigned int *row = (const unsigned int*) ptr;
237 if (format == VK_FORMAT_B8G8R8A8_UNORM)
238 {
239 for (x = 0; x < width; x++) {
240 unsigned int swapped;
241 swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16;
242 file.write((char *)&swapped, 3);
243 row++;
244 }
245 }
246 else if (format == VK_FORMAT_R8G8B8A8_UNORM)
247 {
248 for (x = 0; x < width; x++) {
249 file.write((char *)row, 3);
250 row++;
251 }
252 }
253 else
254 {
255 // TODO: add support for addition formats
256 printf("Unrecognized image format\n");
257 break;
258 }
259 ptr += sr_layout.rowPitch;
260 }
261 file.close();
262
263 // Clean up
264 err = pTableDevice->UnmapMemory(device, mem2);
265 assert(!err);
266 err = pTableDevice->FreeMemory(device, mem2);
267 assert(!err);
268 err = pTableDevice->DestroyObject(device, VK_OBJECT_TYPE_COMMAND_BUFFER, cmdBuffer);
269 assert(!err);
270}
271
272
273VkResult VKAPI vkCreateInstance(
274 const VkInstanceCreateInfo* pCreateInfo,
275 VkInstance* pInstance)
276{
277 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(screenshot_instance_table_map, *pInstance);
278 VkResult result = pTable->CreateInstance(pCreateInfo, pInstance);
279
280 if (result == VK_SUCCESS) {
281 init_screenshot();
282 }
283 return result;
284}
285
Courtney Goeltzenleuchterdb922a12015-06-23 08:50:27 -0600286// hook DestroyInstance to remove tableInstanceMap entry
287VK_LAYER_EXPORT VkResult VKAPI vkDestroyInstance(VkInstance instance)
288{
289 // Grab the key before the instance is destroyed.
290 dispatch_key key = get_dispatch_key(instance);
291 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(screenshot_instance_table_map, instance);
292 VkResult res = pTable->DestroyInstance(instance);
293
294 screenshot_instance_table_map.erase(key);
295 return res;
296}
297
David Pinedo8a4f5562015-06-19 13:48:18 -0600298static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
299{
300 uint32_t i, ext_idx;
301 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
302 deviceExtMap[pDisp].wsi_lunarg_enabled = false;
303 for (i = 0; i < pCreateInfo->extensionCount; i++) {
304 if (strcmp(pCreateInfo->pEnabledExtensions[i].name, VK_WSI_LUNARG_EXTENSION_NAME) == 0)
305 deviceExtMap[pDisp].wsi_lunarg_enabled = true;
306 }
307}
308
David Pinedoeeca2a22015-06-18 17:03:14 -0600309VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(
310 VkPhysicalDevice gpu,
311 const VkDeviceCreateInfo *pCreateInfo,
312 VkDevice *pDevice)
313{
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600314 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice);
315 VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pDevice);
David Pinedoeeca2a22015-06-18 17:03:14 -0600316
David Pinedo8a4f5562015-06-19 13:48:18 -0600317 if (result == VK_SUCCESS) {
318 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
319 }
320
David Pinedoeeca2a22015-06-18 17:03:14 -0600321 return result;
322}
323
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600324/* TODO: Probably need a DestroyDevice as well */
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",
David Pinedo8a4f5562015-06-19 13:48:18 -0600333 }
334
David Pinedoeeca2a22015-06-18 17:03:14 -0600335};
Tony Barbour426b9052015-06-24 16:06:58 -0600336VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionCount(
337 uint32_t* pCount)
David Pinedoeeca2a22015-06-18 17:03:14 -0600338{
Tony Barbour426b9052015-06-24 16:06:58 -0600339 *pCount = SCREENSHOT_LAYER_EXT_ARRAY_SIZE;
David Pinedoeeca2a22015-06-18 17:03:14 -0600340 return VK_SUCCESS;
341}
342
Tony Barbour426b9052015-06-24 16:06:58 -0600343VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionProperties(
344 uint32_t extensionIndex,
345 VkExtensionProperties* pProperties)
David Pinedoeeca2a22015-06-18 17:03:14 -0600346{
Tony Barbour426b9052015-06-24 16:06:58 -0600347 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
348 if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE)
349 return VK_ERROR_INVALID_VALUE;
David Pinedoeeca2a22015-06-18 17:03:14 -0600350
Tony Barbour426b9052015-06-24 16:06:58 -0600351 memcpy(pProperties, &ssExts[extensionIndex], sizeof(VkExtensionProperties));
David Pinedoeeca2a22015-06-18 17:03:14 -0600352
Tony Barbour426b9052015-06-24 16:06:58 -0600353 return VK_SUCCESS;
354}
355VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionCount(
356 VkPhysicalDevice gpu,
357 uint32_t* pCount)
358{
359 *pCount = SCREENSHOT_LAYER_EXT_ARRAY_SIZE;
360 return VK_SUCCESS;
361}
362
363VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionProperties(
364 VkPhysicalDevice gpu,
365 uint32_t extensionIndex,
366 VkExtensionProperties* pProperties)
367{
368 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
369
370 if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE)
371 return VK_ERROR_INVALID_VALUE;
372 memcpy(pProperties, &ssExts[extensionIndex], sizeof(VkExtensionProperties));
David Pinedoeeca2a22015-06-18 17:03:14 -0600373
374 return VK_SUCCESS;
375}
376
377VK_LAYER_EXPORT VkResult VKAPI vkGetDeviceQueue(
378 VkDevice device,
379 uint32_t queueNodeIndex,
380 uint32_t queueIndex,
381 VkQueue *pQueue)
382{
383 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
384 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
385
386 loader_platform_thread_lock_mutex(&globalLock);
387 if (screenshotEnvQueried && screenshotFrames.empty()) {
388 // We are all done taking screenshots, so don't do anything else
389 loader_platform_thread_unlock_mutex(&globalLock);
390 return result;
391 }
392
393 if (result == VK_SUCCESS) {
394 screenshot_device_table_map.emplace(*pQueue, pTable);
395
396 // Create a mapping for the swapchain object into the dispatch table
397 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
398 deviceMapElem->queue = *pQueue;
399 deviceMapElem->queueNodeIndex = queueNodeIndex;
400 deviceMapElem->queueIndex = queueIndex;
401 deviceMap.insert(make_pair(device, deviceMapElem));
402 }
403 loader_platform_thread_unlock_mutex(&globalLock);
404 return result;
405}
406
407VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapChainWSI(
408 VkDevice device,
409 const VkSwapChainCreateInfoWSI *pCreateInfo,
410 VkSwapChainWSI *pSwapChain)
411{
412 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
413 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapChainWSI(device, pCreateInfo, pSwapChain);
414
415 loader_platform_thread_lock_mutex(&globalLock);
416 if (screenshotEnvQueried && screenshotFrames.empty()) {
417 // We are all done taking screenshots, so don't do anything else
418 loader_platform_thread_unlock_mutex(&globalLock);
419 return result;
420 }
421
422 if (result == VK_SUCCESS)
423 {
424 // Create a mapping for a swapchain to a device, image extent, and format
425 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
426 swapchainMapElem->device = device;
427 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
428 swapchainMapElem->format = pCreateInfo->imageFormat;
429 swapchainMap.insert(make_pair(*pSwapChain, swapchainMapElem));
430
431 // Create a mapping for the swapchain object into the dispatch table
432 screenshot_device_table_map.emplace(*pSwapChain, pTable);
433 }
434 loader_platform_thread_unlock_mutex(&globalLock);
435
436 return result;
437}
438
439VK_LAYER_EXPORT VkResult VKAPI vkGetSwapChainInfoWSI(
440 VkSwapChainWSI swapChain,
441 VkSwapChainInfoTypeWSI infoType,
442 size_t *pDataSize,
443 void *pData)
444{
445 VkResult result = get_dispatch_table(screenshot_device_table_map, swapChain)->GetSwapChainInfoWSI(swapChain, infoType, pDataSize, pData);
446
447 loader_platform_thread_lock_mutex(&globalLock);
448 if (screenshotEnvQueried && screenshotFrames.empty()) {
449 // We are all done taking screenshots, so don't do anything else
450 loader_platform_thread_unlock_mutex(&globalLock);
451 return result;
452 }
453
454 if (result == VK_SUCCESS &&
455 !swapchainMap.empty() && swapchainMap.find(swapChain) != swapchainMap.end())
456 {
457 VkSwapChainImageInfoWSI *swapChainImageInfo = (VkSwapChainImageInfoWSI *)pData;
458 for (int i=0; i<*pDataSize/sizeof(VkSwapChainImageInfoWSI); i++,swapChainImageInfo++)
459 {
460 // Create a mapping for an image to a device, image extent, and format
461 ImageMapStruct *imageMapElem = new ImageMapStruct;
462 imageMapElem->device = swapchainMap[swapChain]->device;
463 imageMapElem->imageExtent = swapchainMap[swapChain]->imageExtent;
464 imageMapElem->format = swapchainMap[swapChain]->format;
465 imageMap.insert(make_pair(swapChainImageInfo->image, imageMapElem));
466 }
467 }
468 loader_platform_thread_unlock_mutex(&globalLock);
469 return result;
470}
471
472VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentWSI(VkQueue queue, const VkPresentInfoWSI* pPresentInfo)
473{
474 static int frameNumber = 0;
475 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentWSI(queue, pPresentInfo);
476
477 loader_platform_thread_lock_mutex(&globalLock);
478
479 if (!screenshotEnvQueried)
480 {
481 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
482 if (_vk_screenshot && *_vk_screenshot)
483 {
484 string spec(_vk_screenshot), word;
485 size_t start = 0, comma = 0;
486
487 while (start < spec.size()) {
488 int frameToAdd;
489 comma = spec.find(',', start);
490 if (comma == string::npos)
491 word = string(spec, start);
492 else
493 word = string(spec, start, comma - start);
494 frameToAdd=atoi(word.c_str());
495 // Add the frame number to list, but only do it if the word started with a digit and if
496 // it's not already in the list
497 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
498 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
499 {
500 screenshotFrames.push_back(frameToAdd);
501 }
502 if (comma == string::npos)
503 break;
504 start = comma + 1;
505 }
506 }
507 screenshotEnvQueried = true;
508 }
509
510
511 if (result == VK_SUCCESS && !screenshotFrames.empty())
512 {
513 vector<int>::iterator it;
514 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
515 if (it != screenshotFrames.end())
516 {
517 string fileName;
518 fileName = to_string(frameNumber) + ".ppm";
519 writePPM(fileName.c_str(), pPresentInfo->image);
520 screenshotFrames.erase(it);
521
522 if (screenshotFrames.empty())
523 {
524 // Free all our maps since we are done with them.
525 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
526 {
527 SwapchainMapStruct *swapchainMapElem = it->second;
528 delete swapchainMapElem;
529 }
530 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
531 {
532 ImageMapStruct *imageMapElem = it->second;
533 delete imageMapElem;
534 }
535 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
536 {
537 DeviceMapStruct *deviceMapElem = it->second;
538 delete deviceMapElem;
539 }
540 swapchainMap.clear();
541 imageMap.clear();
542 deviceMap.clear();
543 }
544 }
545 }
546 frameNumber++;
547 loader_platform_thread_unlock_mutex(&globalLock);
548 return result;
549}
550
551VK_LAYER_EXPORT void* VKAPI vkGetDeviceProcAddr(
552 VkDevice dev,
553 const char *funcName)
554{
555 void *fptr;
556
557 if (dev == NULL) {
558 return NULL;
559 }
560
561 /* loader uses this to force layer initialization; device object is wrapped */
562 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
563 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
564 return (void *) vkGetDeviceProcAddr;
565 }
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600566 if (!strcmp(funcName, "vkCreateDevice"))
567 return (void*) vkCreateDevice;
568
David Pinedoeeca2a22015-06-18 17:03:14 -0600569 if (!strcmp(funcName, "vkGetDeviceQueue"))
570 return (void*) vkGetDeviceQueue;
David Pinedoeeca2a22015-06-18 17:03:14 -0600571
Tony Barbour426b9052015-06-24 16:06:58 -0600572 if (!strcmp(funcName, "vkGetGlobalExtensionCount"))
573 return (void*) vkGetGlobalExtensionCount;
574
575 if (!strcmp(funcName, "vkGetPhysicalDeviceExtensionCount"))
576 return (void*) vkGetPhysicalDeviceExtensionCount;
577
578 if (!strcmp(funcName, "vkGetGlobalExtensionProperties"))
579 return (void*) vkGetGlobalExtensionProperties;
580
581 if (!strcmp(funcName, "vkGetPhysicalDeviceExtensionProperties"))
582 return (void*) vkGetPhysicalDeviceExtensionProperties;
583
David Pinedo8a4f5562015-06-19 13:48:18 -0600584 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
585 if (deviceExtMap.size() == 0 || deviceExtMap[pDisp].wsi_lunarg_enabled)
586 {
587 if (!strcmp(funcName, "vkCreateSwapChainWSI"))
588 return (void*) vkCreateSwapChainWSI;
589 if (!strcmp(funcName, "vkGetSwapChainInfoWSI"))
590 return (void*) vkGetSwapChainInfoWSI;
591 if (!strcmp(funcName, "vkQueuePresentWSI"))
592 return (void*) vkQueuePresentWSI;
593 }
594
595 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600596 return NULL;
David Pinedo8a4f5562015-06-19 13:48:18 -0600597 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedoeeca2a22015-06-18 17:03:14 -0600598}
599
600VK_LAYER_EXPORT void* VKAPI vkGetInstanceProcAddr(
601 VkInstance instance,
602 const char *funcName)
603{
604 if (instance == NULL) {
605 return NULL;
606 }
607
608 /* loader uses this to force layer initialization; instance object is wrapped */
609 if (!strcmp(funcName, "vkGetInstanceProcAddr")) {
610 initInstanceTable(screenshot_instance_table_map, (const VkBaseLayerObject *) instance);
611 return (void *) vkGetInstanceProcAddr;
612 }
613
Courtney Goeltzenleuchter8633c332015-06-23 08:50:57 -0600614 if (!strcmp(funcName, "vkDestroyInstance"))
615 return (void *) vkDestroyInstance;
David Pinedoeeca2a22015-06-18 17:03:14 -0600616 if (!strcmp(funcName, "vkCreateInstance"))
617 return (void*) vkCreateInstance;
David Pinedoeeca2a22015-06-18 17:03:14 -0600618
619 if (get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr == NULL)
620 return NULL;
621 return get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr(instance, funcName);
622}