blob: dca91a04d5e0c95a4cfc63e2cfc616368630514e [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
Cody Northrop98fb22a2015-09-01 10:18:45 -060060// unordered map: associates a swap chain with a device, image extent, format, and
David Pinedoaa4d1da2015-07-31 10:56:20 -060061// 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
Cody Northrop98fb22a2015-09-01 10:18:45 -060071// unordered map: associates an image with a device, image extent, and format
David Pinedoeeca2a22015-06-18 17:03:14 -060072typedef 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
Cody Northrop98fb22a2015-09-01 10:18:45 -060080// unordered map: associates a device with a queue, cmdPool, and physical device
David Pinedoeeca2a22015-06-18 17:03:14 -060081typedef struct
82{
83 VkQueue queue;
David Pinedoaa4d1da2015-07-31 10:56:20 -060084 VkCmdPool cmdPool;
Cody Northrop98fb22a2015-09-01 10:18:45 -060085 VkPhysicalDevice physicalDevice;
David Pinedoeeca2a22015-06-18 17:03:14 -060086} DeviceMapStruct;
87static unordered_map<VkDevice, DeviceMapStruct *> deviceMap;
88
Cody Northrop98fb22a2015-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 Pinedoeeca2a22015-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 Northrop98fb22a2015-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 Pinedoeeca2a22015-06-18 17:03:14 -0600123static void init_screenshot()
124{
David Pinedoeeca2a22015-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 Pinedoaa4d1da2015-07-31 10:56:20 -0600145 VkDevice device = imageMap[image1.handle]->device;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600146 VkPhysicalDevice physicalDevice = deviceMap[device]->physicalDevice;
147 VkInstance instance = physDeviceMap[physicalDevice]->instance;
David Pinedoeeca2a22015-06-18 17:03:14 -0600148 VkQueue queue = deviceMap[device]->queue;
David Pinedoaa4d1da2015-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 Pinedoeeca2a22015-06-18 17:03:14 -0600152 const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR, 0, 0};
153 VkSubresourceLayout sr_layout;
David Pinedoeeca2a22015-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
171 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
172 VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT |
Mark Lobodzinski99d272a2015-07-02 17:09:57 -0600173 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT)
David Pinedoeeca2a22015-06-18 17:03:14 -0600174 };
175 const VkCmdBufferCreateInfo createCommandBufferInfo = {
176 VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
177 NULL,
David Pinedoaa4d1da2015-07-31 10:56:20 -0600178 deviceMap[device]->cmdPool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800179 VK_CMD_BUFFER_LEVEL_PRIMARY,
David Pinedoeeca2a22015-06-18 17:03:14 -0600180 0
181 };
182 const VkCmdBufferBeginInfo cmdBufferBeginInfo = {
183 VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
184 NULL,
185 VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
186 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
187 };
188 const VkImageCopy imageCopyRegion = {
189 {VK_IMAGE_ASPECT_COLOR, 0, 0},
190 {0, 0, 0},
191 {VK_IMAGE_ASPECT_COLOR, 0, 0},
192 {0, 0, 0},
193 {width, height, 1}
194 };
195 VkMemoryRequirements memRequirements;
David Pinedoeeca2a22015-06-18 17:03:14 -0600196 uint32_t num_allocations = 0;
197 size_t num_alloc_size = sizeof(num_allocations);
David Pinedoc3256662015-06-30 13:08:37 -0600198 VkLayerDispatchTable* pTableDevice = get_dispatch_table(screenshot_device_table_map, device);
199 VkLayerDispatchTable* pTableQueue = get_dispatch_table(screenshot_device_table_map, queue);
Cody Northrop98fb22a2015-09-01 10:18:45 -0600200 VkLayerInstanceDispatchTable* pInstanceTable;
David Pinedoeeca2a22015-06-18 17:03:14 -0600201 VkLayerDispatchTable* pTableCmdBuffer;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600202 VkPhysicalDeviceMemoryProperties memory_properties;
David Pinedoeeca2a22015-06-18 17:03:14 -0600203
David Pinedoaa4d1da2015-07-31 10:56:20 -0600204 if (imageMap.empty() || imageMap.find(image1.handle) == imageMap.end())
David Pinedoeeca2a22015-06-18 17:03:14 -0600205 return;
206
207 // The VkImage image1 we are going to dump may not be mappable,
208 // and/or it may have a tiling mode of optimal rather than linear.
209 // To make sure we have an image that we can map and read linearly, we:
210 // create image2 that is mappable and linear
211 // copy image1 to image2
212 // map image2
213 // read from image2's mapped memeory.
214
215 err = pTableDevice->CreateImage(device, &imgCreateInfo, &image2);
216 assert(!err);
217
David Pinedoaa4d1da2015-07-31 10:56:20 -0600218 err = pTableDevice->GetImageMemoryRequirements(device, image2, &memRequirements);
David Pinedoeeca2a22015-06-18 17:03:14 -0600219 assert(!err);
220
221 memAllocInfo.allocationSize = memRequirements.size;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600222 pInstanceTable = instance_dispatch_table(instance);
223 err = pInstanceTable->GetPhysicalDeviceMemoryProperties(physicalDevice, &memory_properties);
224 assert(!err);
225
226 err = memory_type_from_properties(&memory_properties,
227 memRequirements.memoryTypeBits,
228 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
229 VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT |
230 VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT),
231 &memAllocInfo.memoryTypeIndex);
232 assert(!err);
233
David Pinedoeeca2a22015-06-18 17:03:14 -0600234 err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2);
235 assert(!err);
236
David Pinedoaa4d1da2015-07-31 10:56:20 -0600237 err = pTableQueue->BindImageMemory(device, image2, mem2, 0);
David Pinedoeeca2a22015-06-18 17:03:14 -0600238 assert(!err);
239
240 err = pTableDevice->CreateCommandBuffer(device, &createCommandBufferInfo, &cmdBuffer);
241 assert(!err);
242
243 screenshot_device_table_map.emplace(cmdBuffer, pTableDevice);
244 pTableCmdBuffer = screenshot_device_table_map[cmdBuffer];
245
246 err = pTableCmdBuffer->BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo);
247 assert(!err);
248
249 pTableCmdBuffer->CmdCopyImage(cmdBuffer, image1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
250 image2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1, &imageCopyRegion);
251
252 err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer);
253 assert(!err);
254
255 err = pTableQueue->QueueSubmit(queue, 1, &cmdBuffer, VK_NULL_HANDLE);
256 assert(!err);
257
258 err = pTableQueue->QueueWaitIdle(queue);
259 assert(!err);
260
261 err = pTableDevice->DeviceWaitIdle(device);
262 assert(!err);
263
Tony Barbour426b9052015-06-24 16:06:58 -0600264 err = pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -0600265 assert(!err);
266
267 err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr );
268 assert(!err);
269
270 ptr += sr_layout.offset;
271
272 ofstream file(filename, ios::binary);
273
274 file << "P6\n";
275 file << width << "\n";
276 file << height << "\n";
277 file << 255 << "\n";
278
279 for (y = 0; y < height; y++) {
280 const unsigned int *row = (const unsigned int*) ptr;
281 if (format == VK_FORMAT_B8G8R8A8_UNORM)
282 {
283 for (x = 0; x < width; x++) {
284 unsigned int swapped;
285 swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16;
286 file.write((char *)&swapped, 3);
287 row++;
288 }
289 }
290 else if (format == VK_FORMAT_R8G8B8A8_UNORM)
291 {
292 for (x = 0; x < width; x++) {
293 file.write((char *)row, 3);
294 row++;
295 }
296 }
297 else
298 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600299 // TODO: add support for additional formats
David Pinedoeeca2a22015-06-18 17:03:14 -0600300 printf("Unrecognized image format\n");
301 break;
302 }
303 ptr += sr_layout.rowPitch;
304 }
305 file.close();
306
307 // Clean up
308 err = pTableDevice->UnmapMemory(device, mem2);
309 assert(!err);
310 err = pTableDevice->FreeMemory(device, mem2);
311 assert(!err);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600312 err = pTableDevice->DestroyCommandBuffer(device, cmdBuffer);
David Pinedoeeca2a22015-06-18 17:03:14 -0600313 assert(!err);
314}
315
316
David Pinedo8a4f5562015-06-19 13:48:18 -0600317static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
318{
Cody Northrop1684adb2015-08-05 11:15:02 -0600319 uint32_t i;
David Pinedo8a4f5562015-06-19 13:48:18 -0600320 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
Ian Elliottb134e842015-07-06 14:31:32 -0600321 deviceExtMap[pDisp].wsi_enabled = false;
David Pinedo8a4f5562015-06-19 13:48:18 -0600322 for (i = 0; i < pCreateInfo->extensionCount; i++) {
Ian Elliottb134e842015-07-06 14:31:32 -0600323 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_WSI_DEVICE_SWAPCHAIN_EXTENSION_NAME) == 0)
324 deviceExtMap[pDisp].wsi_enabled = true;
David Pinedo8a4f5562015-06-19 13:48:18 -0600325 }
326}
327
David Pinedoeeca2a22015-06-18 17:03:14 -0600328VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(
329 VkPhysicalDevice gpu,
330 const VkDeviceCreateInfo *pCreateInfo,
331 VkDevice *pDevice)
332{
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600333 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice);
334 VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pDevice);
David Pinedoeeca2a22015-06-18 17:03:14 -0600335
David Pinedo8a4f5562015-06-19 13:48:18 -0600336 if (result == VK_SUCCESS) {
Jon Ashburn59f3dfc2015-07-06 15:09:36 -0600337 init_screenshot();
David Pinedo8a4f5562015-06-19 13:48:18 -0600338 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
Cody Northrop98fb22a2015-09-01 10:18:45 -0600339 // Create a mapping from a device to a physicalDevice
340 if (deviceMap[*pDevice] == NULL)
341 {
342 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
343 deviceMap[*pDevice] = deviceMapElem;
344 }
345 deviceMap[*pDevice]->physicalDevice = gpu;
David Pinedo8a4f5562015-06-19 13:48:18 -0600346 }
347
David Pinedoeeca2a22015-06-18 17:03:14 -0600348 return result;
349}
350
Cody Northrop98fb22a2015-09-01 10:18:45 -0600351VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
352 VkInstance instance,
353 uint32_t* pPhysicalDeviceCount,
354 VkPhysicalDevice* pPhysicalDevices)
355{
356 VkResult result;
357
358 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
359 result = pTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
360 if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0)
361 {
362 for (uint32_t i=0; i<*pPhysicalDeviceCount ; i++)
363 {
364 // Create a mapping from a physicalDevice to an instance
365 if (physDeviceMap[pPhysicalDevices[i]] == NULL)
366 {
367 PhysDeviceMapStruct *physDeviceMapElem = new PhysDeviceMapStruct;
368 physDeviceMap[pPhysicalDevices[i]] = physDeviceMapElem;
369 }
370 physDeviceMap[pPhysicalDevices[i]]->instance = instance;
371 }
372 }
373 return result;
374}
375
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600376/* TODO: Probably need a DestroyDevice as well */
377
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600378static const VkLayerProperties ss_device_layers[] = {
David Pinedoeeca2a22015-06-18 17:03:14 -0600379 {
David Pinedoeeca2a22015-06-18 17:03:14 -0600380 "ScreenShot",
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600381 VK_API_VERSION,
382 VK_MAKE_VERSION(0, 1, 0),
David Pinedoeeca2a22015-06-18 17:03:14 -0600383 "Layer: ScreenShot",
David Pinedo8a4f5562015-06-19 13:48:18 -0600384 }
David Pinedoeeca2a22015-06-18 17:03:14 -0600385};
David Pinedoeeca2a22015-06-18 17:03:14 -0600386
Tony Barbour426b9052015-06-24 16:06:58 -0600387VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600388 const char *pLayerName,
389 uint32_t *pCount,
390 VkExtensionProperties* pProperties)
David Pinedoeeca2a22015-06-18 17:03:14 -0600391{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600392 /* ScreenShot does not have any global extensions */
393 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600394}
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600395
396VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalLayerProperties(
397 uint32_t *pCount,
398 VkLayerProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600399{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600400 /* ScreenShot does not have any global layers */
401 return util_GetLayerProperties(0, NULL,
402 pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600403}
404
405VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600406 VkPhysicalDevice physicalDevice,
407 const char* pLayerName,
408 uint32_t* pCount,
409 VkExtensionProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600410{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600411 /* ScreenShot does not have any physical device extensions */
412 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
413}
Tony Barbour426b9052015-06-24 16:06:58 -0600414
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600415VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceLayerProperties(
416 VkPhysicalDevice physicalDevice,
417 uint32_t* pCount,
418 VkLayerProperties* pProperties)
419{
420 return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers),
421 ss_device_layers,
422 pCount, pProperties);
David Pinedoeeca2a22015-06-18 17:03:14 -0600423}
424
425VK_LAYER_EXPORT VkResult VKAPI vkGetDeviceQueue(
426 VkDevice device,
427 uint32_t queueNodeIndex,
428 uint32_t queueIndex,
429 VkQueue *pQueue)
430{
431 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
432 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
433
434 loader_platform_thread_lock_mutex(&globalLock);
435 if (screenshotEnvQueried && screenshotFrames.empty()) {
436 // We are all done taking screenshots, so don't do anything else
437 loader_platform_thread_unlock_mutex(&globalLock);
438 return result;
439 }
440
441 if (result == VK_SUCCESS) {
442 screenshot_device_table_map.emplace(*pQueue, pTable);
443
Cody Northrop98fb22a2015-09-01 10:18:45 -0600444 // Create a mapping from a device to a queue
445 if (deviceMap[device] == NULL)
446 {
447 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
448 deviceMap[device] = deviceMapElem;
449 }
450 deviceMap[device]->queue = *pQueue;
David Pinedoeeca2a22015-06-18 17:03:14 -0600451 }
452 loader_platform_thread_unlock_mutex(&globalLock);
453 return result;
454}
455
David Pinedoaa4d1da2015-07-31 10:56:20 -0600456VK_LAYER_EXPORT VkResult VKAPI vkCreateCommandPool(
457 VkDevice device,
458 const VkCmdPoolCreateInfo *pCreateInfo,
459 VkCmdPool *pCmdPool)
460{
461 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
462 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateCommandPool(device, pCreateInfo, pCmdPool);
463
464 loader_platform_thread_lock_mutex(&globalLock);
465 if (screenshotEnvQueried && screenshotFrames.empty()) {
466 // We are all done taking screenshots, so don't do anything else
467 loader_platform_thread_unlock_mutex(&globalLock);
468 return result;
469 }
470
Cody Northrop98fb22a2015-09-01 10:18:45 -0600471 // Create a mapping from a device to a cmdPool
472 if (deviceMap[device] == NULL)
473 {
474 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
475 deviceMap[device] = deviceMapElem;
476 }
David Pinedoaa4d1da2015-07-31 10:56:20 -0600477 deviceMap[device]->cmdPool = *pCmdPool;
478 loader_platform_thread_unlock_mutex(&globalLock);
479 return result;
480}
481
David Pinedoeeca2a22015-06-18 17:03:14 -0600482VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapChainWSI(
483 VkDevice device,
484 const VkSwapChainCreateInfoWSI *pCreateInfo,
485 VkSwapChainWSI *pSwapChain)
486{
487 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
488 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapChainWSI(device, pCreateInfo, pSwapChain);
489
490 loader_platform_thread_lock_mutex(&globalLock);
491 if (screenshotEnvQueried && screenshotFrames.empty()) {
492 // We are all done taking screenshots, so don't do anything else
493 loader_platform_thread_unlock_mutex(&globalLock);
494 return result;
495 }
496
497 if (result == VK_SUCCESS)
498 {
499 // Create a mapping for a swapchain to a device, image extent, and format
500 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
501 swapchainMapElem->device = device;
502 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
503 swapchainMapElem->format = pCreateInfo->imageFormat;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600504 swapchainMap.insert(make_pair(pSwapChain->handle, swapchainMapElem));
David Pinedoeeca2a22015-06-18 17:03:14 -0600505
506 // Create a mapping for the swapchain object into the dispatch table
David Pinedoaa4d1da2015-07-31 10:56:20 -0600507 screenshot_device_table_map.emplace((void *)pSwapChain->handle, pTable);
David Pinedoeeca2a22015-06-18 17:03:14 -0600508 }
509 loader_platform_thread_unlock_mutex(&globalLock);
510
511 return result;
512}
513
Ian Elliottccac0232015-08-07 14:11:14 -0600514VK_LAYER_EXPORT VkResult VKAPI vkGetSwapChainImagesWSI(
Ian Elliottb134e842015-07-06 14:31:32 -0600515 VkDevice device,
David Pinedoeeca2a22015-06-18 17:03:14 -0600516 VkSwapChainWSI swapChain,
Ian Elliottccac0232015-08-07 14:11:14 -0600517 uint32_t* pCount,
518 VkImage* pSwapChainImages)
David Pinedoeeca2a22015-06-18 17:03:14 -0600519{
Ian Elliottccac0232015-08-07 14:11:14 -0600520 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapChainImagesWSI(device, swapChain, pCount, pSwapChainImages);
David Pinedoeeca2a22015-06-18 17:03:14 -0600521
522 loader_platform_thread_lock_mutex(&globalLock);
523 if (screenshotEnvQueried && screenshotFrames.empty()) {
524 // We are all done taking screenshots, so don't do anything else
525 loader_platform_thread_unlock_mutex(&globalLock);
526 return result;
527 }
528
529 if (result == VK_SUCCESS &&
Ian Elliottccac0232015-08-07 14:11:14 -0600530 pSwapChainImages &&
David Pinedoaa4d1da2015-07-31 10:56:20 -0600531 !swapchainMap.empty() && swapchainMap.find(swapChain.handle) != swapchainMap.end())
Ian Elliottccac0232015-08-07 14:11:14 -0600532 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600533 int i;
534
Ian Elliottccac0232015-08-07 14:11:14 -0600535 for (i=0; i<*pCount; i++)
David Pinedoeeca2a22015-06-18 17:03:14 -0600536 {
537 // Create a mapping for an image to a device, image extent, and format
Cody Northrop98fb22a2015-09-01 10:18:45 -0600538 if (imageMap[pSwapChainImages[i].handle] == NULL)
539 {
540 ImageMapStruct *imageMapElem = new ImageMapStruct;
541 imageMap[pSwapChainImages[i].handle] = imageMapElem;
542 }
543 imageMap[pSwapChainImages[i].handle]->device = swapchainMap[swapChain.handle]->device;
544 imageMap[pSwapChainImages[i].handle]->imageExtent = swapchainMap[swapChain.handle]->imageExtent;
545 imageMap[pSwapChainImages[i].handle]->format = swapchainMap[swapChain.handle]->format;
David Pinedoeeca2a22015-06-18 17:03:14 -0600546 }
David Pinedoaa4d1da2015-07-31 10:56:20 -0600547
548 // Add list of images to swapchain to image map
549 SwapchainMapStruct *swapchainMapElem = swapchainMap[swapChain.handle];
550 if (i >= 1 && swapchainMapElem)
551 {
552 VkImage *imageList = new VkImage[i];
553 swapchainMapElem->imageList = imageList;
Ian Elliottccac0232015-08-07 14:11:14 -0600554 for (int j=0; j<i; j++)
David Pinedoaa4d1da2015-07-31 10:56:20 -0600555 {
Ian Elliottccac0232015-08-07 14:11:14 -0600556 swapchainMapElem->imageList[j] = pSwapChainImages[j].handle;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600557 }
558 }
559
David Pinedoeeca2a22015-06-18 17:03:14 -0600560 }
561 loader_platform_thread_unlock_mutex(&globalLock);
562 return result;
563}
564
Ian Elliottb134e842015-07-06 14:31:32 -0600565VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentWSI(VkQueue queue, VkPresentInfoWSI* pPresentInfo)
David Pinedoeeca2a22015-06-18 17:03:14 -0600566{
567 static int frameNumber = 0;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600568 if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ }
David Pinedoeeca2a22015-06-18 17:03:14 -0600569 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentWSI(queue, pPresentInfo);
570
571 loader_platform_thread_lock_mutex(&globalLock);
572
573 if (!screenshotEnvQueried)
574 {
575 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
576 if (_vk_screenshot && *_vk_screenshot)
577 {
578 string spec(_vk_screenshot), word;
579 size_t start = 0, comma = 0;
580
581 while (start < spec.size()) {
582 int frameToAdd;
583 comma = spec.find(',', start);
584 if (comma == string::npos)
585 word = string(spec, start);
586 else
587 word = string(spec, start, comma - start);
588 frameToAdd=atoi(word.c_str());
589 // Add the frame number to list, but only do it if the word started with a digit and if
590 // it's not already in the list
591 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
592 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
593 {
594 screenshotFrames.push_back(frameToAdd);
595 }
596 if (comma == string::npos)
597 break;
598 start = comma + 1;
599 }
600 }
601 screenshotEnvQueried = true;
602 }
603
604
605 if (result == VK_SUCCESS && !screenshotFrames.empty())
606 {
607 vector<int>::iterator it;
608 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
609 if (it != screenshotFrames.end())
610 {
611 string fileName;
612 fileName = to_string(frameNumber) + ".ppm";
David Pinedoaa4d1da2015-07-31 10:56:20 -0600613
614 VkImage image;
615 uint64_t swapChain;
616 // We'll dump only one image: the first
617 swapChain = pPresentInfo->swapChains[0].handle;
618 image = swapchainMap[swapChain]->imageList[pPresentInfo->imageIndices[0]];
619 writePPM(fileName.c_str(), image);
David Pinedoeeca2a22015-06-18 17:03:14 -0600620 screenshotFrames.erase(it);
621
622 if (screenshotFrames.empty())
623 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600624 // Free all our maps since we are done with them.
David Pinedoeeca2a22015-06-18 17:03:14 -0600625 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
626 {
627 SwapchainMapStruct *swapchainMapElem = it->second;
628 delete swapchainMapElem;
629 }
630 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
631 {
632 ImageMapStruct *imageMapElem = it->second;
633 delete imageMapElem;
634 }
635 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
636 {
637 DeviceMapStruct *deviceMapElem = it->second;
638 delete deviceMapElem;
639 }
Cody Northrop98fb22a2015-09-01 10:18:45 -0600640 for (auto it = physDeviceMap.begin(); it != physDeviceMap.end(); it++)
641 {
642 PhysDeviceMapStruct *physDeviceMapElem = it->second;
643 delete physDeviceMapElem;
644 }
David Pinedoeeca2a22015-06-18 17:03:14 -0600645 swapchainMap.clear();
646 imageMap.clear();
647 deviceMap.clear();
Cody Northrop98fb22a2015-09-01 10:18:45 -0600648 physDeviceMap.clear();
David Pinedoeeca2a22015-06-18 17:03:14 -0600649 }
650 }
651 }
652 frameNumber++;
653 loader_platform_thread_unlock_mutex(&globalLock);
654 return result;
655}
656
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600657VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(
David Pinedoeeca2a22015-06-18 17:03:14 -0600658 VkDevice dev,
659 const char *funcName)
660{
David Pinedoeeca2a22015-06-18 17:03:14 -0600661 if (dev == NULL) {
662 return NULL;
663 }
664
665 /* loader uses this to force layer initialization; device object is wrapped */
666 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
667 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600668 return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
David Pinedoeeca2a22015-06-18 17:03:14 -0600669 }
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600670 if (!strcmp(funcName, "vkCreateDevice"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600671 return (PFN_vkVoidFunction) vkCreateDevice;
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600672
David Pinedoeeca2a22015-06-18 17:03:14 -0600673 if (!strcmp(funcName, "vkGetDeviceQueue"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600674 return (PFN_vkVoidFunction) vkGetDeviceQueue;
675
676 if (!strcmp(funcName, "vkCreateCommandPool"))
677 return (PFN_vkVoidFunction) vkCreateCommandPool;
David Pinedoeeca2a22015-06-18 17:03:14 -0600678
David Pinedo8a4f5562015-06-19 13:48:18 -0600679 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
Ian Elliottb134e842015-07-06 14:31:32 -0600680 if (deviceExtMap.size() == 0 || deviceExtMap[pDisp].wsi_enabled)
David Pinedo8a4f5562015-06-19 13:48:18 -0600681 {
682 if (!strcmp(funcName, "vkCreateSwapChainWSI"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600683 return (PFN_vkVoidFunction) vkCreateSwapChainWSI;
Ian Elliottccac0232015-08-07 14:11:14 -0600684 if (!strcmp(funcName, "vkGetSwapChainImagesWSI"))
685 return (PFN_vkVoidFunction) vkGetSwapChainImagesWSI;
David Pinedo8a4f5562015-06-19 13:48:18 -0600686 if (!strcmp(funcName, "vkQueuePresentWSI"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600687 return (PFN_vkVoidFunction) vkQueuePresentWSI;
David Pinedo8a4f5562015-06-19 13:48:18 -0600688 }
689
690 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600691 return NULL;
David Pinedo8a4f5562015-06-19 13:48:18 -0600692 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedoeeca2a22015-06-18 17:03:14 -0600693}
David Pinedo07238d42015-07-09 16:23:44 -0600694
695
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600696VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
David Pinedo07238d42015-07-09 16:23:44 -0600697{
David Pinedo07238d42015-07-09 16:23:44 -0600698 if (instance == VK_NULL_HANDLE) {
699 return NULL;
700 }
701
702 /* loader uses this to force layer initialization; instance object is wrapped */
703 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
704 initInstanceTable((const VkBaseLayerObject *) instance);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600705 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
David Pinedo07238d42015-07-09 16:23:44 -0600706 }
707
Cody Northrop98fb22a2015-09-01 10:18:45 -0600708 if (!strcmp(funcName, "vkEnumeratePhysicalDevices"))
709 return (PFN_vkVoidFunction)vkEnumeratePhysicalDevices;
710
David Pinedo07238d42015-07-09 16:23:44 -0600711 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
712 if (pTable->GetInstanceProcAddr == NULL)
713 return NULL;
714 return pTable->GetInstanceProcAddr(instance, funcName);
David Pinedo07238d42015-07-09 16:23:44 -0600715}