David Pinedo | fb5b538 | 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 | b835d1b | 2015-07-03 10:34:49 -0600 | [diff] [blame] | 40 | #include "vk_loader_platform.h" |
David Pinedo | fb5b538 | 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 | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 43 | #include "vk_layer_config.h" |
Tobin Ehlis | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 44 | #include "vk_layer_table.h" |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 45 | #include "vk_layer_extension_utils.h" |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 46 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 47 | |
| 48 | struct devExts { |
Ian Elliott | 1064fe3 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 49 | bool wsi_enabled; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 50 | }; |
| 51 | static std::unordered_map<void *, struct devExts> deviceExtMap; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 52 | static device_table_map screenshot_device_table_map; |
David Pinedo | fb5b538 | 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 | 49f885c | 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 | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 58 | // list of images |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 59 | typedef struct |
| 60 | { |
| 61 | VkDevice device; |
| 62 | VkExtent2D imageExtent; |
| 63 | VkFormat format; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 64 | VkImage *imageList; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 65 | } SwapchainMapStruct; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 66 | static unordered_map<VkSwapchainKHR, SwapchainMapStruct *> swapchainMap; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 67 | |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 68 | // unordered map: associates an image with a device, image extent, and format |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 69 | typedef struct |
| 70 | { |
| 71 | VkDevice device; |
| 72 | VkExtent2D imageExtent; |
| 73 | VkFormat format; |
| 74 | } ImageMapStruct; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 75 | static unordered_map<VkImage, ImageMapStruct *> imageMap; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 76 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 77 | // unordered map: associates a device with a queue, commandPool, and physical device |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 78 | typedef struct |
| 79 | { |
| 80 | VkQueue queue; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 81 | VkCommandPool commandPool; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 82 | VkPhysicalDevice physicalDevice; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 83 | } DeviceMapStruct; |
| 84 | static unordered_map<VkDevice, DeviceMapStruct *> deviceMap; |
| 85 | |
Cody Northrop | 49f885c | 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 | fb5b538 | 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 | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 99 | static bool memory_type_from_properties( |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 100 | VkPhysicalDeviceMemoryProperties *memory_properties, |
| 101 | uint32_t typeBits, |
Tony Barbour | af392d0 | 2015-10-15 12:42:56 -0600 | [diff] [blame] | 102 | VkFlags requirements_mask, |
Cody Northrop | 49f885c | 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 | af392d0 | 2015-10-15 12:42:56 -0600 | [diff] [blame] | 109 | if ((memory_properties->memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) { |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 110 | *typeIndex = i; |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 111 | return true; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | typeBits >>= 1; |
| 115 | } |
| 116 | // No memory types matched, return failure |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 117 | return false; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 118 | } |
| 119 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 120 | static void init_screenshot() |
| 121 | { |
David Pinedo | fb5b538 | 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 | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 138 | bool pass; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 139 | int x, y; |
| 140 | const char *ptr; |
| 141 | VkDeviceMemory mem2; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 142 | VkCommandBuffer commandBuffer; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 143 | VkDevice device = imageMap[image1]->device; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 144 | VkPhysicalDevice physicalDevice = deviceMap[device]->physicalDevice; |
| 145 | VkInstance instance = physDeviceMap[physicalDevice]->instance; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 146 | VkQueue queue = deviceMap[device]->queue; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 147 | int width = imageMap[image1]->imageExtent.width; |
| 148 | int height = imageMap[image1]->imageExtent.height; |
| 149 | VkFormat format = imageMap[image1]->format; |
Courtney Goeltzenleuchter | 908e767 | 2015-10-21 17:00:51 -0600 | [diff] [blame] | 150 | const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0}; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 151 | VkSubresourceLayout sr_layout; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 152 | const VkImageCreateInfo imgCreateInfo = { |
| 153 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 154 | NULL, |
Chia-I Wu | 439c077 | 2015-10-26 17:00:46 +0800 | [diff] [blame] | 155 | 0, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 156 | VK_IMAGE_TYPE_2D, |
| 157 | format, |
| 158 | {width, height, 1}, |
| 159 | 1, |
| 160 | 1, |
| 161 | 1, |
| 162 | VK_IMAGE_TILING_LINEAR, |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 163 | (VK_IMAGE_USAGE_TRANSFER_DST_BIT|VK_IMAGE_USAGE_STORAGE_BIT), |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 164 | }; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 165 | VkMemoryAllocateInfo memAllocInfo = { |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 166 | VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
| 167 | NULL, |
| 168 | 0, // allocationSize, queried later |
Cody Northrop | 488f547 | 2015-09-01 11:47:50 -0600 | [diff] [blame] | 169 | 0 // memoryTypeIndex, queried later |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 170 | }; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 171 | const VkCommandBufferAllocateInfo allocCommandBufferInfo = { |
| 172 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOC_INFO, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 173 | NULL, |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 174 | deviceMap[device]->commandPool, |
| 175 | VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 176 | 1 |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 177 | }; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 178 | const VkCommandBufferBeginInfo commandBufferBeginInfo = { |
| 179 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 180 | NULL, |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 181 | VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 182 | }; |
| 183 | const VkImageCopy imageCopyRegion = { |
Courtney Goeltzenleuchter | 908e767 | 2015-10-21 17:00:51 -0600 | [diff] [blame] | 184 | {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0}, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 185 | {0, 0, 0}, |
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}, |
| 188 | {width, height, 1} |
| 189 | }; |
| 190 | VkMemoryRequirements memRequirements; |
David Pinedo | fb5b538 | 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 | 6829587 | 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 | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 195 | VkLayerInstanceDispatchTable* pInstanceTable; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 196 | VkLayerDispatchTable* pTableCommandBuffer; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 197 | VkPhysicalDeviceMemoryProperties memory_properties; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 198 | |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 199 | if (imageMap.empty() || imageMap.find(image1) == imageMap.end()) |
David Pinedo | fb5b538 | 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 | |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 210 | err = pTableDevice->CreateImage(device, &imgCreateInfo, NULL, &image2); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 211 | assert(!err); |
| 212 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 213 | pTableDevice->GetImageMemoryRequirements(device, image2, &memRequirements); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 214 | |
| 215 | memAllocInfo.allocationSize = memRequirements.size; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 216 | pInstanceTable = instance_dispatch_table(instance); |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 217 | pInstanceTable->GetPhysicalDeviceMemoryProperties(physicalDevice, &memory_properties); |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 218 | |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 219 | pass = memory_type_from_properties(&memory_properties, |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 220 | memRequirements.memoryTypeBits, |
Cody Northrop | 488f547 | 2015-09-01 11:47:50 -0600 | [diff] [blame] | 221 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 222 | &memAllocInfo.memoryTypeIndex); |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 223 | assert(pass); |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 224 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 225 | err = pTableDevice->AllocateMemory(device, &memAllocInfo, NULL, &mem2); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 226 | assert(!err); |
| 227 | |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 228 | err = pTableQueue->BindImageMemory(device, image2, mem2, 0); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 229 | assert(!err); |
| 230 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 231 | err = pTableDevice->AllocateCommandBuffers(device, &allocCommandBufferInfo, &commandBuffer); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 232 | assert(!err); |
| 233 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 234 | screenshot_device_table_map.emplace(commandBuffer, pTableDevice); |
| 235 | pTableCommandBuffer = screenshot_device_table_map[commandBuffer]; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 236 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 237 | err = pTableCommandBuffer->BeginCommandBuffer(commandBuffer, &commandBufferBeginInfo); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 238 | assert(!err); |
| 239 | |
Cody Northrop | 488f547 | 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 |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 241 | pTableCommandBuffer->CmdCopyImage(commandBuffer, image1, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 242 | image2, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &imageCopyRegion); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 243 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 244 | err = pTableCommandBuffer->EndCommandBuffer(commandBuffer); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 245 | assert(!err); |
| 246 | |
Courtney Goeltzenleuchter | 646b907 | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 247 | VkFence nullFence = { VK_NULL_HANDLE }; |
Courtney Goeltzenleuchter | 806c700 | 2015-10-27 11:22:14 -0600 | [diff] [blame] | 248 | VkSubmitInfo submit_info; |
Chia-I Wu | f9be13c | 2015-10-26 20:37:06 +0800 | [diff] [blame] | 249 | submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 250 | submit_info.pNext = NULL; |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 251 | submit_info.waitSemaphoreCount = 0; |
Courtney Goeltzenleuchter | 806c700 | 2015-10-27 11:22:14 -0600 | [diff] [blame] | 252 | submit_info.pWaitSemaphores = NULL; |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 253 | submit_info.commandBufferCount = 1; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 254 | submit_info.pCommandBuffers = &commandBuffer; |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 255 | submit_info.signalSemaphoreCount = 0; |
Courtney Goeltzenleuchter | 806c700 | 2015-10-27 11:22:14 -0600 | [diff] [blame] | 256 | submit_info.pSignalSemaphores = NULL; |
Courtney Goeltzenleuchter | 646b907 | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 257 | |
| 258 | err = pTableQueue->QueueSubmit(queue, 1, &submit_info, nullFence); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 259 | assert(!err); |
| 260 | |
| 261 | err = pTableQueue->QueueWaitIdle(queue); |
| 262 | assert(!err); |
| 263 | |
| 264 | err = pTableDevice->DeviceWaitIdle(device); |
| 265 | assert(!err); |
| 266 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 267 | pTableDevice->GetImageSubresourceLayout(device, image2, &sr, &sr_layout); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 268 | |
| 269 | err = pTableDevice->MapMemory(device, mem2, 0, 0, 0, (void **) &ptr ); |
| 270 | assert(!err); |
| 271 | |
| 272 | ptr += sr_layout.offset; |
| 273 | |
| 274 | ofstream file(filename, ios::binary); |
| 275 | |
| 276 | file << "P6\n"; |
| 277 | file << width << "\n"; |
| 278 | file << height << "\n"; |
| 279 | file << 255 << "\n"; |
| 280 | |
| 281 | for (y = 0; y < height; y++) { |
| 282 | const unsigned int *row = (const unsigned int*) ptr; |
| 283 | if (format == VK_FORMAT_B8G8R8A8_UNORM) |
| 284 | { |
| 285 | for (x = 0; x < width; x++) { |
| 286 | unsigned int swapped; |
| 287 | swapped = (*row & 0xff00ff00) | (*row & 0x000000ff) << 16 | (*row & 0x00ff0000) >> 16; |
| 288 | file.write((char *)&swapped, 3); |
| 289 | row++; |
| 290 | } |
| 291 | } |
| 292 | else if (format == VK_FORMAT_R8G8B8A8_UNORM) |
| 293 | { |
| 294 | for (x = 0; x < width; x++) { |
| 295 | file.write((char *)row, 3); |
| 296 | row++; |
| 297 | } |
| 298 | } |
| 299 | else |
| 300 | { |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 301 | // TODO: add support for additional formats |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 302 | printf("Unrecognized image format\n"); |
| 303 | break; |
| 304 | } |
| 305 | ptr += sr_layout.rowPitch; |
| 306 | } |
| 307 | file.close(); |
| 308 | |
| 309 | // Clean up |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 310 | pTableDevice->UnmapMemory(device, mem2); |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 311 | pTableDevice->FreeMemory(device, mem2, NULL); |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 312 | pTableDevice->FreeCommandBuffers(device, deviceMap[device]->commandPool, 1, &commandBuffer); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 316 | static void createDeviceRegisterExtensions(const VkDeviceCreateInfo* pCreateInfo, VkDevice device) |
| 317 | { |
Cody Northrop | d2ad034 | 2015-08-05 11:15:02 -0600 | [diff] [blame] | 318 | uint32_t i; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 319 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, device); |
Jon Ashburn | 8acd233 | 2015-09-16 18:08:32 -0600 | [diff] [blame] | 320 | PFN_vkGetDeviceProcAddr gpa = pDisp->GetDeviceProcAddr; |
| 321 | pDisp->GetSurfacePropertiesKHR = (PFN_vkGetSurfacePropertiesKHR) gpa(device, "vkGetSurfacePropertiesKHR"); |
| 322 | pDisp->GetSurfaceFormatsKHR = (PFN_vkGetSurfaceFormatsKHR) gpa(device, "vkGetSurfaceFormatsKHR"); |
| 323 | pDisp->GetSurfacePresentModesKHR = (PFN_vkGetSurfacePresentModesKHR) gpa(device, "vkGetSurfacePresentModesKHR"); |
| 324 | pDisp->CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR) gpa(device, "vkCreateSwapchainKHR"); |
| 325 | pDisp->DestroySwapchainKHR = (PFN_vkDestroySwapchainKHR) gpa(device, "vkDestroySwapchainKHR"); |
| 326 | pDisp->GetSwapchainImagesKHR = (PFN_vkGetSwapchainImagesKHR) gpa(device, "vkGetSwapchainImagesKHR"); |
| 327 | pDisp->AcquireNextImageKHR = (PFN_vkAcquireNextImageKHR) gpa(device, "vkAcquireNextImageKHR"); |
| 328 | pDisp->QueuePresentKHR = (PFN_vkQueuePresentKHR) gpa(device, "vkQueuePresentKHR"); |
Ian Elliott | 1064fe3 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 329 | deviceExtMap[pDisp].wsi_enabled = false; |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 330 | for (i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) { |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 331 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_EXT_KHR_DEVICE_SWAPCHAIN_EXTENSION_NAME) == 0) |
Ian Elliott | 1064fe3 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 332 | deviceExtMap[pDisp].wsi_enabled = true; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 336 | VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice( |
| 337 | VkPhysicalDevice gpu, |
| 338 | const VkDeviceCreateInfo *pCreateInfo, |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 339 | const VkAllocationCallbacks* pAllocator, |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 340 | VkDevice *pDevice) |
| 341 | { |
Courtney Goeltzenleuchter | ca173b8 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 342 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, *pDevice); |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 343 | VkResult result = pDisp->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 344 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 345 | if (result == VK_SUCCESS) { |
Jon Ashburn | ec10533 | 2015-07-06 15:09:36 -0600 | [diff] [blame] | 346 | init_screenshot(); |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 347 | createDeviceRegisterExtensions(pCreateInfo, *pDevice); |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 348 | // Create a mapping from a device to a physicalDevice |
| 349 | if (deviceMap[*pDevice] == NULL) |
| 350 | { |
| 351 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 352 | deviceMap[*pDevice] = deviceMapElem; |
| 353 | } |
| 354 | deviceMap[*pDevice]->physicalDevice = gpu; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 355 | } |
| 356 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 357 | return result; |
| 358 | } |
| 359 | |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 360 | VK_LAYER_EXPORT VkResult VKAPI vkEnumeratePhysicalDevices( |
| 361 | VkInstance instance, |
| 362 | uint32_t* pPhysicalDeviceCount, |
| 363 | VkPhysicalDevice* pPhysicalDevices) |
| 364 | { |
| 365 | VkResult result; |
| 366 | |
| 367 | VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance); |
| 368 | result = pTable->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices); |
Tobin Ehlis | be42774 | 2015-09-08 08:59:48 -0600 | [diff] [blame] | 369 | if (result==VK_SUCCESS && *pPhysicalDeviceCount > 0 && pPhysicalDevices) |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 370 | { |
| 371 | for (uint32_t i=0; i<*pPhysicalDeviceCount ; i++) |
| 372 | { |
| 373 | // Create a mapping from a physicalDevice to an instance |
| 374 | if (physDeviceMap[pPhysicalDevices[i]] == NULL) |
| 375 | { |
| 376 | PhysDeviceMapStruct *physDeviceMapElem = new PhysDeviceMapStruct; |
| 377 | physDeviceMap[pPhysicalDevices[i]] = physDeviceMapElem; |
| 378 | } |
| 379 | physDeviceMap[pPhysicalDevices[i]]->instance = instance; |
| 380 | } |
| 381 | } |
| 382 | return result; |
| 383 | } |
| 384 | |
Courtney Goeltzenleuchter | ca173b8 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 385 | /* TODO: Probably need a DestroyDevice as well */ |
| 386 | |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 387 | static const VkLayerProperties ss_device_layers[] = { |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 388 | { |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 389 | "ScreenShot", |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 390 | VK_API_VERSION, |
| 391 | VK_MAKE_VERSION(0, 1, 0), |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 392 | "Layer: ScreenShot", |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 393 | } |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 394 | }; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 395 | |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 396 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties( |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 397 | const char *pLayerName, |
| 398 | uint32_t *pCount, |
| 399 | VkExtensionProperties* pProperties) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 400 | { |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 401 | /* ScreenShot does not have any global extensions */ |
| 402 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 403 | } |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 404 | |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 405 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties( |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 406 | uint32_t *pCount, |
| 407 | VkLayerProperties* pProperties) |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 408 | { |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 409 | /* ScreenShot does not have any global layers */ |
| 410 | return util_GetLayerProperties(0, NULL, |
| 411 | pCount, pProperties); |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 412 | } |
| 413 | |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 414 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties( |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 415 | VkPhysicalDevice physicalDevice, |
| 416 | const char* pLayerName, |
| 417 | uint32_t* pCount, |
| 418 | VkExtensionProperties* pProperties) |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 419 | { |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 420 | /* ScreenShot does not have any physical device extensions */ |
| 421 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
| 422 | } |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 423 | |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 424 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties( |
Courtney Goeltzenleuchter | a6db346 | 2015-07-07 11:25:43 -0600 | [diff] [blame] | 425 | VkPhysicalDevice physicalDevice, |
| 426 | uint32_t* pCount, |
| 427 | VkLayerProperties* pProperties) |
| 428 | { |
| 429 | return util_GetLayerProperties(ARRAY_SIZE(ss_device_layers), |
| 430 | ss_device_layers, |
| 431 | pCount, pProperties); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 432 | } |
| 433 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 434 | VK_LAYER_EXPORT void VKAPI vkGetDeviceQueue( |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 435 | VkDevice device, |
| 436 | uint32_t queueNodeIndex, |
| 437 | uint32_t queueIndex, |
| 438 | VkQueue *pQueue) |
| 439 | { |
| 440 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 441 | 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] | 442 | |
| 443 | loader_platform_thread_lock_mutex(&globalLock); |
| 444 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 445 | // We are all done taking screenshots, so don't do anything else |
| 446 | loader_platform_thread_unlock_mutex(&globalLock); |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 447 | return; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 448 | } |
| 449 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 450 | screenshot_device_table_map.emplace(*pQueue, pTable); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 451 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 452 | // Create a mapping from a device to a queue |
| 453 | if (deviceMap[device] == NULL) |
| 454 | { |
| 455 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 456 | deviceMap[device] = deviceMapElem; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 457 | } |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 458 | deviceMap[device]->queue = *pQueue; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 459 | loader_platform_thread_unlock_mutex(&globalLock); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 460 | } |
| 461 | |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 462 | VK_LAYER_EXPORT VkResult VKAPI vkCreateCommandPool( |
| 463 | VkDevice device, |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 464 | const VkCommandPoolCreateInfo *pCreateInfo, |
| 465 | const VkAllocationCallbacks* pAllocator, |
| 466 | VkCommandPool *pCommandPool) |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 467 | { |
| 468 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 469 | 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] | 470 | |
| 471 | loader_platform_thread_lock_mutex(&globalLock); |
| 472 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 473 | // We are all done taking screenshots, so don't do anything else |
| 474 | loader_platform_thread_unlock_mutex(&globalLock); |
| 475 | return result; |
| 476 | } |
| 477 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 478 | // Create a mapping from a device to a commandPool |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 479 | if (deviceMap[device] == NULL) |
| 480 | { |
| 481 | DeviceMapStruct *deviceMapElem = new DeviceMapStruct; |
| 482 | deviceMap[device] = deviceMapElem; |
| 483 | } |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 484 | deviceMap[device]->commandPool = *pCommandPool; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 485 | loader_platform_thread_unlock_mutex(&globalLock); |
| 486 | return result; |
| 487 | } |
| 488 | |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 489 | VK_LAYER_EXPORT VkResult VKAPI vkCreateSwapchainKHR( |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 490 | VkDevice device, |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 491 | const VkSwapchainCreateInfoKHR *pCreateInfo, |
| 492 | VkSwapchainKHR *pSwapchain) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 493 | { |
| 494 | VkLayerDispatchTable* pTable = screenshot_device_table_map[device]; |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 495 | VkResult result = get_dispatch_table(screenshot_device_table_map, device)->CreateSwapchainKHR(device, pCreateInfo, pSwapchain); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 496 | |
| 497 | loader_platform_thread_lock_mutex(&globalLock); |
| 498 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 499 | // We are all done taking screenshots, so don't do anything else |
| 500 | loader_platform_thread_unlock_mutex(&globalLock); |
| 501 | return result; |
| 502 | } |
| 503 | |
| 504 | if (result == VK_SUCCESS) |
| 505 | { |
| 506 | // Create a mapping for a swapchain to a device, image extent, and format |
| 507 | SwapchainMapStruct *swapchainMapElem = new SwapchainMapStruct; |
| 508 | swapchainMapElem->device = device; |
| 509 | swapchainMapElem->imageExtent = pCreateInfo->imageExtent; |
| 510 | swapchainMapElem->format = pCreateInfo->imageFormat; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 511 | swapchainMap.insert(make_pair(*pSwapchain, swapchainMapElem)); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 512 | |
| 513 | // Create a mapping for the swapchain object into the dispatch table |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 514 | screenshot_device_table_map.emplace((void *)pSwapchain, pTable); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 515 | } |
| 516 | loader_platform_thread_unlock_mutex(&globalLock); |
| 517 | |
| 518 | return result; |
| 519 | } |
| 520 | |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 521 | VK_LAYER_EXPORT VkResult VKAPI vkGetSwapchainImagesKHR( |
Ian Elliott | 1064fe3 | 2015-07-06 14:31:32 -0600 | [diff] [blame] | 522 | VkDevice device, |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 523 | VkSwapchainKHR swapchain, |
Ian Elliott | 2b6b68a | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 524 | uint32_t* pCount, |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 525 | VkImage* pSwapchainImages) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 526 | { |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 527 | 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] | 528 | |
| 529 | loader_platform_thread_lock_mutex(&globalLock); |
| 530 | if (screenshotEnvQueried && screenshotFrames.empty()) { |
| 531 | // We are all done taking screenshots, so don't do anything else |
| 532 | loader_platform_thread_unlock_mutex(&globalLock); |
| 533 | return result; |
| 534 | } |
| 535 | |
| 536 | if (result == VK_SUCCESS && |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 537 | pSwapchainImages && |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 538 | !swapchainMap.empty() && swapchainMap.find(swapchain) != swapchainMap.end()) |
Ian Elliott | 2b6b68a | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 539 | { |
Cody Northrop | bf06582 | 2015-09-01 11:48:13 -0600 | [diff] [blame] | 540 | unsigned i; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 541 | |
Ian Elliott | 2b6b68a | 2015-08-07 14:11:14 -0600 | [diff] [blame] | 542 | for (i=0; i<*pCount; i++) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 543 | { |
| 544 | // 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] | 545 | if (imageMap[pSwapchainImages[i]] == NULL) |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 546 | { |
| 547 | ImageMapStruct *imageMapElem = new ImageMapStruct; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 548 | imageMap[pSwapchainImages[i]] = imageMapElem; |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 549 | } |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 550 | imageMap[pSwapchainImages[i]]->device = swapchainMap[swapchain]->device; |
| 551 | imageMap[pSwapchainImages[i]]->imageExtent = swapchainMap[swapchain]->imageExtent; |
| 552 | imageMap[pSwapchainImages[i]]->format = swapchainMap[swapchain]->format; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 553 | } |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 554 | |
| 555 | // Add list of images to swapchain to image map |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 556 | SwapchainMapStruct *swapchainMapElem = swapchainMap[swapchain]; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 557 | if (i >= 1 && swapchainMapElem) |
| 558 | { |
| 559 | VkImage *imageList = new VkImage[i]; |
| 560 | swapchainMapElem->imageList = imageList; |
Cody Northrop | bf06582 | 2015-09-01 11:48:13 -0600 | [diff] [blame] | 561 | for (unsigned j=0; j<i; j++) |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 562 | { |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 563 | swapchainMapElem->imageList[j] = pSwapchainImages[j]; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 567 | } |
| 568 | loader_platform_thread_unlock_mutex(&globalLock); |
| 569 | return result; |
| 570 | } |
| 571 | |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 572 | VK_LAYER_EXPORT VkResult VKAPI vkQueuePresentKHR(VkQueue queue, VkPresentInfoKHR* pPresentInfo) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 573 | { |
| 574 | static int frameNumber = 0; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 575 | if (frameNumber == 10) {fflush(stdout); /* *((int*)0)=0; */ } |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 576 | 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] | 577 | |
| 578 | loader_platform_thread_lock_mutex(&globalLock); |
| 579 | |
| 580 | if (!screenshotEnvQueried) |
| 581 | { |
| 582 | const char *_vk_screenshot = getenv("_VK_SCREENSHOT"); |
| 583 | if (_vk_screenshot && *_vk_screenshot) |
| 584 | { |
| 585 | string spec(_vk_screenshot), word; |
| 586 | size_t start = 0, comma = 0; |
| 587 | |
| 588 | while (start < spec.size()) { |
| 589 | int frameToAdd; |
| 590 | comma = spec.find(',', start); |
| 591 | if (comma == string::npos) |
| 592 | word = string(spec, start); |
| 593 | else |
| 594 | word = string(spec, start, comma - start); |
| 595 | frameToAdd=atoi(word.c_str()); |
| 596 | // Add the frame number to list, but only do it if the word started with a digit and if |
| 597 | // it's not already in the list |
| 598 | if (*(word.c_str()) >= '0' && *(word.c_str()) <= '9' && |
| 599 | find(screenshotFrames.begin(), screenshotFrames.end(), frameToAdd) == screenshotFrames.end()) |
| 600 | { |
| 601 | screenshotFrames.push_back(frameToAdd); |
| 602 | } |
| 603 | if (comma == string::npos) |
| 604 | break; |
| 605 | start = comma + 1; |
| 606 | } |
| 607 | } |
| 608 | screenshotEnvQueried = true; |
| 609 | } |
| 610 | |
| 611 | |
| 612 | if (result == VK_SUCCESS && !screenshotFrames.empty()) |
| 613 | { |
| 614 | vector<int>::iterator it; |
| 615 | it = find(screenshotFrames.begin(), screenshotFrames.end(), frameNumber); |
| 616 | if (it != screenshotFrames.end()) |
| 617 | { |
| 618 | string fileName; |
| 619 | fileName = to_string(frameNumber) + ".ppm"; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 620 | |
| 621 | VkImage image; |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 622 | VkSwapchainKHR swapchain; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 623 | // We'll dump only one image: the first |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 624 | swapchain = pPresentInfo->swapchains[0]; |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 625 | image = swapchainMap[swapchain]->imageList[pPresentInfo->imageIndices[0]]; |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 626 | writePPM(fileName.c_str(), image); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 627 | screenshotFrames.erase(it); |
| 628 | |
| 629 | if (screenshotFrames.empty()) |
| 630 | { |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 631 | // Free all our maps since we are done with them. |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 632 | for (auto it = swapchainMap.begin(); it != swapchainMap.end(); it++) |
| 633 | { |
| 634 | SwapchainMapStruct *swapchainMapElem = it->second; |
| 635 | delete swapchainMapElem; |
| 636 | } |
| 637 | for (auto it = imageMap.begin(); it != imageMap.end(); it++) |
| 638 | { |
| 639 | ImageMapStruct *imageMapElem = it->second; |
| 640 | delete imageMapElem; |
| 641 | } |
| 642 | for (auto it = deviceMap.begin(); it != deviceMap.end(); it++) |
| 643 | { |
| 644 | DeviceMapStruct *deviceMapElem = it->second; |
| 645 | delete deviceMapElem; |
| 646 | } |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 647 | for (auto it = physDeviceMap.begin(); it != physDeviceMap.end(); it++) |
| 648 | { |
| 649 | PhysDeviceMapStruct *physDeviceMapElem = it->second; |
| 650 | delete physDeviceMapElem; |
| 651 | } |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 652 | swapchainMap.clear(); |
| 653 | imageMap.clear(); |
| 654 | deviceMap.clear(); |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 655 | physDeviceMap.clear(); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | } |
| 659 | frameNumber++; |
| 660 | loader_platform_thread_unlock_mutex(&globalLock); |
| 661 | return result; |
| 662 | } |
| 663 | |
Courtney Goeltzenleuchter | 2d3ba63 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 664 | VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr( |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 665 | VkDevice dev, |
| 666 | const char *funcName) |
| 667 | { |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 668 | if (dev == NULL) { |
| 669 | return NULL; |
| 670 | } |
| 671 | |
| 672 | /* loader uses this to force layer initialization; device object is wrapped */ |
| 673 | if (!strcmp(funcName, "vkGetDeviceProcAddr")) { |
| 674 | initDeviceTable(screenshot_device_table_map, (const VkBaseLayerObject *) dev); |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 675 | return (PFN_vkVoidFunction)vkGetDeviceProcAddr; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 676 | } |
Courtney Goeltzenleuchter | ca173b8 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 677 | if (!strcmp(funcName, "vkCreateDevice")) |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 678 | return (PFN_vkVoidFunction) vkCreateDevice; |
Courtney Goeltzenleuchter | ca173b8 | 2015-06-25 18:01:43 -0600 | [diff] [blame] | 679 | |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 680 | if (!strcmp(funcName, "vkGetDeviceQueue")) |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 681 | return (PFN_vkVoidFunction) vkGetDeviceQueue; |
| 682 | |
| 683 | if (!strcmp(funcName, "vkCreateCommandPool")) |
| 684 | return (PFN_vkVoidFunction) vkCreateCommandPool; |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 685 | |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 686 | VkLayerDispatchTable *pDisp = get_dispatch_table(screenshot_device_table_map, dev); |
Jon Ashburn | 8acd233 | 2015-09-16 18:08:32 -0600 | [diff] [blame] | 687 | if (deviceExtMap.size() != 0 && deviceExtMap[pDisp].wsi_enabled) |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 688 | { |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 689 | if (!strcmp(funcName, "vkCreateSwapchainKHR")) |
| 690 | return (PFN_vkVoidFunction) vkCreateSwapchainKHR; |
| 691 | if (!strcmp(funcName, "vkGetSwapchainImagesKHR")) |
| 692 | return (PFN_vkVoidFunction) vkGetSwapchainImagesKHR; |
| 693 | if (!strcmp(funcName, "vkQueuePresentKHR")) |
| 694 | return (PFN_vkVoidFunction) vkQueuePresentKHR; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | if (pDisp->GetDeviceProcAddr == NULL) |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 698 | return NULL; |
David Pinedo | bcd0e79 | 2015-06-19 13:48:18 -0600 | [diff] [blame] | 699 | return pDisp->GetDeviceProcAddr(dev, funcName); |
David Pinedo | fb5b538 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 700 | } |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 701 | |
| 702 | |
Courtney Goeltzenleuchter | 2d3ba63 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 703 | VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName) |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 704 | { |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 705 | if (instance == VK_NULL_HANDLE) { |
| 706 | return NULL; |
| 707 | } |
| 708 | |
| 709 | /* loader uses this to force layer initialization; instance object is wrapped */ |
| 710 | if (!strcmp("vkGetInstanceProcAddr", funcName)) { |
| 711 | initInstanceTable((const VkBaseLayerObject *) instance); |
David Pinedo | 8897e19 | 2015-07-31 10:56:20 -0600 | [diff] [blame] | 712 | return (PFN_vkVoidFunction) vkGetInstanceProcAddr; |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 713 | } |
| 714 | |
Cody Northrop | 49f885c | 2015-09-01 10:18:45 -0600 | [diff] [blame] | 715 | if (!strcmp(funcName, "vkEnumeratePhysicalDevices")) |
| 716 | return (PFN_vkVoidFunction)vkEnumeratePhysicalDevices; |
| 717 | |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 718 | VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(instance); |
| 719 | if (pTable->GetInstanceProcAddr == NULL) |
| 720 | return NULL; |
| 721 | return pTable->GetInstanceProcAddr(instance, funcName); |
David Pinedo | 3831094 | 2015-07-09 16:23:44 -0600 | [diff] [blame] | 722 | } |