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 | |
Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 99 | static bool memory_type_from_properties( |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 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; |
Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 111 | return true; |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | typeBits >>= 1; |
| 115 | } |
| 116 | // No memory types matched, return failure |
Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 117 | return false; |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 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; |
Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 138 | bool pass; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 139 | int x, y; |
| 140 | const char *ptr; |
| 141 | VkDeviceMemory mem2; |
| 142 | VkCmdBuffer cmdBuffer; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 143 | VkDevice device = imageMap[image1.handle]->device; |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 144 | VkPhysicalDevice physicalDevice = deviceMap[device]->physicalDevice; |
| 145 | VkInstance instance = physDeviceMap[physicalDevice]->instance; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 146 | VkQueue queue = deviceMap[device]->queue; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 147 | int width = imageMap[image1.handle]->imageExtent.width; |
| 148 | int height = imageMap[image1.handle]->imageExtent.height; |
| 149 | VkFormat format = imageMap[image1.handle]->format; |
Courtney Goeltzenleuchter | ba11ebe | 2015-10-21 17:00:51 -0600 | [diff] [blame] | 150 | const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0}; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 151 | VkSubresourceLayout sr_layout; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 152 | const VkImageCreateInfo imgCreateInfo = { |
| 153 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 154 | NULL, |
| 155 | VK_IMAGE_TYPE_2D, |
| 156 | format, |
| 157 | {width, height, 1}, |
| 158 | 1, |
| 159 | 1, |
| 160 | 1, |
| 161 | VK_IMAGE_TILING_LINEAR, |
| 162 | (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT|VK_IMAGE_USAGE_STORAGE_BIT), |
| 163 | 0 |
| 164 | }; |
| 165 | VkMemoryAllocInfo memAllocInfo = { |
| 166 | VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
| 167 | NULL, |
| 168 | 0, // allocationSize, queried later |
Cody Northrop | 46d10d0 | 2015-09-01 11:47:50 -0600 | [diff] [blame] | 169 | 0 // memoryTypeIndex, queried later |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 170 | }; |
Courtney Goeltzenleuchter | 831c183 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 171 | const VkCmdBufferAllocInfo allocCommandBufferInfo = { |
| 172 | VK_STRUCTURE_TYPE_CMD_BUFFER_ALLOC_INFO, |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 173 | NULL, |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 174 | deviceMap[device]->cmdPool, |
Chia-I Wu | 88eaa3b | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 175 | VK_CMD_BUFFER_LEVEL_PRIMARY, |
Courtney Goeltzenleuchter | 831c183 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 176 | 1 |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 177 | }; |
| 178 | const VkCmdBufferBeginInfo cmdBufferBeginInfo = { |
| 179 | VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
| 180 | NULL, |
Courtney Goeltzenleuchter | a32436b | 2015-10-21 18:11:04 -0600 | [diff] [blame] | 181 | VK_CMD_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 182 | }; |
| 183 | const VkImageCopy imageCopyRegion = { |
Courtney Goeltzenleuchter | ba11ebe | 2015-10-21 17:00:51 -0600 | [diff] [blame] | 184 | {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0}, |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 185 | {0, 0, 0}, |
Courtney Goeltzenleuchter | ba11ebe | 2015-10-21 17:00:51 -0600 | [diff] [blame] | 186 | {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0}, |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 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 | |
Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 219 | pass = memory_type_from_properties(&memory_properties, |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 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); |
Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 223 | assert(pass); |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 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 | |
Courtney Goeltzenleuchter | 831c183 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 231 | err = pTableDevice->AllocCommandBuffers(device, &allocCommandBufferInfo, &cmdBuffer); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 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 | |
Courtney Goeltzenleuchter | 3ec3162 | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 247 | VkFence nullFence = { VK_NULL_HANDLE }; |
Courtney Goeltzenleuchter | 1a748e9 | 2015-10-27 11:22:14 -0600 | [diff] [blame] | 248 | VkSubmitInfo submit_info; |
| 249 | submit_info.waitSemCount = 0; |
| 250 | submit_info.pWaitSemaphores = NULL; |
| 251 | submit_info.cmdBufferCount = 1; |
| 252 | submit_info.pCommandBuffers = &cmdBuffer; |
| 253 | submit_info.signalSemCount = 0; |
| 254 | submit_info.pSignalSemaphores = NULL; |
Courtney Goeltzenleuchter | 3ec3162 | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 255 | |
| 256 | err = pTableQueue->QueueSubmit(queue, 1, &submit_info, nullFence); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 257 | assert(!err); |
| 258 | |
| 259 | err = pTableQueue->QueueWaitIdle(queue); |
| 260 | assert(!err); |
| 261 | |
| 262 | err = pTableDevice->DeviceWaitIdle(device); |
| 263 | assert(!err); |
| 264 | |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 265 | pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 266 | |
| 267 | err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr ); |
| 268 | assert(!err); |
| 269 | |
| 270 | ptr += sr_layout.offset; |
| 271 | |
| 272 | ofstream file(filename, ios::binary); |
| 273 | |
| 274 | file << "P6\n"; |
| 275 | file << width << "\n"; |
| 276 | file << height << "\n"; |
| 277 | file << 255 << "\n"; |
| 278 | |
| 279 | for (y = 0; y < height; y++) { |
| 280 | const unsigned int *row = (const unsigned int*) ptr; |
| 281 | if (format == VK_FORMAT_B8G8R8A8_UNORM) |
| 282 | { |
| 283 | for (x = 0; x < width; x++) { |
| 284 | unsigned int swapped; |
| 285 | swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16; |
| 286 | file.write((char *)&swapped, 3); |
| 287 | row++; |
| 288 | } |
| 289 | } |
| 290 | else if (format == VK_FORMAT_R8G8B8A8_UNORM) |
| 291 | { |
| 292 | for (x = 0; x < width; x++) { |
| 293 | file.write((char *)row, 3); |
| 294 | row++; |
| 295 | } |
| 296 | } |
| 297 | else |
| 298 | { |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 299 | // TODO: add support for additional formats |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 300 | printf("Unrecognized image format\n"); |
| 301 | break; |
| 302 | } |
| 303 | ptr += sr_layout.rowPitch; |
| 304 | } |
| 305 | file.close(); |
| 306 | |
| 307 | // Clean up |
Mark Lobodzinski | 67b42b7 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 308 | pTableDevice->UnmapMemory(device, mem2); |
| 309 | pTableDevice->FreeMemory(device, mem2); |
Courtney Goeltzenleuchter | 831c183 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 310 | pTableDevice->FreeCommandBuffers(device, deviceMap[device]->cmdPool, 1, &cmdBuffer); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 314 | static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device) |
| 315 | { |
Cody Northrop | 1684adb | 2015-08-05 11:15:02 -0600 | [diff] [blame] | 316 | uint32_t i; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 317 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device); |
Jon Ashburn | 83334db | 2015-09-16 18:08:32 -0600 | [diff] [blame] | 318 | PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr; |
| 319 | pDisp->GetSurfacePropertiesKHR = (PFN_vkGetSurfacePropertiesKHR) gpa(device, "vkGetSurfacePropertiesKHR"); |
| 320 | pDisp->GetSurfaceFormatsKHR = (PFN_vkGetSurfaceFormatsKHR) gpa(device, "vkGetSurfaceFormatsKHR"); |
| 321 | pDisp->GetSurfacePresentModesKHR = (PFN_vkGetSurfacePresentModesKHR) gpa(device, "vkGetSurfacePresentModesKHR"); |
| 322 | pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR) gpa(device, "vkCreateSwapchainKHR"); |
| 323 | pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR) gpa(device, "vkDestroySwapchainKHR"); |
| 324 | pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR) gpa(device, "vkGetSwapchainImagesKHR"); |
| 325 | pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR) gpa(device, "vkAcquireNextImageKHR"); |
| 326 | pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR) gpa(device, "vkQueuePresentKHR"); |
Ian Elliott | b134e84 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 327 | deviceExtMap[pDisp].wsi_enabled = false; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 328 | for (i = 0; i < pCreateInfo->extensionCount; i++) { |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 329 | 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] | 330 | deviceExtMap[pDisp].wsi_enabled = true; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 334 | VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice( |
| 335 | VkPhysicalDevice gpu, |
| 336 | const VkDeviceCreateInfo *pCreateInfo, |
| 337 | VkDevice *pDevice) |
| 338 | { |
Courtney Goeltzenleuchter | be63799 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 339 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice); |
| 340 | VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pDevice); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 341 | |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 342 | if (result == VK_SUCCESS) { |
Jon Ashburn | 59f3dfc | 2015-07-06 15:09:36 -0600 | [diff] [blame] | 343 | init_screenshot(); |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 344 | createDeviceRegisterExtensions(pCreateInfo, *pDevice); |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 345 | // Create a mapping from a device to a physicalDevice |
| 346 | if (deviceMap[*pDevice] == NULL) |
| 347 | { |
| 348 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 349 | deviceMap[*pDevice] = deviceMapElem; |
| 350 | } |
| 351 | deviceMap[*pDevice]->physicalDevice = gpu; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 352 | } |
| 353 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 354 | return result; |
| 355 | } |
| 356 | |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 357 | VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices( |
| 358 | VkInstance instance, |
| 359 | uint32_t* pPhysicalDeviceCount, |
| 360 | VkPhysicalDevice* pPhysicalDevices) |
| 361 | { |
| 362 | VkResult result; |
| 363 | |
| 364 | VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance); |
| 365 | result = pTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices); |
Tobin Ehlis | d8dd86b | 2015-09-08 08:59:48 -0600 | [diff] [blame] | 366 | if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0 && pPhysicalDevices) |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 367 | { |
| 368 | for (uint32_t i=0; i<*pPhysicalDeviceCount ; i++) |
| 369 | { |
| 370 | // Create a mapping from a physicalDevice to an instance |
| 371 | if (physDeviceMap[pPhysicalDevices[i]] == NULL) |
| 372 | { |
| 373 | PhysDeviceMapStruct *physDeviceMapElem = new PhysDeviceMapStruct; |
| 374 | physDeviceMap[pPhysicalDevices[i]] = physDeviceMapElem; |
| 375 | } |
| 376 | physDeviceMap[pPhysicalDevices[i]]->instance = instance; |
| 377 | } |
| 378 | } |
| 379 | return result; |
| 380 | } |
| 381 | |
Courtney Goeltzenleuchter | be63799 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 382 | /* TODO: Probably need a DestroyDevice as well */ |
| 383 | |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 384 | static const VkLayerProperties ss_device_layers[] = { |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 385 | { |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 386 | "ScreenShot", |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 387 | VK_API_VERSION, |
| 388 | VK_MAKE_VERSION(0, 1, 0), |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 389 | "Layer: ScreenShot", |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 390 | } |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 391 | }; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 392 | |
Courtney Goeltzenleuchter | 74c4ce9 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 393 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties( |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 394 | const char *pLayerName, |
| 395 | uint32_t *pCount, |
| 396 | VkExtensionProperties* pProperties) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 397 | { |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 398 | /* ScreenShot does not have any global extensions */ |
| 399 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 400 | } |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 401 | |
Courtney Goeltzenleuchter | 74c4ce9 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 402 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties( |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 403 | uint32_t *pCount, |
| 404 | VkLayerProperties* pProperties) |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 405 | { |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 406 | /* ScreenShot does not have any global layers */ |
| 407 | return util_GetLayerProperties(0, NULL, |
| 408 | pCount, pProperties); |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 409 | } |
| 410 | |
Courtney Goeltzenleuchter | 74c4ce9 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 411 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties( |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 412 | VkPhysicalDevice physicalDevice, |
| 413 | const char* pLayerName, |
| 414 | uint32_t* pCount, |
| 415 | VkExtensionProperties* pProperties) |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 416 | { |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 417 | /* ScreenShot does not have any physical device extensions */ |
| 418 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
| 419 | } |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 420 | |
Courtney Goeltzenleuchter | 74c4ce9 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 421 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties( |
Courtney Goeltzenleuchter | b96abfc | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 422 | VkPhysicalDevice physicalDevice, |
| 423 | uint32_t* pCount, |
| 424 | VkLayerProperties* pProperties) |
| 425 | { |
| 426 | return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers), |
| 427 | ss_device_layers, |
| 428 | pCount, pProperties); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 429 | } |
| 430 | |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 431 | VK_LAYER_EXPORT void VKAPI vkGetDeviceQueue( |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 432 | VkDevice device, |
| 433 | uint32_t queueNodeIndex, |
| 434 | uint32_t queueIndex, |
| 435 | VkQueue *pQueue) |
| 436 | { |
| 437 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 438 | 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] | 439 | |
| 440 | loader_platform_thread_lock_mutex(&globalLock); |
| 441 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 442 | // We are all done taking screenshots, so don't do anything else |
| 443 | loader_platform_thread_unlock_mutex(&globalLock); |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 444 | return; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 445 | } |
| 446 | |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 447 | screenshot_device_table_map.emplace(*pQueue, pTable); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 448 | |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 449 | // Create a mapping from a device to a queue |
| 450 | if (deviceMap[device] == NULL) |
| 451 | { |
| 452 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 453 | deviceMap[device] = deviceMapElem; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 454 | } |
Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 455 | deviceMap[device]->queue = *pQueue; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 456 | loader_platform_thread_unlock_mutex(&globalLock); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 457 | } |
| 458 | |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 459 | VK_LAYER_EXPORT VkResult VKAPI vkCreateCommandPool( |
| 460 | VkDevice device, |
| 461 | const VkCmdPoolCreateInfo *pCreateInfo, |
| 462 | VkCmdPool *pCmdPool) |
| 463 | { |
| 464 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
| 465 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateCommandPool(device, pCreateInfo, pCmdPool); |
| 466 | |
| 467 | loader_platform_thread_lock_mutex(&globalLock); |
| 468 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 469 | // We are all done taking screenshots, so don't do anything else |
| 470 | loader_platform_thread_unlock_mutex(&globalLock); |
| 471 | return result; |
| 472 | } |
| 473 | |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 474 | // Create a mapping from a device to a cmdPool |
| 475 | if (deviceMap[device] == NULL) |
| 476 | { |
| 477 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 478 | deviceMap[device] = deviceMapElem; |
| 479 | } |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 480 | deviceMap[device]->cmdPool = *pCmdPool; |
| 481 | loader_platform_thread_unlock_mutex(&globalLock); |
| 482 | return result; |
| 483 | } |
| 484 | |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 485 | VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapchainKHR( |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 486 | VkDevice device, |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 487 | const VkSwapchainCreateInfoKHR *pCreateInfo, |
| 488 | VkSwapchainKHR *pSwapchain) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 489 | { |
| 490 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 491 | 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] | 492 | |
| 493 | loader_platform_thread_lock_mutex(&globalLock); |
| 494 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 495 | // We are all done taking screenshots, so don't do anything else |
| 496 | loader_platform_thread_unlock_mutex(&globalLock); |
| 497 | return result; |
| 498 | } |
| 499 | |
| 500 | if (result == VK_SUCCESS) |
| 501 | { |
| 502 | // Create a mapping for a swapchain to a device, image extent, and format |
| 503 | SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct; |
| 504 | swapchainMapElem->device = device; |
| 505 | swapchainMapElem->imageExtent = pCreateInfo->imageExtent; |
| 506 | swapchainMapElem->format = pCreateInfo->imageFormat; |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 507 | swapchainMap.insert(make_pair(pSwapchain->handle, swapchainMapElem)); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 508 | |
| 509 | // Create a mapping for the swapchain object into the dispatch table |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 510 | screenshot_device_table_map.emplace((void *)pSwapchain->handle, pTable); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 511 | } |
| 512 | loader_platform_thread_unlock_mutex(&globalLock); |
| 513 | |
| 514 | return result; |
| 515 | } |
| 516 | |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 517 | VK_LAYER_EXPORT VkResult VKAPI vkGetSwapchainImagesKHR( |
Ian Elliott | b134e84 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 518 | VkDevice device, |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 519 | VkSwapchainKHR swapchain, |
Ian Elliott | ccac023 | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 520 | uint32_t* pCount, |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 521 | VkImage* pSwapchainImages) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 522 | { |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 523 | 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] | 524 | |
| 525 | loader_platform_thread_lock_mutex(&globalLock); |
| 526 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 527 | // We are all done taking screenshots, so don't do anything else |
| 528 | loader_platform_thread_unlock_mutex(&globalLock); |
| 529 | return result; |
| 530 | } |
| 531 | |
| 532 | if (result == VK_SUCCESS && |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 533 | pSwapchainImages && |
| 534 | !swapchainMap.empty() && swapchainMap.find(swapchain.handle) != swapchainMap.end()) |
Ian Elliott | ccac023 | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 535 | { |
Cody Northrop | 46d92c0 | 2015-09-01 11:48:13 -0600 | [diff] [blame] | 536 | unsigned i; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 537 | |
Ian Elliott | ccac023 | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 538 | for (i=0; i<*pCount; i++) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 539 | { |
| 540 | // 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] | 541 | if (imageMap[pSwapchainImages[i].handle] == NULL) |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 542 | { |
| 543 | ImageMapStruct *imageMapElem = new ImageMapStruct; |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 544 | imageMap[pSwapchainImages[i].handle] = imageMapElem; |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 545 | } |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 546 | imageMap[pSwapchainImages[i].handle]->device = swapchainMap[swapchain.handle]->device; |
| 547 | imageMap[pSwapchainImages[i].handle]->imageExtent = swapchainMap[swapchain.handle]->imageExtent; |
| 548 | imageMap[pSwapchainImages[i].handle]->format = swapchainMap[swapchain.handle]->format; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 549 | } |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 550 | |
| 551 | // Add list of images to swapchain to image map |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 552 | SwapchainMapStruct *swapchainMapElem = swapchainMap[swapchain.handle]; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 553 | if (i >= 1 && swapchainMapElem) |
| 554 | { |
| 555 | VkImage *imageList = new VkImage[i]; |
| 556 | swapchainMapElem->imageList = imageList; |
Cody Northrop | 46d92c0 | 2015-09-01 11:48:13 -0600 | [diff] [blame] | 557 | for (unsigned j=0; j<i; j++) |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 558 | { |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 559 | swapchainMapElem->imageList[j] = pSwapchainImages[j].handle; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 563 | } |
| 564 | loader_platform_thread_unlock_mutex(&globalLock); |
| 565 | return result; |
| 566 | } |
| 567 | |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 568 | VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentKHR(VkQueue queue, VkPresentInfoKHR* pPresentInfo) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 569 | { |
| 570 | static int frameNumber = 0; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 571 | if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ } |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 572 | 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] | 573 | |
| 574 | loader_platform_thread_lock_mutex(&globalLock); |
| 575 | |
| 576 | if (!screenshotEnvQueried) |
| 577 | { |
| 578 | const char *_vk_screenshot = getenv("_VK_SCREENSHOT"); |
| 579 | if (_vk_screenshot && *_vk_screenshot) |
| 580 | { |
| 581 | string spec(_vk_screenshot), word; |
| 582 | size_t start = 0, comma = 0; |
| 583 | |
| 584 | while (start < spec.size()) { |
| 585 | int frameToAdd; |
| 586 | comma = spec.find(',', start); |
| 587 | if (comma == string::npos) |
| 588 | word = string(spec, start); |
| 589 | else |
| 590 | word = string(spec, start, comma - start); |
| 591 | frameToAdd=atoi(word.c_str()); |
| 592 | // Add the frame number to list, but only do it if the word started with a digit and if |
| 593 | // it's not already in the list |
| 594 | if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' && |
| 595 | find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end()) |
| 596 | { |
| 597 | screenshotFrames.push_back(frameToAdd); |
| 598 | } |
| 599 | if (comma == string::npos) |
| 600 | break; |
| 601 | start = comma + 1; |
| 602 | } |
| 603 | } |
| 604 | screenshotEnvQueried = true; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | if (result == VK_SUCCESS && !screenshotFrames.empty()) |
| 609 | { |
| 610 | vector<int>::iterator it; |
| 611 | it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber); |
| 612 | if (it != screenshotFrames.end()) |
| 613 | { |
| 614 | string fileName; |
| 615 | fileName = to_string(frameNumber) + ".ppm"; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 616 | |
| 617 | VkImage image; |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 618 | uint64_t swapchain; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 619 | // We'll dump only one image: the first |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 620 | swapchain = pPresentInfo->swapchains[0].handle; |
| 621 | image = swapchainMap[swapchain]->imageList[pPresentInfo->imageIndices[0]]; |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 622 | writePPM(fileName.c_str(), image); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 623 | screenshotFrames.erase(it); |
| 624 | |
| 625 | if (screenshotFrames.empty()) |
| 626 | { |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 627 | // Free all our maps since we are done with them. |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 628 | for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++) |
| 629 | { |
| 630 | SwapchainMapStruct *swapchainMapElem = it->second; |
| 631 | delete swapchainMapElem; |
| 632 | } |
| 633 | for (auto it = imageMap.begin(); it != imageMap.end(); it++) |
| 634 | { |
| 635 | ImageMapStruct *imageMapElem = it->second; |
| 636 | delete imageMapElem; |
| 637 | } |
| 638 | for (auto it = deviceMap.begin(); it != deviceMap.end(); it++) |
| 639 | { |
| 640 | DeviceMapStruct *deviceMapElem = it->second; |
| 641 | delete deviceMapElem; |
| 642 | } |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 643 | for (auto it = physDeviceMap.begin(); it != physDeviceMap.end(); it++) |
| 644 | { |
| 645 | PhysDeviceMapStruct *physDeviceMapElem = it->second; |
| 646 | delete physDeviceMapElem; |
| 647 | } |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 648 | swapchainMap.clear(); |
| 649 | imageMap.clear(); |
| 650 | deviceMap.clear(); |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 651 | physDeviceMap.clear(); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | } |
| 655 | frameNumber++; |
| 656 | loader_platform_thread_unlock_mutex(&globalLock); |
| 657 | return result; |
| 658 | } |
| 659 | |
Courtney Goeltzenleuchter | a4c8c71 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 660 | VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr( |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 661 | VkDevice dev, |
| 662 | const char *funcName) |
| 663 | { |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 664 | if (dev == NULL) { |
| 665 | return NULL; |
| 666 | } |
| 667 | |
| 668 | /* loader uses this to force layer initialization; device object is wrapped */ |
| 669 | if (!strcmp(funcName, "vkGetDeviceProcAddr")) { |
| 670 | initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev); |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 671 | return (PFN_vkVoidFunction)vkGetDeviceProcAddr; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 672 | } |
Courtney Goeltzenleuchter | be63799 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 673 | if (!strcmp(funcName, "vkCreateDevice")) |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 674 | return (PFN_vkVoidFunction) vkCreateDevice; |
Courtney Goeltzenleuchter | be63799 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 675 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 676 | if (!strcmp(funcName, "vkGetDeviceQueue")) |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 677 | return (PFN_vkVoidFunction) vkGetDeviceQueue; |
| 678 | |
| 679 | if (!strcmp(funcName, "vkCreateCommandPool")) |
| 680 | return (PFN_vkVoidFunction) vkCreateCommandPool; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 681 | |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 682 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev); |
Jon Ashburn | 83334db | 2015-09-16 18:08:32 -0600 | [diff] [blame] | 683 | if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].wsi_enabled) |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 684 | { |
Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 685 | if (!strcmp(funcName, "vkCreateSwapchainKHR")) |
| 686 | return (PFN_vkVoidFunction) vkCreateSwapchainKHR; |
| 687 | if (!strcmp(funcName, "vkGetSwapchainImagesKHR")) |
| 688 | return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR; |
| 689 | if (!strcmp(funcName, "vkQueuePresentKHR")) |
| 690 | return (PFN_vkVoidFunction) vkQueuePresentKHR; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | if (pDisp->GetDeviceProcAddr == NULL) |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 694 | return NULL; |
David Pinedo | 8a4f556 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 695 | return pDisp->GetDeviceProcAddr(dev, funcName); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 696 | } |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 697 | |
| 698 | |
Courtney Goeltzenleuchter | a4c8c71 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 699 | VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName) |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 700 | { |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 701 | if (instance == VK_NULL_HANDLE) { |
| 702 | return NULL; |
| 703 | } |
| 704 | |
| 705 | /* loader uses this to force layer initialization; instance object is wrapped */ |
| 706 | if (!strcmp("vkGetInstanceProcAddr", funcName)) { |
| 707 | initInstanceTable((const VkBaseLayerObject *) instance); |
David Pinedo | aa4d1da | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 708 | return (PFN_vkVoidFunction) vkGetInstanceProcAddr; |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 709 | } |
| 710 | |
Cody Northrop | 98fb22a | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 711 | if (!strcmp(funcName, "vkEnumeratePhysicalDevices")) |
| 712 | return (PFN_vkVoidFunction)vkEnumeratePhysicalDevices; |
| 713 | |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 714 | VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance); |
| 715 | if (pTable->GetInstanceProcAddr == NULL) |
| 716 | return NULL; |
| 717 | return pTable->GetInstanceProcAddr(instance, funcName); |
David Pinedo | 07238d4 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 718 | } |