blob: e9ec3eb8776cbe47dbb534bb4ebe0cb0fef6f44b [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 };
171 const VkCmdBufferCreateInfo createCommandBufferInfo = {
172 VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
173 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,
David Pinedoeeca2a22015-06-18 17:03:14 -0600176 0
177 };
178 const VkCmdBufferBeginInfo cmdBufferBeginInfo = {
179 VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
180 NULL,
181 VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
182 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
183 };
184 const VkImageCopy imageCopyRegion = {
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600185 {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0},
David Pinedoeeca2a22015-06-18 17:03:14 -0600186 {0, 0, 0},
Courtney Goeltzenleuchterba11ebe2015-10-21 17:00:51 -0600187 {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0},
David Pinedoeeca2a22015-06-18 17:03:14 -0600188 {0, 0, 0},
189 {width, height, 1}
190 };
191 VkMemoryRequirements memRequirements;
David Pinedoeeca2a22015-06-18 17:03:14 -0600192 uint32_t num_allocations = 0;
193 size_t num_alloc_size = sizeof(num_allocations);
David Pinedoc3256662015-06-30 13:08:37 -0600194 VkLayerDispatchTable* pTableDevice = get_dispatch_table(screenshot_device_table_map, device);
195 VkLayerDispatchTable* pTableQueue = get_dispatch_table(screenshot_device_table_map, queue);
Cody Northrop98fb22a2015-09-01 10:18:45 -0600196 VkLayerInstanceDispatchTable* pInstanceTable;
David Pinedoeeca2a22015-06-18 17:03:14 -0600197 VkLayerDispatchTable* pTableCmdBuffer;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600198 VkPhysicalDeviceMemoryProperties memory_properties;
David Pinedoeeca2a22015-06-18 17:03:14 -0600199
David Pinedoaa4d1da2015-07-31 10:56:20 -0600200 if (imageMap.empty() || imageMap.find(image1.handle) == imageMap.end())
David Pinedoeeca2a22015-06-18 17:03:14 -0600201 return;
202
203 // The VkImage image1 we are going to dump may not be mappable,
204 // and/or it may have a tiling mode of optimal rather than linear.
205 // To make sure we have an image that we can map and read linearly, we:
206 // create image2 that is mappable and linear
207 // copy image1 to image2
208 // map image2
209 // read from image2's mapped memeory.
210
211 err = pTableDevice->CreateImage(device, &imgCreateInfo, &image2);
212 assert(!err);
213
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600214 pTableDevice->GetImageMemoryRequirements(device, image2, &memRequirements);
David Pinedoeeca2a22015-06-18 17:03:14 -0600215
216 memAllocInfo.allocationSize = memRequirements.size;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600217 pInstanceTable = instance_dispatch_table(instance);
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600218 pInstanceTable->GetPhysicalDeviceMemoryProperties(physicalDevice, &memory_properties);
Cody Northrop98fb22a2015-09-01 10:18:45 -0600219
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600220 pass = memory_type_from_properties(&memory_properties,
Cody Northrop98fb22a2015-09-01 10:18:45 -0600221 memRequirements.memoryTypeBits,
Cody Northrop46d10d02015-09-01 11:47:50 -0600222 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Cody Northrop98fb22a2015-09-01 10:18:45 -0600223 &memAllocInfo.memoryTypeIndex);
Courtney Goeltzenleuchter37a43a62015-10-22 11:03:31 -0600224 assert(pass);
Cody Northrop98fb22a2015-09-01 10:18:45 -0600225
David Pinedoeeca2a22015-06-18 17:03:14 -0600226 err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2);
227 assert(!err);
228
David Pinedoaa4d1da2015-07-31 10:56:20 -0600229 err = pTableQueue->BindImageMemory(device, image2, mem2, 0);
David Pinedoeeca2a22015-06-18 17:03:14 -0600230 assert(!err);
231
232 err = pTableDevice->CreateCommandBuffer(device, &createCommandBufferInfo, &cmdBuffer);
233 assert(!err);
234
235 screenshot_device_table_map.emplace(cmdBuffer, pTableDevice);
236 pTableCmdBuffer = screenshot_device_table_map[cmdBuffer];
237
238 err = pTableCmdBuffer->BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo);
239 assert(!err);
240
Cody Northrop46d10d02015-09-01 11:47:50 -0600241 // TODO: We need to transition images to match these layouts, then restore the original layouts
242 pTableCmdBuffer->CmdCopyImage(cmdBuffer, image1, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
243 image2, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, 1, &imageCopyRegion);
David Pinedoeeca2a22015-06-18 17:03:14 -0600244
245 err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer);
246 assert(!err);
247
Courtney Goeltzenleuchter3ec31622015-10-20 18:04:07 -0600248 VkFence nullFence = { VK_NULL_HANDLE };
249 VkSubmitInfo submit_info = {
250 .waitSemCount = 0,
251 .pWaitSemaphores = NULL,
252 .cmdBufferCount = 1,
253 .pCommandBuffers = &cmdBuffer,
254 .signalSemCount = 0,
255 .pSignalSemaphores = NULL
256 };
257
258 err = pTableQueue->QueueSubmit(queue, 1, &submit_info, nullFence);
David Pinedoeeca2a22015-06-18 17:03:14 -0600259 assert(!err);
260
261 err = pTableQueue->QueueWaitIdle(queue);
262 assert(!err);
263
264 err = pTableDevice->DeviceWaitIdle(device);
265 assert(!err);
266
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600267 pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -0600268
269 err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr );
270 assert(!err);
271
272 ptr += sr_layout.offset;
273
274 ofstream file(filename, ios::binary);
275
276 file << "P6\n";
277 file << width << "\n";
278 file << height << "\n";
279 file << 255 << "\n";
280
281 for (y = 0; y < height; y++) {
282 const unsigned int *row = (const unsigned int*) ptr;
283 if (format == VK_FORMAT_B8G8R8A8_UNORM)
284 {
285 for (x = 0; x < width; x++) {
286 unsigned int swapped;
287 swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16;
288 file.write((char *)&swapped, 3);
289 row++;
290 }
291 }
292 else if (format == VK_FORMAT_R8G8B8A8_UNORM)
293 {
294 for (x = 0; x < width; x++) {
295 file.write((char *)row, 3);
296 row++;
297 }
298 }
299 else
300 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600301 // TODO: add support for additional formats
David Pinedoeeca2a22015-06-18 17:03:14 -0600302 printf("Unrecognized image format\n");
303 break;
304 }
305 ptr += sr_layout.rowPitch;
306 }
307 file.close();
308
309 // Clean up
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600310 pTableDevice->UnmapMemory(device, mem2);
311 pTableDevice->FreeMemory(device, mem2);
312 pTableDevice->DestroyCommandBuffer(device, cmdBuffer);
David Pinedoeeca2a22015-06-18 17:03:14 -0600313}
314
315
David Pinedo8a4f5562015-06-19 13:48:18 -0600316static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
317{
Cody Northrop1684adb2015-08-05 11:15:02 -0600318 uint32_t i;
David Pinedo8a4f5562015-06-19 13:48:18 -0600319 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
Jon Ashburn83334db2015-09-16 18:08:32 -0600320 PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr;
321 pDisp->GetSurfacePropertiesKHR = (PFN_vkGetSurfacePropertiesKHR) gpa(device, "vkGetSurfacePropertiesKHR");
322 pDisp->GetSurfaceFormatsKHR = (PFN_vkGetSurfaceFormatsKHR) gpa(device, "vkGetSurfaceFormatsKHR");
323 pDisp->GetSurfacePresentModesKHR = (PFN_vkGetSurfacePresentModesKHR) gpa(device, "vkGetSurfacePresentModesKHR");
324 pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR) gpa(device, "vkCreateSwapchainKHR");
325 pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR) gpa(device, "vkDestroySwapchainKHR");
326 pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR) gpa(device, "vkGetSwapchainImagesKHR");
327 pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR) gpa(device, "vkAcquireNextImageKHR");
328 pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR) gpa(device, "vkQueuePresentKHR");
Ian Elliottb134e842015-07-06 14:31:32 -0600329 deviceExtMap[pDisp].wsi_enabled = false;
David Pinedo8a4f5562015-06-19 13:48:18 -0600330 for (i = 0; i < pCreateInfo->extensionCount; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -0600331 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_EXT_KHR_DEVICE_SWAPCHAIN_EXTENSION_NAME) == 0)
Ian Elliottb134e842015-07-06 14:31:32 -0600332 deviceExtMap[pDisp].wsi_enabled = true;
David Pinedo8a4f5562015-06-19 13:48:18 -0600333 }
334}
335
David Pinedoeeca2a22015-06-18 17:03:14 -0600336VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(
337 VkPhysicalDevice gpu,
338 const VkDeviceCreateInfo *pCreateInfo,
339 VkDevice *pDevice)
340{
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600341 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice);
342 VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pDevice);
David Pinedoeeca2a22015-06-18 17:03:14 -0600343
David Pinedo8a4f5562015-06-19 13:48:18 -0600344 if (result == VK_SUCCESS) {
Jon Ashburn59f3dfc2015-07-06 15:09:36 -0600345 init_screenshot();
David Pinedo8a4f5562015-06-19 13:48:18 -0600346 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
Cody Northrop98fb22a2015-09-01 10:18:45 -0600347 // Create a mapping from a device to a physicalDevice
348 if (deviceMap[*pDevice] == NULL)
349 {
350 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
351 deviceMap[*pDevice] = deviceMapElem;
352 }
353 deviceMap[*pDevice]->physicalDevice = gpu;
David Pinedo8a4f5562015-06-19 13:48:18 -0600354 }
355
David Pinedoeeca2a22015-06-18 17:03:14 -0600356 return result;
357}
358
Cody Northrop98fb22a2015-09-01 10:18:45 -0600359VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
360 VkInstance instance,
361 uint32_t* pPhysicalDeviceCount,
362 VkPhysicalDevice* pPhysicalDevices)
363{
364 VkResult result;
365
366 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
367 result = pTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Tobin Ehlisd8dd86b2015-09-08 08:59:48 -0600368 if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0 && pPhysicalDevices)
Cody Northrop98fb22a2015-09-01 10:18:45 -0600369 {
370 for (uint32_t i=0; i<*pPhysicalDeviceCount ; i++)
371 {
372 // Create a mapping from a physicalDevice to an instance
373 if (physDeviceMap[pPhysicalDevices[i]] == NULL)
374 {
375 PhysDeviceMapStruct *physDeviceMapElem = new PhysDeviceMapStruct;
376 physDeviceMap[pPhysicalDevices[i]] = physDeviceMapElem;
377 }
378 physDeviceMap[pPhysicalDevices[i]]->instance = instance;
379 }
380 }
381 return result;
382}
383
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600384/* TODO: Probably need a DestroyDevice as well */
385
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600386static const VkLayerProperties ss_device_layers[] = {
David Pinedoeeca2a22015-06-18 17:03:14 -0600387 {
David Pinedoeeca2a22015-06-18 17:03:14 -0600388 "ScreenShot",
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600389 VK_API_VERSION,
390 VK_MAKE_VERSION(0, 1, 0),
David Pinedoeeca2a22015-06-18 17:03:14 -0600391 "Layer: ScreenShot",
David Pinedo8a4f5562015-06-19 13:48:18 -0600392 }
David Pinedoeeca2a22015-06-18 17:03:14 -0600393};
David Pinedoeeca2a22015-06-18 17:03:14 -0600394
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600395VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600396 const char *pLayerName,
397 uint32_t *pCount,
398 VkExtensionProperties* pProperties)
David Pinedoeeca2a22015-06-18 17:03:14 -0600399{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600400 /* ScreenShot does not have any global extensions */
401 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600402}
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600403
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600404VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600405 uint32_t *pCount,
406 VkLayerProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600407{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600408 /* ScreenShot does not have any global layers */
409 return util_GetLayerProperties(0, NULL,
410 pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600411}
412
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600413VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600414 VkPhysicalDevice physicalDevice,
415 const char* pLayerName,
416 uint32_t* pCount,
417 VkExtensionProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600418{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600419 /* ScreenShot does not have any physical device extensions */
420 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
421}
Tony Barbour426b9052015-06-24 16:06:58 -0600422
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600423VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600424 VkPhysicalDevice physicalDevice,
425 uint32_t* pCount,
426 VkLayerProperties* pProperties)
427{
428 return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers),
429 ss_device_layers,
430 pCount, pProperties);
David Pinedoeeca2a22015-06-18 17:03:14 -0600431}
432
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600433VK_LAYER_EXPORT void VKAPI vkGetDeviceQueue(
David Pinedoeeca2a22015-06-18 17:03:14 -0600434 VkDevice device,
435 uint32_t queueNodeIndex,
436 uint32_t queueIndex,
437 VkQueue *pQueue)
438{
439 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600440 get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
David Pinedoeeca2a22015-06-18 17:03:14 -0600441
442 loader_platform_thread_lock_mutex(&globalLock);
443 if (screenshotEnvQueried && screenshotFrames.empty()) {
444 // We are all done taking screenshots, so don't do anything else
445 loader_platform_thread_unlock_mutex(&globalLock);
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600446 return;
David Pinedoeeca2a22015-06-18 17:03:14 -0600447 }
448
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600449 screenshot_device_table_map.emplace(*pQueue, pTable);
David Pinedoeeca2a22015-06-18 17:03:14 -0600450
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600451 // Create a mapping from a device to a queue
452 if (deviceMap[device] == NULL)
453 {
454 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
455 deviceMap[device] = deviceMapElem;
David Pinedoeeca2a22015-06-18 17:03:14 -0600456 }
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600457 deviceMap[device]->queue = *pQueue;
David Pinedoeeca2a22015-06-18 17:03:14 -0600458 loader_platform_thread_unlock_mutex(&globalLock);
David Pinedoeeca2a22015-06-18 17:03:14 -0600459}
460
David Pinedoaa4d1da2015-07-31 10:56:20 -0600461VK_LAYER_EXPORT VkResult VKAPI vkCreateCommandPool(
462 VkDevice device,
463 const VkCmdPoolCreateInfo *pCreateInfo,
464 VkCmdPool *pCmdPool)
465{
466 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
467 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateCommandPool(device, pCreateInfo, pCmdPool);
468
469 loader_platform_thread_lock_mutex(&globalLock);
470 if (screenshotEnvQueried && screenshotFrames.empty()) {
471 // We are all done taking screenshots, so don't do anything else
472 loader_platform_thread_unlock_mutex(&globalLock);
473 return result;
474 }
475
Cody Northrop98fb22a2015-09-01 10:18:45 -0600476 // Create a mapping from a device to a cmdPool
477 if (deviceMap[device] == NULL)
478 {
479 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
480 deviceMap[device] = deviceMapElem;
481 }
David Pinedoaa4d1da2015-07-31 10:56:20 -0600482 deviceMap[device]->cmdPool = *pCmdPool;
483 loader_platform_thread_unlock_mutex(&globalLock);
484 return result;
485}
486
Ian Elliott338dedb2015-08-21 15:09:33 -0600487VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapchainKHR(
David Pinedoeeca2a22015-06-18 17:03:14 -0600488 VkDevice device,
Ian Elliott338dedb2015-08-21 15:09:33 -0600489 const VkSwapchainCreateInfoKHR *pCreateInfo,
490 VkSwapchainKHR *pSwapchain)
David Pinedoeeca2a22015-06-18 17:03:14 -0600491{
492 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Ian Elliott338dedb2015-08-21 15:09:33 -0600493 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapchainKHR(device, pCreateInfo, pSwapchain);
David Pinedoeeca2a22015-06-18 17:03:14 -0600494
495 loader_platform_thread_lock_mutex(&globalLock);
496 if (screenshotEnvQueried && screenshotFrames.empty()) {
497 // We are all done taking screenshots, so don't do anything else
498 loader_platform_thread_unlock_mutex(&globalLock);
499 return result;
500 }
501
502 if (result == VK_SUCCESS)
503 {
504 // Create a mapping for a swapchain to a device, image extent, and format
505 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
506 swapchainMapElem->device = device;
507 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
508 swapchainMapElem->format = pCreateInfo->imageFormat;
Ian Elliott338dedb2015-08-21 15:09:33 -0600509 swapchainMap.insert(make_pair(pSwapchain->handle, swapchainMapElem));
David Pinedoeeca2a22015-06-18 17:03:14 -0600510
511 // Create a mapping for the swapchain object into the dispatch table
Ian Elliott338dedb2015-08-21 15:09:33 -0600512 screenshot_device_table_map.emplace((void *)pSwapchain->handle, pTable);
David Pinedoeeca2a22015-06-18 17:03:14 -0600513 }
514 loader_platform_thread_unlock_mutex(&globalLock);
515
516 return result;
517}
518
Ian Elliott338dedb2015-08-21 15:09:33 -0600519VK_LAYER_EXPORT VkResult VKAPI vkGetSwapchainImagesKHR(
Ian Elliottb134e842015-07-06 14:31:32 -0600520 VkDevice device,
Ian Elliott338dedb2015-08-21 15:09:33 -0600521 VkSwapchainKHR swapchain,
Ian Elliottccac0232015-08-07 14:11:14 -0600522 uint32_t* pCount,
Ian Elliott338dedb2015-08-21 15:09:33 -0600523 VkImage* pSwapchainImages)
David Pinedoeeca2a22015-06-18 17:03:14 -0600524{
Ian Elliott338dedb2015-08-21 15:09:33 -0600525 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapchainImagesKHR(device, swapchain, pCount, pSwapchainImages);
David Pinedoeeca2a22015-06-18 17:03:14 -0600526
527 loader_platform_thread_lock_mutex(&globalLock);
528 if (screenshotEnvQueried && screenshotFrames.empty()) {
529 // We are all done taking screenshots, so don't do anything else
530 loader_platform_thread_unlock_mutex(&globalLock);
531 return result;
532 }
533
534 if (result == VK_SUCCESS &&
Ian Elliott338dedb2015-08-21 15:09:33 -0600535 pSwapchainImages &&
536 !swapchainMap.empty() && swapchainMap.find(swapchain.handle) != swapchainMap.end())
Ian Elliottccac0232015-08-07 14:11:14 -0600537 {
Cody Northrop46d92c02015-09-01 11:48:13 -0600538 unsigned i;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600539
Ian Elliottccac0232015-08-07 14:11:14 -0600540 for (i=0; i<*pCount; i++)
David Pinedoeeca2a22015-06-18 17:03:14 -0600541 {
542 // Create a mapping for an image to a device, image extent, and format
Ian Elliott338dedb2015-08-21 15:09:33 -0600543 if (imageMap[pSwapchainImages[i].handle] == NULL)
Cody Northrop98fb22a2015-09-01 10:18:45 -0600544 {
545 ImageMapStruct *imageMapElem = new ImageMapStruct;
Ian Elliott338dedb2015-08-21 15:09:33 -0600546 imageMap[pSwapchainImages[i].handle] = imageMapElem;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600547 }
Ian Elliott338dedb2015-08-21 15:09:33 -0600548 imageMap[pSwapchainImages[i].handle]->device = swapchainMap[swapchain.handle]->device;
549 imageMap[pSwapchainImages[i].handle]->imageExtent = swapchainMap[swapchain.handle]->imageExtent;
550 imageMap[pSwapchainImages[i].handle]->format = swapchainMap[swapchain.handle]->format;
David Pinedoeeca2a22015-06-18 17:03:14 -0600551 }
David Pinedoaa4d1da2015-07-31 10:56:20 -0600552
553 // Add list of images to swapchain to image map
Ian Elliott338dedb2015-08-21 15:09:33 -0600554 SwapchainMapStruct *swapchainMapElem = swapchainMap[swapchain.handle];
David Pinedoaa4d1da2015-07-31 10:56:20 -0600555 if (i >= 1 && swapchainMapElem)
556 {
557 VkImage *imageList = new VkImage[i];
558 swapchainMapElem->imageList = imageList;
Cody Northrop46d92c02015-09-01 11:48:13 -0600559 for (unsigned j=0; j<i; j++)
David Pinedoaa4d1da2015-07-31 10:56:20 -0600560 {
Ian Elliott338dedb2015-08-21 15:09:33 -0600561 swapchainMapElem->imageList[j] = pSwapchainImages[j].handle;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600562 }
563 }
564
David Pinedoeeca2a22015-06-18 17:03:14 -0600565 }
566 loader_platform_thread_unlock_mutex(&globalLock);
567 return result;
568}
569
Ian Elliott338dedb2015-08-21 15:09:33 -0600570VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentKHR(VkQueue queue, VkPresentInfoKHR* pPresentInfo)
David Pinedoeeca2a22015-06-18 17:03:14 -0600571{
572 static int frameNumber = 0;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600573 if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ }
Ian Elliott338dedb2015-08-21 15:09:33 -0600574 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentKHR(queue, pPresentInfo);
David Pinedoeeca2a22015-06-18 17:03:14 -0600575
576 loader_platform_thread_lock_mutex(&globalLock);
577
578 if (!screenshotEnvQueried)
579 {
580 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
581 if (_vk_screenshot && *_vk_screenshot)
582 {
583 string spec(_vk_screenshot), word;
584 size_t start = 0, comma = 0;
585
586 while (start < spec.size()) {
587 int frameToAdd;
588 comma = spec.find(',', start);
589 if (comma == string::npos)
590 word = string(spec, start);
591 else
592 word = string(spec, start, comma - start);
593 frameToAdd=atoi(word.c_str());
594 // Add the frame number to list, but only do it if the word started with a digit and if
595 // it's not already in the list
596 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
597 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
598 {
599 screenshotFrames.push_back(frameToAdd);
600 }
601 if (comma == string::npos)
602 break;
603 start = comma + 1;
604 }
605 }
606 screenshotEnvQueried = true;
607 }
608
609
610 if (result == VK_SUCCESS && !screenshotFrames.empty())
611 {
612 vector<int>::iterator it;
613 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
614 if (it != screenshotFrames.end())
615 {
616 string fileName;
617 fileName = to_string(frameNumber) + ".ppm";
David Pinedoaa4d1da2015-07-31 10:56:20 -0600618
619 VkImage image;
Ian Elliott338dedb2015-08-21 15:09:33 -0600620 uint64_t swapchain;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600621 // We'll dump only one image: the first
Ian Elliott338dedb2015-08-21 15:09:33 -0600622 swapchain = pPresentInfo->swapchains[0].handle;
623 image = swapchainMap[swapchain]->imageList[pPresentInfo->imageIndices[0]];
David Pinedoaa4d1da2015-07-31 10:56:20 -0600624 writePPM(fileName.c_str(), image);
David Pinedoeeca2a22015-06-18 17:03:14 -0600625 screenshotFrames.erase(it);
626
627 if (screenshotFrames.empty())
628 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600629 // Free all our maps since we are done with them.
David Pinedoeeca2a22015-06-18 17:03:14 -0600630 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
631 {
632 SwapchainMapStruct *swapchainMapElem = it->second;
633 delete swapchainMapElem;
634 }
635 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
636 {
637 ImageMapStruct *imageMapElem = it->second;
638 delete imageMapElem;
639 }
640 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
641 {
642 DeviceMapStruct *deviceMapElem = it->second;
643 delete deviceMapElem;
644 }
Cody Northrop98fb22a2015-09-01 10:18:45 -0600645 for (auto it = physDeviceMap.begin(); it != physDeviceMap.end(); it++)
646 {
647 PhysDeviceMapStruct *physDeviceMapElem = it->second;
648 delete physDeviceMapElem;
649 }
David Pinedoeeca2a22015-06-18 17:03:14 -0600650 swapchainMap.clear();
651 imageMap.clear();
652 deviceMap.clear();
Cody Northrop98fb22a2015-09-01 10:18:45 -0600653 physDeviceMap.clear();
David Pinedoeeca2a22015-06-18 17:03:14 -0600654 }
655 }
656 }
657 frameNumber++;
658 loader_platform_thread_unlock_mutex(&globalLock);
659 return result;
660}
661
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600662VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(
David Pinedoeeca2a22015-06-18 17:03:14 -0600663 VkDevice dev,
664 const char *funcName)
665{
David Pinedoeeca2a22015-06-18 17:03:14 -0600666 if (dev == NULL) {
667 return NULL;
668 }
669
670 /* loader uses this to force layer initialization; device object is wrapped */
671 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
672 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600673 return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
David Pinedoeeca2a22015-06-18 17:03:14 -0600674 }
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600675 if (!strcmp(funcName, "vkCreateDevice"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600676 return (PFN_vkVoidFunction) vkCreateDevice;
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600677
David Pinedoeeca2a22015-06-18 17:03:14 -0600678 if (!strcmp(funcName, "vkGetDeviceQueue"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600679 return (PFN_vkVoidFunction) vkGetDeviceQueue;
680
681 if (!strcmp(funcName, "vkCreateCommandPool"))
682 return (PFN_vkVoidFunction) vkCreateCommandPool;
David Pinedoeeca2a22015-06-18 17:03:14 -0600683
David Pinedo8a4f5562015-06-19 13:48:18 -0600684 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
Jon Ashburn83334db2015-09-16 18:08:32 -0600685 if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].wsi_enabled)
David Pinedo8a4f5562015-06-19 13:48:18 -0600686 {
Ian Elliott338dedb2015-08-21 15:09:33 -0600687 if (!strcmp(funcName, "vkCreateSwapchainKHR"))
688 return (PFN_vkVoidFunction) vkCreateSwapchainKHR;
689 if (!strcmp(funcName, "vkGetSwapchainImagesKHR"))
690 return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR;
691 if (!strcmp(funcName, "vkQueuePresentKHR"))
692 return (PFN_vkVoidFunction) vkQueuePresentKHR;
David Pinedo8a4f5562015-06-19 13:48:18 -0600693 }
694
695 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600696 return NULL;
David Pinedo8a4f5562015-06-19 13:48:18 -0600697 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedoeeca2a22015-06-18 17:03:14 -0600698}
David Pinedo07238d42015-07-09 16:23:44 -0600699
700
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600701VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
David Pinedo07238d42015-07-09 16:23:44 -0600702{
David Pinedo07238d42015-07-09 16:23:44 -0600703 if (instance == VK_NULL_HANDLE) {
704 return NULL;
705 }
706
707 /* loader uses this to force layer initialization; instance object is wrapped */
708 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
709 initInstanceTable((const VkBaseLayerObject *) instance);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600710 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
David Pinedo07238d42015-07-09 16:23:44 -0600711 }
712
Cody Northrop98fb22a2015-09-01 10:18:45 -0600713 if (!strcmp(funcName, "vkEnumeratePhysicalDevices"))
714 return (PFN_vkVoidFunction)vkEnumeratePhysicalDevices;
715
David Pinedo07238d42015-07-09 16:23:44 -0600716 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
717 if (pTable->GetInstanceProcAddr == NULL)
718 return NULL;
719 return pTable->GetInstanceProcAddr(instance, funcName);
David Pinedo07238d42015-07-09 16:23:44 -0600720}