blob: 0fa3337a249512036f853b05b00093f23782dc1a [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
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -060099static bool memory_type_from_properties(
Cody Northrop98fb22a2015-09-01 10:18:45 -0600100 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;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600111 return true;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600112 }
113 }
114 typeBits >>= 1;
115 }
116 // No memory types matched, return failure
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600117 return false;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600118}
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;
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600138 bool pass;
David Pinedoeeca2a22015-06-18 17:03:14 -0600139 int x, y;
140 const char *ptr;
141 VkDeviceMemory mem2;
142 VkCmdBuffer cmdBuffer;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600143 VkDevice device = imageMap[image1.handle]->device;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600144 VkPhysicalDevice physicalDevice = deviceMap[device]->physicalDevice;
145 VkInstance instance = physDeviceMap[physicalDevice]->instance;
David Pinedoeeca2a22015-06-18 17:03:14 -0600146 VkQueue queue = deviceMap[device]->queue;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600147 int width = imageMap[image1.handle]->imageExtent.width;
148 int height = imageMap[image1.handle]->imageExtent.height;
149 VkFormat format = imageMap[image1.handle]->format;
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600150 const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0};
David Pinedoeeca2a22015-06-18 17:03:14 -0600151 VkSubresourceLayout sr_layout;
David Pinedoeeca2a22015-06-18 17:03:14 -0600152 const VkImageCreateInfo imgCreateInfo = {
153 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
154 NULL,
155 VK_IMAGE_TYPE_2D,
156 format,
157 {width, height, 1},
158 1,
159 1,
160 1,
161 VK_IMAGE_TILING_LINEAR,
162 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT|VK_IMAGE_USAGE_STORAGE_BIT),
163 0
164 };
165 VkMemoryAllocInfo memAllocInfo = {
166 VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
167 NULL,
168 0, // allocationSize, queried later
Cody Northrop46d10d02015-09-01 11:47:50 -0600169 0 // memoryTypeIndex, queried later
David Pinedoeeca2a22015-06-18 17:03:14 -0600170 };
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600171 const VkCmdBufferAllocInfo allocCommandBufferInfo = {
172 VK_STRUCTURE_TYPE_CMD_BUFFER_ALLOC_INFO,
David Pinedoeeca2a22015-06-18 17:03:14 -0600173 NULL,
David Pinedoaa4d1da2015-07-31 10:56:20 -0600174 deviceMap[device]->cmdPool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800175 VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600176 1
David Pinedoeeca2a22015-06-18 17:03:14 -0600177 };
178 const VkCmdBufferBeginInfo cmdBufferBeginInfo = {
179 VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
180 NULL,
Courtney Goeltzenleuchtera32436b2015-10-21 18:11:04 -0600181 VK_CMD_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
David Pinedoeeca2a22015-06-18 17:03:14 -0600182 };
183 const VkImageCopy imageCopyRegion = {
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600184 {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0},
David Pinedoeeca2a22015-06-18 17:03:14 -0600185 {0, 0, 0},
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600186 {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0},
David Pinedoeeca2a22015-06-18 17:03:14 -0600187 {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
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600213 pTableDevice->GetImageMemoryRequirements(device, image2, &memRequirements);
David Pinedoeeca2a22015-06-18 17:03:14 -0600214
215 memAllocInfo.allocationSize = memRequirements.size;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600216 pInstanceTable = instance_dispatch_table(instance);
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600217 pInstanceTable->GetPhysicalDeviceMemoryProperties(physicalDevice, &memory_properties);
Cody Northrop98fb22a2015-09-01 10:18:45 -0600218
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600219 pass = memory_type_from_properties(&memory_properties,
Cody Northrop98fb22a2015-09-01 10:18:45 -0600220 memRequirements.memoryTypeBits,
Cody Northrop46d10d02015-09-01 11:47:50 -0600221 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Cody Northrop98fb22a2015-09-01 10:18:45 -0600222 &memAllocInfo.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600223 assert(pass);
Cody Northrop98fb22a2015-09-01 10:18:45 -0600224
David Pinedoeeca2a22015-06-18 17:03:14 -0600225 err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2);
226 assert(!err);
227
David Pinedoaa4d1da2015-07-31 10:56:20 -0600228 err = pTableQueue->BindImageMemory(device, image2, mem2, 0);
David Pinedoeeca2a22015-06-18 17:03:14 -0600229 assert(!err);
230
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600231 err = pTableDevice->AllocCommandBuffers(device, &allocCommandBufferInfo, &cmdBuffer);
David Pinedoeeca2a22015-06-18 17:03:14 -0600232 assert(!err);
233
234 screenshot_device_table_map.emplace(cmdBuffer, pTableDevice);
235 pTableCmdBuffer = screenshot_device_table_map[cmdBuffer];
236
237 err = pTableCmdBuffer->BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo);
238 assert(!err);
239
Cody Northrop46d10d02015-09-01 11:47:50 -0600240 // TODO: We need to transition images to match these layouts, then restore the original layouts
241 pTableCmdBuffer->CmdCopyImage(cmdBuffer, image1, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
242 image2, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, 1, &imageCopyRegion);
David Pinedoeeca2a22015-06-18 17:03:14 -0600243
244 err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer);
245 assert(!err);
246
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600247 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter1a748e92015-10-27 11:22:14 -0600248 VkSubmitInfo submit_info;
249 submit_info.waitSemCount = 0;
250 submit_info.pWaitSemaphores = NULL;
251 submit_info.cmdBufferCount = 1;
252 submit_info.pCommandBuffers = &cmdBuffer;
253 submit_info.signalSemCount = 0;
254 submit_info.pSignalSemaphores = NULL;
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600255
256 err = pTableQueue->QueueSubmit(queue, 1, &submit_info, nullFence);
David Pinedoeeca2a22015-06-18 17:03:14 -0600257 assert(!err);
258
259 err = pTableQueue->QueueWaitIdle(queue);
260 assert(!err);
261
262 err = pTableDevice->DeviceWaitIdle(device);
263 assert(!err);
264
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600265 pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -0600266
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
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600308 pTableDevice->UnmapMemory(device, mem2);
309 pTableDevice->FreeMemory(device, mem2);
Courtney Goeltzenleuchter831c1832015-10-23 14:21:05 -0600310 pTableDevice->FreeCommandBuffers(device, deviceMap[device]->cmdPool, 1, &cmdBuffer);
David Pinedoeeca2a22015-06-18 17:03:14 -0600311}
312
313
David Pinedo8a4f5562015-06-19 13:48:18 -0600314static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
315{
Cody Northrop1684adb2015-08-05 11:15:02 -0600316 uint32_t i;
David Pinedo8a4f5562015-06-19 13:48:18 -0600317 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
Jon Ashburn83334db2015-09-16 18:08:32 -0600318 PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr;
319 pDisp->GetSurfacePropertiesKHR = (PFN_vkGetSurfacePropertiesKHR) gpa(device, "vkGetSurfacePropertiesKHR");
320 pDisp->GetSurfaceFormatsKHR = (PFN_vkGetSurfaceFormatsKHR) gpa(device, "vkGetSurfaceFormatsKHR");
321 pDisp->GetSurfacePresentModesKHR = (PFN_vkGetSurfacePresentModesKHR) gpa(device, "vkGetSurfacePresentModesKHR");
322 pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR) gpa(device, "vkCreateSwapchainKHR");
323 pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR) gpa(device, "vkDestroySwapchainKHR");
324 pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR) gpa(device, "vkGetSwapchainImagesKHR");
325 pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR) gpa(device, "vkAcquireNextImageKHR");
326 pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR) gpa(device, "vkQueuePresentKHR");
Ian Elliottb134e842015-07-06 14:31:32 -0600327 deviceExtMap[pDisp].wsi_enabled = false;
David Pinedo8a4f5562015-06-19 13:48:18 -0600328 for (i = 0; i < pCreateInfo->extensionCount; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -0600329 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_EXT_KHR_DEVICE_SWAPCHAIN_EXTENSION_NAME) == 0)
Ian Elliottb134e842015-07-06 14:31:32 -0600330 deviceExtMap[pDisp].wsi_enabled = true;
David Pinedo8a4f5562015-06-19 13:48:18 -0600331 }
332}
333
David Pinedoeeca2a22015-06-18 17:03:14 -0600334VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(
335 VkPhysicalDevice gpu,
336 const VkDeviceCreateInfo *pCreateInfo,
337 VkDevice *pDevice)
338{
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600339 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice);
340 VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pDevice);
David Pinedoeeca2a22015-06-18 17:03:14 -0600341
David Pinedo8a4f5562015-06-19 13:48:18 -0600342 if (result == VK_SUCCESS) {
Jon Ashburn59f3dfc2015-07-06 15:09:36 -0600343 init_screenshot();
David Pinedo8a4f5562015-06-19 13:48:18 -0600344 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
Cody Northrop98fb22a2015-09-01 10:18:45 -0600345 // Create a mapping from a device to a physicalDevice
346 if (deviceMap[*pDevice] == NULL)
347 {
348 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
349 deviceMap[*pDevice] = deviceMapElem;
350 }
351 deviceMap[*pDevice]->physicalDevice = gpu;
David Pinedo8a4f5562015-06-19 13:48:18 -0600352 }
353
David Pinedoeeca2a22015-06-18 17:03:14 -0600354 return result;
355}
356
Cody Northrop98fb22a2015-09-01 10:18:45 -0600357VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
358 VkInstance instance,
359 uint32_t* pPhysicalDeviceCount,
360 VkPhysicalDevice* pPhysicalDevices)
361{
362 VkResult result;
363
364 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
365 result = pTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Tobin Ehlisd8dd86b2015-09-08 08:59:48 -0600366 if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0 && pPhysicalDevices)
Cody Northrop98fb22a2015-09-01 10:18:45 -0600367 {
368 for (uint32_t i=0; i<*pPhysicalDeviceCount ; i++)
369 {
370 // Create a mapping from a physicalDevice to an instance
371 if (physDeviceMap[pPhysicalDevices[i]] == NULL)
372 {
373 PhysDeviceMapStruct *physDeviceMapElem = new PhysDeviceMapStruct;
374 physDeviceMap[pPhysicalDevices[i]] = physDeviceMapElem;
375 }
376 physDeviceMap[pPhysicalDevices[i]]->instance = instance;
377 }
378 }
379 return result;
380}
381
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600382/* TODO: Probably need a DestroyDevice as well */
383
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600384static const VkLayerProperties ss_device_layers[] = {
David Pinedoeeca2a22015-06-18 17:03:14 -0600385 {
David Pinedoeeca2a22015-06-18 17:03:14 -0600386 "ScreenShot",
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600387 VK_API_VERSION,
388 VK_MAKE_VERSION(0, 1, 0),
David Pinedoeeca2a22015-06-18 17:03:14 -0600389 "Layer: ScreenShot",
David Pinedo8a4f5562015-06-19 13:48:18 -0600390 }
David Pinedoeeca2a22015-06-18 17:03:14 -0600391};
David Pinedoeeca2a22015-06-18 17:03:14 -0600392
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600393VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600394 const char *pLayerName,
395 uint32_t *pCount,
396 VkExtensionProperties* pProperties)
David Pinedoeeca2a22015-06-18 17:03:14 -0600397{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600398 /* ScreenShot does not have any global extensions */
399 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600400}
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600401
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600402VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600403 uint32_t *pCount,
404 VkLayerProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600405{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600406 /* ScreenShot does not have any global layers */
407 return util_GetLayerProperties(0, NULL,
408 pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600409}
410
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600411VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600412 VkPhysicalDevice physicalDevice,
413 const char* pLayerName,
414 uint32_t* pCount,
415 VkExtensionProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600416{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600417 /* ScreenShot does not have any physical device extensions */
418 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
419}
Tony Barbour426b9052015-06-24 16:06:58 -0600420
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600421VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600422 VkPhysicalDevice physicalDevice,
423 uint32_t* pCount,
424 VkLayerProperties* pProperties)
425{
426 return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers),
427 ss_device_layers,
428 pCount, pProperties);
David Pinedoeeca2a22015-06-18 17:03:14 -0600429}
430
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600431VK_LAYER_EXPORT void VKAPI vkGetDeviceQueue(
David Pinedoeeca2a22015-06-18 17:03:14 -0600432 VkDevice device,
433 uint32_t queueNodeIndex,
434 uint32_t queueIndex,
435 VkQueue *pQueue)
436{
437 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600438 get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
David Pinedoeeca2a22015-06-18 17:03:14 -0600439
440 loader_platform_thread_lock_mutex(&globalLock);
441 if (screenshotEnvQueried && screenshotFrames.empty()) {
442 // We are all done taking screenshots, so don't do anything else
443 loader_platform_thread_unlock_mutex(&globalLock);
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600444 return;
David Pinedoeeca2a22015-06-18 17:03:14 -0600445 }
446
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600447 screenshot_device_table_map.emplace(*pQueue, pTable);
David Pinedoeeca2a22015-06-18 17:03:14 -0600448
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600449 // Create a mapping from a device to a queue
450 if (deviceMap[device] == NULL)
451 {
452 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
453 deviceMap[device] = deviceMapElem;
David Pinedoeeca2a22015-06-18 17:03:14 -0600454 }
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600455 deviceMap[device]->queue = *pQueue;
David Pinedoeeca2a22015-06-18 17:03:14 -0600456 loader_platform_thread_unlock_mutex(&globalLock);
David Pinedoeeca2a22015-06-18 17:03:14 -0600457}
458
David Pinedoaa4d1da2015-07-31 10:56:20 -0600459VK_LAYER_EXPORT VkResult VKAPI vkCreateCommandPool(
460 VkDevice device,
461 const VkCmdPoolCreateInfo *pCreateInfo,
462 VkCmdPool *pCmdPool)
463{
464 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
465 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateCommandPool(device, pCreateInfo, pCmdPool);
466
467 loader_platform_thread_lock_mutex(&globalLock);
468 if (screenshotEnvQueried && screenshotFrames.empty()) {
469 // We are all done taking screenshots, so don't do anything else
470 loader_platform_thread_unlock_mutex(&globalLock);
471 return result;
472 }
473
Cody Northrop98fb22a2015-09-01 10:18:45 -0600474 // Create a mapping from a device to a cmdPool
475 if (deviceMap[device] == NULL)
476 {
477 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
478 deviceMap[device] = deviceMapElem;
479 }
David Pinedoaa4d1da2015-07-31 10:56:20 -0600480 deviceMap[device]->cmdPool = *pCmdPool;
481 loader_platform_thread_unlock_mutex(&globalLock);
482 return result;
483}
484
Ian Elliott338dedb2015-08-21 15:09:33 -0600485VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapchainKHR(
David Pinedoeeca2a22015-06-18 17:03:14 -0600486 VkDevice device,
Ian Elliott338dedb2015-08-21 15:09:33 -0600487 const VkSwapchainCreateInfoKHR *pCreateInfo,
488 VkSwapchainKHR *pSwapchain)
David Pinedoeeca2a22015-06-18 17:03:14 -0600489{
490 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Ian Elliott338dedb2015-08-21 15:09:33 -0600491 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapchainKHR(device, pCreateInfo, pSwapchain);
David Pinedoeeca2a22015-06-18 17:03:14 -0600492
493 loader_platform_thread_lock_mutex(&globalLock);
494 if (screenshotEnvQueried && screenshotFrames.empty()) {
495 // We are all done taking screenshots, so don't do anything else
496 loader_platform_thread_unlock_mutex(&globalLock);
497 return result;
498 }
499
500 if (result == VK_SUCCESS)
501 {
502 // Create a mapping for a swapchain to a device, image extent, and format
503 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
504 swapchainMapElem->device = device;
505 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
506 swapchainMapElem->format = pCreateInfo->imageFormat;
Ian Elliott338dedb2015-08-21 15:09:33 -0600507 swapchainMap.insert(make_pair(pSwapchain->handle, swapchainMapElem));
David Pinedoeeca2a22015-06-18 17:03:14 -0600508
509 // Create a mapping for the swapchain object into the dispatch table
Ian Elliott338dedb2015-08-21 15:09:33 -0600510 screenshot_device_table_map.emplace((void *)pSwapchain->handle, pTable);
David Pinedoeeca2a22015-06-18 17:03:14 -0600511 }
512 loader_platform_thread_unlock_mutex(&globalLock);
513
514 return result;
515}
516
Ian Elliott338dedb2015-08-21 15:09:33 -0600517VK_LAYER_EXPORT VkResult VKAPI vkGetSwapchainImagesKHR(
Ian Elliottb134e842015-07-06 14:31:32 -0600518 VkDevice device,
Ian Elliott338dedb2015-08-21 15:09:33 -0600519 VkSwapchainKHR swapchain,
Ian Elliottccac0232015-08-07 14:11:14 -0600520 uint32_t* pCount,
Ian Elliott338dedb2015-08-21 15:09:33 -0600521 VkImage* pSwapchainImages)
David Pinedoeeca2a22015-06-18 17:03:14 -0600522{
Ian Elliott338dedb2015-08-21 15:09:33 -0600523 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapchainImagesKHR(device, swapchain, pCount, pSwapchainImages);
David Pinedoeeca2a22015-06-18 17:03:14 -0600524
525 loader_platform_thread_lock_mutex(&globalLock);
526 if (screenshotEnvQueried && screenshotFrames.empty()) {
527 // We are all done taking screenshots, so don't do anything else
528 loader_platform_thread_unlock_mutex(&globalLock);
529 return result;
530 }
531
532 if (result == VK_SUCCESS &&
Ian Elliott338dedb2015-08-21 15:09:33 -0600533 pSwapchainImages &&
534 !swapchainMap.empty() && swapchainMap.find(swapchain.handle) != swapchainMap.end())
Ian Elliottccac0232015-08-07 14:11:14 -0600535 {
Cody Northrop46d92c02015-09-01 11:48:13 -0600536 unsigned i;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600537
Ian Elliottccac0232015-08-07 14:11:14 -0600538 for (i=0; i<*pCount; i++)
David Pinedoeeca2a22015-06-18 17:03:14 -0600539 {
540 // Create a mapping for an image to a device, image extent, and format
Ian Elliott338dedb2015-08-21 15:09:33 -0600541 if (imageMap[pSwapchainImages[i].handle] == NULL)
Cody Northrop98fb22a2015-09-01 10:18:45 -0600542 {
543 ImageMapStruct *imageMapElem = new ImageMapStruct;
Ian Elliott338dedb2015-08-21 15:09:33 -0600544 imageMap[pSwapchainImages[i].handle] = imageMapElem;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600545 }
Ian Elliott338dedb2015-08-21 15:09:33 -0600546 imageMap[pSwapchainImages[i].handle]->device = swapchainMap[swapchain.handle]->device;
547 imageMap[pSwapchainImages[i].handle]->imageExtent = swapchainMap[swapchain.handle]->imageExtent;
548 imageMap[pSwapchainImages[i].handle]->format = swapchainMap[swapchain.handle]->format;
David Pinedoeeca2a22015-06-18 17:03:14 -0600549 }
David Pinedoaa4d1da2015-07-31 10:56:20 -0600550
551 // Add list of images to swapchain to image map
Ian Elliott338dedb2015-08-21 15:09:33 -0600552 SwapchainMapStruct *swapchainMapElem = swapchainMap[swapchain.handle];
David Pinedoaa4d1da2015-07-31 10:56:20 -0600553 if (i >= 1 && swapchainMapElem)
554 {
555 VkImage *imageList = new VkImage[i];
556 swapchainMapElem->imageList = imageList;
Cody Northrop46d92c02015-09-01 11:48:13 -0600557 for (unsigned j=0; j<i; j++)
David Pinedoaa4d1da2015-07-31 10:56:20 -0600558 {
Ian Elliott338dedb2015-08-21 15:09:33 -0600559 swapchainMapElem->imageList[j] = pSwapchainImages[j].handle;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600560 }
561 }
562
David Pinedoeeca2a22015-06-18 17:03:14 -0600563 }
564 loader_platform_thread_unlock_mutex(&globalLock);
565 return result;
566}
567
Ian Elliott338dedb2015-08-21 15:09:33 -0600568VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentKHR(VkQueue queue, VkPresentInfoKHR* pPresentInfo)
David Pinedoeeca2a22015-06-18 17:03:14 -0600569{
570 static int frameNumber = 0;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600571 if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ }
Ian Elliott338dedb2015-08-21 15:09:33 -0600572 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentKHR(queue, pPresentInfo);
David Pinedoeeca2a22015-06-18 17:03:14 -0600573
574 loader_platform_thread_lock_mutex(&globalLock);
575
576 if (!screenshotEnvQueried)
577 {
578 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
579 if (_vk_screenshot && *_vk_screenshot)
580 {
581 string spec(_vk_screenshot), word;
582 size_t start = 0, comma = 0;
583
584 while (start < spec.size()) {
585 int frameToAdd;
586 comma = spec.find(',', start);
587 if (comma == string::npos)
588 word = string(spec, start);
589 else
590 word = string(spec, start, comma - start);
591 frameToAdd=atoi(word.c_str());
592 // Add the frame number to list, but only do it if the word started with a digit and if
593 // it's not already in the list
594 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
595 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
596 {
597 screenshotFrames.push_back(frameToAdd);
598 }
599 if (comma == string::npos)
600 break;
601 start = comma + 1;
602 }
603 }
604 screenshotEnvQueried = true;
605 }
606
607
608 if (result == VK_SUCCESS && !screenshotFrames.empty())
609 {
610 vector<int>::iterator it;
611 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
612 if (it != screenshotFrames.end())
613 {
614 string fileName;
615 fileName = to_string(frameNumber) + ".ppm";
David Pinedoaa4d1da2015-07-31 10:56:20 -0600616
617 VkImage image;
Ian Elliott338dedb2015-08-21 15:09:33 -0600618 uint64_t swapchain;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600619 // We'll dump only one image: the first
Ian Elliott338dedb2015-08-21 15:09:33 -0600620 swapchain = pPresentInfo->swapchains[0].handle;
621 image = swapchainMap[swapchain]->imageList[pPresentInfo->imageIndices[0]];
David Pinedoaa4d1da2015-07-31 10:56:20 -0600622 writePPM(fileName.c_str(), image);
David Pinedoeeca2a22015-06-18 17:03:14 -0600623 screenshotFrames.erase(it);
624
625 if (screenshotFrames.empty())
626 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600627 // Free all our maps since we are done with them.
David Pinedoeeca2a22015-06-18 17:03:14 -0600628 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
629 {
630 SwapchainMapStruct *swapchainMapElem = it->second;
631 delete swapchainMapElem;
632 }
633 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
634 {
635 ImageMapStruct *imageMapElem = it->second;
636 delete imageMapElem;
637 }
638 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
639 {
640 DeviceMapStruct *deviceMapElem = it->second;
641 delete deviceMapElem;
642 }
Cody Northrop98fb22a2015-09-01 10:18:45 -0600643 for (auto it = physDeviceMap.begin(); it != physDeviceMap.end(); it++)
644 {
645 PhysDeviceMapStruct *physDeviceMapElem = it->second;
646 delete physDeviceMapElem;
647 }
David Pinedoeeca2a22015-06-18 17:03:14 -0600648 swapchainMap.clear();
649 imageMap.clear();
650 deviceMap.clear();
Cody Northrop98fb22a2015-09-01 10:18:45 -0600651 physDeviceMap.clear();
David Pinedoeeca2a22015-06-18 17:03:14 -0600652 }
653 }
654 }
655 frameNumber++;
656 loader_platform_thread_unlock_mutex(&globalLock);
657 return result;
658}
659
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600660VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(
David Pinedoeeca2a22015-06-18 17:03:14 -0600661 VkDevice dev,
662 const char *funcName)
663{
David Pinedoeeca2a22015-06-18 17:03:14 -0600664 if (dev == NULL) {
665 return NULL;
666 }
667
668 /* loader uses this to force layer initialization; device object is wrapped */
669 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
670 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600671 return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
David Pinedoeeca2a22015-06-18 17:03:14 -0600672 }
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600673 if (!strcmp(funcName, "vkCreateDevice"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600674 return (PFN_vkVoidFunction) vkCreateDevice;
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600675
David Pinedoeeca2a22015-06-18 17:03:14 -0600676 if (!strcmp(funcName, "vkGetDeviceQueue"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600677 return (PFN_vkVoidFunction) vkGetDeviceQueue;
678
679 if (!strcmp(funcName, "vkCreateCommandPool"))
680 return (PFN_vkVoidFunction) vkCreateCommandPool;
David Pinedoeeca2a22015-06-18 17:03:14 -0600681
David Pinedo8a4f5562015-06-19 13:48:18 -0600682 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
Jon Ashburn83334db2015-09-16 18:08:32 -0600683 if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].wsi_enabled)
David Pinedo8a4f5562015-06-19 13:48:18 -0600684 {
Ian Elliott338dedb2015-08-21 15:09:33 -0600685 if (!strcmp(funcName, "vkCreateSwapchainKHR"))
686 return (PFN_vkVoidFunction) vkCreateSwapchainKHR;
687 if (!strcmp(funcName, "vkGetSwapchainImagesKHR"))
688 return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR;
689 if (!strcmp(funcName, "vkQueuePresentKHR"))
690 return (PFN_vkVoidFunction) vkQueuePresentKHR;
David Pinedo8a4f5562015-06-19 13:48:18 -0600691 }
692
693 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600694 return NULL;
David Pinedo8a4f5562015-06-19 13:48:18 -0600695 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedoeeca2a22015-06-18 17:03:14 -0600696}
David Pinedo07238d42015-07-09 16:23:44 -0600697
698
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600699VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
David Pinedo07238d42015-07-09 16:23:44 -0600700{
David Pinedo07238d42015-07-09 16:23:44 -0600701 if (instance == VK_NULL_HANDLE) {
702 return NULL;
703 }
704
705 /* loader uses this to force layer initialization; instance object is wrapped */
706 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
707 initInstanceTable((const VkBaseLayerObject *) instance);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600708 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
David Pinedo07238d42015-07-09 16:23:44 -0600709 }
710
Cody Northrop98fb22a2015-09-01 10:18:45 -0600711 if (!strcmp(funcName, "vkEnumeratePhysicalDevices"))
712 return (PFN_vkVoidFunction)vkEnumeratePhysicalDevices;
713
David Pinedo07238d42015-07-09 16:23:44 -0600714 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
715 if (pTable->GetInstanceProcAddr == NULL)
716 return NULL;
717 return pTable->GetInstanceProcAddr(instance, funcName);
David Pinedo07238d42015-07-09 16:23:44 -0600718}