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 | |
Tobin Ehlis | 7a51d90 | 2015-07-03 10:34:49 -0600 | [diff] [blame] | 40 | #include "vk_loader_platform.h" |
David Pinedo | eeca2a2 | 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 | 56d204a | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 43 | #include "vk_layer_config.h" |
Tobin Ehlis | 56d204a | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 44 | #include "vk_layer_table.h" |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 45 | #include "vk_layer_extension_utils.h" |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 46 | |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 47 | |
| 48 | struct devExts { |
Ian Elliott | b134e84 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 49 | bool wsi_enabled; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 50 | }; |
| 51 | static std::unordered_map<void *, struct devExts> deviceExtMap; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 52 | static device_table_map screenshot_device_table_map; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 53 | |
| 54 | static int globalLockInitialized = 0; |
| 55 | static loader_platform_thread_mutex globalLock; |
| 56 | |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 57 | // unordered map: associates a swap chain with a device, image extent, format, and |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 58 | // list of images |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 59 | typedef struct |
| 60 | { |
| 61 | VkDevice device; |
| 62 | VkExtent2D imageExtent; |
| 63 | VkFormat format; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 64 | VkImage *imageList; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 65 | } SwapchainMapStruct; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 66 | static unordered_map<uint64_t, SwapchainMapStruct *> swapchainMap; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 67 | |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 68 | // unordered map: associates an image with a device, image extent, and format |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 69 | typedef struct |
| 70 | { |
| 71 | VkDevice device; |
| 72 | VkExtent2D imageExtent; |
| 73 | VkFormat format; |
| 74 | } ImageMapStruct; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 75 | static unordered_map<uint64_t, ImageMapStruct *> imageMap; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 76 | |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 77 | // unordered map: associates a device with a queue, cmdPool, and physical device |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 78 | typedef struct |
| 79 | { |
| 80 | VkQueue queue; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 81 | VkCmdPool cmdPool; |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 82 | VkPhysicalDevice physicalDevice; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 83 | } DeviceMapStruct; |
| 84 | static unordered_map<VkDevice, DeviceMapStruct *> deviceMap; |
| 85 | |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 86 | // unordered map: associates a physical device with an instance |
| 87 | typedef struct |
| 88 | { |
| 89 | VkInstance instance; |
| 90 | } PhysDeviceMapStruct; |
| 91 | static unordered_map<VkPhysicalDevice, PhysDeviceMapStruct *> physDeviceMap; |
| 92 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 93 | // List of frames to we will get a screenshot of |
| 94 | static vector<int> screenshotFrames; |
| 95 | |
| 96 | // Flag indicating we have queried _VK_SCREENSHOT env var |
| 97 | static bool screenshotEnvQueried = false; |
| 98 | |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 99 | static VkResult memory_type_from_properties( |
| 100 | VkPhysicalDeviceMemoryProperties *memory_properties, |
| 101 | uint32_t typeBits, |
Tony Barbour | f1eceb9 | 2015-10-15 12:42:56 -0600 | [diff] [blame] | 102 | VkFlags requirements_mask, |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 103 | 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 Barbour | f1eceb9 | 2015-10-15 12:42:56 -0600 | [diff] [blame] | 109 | if ((memory_properties->memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) { |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 110 | *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 Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 120 | static void init_screenshot() |
| 121 | { |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 122 | 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 | |
| 134 | static 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 Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 142 | VkDevice device = imageMap[image1.handle]->device; |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 143 | VkPhysicalDevice physicalDevice = deviceMap[device]->physicalDevice; |
| 144 | VkInstance instance = physDeviceMap[physicalDevice]->instance; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 145 | VkQueue queue = deviceMap[device]->queue; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 146 | int width = imageMap[image1.handle]->imageExtent.width; |
| 147 | int height = imageMap[image1.handle]->imageExtent.height; |
| 148 | VkFormat format = imageMap[image1.handle]->format; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 149 | const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR, 0, 0}; |
| 150 | VkSubresourceLayout sr_layout; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 151 | 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 Northrop | 46d10d0 | 2015-09-01 11:47:50 -0600 | [diff] [blame] | 168 | 0 // memoryTypeIndex, queried later |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 169 | }; |
| 170 | const VkCmdBufferCreateInfo createCommandBufferInfo = { |
| 171 | VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
| 172 | NULL, |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 173 | deviceMap[device]->cmdPool, |
Chia-I Wu | 88eaa3b | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 174 | VK_CMD_BUFFER_LEVEL_PRIMARY, |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 175 | 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 Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 191 | uint32_t num_allocations = 0; |
| 192 | size_t num_alloc_size = sizeof(num_allocations); |
David Pinedo | c325666 | 2015-06-30 13:08:37 -0600 | [diff] [blame] | 193 | VkLayerDispatchTable* pTableDevice = get_dispatch_table(screenshot_device_table_map, device); |
| 194 | VkLayerDispatchTable* pTableQueue = get_dispatch_table(screenshot_device_table_map, queue); |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 195 | VkLayerInstanceDispatchTable* pInstanceTable; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 196 | VkLayerDispatchTable* pTableCmdBuffer; |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 197 | VkPhysicalDeviceMemoryProperties memory_properties; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 198 | |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 199 | if (imageMap.empty() || imageMap.find(image1.handle) == imageMap.end()) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 200 | 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 Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 213 | pTableDevice->GetImageMemoryRequirements(device, image2, &memRequirements); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 214 | |
| 215 | memAllocInfo.allocationSize = memRequirements.size; |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 216 | pInstanceTable = instance_dispatch_table(instance); |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 217 | pInstanceTable->GetPhysicalDeviceMemoryProperties(physicalDevice, &memory_properties); |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 218 | |
| 219 | err = memory_type_from_properties(&memory_properties, |
| 220 | memRequirements.memoryTypeBits, |
Cody Northrop | 46d10d0 | 2015-09-01 11:47:50 -0600 | [diff] [blame] | 221 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 222 | &memAllocInfo.memoryTypeIndex); |
| 223 | assert(!err); |
| 224 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 225 | err = pTableDevice->AllocMemory(device, &memAllocInfo, &mem2); |
| 226 | assert(!err); |
| 227 | |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 228 | err = pTableQueue->BindImageMemory(device, image2, mem2, 0); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 229 | 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 Northrop | 46d10d0 | 2015-09-01 11:47:50 -0600 | [diff] [blame] | 240 | // 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 Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 243 | |
| 244 | err = pTableCmdBuffer->EndCommandBuffer(cmdBuffer); |
| 245 | assert(!err); |
| 246 | |
| 247 | err = pTableQueue->QueueSubmit(queue, 1, &cmdBuffer, VK_NULL_HANDLE); |
| 248 | assert(!err); |
| 249 | |
| 250 | err = pTableQueue->QueueWaitIdle(queue); |
| 251 | assert(!err); |
| 252 | |
| 253 | err = pTableDevice->DeviceWaitIdle(device); |
| 254 | assert(!err); |
| 255 | |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 256 | pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 257 | |
| 258 | err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr ); |
| 259 | assert(!err); |
| 260 | |
| 261 | ptr += sr_layout.offset; |
| 262 | |
| 263 | ofstream file(filename, ios::binary); |
| 264 | |
| 265 | file << "P6\n"; |
| 266 | file << width << "\n"; |
| 267 | file << height << "\n"; |
| 268 | file << 255 << "\n"; |
| 269 | |
| 270 | for (y = 0; y < height; y++) { |
| 271 | const unsigned int *row = (const unsigned int*) ptr; |
| 272 | if (format == VK_FORMAT_B8G8R8A8_UNORM) |
| 273 | { |
| 274 | for (x = 0; x < width; x++) { |
| 275 | unsigned int swapped; |
| 276 | swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16; |
| 277 | file.write((char *)&swapped, 3); |
| 278 | row++; |
| 279 | } |
| 280 | } |
| 281 | else if (format == VK_FORMAT_R8G8B8A8_UNORM) |
| 282 | { |
| 283 | for (x = 0; x < width; x++) { |
| 284 | file.write((char *)row, 3); |
| 285 | row++; |
| 286 | } |
| 287 | } |
| 288 | else |
| 289 | { |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 290 | // TODO: add support for additional formats |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 291 | printf("Unrecognized image format\n"); |
| 292 | break; |
| 293 | } |
| 294 | ptr += sr_layout.rowPitch; |
| 295 | } |
| 296 | file.close(); |
| 297 | |
| 298 | // Clean up |
Mark Lobodzinski | 67b42b7 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 299 | pTableDevice->UnmapMemory(device, mem2); |
| 300 | pTableDevice->FreeMemory(device, mem2); |
| 301 | pTableDevice->DestroyCommandBuffer(device, cmdBuffer); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 305 | static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device) |
| 306 | { |
Cody Northrop | 1684adb | 2015-08-05 11:15:02 -0600 | [diff] [blame] | 307 | uint32_t i; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 308 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device); |
Jon Ashburn | 83334db | 2015-09-16 18:08:32 -0600 | [diff] [blame] | 309 | PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr; |
| 310 | pDisp->GetSurfacePropertiesKHR = (PFN_vkGetSurfacePropertiesKHR) gpa(device, "vkGetSurfacePropertiesKHR"); |
| 311 | pDisp->GetSurfaceFormatsKHR = (PFN_vkGetSurfaceFormatsKHR) gpa(device, "vkGetSurfaceFormatsKHR"); |
| 312 | pDisp->GetSurfacePresentModesKHR = (PFN_vkGetSurfacePresentModesKHR) gpa(device, "vkGetSurfacePresentModesKHR"); |
| 313 | pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR) gpa(device, "vkCreateSwapchainKHR"); |
| 314 | pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR) gpa(device, "vkDestroySwapchainKHR"); |
| 315 | pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR) gpa(device, "vkGetSwapchainImagesKHR"); |
| 316 | pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR) gpa(device, "vkAcquireNextImageKHR"); |
| 317 | pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR) gpa(device, "vkQueuePresentKHR"); |
Ian Elliott | b134e84 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 318 | deviceExtMap[pDisp].wsi_enabled = false; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 319 | for (i = 0; i < pCreateInfo->extensionCount; i++) { |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 320 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_EXT_KHR_DEVICE_SWAPCHAIN_EXTENSION_NAME) == 0) |
Ian Elliott | b134e84 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 321 | deviceExtMap[pDisp].wsi_enabled = true; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 325 | VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice( |
| 326 | VkPhysicalDevice gpu, |
| 327 | const VkDeviceCreateInfo *pCreateInfo, |
| 328 | VkDevice *pDevice) |
| 329 | { |
Courtney Goeltzenleuchter | be63799 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 330 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice); |
| 331 | VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pDevice); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 332 | |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 333 | if (result == VK_SUCCESS) { |
Jon Ashburn | 59f3dfc | 2015-07-06 15:09:36 -0600 | [diff] [blame] | 334 | init_screenshot(); |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 335 | createDeviceRegisterExtensions(pCreateInfo, *pDevice); |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 336 | // Create a mapping from a device to a physicalDevice |
| 337 | if (deviceMap[*pDevice] == NULL) |
| 338 | { |
| 339 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 340 | deviceMap[*pDevice] = deviceMapElem; |
| 341 | } |
| 342 | deviceMap[*pDevice]->physicalDevice = gpu; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 343 | } |
| 344 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 345 | return result; |
| 346 | } |
| 347 | |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 348 | VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices( |
| 349 | VkInstance instance, |
| 350 | uint32_t* pPhysicalDeviceCount, |
| 351 | VkPhysicalDevice* pPhysicalDevices) |
| 352 | { |
| 353 | VkResult result; |
| 354 | |
| 355 | VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance); |
| 356 | result = pTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices); |
Tobin Ehlis | d8dd86b | 2015-09-08 08:59:48 -0600 | [diff] [blame] | 357 | if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0 && pPhysicalDevices) |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 358 | { |
| 359 | for (uint32_t i=0; i<*pPhysicalDeviceCount ; i++) |
| 360 | { |
| 361 | // Create a mapping from a physicalDevice to an instance |
| 362 | if (physDeviceMap[pPhysicalDevices[i]] == NULL) |
| 363 | { |
| 364 | PhysDeviceMapStruct *physDeviceMapElem = new PhysDeviceMapStruct; |
| 365 | physDeviceMap[pPhysicalDevices[i]] = physDeviceMapElem; |
| 366 | } |
| 367 | physDeviceMap[pPhysicalDevices[i]]->instance = instance; |
| 368 | } |
| 369 | } |
| 370 | return result; |
| 371 | } |
| 372 | |
Courtney Goeltzenleuchter | be63799 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 373 | /* TODO: Probably need a DestroyDevice as well */ |
| 374 | |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 375 | static const VkLayerProperties ss_device_layers[] = { |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 376 | { |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 377 | "ScreenShot", |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 378 | VK_API_VERSION, |
| 379 | VK_MAKE_VERSION(0, 1, 0), |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 380 | "Layer: ScreenShot", |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 381 | } |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 382 | }; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 383 | |
Courtney Goeltzenleuchter | 74c4ce9 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 384 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties( |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 385 | const char *pLayerName, |
| 386 | uint32_t *pCount, |
| 387 | VkExtensionProperties* pProperties) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 388 | { |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 389 | /* ScreenShot does not have any global extensions */ |
| 390 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 391 | } |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 392 | |
Courtney Goeltzenleuchter | 74c4ce9 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 393 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties( |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 394 | uint32_t *pCount, |
| 395 | VkLayerProperties* pProperties) |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 396 | { |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 397 | /* ScreenShot does not have any global layers */ |
| 398 | return util_GetLayerProperties(0, NULL, |
| 399 | pCount, pProperties); |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 400 | } |
| 401 | |
Courtney Goeltzenleuchter | 74c4ce9 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 402 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties( |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 403 | VkPhysicalDevice physicalDevice, |
| 404 | const char* pLayerName, |
| 405 | uint32_t* pCount, |
| 406 | VkExtensionProperties* pProperties) |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 407 | { |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 408 | /* ScreenShot does not have any physical device extensions */ |
| 409 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
| 410 | } |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 411 | |
Courtney Goeltzenleuchter | 74c4ce9 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 412 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties( |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 413 | VkPhysicalDevice physicalDevice, |
| 414 | uint32_t* pCount, |
| 415 | VkLayerProperties* pProperties) |
| 416 | { |
| 417 | return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers), |
| 418 | ss_device_layers, |
| 419 | pCount, pProperties); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 420 | } |
| 421 | |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 422 | VK_LAYER_EXPORT void VKAPI vkGetDeviceQueue( |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 423 | VkDevice device, |
| 424 | uint32_t queueNodeIndex, |
| 425 | uint32_t queueIndex, |
| 426 | VkQueue *pQueue) |
| 427 | { |
| 428 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 429 | get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 430 | |
| 431 | loader_platform_thread_lock_mutex(&globalLock); |
| 432 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 433 | // We are all done taking screenshots, so don't do anything else |
| 434 | loader_platform_thread_unlock_mutex(&globalLock); |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 435 | return; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 436 | } |
| 437 | |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 438 | screenshot_device_table_map.emplace(*pQueue, pTable); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 439 | |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 440 | // Create a mapping from a device to a queue |
| 441 | if (deviceMap[device] == NULL) |
| 442 | { |
| 443 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 444 | deviceMap[device] = deviceMapElem; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 445 | } |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 446 | deviceMap[device]->queue = *pQueue; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 447 | loader_platform_thread_unlock_mutex(&globalLock); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 448 | } |
| 449 | |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 450 | VK_LAYER_EXPORT VkResult VKAPI vkCreateCommandPool( |
| 451 | VkDevice device, |
| 452 | const VkCmdPoolCreateInfo *pCreateInfo, |
| 453 | VkCmdPool *pCmdPool) |
| 454 | { |
| 455 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
| 456 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateCommandPool(device, pCreateInfo, pCmdPool); |
| 457 | |
| 458 | loader_platform_thread_lock_mutex(&globalLock); |
| 459 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 460 | // We are all done taking screenshots, so don't do anything else |
| 461 | loader_platform_thread_unlock_mutex(&globalLock); |
| 462 | return result; |
| 463 | } |
| 464 | |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 465 | // Create a mapping from a device to a cmdPool |
| 466 | if (deviceMap[device] == NULL) |
| 467 | { |
| 468 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 469 | deviceMap[device] = deviceMapElem; |
| 470 | } |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 471 | deviceMap[device]->cmdPool = *pCmdPool; |
| 472 | loader_platform_thread_unlock_mutex(&globalLock); |
| 473 | return result; |
| 474 | } |
| 475 | |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 476 | VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapchainKHR( |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 477 | VkDevice device, |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 478 | const VkSwapchainCreateInfoKHR *pCreateInfo, |
| 479 | VkSwapchainKHR *pSwapchain) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 480 | { |
| 481 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 482 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapchainKHR(device, pCreateInfo, pSwapchain); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 483 | |
| 484 | loader_platform_thread_lock_mutex(&globalLock); |
| 485 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 486 | // We are all done taking screenshots, so don't do anything else |
| 487 | loader_platform_thread_unlock_mutex(&globalLock); |
| 488 | return result; |
| 489 | } |
| 490 | |
| 491 | if (result == VK_SUCCESS) |
| 492 | { |
| 493 | // Create a mapping for a swapchain to a device, image extent, and format |
| 494 | SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct; |
| 495 | swapchainMapElem->device = device; |
| 496 | swapchainMapElem->imageExtent = pCreateInfo->imageExtent; |
| 497 | swapchainMapElem->format = pCreateInfo->imageFormat; |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 498 | swapchainMap.insert(make_pair(pSwapchain->handle, swapchainMapElem)); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 499 | |
| 500 | // Create a mapping for the swapchain object into the dispatch table |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 501 | screenshot_device_table_map.emplace((void *)pSwapchain->handle, pTable); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 502 | } |
| 503 | loader_platform_thread_unlock_mutex(&globalLock); |
| 504 | |
| 505 | return result; |
| 506 | } |
| 507 | |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 508 | VK_LAYER_EXPORT VkResult VKAPI vkGetSwapchainImagesKHR( |
Ian Elliott | b134e84 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 509 | VkDevice device, |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 510 | VkSwapchainKHR swapchain, |
Ian Elliott | ccac023 | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 511 | uint32_t* pCount, |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 512 | VkImage* pSwapchainImages) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 513 | { |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 514 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapchainImagesKHR(device, swapchain, pCount, pSwapchainImages); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 515 | |
| 516 | loader_platform_thread_lock_mutex(&globalLock); |
| 517 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 518 | // We are all done taking screenshots, so don't do anything else |
| 519 | loader_platform_thread_unlock_mutex(&globalLock); |
| 520 | return result; |
| 521 | } |
| 522 | |
| 523 | if (result == VK_SUCCESS && |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 524 | pSwapchainImages && |
| 525 | !swapchainMap.empty() && swapchainMap.find(swapchain.handle) != swapchainMap.end()) |
Ian Elliott | ccac023 | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 526 | { |
Cody Northrop | 46d92c0 | 2015-09-01 11:48:13 -0600 | [diff] [blame] | 527 | unsigned i; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 528 | |
Ian Elliott | ccac023 | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 529 | for (i=0; i<*pCount; i++) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 530 | { |
| 531 | // Create a mapping for an image to a device, image extent, and format |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 532 | if (imageMap[pSwapchainImages[i].handle] == NULL) |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 533 | { |
| 534 | ImageMapStruct *imageMapElem = new ImageMapStruct; |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 535 | imageMap[pSwapchainImages[i].handle] = imageMapElem; |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 536 | } |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 537 | imageMap[pSwapchainImages[i].handle]->device = swapchainMap[swapchain.handle]->device; |
| 538 | imageMap[pSwapchainImages[i].handle]->imageExtent = swapchainMap[swapchain.handle]->imageExtent; |
| 539 | imageMap[pSwapchainImages[i].handle]->format = swapchainMap[swapchain.handle]->format; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 540 | } |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 541 | |
| 542 | // Add list of images to swapchain to image map |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 543 | SwapchainMapStruct *swapchainMapElem = swapchainMap[swapchain.handle]; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 544 | if (i >= 1 && swapchainMapElem) |
| 545 | { |
| 546 | VkImage *imageList = new VkImage[i]; |
| 547 | swapchainMapElem->imageList = imageList; |
Cody Northrop | 46d92c0 | 2015-09-01 11:48:13 -0600 | [diff] [blame] | 548 | for (unsigned j=0; j<i; j++) |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 549 | { |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 550 | swapchainMapElem->imageList[j] = pSwapchainImages[j].handle; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 554 | } |
| 555 | loader_platform_thread_unlock_mutex(&globalLock); |
| 556 | return result; |
| 557 | } |
| 558 | |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 559 | VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentKHR(VkQueue queue, VkPresentInfoKHR* pPresentInfo) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 560 | { |
| 561 | static int frameNumber = 0; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 562 | if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ } |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 563 | VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentKHR(queue, pPresentInfo); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 564 | |
| 565 | loader_platform_thread_lock_mutex(&globalLock); |
| 566 | |
| 567 | if (!screenshotEnvQueried) |
| 568 | { |
| 569 | const char *_vk_screenshot = getenv("_VK_SCREENSHOT"); |
| 570 | if (_vk_screenshot && *_vk_screenshot) |
| 571 | { |
| 572 | string spec(_vk_screenshot), word; |
| 573 | size_t start = 0, comma = 0; |
| 574 | |
| 575 | while (start < spec.size()) { |
| 576 | int frameToAdd; |
| 577 | comma = spec.find(',', start); |
| 578 | if (comma == string::npos) |
| 579 | word = string(spec, start); |
| 580 | else |
| 581 | word = string(spec, start, comma - start); |
| 582 | frameToAdd=atoi(word.c_str()); |
| 583 | // Add the frame number to list, but only do it if the word started with a digit and if |
| 584 | // it's not already in the list |
| 585 | if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' && |
| 586 | find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end()) |
| 587 | { |
| 588 | screenshotFrames.push_back(frameToAdd); |
| 589 | } |
| 590 | if (comma == string::npos) |
| 591 | break; |
| 592 | start = comma + 1; |
| 593 | } |
| 594 | } |
| 595 | screenshotEnvQueried = true; |
| 596 | } |
| 597 | |
| 598 | |
| 599 | if (result == VK_SUCCESS && !screenshotFrames.empty()) |
| 600 | { |
| 601 | vector<int>::iterator it; |
| 602 | it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber); |
| 603 | if (it != screenshotFrames.end()) |
| 604 | { |
| 605 | string fileName; |
| 606 | fileName = to_string(frameNumber) + ".ppm"; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 607 | |
| 608 | VkImage image; |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 609 | uint64_t swapchain; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 610 | // We'll dump only one image: the first |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 611 | swapchain = pPresentInfo->swapchains[0].handle; |
| 612 | image = swapchainMap[swapchain]->imageList[pPresentInfo->imageIndices[0]]; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 613 | writePPM(fileName.c_str(), image); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 614 | screenshotFrames.erase(it); |
| 615 | |
| 616 | if (screenshotFrames.empty()) |
| 617 | { |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 618 | // Free all our maps since we are done with them. |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 619 | for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++) |
| 620 | { |
| 621 | SwapchainMapStruct *swapchainMapElem = it->second; |
| 622 | delete swapchainMapElem; |
| 623 | } |
| 624 | for (auto it = imageMap.begin(); it != imageMap.end(); it++) |
| 625 | { |
| 626 | ImageMapStruct *imageMapElem = it->second; |
| 627 | delete imageMapElem; |
| 628 | } |
| 629 | for (auto it = deviceMap.begin(); it != deviceMap.end(); it++) |
| 630 | { |
| 631 | DeviceMapStruct *deviceMapElem = it->second; |
| 632 | delete deviceMapElem; |
| 633 | } |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 634 | for (auto it = physDeviceMap.begin(); it != physDeviceMap.end(); it++) |
| 635 | { |
| 636 | PhysDeviceMapStruct *physDeviceMapElem = it->second; |
| 637 | delete physDeviceMapElem; |
| 638 | } |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 639 | swapchainMap.clear(); |
| 640 | imageMap.clear(); |
| 641 | deviceMap.clear(); |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 642 | physDeviceMap.clear(); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | } |
| 646 | frameNumber++; |
| 647 | loader_platform_thread_unlock_mutex(&globalLock); |
| 648 | return result; |
| 649 | } |
| 650 | |
Courtney Goeltzenleuchter | a4c8c71 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 651 | VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr( |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 652 | VkDevice dev, |
| 653 | const char *funcName) |
| 654 | { |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 655 | if (dev == NULL) { |
| 656 | return NULL; |
| 657 | } |
| 658 | |
| 659 | /* loader uses this to force layer initialization; device object is wrapped */ |
| 660 | if (!strcmp(funcName, "vkGetDeviceProcAddr")) { |
| 661 | initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev); |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 662 | return (PFN_vkVoidFunction)vkGetDeviceProcAddr; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 663 | } |
Courtney Goeltzenleuchter | be63799 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 664 | if (!strcmp(funcName, "vkCreateDevice")) |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 665 | return (PFN_vkVoidFunction) vkCreateDevice; |
Courtney Goeltzenleuchter | be63799 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 666 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 667 | if (!strcmp(funcName, "vkGetDeviceQueue")) |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 668 | return (PFN_vkVoidFunction) vkGetDeviceQueue; |
| 669 | |
| 670 | if (!strcmp(funcName, "vkCreateCommandPool")) |
| 671 | return (PFN_vkVoidFunction) vkCreateCommandPool; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 672 | |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 673 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev); |
Jon Ashburn | 83334db | 2015-09-16 18:08:32 -0600 | [diff] [blame] | 674 | if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].wsi_enabled) |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 675 | { |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 676 | if (!strcmp(funcName, "vkCreateSwapchainKHR")) |
| 677 | return (PFN_vkVoidFunction) vkCreateSwapchainKHR; |
| 678 | if (!strcmp(funcName, "vkGetSwapchainImagesKHR")) |
| 679 | return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR; |
| 680 | if (!strcmp(funcName, "vkQueuePresentKHR")) |
| 681 | return (PFN_vkVoidFunction) vkQueuePresentKHR; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | if (pDisp->GetDeviceProcAddr == NULL) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 685 | return NULL; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 686 | return pDisp->GetDeviceProcAddr(dev, funcName); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 687 | } |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 688 | |
| 689 | |
Courtney Goeltzenleuchter | a4c8c71 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 690 | VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName) |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 691 | { |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 692 | if (instance == VK_NULL_HANDLE) { |
| 693 | return NULL; |
| 694 | } |
| 695 | |
| 696 | /* loader uses this to force layer initialization; instance object is wrapped */ |
| 697 | if (!strcmp("vkGetInstanceProcAddr", funcName)) { |
| 698 | initInstanceTable((const VkBaseLayerObject *) instance); |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 699 | return (PFN_vkVoidFunction) vkGetInstanceProcAddr; |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 700 | } |
| 701 | |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 702 | if (!strcmp(funcName, "vkEnumeratePhysicalDevices")) |
| 703 | return (PFN_vkVoidFunction)vkEnumeratePhysicalDevices; |
| 704 | |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 705 | VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance); |
| 706 | if (pTable->GetInstanceProcAddr == NULL) |
| 707 | return NULL; |
| 708 | return pTable->GetInstanceProcAddr(instance, funcName); |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 709 | } |