blob: 1b8197e2e9f2eebbc603aeb456a1b1ca18c3fc46 [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
Tobin Ehlis7a51d902015-07-03 10:34:49 -060040#include "vk_loader_platform.h"
David Pinedoeeca2a22015-06-18 17:03:14 -060041#include "vk_dispatch_table_helper.h"
42#include "vk_struct_string_helper_cpp.h"
Tobin Ehlis56d204a2015-07-03 10:15:26 -060043#include "vk_layer_config.h"
David Pinedoeeca2a22015-06-18 17:03:14 -060044// The following is #included again to catch certain OS-specific functions
45// being used:
Tobin Ehlis7a51d902015-07-03 10:34:49 -060046#include "vk_loader_platform.h"
Tobin Ehlis56d204a2015-07-03 10:15:26 -060047#include "vk_layer_table.h"
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -060048#include "vk_layer_extension_utils.h"
David Pinedoeeca2a22015-06-18 17:03:14 -060049
David Pinedo8a4f5562015-06-19 13:48:18 -060050
51struct devExts {
Ian Elliottb134e842015-07-06 14:31:32 -060052 bool wsi_enabled;
David Pinedo8a4f5562015-06-19 13:48:18 -060053};
54static std::unordered_map<void *, struct devExts> deviceExtMap;
David Pinedoeeca2a22015-06-18 17:03:14 -060055static device_table_map screenshot_device_table_map;
David Pinedoeeca2a22015-06-18 17:03:14 -060056
57static int globalLockInitialized = 0;
58static loader_platform_thread_mutex globalLock;
59
David Pinedoaa4d1da2015-07-31 10:56:20 -060060// unordered map, associates a swap chain with a device, image extent, format, and
61// list of images
David Pinedoeeca2a22015-06-18 17:03:14 -060062typedef struct
63{
64 VkDevice device;
65 VkExtent2D imageExtent;
66 VkFormat format;
David Pinedoaa4d1da2015-07-31 10:56:20 -060067 VkImage *imageList;
David Pinedoeeca2a22015-06-18 17:03:14 -060068} SwapchainMapStruct;
David Pinedoaa4d1da2015-07-31 10:56:20 -060069static unordered_map<uint64_t, SwapchainMapStruct *> swapchainMap;
David Pinedoeeca2a22015-06-18 17:03:14 -060070
71// unordered map, associates an image with a device, image extent, and format
72typedef struct
73{
74 VkDevice device;
75 VkExtent2D imageExtent;
76 VkFormat format;
77} ImageMapStruct;
David Pinedoaa4d1da2015-07-31 10:56:20 -060078static unordered_map<uint64_t, ImageMapStruct *> imageMap;
David Pinedoeeca2a22015-06-18 17:03:14 -060079
David Pinedoaa4d1da2015-07-31 10:56:20 -060080// unordered map, associates a device with a queue and a cmdPool
David Pinedoeeca2a22015-06-18 17:03:14 -060081typedef struct
82{
83 VkQueue queue;
David Pinedoaa4d1da2015-07-31 10:56:20 -060084 VkCmdPool cmdPool;
David Pinedoeeca2a22015-06-18 17:03:14 -060085} DeviceMapStruct;
86static unordered_map<VkDevice, DeviceMapStruct *> deviceMap;
87
88// List of frames to we will get a screenshot of
89static vector<int> screenshotFrames;
90
91// Flag indicating we have queried _VK_SCREENSHOT env var
92static bool screenshotEnvQueried = false;
93
94static void init_screenshot()
95{
David Pinedoeeca2a22015-06-18 17:03:14 -060096 if (!globalLockInitialized)
97 {
98 // TODO/TBD: Need to delete this mutex sometime. How??? One
99 // suggestion is to call this during vkCreateInstance(), and then we
100 // can clean it up during vkDestroyInstance(). However, that requires
101 // that the layer have per-instance locks. We need to come back and
102 // address this soon.
103 loader_platform_thread_create_mutex(&globalLock);
104 globalLockInitialized = 1;
105 }
106}
107
108static void writePPM( const char *filename, VkImage image1)
109{
110 VkImage image2;
111 VkResult err;
112 int x, y;
113 const char *ptr;
114 VkDeviceMemory mem2;
115 VkCmdBuffer cmdBuffer;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600116 VkDevice device = imageMap[image1.handle]->device;
David Pinedoeeca2a22015-06-18 17:03:14 -0600117 VkQueue queue = deviceMap[device]->queue;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600118 int width = imageMap[image1.handle]->imageExtent.width;
119 int height = imageMap[image1.handle]->imageExtent.height;
120 VkFormat format = imageMap[image1.handle]->format;
David Pinedoeeca2a22015-06-18 17:03:14 -0600121 const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR, 0, 0};
122 VkSubresourceLayout sr_layout;
David Pinedoeeca2a22015-06-18 17:03:14 -0600123 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 |
Mark Lobodzinski99d272a2015-07-02 17:09:57 -0600142 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT)
David Pinedoeeca2a22015-06-18 17:03:14 -0600143 };
144 const VkCmdBufferCreateInfo createCommandBufferInfo = {
145 VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
146 NULL,
David Pinedoaa4d1da2015-07-31 10:56:20 -0600147 deviceMap[device]->cmdPool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800148 VK_CMD_BUFFER_LEVEL_PRIMARY,
David Pinedoeeca2a22015-06-18 17:03:14 -0600149 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;
David Pinedoeeca2a22015-06-18 17:03:14 -0600165 uint32_t num_allocations = 0;
166 size_t num_alloc_size = sizeof(num_allocations);
David Pinedoc3256662015-06-30 13:08:37 -0600167 VkLayerDispatchTable* pTableDevice = get_dispatch_table(screenshot_device_table_map, device);
168 VkLayerDispatchTable* pTableQueue = get_dispatch_table(screenshot_device_table_map, queue);
David Pinedoeeca2a22015-06-18 17:03:14 -0600169 VkLayerDispatchTable* pTableCmdBuffer;
170
David Pinedoaa4d1da2015-07-31 10:56:20 -0600171 if (imageMap.empty() || imageMap.find(image1.handle) == imageMap.end())
David Pinedoeeca2a22015-06-18 17:03:14 -0600172 return;
173
174 // The VkImage image1 we are going to dump may not be mappable,
175 // and/or it may have a tiling mode of optimal rather than linear.
176 // To make sure we have an image that we can map and read linearly, we:
177 // create image2 that is mappable and linear
178 // copy image1 to image2
179 // map image2
180 // read from image2's mapped memeory.
181
182 err = pTableDevice->CreateImage(device, &imgCreateInfo, &image2);
183 assert(!err);
184
David Pinedoaa4d1da2015-07-31 10:56:20 -0600185 err = pTableDevice->GetImageMemoryRequirements(device, image2, &memRequirements);
David Pinedoeeca2a22015-06-18 17:03:14 -0600186 assert(!err);
187
188 memAllocInfo.allocationSize = memRequirements.size;
189 err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2);
190 assert(!err);
191
David Pinedoaa4d1da2015-07-31 10:56:20 -0600192 err = pTableQueue->BindImageMemory(device, image2, mem2, 0);
David Pinedoeeca2a22015-06-18 17:03:14 -0600193 assert(!err);
194
195 err = pTableDevice->CreateCommandBuffer(device, &createCommandBufferInfo, &cmdBuffer);
196 assert(!err);
197
198 screenshot_device_table_map.emplace(cmdBuffer, pTableDevice);
199 pTableCmdBuffer = screenshot_device_table_map[cmdBuffer];
200
201 err = pTableCmdBuffer->BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo);
202 assert(!err);
203
204 pTableCmdBuffer->CmdCopyImage(cmdBuffer, image1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
205 image2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1, &imageCopyRegion);
206
207 err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer);
208 assert(!err);
209
210 err = pTableQueue->QueueSubmit(queue, 1, &cmdBuffer, VK_NULL_HANDLE);
211 assert(!err);
212
213 err = pTableQueue->QueueWaitIdle(queue);
214 assert(!err);
215
216 err = pTableDevice->DeviceWaitIdle(device);
217 assert(!err);
218
Tony Barbour426b9052015-06-24 16:06:58 -0600219 err = pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -0600220 assert(!err);
221
222 err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr );
223 assert(!err);
224
225 ptr += sr_layout.offset;
226
227 ofstream file(filename, ios::binary);
228
229 file << "P6\n";
230 file << width << "\n";
231 file << height << "\n";
232 file << 255 << "\n";
233
234 for (y = 0; y < height; y++) {
235 const unsigned int *row = (const unsigned int*) ptr;
236 if (format == VK_FORMAT_B8G8R8A8_UNORM)
237 {
238 for (x = 0; x < width; x++) {
239 unsigned int swapped;
240 swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16;
241 file.write((char *)&swapped, 3);
242 row++;
243 }
244 }
245 else if (format == VK_FORMAT_R8G8B8A8_UNORM)
246 {
247 for (x = 0; x < width; x++) {
248 file.write((char *)row, 3);
249 row++;
250 }
251 }
252 else
253 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600254 // TODO: add support for additional formats
David Pinedoeeca2a22015-06-18 17:03:14 -0600255 printf("Unrecognized image format\n");
256 break;
257 }
258 ptr += sr_layout.rowPitch;
259 }
260 file.close();
261
262 // Clean up
263 err = pTableDevice->UnmapMemory(device, mem2);
264 assert(!err);
265 err = pTableDevice->FreeMemory(device, mem2);
266 assert(!err);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600267 err = pTableDevice->DestroyCommandBuffer(device, cmdBuffer);
David Pinedoeeca2a22015-06-18 17:03:14 -0600268 assert(!err);
269}
270
271
David Pinedo8a4f5562015-06-19 13:48:18 -0600272static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
273{
274 uint32_t i, ext_idx;
275 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
Ian Elliottb134e842015-07-06 14:31:32 -0600276 deviceExtMap[pDisp].wsi_enabled = false;
David Pinedo8a4f5562015-06-19 13:48:18 -0600277 for (i = 0; i < pCreateInfo->extensionCount; i++) {
Ian Elliottb134e842015-07-06 14:31:32 -0600278 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_WSI_DEVICE_SWAPCHAIN_EXTENSION_NAME) == 0)
279 deviceExtMap[pDisp].wsi_enabled = true;
David Pinedo8a4f5562015-06-19 13:48:18 -0600280 }
281}
282
David Pinedoeeca2a22015-06-18 17:03:14 -0600283VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(
284 VkPhysicalDevice gpu,
285 const VkDeviceCreateInfo *pCreateInfo,
286 VkDevice *pDevice)
287{
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600288 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice);
289 VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pDevice);
David Pinedoeeca2a22015-06-18 17:03:14 -0600290
David Pinedo8a4f5562015-06-19 13:48:18 -0600291 if (result == VK_SUCCESS) {
Jon Ashburn59f3dfc2015-07-06 15:09:36 -0600292 init_screenshot();
David Pinedo8a4f5562015-06-19 13:48:18 -0600293 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
294 }
295
David Pinedoeeca2a22015-06-18 17:03:14 -0600296 return result;
297}
298
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600299/* TODO: Probably need a DestroyDevice as well */
300
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600301static const VkLayerProperties ss_device_layers[] = {
David Pinedoeeca2a22015-06-18 17:03:14 -0600302 {
David Pinedoeeca2a22015-06-18 17:03:14 -0600303 "ScreenShot",
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600304 VK_API_VERSION,
305 VK_MAKE_VERSION(0, 1, 0),
David Pinedoeeca2a22015-06-18 17:03:14 -0600306 "Layer: ScreenShot",
David Pinedo8a4f5562015-06-19 13:48:18 -0600307 }
David Pinedoeeca2a22015-06-18 17:03:14 -0600308};
David Pinedoeeca2a22015-06-18 17:03:14 -0600309
Tony Barbour426b9052015-06-24 16:06:58 -0600310VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600311 const char *pLayerName,
312 uint32_t *pCount,
313 VkExtensionProperties* pProperties)
David Pinedoeeca2a22015-06-18 17:03:14 -0600314{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600315 /* ScreenShot does not have any global extensions */
316 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600317}
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600318
319VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalLayerProperties(
320 uint32_t *pCount,
321 VkLayerProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600322{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600323 /* ScreenShot does not have any global layers */
324 return util_GetLayerProperties(0, NULL,
325 pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600326}
327
328VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600329 VkPhysicalDevice physicalDevice,
330 const char* pLayerName,
331 uint32_t* pCount,
332 VkExtensionProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600333{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600334 /* ScreenShot does not have any physical device extensions */
335 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
336}
Tony Barbour426b9052015-06-24 16:06:58 -0600337
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600338VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceLayerProperties(
339 VkPhysicalDevice physicalDevice,
340 uint32_t* pCount,
341 VkLayerProperties* pProperties)
342{
343 return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers),
344 ss_device_layers,
345 pCount, pProperties);
David Pinedoeeca2a22015-06-18 17:03:14 -0600346}
347
348VK_LAYER_EXPORT VkResult VKAPI vkGetDeviceQueue(
349 VkDevice device,
350 uint32_t queueNodeIndex,
351 uint32_t queueIndex,
352 VkQueue *pQueue)
353{
354 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
355 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
356
357 loader_platform_thread_lock_mutex(&globalLock);
358 if (screenshotEnvQueried && screenshotFrames.empty()) {
359 // We are all done taking screenshots, so don't do anything else
360 loader_platform_thread_unlock_mutex(&globalLock);
361 return result;
362 }
363
364 if (result == VK_SUCCESS) {
365 screenshot_device_table_map.emplace(*pQueue, pTable);
366
David Pinedoaa4d1da2015-07-31 10:56:20 -0600367 // Create a device to queue mapping
David Pinedoeeca2a22015-06-18 17:03:14 -0600368 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
369 deviceMapElem->queue = *pQueue;
David Pinedoeeca2a22015-06-18 17:03:14 -0600370 deviceMap.insert(make_pair(device, deviceMapElem));
371 }
372 loader_platform_thread_unlock_mutex(&globalLock);
373 return result;
374}
375
David Pinedoaa4d1da2015-07-31 10:56:20 -0600376VK_LAYER_EXPORT VkResult VKAPI vkCreateCommandPool(
377 VkDevice device,
378 const VkCmdPoolCreateInfo *pCreateInfo,
379 VkCmdPool *pCmdPool)
380{
381 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
382 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateCommandPool(device, pCreateInfo, pCmdPool);
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 // TODO: What if not already in deviceMap???
392 deviceMap[device]->cmdPool = *pCmdPool;
393 loader_platform_thread_unlock_mutex(&globalLock);
394 return result;
395}
396
David Pinedoeeca2a22015-06-18 17:03:14 -0600397VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapChainWSI(
398 VkDevice device,
399 const VkSwapChainCreateInfoWSI *pCreateInfo,
400 VkSwapChainWSI *pSwapChain)
401{
402 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
403 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapChainWSI(device, pCreateInfo, pSwapChain);
404
405 loader_platform_thread_lock_mutex(&globalLock);
406 if (screenshotEnvQueried && screenshotFrames.empty()) {
407 // We are all done taking screenshots, so don't do anything else
408 loader_platform_thread_unlock_mutex(&globalLock);
409 return result;
410 }
411
412 if (result == VK_SUCCESS)
413 {
414 // Create a mapping for a swapchain to a device, image extent, and format
415 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
416 swapchainMapElem->device = device;
417 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
418 swapchainMapElem->format = pCreateInfo->imageFormat;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600419 swapchainMap.insert(make_pair(pSwapChain->handle, swapchainMapElem));
David Pinedoeeca2a22015-06-18 17:03:14 -0600420
421 // Create a mapping for the swapchain object into the dispatch table
David Pinedoaa4d1da2015-07-31 10:56:20 -0600422 screenshot_device_table_map.emplace((void *)pSwapChain->handle, pTable);
David Pinedoeeca2a22015-06-18 17:03:14 -0600423 }
424 loader_platform_thread_unlock_mutex(&globalLock);
425
426 return result;
427}
428
429VK_LAYER_EXPORT VkResult VKAPI vkGetSwapChainInfoWSI(
Ian Elliottb134e842015-07-06 14:31:32 -0600430 VkDevice device,
David Pinedoeeca2a22015-06-18 17:03:14 -0600431 VkSwapChainWSI swapChain,
432 VkSwapChainInfoTypeWSI infoType,
433 size_t *pDataSize,
434 void *pData)
435{
David Pinedoaa4d1da2015-07-31 10:56:20 -0600436 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapChainInfoWSI(device, swapChain, infoType, pDataSize, pData);
David Pinedoeeca2a22015-06-18 17:03:14 -0600437
438 loader_platform_thread_lock_mutex(&globalLock);
439 if (screenshotEnvQueried && screenshotFrames.empty()) {
440 // We are all done taking screenshots, so don't do anything else
441 loader_platform_thread_unlock_mutex(&globalLock);
442 return result;
443 }
444
445 if (result == VK_SUCCESS &&
David Pinedoaa4d1da2015-07-31 10:56:20 -0600446 pData &&
447 !swapchainMap.empty() && swapchainMap.find(swapChain.handle) != swapchainMap.end())
David Pinedoeeca2a22015-06-18 17:03:14 -0600448 {
Ian Elliottb134e842015-07-06 14:31:32 -0600449 VkSwapChainImagePropertiesWSI *swapChainImageInfo = (VkSwapChainImagePropertiesWSI *)pData;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600450 int i;
451
452 for (i=0; i<*pDataSize/sizeof(VkSwapChainImagePropertiesWSI); i++,swapChainImageInfo++)
David Pinedoeeca2a22015-06-18 17:03:14 -0600453 {
454 // Create a mapping for an image to a device, image extent, and format
455 ImageMapStruct *imageMapElem = new ImageMapStruct;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600456 imageMapElem->device = swapchainMap[swapChain.handle]->device;
457 imageMapElem->imageExtent = swapchainMap[swapChain.handle]->imageExtent;
458 imageMapElem->format = swapchainMap[swapChain.handle]->format;
459 imageMap.insert(make_pair(swapChainImageInfo->image.handle, imageMapElem));
David Pinedoeeca2a22015-06-18 17:03:14 -0600460 }
David Pinedoaa4d1da2015-07-31 10:56:20 -0600461
462 // Add list of images to swapchain to image map
463 SwapchainMapStruct *swapchainMapElem = swapchainMap[swapChain.handle];
464 if (i >= 1 && swapchainMapElem)
465 {
466 VkImage *imageList = new VkImage[i];
467 swapchainMapElem->imageList = imageList;
468 VkSwapChainImagePropertiesWSI *swapChainImageInfo = (VkSwapChainImagePropertiesWSI *)pData;
469 for (int j=0; j<i; j++,swapChainImageInfo++)
470 {
471 swapchainMapElem->imageList[j] = swapChainImageInfo->image.handle;
472 }
473 }
474
David Pinedoeeca2a22015-06-18 17:03:14 -0600475 }
476 loader_platform_thread_unlock_mutex(&globalLock);
477 return result;
478}
479
Ian Elliottb134e842015-07-06 14:31:32 -0600480VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentWSI(VkQueue queue, VkPresentInfoWSI* pPresentInfo)
David Pinedoeeca2a22015-06-18 17:03:14 -0600481{
482 static int frameNumber = 0;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600483 if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ }
David Pinedoeeca2a22015-06-18 17:03:14 -0600484 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentWSI(queue, pPresentInfo);
485
486 loader_platform_thread_lock_mutex(&globalLock);
487
488 if (!screenshotEnvQueried)
489 {
490 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
491 if (_vk_screenshot && *_vk_screenshot)
492 {
493 string spec(_vk_screenshot), word;
494 size_t start = 0, comma = 0;
495
496 while (start < spec.size()) {
497 int frameToAdd;
498 comma = spec.find(',', start);
499 if (comma == string::npos)
500 word = string(spec, start);
501 else
502 word = string(spec, start, comma - start);
503 frameToAdd=atoi(word.c_str());
504 // Add the frame number to list, but only do it if the word started with a digit and if
505 // it's not already in the list
506 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
507 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
508 {
509 screenshotFrames.push_back(frameToAdd);
510 }
511 if (comma == string::npos)
512 break;
513 start = comma + 1;
514 }
515 }
516 screenshotEnvQueried = true;
517 }
518
519
520 if (result == VK_SUCCESS && !screenshotFrames.empty())
521 {
522 vector<int>::iterator it;
523 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
524 if (it != screenshotFrames.end())
525 {
526 string fileName;
527 fileName = to_string(frameNumber) + ".ppm";
David Pinedoaa4d1da2015-07-31 10:56:20 -0600528
529 VkImage image;
530 uint64_t swapChain;
531 // We'll dump only one image: the first
532 swapChain = pPresentInfo->swapChains[0].handle;
533 image = swapchainMap[swapChain]->imageList[pPresentInfo->imageIndices[0]];
534 writePPM(fileName.c_str(), image);
David Pinedoeeca2a22015-06-18 17:03:14 -0600535 screenshotFrames.erase(it);
536
537 if (screenshotFrames.empty())
538 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600539 // Free all our maps since we are done with them.
David Pinedoeeca2a22015-06-18 17:03:14 -0600540 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
541 {
542 SwapchainMapStruct *swapchainMapElem = it->second;
543 delete swapchainMapElem;
544 }
545 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
546 {
547 ImageMapStruct *imageMapElem = it->second;
548 delete imageMapElem;
549 }
550 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
551 {
552 DeviceMapStruct *deviceMapElem = it->second;
553 delete deviceMapElem;
554 }
555 swapchainMap.clear();
556 imageMap.clear();
557 deviceMap.clear();
558 }
559 }
560 }
561 frameNumber++;
562 loader_platform_thread_unlock_mutex(&globalLock);
563 return result;
564}
565
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600566VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(
David Pinedoeeca2a22015-06-18 17:03:14 -0600567 VkDevice dev,
568 const char *funcName)
569{
David Pinedoeeca2a22015-06-18 17:03:14 -0600570 if (dev == NULL) {
571 return NULL;
572 }
573
574 /* loader uses this to force layer initialization; device object is wrapped */
575 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
576 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600577 return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
David Pinedoeeca2a22015-06-18 17:03:14 -0600578 }
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600579 if (!strcmp(funcName, "vkCreateDevice"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600580 return (PFN_vkVoidFunction) vkCreateDevice;
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600581
David Pinedoeeca2a22015-06-18 17:03:14 -0600582 if (!strcmp(funcName, "vkGetDeviceQueue"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600583 return (PFN_vkVoidFunction) vkGetDeviceQueue;
584
585 if (!strcmp(funcName, "vkCreateCommandPool"))
586 return (PFN_vkVoidFunction) vkCreateCommandPool;
David Pinedoeeca2a22015-06-18 17:03:14 -0600587
David Pinedo8a4f5562015-06-19 13:48:18 -0600588 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
Ian Elliottb134e842015-07-06 14:31:32 -0600589 if (deviceExtMap.size() == 0 || deviceExtMap[pDisp].wsi_enabled)
David Pinedo8a4f5562015-06-19 13:48:18 -0600590 {
591 if (!strcmp(funcName, "vkCreateSwapChainWSI"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600592 return (PFN_vkVoidFunction) vkCreateSwapChainWSI;
David Pinedo8a4f5562015-06-19 13:48:18 -0600593 if (!strcmp(funcName, "vkGetSwapChainInfoWSI"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600594 return (PFN_vkVoidFunction) vkGetSwapChainInfoWSI;
David Pinedo8a4f5562015-06-19 13:48:18 -0600595 if (!strcmp(funcName, "vkQueuePresentWSI"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600596 return (PFN_vkVoidFunction) vkQueuePresentWSI;
David Pinedo8a4f5562015-06-19 13:48:18 -0600597 }
598
599 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600600 return NULL;
David Pinedo8a4f5562015-06-19 13:48:18 -0600601 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedoeeca2a22015-06-18 17:03:14 -0600602}
David Pinedo07238d42015-07-09 16:23:44 -0600603
604
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600605VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
David Pinedo07238d42015-07-09 16:23:44 -0600606{
David Pinedo07238d42015-07-09 16:23:44 -0600607 if (instance == VK_NULL_HANDLE) {
608 return NULL;
609 }
610
611 /* loader uses this to force layer initialization; instance object is wrapped */
612 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
613 initInstanceTable((const VkBaseLayerObject *) instance);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600614 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
David Pinedo07238d42015-07-09 16:23:44 -0600615 }
616
617 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
618 if (pTable->GetInstanceProcAddr == NULL)
619 return NULL;
620 return pTable->GetInstanceProcAddr(instance, funcName);
David Pinedo07238d42015-07-09 16:23:44 -0600621}