David Pinedo | eeca2a2 | 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 | |
| 40 | #include "loader_platform.h" |
| 41 | #include "vk_dispatch_table_helper.h" |
| 42 | #include "vk_struct_string_helper_cpp.h" |
| 43 | #include "layers_config.h" |
| 44 | // The following is #included again to catch certain OS-specific functions |
| 45 | // being used: |
| 46 | #include "loader_platform.h" |
| 47 | #include "layers_table.h" |
| 48 | |
| 49 | static device_table_map screenshot_device_table_map; |
| 50 | static instance_table_map screenshot_instance_table_map; |
| 51 | |
| 52 | static int globalLockInitialized = 0; |
| 53 | static loader_platform_thread_mutex globalLock; |
| 54 | |
| 55 | // unordered map, associates a swap chain with a device, image extent, and format |
| 56 | typedef struct |
| 57 | { |
| 58 | VkDevice device; |
| 59 | VkExtent2D imageExtent; |
| 60 | VkFormat format; |
| 61 | } SwapchainMapStruct; |
| 62 | static unordered_map<VkSwapChainWSI, SwapchainMapStruct *> swapchainMap; |
| 63 | |
| 64 | // unordered map, associates an image with a device, image extent, and format |
| 65 | typedef struct |
| 66 | { |
| 67 | VkDevice device; |
| 68 | VkExtent2D imageExtent; |
| 69 | VkFormat format; |
| 70 | } ImageMapStruct; |
| 71 | static unordered_map<VkImage, ImageMapStruct *> imageMap; |
| 72 | |
| 73 | // unordered map, associates a device with a queue |
| 74 | typedef struct |
| 75 | { |
| 76 | VkQueue queue; |
| 77 | uint32_t queueNodeIndex; |
| 78 | uint32_t queueIndex; |
| 79 | } DeviceMapStruct; |
| 80 | static unordered_map<VkDevice, DeviceMapStruct *> deviceMap; |
| 81 | |
| 82 | // List of frames to we will get a screenshot of |
| 83 | static vector<int> screenshotFrames; |
| 84 | |
| 85 | // Flag indicating we have queried _VK_SCREENSHOT env var |
| 86 | static bool screenshotEnvQueried = false; |
| 87 | |
| 88 | static void init_screenshot() |
| 89 | { |
| 90 | uint32_t report_flags = 0; |
| 91 | uint32_t debug_action = 0; |
| 92 | FILE *log_output = NULL; |
| 93 | const char *option_str; |
| 94 | |
| 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; |
| 122 | size_t data_size = sizeof(sr_layout); |
| 123 | const VkImageCreateInfo imgCreateInfo = { |
| 124 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 125 | NULL, |
| 126 | VK_IMAGE_TYPE_2D, |
| 127 | format, |
| 128 | {width, height, 1}, |
| 129 | 1, |
| 130 | 1, |
| 131 | 1, |
| 132 | VK_IMAGE_TILING_LINEAR, |
| 133 | (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT|VK_IMAGE_USAGE_STORAGE_BIT), |
| 134 | 0 |
| 135 | }; |
| 136 | VkMemoryAllocInfo memAllocInfo = { |
| 137 | VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
| 138 | NULL, |
| 139 | 0, // allocationSize, queried later |
| 140 | (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | |
| 141 | VK_MEMORY_PROPERTY_HOST_UNCACHED_BIT | |
| 142 | VK_MEMORY_PROPERTY_HOST_WRITE_COMBINED_BIT | |
| 143 | VK_MEMORY_PROPERTY_PREFER_HOST_LOCAL | |
| 144 | VK_MEMORY_PROPERTY_SHAREABLE_BIT) |
| 145 | }; |
| 146 | const VkCmdBufferCreateInfo createCommandBufferInfo = { |
| 147 | VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
| 148 | NULL, |
| 149 | deviceMap[device]->queueNodeIndex, |
| 150 | 0 |
| 151 | }; |
| 152 | const VkCmdBufferBeginInfo cmdBufferBeginInfo = { |
| 153 | VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
| 154 | NULL, |
| 155 | VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT | |
| 156 | VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
| 157 | }; |
| 158 | const VkImageCopy imageCopyRegion = { |
| 159 | {VK_IMAGE_ASPECT_COLOR, 0, 0}, |
| 160 | {0, 0, 0}, |
| 161 | {VK_IMAGE_ASPECT_COLOR, 0, 0}, |
| 162 | {0, 0, 0}, |
| 163 | {width, height, 1} |
| 164 | }; |
| 165 | VkMemoryRequirements memRequirements; |
| 166 | size_t memRequirementsSize = sizeof(memRequirements); |
| 167 | uint32_t num_allocations = 0; |
| 168 | size_t num_alloc_size = sizeof(num_allocations); |
| 169 | VkLayerDispatchTable* pTableDevice = screenshot_device_table_map[device]; |
| 170 | VkLayerDispatchTable* pTableQueue = screenshot_device_table_map[queue]; |
| 171 | VkLayerDispatchTable* pTableCmdBuffer; |
| 172 | |
| 173 | if (imageMap.empty() || imageMap.find(image1) == imageMap.end()) |
| 174 | return; |
| 175 | |
| 176 | // The VkImage image1 we are going to dump may not be mappable, |
| 177 | // and/or it may have a tiling mode of optimal rather than linear. |
| 178 | // To make sure we have an image that we can map and read linearly, we: |
| 179 | // create image2 that is mappable and linear |
| 180 | // copy image1 to image2 |
| 181 | // map image2 |
| 182 | // read from image2's mapped memeory. |
| 183 | |
| 184 | err = pTableDevice->CreateImage(device, &imgCreateInfo, &image2); |
| 185 | assert(!err); |
| 186 | |
| 187 | err = pTableDevice->GetObjectInfo(device, |
| 188 | VK_OBJECT_TYPE_IMAGE, image2, |
| 189 | VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 190 | &memRequirementsSize, &memRequirements); |
| 191 | assert(!err); |
| 192 | |
| 193 | memAllocInfo.allocationSize = memRequirements.size; |
| 194 | err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2); |
| 195 | assert(!err); |
| 196 | |
| 197 | err = pTableQueue->BindObjectMemory(device, VK_OBJECT_TYPE_IMAGE, image2, mem2, 0); |
| 198 | assert(!err); |
| 199 | |
| 200 | err = pTableDevice->CreateCommandBuffer(device, &createCommandBufferInfo, &cmdBuffer); |
| 201 | assert(!err); |
| 202 | |
| 203 | screenshot_device_table_map.emplace(cmdBuffer, pTableDevice); |
| 204 | pTableCmdBuffer = screenshot_device_table_map[cmdBuffer]; |
| 205 | |
| 206 | err = pTableCmdBuffer->BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo); |
| 207 | assert(!err); |
| 208 | |
| 209 | pTableCmdBuffer->CmdCopyImage(cmdBuffer, image1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 210 | image2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1, &imageCopyRegion); |
| 211 | |
| 212 | err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer); |
| 213 | assert(!err); |
| 214 | |
| 215 | err = pTableQueue->QueueSubmit(queue, 1, &cmdBuffer, VK_NULL_HANDLE); |
| 216 | assert(!err); |
| 217 | |
| 218 | err = pTableQueue->QueueWaitIdle(queue); |
| 219 | assert(!err); |
| 220 | |
| 221 | err = pTableDevice->DeviceWaitIdle(device); |
| 222 | assert(!err); |
| 223 | |
| 224 | err = pTableDevice->GetImageSubresourceInfo(device, image2, &sr, |
| 225 | VK_SUBRESOURCE_INFO_TYPE_LAYOUT, |
| 226 | &data_size, &sr_layout); |
| 227 | assert(!err); |
| 228 | |
| 229 | err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr ); |
| 230 | assert(!err); |
| 231 | |
| 232 | ptr += sr_layout.offset; |
| 233 | |
| 234 | ofstream file(filename, ios::binary); |
| 235 | |
| 236 | file << "P6\n"; |
| 237 | file << width << "\n"; |
| 238 | file << height << "\n"; |
| 239 | file << 255 << "\n"; |
| 240 | |
| 241 | for (y = 0; y < height; y++) { |
| 242 | const unsigned int *row = (const unsigned int*) ptr; |
| 243 | if (format == VK_FORMAT_B8G8R8A8_UNORM) |
| 244 | { |
| 245 | for (x = 0; x < width; x++) { |
| 246 | unsigned int swapped; |
| 247 | swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16; |
| 248 | file.write((char *)&swapped, 3); |
| 249 | row++; |
| 250 | } |
| 251 | } |
| 252 | else if (format == VK_FORMAT_R8G8B8A8_UNORM) |
| 253 | { |
| 254 | for (x = 0; x < width; x++) { |
| 255 | file.write((char *)row, 3); |
| 256 | row++; |
| 257 | } |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | // TODO: add support for addition formats |
| 262 | printf("Unrecognized image format\n"); |
| 263 | break; |
| 264 | } |
| 265 | ptr += sr_layout.rowPitch; |
| 266 | } |
| 267 | file.close(); |
| 268 | |
| 269 | // Clean up |
| 270 | err = pTableDevice->UnmapMemory(device, mem2); |
| 271 | assert(!err); |
| 272 | err = pTableDevice->FreeMemory(device, mem2); |
| 273 | assert(!err); |
| 274 | err = pTableDevice->DestroyObject(device, VK_OBJECT_TYPE_COMMAND_BUFFER, cmdBuffer); |
| 275 | assert(!err); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | VkResult VKAPI vkCreateInstance( |
| 280 | const VkInstanceCreateInfo* pCreateInfo, |
| 281 | VkInstance* pInstance) |
| 282 | { |
| 283 | VkLayerInstanceDispatchTable *pTable = get_dispatch_table(screenshot_instance_table_map, *pInstance); |
| 284 | VkResult result = pTable->CreateInstance(pCreateInfo, pInstance); |
| 285 | |
| 286 | if (result == VK_SUCCESS) { |
| 287 | init_screenshot(); |
| 288 | } |
| 289 | return result; |
| 290 | } |
| 291 | |
| 292 | VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice( |
| 293 | VkPhysicalDevice gpu, |
| 294 | const VkDeviceCreateInfo *pCreateInfo, |
| 295 | VkDevice *pDevice) |
| 296 | { |
| 297 | |
| 298 | VkLayerInstanceDispatchTable *pInstanceTable = get_dispatch_table(screenshot_instance_table_map, gpu); |
| 299 | VkResult result = pInstanceTable->CreateDevice(gpu, pCreateInfo, pDevice); |
| 300 | |
| 301 | if (screenshotEnvQueried && screenshotFrames.empty()) |
| 302 | // We are all done taking screenshots, so don't do anything else |
| 303 | return result; |
| 304 | |
| 305 | if (result == VK_SUCCESS) { |
| 306 | VkLayerDispatchTable *pTable = get_dispatch_table(screenshot_device_table_map, *pDevice); |
| 307 | screenshot_device_table_map.emplace(*pDevice, pTable); |
| 308 | } |
| 309 | |
| 310 | return result; |
| 311 | } |
| 312 | |
| 313 | struct extProps { |
| 314 | uint32_t version; |
| 315 | const char * const name; |
| 316 | }; |
| 317 | #define SCREENSHOT_LAYER_EXT_ARRAY_SIZE 1 |
| 318 | static const VkExtensionProperties mtExts[SCREENSHOT_LAYER_EXT_ARRAY_SIZE] = { |
| 319 | { |
| 320 | VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES, |
| 321 | "ScreenShot", |
| 322 | 0x10, |
| 323 | "Layer: ScreenShot", |
| 324 | }, |
| 325 | }; |
| 326 | |
| 327 | VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo( |
| 328 | VkExtensionInfoType infoType, |
| 329 | uint32_t extensionIndex, |
| 330 | size_t *pDataSize, |
| 331 | void *pData) |
| 332 | { |
| 333 | // This entrypoint is NOT going to init its own dispatch table since loader calls here early |
| 334 | uint32_t *count; |
| 335 | |
| 336 | if (pDataSize == NULL) { |
| 337 | return VK_ERROR_INVALID_POINTER; |
| 338 | } |
| 339 | |
| 340 | switch (infoType) { |
| 341 | case VK_EXTENSION_INFO_TYPE_COUNT: |
| 342 | *pDataSize = sizeof(uint32_t); |
| 343 | if (pData == NULL) { |
| 344 | return VK_SUCCESS; |
| 345 | } |
| 346 | count = (uint32_t *) pData; |
| 347 | *count = SCREENSHOT_LAYER_EXT_ARRAY_SIZE; |
| 348 | break; |
| 349 | case VK_EXTENSION_INFO_TYPE_PROPERTIES: |
| 350 | *pDataSize = sizeof(VkExtensionProperties); |
| 351 | if (pData == NULL) { |
| 352 | return VK_SUCCESS; |
| 353 | } |
| 354 | if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE) { |
| 355 | return VK_ERROR_INVALID_VALUE; |
| 356 | } |
| 357 | memcpy((VkExtensionProperties *) pData, &mtExts[extensionIndex], sizeof(VkExtensionProperties)); |
| 358 | break; |
| 359 | default: |
| 360 | return VK_ERROR_INVALID_VALUE; |
| 361 | }; |
| 362 | |
| 363 | return VK_SUCCESS; |
| 364 | } |
| 365 | |
| 366 | VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionInfo( |
| 367 | VkPhysicalDevice physical_device, |
| 368 | VkExtensionInfoType infoType, |
| 369 | uint32_t extensionIndex, |
| 370 | size_t *pDataSize, |
| 371 | void *pData) |
| 372 | { |
| 373 | uint32_t *count; |
| 374 | |
| 375 | if (pDataSize == NULL) { |
| 376 | return VK_ERROR_INVALID_POINTER; |
| 377 | } |
| 378 | |
| 379 | switch (infoType) { |
| 380 | case VK_EXTENSION_INFO_TYPE_COUNT: |
| 381 | *pDataSize = sizeof(uint32_t); |
| 382 | if (pData == NULL) { |
| 383 | return VK_SUCCESS; |
| 384 | } |
| 385 | count = (uint32_t *) pData; |
| 386 | *count = SCREENSHOT_LAYER_EXT_ARRAY_SIZE; |
| 387 | break; |
| 388 | case VK_EXTENSION_INFO_TYPE_PROPERTIES: |
| 389 | *pDataSize = sizeof(VkExtensionProperties); |
| 390 | if (pData == NULL) { |
| 391 | return VK_SUCCESS; |
| 392 | } |
| 393 | if (extensionIndex >= SCREENSHOT_LAYER_EXT_ARRAY_SIZE) { |
| 394 | return VK_ERROR_INVALID_VALUE; |
| 395 | } |
| 396 | memcpy((VkExtensionProperties *) pData, &mtExts[extensionIndex], sizeof(VkExtensionProperties)); |
| 397 | break; |
| 398 | default: |
| 399 | return VK_ERROR_INVALID_VALUE; |
| 400 | } |
| 401 | |
| 402 | return VK_SUCCESS; |
| 403 | } |
| 404 | |
| 405 | VK_LAYER_EXPORT VkResult VKAPI vkGetDeviceQueue( |
| 406 | VkDevice device, |
| 407 | uint32_t queueNodeIndex, |
| 408 | uint32_t queueIndex, |
| 409 | VkQueue *pQueue) |
| 410 | { |
| 411 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
| 412 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue); |
| 413 | |
| 414 | loader_platform_thread_lock_mutex(&globalLock); |
| 415 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 416 | // We are all done taking screenshots, so don't do anything else |
| 417 | loader_platform_thread_unlock_mutex(&globalLock); |
| 418 | return result; |
| 419 | } |
| 420 | |
| 421 | if (result == VK_SUCCESS) { |
| 422 | screenshot_device_table_map.emplace(*pQueue, pTable); |
| 423 | |
| 424 | // Create a mapping for the swapchain object into the dispatch table |
| 425 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 426 | deviceMapElem->queue = *pQueue; |
| 427 | deviceMapElem->queueNodeIndex = queueNodeIndex; |
| 428 | deviceMapElem->queueIndex = queueIndex; |
| 429 | deviceMap.insert(make_pair(device, deviceMapElem)); |
| 430 | } |
| 431 | loader_platform_thread_unlock_mutex(&globalLock); |
| 432 | return result; |
| 433 | } |
| 434 | |
| 435 | VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapChainWSI( |
| 436 | VkDevice device, |
| 437 | const VkSwapChainCreateInfoWSI *pCreateInfo, |
| 438 | VkSwapChainWSI *pSwapChain) |
| 439 | { |
| 440 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
| 441 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapChainWSI(device, pCreateInfo, pSwapChain); |
| 442 | |
| 443 | loader_platform_thread_lock_mutex(&globalLock); |
| 444 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 445 | // We are all done taking screenshots, so don't do anything else |
| 446 | loader_platform_thread_unlock_mutex(&globalLock); |
| 447 | return result; |
| 448 | } |
| 449 | |
| 450 | if (result == VK_SUCCESS) |
| 451 | { |
| 452 | // Create a mapping for a swapchain to a device, image extent, and format |
| 453 | SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct; |
| 454 | swapchainMapElem->device = device; |
| 455 | swapchainMapElem->imageExtent = pCreateInfo->imageExtent; |
| 456 | swapchainMapElem->format = pCreateInfo->imageFormat; |
| 457 | swapchainMap.insert(make_pair(*pSwapChain, swapchainMapElem)); |
| 458 | |
| 459 | // Create a mapping for the swapchain object into the dispatch table |
| 460 | screenshot_device_table_map.emplace(*pSwapChain, pTable); |
| 461 | } |
| 462 | loader_platform_thread_unlock_mutex(&globalLock); |
| 463 | |
| 464 | return result; |
| 465 | } |
| 466 | |
| 467 | VK_LAYER_EXPORT VkResult VKAPI vkGetSwapChainInfoWSI( |
| 468 | VkSwapChainWSI swapChain, |
| 469 | VkSwapChainInfoTypeWSI infoType, |
| 470 | size_t *pDataSize, |
| 471 | void *pData) |
| 472 | { |
| 473 | VkResult result = get_dispatch_table(screenshot_device_table_map, swapChain)->GetSwapChainInfoWSI(swapChain, infoType, pDataSize, pData); |
| 474 | |
| 475 | loader_platform_thread_lock_mutex(&globalLock); |
| 476 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 477 | // We are all done taking screenshots, so don't do anything else |
| 478 | loader_platform_thread_unlock_mutex(&globalLock); |
| 479 | return result; |
| 480 | } |
| 481 | |
| 482 | if (result == VK_SUCCESS && |
| 483 | !swapchainMap.empty() && swapchainMap.find(swapChain) != swapchainMap.end()) |
| 484 | { |
| 485 | VkSwapChainImageInfoWSI *swapChainImageInfo = (VkSwapChainImageInfoWSI *)pData; |
| 486 | for (int i=0; i<*pDataSize/sizeof(VkSwapChainImageInfoWSI); i++,swapChainImageInfo++) |
| 487 | { |
| 488 | // Create a mapping for an image to a device, image extent, and format |
| 489 | ImageMapStruct *imageMapElem = new ImageMapStruct; |
| 490 | imageMapElem->device = swapchainMap[swapChain]->device; |
| 491 | imageMapElem->imageExtent = swapchainMap[swapChain]->imageExtent; |
| 492 | imageMapElem->format = swapchainMap[swapChain]->format; |
| 493 | imageMap.insert(make_pair(swapChainImageInfo->image, imageMapElem)); |
| 494 | } |
| 495 | } |
| 496 | loader_platform_thread_unlock_mutex(&globalLock); |
| 497 | return result; |
| 498 | } |
| 499 | |
| 500 | VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentWSI(VkQueue queue, const VkPresentInfoWSI* pPresentInfo) |
| 501 | { |
| 502 | static int frameNumber = 0; |
| 503 | VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentWSI(queue, pPresentInfo); |
| 504 | |
| 505 | loader_platform_thread_lock_mutex(&globalLock); |
| 506 | |
| 507 | if (!screenshotEnvQueried) |
| 508 | { |
| 509 | const char *_vk_screenshot = getenv("_VK_SCREENSHOT"); |
| 510 | if (_vk_screenshot && *_vk_screenshot) |
| 511 | { |
| 512 | string spec(_vk_screenshot), word; |
| 513 | size_t start = 0, comma = 0; |
| 514 | |
| 515 | while (start < spec.size()) { |
| 516 | int frameToAdd; |
| 517 | comma = spec.find(',', start); |
| 518 | if (comma == string::npos) |
| 519 | word = string(spec, start); |
| 520 | else |
| 521 | word = string(spec, start, comma - start); |
| 522 | frameToAdd=atoi(word.c_str()); |
| 523 | // Add the frame number to list, but only do it if the word started with a digit and if |
| 524 | // it's not already in the list |
| 525 | if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' && |
| 526 | find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end()) |
| 527 | { |
| 528 | screenshotFrames.push_back(frameToAdd); |
| 529 | } |
| 530 | if (comma == string::npos) |
| 531 | break; |
| 532 | start = comma + 1; |
| 533 | } |
| 534 | } |
| 535 | screenshotEnvQueried = true; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | if (result == VK_SUCCESS && !screenshotFrames.empty()) |
| 540 | { |
| 541 | vector<int>::iterator it; |
| 542 | it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber); |
| 543 | if (it != screenshotFrames.end()) |
| 544 | { |
| 545 | string fileName; |
| 546 | fileName = to_string(frameNumber) + ".ppm"; |
| 547 | writePPM(fileName.c_str(), pPresentInfo->image); |
| 548 | screenshotFrames.erase(it); |
| 549 | |
| 550 | if (screenshotFrames.empty()) |
| 551 | { |
| 552 | // Free all our maps since we are done with them. |
| 553 | for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++) |
| 554 | { |
| 555 | SwapchainMapStruct *swapchainMapElem = it->second; |
| 556 | delete swapchainMapElem; |
| 557 | } |
| 558 | for (auto it = imageMap.begin(); it != imageMap.end(); it++) |
| 559 | { |
| 560 | ImageMapStruct *imageMapElem = it->second; |
| 561 | delete imageMapElem; |
| 562 | } |
| 563 | for (auto it = deviceMap.begin(); it != deviceMap.end(); it++) |
| 564 | { |
| 565 | DeviceMapStruct *deviceMapElem = it->second; |
| 566 | delete deviceMapElem; |
| 567 | } |
| 568 | swapchainMap.clear(); |
| 569 | imageMap.clear(); |
| 570 | deviceMap.clear(); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | frameNumber++; |
| 575 | loader_platform_thread_unlock_mutex(&globalLock); |
| 576 | return result; |
| 577 | } |
| 578 | |
| 579 | VK_LAYER_EXPORT void* VKAPI vkGetDeviceProcAddr( |
| 580 | VkDevice dev, |
| 581 | const char *funcName) |
| 582 | { |
| 583 | void *fptr; |
| 584 | |
| 585 | if (dev == NULL) { |
| 586 | return NULL; |
| 587 | } |
| 588 | |
| 589 | /* loader uses this to force layer initialization; device object is wrapped */ |
| 590 | if (!strcmp(funcName, "vkGetDeviceProcAddr")) { |
| 591 | initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev); |
| 592 | return (void *) vkGetDeviceProcAddr; |
| 593 | } |
| 594 | if (!strcmp(funcName, "vkGetDeviceQueue")) |
| 595 | return (void*) vkGetDeviceQueue; |
| 596 | if (!strcmp(funcName, "vkCreateSwapChainWSI")) |
| 597 | return (void*) vkCreateSwapChainWSI; |
| 598 | if (!strcmp(funcName, "vkGetSwapChainInfoWSI")) |
| 599 | return (void*) vkGetSwapChainInfoWSI; |
| 600 | if (!strcmp(funcName, "vkQueuePresentWSI")) |
| 601 | return (void*) vkQueuePresentWSI; |
| 602 | |
| 603 | if (get_dispatch_table(screenshot_device_table_map, dev)->GetDeviceProcAddr == NULL) |
| 604 | return NULL; |
| 605 | return get_dispatch_table(screenshot_device_table_map, dev)->GetDeviceProcAddr(dev, funcName); |
| 606 | } |
| 607 | |
| 608 | VK_LAYER_EXPORT void* VKAPI vkGetInstanceProcAddr( |
| 609 | VkInstance instance, |
| 610 | const char *funcName) |
| 611 | { |
| 612 | if (instance == NULL) { |
| 613 | return NULL; |
| 614 | } |
| 615 | |
| 616 | /* loader uses this to force layer initialization; instance object is wrapped */ |
| 617 | if (!strcmp(funcName, "vkGetInstanceProcAddr")) { |
| 618 | initInstanceTable(screenshot_instance_table_map, (const VkBaseLayerObject *) instance); |
| 619 | return (void *) vkGetInstanceProcAddr; |
| 620 | } |
| 621 | |
| 622 | if (!strcmp(funcName, "vkCreateInstance")) |
| 623 | return (void*) vkCreateInstance; |
| 624 | if (!strcmp(funcName, "vkCreateDevice")) |
| 625 | return (void*) vkCreateDevice; |
| 626 | |
| 627 | if (get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr == NULL) |
| 628 | return NULL; |
| 629 | return get_dispatch_table(screenshot_instance_table_map, instance)->GetInstanceProcAddr(instance, funcName); |
| 630 | } |