David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1 | /* |
| 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 | |
| 38 | using namespace std; |
| 39 | |
Tobin Ehlis | b835d1b | 2015-07-03 10:34:49 -0600 | [diff] [blame] | 40 | #include "vk_loader_platform.h" |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 41 | #include "vk_dispatch_table_helper.h" |
| 42 | #include "vk_struct_string_helper_cpp.h" |
Tobin Ehlis | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 43 | #include "vk_layer_config.h" |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 44 | // The following is #included again to catch certain OS-specific functions |
| 45 | // being used: |
Tobin Ehlis | b835d1b | 2015-07-03 10:34:49 -0600 | [diff] [blame] | 46 | #include "vk_loader_platform.h" |
Tobin Ehlis | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 47 | #include "vk_layer_table.h" |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 48 | #include "vk_layer_extension_utils.h" |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 49 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 50 | |
| 51 | struct devExts { |
| 52 | bool wsi_lunarg_enabled; |
| 53 | }; |
| 54 | static std::unordered_map<void *, struct devExts> deviceExtMap; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 55 | static device_table_map screenshot_device_table_map; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 56 | |
| 57 | static int globalLockInitialized = 0; |
| 58 | static loader_platform_thread_mutex globalLock; |
| 59 | |
| 60 | // unordered map, associates a swap chain with a device, image extent, and format |
| 61 | typedef struct |
| 62 | { |
| 63 | VkDevice device; |
| 64 | VkExtent2D imageExtent; |
| 65 | VkFormat format; |
| 66 | } SwapchainMapStruct; |
| 67 | static unordered_map<VkSwapChainWSI, SwapchainMapStruct *> swapchainMap; |
| 68 | |
| 69 | // unordered map, associates an image with a device, image extent, and format |
| 70 | typedef struct |
| 71 | { |
| 72 | VkDevice device; |
| 73 | VkExtent2D imageExtent; |
| 74 | VkFormat format; |
| 75 | } ImageMapStruct; |
| 76 | static unordered_map<VkImage, ImageMapStruct *> imageMap; |
| 77 | |
| 78 | // unordered map, associates a device with a queue |
| 79 | typedef struct |
| 80 | { |
| 81 | VkQueue queue; |
| 82 | uint32_t queueNodeIndex; |
| 83 | uint32_t queueIndex; |
| 84 | } DeviceMapStruct; |
| 85 | static unordered_map<VkDevice, DeviceMapStruct *> deviceMap; |
| 86 | |
| 87 | // List of frames to we will get a screenshot of |
| 88 | static vector<int> screenshotFrames; |
| 89 | |
| 90 | // Flag indicating we have queried _VK_SCREENSHOT env var |
| 91 | static bool screenshotEnvQueried = false; |
| 92 | |
| 93 | static void init_screenshot() |
| 94 | { |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 95 | if (!globalLockInitialized) |
| 96 | { |
| 97 | // TODO/TBD: Need to delete this mutex sometime. How??? One |
| 98 | // suggestion is to call this during vkCreateInstance(), and then we |
| 99 | // can clean it up during vkDestroyInstance(). However, that requires |
| 100 | // that the layer have per-instance locks. We need to come back and |
| 101 | // address this soon. |
| 102 | loader_platform_thread_create_mutex(&globalLock); |
| 103 | globalLockInitialized = 1; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static void writePPM( const char *filename, VkImage image1) |
| 108 | { |
| 109 | VkImage image2; |
| 110 | VkResult err; |
| 111 | int x, y; |
| 112 | const char *ptr; |
| 113 | VkDeviceMemory mem2; |
| 114 | VkCmdBuffer cmdBuffer; |
| 115 | VkDevice device = imageMap[image1]->device; |
| 116 | VkQueue queue = deviceMap[device]->queue; |
| 117 | int width = imageMap[image1]->imageExtent.width; |
| 118 | int height = imageMap[image1]->imageExtent.height; |
| 119 | VkFormat format = imageMap[image1]->format; |
| 120 | const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR, 0, 0}; |
| 121 | VkSubresourceLayout sr_layout; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 122 | const VkImageCreateInfo imgCreateInfo = { |
| 123 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 124 | NULL, |
| 125 | VK_IMAGE_TYPE_2D, |
| 126 | format, |
| 127 | {width, height, 1}, |
| 128 | 1, |
| 129 | 1, |
| 130 | 1, |
| 131 | VK_IMAGE_TILING_LINEAR, |
| 132 | (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT|VK_IMAGE_USAGE_STORAGE_BIT), |
| 133 | 0 |
| 134 | }; |
| 135 | VkMemoryAllocInfo memAllocInfo = { |
| 136 | VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
| 137 | NULL, |
| 138 | 0, // allocationSize, queried later |
| 139 | (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | |
| 140 | VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | |
Mark Lobodzinski | 3fbc9c2 | 2015-07-02 17:09:57 -0600 | [diff] [blame] | 141 | VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 142 | }; |
| 143 | const VkCmdBufferCreateInfo createCommandBufferInfo = { |
| 144 | VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
| 145 | NULL, |
| 146 | deviceMap[device]->queueNodeIndex, |
Chia-I Wu | 0b50a1c | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 147 | VK_CMD_BUFFER_LEVEL_PRIMARY, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 148 | 0 |
| 149 | }; |
| 150 | const VkCmdBufferBeginInfo cmdBufferBeginInfo = { |
| 151 | VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
| 152 | NULL, |
| 153 | VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT | |
| 154 | VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
| 155 | }; |
| 156 | const VkImageCopy imageCopyRegion = { |
| 157 | {VK_IMAGE_ASPECT_COLOR, 0, 0}, |
| 158 | {0, 0, 0}, |
| 159 | {VK_IMAGE_ASPECT_COLOR, 0, 0}, |
| 160 | {0, 0, 0}, |
| 161 | {width, height, 1} |
| 162 | }; |
| 163 | VkMemoryRequirements memRequirements; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 164 | uint32_t num_allocations = 0; |
| 165 | size_t num_alloc_size = sizeof(num_allocations); |
David Pinedo | 6829587 | 2015-06-30 13:08:37 -0600 | [diff] [blame] | 166 | VkLayerDispatchTable* pTableDevice = get_dispatch_table(screenshot_device_table_map, device); |
| 167 | VkLayerDispatchTable* pTableQueue = get_dispatch_table(screenshot_device_table_map, queue); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 168 | VkLayerDispatchTable* pTableCmdBuffer; |
| 169 | |
| 170 | if (imageMap.empty() || imageMap.find(image1) == imageMap.end()) |
| 171 | return; |
| 172 | |
| 173 | // The VkImage image1 we are going to dump may not be mappable, |
| 174 | // and/or it may have a tiling mode of optimal rather than linear. |
| 175 | // To make sure we have an image that we can map and read linearly, we: |
| 176 | // create image2 that is mappable and linear |
| 177 | // copy image1 to image2 |
| 178 | // map image2 |
| 179 | // read from image2's mapped memeory. |
| 180 | |
| 181 | err = pTableDevice->CreateImage(device, &imgCreateInfo, &image2); |
| 182 | assert(!err); |
| 183 | |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 184 | err = pTableDevice->GetObjectMemoryRequirements(device, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 185 | VK_OBJECT_TYPE_IMAGE, image2, |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 186 | &memRequirements); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 187 | assert(!err); |
| 188 | |
| 189 | memAllocInfo.allocationSize = memRequirements.size; |
| 190 | err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2); |
| 191 | assert(!err); |
| 192 | |
| 193 | err = pTableQueue->BindObjectMemory(device, VK_OBJECT_TYPE_IMAGE, image2, mem2, 0); |
| 194 | assert(!err); |
| 195 | |
| 196 | err = pTableDevice->CreateCommandBuffer(device, &createCommandBufferInfo, &cmdBuffer); |
| 197 | assert(!err); |
| 198 | |
| 199 | screenshot_device_table_map.emplace(cmdBuffer, pTableDevice); |
| 200 | pTableCmdBuffer = screenshot_device_table_map[cmdBuffer]; |
| 201 | |
| 202 | err = pTableCmdBuffer->BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo); |
| 203 | assert(!err); |
| 204 | |
| 205 | pTableCmdBuffer->CmdCopyImage(cmdBuffer, image1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 206 | image2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1, &imageCopyRegion); |
| 207 | |
| 208 | err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer); |
| 209 | assert(!err); |
| 210 | |
| 211 | err = pTableQueue->QueueSubmit(queue, 1, &cmdBuffer, VK_NULL_HANDLE); |
| 212 | assert(!err); |
| 213 | |
| 214 | err = pTableQueue->QueueWaitIdle(queue); |
| 215 | assert(!err); |
| 216 | |
| 217 | err = pTableDevice->DeviceWaitIdle(device); |
| 218 | assert(!err); |
| 219 | |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 220 | err = pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 221 | assert(!err); |
| 222 | |
| 223 | err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr ); |
| 224 | assert(!err); |
| 225 | |
| 226 | ptr += sr_layout.offset; |
| 227 | |
| 228 | ofstream file(filename, ios::binary); |
| 229 | |
| 230 | file << "P6\n"; |
| 231 | file << width << "\n"; |
| 232 | file << height << "\n"; |
| 233 | file << 255 << "\n"; |
| 234 | |
| 235 | for (y = 0; y < height; y++) { |
| 236 | const unsigned int *row = (const unsigned int*) ptr; |
| 237 | if (format == VK_FORMAT_B8G8R8A8_UNORM) |
| 238 | { |
| 239 | for (x = 0; x < width; x++) { |
| 240 | unsigned int swapped; |
| 241 | swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16; |
| 242 | file.write((char *)&swapped, 3); |
| 243 | row++; |
| 244 | } |
| 245 | } |
| 246 | else if (format == VK_FORMAT_R8G8B8A8_UNORM) |
| 247 | { |
| 248 | for (x = 0; x < width; x++) { |
| 249 | file.write((char *)row, 3); |
| 250 | row++; |
| 251 | } |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | // TODO: add support for addition formats |
| 256 | printf("Unrecognized image format\n"); |
| 257 | break; |
| 258 | } |
| 259 | ptr += sr_layout.rowPitch; |
| 260 | } |
| 261 | file.close(); |
| 262 | |
| 263 | // Clean up |
| 264 | err = pTableDevice->UnmapMemory(device, mem2); |
| 265 | assert(!err); |
| 266 | err = pTableDevice->FreeMemory(device, mem2); |
| 267 | assert(!err); |
| 268 | err = pTableDevice->DestroyObject(device, VK_OBJECT_TYPE_COMMAND_BUFFER, cmdBuffer); |
| 269 | assert(!err); |
| 270 | } |
| 271 | |
| 272 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 273 | static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device) |
| 274 | { |
| 275 | uint32_t i, ext_idx; |
| 276 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device); |
| 277 | deviceExtMap[pDisp].wsi_lunarg_enabled = false; |
| 278 | for (i = 0; i < pCreateInfo->extensionCount; i++) { |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 279 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_WSI_LUNARG_EXTENSION_NAME) == 0) |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 280 | deviceExtMap[pDisp].wsi_lunarg_enabled = true; |
| 281 | } |
| 282 | } |
| 283 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 284 | VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice( |
| 285 | VkPhysicalDevice gpu, |
| 286 | const VkDeviceCreateInfo *pCreateInfo, |
| 287 | VkDevice *pDevice) |
| 288 | { |
Courtney Goeltzenleuchter | ca173b8 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 289 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice); |
| 290 | VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pDevice); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 291 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 292 | if (result == VK_SUCCESS) { |
Jon Ashburn | ec10533 | 2015-07-06 15:09:36 -0600 | [diff] [blame] | 293 | init_screenshot(); |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 294 | createDeviceRegisterExtensions(pCreateInfo, *pDevice); |
| 295 | } |
| 296 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 297 | return result; |
| 298 | } |
| 299 | |
Courtney Goeltzenleuchter | ca173b8 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 300 | /* TODO: Probably need a DestroyDevice as well */ |
| 301 | |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 302 | static const VkLayerProperties ss_device_layers[] = { |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 303 | { |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 304 | "ScreenShot", |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 305 | VK_API_VERSION, |
| 306 | VK_MAKE_VERSION(0, 1, 0), |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 307 | "Layer: ScreenShot", |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 308 | } |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 309 | }; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 310 | |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 311 | VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionProperties( |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 312 | const char *pLayerName, |
| 313 | uint32_t *pCount, |
| 314 | VkExtensionProperties* pProperties) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 315 | { |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 316 | /* ScreenShot does not have any global extensions */ |
| 317 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 318 | } |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 319 | |
| 320 | VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalLayerProperties( |
| 321 | uint32_t *pCount, |
| 322 | VkLayerProperties* pProperties) |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 323 | { |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 324 | /* ScreenShot does not have any global layers */ |
| 325 | return util_GetLayerProperties(0, NULL, |
| 326 | pCount, pProperties); |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionProperties( |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 330 | VkPhysicalDevice physicalDevice, |
| 331 | const char* pLayerName, |
| 332 | uint32_t* pCount, |
| 333 | VkExtensionProperties* pProperties) |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 334 | { |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 335 | /* ScreenShot does not have any physical device extensions */ |
| 336 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
| 337 | } |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 338 | |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 339 | VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceLayerProperties( |
| 340 | VkPhysicalDevice physicalDevice, |
| 341 | uint32_t* pCount, |
| 342 | VkLayerProperties* pProperties) |
| 343 | { |
| 344 | return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers), |
| 345 | ss_device_layers, |
| 346 | pCount, pProperties); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | VK_LAYER_EXPORT VkResult VKAPI vkGetDeviceQueue( |
| 350 | VkDevice device, |
| 351 | uint32_t queueNodeIndex, |
| 352 | uint32_t queueIndex, |
| 353 | VkQueue *pQueue) |
| 354 | { |
| 355 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
| 356 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue); |
| 357 | |
| 358 | loader_platform_thread_lock_mutex(&globalLock); |
| 359 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 360 | // We are all done taking screenshots, so don't do anything else |
| 361 | loader_platform_thread_unlock_mutex(&globalLock); |
| 362 | return result; |
| 363 | } |
| 364 | |
| 365 | if (result == VK_SUCCESS) { |
| 366 | screenshot_device_table_map.emplace(*pQueue, pTable); |
| 367 | |
| 368 | // Create a mapping for the swapchain object into the dispatch table |
| 369 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 370 | deviceMapElem->queue = *pQueue; |
| 371 | deviceMapElem->queueNodeIndex = queueNodeIndex; |
| 372 | deviceMapElem->queueIndex = queueIndex; |
| 373 | deviceMap.insert(make_pair(device, deviceMapElem)); |
| 374 | } |
| 375 | loader_platform_thread_unlock_mutex(&globalLock); |
| 376 | return result; |
| 377 | } |
| 378 | |
| 379 | VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapChainWSI( |
| 380 | VkDevice device, |
| 381 | const VkSwapChainCreateInfoWSI *pCreateInfo, |
| 382 | VkSwapChainWSI *pSwapChain) |
| 383 | { |
| 384 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
| 385 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapChainWSI(device, pCreateInfo, pSwapChain); |
| 386 | |
| 387 | loader_platform_thread_lock_mutex(&globalLock); |
| 388 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 389 | // We are all done taking screenshots, so don't do anything else |
| 390 | loader_platform_thread_unlock_mutex(&globalLock); |
| 391 | return result; |
| 392 | } |
| 393 | |
| 394 | if (result == VK_SUCCESS) |
| 395 | { |
| 396 | // Create a mapping for a swapchain to a device, image extent, and format |
| 397 | SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct; |
| 398 | swapchainMapElem->device = device; |
| 399 | swapchainMapElem->imageExtent = pCreateInfo->imageExtent; |
| 400 | swapchainMapElem->format = pCreateInfo->imageFormat; |
| 401 | swapchainMap.insert(make_pair(*pSwapChain, swapchainMapElem)); |
| 402 | |
| 403 | // Create a mapping for the swapchain object into the dispatch table |
| 404 | screenshot_device_table_map.emplace(*pSwapChain, pTable); |
| 405 | } |
| 406 | loader_platform_thread_unlock_mutex(&globalLock); |
| 407 | |
| 408 | return result; |
| 409 | } |
| 410 | |
| 411 | VK_LAYER_EXPORT VkResult VKAPI vkGetSwapChainInfoWSI( |
| 412 | VkSwapChainWSI swapChain, |
| 413 | VkSwapChainInfoTypeWSI infoType, |
| 414 | size_t *pDataSize, |
| 415 | void *pData) |
| 416 | { |
| 417 | VkResult result = get_dispatch_table(screenshot_device_table_map, swapChain)->GetSwapChainInfoWSI(swapChain, infoType, pDataSize, pData); |
| 418 | |
| 419 | loader_platform_thread_lock_mutex(&globalLock); |
| 420 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 421 | // We are all done taking screenshots, so don't do anything else |
| 422 | loader_platform_thread_unlock_mutex(&globalLock); |
| 423 | return result; |
| 424 | } |
| 425 | |
| 426 | if (result == VK_SUCCESS && |
| 427 | !swapchainMap.empty() && swapchainMap.find(swapChain) != swapchainMap.end()) |
| 428 | { |
| 429 | VkSwapChainImageInfoWSI *swapChainImageInfo = (VkSwapChainImageInfoWSI *)pData; |
| 430 | for (int i=0; i<*pDataSize/sizeof(VkSwapChainImageInfoWSI); i++,swapChainImageInfo++) |
| 431 | { |
| 432 | // Create a mapping for an image to a device, image extent, and format |
| 433 | ImageMapStruct *imageMapElem = new ImageMapStruct; |
| 434 | imageMapElem->device = swapchainMap[swapChain]->device; |
| 435 | imageMapElem->imageExtent = swapchainMap[swapChain]->imageExtent; |
| 436 | imageMapElem->format = swapchainMap[swapChain]->format; |
| 437 | imageMap.insert(make_pair(swapChainImageInfo->image, imageMapElem)); |
| 438 | } |
| 439 | } |
| 440 | loader_platform_thread_unlock_mutex(&globalLock); |
| 441 | return result; |
| 442 | } |
| 443 | |
| 444 | VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentWSI(VkQueue queue, const VkPresentInfoWSI* pPresentInfo) |
| 445 | { |
| 446 | static int frameNumber = 0; |
| 447 | VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentWSI(queue, pPresentInfo); |
| 448 | |
| 449 | loader_platform_thread_lock_mutex(&globalLock); |
| 450 | |
| 451 | if (!screenshotEnvQueried) |
| 452 | { |
| 453 | const char *_vk_screenshot = getenv("_VK_SCREENSHOT"); |
| 454 | if (_vk_screenshot && *_vk_screenshot) |
| 455 | { |
| 456 | string spec(_vk_screenshot), word; |
| 457 | size_t start = 0, comma = 0; |
| 458 | |
| 459 | while (start < spec.size()) { |
| 460 | int frameToAdd; |
| 461 | comma = spec.find(',', start); |
| 462 | if (comma == string::npos) |
| 463 | word = string(spec, start); |
| 464 | else |
| 465 | word = string(spec, start, comma - start); |
| 466 | frameToAdd=atoi(word.c_str()); |
| 467 | // Add the frame number to list, but only do it if the word started with a digit and if |
| 468 | // it's not already in the list |
| 469 | if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' && |
| 470 | find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end()) |
| 471 | { |
| 472 | screenshotFrames.push_back(frameToAdd); |
| 473 | } |
| 474 | if (comma == string::npos) |
| 475 | break; |
| 476 | start = comma + 1; |
| 477 | } |
| 478 | } |
| 479 | screenshotEnvQueried = true; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | if (result == VK_SUCCESS && !screenshotFrames.empty()) |
| 484 | { |
| 485 | vector<int>::iterator it; |
| 486 | it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber); |
| 487 | if (it != screenshotFrames.end()) |
| 488 | { |
| 489 | string fileName; |
| 490 | fileName = to_string(frameNumber) + ".ppm"; |
| 491 | writePPM(fileName.c_str(), pPresentInfo->image); |
| 492 | screenshotFrames.erase(it); |
| 493 | |
| 494 | if (screenshotFrames.empty()) |
| 495 | { |
| 496 | // Free all our maps since we are done with them. |
| 497 | for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++) |
| 498 | { |
| 499 | SwapchainMapStruct *swapchainMapElem = it->second; |
| 500 | delete swapchainMapElem; |
| 501 | } |
| 502 | for (auto it = imageMap.begin(); it != imageMap.end(); it++) |
| 503 | { |
| 504 | ImageMapStruct *imageMapElem = it->second; |
| 505 | delete imageMapElem; |
| 506 | } |
| 507 | for (auto it = deviceMap.begin(); it != deviceMap.end(); it++) |
| 508 | { |
| 509 | DeviceMapStruct *deviceMapElem = it->second; |
| 510 | delete deviceMapElem; |
| 511 | } |
| 512 | swapchainMap.clear(); |
| 513 | imageMap.clear(); |
| 514 | deviceMap.clear(); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | frameNumber++; |
| 519 | loader_platform_thread_unlock_mutex(&globalLock); |
| 520 | return result; |
| 521 | } |
| 522 | |
| 523 | VK_LAYER_EXPORT void* VKAPI vkGetDeviceProcAddr( |
| 524 | VkDevice dev, |
| 525 | const char *funcName) |
| 526 | { |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 527 | if (dev == NULL) { |
| 528 | return NULL; |
| 529 | } |
| 530 | |
| 531 | /* loader uses this to force layer initialization; device object is wrapped */ |
| 532 | if (!strcmp(funcName, "vkGetDeviceProcAddr")) { |
| 533 | initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev); |
| 534 | return (void *) vkGetDeviceProcAddr; |
| 535 | } |
Courtney Goeltzenleuchter | ca173b8 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 536 | if (!strcmp(funcName, "vkCreateDevice")) |
| 537 | return (void*) vkCreateDevice; |
| 538 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 539 | if (!strcmp(funcName, "vkGetDeviceQueue")) |
| 540 | return (void*) vkGetDeviceQueue; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 541 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 542 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev); |
| 543 | if (deviceExtMap.size() == 0 || deviceExtMap[pDisp].wsi_lunarg_enabled) |
| 544 | { |
| 545 | if (!strcmp(funcName, "vkCreateSwapChainWSI")) |
| 546 | return (void*) vkCreateSwapChainWSI; |
| 547 | if (!strcmp(funcName, "vkGetSwapChainInfoWSI")) |
| 548 | return (void*) vkGetSwapChainInfoWSI; |
| 549 | if (!strcmp(funcName, "vkQueuePresentWSI")) |
| 550 | return (void*) vkQueuePresentWSI; |
| 551 | } |
| 552 | |
| 553 | if (pDisp->GetDeviceProcAddr == NULL) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 554 | return NULL; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 555 | return pDisp->GetDeviceProcAddr(dev, funcName); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 556 | } |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame^] | 557 | |
| 558 | |
| 559 | VK_LAYER_EXPORT void* VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName) |
| 560 | { |
| 561 | return NULL; |
| 562 | #if 0 |
| 563 | if (instance == VK_NULL_HANDLE) { |
| 564 | return NULL; |
| 565 | } |
| 566 | |
| 567 | /* loader uses this to force layer initialization; instance object is wrapped */ |
| 568 | if (!strcmp("vkGetInstanceProcAddr", funcName)) { |
| 569 | initInstanceTable((const VkBaseLayerObject *) instance); |
| 570 | return (void *) vkGetInstanceProcAddr; |
| 571 | } |
| 572 | |
| 573 | VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance); |
| 574 | if (pTable->GetInstanceProcAddr == NULL) |
| 575 | return NULL; |
| 576 | return pTable->GetInstanceProcAddr(instance, funcName); |
| 577 | #endif |
| 578 | } |