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