blob: ac3e20d74cb130c562b11e00650759379dbee508 [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"
Tobin Ehlis56d204a2015-07-03 10:15:26 -060044#include "vk_layer_table.h"
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -060045#include "vk_layer_extension_utils.h"
David Pinedoeeca2a22015-06-18 17:03:14 -060046
David Pinedo8a4f5562015-06-19 13:48:18 -060047
48struct devExts {
Ian Elliottb134e842015-07-06 14:31:32 -060049 bool wsi_enabled;
David Pinedo8a4f5562015-06-19 13:48:18 -060050};
51static std::unordered_map<void *, struct devExts> deviceExtMap;
David Pinedoeeca2a22015-06-18 17:03:14 -060052static device_table_map screenshot_device_table_map;
David Pinedoeeca2a22015-06-18 17:03:14 -060053
54static int globalLockInitialized = 0;
55static loader_platform_thread_mutex globalLock;
56
Cody Northrop98fb22a2015-09-01 10:18:45 -060057// unordered map: associates a swap chain with a device, image extent, format, and
David Pinedoaa4d1da2015-07-31 10:56:20 -060058// list of images
David Pinedoeeca2a22015-06-18 17:03:14 -060059typedef struct
60{
61 VkDevice device;
62 VkExtent2D imageExtent;
63 VkFormat format;
David Pinedoaa4d1da2015-07-31 10:56:20 -060064 VkImage *imageList;
David Pinedoeeca2a22015-06-18 17:03:14 -060065} SwapchainMapStruct;
David Pinedoaa4d1da2015-07-31 10:56:20 -060066static unordered_map<uint64_t, SwapchainMapStruct *> swapchainMap;
David Pinedoeeca2a22015-06-18 17:03:14 -060067
Cody Northrop98fb22a2015-09-01 10:18:45 -060068// unordered map: associates an image with a device, image extent, and format
David Pinedoeeca2a22015-06-18 17:03:14 -060069typedef struct
70{
71 VkDevice device;
72 VkExtent2D imageExtent;
73 VkFormat format;
74} ImageMapStruct;
David Pinedoaa4d1da2015-07-31 10:56:20 -060075static unordered_map<uint64_t, ImageMapStruct *> imageMap;
David Pinedoeeca2a22015-06-18 17:03:14 -060076
Cody Northrop98fb22a2015-09-01 10:18:45 -060077// unordered map: associates a device with a queue, cmdPool, and physical device
David Pinedoeeca2a22015-06-18 17:03:14 -060078typedef struct
79{
80 VkQueue queue;
David Pinedoaa4d1da2015-07-31 10:56:20 -060081 VkCmdPool cmdPool;
Cody Northrop98fb22a2015-09-01 10:18:45 -060082 VkPhysicalDevice physicalDevice;
David Pinedoeeca2a22015-06-18 17:03:14 -060083} DeviceMapStruct;
84static unordered_map<VkDevice, DeviceMapStruct *> deviceMap;
85
Cody Northrop98fb22a2015-09-01 10:18:45 -060086// unordered map: associates a physical device with an instance
87typedef struct
88{
89 VkInstance instance;
90} PhysDeviceMapStruct;
91static unordered_map<VkPhysicalDevice, PhysDeviceMapStruct *> physDeviceMap;
92
David Pinedoeeca2a22015-06-18 17:03:14 -060093// List of frames to we will get a screenshot of
94static vector<int> screenshotFrames;
95
96// Flag indicating we have queried _VK_SCREENSHOT env var
97static bool screenshotEnvQueried = false;
98
Cody Northrop98fb22a2015-09-01 10:18:45 -060099static VkResult memory_type_from_properties(
100 VkPhysicalDeviceMemoryProperties *memory_properties,
101 uint32_t typeBits,
Tony Barbourf1eceb92015-10-15 12:42:56 -0600102 VkFlags requirements_mask,
Cody Northrop98fb22a2015-09-01 10:18:45 -0600103 uint32_t *typeIndex)
104{
105 // Search memtypes to find first index with those properties
106 for (uint32_t i = 0; i < 32; i++) {
107 if ((typeBits & 1) == 1) {
108 // Type is available, does it match user properties?
Tony Barbourf1eceb92015-10-15 12:42:56 -0600109 if ((memory_properties->memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Cody Northrop98fb22a2015-09-01 10:18:45 -0600110 *typeIndex = i;
111 return VK_SUCCESS;
112 }
113 }
114 typeBits >>= 1;
115 }
116 // No memory types matched, return failure
117 return VK_UNSUPPORTED;
118}
119
David Pinedoeeca2a22015-06-18 17:03:14 -0600120static void init_screenshot()
121{
David Pinedoeeca2a22015-06-18 17:03:14 -0600122 if (!globalLockInitialized)
123 {
124 // TODO/TBD: Need to delete this mutex sometime. How??? One
125 // suggestion is to call this during vkCreateInstance(), and then we
126 // can clean it up during vkDestroyInstance(). However, that requires
127 // that the layer have per-instance locks. We need to come back and
128 // address this soon.
129 loader_platform_thread_create_mutex(&globalLock);
130 globalLockInitialized = 1;
131 }
132}
133
134static void writePPM( const char *filename, VkImage image1)
135{
136 VkImage image2;
137 VkResult err;
138 int x, y;
139 const char *ptr;
140 VkDeviceMemory mem2;
141 VkCmdBuffer cmdBuffer;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600142 VkDevice device = imageMap[image1.handle]->device;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600143 VkPhysicalDevice physicalDevice = deviceMap[device]->physicalDevice;
144 VkInstance instance = physDeviceMap[physicalDevice]->instance;
David Pinedoeeca2a22015-06-18 17:03:14 -0600145 VkQueue queue = deviceMap[device]->queue;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600146 int width = imageMap[image1.handle]->imageExtent.width;
147 int height = imageMap[image1.handle]->imageExtent.height;
148 VkFormat format = imageMap[image1.handle]->format;
David Pinedoeeca2a22015-06-18 17:03:14 -0600149 const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR, 0, 0};
150 VkSubresourceLayout sr_layout;
David Pinedoeeca2a22015-06-18 17:03:14 -0600151 const VkImageCreateInfo imgCreateInfo = {
152 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
153 NULL,
154 VK_IMAGE_TYPE_2D,
155 format,
156 {width, height, 1},
157 1,
158 1,
159 1,
160 VK_IMAGE_TILING_LINEAR,
161 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT|VK_IMAGE_USAGE_STORAGE_BIT),
162 0
163 };
164 VkMemoryAllocInfo memAllocInfo = {
165 VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
166 NULL,
167 0, // allocationSize, queried later
Cody Northrop46d10d02015-09-01 11:47:50 -0600168 0 // memoryTypeIndex, queried later
David Pinedoeeca2a22015-06-18 17:03:14 -0600169 };
170 const VkCmdBufferCreateInfo createCommandBufferInfo = {
171 VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
172 NULL,
David Pinedoaa4d1da2015-07-31 10:56:20 -0600173 deviceMap[device]->cmdPool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800174 VK_CMD_BUFFER_LEVEL_PRIMARY,
David Pinedoeeca2a22015-06-18 17:03:14 -0600175 0
176 };
177 const VkCmdBufferBeginInfo cmdBufferBeginInfo = {
178 VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
179 NULL,
180 VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
181 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
182 };
183 const VkImageCopy imageCopyRegion = {
184 {VK_IMAGE_ASPECT_COLOR, 0, 0},
185 {0, 0, 0},
186 {VK_IMAGE_ASPECT_COLOR, 0, 0},
187 {0, 0, 0},
188 {width, height, 1}
189 };
190 VkMemoryRequirements memRequirements;
David Pinedoeeca2a22015-06-18 17:03:14 -0600191 uint32_t num_allocations = 0;
192 size_t num_alloc_size = sizeof(num_allocations);
David Pinedoc3256662015-06-30 13:08:37 -0600193 VkLayerDispatchTable* pTableDevice = get_dispatch_table(screenshot_device_table_map, device);
194 VkLayerDispatchTable* pTableQueue = get_dispatch_table(screenshot_device_table_map, queue);
Cody Northrop98fb22a2015-09-01 10:18:45 -0600195 VkLayerInstanceDispatchTable* pInstanceTable;
David Pinedoeeca2a22015-06-18 17:03:14 -0600196 VkLayerDispatchTable* pTableCmdBuffer;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600197 VkPhysicalDeviceMemoryProperties memory_properties;
David Pinedoeeca2a22015-06-18 17:03:14 -0600198
David Pinedoaa4d1da2015-07-31 10:56:20 -0600199 if (imageMap.empty() || imageMap.find(image1.handle) == imageMap.end())
David Pinedoeeca2a22015-06-18 17:03:14 -0600200 return;
201
202 // The VkImage image1 we are going to dump may not be mappable,
203 // and/or it may have a tiling mode of optimal rather than linear.
204 // To make sure we have an image that we can map and read linearly, we:
205 // create image2 that is mappable and linear
206 // copy image1 to image2
207 // map image2
208 // read from image2's mapped memeory.
209
210 err = pTableDevice->CreateImage(device, &imgCreateInfo, &image2);
211 assert(!err);
212
David Pinedoaa4d1da2015-07-31 10:56:20 -0600213 err = pTableDevice->GetImageMemoryRequirements(device, image2, &memRequirements);
David Pinedoeeca2a22015-06-18 17:03:14 -0600214 assert(!err);
215
216 memAllocInfo.allocationSize = memRequirements.size;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600217 pInstanceTable = instance_dispatch_table(instance);
218 err = pInstanceTable->GetPhysicalDeviceMemoryProperties(physicalDevice, &memory_properties);
219 assert(!err);
220
221 err = memory_type_from_properties(&memory_properties,
222 memRequirements.memoryTypeBits,
Cody Northrop46d10d02015-09-01 11:47:50 -0600223 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Cody Northrop98fb22a2015-09-01 10:18:45 -0600224 &memAllocInfo.memoryTypeIndex);
225 assert(!err);
226
David Pinedoeeca2a22015-06-18 17:03:14 -0600227 err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2);
228 assert(!err);
229
David Pinedoaa4d1da2015-07-31 10:56:20 -0600230 err = pTableQueue->BindImageMemory(device, image2, mem2, 0);
David Pinedoeeca2a22015-06-18 17:03:14 -0600231 assert(!err);
232
233 err = pTableDevice->CreateCommandBuffer(device, &createCommandBufferInfo, &cmdBuffer);
234 assert(!err);
235
236 screenshot_device_table_map.emplace(cmdBuffer, pTableDevice);
237 pTableCmdBuffer = screenshot_device_table_map[cmdBuffer];
238
239 err = pTableCmdBuffer->BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo);
240 assert(!err);
241
Cody Northrop46d10d02015-09-01 11:47:50 -0600242 // TODO: We need to transition images to match these layouts, then restore the original layouts
243 pTableCmdBuffer->CmdCopyImage(cmdBuffer, image1, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
244 image2, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, 1, &imageCopyRegion);
David Pinedoeeca2a22015-06-18 17:03:14 -0600245
246 err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer);
247 assert(!err);
248
249 err = pTableQueue->QueueSubmit(queue, 1, &cmdBuffer, VK_NULL_HANDLE);
250 assert(!err);
251
252 err = pTableQueue->QueueWaitIdle(queue);
253 assert(!err);
254
255 err = pTableDevice->DeviceWaitIdle(device);
256 assert(!err);
257
Tony Barbour426b9052015-06-24 16:06:58 -0600258 err = pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -0600259 assert(!err);
260
261 err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr );
262 assert(!err);
263
264 ptr += sr_layout.offset;
265
266 ofstream file(filename, ios::binary);
267
268 file << "P6\n";
269 file << width << "\n";
270 file << height << "\n";
271 file << 255 << "\n";
272
273 for (y = 0; y < height; y++) {
274 const unsigned int *row = (const unsigned int*) ptr;
275 if (format == VK_FORMAT_B8G8R8A8_UNORM)
276 {
277 for (x = 0; x < width; x++) {
278 unsigned int swapped;
279 swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16;
280 file.write((char *)&swapped, 3);
281 row++;
282 }
283 }
284 else if (format == VK_FORMAT_R8G8B8A8_UNORM)
285 {
286 for (x = 0; x < width; x++) {
287 file.write((char *)row, 3);
288 row++;
289 }
290 }
291 else
292 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600293 // TODO: add support for additional formats
David Pinedoeeca2a22015-06-18 17:03:14 -0600294 printf("Unrecognized image format\n");
295 break;
296 }
297 ptr += sr_layout.rowPitch;
298 }
299 file.close();
300
301 // Clean up
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600302 pTableDevice->UnmapMemory(device, mem2);
303 pTableDevice->FreeMemory(device, mem2);
304 pTableDevice->DestroyCommandBuffer(device, cmdBuffer);
David Pinedoeeca2a22015-06-18 17:03:14 -0600305}
306
307
David Pinedo8a4f5562015-06-19 13:48:18 -0600308static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
309{
Cody Northrop1684adb2015-08-05 11:15:02 -0600310 uint32_t i;
David Pinedo8a4f5562015-06-19 13:48:18 -0600311 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
Jon Ashburn83334db2015-09-16 18:08:32 -0600312 PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr;
313 pDisp->GetSurfacePropertiesKHR = (PFN_vkGetSurfacePropertiesKHR) gpa(device, "vkGetSurfacePropertiesKHR");
314 pDisp->GetSurfaceFormatsKHR = (PFN_vkGetSurfaceFormatsKHR) gpa(device, "vkGetSurfaceFormatsKHR");
315 pDisp->GetSurfacePresentModesKHR = (PFN_vkGetSurfacePresentModesKHR) gpa(device, "vkGetSurfacePresentModesKHR");
316 pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR) gpa(device, "vkCreateSwapchainKHR");
317 pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR) gpa(device, "vkDestroySwapchainKHR");
318 pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR) gpa(device, "vkGetSwapchainImagesKHR");
319 pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR) gpa(device, "vkAcquireNextImageKHR");
320 pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR) gpa(device, "vkQueuePresentKHR");
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 Elliott338dedb2015-08-21 15:09:33 -0600323 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_EXT_KHR_DEVICE_SWAPCHAIN_EXTENSION_NAME) == 0)
Ian Elliottb134e842015-07-06 14:31:32 -0600324 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);
Tobin Ehlisd8dd86b2015-09-08 08:59:48 -0600360 if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0 && pPhysicalDevices)
Cody Northrop98fb22a2015-09-01 10:18:45 -0600361 {
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
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600387VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
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
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600396VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600397 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
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600405VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
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 Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600415VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600416 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
Ian Elliott338dedb2015-08-21 15:09:33 -0600482VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapchainKHR(
David Pinedoeeca2a22015-06-18 17:03:14 -0600483 VkDevice device,
Ian Elliott338dedb2015-08-21 15:09:33 -0600484 const VkSwapchainCreateInfoKHR *pCreateInfo,
485 VkSwapchainKHR *pSwapchain)
David Pinedoeeca2a22015-06-18 17:03:14 -0600486{
487 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Ian Elliott338dedb2015-08-21 15:09:33 -0600488 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapchainKHR(device, pCreateInfo, pSwapchain);
David Pinedoeeca2a22015-06-18 17:03:14 -0600489
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;
Ian Elliott338dedb2015-08-21 15:09:33 -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
Ian Elliott338dedb2015-08-21 15:09:33 -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 Elliott338dedb2015-08-21 15:09:33 -0600514VK_LAYER_EXPORT VkResult VKAPI vkGetSwapchainImagesKHR(
Ian Elliottb134e842015-07-06 14:31:32 -0600515 VkDevice device,
Ian Elliott338dedb2015-08-21 15:09:33 -0600516 VkSwapchainKHR swapchain,
Ian Elliottccac0232015-08-07 14:11:14 -0600517 uint32_t* pCount,
Ian Elliott338dedb2015-08-21 15:09:33 -0600518 VkImage* pSwapchainImages)
David Pinedoeeca2a22015-06-18 17:03:14 -0600519{
Ian Elliott338dedb2015-08-21 15:09:33 -0600520 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapchainImagesKHR(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 Elliott338dedb2015-08-21 15:09:33 -0600530 pSwapchainImages &&
531 !swapchainMap.empty() && swapchainMap.find(swapchain.handle) != swapchainMap.end())
Ian Elliottccac0232015-08-07 14:11:14 -0600532 {
Cody Northrop46d92c02015-09-01 11:48:13 -0600533 unsigned i;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600534
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
Ian Elliott338dedb2015-08-21 15:09:33 -0600538 if (imageMap[pSwapchainImages[i].handle] == NULL)
Cody Northrop98fb22a2015-09-01 10:18:45 -0600539 {
540 ImageMapStruct *imageMapElem = new ImageMapStruct;
Ian Elliott338dedb2015-08-21 15:09:33 -0600541 imageMap[pSwapchainImages[i].handle] = imageMapElem;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600542 }
Ian Elliott338dedb2015-08-21 15:09:33 -0600543 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
Ian Elliott338dedb2015-08-21 15:09:33 -0600549 SwapchainMapStruct *swapchainMapElem = swapchainMap[swapchain.handle];
David Pinedoaa4d1da2015-07-31 10:56:20 -0600550 if (i >= 1 && swapchainMapElem)
551 {
552 VkImage *imageList = new VkImage[i];
553 swapchainMapElem->imageList = imageList;
Cody Northrop46d92c02015-09-01 11:48:13 -0600554 for (unsigned j=0; j<i; j++)
David Pinedoaa4d1da2015-07-31 10:56:20 -0600555 {
Ian Elliott338dedb2015-08-21 15:09:33 -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 Elliott338dedb2015-08-21 15:09:33 -0600565VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentKHR(VkQueue queue, VkPresentInfoKHR* 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; */ }
Ian Elliott338dedb2015-08-21 15:09:33 -0600569 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentKHR(queue, pPresentInfo);
David Pinedoeeca2a22015-06-18 17:03:14 -0600570
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;
Ian Elliott338dedb2015-08-21 15:09:33 -0600615 uint64_t swapchain;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600616 // We'll dump only one image: the first
Ian Elliott338dedb2015-08-21 15:09:33 -0600617 swapchain = pPresentInfo->swapchains[0].handle;
618 image = swapchainMap[swapchain]->imageList[pPresentInfo->imageIndices[0]];
David Pinedoaa4d1da2015-07-31 10:56:20 -0600619 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);
Jon Ashburn83334db2015-09-16 18:08:32 -0600680 if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].wsi_enabled)
David Pinedo8a4f5562015-06-19 13:48:18 -0600681 {
Ian Elliott338dedb2015-08-21 15:09:33 -0600682 if (!strcmp(funcName, "vkCreateSwapchainKHR"))
683 return (PFN_vkVoidFunction) vkCreateSwapchainKHR;
684 if (!strcmp(funcName, "vkGetSwapchainImagesKHR"))
685 return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR;
686 if (!strcmp(funcName, "vkQueuePresentKHR"))
687 return (PFN_vkVoidFunction) vkQueuePresentKHR;
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}