blob: a1021698bf442c68b71111c1acae9b04476f1fc0 [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{
David Pinedoeeca2a22015-06-18 17:03:14 -0600314 VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(screenshot_instance_table_map, gpu);
315 VkResult result = pInstanceTable->CreateDevice(gpu, pCreateInfo, pDevice);
316
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
David Pinedo8a4f5562015-06-19 13:48:18 -0600324#define SCREENSHOT_LAYER_EXT_ARRAY_SIZE 2
325static const VkExtensionProperties ssExts[SCREENSHOT_LAYER_EXT_ARRAY_SIZE] = {
David Pinedoeeca2a22015-06-18 17:03:14 -0600326 {
327 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
328 "ScreenShot",
329 0x10,
330 "Layer: ScreenShot",
331 },
David Pinedo8a4f5562015-06-19 13:48:18 -0600332 {
333 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
334 VK_WSI_LUNARG_EXTENSION_NAME,
335 0x10,
336 "Layer: Screenshot",
337 }
338
David Pinedoeeca2a22015-06-18 17:03:14 -0600339};
Tony Barbour426b9052015-06-24 16:06:58 -0600340VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionCount(
341 uint32_t* pCount)
David Pinedoeeca2a22015-06-18 17:03:14 -0600342{
Tony Barbour426b9052015-06-24 16:06:58 -0600343 *pCount = SCREENSHOT_LAYER_EXT_ARRAY_SIZE;
David Pinedoeeca2a22015-06-18 17:03:14 -0600344 return VK_SUCCESS;
345}
346
Tony Barbour426b9052015-06-24 16:06:58 -0600347VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionProperties(
348 uint32_t extensionIndex,
349 VkExtensionProperties* pProperties)
David Pinedoeeca2a22015-06-18 17:03:14 -0600350{
Tony Barbour426b9052015-06-24 16:06:58 -0600351 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
352 if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE)
353 return VK_ERROR_INVALID_VALUE;
David Pinedoeeca2a22015-06-18 17:03:14 -0600354
Tony Barbour426b9052015-06-24 16:06:58 -0600355 memcpy(pProperties, &ssExts[extensionIndex], sizeof(VkExtensionProperties));
David Pinedoeeca2a22015-06-18 17:03:14 -0600356
Tony Barbour426b9052015-06-24 16:06:58 -0600357 return VK_SUCCESS;
358}
359VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionCount(
360 VkPhysicalDevice gpu,
361 uint32_t* pCount)
362{
363 *pCount = SCREENSHOT_LAYER_EXT_ARRAY_SIZE;
364 return VK_SUCCESS;
365}
366
367VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionProperties(
368 VkPhysicalDevice gpu,
369 uint32_t extensionIndex,
370 VkExtensionProperties* pProperties)
371{
372 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
373
374 if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE)
375 return VK_ERROR_INVALID_VALUE;
376 memcpy(pProperties, &ssExts[extensionIndex], sizeof(VkExtensionProperties));
David Pinedoeeca2a22015-06-18 17:03:14 -0600377
378 return VK_SUCCESS;
379}
380
381VK_LAYER_EXPORT VkResult VKAPI vkGetDeviceQueue(
382 VkDevice device,
383 uint32_t queueNodeIndex,
384 uint32_t queueIndex,
385 VkQueue *pQueue)
386{
387 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
388 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
389
390 loader_platform_thread_lock_mutex(&globalLock);
391 if (screenshotEnvQueried && screenshotFrames.empty()) {
392 // We are all done taking screenshots, so don't do anything else
393 loader_platform_thread_unlock_mutex(&globalLock);
394 return result;
395 }
396
397 if (result == VK_SUCCESS) {
398 screenshot_device_table_map.emplace(*pQueue, pTable);
399
400 // Create a mapping for the swapchain object into the dispatch table
401 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
402 deviceMapElem->queue = *pQueue;
403 deviceMapElem->queueNodeIndex = queueNodeIndex;
404 deviceMapElem->queueIndex = queueIndex;
405 deviceMap.insert(make_pair(device, deviceMapElem));
406 }
407 loader_platform_thread_unlock_mutex(&globalLock);
408 return result;
409}
410
411VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapChainWSI(
412 VkDevice device,
413 const VkSwapChainCreateInfoWSI *pCreateInfo,
414 VkSwapChainWSI *pSwapChain)
415{
416 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
417 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapChainWSI(device, pCreateInfo, pSwapChain);
418
419 loader_platform_thread_lock_mutex(&globalLock);
420 if (screenshotEnvQueried && screenshotFrames.empty()) {
421 // We are all done taking screenshots, so don't do anything else
422 loader_platform_thread_unlock_mutex(&globalLock);
423 return result;
424 }
425
426 if (result == VK_SUCCESS)
427 {
428 // Create a mapping for a swapchain to a device, image extent, and format
429 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
430 swapchainMapElem->device = device;
431 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
432 swapchainMapElem->format = pCreateInfo->imageFormat;
433 swapchainMap.insert(make_pair(*pSwapChain, swapchainMapElem));
434
435 // Create a mapping for the swapchain object into the dispatch table
436 screenshot_device_table_map.emplace(*pSwapChain, pTable);
437 }
438 loader_platform_thread_unlock_mutex(&globalLock);
439
440 return result;
441}
442
443VK_LAYER_EXPORT VkResult VKAPI vkGetSwapChainInfoWSI(
444 VkSwapChainWSI swapChain,
445 VkSwapChainInfoTypeWSI infoType,
446 size_t *pDataSize,
447 void *pData)
448{
449 VkResult result = get_dispatch_table(screenshot_device_table_map, swapChain)->GetSwapChainInfoWSI(swapChain, infoType, pDataSize, pData);
450
451 loader_platform_thread_lock_mutex(&globalLock);
452 if (screenshotEnvQueried && screenshotFrames.empty()) {
453 // We are all done taking screenshots, so don't do anything else
454 loader_platform_thread_unlock_mutex(&globalLock);
455 return result;
456 }
457
458 if (result == VK_SUCCESS &&
459 !swapchainMap.empty() && swapchainMap.find(swapChain) != swapchainMap.end())
460 {
461 VkSwapChainImageInfoWSI *swapChainImageInfo = (VkSwapChainImageInfoWSI *)pData;
462 for (int i=0; i<*pDataSize/sizeof(VkSwapChainImageInfoWSI); i++,swapChainImageInfo++)
463 {
464 // Create a mapping for an image to a device, image extent, and format
465 ImageMapStruct *imageMapElem = new ImageMapStruct;
466 imageMapElem->device = swapchainMap[swapChain]->device;
467 imageMapElem->imageExtent = swapchainMap[swapChain]->imageExtent;
468 imageMapElem->format = swapchainMap[swapChain]->format;
469 imageMap.insert(make_pair(swapChainImageInfo->image, imageMapElem));
470 }
471 }
472 loader_platform_thread_unlock_mutex(&globalLock);
473 return result;
474}
475
476VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentWSI(VkQueue queue, const VkPresentInfoWSI* pPresentInfo)
477{
478 static int frameNumber = 0;
479 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentWSI(queue, pPresentInfo);
480
481 loader_platform_thread_lock_mutex(&globalLock);
482
483 if (!screenshotEnvQueried)
484 {
485 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
486 if (_vk_screenshot && *_vk_screenshot)
487 {
488 string spec(_vk_screenshot), word;
489 size_t start = 0, comma = 0;
490
491 while (start < spec.size()) {
492 int frameToAdd;
493 comma = spec.find(',', start);
494 if (comma == string::npos)
495 word = string(spec, start);
496 else
497 word = string(spec, start, comma - start);
498 frameToAdd=atoi(word.c_str());
499 // Add the frame number to list, but only do it if the word started with a digit and if
500 // it's not already in the list
501 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
502 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
503 {
504 screenshotFrames.push_back(frameToAdd);
505 }
506 if (comma == string::npos)
507 break;
508 start = comma + 1;
509 }
510 }
511 screenshotEnvQueried = true;
512 }
513
514
515 if (result == VK_SUCCESS && !screenshotFrames.empty())
516 {
517 vector<int>::iterator it;
518 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
519 if (it != screenshotFrames.end())
520 {
521 string fileName;
522 fileName = to_string(frameNumber) + ".ppm";
523 writePPM(fileName.c_str(), pPresentInfo->image);
524 screenshotFrames.erase(it);
525
526 if (screenshotFrames.empty())
527 {
528 // Free all our maps since we are done with them.
529 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
530 {
531 SwapchainMapStruct *swapchainMapElem = it->second;
532 delete swapchainMapElem;
533 }
534 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
535 {
536 ImageMapStruct *imageMapElem = it->second;
537 delete imageMapElem;
538 }
539 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
540 {
541 DeviceMapStruct *deviceMapElem = it->second;
542 delete deviceMapElem;
543 }
544 swapchainMap.clear();
545 imageMap.clear();
546 deviceMap.clear();
547 }
548 }
549 }
550 frameNumber++;
551 loader_platform_thread_unlock_mutex(&globalLock);
552 return result;
553}
554
555VK_LAYER_EXPORT void* VKAPI vkGetDeviceProcAddr(
556 VkDevice dev,
557 const char *funcName)
558{
559 void *fptr;
560
561 if (dev == NULL) {
562 return NULL;
563 }
564
565 /* loader uses this to force layer initialization; device object is wrapped */
566 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
567 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
568 return (void *) vkGetDeviceProcAddr;
569 }
570 if (!strcmp(funcName, "vkGetDeviceQueue"))
571 return (void*) vkGetDeviceQueue;
David Pinedoeeca2a22015-06-18 17:03:14 -0600572
Tony Barbour426b9052015-06-24 16:06:58 -0600573 if (!strcmp(funcName, "vkGetGlobalExtensionCount"))
574 return (void*) vkGetGlobalExtensionCount;
575
576 if (!strcmp(funcName, "vkGetPhysicalDeviceExtensionCount"))
577 return (void*) vkGetPhysicalDeviceExtensionCount;
578
579 if (!strcmp(funcName, "vkGetGlobalExtensionProperties"))
580 return (void*) vkGetGlobalExtensionProperties;
581
582 if (!strcmp(funcName, "vkGetPhysicalDeviceExtensionProperties"))
583 return (void*) vkGetPhysicalDeviceExtensionProperties;
584
David Pinedo8a4f5562015-06-19 13:48:18 -0600585 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
586 if (deviceExtMap.size() == 0 || deviceExtMap[pDisp].wsi_lunarg_enabled)
587 {
588 if (!strcmp(funcName, "vkCreateSwapChainWSI"))
589 return (void*) vkCreateSwapChainWSI;
590 if (!strcmp(funcName, "vkGetSwapChainInfoWSI"))
591 return (void*) vkGetSwapChainInfoWSI;
592 if (!strcmp(funcName, "vkQueuePresentWSI"))
593 return (void*) vkQueuePresentWSI;
594 }
595
596 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600597 return NULL;
David Pinedo8a4f5562015-06-19 13:48:18 -0600598 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedoeeca2a22015-06-18 17:03:14 -0600599}
600
601VK_LAYER_EXPORT void* VKAPI vkGetInstanceProcAddr(
602 VkInstance instance,
603 const char *funcName)
604{
605 if (instance == NULL) {
606 return NULL;
607 }
608
609 /* loader uses this to force layer initialization; instance object is wrapped */
610 if (!strcmp(funcName, "vkGetInstanceProcAddr")) {
611 initInstanceTable(screenshot_instance_table_map, (const VkBaseLayerObject *) instance);
612 return (void *) vkGetInstanceProcAddr;
613 }
614
Courtney Goeltzenleuchter8633c332015-06-23 08:50:57 -0600615 if (!strcmp(funcName, "vkDestroyInstance"))
616 return (void *) vkDestroyInstance;
David Pinedoeeca2a22015-06-18 17:03:14 -0600617 if (!strcmp(funcName, "vkCreateInstance"))
618 return (void*) vkCreateInstance;
619 if (!strcmp(funcName, "vkCreateDevice"))
620 return (void*) vkCreateDevice;
621
622 if (get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr == NULL)
623 return NULL;
624 return get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr(instance, funcName);
625}