blob: a9313bb71372ab0d042fbbf75ab6761868ff4b2c [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{
Cody Northrop1684adb2015-08-05 11:15:02 -0600274 uint32_t i;
David Pinedo8a4f5562015-06-19 13:48:18 -0600275 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
Ian Elliottccac0232015-08-07 14:11:14 -0600429VK_LAYER_EXPORT VkResult VKAPI vkGetSwapChainImagesWSI(
Ian Elliottb134e842015-07-06 14:31:32 -0600430 VkDevice device,
David Pinedoeeca2a22015-06-18 17:03:14 -0600431 VkSwapChainWSI swapChain,
Ian Elliottccac0232015-08-07 14:11:14 -0600432 uint32_t* pCount,
433 VkImage* pSwapChainImages)
David Pinedoeeca2a22015-06-18 17:03:14 -0600434{
Ian Elliottccac0232015-08-07 14:11:14 -0600435 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapChainImagesWSI(device, swapChain, pCount, pSwapChainImages);
David Pinedoeeca2a22015-06-18 17:03:14 -0600436
437 loader_platform_thread_lock_mutex(&globalLock);
438 if (screenshotEnvQueried && screenshotFrames.empty()) {
439 // We are all done taking screenshots, so don't do anything else
440 loader_platform_thread_unlock_mutex(&globalLock);
441 return result;
442 }
443
444 if (result == VK_SUCCESS &&
Ian Elliottccac0232015-08-07 14:11:14 -0600445 pSwapChainImages &&
David Pinedoaa4d1da2015-07-31 10:56:20 -0600446 !swapchainMap.empty() && swapchainMap.find(swapChain.handle) != swapchainMap.end())
Ian Elliottccac0232015-08-07 14:11:14 -0600447 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600448 int i;
449
Ian Elliottccac0232015-08-07 14:11:14 -0600450 for (i=0; i<*pCount; i++)
David Pinedoeeca2a22015-06-18 17:03:14 -0600451 {
452 // Create a mapping for an image to a device, image extent, and format
453 ImageMapStruct *imageMapElem = new ImageMapStruct;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600454 imageMapElem->device = swapchainMap[swapChain.handle]->device;
455 imageMapElem->imageExtent = swapchainMap[swapChain.handle]->imageExtent;
456 imageMapElem->format = swapchainMap[swapChain.handle]->format;
Ian Elliottccac0232015-08-07 14:11:14 -0600457 imageMap.insert(make_pair(pSwapChainImages[i].handle, imageMapElem));
David Pinedoeeca2a22015-06-18 17:03:14 -0600458 }
David Pinedoaa4d1da2015-07-31 10:56:20 -0600459
460 // Add list of images to swapchain to image map
461 SwapchainMapStruct *swapchainMapElem = swapchainMap[swapChain.handle];
462 if (i >= 1 && swapchainMapElem)
463 {
464 VkImage *imageList = new VkImage[i];
465 swapchainMapElem->imageList = imageList;
Ian Elliottccac0232015-08-07 14:11:14 -0600466 for (int j=0; j<i; j++)
David Pinedoaa4d1da2015-07-31 10:56:20 -0600467 {
Ian Elliottccac0232015-08-07 14:11:14 -0600468 swapchainMapElem->imageList[j] = pSwapChainImages[j].handle;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600469 }
470 }
471
David Pinedoeeca2a22015-06-18 17:03:14 -0600472 }
473 loader_platform_thread_unlock_mutex(&globalLock);
474 return result;
475}
476
Ian Elliottb134e842015-07-06 14:31:32 -0600477VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentWSI(VkQueue queue, VkPresentInfoWSI* pPresentInfo)
David Pinedoeeca2a22015-06-18 17:03:14 -0600478{
479 static int frameNumber = 0;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600480 if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ }
David Pinedoeeca2a22015-06-18 17:03:14 -0600481 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentWSI(queue, pPresentInfo);
482
483 loader_platform_thread_lock_mutex(&globalLock);
484
485 if (!screenshotEnvQueried)
486 {
487 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
488 if (_vk_screenshot && *_vk_screenshot)
489 {
490 string spec(_vk_screenshot), word;
491 size_t start = 0, comma = 0;
492
493 while (start < spec.size()) {
494 int frameToAdd;
495 comma = spec.find(',', start);
496 if (comma == string::npos)
497 word = string(spec, start);
498 else
499 word = string(spec, start, comma - start);
500 frameToAdd=atoi(word.c_str());
501 // Add the frame number to list, but only do it if the word started with a digit and if
502 // it's not already in the list
503 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
504 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
505 {
506 screenshotFrames.push_back(frameToAdd);
507 }
508 if (comma == string::npos)
509 break;
510 start = comma + 1;
511 }
512 }
513 screenshotEnvQueried = true;
514 }
515
516
517 if (result == VK_SUCCESS && !screenshotFrames.empty())
518 {
519 vector<int>::iterator it;
520 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
521 if (it != screenshotFrames.end())
522 {
523 string fileName;
524 fileName = to_string(frameNumber) + ".ppm";
David Pinedoaa4d1da2015-07-31 10:56:20 -0600525
526 VkImage image;
527 uint64_t swapChain;
528 // We'll dump only one image: the first
529 swapChain = pPresentInfo->swapChains[0].handle;
530 image = swapchainMap[swapChain]->imageList[pPresentInfo->imageIndices[0]];
531 writePPM(fileName.c_str(), image);
David Pinedoeeca2a22015-06-18 17:03:14 -0600532 screenshotFrames.erase(it);
533
534 if (screenshotFrames.empty())
535 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600536 // Free all our maps since we are done with them.
David Pinedoeeca2a22015-06-18 17:03:14 -0600537 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
538 {
539 SwapchainMapStruct *swapchainMapElem = it->second;
540 delete swapchainMapElem;
541 }
542 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
543 {
544 ImageMapStruct *imageMapElem = it->second;
545 delete imageMapElem;
546 }
547 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
548 {
549 DeviceMapStruct *deviceMapElem = it->second;
550 delete deviceMapElem;
551 }
552 swapchainMap.clear();
553 imageMap.clear();
554 deviceMap.clear();
555 }
556 }
557 }
558 frameNumber++;
559 loader_platform_thread_unlock_mutex(&globalLock);
560 return result;
561}
562
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600563VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(
David Pinedoeeca2a22015-06-18 17:03:14 -0600564 VkDevice dev,
565 const char *funcName)
566{
David Pinedoeeca2a22015-06-18 17:03:14 -0600567 if (dev == NULL) {
568 return NULL;
569 }
570
571 /* loader uses this to force layer initialization; device object is wrapped */
572 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
573 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600574 return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
David Pinedoeeca2a22015-06-18 17:03:14 -0600575 }
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600576 if (!strcmp(funcName, "vkCreateDevice"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600577 return (PFN_vkVoidFunction) vkCreateDevice;
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600578
David Pinedoeeca2a22015-06-18 17:03:14 -0600579 if (!strcmp(funcName, "vkGetDeviceQueue"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600580 return (PFN_vkVoidFunction) vkGetDeviceQueue;
581
582 if (!strcmp(funcName, "vkCreateCommandPool"))
583 return (PFN_vkVoidFunction) vkCreateCommandPool;
David Pinedoeeca2a22015-06-18 17:03:14 -0600584
David Pinedo8a4f5562015-06-19 13:48:18 -0600585 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
Ian Elliottb134e842015-07-06 14:31:32 -0600586 if (deviceExtMap.size() == 0 || deviceExtMap[pDisp].wsi_enabled)
David Pinedo8a4f5562015-06-19 13:48:18 -0600587 {
588 if (!strcmp(funcName, "vkCreateSwapChainWSI"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600589 return (PFN_vkVoidFunction) vkCreateSwapChainWSI;
Ian Elliottccac0232015-08-07 14:11:14 -0600590 if (!strcmp(funcName, "vkGetSwapChainImagesWSI"))
591 return (PFN_vkVoidFunction) vkGetSwapChainImagesWSI;
David Pinedo8a4f5562015-06-19 13:48:18 -0600592 if (!strcmp(funcName, "vkQueuePresentWSI"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600593 return (PFN_vkVoidFunction) vkQueuePresentWSI;
David Pinedo8a4f5562015-06-19 13:48:18 -0600594 }
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}
David Pinedo07238d42015-07-09 16:23:44 -0600600
601
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600602VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
David Pinedo07238d42015-07-09 16:23:44 -0600603{
David Pinedo07238d42015-07-09 16:23:44 -0600604 if (instance == VK_NULL_HANDLE) {
605 return NULL;
606 }
607
608 /* loader uses this to force layer initialization; instance object is wrapped */
609 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
610 initInstanceTable((const VkBaseLayerObject *) instance);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600611 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
David Pinedo07238d42015-07-09 16:23:44 -0600612 }
613
614 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
615 if (pTable->GetInstanceProcAddr == NULL)
616 return NULL;
617 return pTable->GetInstanceProcAddr(instance, funcName);
David Pinedo07238d42015-07-09 16:23:44 -0600618}