blob: 975ffb322e4ec7d46d127f60e358c962dff28c84 [file] [log] [blame]
David Pinedofb5b5382015-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 Ehlisb835d1b2015-07-03 10:34:49 -060040#include "vk_loader_platform.h"
David Pinedofb5b5382015-06-18 17:03:14 -060041#include "vk_dispatch_table_helper.h"
42#include "vk_struct_string_helper_cpp.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060043#include "vk_layer_config.h"
David Pinedofb5b5382015-06-18 17:03:14 -060044// The following is #included again to catch certain OS-specific functions
45// being used:
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060046#include "vk_loader_platform.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060047#include "vk_layer_table.h"
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -060048#include "vk_layer_extension_utils.h"
David Pinedofb5b5382015-06-18 17:03:14 -060049
David Pinedobcd0e792015-06-19 13:48:18 -060050
51struct devExts {
Ian Elliott1064fe32015-07-06 14:31:32 -060052 bool wsi_enabled;
David Pinedobcd0e792015-06-19 13:48:18 -060053};
54static std::unordered_map<void *, struct devExts> deviceExtMap;
David Pinedofb5b5382015-06-18 17:03:14 -060055static device_table_map screenshot_device_table_map;
David Pinedofb5b5382015-06-18 17:03:14 -060056
57static int globalLockInitialized = 0;
58static loader_platform_thread_mutex globalLock;
59
Cody Northrop49f885c2015-09-01 10:18:45 -060060// unordered map: associates a swap chain with a device, image extent, format, and
David Pinedo8897e192015-07-31 10:56:20 -060061// list of images
David Pinedofb5b5382015-06-18 17:03:14 -060062typedef struct
63{
64 VkDevice device;
65 VkExtent2D imageExtent;
66 VkFormat format;
David Pinedo8897e192015-07-31 10:56:20 -060067 VkImage *imageList;
David Pinedofb5b5382015-06-18 17:03:14 -060068} SwapchainMapStruct;
David Pinedo8897e192015-07-31 10:56:20 -060069static unordered_map<uint64_t, SwapchainMapStruct *> swapchainMap;
David Pinedofb5b5382015-06-18 17:03:14 -060070
Cody Northrop49f885c2015-09-01 10:18:45 -060071// unordered map: associates an image with a device, image extent, and format
David Pinedofb5b5382015-06-18 17:03:14 -060072typedef struct
73{
74 VkDevice device;
75 VkExtent2D imageExtent;
76 VkFormat format;
77} ImageMapStruct;
David Pinedo8897e192015-07-31 10:56:20 -060078static unordered_map<uint64_t, ImageMapStruct *> imageMap;
David Pinedofb5b5382015-06-18 17:03:14 -060079
Cody Northrop49f885c2015-09-01 10:18:45 -060080// unordered map: associates a device with a queue, cmdPool, and physical device
David Pinedofb5b5382015-06-18 17:03:14 -060081typedef struct
82{
83 VkQueue queue;
David Pinedo8897e192015-07-31 10:56:20 -060084 VkCmdPool cmdPool;
Cody Northrop49f885c2015-09-01 10:18:45 -060085 VkPhysicalDevice physicalDevice;
David Pinedofb5b5382015-06-18 17:03:14 -060086} DeviceMapStruct;
87static unordered_map<VkDevice, DeviceMapStruct *> deviceMap;
88
Cody Northrop49f885c2015-09-01 10:18:45 -060089// unordered map: associates a physical device with an instance
90typedef struct
91{
92 VkInstance instance;
93} PhysDeviceMapStruct;
94static unordered_map<VkPhysicalDevice, PhysDeviceMapStruct *> physDeviceMap;
95
David Pinedofb5b5382015-06-18 17:03:14 -060096// List of frames to we will get a screenshot of
97static vector<int> screenshotFrames;
98
99// Flag indicating we have queried _VK_SCREENSHOT env var
100static bool screenshotEnvQueried = false;
101
Cody Northrop49f885c2015-09-01 10:18:45 -0600102static VkResult memory_type_from_properties(
103 VkPhysicalDeviceMemoryProperties *memory_properties,
104 uint32_t typeBits,
105 VkFlags properties,
106 uint32_t *typeIndex)
107{
108 // Search memtypes to find first index with those properties
109 for (uint32_t i = 0; i < 32; i++) {
110 if ((typeBits & 1) == 1) {
111 // Type is available, does it match user properties?
112 if ((memory_properties->memoryTypes[i].propertyFlags & properties) == properties) {
113 *typeIndex = i;
114 return VK_SUCCESS;
115 }
116 }
117 typeBits >>= 1;
118 }
119 // No memory types matched, return failure
120 return VK_UNSUPPORTED;
121}
122
David Pinedofb5b5382015-06-18 17:03:14 -0600123static void init_screenshot()
124{
David Pinedofb5b5382015-06-18 17:03:14 -0600125 if (!globalLockInitialized)
126 {
127 // TODO/TBD: Need to delete this mutex sometime. How??? One
128 // suggestion is to call this during vkCreateInstance(), and then we
129 // can clean it up during vkDestroyInstance(). However, that requires
130 // that the layer have per-instance locks. We need to come back and
131 // address this soon.
132 loader_platform_thread_create_mutex(&globalLock);
133 globalLockInitialized = 1;
134 }
135}
136
137static void writePPM( const char *filename, VkImage image1)
138{
139 VkImage image2;
140 VkResult err;
141 int x, y;
142 const char *ptr;
143 VkDeviceMemory mem2;
144 VkCmdBuffer cmdBuffer;
David Pinedo8897e192015-07-31 10:56:20 -0600145 VkDevice device = imageMap[image1.handle]->device;
Cody Northrop49f885c2015-09-01 10:18:45 -0600146 VkPhysicalDevice physicalDevice = deviceMap[device]->physicalDevice;
147 VkInstance instance = physDeviceMap[physicalDevice]->instance;
David Pinedofb5b5382015-06-18 17:03:14 -0600148 VkQueue queue = deviceMap[device]->queue;
David Pinedo8897e192015-07-31 10:56:20 -0600149 int width = imageMap[image1.handle]->imageExtent.width;
150 int height = imageMap[image1.handle]->imageExtent.height;
151 VkFormat format = imageMap[image1.handle]->format;
David Pinedofb5b5382015-06-18 17:03:14 -0600152 const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR, 0, 0};
153 VkSubresourceLayout sr_layout;
David Pinedofb5b5382015-06-18 17:03:14 -0600154 const VkImageCreateInfo imgCreateInfo = {
155 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
156 NULL,
157 VK_IMAGE_TYPE_2D,
158 format,
159 {width, height, 1},
160 1,
161 1,
162 1,
163 VK_IMAGE_TILING_LINEAR,
164 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT|VK_IMAGE_USAGE_STORAGE_BIT),
165 0
166 };
167 VkMemoryAllocInfo memAllocInfo = {
168 VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
169 NULL,
170 0, // allocationSize, queried later
Cody Northrop488f5472015-09-01 11:47:50 -0600171 0 // memoryTypeIndex, queried later
David Pinedofb5b5382015-06-18 17:03:14 -0600172 };
173 const VkCmdBufferCreateInfo createCommandBufferInfo = {
174 VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
175 NULL,
David Pinedo8897e192015-07-31 10:56:20 -0600176 deviceMap[device]->cmdPool,
Chia-I Wu0b50a1c2015-06-26 15:34:39 +0800177 VK_CMD_BUFFER_LEVEL_PRIMARY,
David Pinedofb5b5382015-06-18 17:03:14 -0600178 0
179 };
180 const VkCmdBufferBeginInfo cmdBufferBeginInfo = {
181 VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
182 NULL,
183 VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
184 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
185 };
186 const VkImageCopy imageCopyRegion = {
187 {VK_IMAGE_ASPECT_COLOR, 0, 0},
188 {0, 0, 0},
189 {VK_IMAGE_ASPECT_COLOR, 0, 0},
190 {0, 0, 0},
191 {width, height, 1}
192 };
193 VkMemoryRequirements memRequirements;
David Pinedofb5b5382015-06-18 17:03:14 -0600194 uint32_t num_allocations = 0;
195 size_t num_alloc_size = sizeof(num_allocations);
David Pinedo68295872015-06-30 13:08:37 -0600196 VkLayerDispatchTable* pTableDevice = get_dispatch_table(screenshot_device_table_map, device);
197 VkLayerDispatchTable* pTableQueue = get_dispatch_table(screenshot_device_table_map, queue);
Cody Northrop49f885c2015-09-01 10:18:45 -0600198 VkLayerInstanceDispatchTable* pInstanceTable;
David Pinedofb5b5382015-06-18 17:03:14 -0600199 VkLayerDispatchTable* pTableCmdBuffer;
Cody Northrop49f885c2015-09-01 10:18:45 -0600200 VkPhysicalDeviceMemoryProperties memory_properties;
David Pinedofb5b5382015-06-18 17:03:14 -0600201
David Pinedo8897e192015-07-31 10:56:20 -0600202 if (imageMap.empty() || imageMap.find(image1.handle) == imageMap.end())
David Pinedofb5b5382015-06-18 17:03:14 -0600203 return;
204
205 // The VkImage image1 we are going to dump may not be mappable,
206 // and/or it may have a tiling mode of optimal rather than linear.
207 // To make sure we have an image that we can map and read linearly, we:
208 // create image2 that is mappable and linear
209 // copy image1 to image2
210 // map image2
211 // read from image2's mapped memeory.
212
213 err = pTableDevice->CreateImage(device, &imgCreateInfo, &image2);
214 assert(!err);
215
David Pinedo8897e192015-07-31 10:56:20 -0600216 err = pTableDevice->GetImageMemoryRequirements(device, image2, &memRequirements);
David Pinedofb5b5382015-06-18 17:03:14 -0600217 assert(!err);
218
219 memAllocInfo.allocationSize = memRequirements.size;
Cody Northrop49f885c2015-09-01 10:18:45 -0600220 pInstanceTable = instance_dispatch_table(instance);
221 err = pInstanceTable->GetPhysicalDeviceMemoryProperties(physicalDevice, &memory_properties);
222 assert(!err);
223
224 err = memory_type_from_properties(&memory_properties,
225 memRequirements.memoryTypeBits,
Cody Northrop488f5472015-09-01 11:47:50 -0600226 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Cody Northrop49f885c2015-09-01 10:18:45 -0600227 &memAllocInfo.memoryTypeIndex);
228 assert(!err);
229
David Pinedofb5b5382015-06-18 17:03:14 -0600230 err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2);
231 assert(!err);
232
David Pinedo8897e192015-07-31 10:56:20 -0600233 err = pTableQueue->BindImageMemory(device, image2, mem2, 0);
David Pinedofb5b5382015-06-18 17:03:14 -0600234 assert(!err);
235
236 err = pTableDevice->CreateCommandBuffer(device, &createCommandBufferInfo, &cmdBuffer);
237 assert(!err);
238
239 screenshot_device_table_map.emplace(cmdBuffer, pTableDevice);
240 pTableCmdBuffer = screenshot_device_table_map[cmdBuffer];
241
242 err = pTableCmdBuffer->BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo);
243 assert(!err);
244
Cody Northrop488f5472015-09-01 11:47:50 -0600245 // TODO: We need to transition images to match these layouts, then restore the original layouts
246 pTableCmdBuffer->CmdCopyImage(cmdBuffer, image1, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
247 image2, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, 1, &imageCopyRegion);
David Pinedofb5b5382015-06-18 17:03:14 -0600248
249 err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer);
250 assert(!err);
251
252 err = pTableQueue->QueueSubmit(queue, 1, &cmdBuffer, VK_NULL_HANDLE);
253 assert(!err);
254
255 err = pTableQueue->QueueWaitIdle(queue);
256 assert(!err);
257
258 err = pTableDevice->DeviceWaitIdle(device);
259 assert(!err);
260
Tony Barbour59a47322015-06-24 16:06:58 -0600261 err = pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout);
David Pinedofb5b5382015-06-18 17:03:14 -0600262 assert(!err);
263
264 err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr );
265 assert(!err);
266
267 ptr += sr_layout.offset;
268
269 ofstream file(filename, ios::binary);
270
271 file << "P6\n";
272 file << width << "\n";
273 file << height << "\n";
274 file << 255 << "\n";
275
276 for (y = 0; y < height; y++) {
277 const unsigned int *row = (const unsigned int*) ptr;
278 if (format == VK_FORMAT_B8G8R8A8_UNORM)
279 {
280 for (x = 0; x < width; x++) {
281 unsigned int swapped;
282 swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16;
283 file.write((char *)&swapped, 3);
284 row++;
285 }
286 }
287 else if (format == VK_FORMAT_R8G8B8A8_UNORM)
288 {
289 for (x = 0; x < width; x++) {
290 file.write((char *)row, 3);
291 row++;
292 }
293 }
294 else
295 {
David Pinedo8897e192015-07-31 10:56:20 -0600296 // TODO: add support for additional formats
David Pinedofb5b5382015-06-18 17:03:14 -0600297 printf("Unrecognized image format\n");
298 break;
299 }
300 ptr += sr_layout.rowPitch;
301 }
302 file.close();
303
304 // Clean up
305 err = pTableDevice->UnmapMemory(device, mem2);
306 assert(!err);
307 err = pTableDevice->FreeMemory(device, mem2);
308 assert(!err);
David Pinedo8897e192015-07-31 10:56:20 -0600309 err = pTableDevice->DestroyCommandBuffer(device, cmdBuffer);
David Pinedofb5b5382015-06-18 17:03:14 -0600310 assert(!err);
311}
312
313
David Pinedobcd0e792015-06-19 13:48:18 -0600314static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
315{
Cody Northropd2ad0342015-08-05 11:15:02 -0600316 uint32_t i;
David Pinedobcd0e792015-06-19 13:48:18 -0600317 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
Ian Elliott1064fe32015-07-06 14:31:32 -0600318 deviceExtMap[pDisp].wsi_enabled = false;
David Pinedobcd0e792015-06-19 13:48:18 -0600319 for (i = 0; i < pCreateInfo->extensionCount; i++) {
Ian Elliott7e40db92015-08-21 15:09:33 -0600320 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_EXT_KHR_DEVICE_SWAPCHAIN_EXTENSION_NAME) == 0)
Ian Elliott1064fe32015-07-06 14:31:32 -0600321 deviceExtMap[pDisp].wsi_enabled = true;
David Pinedobcd0e792015-06-19 13:48:18 -0600322 }
323}
324
David Pinedofb5b5382015-06-18 17:03:14 -0600325VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(
326 VkPhysicalDevice gpu,
327 const VkDeviceCreateInfo *pCreateInfo,
328 VkDevice *pDevice)
329{
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600330 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice);
331 VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pDevice);
David Pinedofb5b5382015-06-18 17:03:14 -0600332
David Pinedobcd0e792015-06-19 13:48:18 -0600333 if (result == VK_SUCCESS) {
Jon Ashburnec105332015-07-06 15:09:36 -0600334 init_screenshot();
David Pinedobcd0e792015-06-19 13:48:18 -0600335 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
Cody Northrop49f885c2015-09-01 10:18:45 -0600336 // Create a mapping from a device to a physicalDevice
337 if (deviceMap[*pDevice] == NULL)
338 {
339 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
340 deviceMap[*pDevice] = deviceMapElem;
341 }
342 deviceMap[*pDevice]->physicalDevice = gpu;
David Pinedobcd0e792015-06-19 13:48:18 -0600343 }
344
David Pinedofb5b5382015-06-18 17:03:14 -0600345 return result;
346}
347
Cody Northrop49f885c2015-09-01 10:18:45 -0600348VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
349 VkInstance instance,
350 uint32_t* pPhysicalDeviceCount,
351 VkPhysicalDevice* pPhysicalDevices)
352{
353 VkResult result;
354
355 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
356 result = pTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
357 if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0)
358 {
359 for (uint32_t i=0; i<*pPhysicalDeviceCount ; i++)
360 {
361 // Create a mapping from a physicalDevice to an instance
362 if (physDeviceMap[pPhysicalDevices[i]] == NULL)
363 {
364 PhysDeviceMapStruct *physDeviceMapElem = new PhysDeviceMapStruct;
365 physDeviceMap[pPhysicalDevices[i]] = physDeviceMapElem;
366 }
367 physDeviceMap[pPhysicalDevices[i]]->instance = instance;
368 }
369 }
370 return result;
371}
372
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600373/* TODO: Probably need a DestroyDevice as well */
374
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600375static const VkLayerProperties ss_device_layers[] = {
David Pinedofb5b5382015-06-18 17:03:14 -0600376 {
David Pinedofb5b5382015-06-18 17:03:14 -0600377 "ScreenShot",
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600378 VK_API_VERSION,
379 VK_MAKE_VERSION(0, 1, 0),
David Pinedofb5b5382015-06-18 17:03:14 -0600380 "Layer: ScreenShot",
David Pinedobcd0e792015-06-19 13:48:18 -0600381 }
David Pinedofb5b5382015-06-18 17:03:14 -0600382};
David Pinedofb5b5382015-06-18 17:03:14 -0600383
Tony Barbour59a47322015-06-24 16:06:58 -0600384VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionProperties(
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600385 const char *pLayerName,
386 uint32_t *pCount,
387 VkExtensionProperties* pProperties)
David Pinedofb5b5382015-06-18 17:03:14 -0600388{
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600389 /* ScreenShot does not have any global extensions */
390 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600391}
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600392
393VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalLayerProperties(
394 uint32_t *pCount,
395 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600396{
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600397 /* ScreenShot does not have any global layers */
398 return util_GetLayerProperties(0, NULL,
399 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600400}
401
402VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionProperties(
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600403 VkPhysicalDevice physicalDevice,
404 const char* pLayerName,
405 uint32_t* pCount,
406 VkExtensionProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600407{
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600408 /* ScreenShot does not have any physical device extensions */
409 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
410}
Tony Barbour59a47322015-06-24 16:06:58 -0600411
Courtney Goeltzenleuchtera6db3462015-07-07 11:25:43 -0600412VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceLayerProperties(
413 VkPhysicalDevice physicalDevice,
414 uint32_t* pCount,
415 VkLayerProperties* pProperties)
416{
417 return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers),
418 ss_device_layers,
419 pCount, pProperties);
David Pinedofb5b5382015-06-18 17:03:14 -0600420}
421
422VK_LAYER_EXPORT VkResult VKAPI vkGetDeviceQueue(
423 VkDevice device,
424 uint32_t queueNodeIndex,
425 uint32_t queueIndex,
426 VkQueue *pQueue)
427{
428 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
429 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
430
431 loader_platform_thread_lock_mutex(&globalLock);
432 if (screenshotEnvQueried && screenshotFrames.empty()) {
433 // We are all done taking screenshots, so don't do anything else
434 loader_platform_thread_unlock_mutex(&globalLock);
435 return result;
436 }
437
438 if (result == VK_SUCCESS) {
439 screenshot_device_table_map.emplace(*pQueue, pTable);
440
Cody Northrop49f885c2015-09-01 10:18:45 -0600441 // Create a mapping from a device to a queue
442 if (deviceMap[device] == NULL)
443 {
444 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
445 deviceMap[device] = deviceMapElem;
446 }
447 deviceMap[device]->queue = *pQueue;
David Pinedofb5b5382015-06-18 17:03:14 -0600448 }
449 loader_platform_thread_unlock_mutex(&globalLock);
450 return result;
451}
452
David Pinedo8897e192015-07-31 10:56:20 -0600453VK_LAYER_EXPORT VkResult VKAPI vkCreateCommandPool(
454 VkDevice device,
455 const VkCmdPoolCreateInfo *pCreateInfo,
456 VkCmdPool *pCmdPool)
457{
458 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
459 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateCommandPool(device, pCreateInfo, pCmdPool);
460
461 loader_platform_thread_lock_mutex(&globalLock);
462 if (screenshotEnvQueried && screenshotFrames.empty()) {
463 // We are all done taking screenshots, so don't do anything else
464 loader_platform_thread_unlock_mutex(&globalLock);
465 return result;
466 }
467
Cody Northrop49f885c2015-09-01 10:18:45 -0600468 // Create a mapping from a device to a cmdPool
469 if (deviceMap[device] == NULL)
470 {
471 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
472 deviceMap[device] = deviceMapElem;
473 }
David Pinedo8897e192015-07-31 10:56:20 -0600474 deviceMap[device]->cmdPool = *pCmdPool;
475 loader_platform_thread_unlock_mutex(&globalLock);
476 return result;
477}
478
Ian Elliott7e40db92015-08-21 15:09:33 -0600479VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapchainKHR(
David Pinedofb5b5382015-06-18 17:03:14 -0600480 VkDevice device,
Ian Elliott7e40db92015-08-21 15:09:33 -0600481 const VkSwapchainCreateInfoKHR *pCreateInfo,
482 VkSwapchainKHR *pSwapchain)
David Pinedofb5b5382015-06-18 17:03:14 -0600483{
484 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Ian Elliott7e40db92015-08-21 15:09:33 -0600485 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapchainKHR(device, pCreateInfo, pSwapchain);
David Pinedofb5b5382015-06-18 17:03:14 -0600486
487 loader_platform_thread_lock_mutex(&globalLock);
488 if (screenshotEnvQueried && screenshotFrames.empty()) {
489 // We are all done taking screenshots, so don't do anything else
490 loader_platform_thread_unlock_mutex(&globalLock);
491 return result;
492 }
493
494 if (result == VK_SUCCESS)
495 {
496 // Create a mapping for a swapchain to a device, image extent, and format
497 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
498 swapchainMapElem->device = device;
499 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
500 swapchainMapElem->format = pCreateInfo->imageFormat;
Ian Elliott7e40db92015-08-21 15:09:33 -0600501 swapchainMap.insert(make_pair(pSwapchain->handle, swapchainMapElem));
David Pinedofb5b5382015-06-18 17:03:14 -0600502
503 // Create a mapping for the swapchain object into the dispatch table
Ian Elliott7e40db92015-08-21 15:09:33 -0600504 screenshot_device_table_map.emplace((void *)pSwapchain->handle, pTable);
David Pinedofb5b5382015-06-18 17:03:14 -0600505 }
506 loader_platform_thread_unlock_mutex(&globalLock);
507
508 return result;
509}
510
Ian Elliott7e40db92015-08-21 15:09:33 -0600511VK_LAYER_EXPORT VkResult VKAPI vkGetSwapchainImagesKHR(
Ian Elliott1064fe32015-07-06 14:31:32 -0600512 VkDevice device,
Ian Elliott7e40db92015-08-21 15:09:33 -0600513 VkSwapchainKHR swapchain,
Ian Elliott2b6b68a2015-08-07 14:11:14 -0600514 uint32_t* pCount,
Ian Elliott7e40db92015-08-21 15:09:33 -0600515 VkImage* pSwapchainImages)
David Pinedofb5b5382015-06-18 17:03:14 -0600516{
Ian Elliott7e40db92015-08-21 15:09:33 -0600517 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapchainImagesKHR(device, swapchain, pCount, pSwapchainImages);
David Pinedofb5b5382015-06-18 17:03:14 -0600518
519 loader_platform_thread_lock_mutex(&globalLock);
520 if (screenshotEnvQueried && screenshotFrames.empty()) {
521 // We are all done taking screenshots, so don't do anything else
522 loader_platform_thread_unlock_mutex(&globalLock);
523 return result;
524 }
525
526 if (result == VK_SUCCESS &&
Ian Elliott7e40db92015-08-21 15:09:33 -0600527 pSwapchainImages &&
528 !swapchainMap.empty() && swapchainMap.find(swapchain.handle) != swapchainMap.end())
Ian Elliott2b6b68a2015-08-07 14:11:14 -0600529 {
Cody Northropbf065822015-09-01 11:48:13 -0600530 unsigned i;
David Pinedo8897e192015-07-31 10:56:20 -0600531
Ian Elliott2b6b68a2015-08-07 14:11:14 -0600532 for (i=0; i<*pCount; i++)
David Pinedofb5b5382015-06-18 17:03:14 -0600533 {
534 // Create a mapping for an image to a device, image extent, and format
Ian Elliott7e40db92015-08-21 15:09:33 -0600535 if (imageMap[pSwapchainImages[i].handle] == NULL)
Cody Northrop49f885c2015-09-01 10:18:45 -0600536 {
537 ImageMapStruct *imageMapElem = new ImageMapStruct;
Ian Elliott7e40db92015-08-21 15:09:33 -0600538 imageMap[pSwapchainImages[i].handle] = imageMapElem;
Cody Northrop49f885c2015-09-01 10:18:45 -0600539 }
Ian Elliott7e40db92015-08-21 15:09:33 -0600540 imageMap[pSwapchainImages[i].handle]->device = swapchainMap[swapchain.handle]->device;
541 imageMap[pSwapchainImages[i].handle]->imageExtent = swapchainMap[swapchain.handle]->imageExtent;
542 imageMap[pSwapchainImages[i].handle]->format = swapchainMap[swapchain.handle]->format;
David Pinedofb5b5382015-06-18 17:03:14 -0600543 }
David Pinedo8897e192015-07-31 10:56:20 -0600544
545 // Add list of images to swapchain to image map
Ian Elliott7e40db92015-08-21 15:09:33 -0600546 SwapchainMapStruct *swapchainMapElem = swapchainMap[swapchain.handle];
David Pinedo8897e192015-07-31 10:56:20 -0600547 if (i >= 1 && swapchainMapElem)
548 {
549 VkImage *imageList = new VkImage[i];
550 swapchainMapElem->imageList = imageList;
Cody Northropbf065822015-09-01 11:48:13 -0600551 for (unsigned j=0; j<i; j++)
David Pinedo8897e192015-07-31 10:56:20 -0600552 {
Ian Elliott7e40db92015-08-21 15:09:33 -0600553 swapchainMapElem->imageList[j] = pSwapchainImages[j].handle;
David Pinedo8897e192015-07-31 10:56:20 -0600554 }
555 }
556
David Pinedofb5b5382015-06-18 17:03:14 -0600557 }
558 loader_platform_thread_unlock_mutex(&globalLock);
559 return result;
560}
561
Ian Elliott7e40db92015-08-21 15:09:33 -0600562VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentKHR(VkQueue queue, VkPresentInfoKHR* pPresentInfo)
David Pinedofb5b5382015-06-18 17:03:14 -0600563{
564 static int frameNumber = 0;
David Pinedo8897e192015-07-31 10:56:20 -0600565 if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ }
Ian Elliott7e40db92015-08-21 15:09:33 -0600566 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentKHR(queue, pPresentInfo);
David Pinedofb5b5382015-06-18 17:03:14 -0600567
568 loader_platform_thread_lock_mutex(&globalLock);
569
570 if (!screenshotEnvQueried)
571 {
572 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
573 if (_vk_screenshot && *_vk_screenshot)
574 {
575 string spec(_vk_screenshot), word;
576 size_t start = 0, comma = 0;
577
578 while (start < spec.size()) {
579 int frameToAdd;
580 comma = spec.find(',', start);
581 if (comma == string::npos)
582 word = string(spec, start);
583 else
584 word = string(spec, start, comma - start);
585 frameToAdd=atoi(word.c_str());
586 // Add the frame number to list, but only do it if the word started with a digit and if
587 // it's not already in the list
588 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
589 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
590 {
591 screenshotFrames.push_back(frameToAdd);
592 }
593 if (comma == string::npos)
594 break;
595 start = comma + 1;
596 }
597 }
598 screenshotEnvQueried = true;
599 }
600
601
602 if (result == VK_SUCCESS && !screenshotFrames.empty())
603 {
604 vector<int>::iterator it;
605 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
606 if (it != screenshotFrames.end())
607 {
608 string fileName;
609 fileName = to_string(frameNumber) + ".ppm";
David Pinedo8897e192015-07-31 10:56:20 -0600610
611 VkImage image;
Ian Elliott7e40db92015-08-21 15:09:33 -0600612 uint64_t swapchain;
David Pinedo8897e192015-07-31 10:56:20 -0600613 // We'll dump only one image: the first
Ian Elliott7e40db92015-08-21 15:09:33 -0600614 swapchain = pPresentInfo->swapchains[0].handle;
615 image = swapchainMap[swapchain]->imageList[pPresentInfo->imageIndices[0]];
David Pinedo8897e192015-07-31 10:56:20 -0600616 writePPM(fileName.c_str(), image);
David Pinedofb5b5382015-06-18 17:03:14 -0600617 screenshotFrames.erase(it);
618
619 if (screenshotFrames.empty())
620 {
David Pinedo8897e192015-07-31 10:56:20 -0600621 // Free all our maps since we are done with them.
David Pinedofb5b5382015-06-18 17:03:14 -0600622 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
623 {
624 SwapchainMapStruct *swapchainMapElem = it->second;
625 delete swapchainMapElem;
626 }
627 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
628 {
629 ImageMapStruct *imageMapElem = it->second;
630 delete imageMapElem;
631 }
632 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
633 {
634 DeviceMapStruct *deviceMapElem = it->second;
635 delete deviceMapElem;
636 }
Cody Northrop49f885c2015-09-01 10:18:45 -0600637 for (auto it = physDeviceMap.begin(); it != physDeviceMap.end(); it++)
638 {
639 PhysDeviceMapStruct *physDeviceMapElem = it->second;
640 delete physDeviceMapElem;
641 }
David Pinedofb5b5382015-06-18 17:03:14 -0600642 swapchainMap.clear();
643 imageMap.clear();
644 deviceMap.clear();
Cody Northrop49f885c2015-09-01 10:18:45 -0600645 physDeviceMap.clear();
David Pinedofb5b5382015-06-18 17:03:14 -0600646 }
647 }
648 }
649 frameNumber++;
650 loader_platform_thread_unlock_mutex(&globalLock);
651 return result;
652}
653
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -0600654VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(
David Pinedofb5b5382015-06-18 17:03:14 -0600655 VkDevice dev,
656 const char *funcName)
657{
David Pinedofb5b5382015-06-18 17:03:14 -0600658 if (dev == NULL) {
659 return NULL;
660 }
661
662 /* loader uses this to force layer initialization; device object is wrapped */
663 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
664 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
David Pinedo8897e192015-07-31 10:56:20 -0600665 return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
David Pinedofb5b5382015-06-18 17:03:14 -0600666 }
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600667 if (!strcmp(funcName, "vkCreateDevice"))
David Pinedo8897e192015-07-31 10:56:20 -0600668 return (PFN_vkVoidFunction) vkCreateDevice;
Courtney Goeltzenleuchterca173b82015-06-25 18:01:43 -0600669
David Pinedofb5b5382015-06-18 17:03:14 -0600670 if (!strcmp(funcName, "vkGetDeviceQueue"))
David Pinedo8897e192015-07-31 10:56:20 -0600671 return (PFN_vkVoidFunction) vkGetDeviceQueue;
672
673 if (!strcmp(funcName, "vkCreateCommandPool"))
674 return (PFN_vkVoidFunction) vkCreateCommandPool;
David Pinedofb5b5382015-06-18 17:03:14 -0600675
David Pinedobcd0e792015-06-19 13:48:18 -0600676 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
Ian Elliott1064fe32015-07-06 14:31:32 -0600677 if (deviceExtMap.size() == 0 || deviceExtMap[pDisp].wsi_enabled)
David Pinedobcd0e792015-06-19 13:48:18 -0600678 {
Ian Elliott7e40db92015-08-21 15:09:33 -0600679 if (!strcmp(funcName, "vkCreateSwapchainKHR"))
680 return (PFN_vkVoidFunction) vkCreateSwapchainKHR;
681 if (!strcmp(funcName, "vkGetSwapchainImagesKHR"))
682 return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR;
683 if (!strcmp(funcName, "vkQueuePresentKHR"))
684 return (PFN_vkVoidFunction) vkQueuePresentKHR;
David Pinedobcd0e792015-06-19 13:48:18 -0600685 }
686
687 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedofb5b5382015-06-18 17:03:14 -0600688 return NULL;
David Pinedobcd0e792015-06-19 13:48:18 -0600689 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedofb5b5382015-06-18 17:03:14 -0600690}
David Pinedo38310942015-07-09 16:23:44 -0600691
692
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -0600693VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
David Pinedo38310942015-07-09 16:23:44 -0600694{
David Pinedo38310942015-07-09 16:23:44 -0600695 if (instance == VK_NULL_HANDLE) {
696 return NULL;
697 }
698
699 /* loader uses this to force layer initialization; instance object is wrapped */
700 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
701 initInstanceTable((const VkBaseLayerObject *) instance);
David Pinedo8897e192015-07-31 10:56:20 -0600702 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
David Pinedo38310942015-07-09 16:23:44 -0600703 }
704
Cody Northrop49f885c2015-09-01 10:18:45 -0600705 if (!strcmp(funcName, "vkEnumeratePhysicalDevices"))
706 return (PFN_vkVoidFunction)vkEnumeratePhysicalDevices;
707
David Pinedo38310942015-07-09 16:23:44 -0600708 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
709 if (pTable->GetInstanceProcAddr == NULL)
710 return NULL;
711 return pTable->GetInstanceProcAddr(instance, funcName);
David Pinedo38310942015-07-09 16:23:44 -0600712}