blob: cb5682c55faa2e9aed99f3594fed4d6dce8f269d [file] [log] [blame]
Ian Elliott0b4d6242015-09-22 10:51:24 -06001/*
Ian Elliott0b4d6242015-09-22 10:51:24 -06002 *
Courtney Goeltzenleuchterfcbe16f2015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Michael Lentine03107b42015-12-11 10:49:51 -08004 * Copyright (C) 2015 Google Inc.
Ian Elliott0b4d6242015-09-22 10:51:24 -06005 *
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 Goeltzenleuchter05559522015-10-30 11:14:30 -060024 * Author: Ian Elliott <ian@lunarg.com>
25 *
Ian Elliott0b4d6242015-09-22 10:51:24 -060026 */
27
28#ifndef SWAPCHAIN_H
29#define SWAPCHAIN_H
30
David Pinedo9316d3b2015-11-06 12:54:48 -070031#include "vulkan/vk_layer.h"
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070032#include "vulkan/vk_ext_debug_report.h"
Tobin Ehlis711ff312015-10-29 12:58:13 -060033#include "vk_layer_config.h"
34#include "vk_layer_logging.h"
Courtney Goeltzenleuchtercf60e0a2015-10-08 17:07:25 -060035#include <vector>
Tobin Ehlis711ff312015-10-29 12:58:13 -060036#include <unordered_map>
Courtney Goeltzenleuchtercf60e0a2015-10-08 17:07:25 -060037
Ian Elliottd8c5db12015-10-07 11:32:31 -060038static const VkLayerProperties globalLayerProps[] = {
39 {
Michael Lentine03107b42015-12-11 10:49:51 -080040 "VK_LAYER_LUNARG_swapchain",
Ian Elliottd8c5db12015-10-07 11:32:31 -060041 VK_API_VERSION, // specVersion
Chia-I Wu3432a0c2015-10-27 18:04:07 +080042 VK_MAKE_VERSION(0, 1, 0), // implementationVersion
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -070043 "layer: swapchain",
Ian Elliottd8c5db12015-10-07 11:32:31 -060044 }
45};
46
47static const VkLayerProperties deviceLayerProps[] = {
48 {
Michael Lentine03107b42015-12-11 10:49:51 -080049 "VK_LAYER_LUNARG_swapchain",
Ian Elliottd8c5db12015-10-07 11:32:31 -060050 VK_API_VERSION, // specVersion
Chia-I Wu3432a0c2015-10-27 18:04:07 +080051 VK_MAKE_VERSION(0, 1, 0), // implementationVersion
Mark Lobodzinski0d054fe2015-12-30 08:16:12 -070052 "layer: swapchain",
Ian Elliottd8c5db12015-10-07 11:32:31 -060053 }
54};
55
Ian Elliott0b4d6242015-09-22 10:51:24 -060056
57using namespace std;
58
Ian Elliottb0f474c2015-09-25 15:50:55 -060059
60// Swapchain ERROR codes
61typedef 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 Elliott1dcd1092015-11-17 17:29:40 -070066 SWAPCHAIN_CREATE_SWAP_WITHOUT_QUERY, // Called vkCreateSwapchainKHR() without calling a query (e.g. vkGetPhysicalDeviceSurfaceCapabilitiesKHR())
Ian Elliottb0f474c2015-09-25 15:50:55 -060067 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
Ian Elliotta2a89c52015-12-28 15:23:57 -070071 SWAPCHAIN_CREATE_SWAP_BAD_COMPOSITE_ALPHA, // Called vkCreateSwapchainKHR() with a non-supported compositeAlpha
Ian Elliottb0f474c2015-09-25 15:50:55 -060072 SWAPCHAIN_CREATE_SWAP_BAD_IMG_ARRAY_SIZE, // Called vkCreateSwapchainKHR() with a non-supported imageArraySize
73 SWAPCHAIN_CREATE_SWAP_BAD_IMG_USAGE_FLAGS, // Called vkCreateSwapchainKHR() with a non-supported imageUsageFlags
74 SWAPCHAIN_CREATE_SWAP_BAD_IMG_COLOR_SPACE, // Called vkCreateSwapchainKHR() with a non-supported imageColorSpace
75 SWAPCHAIN_CREATE_SWAP_BAD_IMG_FORMAT, // Called vkCreateSwapchainKHR() with a non-supported imageFormat
76 SWAPCHAIN_CREATE_SWAP_BAD_IMG_FMT_CLR_SP, // Called vkCreateSwapchainKHR() with a non-supported imageColorSpace
77 SWAPCHAIN_CREATE_SWAP_BAD_PRESENT_MODE, // Called vkCreateSwapchainKHR() with a non-supported presentMode
Ian Elliotta2a89c52015-12-28 15:23:57 -070078 SWAPCHAIN_CREATE_SWAP_BAD_SHARING_MODE, // Called vkCreateSwapchainKHR() with a non-supported imageSharingMode
79 SWAPCHAIN_CREATE_SWAP_BAD_SHARING_VALUES, // Called vkCreateSwapchainKHR() with bad values when imageSharingMode is VK_SHARING_MODE_CONCURRENT
Ian Elliottb0f474c2015-09-25 15:50:55 -060080 SWAPCHAIN_DESTROY_SWAP_DIFF_DEVICE, // Called vkDestroySwapchainKHR() with a different VkDevice than vkCreateSwapchainKHR()
81 SWAPCHAIN_APP_OWNS_TOO_MANY_IMAGES, // vkAcquireNextImageKHR() asked for more images than are available
82 SWAPCHAIN_INDEX_TOO_LARGE, // Index is too large for swapchain
Ian Elliottb0f474c2015-09-25 15:50:55 -060083 SWAPCHAIN_INDEX_NOT_IN_USE, // vkQueuePresentKHR() given index that is not owned by app
Ian Elliotta2a89c52015-12-28 15:23:57 -070084 SWAPCHAIN_BAD_BOOL, // VkBool32 that doesn't have value of VK_TRUE or VK_FALSE (e.g. is a non-zero form of true)
Ian Elliottb0f474c2015-09-25 15:50:55 -060085} SWAPCHAIN_ERROR;
86
87
Ian Elliott0b4d6242015-09-22 10:51:24 -060088// The following is for logging error messages:
Ian Elliott0b4d6242015-09-22 10:51:24 -060089#define LAYER_NAME (char *) "Swapchain"
90#define LOG_ERROR_NON_VALID_OBJ(objType, type, obj) \
Ian Elliott68124ac2015-10-07 16:18:35 -060091 (my_data) ? \
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070092 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (objType), \
Mark Lobodzinski80e774f2016-01-04 15:54:59 -070093 (uint64_t) (obj), __LINE__, SWAPCHAIN_INVALID_HANDLE, LAYER_NAME, \
Ian Elliott68124ac2015-10-07 16:18:35 -060094 "%s() called with a non-valid %s.", __FUNCTION__, (obj)) \
95 : VK_FALSE
Ian Elliott0b4d6242015-09-22 10:51:24 -060096
Ian Elliottb0f474c2015-09-25 15:50:55 -060097#define LOG_ERROR(objType, type, obj, enm, fmt, ...) \
Ian Elliott68124ac2015-10-07 16:18:35 -060098 (my_data) ? \
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070099 log_msg(my_data->report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (objType), \
Mark Lobodzinski80e774f2016-01-04 15:54:59 -0700100 (uint64_t) (obj), __LINE__, (enm), LAYER_NAME, (fmt), __VA_ARGS__) \
Ian Elliott68124ac2015-10-07 16:18:35 -0600101 : VK_FALSE
Ian Elliottb0f474c2015-09-25 15:50:55 -0600102#define LOG_PERF_WARNING(objType, type, obj, enm, fmt, ...) \
Ian Elliott68124ac2015-10-07 16:18:35 -0600103 (my_data) ? \
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700104 log_msg(my_data->report_data, VK_DEBUG_REPORT_PERF_WARN_BIT_EXT, (objType), \
Mark Lobodzinski80e774f2016-01-04 15:54:59 -0700105 (uint64_t) (obj), __LINE__, (enm), LAYER_NAME, (fmt), __VA_ARGS__) \
Ian Elliott68124ac2015-10-07 16:18:35 -0600106 : VK_FALSE
Ian Elliott0b4d6242015-09-22 10:51:24 -0600107
108
109// NOTE: The following struct's/typedef's are for keeping track of
110// info that is used for validating the WSI extensions.
111
112// Forward declarations:
113struct _SwpInstance;
Ian Elliotta983e9a2015-12-22 12:18:12 -0700114struct _SwpSurface;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600115struct _SwpPhysicalDevice;
116struct _SwpDevice;
117struct _SwpSwapchain;
118struct _SwpImage;
119
120typedef _SwpInstance SwpInstance;
Ian Elliotta983e9a2015-12-22 12:18:12 -0700121typedef _SwpSurface SwpSurface;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600122typedef _SwpPhysicalDevice SwpPhysicalDevice;
123typedef _SwpDevice SwpDevice;
124typedef _SwpSwapchain SwpSwapchain;
125typedef _SwpImage SwpImage;
126
127// Create one of these for each VkInstance:
128struct _SwpInstance {
129 // The actual handle for this VkInstance:
130 VkInstance instance;
131
Ian Elliotta983e9a2015-12-22 12:18:12 -0700132 // Remember the VkSurfaceKHR's that are created for this VkInstance:
133 unordered_map<const void*, SwpSurface*> surfaces;
134
Ian Elliott0b4d6242015-09-22 10:51:24 -0600135 // When vkEnumeratePhysicalDevices is called, the VkPhysicalDevice's are
136 // remembered:
137 unordered_map<const void*, SwpPhysicalDevice*> physicalDevices;
138
Ian Elliott1dcd1092015-11-17 17:29:40 -0700139 // Set to true if VK_KHR_SURFACE_EXTENSION_NAME was enabled for this VkInstance:
Ian Elliott1cb77a62015-12-29 16:44:39 -0700140 bool surfaceExtensionEnabled;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600141
142 // TODO: Add additional booleans for platform-specific extensions:
143};
144
Ian Elliotta983e9a2015-12-22 12:18:12 -0700145// Create one of these for each VkSurfaceKHR:
146struct _SwpSurface {
147 // The actual handle for this VkSurfaceKHR:
148 VkSurfaceKHR surface;
149
150 // VkInstance that this VkSurfaceKHR is associated with:
151 SwpInstance *pInstance;
152
153 // Which platform this VkSurfaceKHR is associated with:
154 VkIcdWsiPlatform platform;
155
156 // TODO: Add additional platform-specific info:
157};
158
Ian Elliott0b4d6242015-09-22 10:51:24 -0600159// Create one of these for each VkPhysicalDevice within a VkInstance:
160struct _SwpPhysicalDevice {
161 // The actual handle for this VkPhysicalDevice:
162 VkPhysicalDevice physicalDevice;
163
164 // Corresponding VkDevice (and info) to this VkPhysicalDevice:
165 SwpDevice *pDevice;
166
167 // VkInstance that this VkPhysicalDevice is associated with:
168 SwpInstance *pInstance;
169
170 // Which queueFamilyIndices support presenting with WSI swapchains:
171 unordered_map<uint32_t, VkBool32> queueFamilyIndexSupport;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600172
173// TODO: Record/use this info per-surface, not per-device, once a
174// non-dispatchable surface object is added to WSI:
Ian Elliott1dcd1092015-11-17 17:29:40 -0700175 // Results of vkGetPhysicalDeviceSurfaceCapabilitiesKHR():
Ian Elliott27d39c72015-11-20 16:39:34 -0700176 bool gotSurfaceCapabilities;
177 VkSurfaceCapabilitiesKHR surfaceCapabilities;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600178
179// TODO: Record/use this info per-surface, not per-device, once a
180// non-dispatchable surface object is added to WSI:
Ian Elliott1dcd1092015-11-17 17:29:40 -0700181 // Count and VkSurfaceFormatKHR's returned by vkGetPhysicalDeviceSurfaceFormatsKHR():
Ian Elliott0b4d6242015-09-22 10:51:24 -0600182 uint32_t surfaceFormatCount;
183 VkSurfaceFormatKHR* pSurfaceFormats;
184
185// TODO: Record/use this info per-surface, not per-device, once a
186// non-dispatchable surface object is added to WSI:
Ian Elliott1dcd1092015-11-17 17:29:40 -0700187 // Count and VkPresentModeKHR's returned by vkGetPhysicalDeviceSurfacePresentModesKHR():
Ian Elliott0b4d6242015-09-22 10:51:24 -0600188 uint32_t presentModeCount;
189 VkPresentModeKHR* pPresentModes;
Ian Elliott27d39c72015-11-20 16:39:34 -0700190};
191
192// Create one of these for each VkDevice within a VkInstance:
193struct _SwpDevice {
194 // The actual handle for this VkDevice:
195 VkDevice device;
196
197 // Corresponding VkPhysicalDevice (and info) to this VkDevice:
198 SwpPhysicalDevice *pPhysicalDevice;
199
200 // Set to true if VK_KHR_SWAPCHAIN_EXTENSION_NAME was enabled:
Ian Elliott427058f2015-12-29 16:45:49 -0700201 bool swapchainExtensionEnabled;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600202
203 // When vkCreateSwapchainKHR is called, the VkSwapchainKHR's are
204 // remembered:
Chia-I Wue2fc5522015-10-26 20:04:44 +0800205 unordered_map<VkSwapchainKHR, SwpSwapchain*> swapchains;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600206};
207
208// Create one of these for each VkImage within a VkSwapchainKHR:
209struct _SwpImage {
210 // The actual handle for this VkImage:
211 VkImage image;
212
213 // Corresponding VkSwapchainKHR (and info) to this VkImage:
214 SwpSwapchain *pSwapchain;
215
216 // true if application got this image from vkAcquireNextImageKHR(), and
217 // hasn't yet called vkQueuePresentKHR() for it; otherwise false:
218 bool ownedByApp;
219};
220
221// Create one of these for each VkSwapchainKHR within a VkDevice:
222struct _SwpSwapchain {
223 // The actual handle for this VkSwapchainKHR:
224 VkSwapchainKHR swapchain;
225
226 // Corresponding VkDevice (and info) to this VkSwapchainKHR:
227 SwpDevice *pDevice;
228
229 // When vkGetSwapchainImagesKHR is called, the VkImage's are
230 // remembered:
231 uint32_t imageCount;
232 unordered_map<int, SwpImage> images;
233};
234
Tobin Ehlis711ff312015-10-29 12:58:13 -0600235struct layer_data {
236 debug_report_data *report_data;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700237 std::vector<VkDebugReportCallbackEXT> logging_callback;
Tobin Ehlis711ff312015-10-29 12:58:13 -0600238 VkLayerDispatchTable* device_dispatch_table;
239 VkLayerInstanceDispatchTable* instance_dispatch_table;
240 // NOTE: The following are for keeping track of info that is used for
241 // validating the WSI extensions.
242 std::unordered_map<void *, SwpInstance> instanceMap;
Ian Elliotta983e9a2015-12-22 12:18:12 -0700243 std::unordered_map<void *, SwpSurface> surfaceMap;
Tobin Ehlis711ff312015-10-29 12:58:13 -0600244 std::unordered_map<void *, SwpPhysicalDevice> physicalDeviceMap;
245 std::unordered_map<void *, SwpDevice> deviceMap;
246 std::unordered_map<VkSwapchainKHR, SwpSwapchain> swapchainMap;
247
248 layer_data() :
249 report_data(nullptr),
250 device_dispatch_table(nullptr),
251 instance_dispatch_table(nullptr)
252 {};
253};
254
Ian Elliott0b4d6242015-09-22 10:51:24 -0600255#endif // SWAPCHAIN_H