Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 1 | /* |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 2 | * |
Courtney Goeltzenleuchter | fcbe16f | 2015-10-29 13:50:34 -0600 | [diff] [blame] | 3 | * Copyright (C) 2015 Valve Corporation |
Michael Lentine | 03107b4 | 2015-12-11 10:49:51 -0800 | [diff] [blame] | 4 | * Copyright (C) 2015 Google Inc. |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 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 | * |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 24 | * Author: Ian Elliott <ian@lunarg.com> |
| 25 | * |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #ifndef SWAPCHAIN_H |
| 29 | #define SWAPCHAIN_H |
| 30 | |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 31 | #include "vulkan/vk_layer.h" |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 32 | #include "vulkan/vk_ext_debug_report.h" |
Tobin Ehlis | 711ff31 | 2015-10-29 12:58:13 -0600 | [diff] [blame] | 33 | #include "vk_layer_config.h" |
| 34 | #include "vk_layer_logging.h" |
Courtney Goeltzenleuchter | cf60e0a | 2015-10-08 17:07:25 -0600 | [diff] [blame] | 35 | #include <vector> |
Tobin Ehlis | 711ff31 | 2015-10-29 12:58:13 -0600 | [diff] [blame] | 36 | #include <unordered_map> |
Courtney Goeltzenleuchter | cf60e0a | 2015-10-08 17:07:25 -0600 | [diff] [blame] | 37 | |
Ian Elliott | d8c5db1 | 2015-10-07 11:32:31 -0600 | [diff] [blame] | 38 | static const VkLayerProperties globalLayerProps[] = { |
| 39 | { |
Michael Lentine | 03107b4 | 2015-12-11 10:49:51 -0800 | [diff] [blame] | 40 | "VK_LAYER_LUNARG_swapchain", |
Ian Elliott | d8c5db1 | 2015-10-07 11:32:31 -0600 | [diff] [blame] | 41 | VK_API_VERSION, // specVersion |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 42 | VK_MAKE_VERSION(0, 1, 0), // implementationVersion |
Mark Lobodzinski | 0d054fe | 2015-12-30 08:16:12 -0700 | [diff] [blame^] | 43 | "layer: swapchain", |
Ian Elliott | d8c5db1 | 2015-10-07 11:32:31 -0600 | [diff] [blame] | 44 | } |
| 45 | }; |
| 46 | |
| 47 | static const VkLayerProperties deviceLayerProps[] = { |
| 48 | { |
Michael Lentine | 03107b4 | 2015-12-11 10:49:51 -0800 | [diff] [blame] | 49 | "VK_LAYER_LUNARG_swapchain", |
Ian Elliott | d8c5db1 | 2015-10-07 11:32:31 -0600 | [diff] [blame] | 50 | VK_API_VERSION, // specVersion |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 51 | VK_MAKE_VERSION(0, 1, 0), // implementationVersion |
Mark Lobodzinski | 0d054fe | 2015-12-30 08:16:12 -0700 | [diff] [blame^] | 52 | "layer: swapchain", |
Ian Elliott | d8c5db1 | 2015-10-07 11:32:31 -0600 | [diff] [blame] | 53 | } |
| 54 | }; |
| 55 | |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 56 | |
| 57 | using namespace std; |
| 58 | |
Ian Elliott | b0f474c | 2015-09-25 15:50:55 -0600 | [diff] [blame] | 59 | |
| 60 | // Swapchain ERROR codes |
| 61 | typedef enum _SWAPCHAIN_ERROR |
| 62 | { |
| 63 | SWAPCHAIN_INVALID_HANDLE, // Handle used that isn't currently valid |
| 64 | SWAPCHAIN_EXT_NOT_ENABLED_BUT_USED, // Did not enable WSI extension, but called WSI function |
| 65 | SWAPCHAIN_DEL_DEVICE_BEFORE_SWAPCHAINS, // Called vkDestroyDevice() before vkDestroySwapchainKHR() |
Ian Elliott | 1dcd109 | 2015-11-17 17:29:40 -0700 | [diff] [blame] | 66 | SWAPCHAIN_CREATE_SWAP_WITHOUT_QUERY, // Called vkCreateSwapchainKHR() without calling a query (e.g. vkGetPhysicalDeviceSurfaceCapabilitiesKHR()) |
Ian Elliott | b0f474c | 2015-09-25 15:50:55 -0600 | [diff] [blame] | 67 | SWAPCHAIN_CREATE_SWAP_BAD_MIN_IMG_COUNT, // Called vkCreateSwapchainKHR() with out-of-bounds minImageCount |
| 68 | SWAPCHAIN_CREATE_SWAP_OUT_OF_BOUNDS_EXTENTS,// Called vkCreateSwapchainKHR() with out-of-bounds imageExtent |
| 69 | SWAPCHAIN_CREATE_SWAP_EXTENTS_NO_MATCH_WIN, // Called vkCreateSwapchainKHR() with imageExtent that doesn't match window's extent |
| 70 | SWAPCHAIN_CREATE_SWAP_BAD_PRE_TRANSFORM, // Called vkCreateSwapchainKHR() with a non-supported preTransform |
| 71 | SWAPCHAIN_CREATE_SWAP_BAD_IMG_ARRAY_SIZE, // Called vkCreateSwapchainKHR() with a non-supported imageArraySize |
| 72 | SWAPCHAIN_CREATE_SWAP_BAD_IMG_USAGE_FLAGS, // Called vkCreateSwapchainKHR() with a non-supported imageUsageFlags |
| 73 | SWAPCHAIN_CREATE_SWAP_BAD_IMG_COLOR_SPACE, // Called vkCreateSwapchainKHR() with a non-supported imageColorSpace |
| 74 | SWAPCHAIN_CREATE_SWAP_BAD_IMG_FORMAT, // Called vkCreateSwapchainKHR() with a non-supported imageFormat |
| 75 | SWAPCHAIN_CREATE_SWAP_BAD_IMG_FMT_CLR_SP, // Called vkCreateSwapchainKHR() with a non-supported imageColorSpace |
| 76 | SWAPCHAIN_CREATE_SWAP_BAD_PRESENT_MODE, // Called vkCreateSwapchainKHR() with a non-supported presentMode |
| 77 | SWAPCHAIN_DESTROY_SWAP_DIFF_DEVICE, // Called vkDestroySwapchainKHR() with a different VkDevice than vkCreateSwapchainKHR() |
| 78 | SWAPCHAIN_APP_OWNS_TOO_MANY_IMAGES, // vkAcquireNextImageKHR() asked for more images than are available |
| 79 | SWAPCHAIN_INDEX_TOO_LARGE, // Index is too large for swapchain |
Ian Elliott | b0f474c | 2015-09-25 15:50:55 -0600 | [diff] [blame] | 80 | SWAPCHAIN_INDEX_NOT_IN_USE, // vkQueuePresentKHR() given index that is not owned by app |
| 81 | } SWAPCHAIN_ERROR; |
| 82 | |
| 83 | |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 84 | // The following is for logging error messages: |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 85 | #define LAYER_NAME (char *) "Swapchain" |
| 86 | #define LOG_ERROR_NON_VALID_OBJ(objType, type, obj) \ |
Ian Elliott | 68124ac | 2015-10-07 16:18:35 -0600 | [diff] [blame] | 87 | (my_data) ? \ |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 88 | log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (objType), \ |
Ian Elliott | 68124ac | 2015-10-07 16:18:35 -0600 | [diff] [blame] | 89 | (uint64_t) (obj), 0, SWAPCHAIN_INVALID_HANDLE, LAYER_NAME, \ |
| 90 | "%s() called with a non-valid %s.", __FUNCTION__, (obj)) \ |
| 91 | : VK_FALSE |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 92 | |
Ian Elliott | b0f474c | 2015-09-25 15:50:55 -0600 | [diff] [blame] | 93 | #define LOG_ERROR(objType, type, obj, enm, fmt, ...) \ |
Ian Elliott | 68124ac | 2015-10-07 16:18:35 -0600 | [diff] [blame] | 94 | (my_data) ? \ |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 95 | log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (objType), \ |
Ian Elliott | 68124ac | 2015-10-07 16:18:35 -0600 | [diff] [blame] | 96 | (uint64_t) (obj), 0, (enm), LAYER_NAME, (fmt), __VA_ARGS__) \ |
| 97 | : VK_FALSE |
Ian Elliott | b0f474c | 2015-09-25 15:50:55 -0600 | [diff] [blame] | 98 | #define LOG_PERF_WARNING(objType, type, obj, enm, fmt, ...) \ |
Ian Elliott | 68124ac | 2015-10-07 16:18:35 -0600 | [diff] [blame] | 99 | (my_data) ? \ |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 100 | log_msg(my_data->report_data, VK_DEBUG_REPORT_PERF_WARN_BIT_EXT, (objType), \ |
Ian Elliott | 68124ac | 2015-10-07 16:18:35 -0600 | [diff] [blame] | 101 | (uint64_t) (obj), 0, (enm), LAYER_NAME, (fmt), __VA_ARGS__) \ |
| 102 | : VK_FALSE |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 103 | |
| 104 | |
| 105 | // NOTE: The following struct's/typedef's are for keeping track of |
| 106 | // info that is used for validating the WSI extensions. |
| 107 | |
| 108 | // Forward declarations: |
| 109 | struct _SwpInstance; |
| 110 | struct _SwpPhysicalDevice; |
| 111 | struct _SwpDevice; |
| 112 | struct _SwpSwapchain; |
| 113 | struct _SwpImage; |
| 114 | |
| 115 | typedef _SwpInstance SwpInstance; |
| 116 | typedef _SwpPhysicalDevice SwpPhysicalDevice; |
| 117 | typedef _SwpDevice SwpDevice; |
| 118 | typedef _SwpSwapchain SwpSwapchain; |
| 119 | typedef _SwpImage SwpImage; |
| 120 | |
| 121 | // Create one of these for each VkInstance: |
| 122 | struct _SwpInstance { |
| 123 | // The actual handle for this VkInstance: |
| 124 | VkInstance instance; |
| 125 | |
| 126 | // When vkEnumeratePhysicalDevices is called, the VkPhysicalDevice's are |
| 127 | // remembered: |
| 128 | unordered_map<const void*, SwpPhysicalDevice*> physicalDevices; |
| 129 | |
Ian Elliott | 1dcd109 | 2015-11-17 17:29:40 -0700 | [diff] [blame] | 130 | // Set to true if VK_KHR_SURFACE_EXTENSION_NAME was enabled for this VkInstance: |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 131 | bool swapchainExtensionEnabled; |
| 132 | |
| 133 | // TODO: Add additional booleans for platform-specific extensions: |
| 134 | }; |
| 135 | |
| 136 | // Create one of these for each VkPhysicalDevice within a VkInstance: |
| 137 | struct _SwpPhysicalDevice { |
| 138 | // The actual handle for this VkPhysicalDevice: |
| 139 | VkPhysicalDevice physicalDevice; |
| 140 | |
| 141 | // Corresponding VkDevice (and info) to this VkPhysicalDevice: |
| 142 | SwpDevice *pDevice; |
| 143 | |
| 144 | // VkInstance that this VkPhysicalDevice is associated with: |
| 145 | SwpInstance *pInstance; |
| 146 | |
| 147 | // Which queueFamilyIndices support presenting with WSI swapchains: |
| 148 | unordered_map<uint32_t, VkBool32> queueFamilyIndexSupport; |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 149 | |
| 150 | // TODO: Record/use this info per-surface, not per-device, once a |
| 151 | // non-dispatchable surface object is added to WSI: |
Ian Elliott | 1dcd109 | 2015-11-17 17:29:40 -0700 | [diff] [blame] | 152 | // Results of vkGetPhysicalDeviceSurfaceCapabilitiesKHR(): |
Ian Elliott | 27d39c7 | 2015-11-20 16:39:34 -0700 | [diff] [blame] | 153 | bool gotSurfaceCapabilities; |
| 154 | VkSurfaceCapabilitiesKHR surfaceCapabilities; |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 155 | |
| 156 | // TODO: Record/use this info per-surface, not per-device, once a |
| 157 | // non-dispatchable surface object is added to WSI: |
Ian Elliott | 1dcd109 | 2015-11-17 17:29:40 -0700 | [diff] [blame] | 158 | // Count and VkSurfaceFormatKHR's returned by vkGetPhysicalDeviceSurfaceFormatsKHR(): |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 159 | uint32_t surfaceFormatCount; |
| 160 | VkSurfaceFormatKHR* pSurfaceFormats; |
| 161 | |
| 162 | // TODO: Record/use this info per-surface, not per-device, once a |
| 163 | // non-dispatchable surface object is added to WSI: |
Ian Elliott | 1dcd109 | 2015-11-17 17:29:40 -0700 | [diff] [blame] | 164 | // Count and VkPresentModeKHR's returned by vkGetPhysicalDeviceSurfacePresentModesKHR(): |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 165 | uint32_t presentModeCount; |
| 166 | VkPresentModeKHR* pPresentModes; |
Ian Elliott | 27d39c7 | 2015-11-20 16:39:34 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | // Create one of these for each VkDevice within a VkInstance: |
| 170 | struct _SwpDevice { |
| 171 | // The actual handle for this VkDevice: |
| 172 | VkDevice device; |
| 173 | |
| 174 | // Corresponding VkPhysicalDevice (and info) to this VkDevice: |
| 175 | SwpPhysicalDevice *pPhysicalDevice; |
| 176 | |
| 177 | // Set to true if VK_KHR_SWAPCHAIN_EXTENSION_NAME was enabled: |
| 178 | bool deviceSwapchainExtensionEnabled; |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 179 | |
| 180 | // When vkCreateSwapchainKHR is called, the VkSwapchainKHR's are |
| 181 | // remembered: |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 182 | unordered_map<VkSwapchainKHR, SwpSwapchain*> swapchains; |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | // Create one of these for each VkImage within a VkSwapchainKHR: |
| 186 | struct _SwpImage { |
| 187 | // The actual handle for this VkImage: |
| 188 | VkImage image; |
| 189 | |
| 190 | // Corresponding VkSwapchainKHR (and info) to this VkImage: |
| 191 | SwpSwapchain *pSwapchain; |
| 192 | |
| 193 | // true if application got this image from vkAcquireNextImageKHR(), and |
| 194 | // hasn't yet called vkQueuePresentKHR() for it; otherwise false: |
| 195 | bool ownedByApp; |
| 196 | }; |
| 197 | |
| 198 | // Create one of these for each VkSwapchainKHR within a VkDevice: |
| 199 | struct _SwpSwapchain { |
| 200 | // The actual handle for this VkSwapchainKHR: |
| 201 | VkSwapchainKHR swapchain; |
| 202 | |
| 203 | // Corresponding VkDevice (and info) to this VkSwapchainKHR: |
| 204 | SwpDevice *pDevice; |
| 205 | |
| 206 | // When vkGetSwapchainImagesKHR is called, the VkImage's are |
| 207 | // remembered: |
| 208 | uint32_t imageCount; |
| 209 | unordered_map<int, SwpImage> images; |
| 210 | }; |
| 211 | |
Tobin Ehlis | 711ff31 | 2015-10-29 12:58:13 -0600 | [diff] [blame] | 212 | struct layer_data { |
| 213 | debug_report_data *report_data; |
Courtney Goeltzenleuchter | 7415d5a | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 214 | std::vector<VkDebugReportCallbackEXT> logging_callback; |
Tobin Ehlis | 711ff31 | 2015-10-29 12:58:13 -0600 | [diff] [blame] | 215 | VkLayerDispatchTable* device_dispatch_table; |
| 216 | VkLayerInstanceDispatchTable* instance_dispatch_table; |
| 217 | // NOTE: The following are for keeping track of info that is used for |
| 218 | // validating the WSI extensions. |
| 219 | std::unordered_map<void *, SwpInstance> instanceMap; |
| 220 | std::unordered_map<void *, SwpPhysicalDevice> physicalDeviceMap; |
| 221 | std::unordered_map<void *, SwpDevice> deviceMap; |
| 222 | std::unordered_map<VkSwapchainKHR, SwpSwapchain> swapchainMap; |
| 223 | |
| 224 | layer_data() : |
| 225 | report_data(nullptr), |
| 226 | device_dispatch_table(nullptr), |
| 227 | instance_dispatch_table(nullptr) |
| 228 | {}; |
| 229 | }; |
| 230 | |
Ian Elliott | 0b4d624 | 2015-09-22 10:51:24 -0600 | [diff] [blame] | 231 | #endif // SWAPCHAIN_H |