blob: 05e7414d4799edd62e333f30d09cb4922f47ca92 [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
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
219 err = memory_type_from_properties(&memory_properties,
220 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);
223 assert(!err);
224
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
231 err = pTableDevice->CreateCommandBuffer(device, &createCommandBufferInfo, &cmdBuffer);
232 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 };
248 VkSubmitInfo submit_info = {
249 .waitSemCount = 0,
250 .pWaitSemaphores = NULL,
251 .cmdBufferCount = 1,
252 .pCommandBuffers = &cmdBuffer,
253 .signalSemCount = 0,
254 .pSignalSemaphores = NULL
255 };
256
257 err = pTableQueue->QueueSubmit(queue, 1, &submit_info, nullFence);
David Pinedoeeca2a22015-06-18 17:03:14 -0600258 assert(!err);
259
260 err = pTableQueue->QueueWaitIdle(queue);
261 assert(!err);
262
263 err = pTableDevice->DeviceWaitIdle(device);
264 assert(!err);
265
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600266 pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -0600267
268 err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr );
269 assert(!err);
270
271 ptr += sr_layout.offset;
272
273 ofstream file(filename, ios::binary);
274
275 file << "P6\n";
276 file << width << "\n";
277 file << height << "\n";
278 file << 255 << "\n";
279
280 for (y = 0; y < height; y++) {
281 const unsigned int *row = (const unsigned int*) ptr;
282 if (format == VK_FORMAT_B8G8R8A8_UNORM)
283 {
284 for (x = 0; x < width; x++) {
285 unsigned int swapped;
286 swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16;
287 file.write((char *)&swapped, 3);
288 row++;
289 }
290 }
291 else if (format == VK_FORMAT_R8G8B8A8_UNORM)
292 {
293 for (x = 0; x < width; x++) {
294 file.write((char *)row, 3);
295 row++;
296 }
297 }
298 else
299 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600300 // TODO: add support for additional formats
David Pinedoeeca2a22015-06-18 17:03:14 -0600301 printf("Unrecognized image format\n");
302 break;
303 }
304 ptr += sr_layout.rowPitch;
305 }
306 file.close();
307
308 // Clean up
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600309 pTableDevice->UnmapMemory(device, mem2);
310 pTableDevice->FreeMemory(device, mem2);
311 pTableDevice->DestroyCommandBuffer(device, cmdBuffer);
David Pinedoeeca2a22015-06-18 17:03:14 -0600312}
313
314
David Pinedo8a4f5562015-06-19 13:48:18 -0600315static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device)
316{
Cody Northrop1684adb2015-08-05 11:15:02 -0600317 uint32_t i;
David Pinedo8a4f5562015-06-19 13:48:18 -0600318 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device);
Jon Ashburn83334db2015-09-16 18:08:32 -0600319 PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr;
320 pDisp->GetSurfacePropertiesKHR = (PFN_vkGetSurfacePropertiesKHR) gpa(device, "vkGetSurfacePropertiesKHR");
321 pDisp->GetSurfaceFormatsKHR = (PFN_vkGetSurfaceFormatsKHR) gpa(device, "vkGetSurfaceFormatsKHR");
322 pDisp->GetSurfacePresentModesKHR = (PFN_vkGetSurfacePresentModesKHR) gpa(device, "vkGetSurfacePresentModesKHR");
323 pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR) gpa(device, "vkCreateSwapchainKHR");
324 pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR) gpa(device, "vkDestroySwapchainKHR");
325 pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR) gpa(device, "vkGetSwapchainImagesKHR");
326 pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR) gpa(device, "vkAcquireNextImageKHR");
327 pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR) gpa(device, "vkQueuePresentKHR");
Ian Elliottb134e842015-07-06 14:31:32 -0600328 deviceExtMap[pDisp].wsi_enabled = false;
David Pinedo8a4f5562015-06-19 13:48:18 -0600329 for (i = 0; i < pCreateInfo->extensionCount; i++) {
Ian Elliott338dedb2015-08-21 15:09:33 -0600330 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_EXT_KHR_DEVICE_SWAPCHAIN_EXTENSION_NAME) == 0)
Ian Elliottb134e842015-07-06 14:31:32 -0600331 deviceExtMap[pDisp].wsi_enabled = true;
David Pinedo8a4f5562015-06-19 13:48:18 -0600332 }
333}
334
David Pinedoeeca2a22015-06-18 17:03:14 -0600335VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(
336 VkPhysicalDevice gpu,
337 const VkDeviceCreateInfo *pCreateInfo,
338 VkDevice *pDevice)
339{
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600340 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice);
341 VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pDevice);
David Pinedoeeca2a22015-06-18 17:03:14 -0600342
David Pinedo8a4f5562015-06-19 13:48:18 -0600343 if (result == VK_SUCCESS) {
Jon Ashburn59f3dfc2015-07-06 15:09:36 -0600344 init_screenshot();
David Pinedo8a4f5562015-06-19 13:48:18 -0600345 createDeviceRegisterExtensions(pCreateInfo, *pDevice);
Cody Northrop98fb22a2015-09-01 10:18:45 -0600346 // Create a mapping from a device to a physicalDevice
347 if (deviceMap[*pDevice] == NULL)
348 {
349 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
350 deviceMap[*pDevice] = deviceMapElem;
351 }
352 deviceMap[*pDevice]->physicalDevice = gpu;
David Pinedo8a4f5562015-06-19 13:48:18 -0600353 }
354
David Pinedoeeca2a22015-06-18 17:03:14 -0600355 return result;
356}
357
Cody Northrop98fb22a2015-09-01 10:18:45 -0600358VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices(
359 VkInstance instance,
360 uint32_t* pPhysicalDeviceCount,
361 VkPhysicalDevice* pPhysicalDevices)
362{
363 VkResult result;
364
365 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
366 result = pTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
Tobin Ehlisd8dd86b2015-09-08 08:59:48 -0600367 if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0 && pPhysicalDevices)
Cody Northrop98fb22a2015-09-01 10:18:45 -0600368 {
369 for (uint32_t i=0; i<*pPhysicalDeviceCount ; i++)
370 {
371 // Create a mapping from a physicalDevice to an instance
372 if (physDeviceMap[pPhysicalDevices[i]] == NULL)
373 {
374 PhysDeviceMapStruct *physDeviceMapElem = new PhysDeviceMapStruct;
375 physDeviceMap[pPhysicalDevices[i]] = physDeviceMapElem;
376 }
377 physDeviceMap[pPhysicalDevices[i]]->instance = instance;
378 }
379 }
380 return result;
381}
382
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600383/* TODO: Probably need a DestroyDevice as well */
384
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600385static const VkLayerProperties ss_device_layers[] = {
David Pinedoeeca2a22015-06-18 17:03:14 -0600386 {
David Pinedoeeca2a22015-06-18 17:03:14 -0600387 "ScreenShot",
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600388 VK_API_VERSION,
389 VK_MAKE_VERSION(0, 1, 0),
David Pinedoeeca2a22015-06-18 17:03:14 -0600390 "Layer: ScreenShot",
David Pinedo8a4f5562015-06-19 13:48:18 -0600391 }
David Pinedoeeca2a22015-06-18 17:03:14 -0600392};
David Pinedoeeca2a22015-06-18 17:03:14 -0600393
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600394VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600395 const char *pLayerName,
396 uint32_t *pCount,
397 VkExtensionProperties* pProperties)
David Pinedoeeca2a22015-06-18 17:03:14 -0600398{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600399 /* ScreenShot does not have any global extensions */
400 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600401}
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600402
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600403VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600404 uint32_t *pCount,
405 VkLayerProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600406{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600407 /* ScreenShot does not have any global layers */
408 return util_GetLayerProperties(0, NULL,
409 pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600410}
411
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600412VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600413 VkPhysicalDevice physicalDevice,
414 const char* pLayerName,
415 uint32_t* pCount,
416 VkExtensionProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600417{
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600418 /* ScreenShot does not have any physical device extensions */
419 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
420}
Tony Barbour426b9052015-06-24 16:06:58 -0600421
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600422VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterb96abfc2015-07-07 11:25:43 -0600423 VkPhysicalDevice physicalDevice,
424 uint32_t* pCount,
425 VkLayerProperties* pProperties)
426{
427 return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers),
428 ss_device_layers,
429 pCount, pProperties);
David Pinedoeeca2a22015-06-18 17:03:14 -0600430}
431
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600432VK_LAYER_EXPORT void VKAPI vkGetDeviceQueue(
David Pinedoeeca2a22015-06-18 17:03:14 -0600433 VkDevice device,
434 uint32_t queueNodeIndex,
435 uint32_t queueIndex,
436 VkQueue *pQueue)
437{
438 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600439 get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue);
David Pinedoeeca2a22015-06-18 17:03:14 -0600440
441 loader_platform_thread_lock_mutex(&globalLock);
442 if (screenshotEnvQueried && screenshotFrames.empty()) {
443 // We are all done taking screenshots, so don't do anything else
444 loader_platform_thread_unlock_mutex(&globalLock);
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600445 return;
David Pinedoeeca2a22015-06-18 17:03:14 -0600446 }
447
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600448 screenshot_device_table_map.emplace(*pQueue, pTable);
David Pinedoeeca2a22015-06-18 17:03:14 -0600449
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600450 // Create a mapping from a device to a queue
451 if (deviceMap[device] == NULL)
452 {
453 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
454 deviceMap[device] = deviceMapElem;
David Pinedoeeca2a22015-06-18 17:03:14 -0600455 }
Courtney Goeltzenleuchter01d2ae12015-10-20 16:40:38 -0600456 deviceMap[device]->queue = *pQueue;
David Pinedoeeca2a22015-06-18 17:03:14 -0600457 loader_platform_thread_unlock_mutex(&globalLock);
David Pinedoeeca2a22015-06-18 17:03:14 -0600458}
459
David Pinedoaa4d1da2015-07-31 10:56:20 -0600460VK_LAYER_EXPORT VkResult VKAPI vkCreateCommandPool(
461 VkDevice device,
462 const VkCmdPoolCreateInfo *pCreateInfo,
463 VkCmdPool *pCmdPool)
464{
465 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
466 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateCommandPool(device, pCreateInfo, pCmdPool);
467
468 loader_platform_thread_lock_mutex(&globalLock);
469 if (screenshotEnvQueried && screenshotFrames.empty()) {
470 // We are all done taking screenshots, so don't do anything else
471 loader_platform_thread_unlock_mutex(&globalLock);
472 return result;
473 }
474
Cody Northrop98fb22a2015-09-01 10:18:45 -0600475 // Create a mapping from a device to a cmdPool
476 if (deviceMap[device] == NULL)
477 {
478 DeviceMapStruct *deviceMapElem = new DeviceMapStruct;
479 deviceMap[device] = deviceMapElem;
480 }
David Pinedoaa4d1da2015-07-31 10:56:20 -0600481 deviceMap[device]->cmdPool = *pCmdPool;
482 loader_platform_thread_unlock_mutex(&globalLock);
483 return result;
484}
485
Ian Elliott338dedb2015-08-21 15:09:33 -0600486VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapchainKHR(
David Pinedoeeca2a22015-06-18 17:03:14 -0600487 VkDevice device,
Ian Elliott338dedb2015-08-21 15:09:33 -0600488 const VkSwapchainCreateInfoKHR *pCreateInfo,
489 VkSwapchainKHR *pSwapchain)
David Pinedoeeca2a22015-06-18 17:03:14 -0600490{
491 VkLayerDispatchTable* pTable = screenshot_device_table_map[device];
Ian Elliott338dedb2015-08-21 15:09:33 -0600492 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapchainKHR(device, pCreateInfo, pSwapchain);
David Pinedoeeca2a22015-06-18 17:03:14 -0600493
494 loader_platform_thread_lock_mutex(&globalLock);
495 if (screenshotEnvQueried && screenshotFrames.empty()) {
496 // We are all done taking screenshots, so don't do anything else
497 loader_platform_thread_unlock_mutex(&globalLock);
498 return result;
499 }
500
501 if (result == VK_SUCCESS)
502 {
503 // Create a mapping for a swapchain to a device, image extent, and format
504 SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct;
505 swapchainMapElem->device = device;
506 swapchainMapElem->imageExtent = pCreateInfo->imageExtent;
507 swapchainMapElem->format = pCreateInfo->imageFormat;
Ian Elliott338dedb2015-08-21 15:09:33 -0600508 swapchainMap.insert(make_pair(pSwapchain->handle, swapchainMapElem));
David Pinedoeeca2a22015-06-18 17:03:14 -0600509
510 // Create a mapping for the swapchain object into the dispatch table
Ian Elliott338dedb2015-08-21 15:09:33 -0600511 screenshot_device_table_map.emplace((void *)pSwapchain->handle, pTable);
David Pinedoeeca2a22015-06-18 17:03:14 -0600512 }
513 loader_platform_thread_unlock_mutex(&globalLock);
514
515 return result;
516}
517
Ian Elliott338dedb2015-08-21 15:09:33 -0600518VK_LAYER_EXPORT VkResult VKAPI vkGetSwapchainImagesKHR(
Ian Elliottb134e842015-07-06 14:31:32 -0600519 VkDevice device,
Ian Elliott338dedb2015-08-21 15:09:33 -0600520 VkSwapchainKHR swapchain,
Ian Elliottccac0232015-08-07 14:11:14 -0600521 uint32_t* pCount,
Ian Elliott338dedb2015-08-21 15:09:33 -0600522 VkImage* pSwapchainImages)
David Pinedoeeca2a22015-06-18 17:03:14 -0600523{
Ian Elliott338dedb2015-08-21 15:09:33 -0600524 VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapchainImagesKHR(device, swapchain, pCount, pSwapchainImages);
David Pinedoeeca2a22015-06-18 17:03:14 -0600525
526 loader_platform_thread_lock_mutex(&globalLock);
527 if (screenshotEnvQueried && screenshotFrames.empty()) {
528 // We are all done taking screenshots, so don't do anything else
529 loader_platform_thread_unlock_mutex(&globalLock);
530 return result;
531 }
532
533 if (result == VK_SUCCESS &&
Ian Elliott338dedb2015-08-21 15:09:33 -0600534 pSwapchainImages &&
535 !swapchainMap.empty() && swapchainMap.find(swapchain.handle) != swapchainMap.end())
Ian Elliottccac0232015-08-07 14:11:14 -0600536 {
Cody Northrop46d92c02015-09-01 11:48:13 -0600537 unsigned i;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600538
Ian Elliottccac0232015-08-07 14:11:14 -0600539 for (i=0; i<*pCount; i++)
David Pinedoeeca2a22015-06-18 17:03:14 -0600540 {
541 // Create a mapping for an image to a device, image extent, and format
Ian Elliott338dedb2015-08-21 15:09:33 -0600542 if (imageMap[pSwapchainImages[i].handle] == NULL)
Cody Northrop98fb22a2015-09-01 10:18:45 -0600543 {
544 ImageMapStruct *imageMapElem = new ImageMapStruct;
Ian Elliott338dedb2015-08-21 15:09:33 -0600545 imageMap[pSwapchainImages[i].handle] = imageMapElem;
Cody Northrop98fb22a2015-09-01 10:18:45 -0600546 }
Ian Elliott338dedb2015-08-21 15:09:33 -0600547 imageMap[pSwapchainImages[i].handle]->device = swapchainMap[swapchain.handle]->device;
548 imageMap[pSwapchainImages[i].handle]->imageExtent = swapchainMap[swapchain.handle]->imageExtent;
549 imageMap[pSwapchainImages[i].handle]->format = swapchainMap[swapchain.handle]->format;
David Pinedoeeca2a22015-06-18 17:03:14 -0600550 }
David Pinedoaa4d1da2015-07-31 10:56:20 -0600551
552 // Add list of images to swapchain to image map
Ian Elliott338dedb2015-08-21 15:09:33 -0600553 SwapchainMapStruct *swapchainMapElem = swapchainMap[swapchain.handle];
David Pinedoaa4d1da2015-07-31 10:56:20 -0600554 if (i >= 1 && swapchainMapElem)
555 {
556 VkImage *imageList = new VkImage[i];
557 swapchainMapElem->imageList = imageList;
Cody Northrop46d92c02015-09-01 11:48:13 -0600558 for (unsigned j=0; j<i; j++)
David Pinedoaa4d1da2015-07-31 10:56:20 -0600559 {
Ian Elliott338dedb2015-08-21 15:09:33 -0600560 swapchainMapElem->imageList[j] = pSwapchainImages[j].handle;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600561 }
562 }
563
David Pinedoeeca2a22015-06-18 17:03:14 -0600564 }
565 loader_platform_thread_unlock_mutex(&globalLock);
566 return result;
567}
568
Ian Elliott338dedb2015-08-21 15:09:33 -0600569VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentKHR(VkQueue queue, VkPresentInfoKHR* pPresentInfo)
David Pinedoeeca2a22015-06-18 17:03:14 -0600570{
571 static int frameNumber = 0;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600572 if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ }
Ian Elliott338dedb2015-08-21 15:09:33 -0600573 VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentKHR(queue, pPresentInfo);
David Pinedoeeca2a22015-06-18 17:03:14 -0600574
575 loader_platform_thread_lock_mutex(&globalLock);
576
577 if (!screenshotEnvQueried)
578 {
579 const char *_vk_screenshot = getenv("_VK_SCREENSHOT");
580 if (_vk_screenshot && *_vk_screenshot)
581 {
582 string spec(_vk_screenshot), word;
583 size_t start = 0, comma = 0;
584
585 while (start < spec.size()) {
586 int frameToAdd;
587 comma = spec.find(',', start);
588 if (comma == string::npos)
589 word = string(spec, start);
590 else
591 word = string(spec, start, comma - start);
592 frameToAdd=atoi(word.c_str());
593 // Add the frame number to list, but only do it if the word started with a digit and if
594 // it's not already in the list
595 if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' &&
596 find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end())
597 {
598 screenshotFrames.push_back(frameToAdd);
599 }
600 if (comma == string::npos)
601 break;
602 start = comma + 1;
603 }
604 }
605 screenshotEnvQueried = true;
606 }
607
608
609 if (result == VK_SUCCESS && !screenshotFrames.empty())
610 {
611 vector<int>::iterator it;
612 it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber);
613 if (it != screenshotFrames.end())
614 {
615 string fileName;
616 fileName = to_string(frameNumber) + ".ppm";
David Pinedoaa4d1da2015-07-31 10:56:20 -0600617
618 VkImage image;
Ian Elliott338dedb2015-08-21 15:09:33 -0600619 uint64_t swapchain;
David Pinedoaa4d1da2015-07-31 10:56:20 -0600620 // We'll dump only one image: the first
Ian Elliott338dedb2015-08-21 15:09:33 -0600621 swapchain = pPresentInfo->swapchains[0].handle;
622 image = swapchainMap[swapchain]->imageList[pPresentInfo->imageIndices[0]];
David Pinedoaa4d1da2015-07-31 10:56:20 -0600623 writePPM(fileName.c_str(), image);
David Pinedoeeca2a22015-06-18 17:03:14 -0600624 screenshotFrames.erase(it);
625
626 if (screenshotFrames.empty())
627 {
David Pinedoaa4d1da2015-07-31 10:56:20 -0600628 // Free all our maps since we are done with them.
David Pinedoeeca2a22015-06-18 17:03:14 -0600629 for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++)
630 {
631 SwapchainMapStruct *swapchainMapElem = it->second;
632 delete swapchainMapElem;
633 }
634 for (auto it = imageMap.begin(); it != imageMap.end(); it++)
635 {
636 ImageMapStruct *imageMapElem = it->second;
637 delete imageMapElem;
638 }
639 for (auto it = deviceMap.begin(); it != deviceMap.end(); it++)
640 {
641 DeviceMapStruct *deviceMapElem = it->second;
642 delete deviceMapElem;
643 }
Cody Northrop98fb22a2015-09-01 10:18:45 -0600644 for (auto it = physDeviceMap.begin(); it != physDeviceMap.end(); it++)
645 {
646 PhysDeviceMapStruct *physDeviceMapElem = it->second;
647 delete physDeviceMapElem;
648 }
David Pinedoeeca2a22015-06-18 17:03:14 -0600649 swapchainMap.clear();
650 imageMap.clear();
651 deviceMap.clear();
Cody Northrop98fb22a2015-09-01 10:18:45 -0600652 physDeviceMap.clear();
David Pinedoeeca2a22015-06-18 17:03:14 -0600653 }
654 }
655 }
656 frameNumber++;
657 loader_platform_thread_unlock_mutex(&globalLock);
658 return result;
659}
660
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600661VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(
David Pinedoeeca2a22015-06-18 17:03:14 -0600662 VkDevice dev,
663 const char *funcName)
664{
David Pinedoeeca2a22015-06-18 17:03:14 -0600665 if (dev == NULL) {
666 return NULL;
667 }
668
669 /* loader uses this to force layer initialization; device object is wrapped */
670 if (!strcmp(funcName, "vkGetDeviceProcAddr")) {
671 initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600672 return (PFN_vkVoidFunction)vkGetDeviceProcAddr;
David Pinedoeeca2a22015-06-18 17:03:14 -0600673 }
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600674 if (!strcmp(funcName, "vkCreateDevice"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600675 return (PFN_vkVoidFunction) vkCreateDevice;
Courtney Goeltzenleuchterbe637992015-06-25 18:01:43 -0600676
David Pinedoeeca2a22015-06-18 17:03:14 -0600677 if (!strcmp(funcName, "vkGetDeviceQueue"))
David Pinedoaa4d1da2015-07-31 10:56:20 -0600678 return (PFN_vkVoidFunction) vkGetDeviceQueue;
679
680 if (!strcmp(funcName, "vkCreateCommandPool"))
681 return (PFN_vkVoidFunction) vkCreateCommandPool;
David Pinedoeeca2a22015-06-18 17:03:14 -0600682
David Pinedo8a4f5562015-06-19 13:48:18 -0600683 VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev);
Jon Ashburn83334db2015-09-16 18:08:32 -0600684 if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].wsi_enabled)
David Pinedo8a4f5562015-06-19 13:48:18 -0600685 {
Ian Elliott338dedb2015-08-21 15:09:33 -0600686 if (!strcmp(funcName, "vkCreateSwapchainKHR"))
687 return (PFN_vkVoidFunction) vkCreateSwapchainKHR;
688 if (!strcmp(funcName, "vkGetSwapchainImagesKHR"))
689 return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR;
690 if (!strcmp(funcName, "vkQueuePresentKHR"))
691 return (PFN_vkVoidFunction) vkQueuePresentKHR;
David Pinedo8a4f5562015-06-19 13:48:18 -0600692 }
693
694 if (pDisp->GetDeviceProcAddr == NULL)
David Pinedoeeca2a22015-06-18 17:03:14 -0600695 return NULL;
David Pinedo8a4f5562015-06-19 13:48:18 -0600696 return pDisp->GetDeviceProcAddr(dev, funcName);
David Pinedoeeca2a22015-06-18 17:03:14 -0600697}
David Pinedo07238d42015-07-09 16:23:44 -0600698
699
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -0600700VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
David Pinedo07238d42015-07-09 16:23:44 -0600701{
David Pinedo07238d42015-07-09 16:23:44 -0600702 if (instance == VK_NULL_HANDLE) {
703 return NULL;
704 }
705
706 /* loader uses this to force layer initialization; instance object is wrapped */
707 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
708 initInstanceTable((const VkBaseLayerObject *) instance);
David Pinedoaa4d1da2015-07-31 10:56:20 -0600709 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
David Pinedo07238d42015-07-09 16:23:44 -0600710 }
711
Cody Northrop98fb22a2015-09-01 10:18:45 -0600712 if (!strcmp(funcName, "vkEnumeratePhysicalDevices"))
713 return (PFN_vkVoidFunction)vkEnumeratePhysicalDevices;
714
David Pinedo07238d42015-07-09 16:23:44 -0600715 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance);
716 if (pTable->GetInstanceProcAddr == NULL)
717 return NULL;
718 return pTable->GetInstanceProcAddr(instance, funcName);
David Pinedo07238d42015-07-09 16:23:44 -0600719}