David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1 | /* |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2 | * |
Courtney Goeltzenleuchter | fcbe16f | 2015-10-29 13:50:34 -0600 | [diff] [blame] | 3 | * Copyright (C) 2015 Valve Corporation |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included |
| 13 | * in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 22 | * |
| 23 | * Author: Cody Northrop <cody@lunarg.com> |
| 24 | * Author: David Pinedo <david@lunarg.com> |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #include <inttypes.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <assert.h> |
| 32 | #include <unordered_map> |
| 33 | #include <iostream> |
| 34 | #include <algorithm> |
| 35 | #include <list> |
| 36 | #include <map> |
| 37 | #include <vector> |
| 38 | #include <fstream> |
| 39 | |
| 40 | using namespace std; |
| 41 | |
Tobin Ehlis | b835d1b | 2015-07-03 10:34:49 -0600 | [diff] [blame] | 42 | #include "vk_loader_platform.h" |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 43 | #include "vk_dispatch_table_helper.h" |
| 44 | #include "vk_struct_string_helper_cpp.h" |
Tobin Ehlis | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 45 | #include "vk_layer_config.h" |
Tobin Ehlis | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 46 | #include "vk_layer_table.h" |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 47 | #include "vk_layer_extension_utils.h" |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 48 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 49 | |
| 50 | struct devExts { |
Ian Elliott | 1064fe3 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 51 | bool wsi_enabled; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 52 | }; |
| 53 | static std::unordered_map<void *, struct devExts> deviceExtMap; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 54 | static device_table_map screenshot_device_table_map; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 55 | |
| 56 | static int globalLockInitialized = 0; |
| 57 | static loader_platform_thread_mutex globalLock; |
| 58 | |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 59 | // unordered map: associates a swap chain with a device, image extent, format, and |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 60 | // list of images |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 61 | typedef struct |
| 62 | { |
| 63 | VkDevice device; |
| 64 | VkExtent2D imageExtent; |
| 65 | VkFormat format; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 66 | VkImage *imageList; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 67 | } SwapchainMapStruct; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 68 | static unordered_map<VkSwapchainKHR, SwapchainMapStruct *> swapchainMap; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 69 | |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 70 | // unordered map: associates an image with a device, image extent, and format |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 71 | typedef struct |
| 72 | { |
| 73 | VkDevice device; |
| 74 | VkExtent2D imageExtent; |
| 75 | VkFormat format; |
| 76 | } ImageMapStruct; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 77 | static unordered_map<VkImage, ImageMapStruct *> imageMap; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 78 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 79 | // unordered map: associates a device with a queue, commandPool, and physical device |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 80 | typedef struct |
| 81 | { |
| 82 | VkQueue queue; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 83 | VkCommandPool commandPool; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 84 | VkPhysicalDevice physicalDevice; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 85 | } DeviceMapStruct; |
| 86 | static unordered_map<VkDevice, DeviceMapStruct *> deviceMap; |
| 87 | |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 88 | // unordered map: associates a physical device with an instance |
| 89 | typedef struct |
| 90 | { |
| 91 | VkInstance instance; |
| 92 | } PhysDeviceMapStruct; |
| 93 | static unordered_map<VkPhysicalDevice, PhysDeviceMapStruct *> physDeviceMap; |
| 94 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 95 | // List of frames to we will get a screenshot of |
| 96 | static vector<int> screenshotFrames; |
| 97 | |
| 98 | // Flag indicating we have queried _VK_SCREENSHOT env var |
| 99 | static bool screenshotEnvQueried = false; |
| 100 | |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 101 | static bool memory_type_from_properties( |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 102 | VkPhysicalDeviceMemoryProperties *memory_properties, |
| 103 | uint32_t typeBits, |
Tony Barbour | af392d0 | 2015-10-15 12:42:56 -0600 | [diff] [blame] | 104 | VkFlags requirements_mask, |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 105 | uint32_t *typeIndex) |
| 106 | { |
| 107 | // Search memtypes to find first index with those properties |
| 108 | for (uint32_t i = 0; i < 32; i++) { |
| 109 | if ((typeBits & 1) == 1) { |
| 110 | // Type is available, does it match user properties? |
Tony Barbour | af392d0 | 2015-10-15 12:42:56 -0600 | [diff] [blame] | 111 | if ((memory_properties->memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) { |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 112 | *typeIndex = i; |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 113 | return true; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | typeBits >>= 1; |
| 117 | } |
| 118 | // No memory types matched, return failure |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 119 | return false; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 120 | } |
| 121 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 122 | static void init_screenshot() |
| 123 | { |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 124 | if (!globalLockInitialized) |
| 125 | { |
| 126 | // TODO/TBD: Need to delete this mutex sometime. How??? One |
| 127 | // suggestion is to call this during vkCreateInstance(), and then we |
| 128 | // can clean it up during vkDestroyInstance(). However, that requires |
| 129 | // that the layer have per-instance locks. We need to come back and |
| 130 | // address this soon. |
| 131 | loader_platform_thread_create_mutex(&globalLock); |
| 132 | globalLockInitialized = 1; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static void writePPM( const char *filename, VkImage image1) |
| 137 | { |
| 138 | VkImage image2; |
| 139 | VkResult err; |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 140 | bool pass; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 141 | int x, y; |
| 142 | const char *ptr; |
| 143 | VkDeviceMemory mem2; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 144 | VkCommandBuffer commandBuffer; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 145 | VkDevice device = imageMap[image1]->device; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 146 | VkPhysicalDevice physicalDevice = deviceMap[device]->physicalDevice; |
| 147 | VkInstance instance = physDeviceMap[physicalDevice]->instance; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 148 | VkQueue queue = deviceMap[device]->queue; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 149 | int width = imageMap[image1]->imageExtent.width; |
| 150 | int height = imageMap[image1]->imageExtent.height; |
| 151 | VkFormat format = imageMap[image1]->format; |
Courtney Goeltzenleuchter | 908e767 | 2015-10-21 17:00:51 -0600 | [diff] [blame] | 152 | const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0}; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 153 | VkSubresourceLayout sr_layout; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 154 | const VkImageCreateInfo imgCreateInfo = { |
| 155 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 156 | NULL, |
Chia-I Wu | 439c077 | 2015-10-26 17:00:46 +0800 | [diff] [blame] | 157 | 0, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 158 | VK_IMAGE_TYPE_2D, |
| 159 | format, |
| 160 | {width, height, 1}, |
| 161 | 1, |
| 162 | 1, |
Chia-I Wu | 5c17c96 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 163 | VK_SAMPLE_COUNT_1_BIT, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 164 | VK_IMAGE_TILING_LINEAR, |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 165 | (VK_IMAGE_USAGE_TRANSFER_DST_BIT|VK_IMAGE_USAGE_STORAGE_BIT), |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 166 | }; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 167 | VkMemoryAllocateInfo memAllocInfo = { |
Chia-I Wu | 00ce540 | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 168 | VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 169 | NULL, |
| 170 | 0, // allocationSize, queried later |
Cody Northrop | 488f547 | 2015-09-01 11:47:50 -0600 | [diff] [blame] | 171 | 0 // memoryTypeIndex, queried later |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 172 | }; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 173 | const VkCommandBufferAllocateInfo allocCommandBufferInfo = { |
Chia-I Wu | 00ce540 | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 174 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 175 | NULL, |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 176 | deviceMap[device]->commandPool, |
| 177 | VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 178 | 1 |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 179 | }; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 180 | const VkCommandBufferBeginInfo commandBufferBeginInfo = { |
| 181 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 182 | NULL, |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 183 | VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 184 | }; |
| 185 | const VkImageCopy imageCopyRegion = { |
Courtney Goeltzenleuchter | 908e767 | 2015-10-21 17:00:51 -0600 | [diff] [blame] | 186 | {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0}, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 187 | {0, 0, 0}, |
Courtney Goeltzenleuchter | 908e767 | 2015-10-21 17:00:51 -0600 | [diff] [blame] | 188 | {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0}, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 189 | {0, 0, 0}, |
| 190 | {width, height, 1} |
| 191 | }; |
| 192 | VkMemoryRequirements memRequirements; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 193 | uint32_t num_allocations = 0; |
| 194 | size_t num_alloc_size = sizeof(num_allocations); |
David Pinedo | 6829587 | 2015-06-30 13:08:37 -0600 | [diff] [blame] | 195 | VkLayerDispatchTable* pTableDevice = get_dispatch_table(screenshot_device_table_map, device); |
| 196 | VkLayerDispatchTable* pTableQueue = get_dispatch_table(screenshot_device_table_map, queue); |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 197 | VkLayerInstanceDispatchTable* pInstanceTable; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 198 | VkLayerDispatchTable* pTableCommandBuffer; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 199 | VkPhysicalDeviceMemoryProperties memory_properties; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 200 | |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 201 | if (imageMap.empty() || imageMap.find(image1) == imageMap.end()) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 202 | return; |
| 203 | |
| 204 | // The VkImage image1 we are going to dump may not be mappable, |
| 205 | // and/or it may have a tiling mode of optimal rather than linear. |
| 206 | // To make sure we have an image that we can map and read linearly, we: |
| 207 | // create image2 that is mappable and linear |
| 208 | // copy image1 to image2 |
| 209 | // map image2 |
| 210 | // read from image2's mapped memeory. |
| 211 | |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 212 | err = pTableDevice->CreateImage(device, &imgCreateInfo, NULL, &image2); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 213 | assert(!err); |
| 214 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 215 | pTableDevice->GetImageMemoryRequirements(device, image2, &memRequirements); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 216 | |
| 217 | memAllocInfo.allocationSize = memRequirements.size; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 218 | pInstanceTable = instance_dispatch_table(instance); |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 219 | pInstanceTable->GetPhysicalDeviceMemoryProperties(physicalDevice, &memory_properties); |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 220 | |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 221 | pass = memory_type_from_properties(&memory_properties, |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 222 | memRequirements.memoryTypeBits, |
Cody Northrop | 488f547 | 2015-09-01 11:47:50 -0600 | [diff] [blame] | 223 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 224 | &memAllocInfo.memoryTypeIndex); |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 225 | assert(pass); |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 226 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 227 | err = pTableDevice->AllocateMemory(device, &memAllocInfo, NULL, &mem2); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 228 | assert(!err); |
| 229 | |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 230 | err = pTableQueue->BindImageMemory(device, image2, mem2, 0); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 231 | assert(!err); |
| 232 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 233 | err = pTableDevice->AllocateCommandBuffers(device, &allocCommandBufferInfo, &commandBuffer); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 234 | assert(!err); |
| 235 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 236 | screenshot_device_table_map.emplace(commandBuffer, pTableDevice); |
| 237 | pTableCommandBuffer = screenshot_device_table_map[commandBuffer]; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 238 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 239 | err = pTableCommandBuffer->BeginCommandBuffer(commandBuffer, &commandBufferBeginInfo); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 240 | assert(!err); |
| 241 | |
Cody Northrop | 488f547 | 2015-09-01 11:47:50 -0600 | [diff] [blame] | 242 | // TODO: We need to transition images to match these layouts, then restore the original layouts |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 243 | pTableCommandBuffer->CmdCopyImage(commandBuffer, image1, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 244 | image2, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &imageCopyRegion); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 245 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 246 | err = pTableCommandBuffer->EndCommandBuffer(commandBuffer); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 247 | assert(!err); |
| 248 | |
Courtney Goeltzenleuchter | 646b907 | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 249 | VkFence nullFence = { VK_NULL_HANDLE }; |
Courtney Goeltzenleuchter | 806c700 | 2015-10-27 11:22:14 -0600 | [diff] [blame] | 250 | VkSubmitInfo submit_info; |
Chia-I Wu | f9be13c | 2015-10-26 20:37:06 +0800 | [diff] [blame] | 251 | submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 252 | submit_info.pNext = NULL; |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 253 | submit_info.waitSemaphoreCount = 0; |
Courtney Goeltzenleuchter | 806c700 | 2015-10-27 11:22:14 -0600 | [diff] [blame] | 254 | submit_info.pWaitSemaphores = NULL; |
Jon Ashburn | 7f9716c | 2015-12-30 16:42:50 -0700 | [diff] [blame] | 255 | submit_info.pWaitDstStageMask = NULL; |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 256 | submit_info.commandBufferCount = 1; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 257 | submit_info.pCommandBuffers = &commandBuffer; |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 258 | submit_info.signalSemaphoreCount = 0; |
Courtney Goeltzenleuchter | 806c700 | 2015-10-27 11:22:14 -0600 | [diff] [blame] | 259 | submit_info.pSignalSemaphores = NULL; |
Courtney Goeltzenleuchter | 646b907 | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 260 | |
| 261 | err = pTableQueue->QueueSubmit(queue, 1, &submit_info, nullFence); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 262 | assert(!err); |
| 263 | |
| 264 | err = pTableQueue->QueueWaitIdle(queue); |
| 265 | assert(!err); |
| 266 | |
| 267 | err = pTableDevice->DeviceWaitIdle(device); |
| 268 | assert(!err); |
| 269 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 270 | pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 271 | |
| 272 | err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr ); |
| 273 | assert(!err); |
| 274 | |
| 275 | ptr += sr_layout.offset; |
| 276 | |
| 277 | ofstream file(filename, ios::binary); |
| 278 | |
| 279 | file << "P6\n"; |
| 280 | file << width << "\n"; |
| 281 | file << height << "\n"; |
| 282 | file << 255 << "\n"; |
| 283 | |
| 284 | for (y = 0; y < height; y++) { |
| 285 | const unsigned int *row = (const unsigned int*) ptr; |
| 286 | if (format == VK_FORMAT_B8G8R8A8_UNORM) |
| 287 | { |
| 288 | for (x = 0; x < width; x++) { |
| 289 | unsigned int swapped; |
| 290 | swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16; |
| 291 | file.write((char *)&swapped, 3); |
| 292 | row++; |
| 293 | } |
| 294 | } |
| 295 | else if (format == VK_FORMAT_R8G8B8A8_UNORM) |
| 296 | { |
| 297 | for (x = 0; x < width; x++) { |
| 298 | file.write((char *)row, 3); |
| 299 | row++; |
| 300 | } |
| 301 | } |
| 302 | else |
| 303 | { |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 304 | // TODO: add support for additional formats |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 305 | printf("Unrecognized image format\n"); |
| 306 | break; |
| 307 | } |
| 308 | ptr += sr_layout.rowPitch; |
| 309 | } |
| 310 | file.close(); |
| 311 | |
| 312 | // Clean up |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 313 | pTableDevice->UnmapMemory(device, mem2); |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 314 | pTableDevice->FreeMemory(device, mem2, NULL); |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 315 | pTableDevice->FreeCommandBuffers(device, deviceMap[device]->commandPool, 1, &commandBuffer); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 319 | static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device) |
| 320 | { |
Cody Northrop | d2ad034 | 2015-08-05 11:15:02 -0600 | [diff] [blame] | 321 | uint32_t i; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 322 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device); |
Jon Ashburn | 8acd233 | 2015-09-16 18:08:32 -0600 | [diff] [blame] | 323 | PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr; |
Jon Ashburn | 8acd233 | 2015-09-16 18:08:32 -0600 | [diff] [blame] | 324 | pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR) gpa(device, "vkCreateSwapchainKHR"); |
Jon Ashburn | 8acd233 | 2015-09-16 18:08:32 -0600 | [diff] [blame] | 325 | pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR) gpa(device, "vkGetSwapchainImagesKHR"); |
| 326 | pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR) gpa(device, "vkAcquireNextImageKHR"); |
| 327 | pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR) gpa(device, "vkQueuePresentKHR"); |
Ian Elliott | 1064fe3 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 328 | deviceExtMap[pDisp].wsi_enabled = false; |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 329 | for (i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) { |
Ian Elliott | 0584606 | 2015-11-20 14:13:17 -0700 | [diff] [blame] | 330 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0) |
Ian Elliott | 1064fe3 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 331 | deviceExtMap[pDisp].wsi_enabled = true; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 335 | VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice( |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 336 | VkPhysicalDevice gpu, |
| 337 | const VkDeviceCreateInfo *pCreateInfo, |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 338 | const VkAllocationCallbacks* pAllocator, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 339 | VkDevice *pDevice) |
| 340 | { |
Courtney Goeltzenleuchter | ca173b8 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 341 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice); |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 342 | VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 343 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 344 | if (result == VK_SUCCESS) { |
Jon Ashburn | ec10533 | 2015-07-06 15:09:36 -0600 | [diff] [blame] | 345 | init_screenshot(); |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 346 | createDeviceRegisterExtensions(pCreateInfo, *pDevice); |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 347 | // Create a mapping from a device to a physicalDevice |
| 348 | if (deviceMap[*pDevice] == NULL) |
| 349 | { |
| 350 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 351 | deviceMap[*pDevice] = deviceMapElem; |
| 352 | } |
| 353 | deviceMap[*pDevice]->physicalDevice = gpu; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 354 | } |
| 355 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 356 | return result; |
| 357 | } |
| 358 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 359 | VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumeratePhysicalDevices( |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 360 | VkInstance instance, |
| 361 | uint32_t* pPhysicalDeviceCount, |
| 362 | VkPhysicalDevice* pPhysicalDevices) |
| 363 | { |
| 364 | VkResult result; |
| 365 | |
| 366 | VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance); |
| 367 | result = pTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices); |
Tobin Ehlis | be42774 | 2015-09-08 08:59:48 -0600 | [diff] [blame] | 368 | if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0 && pPhysicalDevices) |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 369 | { |
| 370 | for (uint32_t i=0; i<*pPhysicalDeviceCount ; i++) |
| 371 | { |
| 372 | // Create a mapping from a physicalDevice to an instance |
| 373 | if (physDeviceMap[pPhysicalDevices[i]] == NULL) |
| 374 | { |
| 375 | PhysDeviceMapStruct *physDeviceMapElem = new PhysDeviceMapStruct; |
| 376 | physDeviceMap[pPhysicalDevices[i]] = physDeviceMapElem; |
| 377 | } |
| 378 | physDeviceMap[pPhysicalDevices[i]]->instance = instance; |
| 379 | } |
| 380 | } |
| 381 | return result; |
| 382 | } |
| 383 | |
Courtney Goeltzenleuchter | ca173b8 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 384 | /* TODO: Probably need a DestroyDevice as well */ |
| 385 | |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 386 | static const VkLayerProperties ss_device_layers[] = { |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 387 | { |
Mark Lobodzinski | 0d054fe | 2015-12-30 08:16:12 -0700 | [diff] [blame] | 388 | "screenshot", |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 389 | VK_API_VERSION, |
| 390 | VK_MAKE_VERSION(0, 1, 0), |
Mark Lobodzinski | 0d054fe | 2015-12-30 08:16:12 -0700 | [diff] [blame] | 391 | "Layer: screenshot", |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 392 | } |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 393 | }; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 394 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 395 | VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties( |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 396 | const char *pLayerName, |
| 397 | uint32_t *pCount, |
| 398 | VkExtensionProperties* pProperties) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 399 | { |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 400 | /* ScreenShot does not have any global extensions */ |
| 401 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 402 | } |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 403 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 404 | VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties( |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 405 | uint32_t *pCount, |
| 406 | VkLayerProperties* pProperties) |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 407 | { |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 408 | /* ScreenShot does not have any global layers */ |
| 409 | return util_GetLayerProperties(0, NULL, |
| 410 | pCount, pProperties); |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 411 | } |
| 412 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 413 | VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties( |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 414 | VkPhysicalDevice physicalDevice, |
| 415 | const char* pLayerName, |
| 416 | uint32_t* pCount, |
| 417 | VkExtensionProperties* pProperties) |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 418 | { |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 419 | /* ScreenShot does not have any physical device extensions */ |
Jon Ashburn | 751c484 | 2015-11-02 17:37:20 -0700 | [diff] [blame] | 420 | if (pLayerName == NULL) { |
| 421 | VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(physicalDevice); |
| 422 | return pTable->EnumerateDeviceExtensionProperties( |
| 423 | physicalDevice, |
| 424 | NULL, |
| 425 | pCount, |
| 426 | pProperties); |
| 427 | } else { |
| 428 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
| 429 | } |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 430 | } |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 431 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 432 | VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties( |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 433 | VkPhysicalDevice physicalDevice, |
| 434 | uint32_t* pCount, |
| 435 | VkLayerProperties* pProperties) |
| 436 | { |
| 437 | return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers), |
| 438 | ss_device_layers, |
| 439 | pCount, pProperties); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 440 | } |
| 441 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 442 | VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkGetDeviceQueue( |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 443 | VkDevice device, |
| 444 | uint32_t queueNodeIndex, |
| 445 | uint32_t queueIndex, |
| 446 | VkQueue *pQueue) |
| 447 | { |
| 448 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 449 | get_dispatch_table(screenshot_device_table_map, device)->GetDeviceQueue(device, queueNodeIndex, queueIndex, pQueue); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 450 | |
| 451 | loader_platform_thread_lock_mutex(&globalLock); |
| 452 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 453 | // We are all done taking screenshots, so don't do anything else |
| 454 | loader_platform_thread_unlock_mutex(&globalLock); |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 455 | return; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 456 | } |
| 457 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 458 | screenshot_device_table_map.emplace(*pQueue, pTable); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 459 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 460 | // Create a mapping from a device to a queue |
| 461 | if (deviceMap[device] == NULL) |
| 462 | { |
| 463 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 464 | deviceMap[device] = deviceMapElem; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 465 | } |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 466 | deviceMap[device]->queue = *pQueue; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 467 | loader_platform_thread_unlock_mutex(&globalLock); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 468 | } |
| 469 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 470 | VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateCommandPool( |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 471 | VkDevice device, |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 472 | const VkCommandPoolCreateInfo *pCreateInfo, |
| 473 | const VkAllocationCallbacks* pAllocator, |
| 474 | VkCommandPool *pCommandPool) |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 475 | { |
| 476 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 477 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateCommandPool(device, pCreateInfo, pAllocator, pCommandPool); |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 478 | |
| 479 | loader_platform_thread_lock_mutex(&globalLock); |
| 480 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 481 | // We are all done taking screenshots, so don't do anything else |
| 482 | loader_platform_thread_unlock_mutex(&globalLock); |
| 483 | return result; |
| 484 | } |
| 485 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 486 | // Create a mapping from a device to a commandPool |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 487 | if (deviceMap[device] == NULL) |
| 488 | { |
| 489 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 490 | deviceMap[device] = deviceMapElem; |
| 491 | } |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 492 | deviceMap[device]->commandPool = *pCommandPool; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 493 | loader_platform_thread_unlock_mutex(&globalLock); |
| 494 | return result; |
| 495 | } |
| 496 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 497 | VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR( |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 498 | VkDevice device, |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 499 | const VkSwapchainCreateInfoKHR *pCreateInfo, |
Ian Elliott | 0584606 | 2015-11-20 14:13:17 -0700 | [diff] [blame] | 500 | const VkAllocationCallbacks *pAllocator, |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 501 | VkSwapchainKHR *pSwapchain) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 502 | { |
| 503 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
Ian Elliott | 0584606 | 2015-11-20 14:13:17 -0700 | [diff] [blame] | 504 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 505 | |
| 506 | loader_platform_thread_lock_mutex(&globalLock); |
| 507 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 508 | // We are all done taking screenshots, so don't do anything else |
| 509 | loader_platform_thread_unlock_mutex(&globalLock); |
| 510 | return result; |
| 511 | } |
| 512 | |
| 513 | if (result == VK_SUCCESS) |
| 514 | { |
| 515 | // Create a mapping for a swapchain to a device, image extent, and format |
| 516 | SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct; |
| 517 | swapchainMapElem->device = device; |
| 518 | swapchainMapElem->imageExtent = pCreateInfo->imageExtent; |
| 519 | swapchainMapElem->format = pCreateInfo->imageFormat; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 520 | swapchainMap.insert(make_pair(*pSwapchain, swapchainMapElem)); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 521 | |
| 522 | // Create a mapping for the swapchain object into the dispatch table |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 523 | screenshot_device_table_map.emplace((void *)pSwapchain, pTable); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 524 | } |
| 525 | loader_platform_thread_unlock_mutex(&globalLock); |
| 526 | |
| 527 | return result; |
| 528 | } |
| 529 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 530 | VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainImagesKHR( |
Ian Elliott | 1064fe3 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 531 | VkDevice device, |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 532 | VkSwapchainKHR swapchain, |
Ian Elliott | 2b6b68a | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 533 | uint32_t* pCount, |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 534 | VkImage* pSwapchainImages) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 535 | { |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 536 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->GetSwapchainImagesKHR(device, swapchain, pCount, pSwapchainImages); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 537 | |
| 538 | loader_platform_thread_lock_mutex(&globalLock); |
| 539 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 540 | // We are all done taking screenshots, so don't do anything else |
| 541 | loader_platform_thread_unlock_mutex(&globalLock); |
| 542 | return result; |
| 543 | } |
| 544 | |
| 545 | if (result == VK_SUCCESS && |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 546 | pSwapchainImages && |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 547 | !swapchainMap.empty() && swapchainMap.find(swapchain) != swapchainMap.end()) |
Ian Elliott | 2b6b68a | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 548 | { |
Cody Northrop | bf06582 | 2015-09-01 11:48:13 -0600 | [diff] [blame] | 549 | unsigned i; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 550 | |
Ian Elliott | 2b6b68a | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 551 | for (i=0; i<*pCount; i++) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 552 | { |
| 553 | // Create a mapping for an image to a device, image extent, and format |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 554 | if (imageMap[pSwapchainImages[i]] == NULL) |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 555 | { |
| 556 | ImageMapStruct *imageMapElem = new ImageMapStruct; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 557 | imageMap[pSwapchainImages[i]] = imageMapElem; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 558 | } |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 559 | imageMap[pSwapchainImages[i]]->device = swapchainMap[swapchain]->device; |
| 560 | imageMap[pSwapchainImages[i]]->imageExtent = swapchainMap[swapchain]->imageExtent; |
| 561 | imageMap[pSwapchainImages[i]]->format = swapchainMap[swapchain]->format; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 562 | } |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 563 | |
| 564 | // Add list of images to swapchain to image map |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 565 | SwapchainMapStruct *swapchainMapElem = swapchainMap[swapchain]; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 566 | if (i >= 1 && swapchainMapElem) |
| 567 | { |
| 568 | VkImage *imageList = new VkImage[i]; |
| 569 | swapchainMapElem->imageList = imageList; |
Cody Northrop | bf06582 | 2015-09-01 11:48:13 -0600 | [diff] [blame] | 570 | for (unsigned j=0; j<i; j++) |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 571 | { |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 572 | swapchainMapElem->imageList[j] = pSwapchainImages[j]; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 576 | } |
| 577 | loader_platform_thread_unlock_mutex(&globalLock); |
| 578 | return result; |
| 579 | } |
| 580 | |
Ian Elliott | 0584606 | 2015-11-20 14:13:17 -0700 | [diff] [blame] | 581 | VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 582 | { |
| 583 | static int frameNumber = 0; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 584 | if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ } |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 585 | VkResult result = get_dispatch_table(screenshot_device_table_map, queue)->QueuePresentKHR(queue, pPresentInfo); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 586 | |
| 587 | loader_platform_thread_lock_mutex(&globalLock); |
| 588 | |
| 589 | if (!screenshotEnvQueried) |
| 590 | { |
Jon Ashburn | 38a497f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 591 | const char *_vk_screenshot = loader_getenv("_VK_SCREENSHOT"); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 592 | if (_vk_screenshot && *_vk_screenshot) |
| 593 | { |
| 594 | string spec(_vk_screenshot), word; |
| 595 | size_t start = 0, comma = 0; |
| 596 | |
| 597 | while (start < spec.size()) { |
| 598 | int frameToAdd; |
| 599 | comma = spec.find(',', start); |
| 600 | if (comma == string::npos) |
| 601 | word = string(spec, start); |
| 602 | else |
| 603 | word = string(spec, start, comma - start); |
| 604 | frameToAdd=atoi(word.c_str()); |
| 605 | // Add the frame number to list, but only do it if the word started with a digit and if |
| 606 | // it's not already in the list |
| 607 | if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' && |
| 608 | find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end()) |
| 609 | { |
| 610 | screenshotFrames.push_back(frameToAdd); |
| 611 | } |
| 612 | if (comma == string::npos) |
| 613 | break; |
| 614 | start = comma + 1; |
| 615 | } |
| 616 | } |
Jon Ashburn | 38a497f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 617 | loader_free_getenv(_vk_screenshot); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 618 | screenshotEnvQueried = true; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | if (result == VK_SUCCESS && !screenshotFrames.empty()) |
| 623 | { |
| 624 | vector<int>::iterator it; |
| 625 | it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber); |
| 626 | if (it != screenshotFrames.end()) |
| 627 | { |
| 628 | string fileName; |
| 629 | fileName = to_string(frameNumber) + ".ppm"; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 630 | |
| 631 | VkImage image; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 632 | VkSwapchainKHR swapchain; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 633 | // We'll dump only one image: the first |
Ian Elliott | 0584606 | 2015-11-20 14:13:17 -0700 | [diff] [blame] | 634 | swapchain = pPresentInfo->pSwapchains[0]; |
| 635 | image = swapchainMap[swapchain]->imageList[pPresentInfo->pImageIndices[0]]; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 636 | writePPM(fileName.c_str(), image); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 637 | screenshotFrames.erase(it); |
| 638 | |
| 639 | if (screenshotFrames.empty()) |
| 640 | { |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 641 | // Free all our maps since we are done with them. |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 642 | for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++) |
| 643 | { |
| 644 | SwapchainMapStruct *swapchainMapElem = it->second; |
| 645 | delete swapchainMapElem; |
| 646 | } |
| 647 | for (auto it = imageMap.begin(); it != imageMap.end(); it++) |
| 648 | { |
| 649 | ImageMapStruct *imageMapElem = it->second; |
| 650 | delete imageMapElem; |
| 651 | } |
| 652 | for (auto it = deviceMap.begin(); it != deviceMap.end(); it++) |
| 653 | { |
| 654 | DeviceMapStruct *deviceMapElem = it->second; |
| 655 | delete deviceMapElem; |
| 656 | } |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 657 | for (auto it = physDeviceMap.begin(); it != physDeviceMap.end(); it++) |
| 658 | { |
| 659 | PhysDeviceMapStruct *physDeviceMapElem = it->second; |
| 660 | delete physDeviceMapElem; |
| 661 | } |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 662 | swapchainMap.clear(); |
| 663 | imageMap.clear(); |
| 664 | deviceMap.clear(); |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 665 | physDeviceMap.clear(); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | } |
| 669 | frameNumber++; |
| 670 | loader_platform_thread_unlock_mutex(&globalLock); |
| 671 | return result; |
| 672 | } |
| 673 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 674 | VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr( |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 675 | VkDevice dev, |
| 676 | const char *funcName) |
| 677 | { |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 678 | if (dev == NULL) { |
| 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | /* loader uses this to force layer initialization; device object is wrapped */ |
| 683 | if (!strcmp(funcName, "vkGetDeviceProcAddr")) { |
| 684 | initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev); |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 685 | return (PFN_vkVoidFunction)vkGetDeviceProcAddr; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 686 | } |
Courtney Goeltzenleuchter | ca173b8 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 687 | if (!strcmp(funcName, "vkCreateDevice")) |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 688 | return (PFN_vkVoidFunction) vkCreateDevice; |
Courtney Goeltzenleuchter | ca173b8 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 689 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 690 | if (!strcmp(funcName, "vkGetDeviceQueue")) |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 691 | return (PFN_vkVoidFunction) vkGetDeviceQueue; |
| 692 | |
| 693 | if (!strcmp(funcName, "vkCreateCommandPool")) |
| 694 | return (PFN_vkVoidFunction) vkCreateCommandPool; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 695 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 696 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev); |
Jon Ashburn | 8acd233 | 2015-09-16 18:08:32 -0600 | [diff] [blame] | 697 | if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].wsi_enabled) |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 698 | { |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 699 | if (!strcmp(funcName, "vkCreateSwapchainKHR")) |
| 700 | return (PFN_vkVoidFunction) vkCreateSwapchainKHR; |
| 701 | if (!strcmp(funcName, "vkGetSwapchainImagesKHR")) |
| 702 | return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR; |
| 703 | if (!strcmp(funcName, "vkQueuePresentKHR")) |
| 704 | return (PFN_vkVoidFunction) vkQueuePresentKHR; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | if (pDisp->GetDeviceProcAddr == NULL) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 708 | return NULL; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 709 | return pDisp->GetDeviceProcAddr(dev, funcName); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 710 | } |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 711 | |
| 712 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 713 | VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* funcName) |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 714 | { |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 715 | if (instance == VK_NULL_HANDLE) { |
| 716 | return NULL; |
| 717 | } |
| 718 | |
| 719 | /* loader uses this to force layer initialization; instance object is wrapped */ |
| 720 | if (!strcmp("vkGetInstanceProcAddr", funcName)) { |
| 721 | initInstanceTable((const VkBaseLayerObject *) instance); |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 722 | return (PFN_vkVoidFunction) vkGetInstanceProcAddr; |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 723 | } |
| 724 | |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 725 | if (!strcmp(funcName, "vkEnumeratePhysicalDevices")) |
| 726 | return (PFN_vkVoidFunction)vkEnumeratePhysicalDevices; |
Jon Ashburn | 751c484 | 2015-11-02 17:37:20 -0700 | [diff] [blame] | 727 | if (!strcmp(funcName, "vkEnumerateDeviceExtensionProperties")) |
| 728 | return (PFN_vkVoidFunction)vkEnumerateDeviceExtensionProperties; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 729 | |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 730 | VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance); |
| 731 | if (pTable->GetInstanceProcAddr == NULL) |
| 732 | return NULL; |
| 733 | return pTable->GetInstanceProcAddr(instance, funcName); |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 734 | } |