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