blob: 98e84ea4ac09035809c3142e3ec256144ebccd7b [file] [log] [blame]
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
Ian Elliott8f78daa2016-01-26 10:51:10 -07004 * Copyright (C) 2015-2016 Google Inc.
Ian Elliott0b4d6242015-09-22 10:51:24 -06005 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
Ian Elliott0b4d6242015-09-22 10:51:24 -06009 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Ian Elliott0b4d6242015-09-22 10:51:24 -060011 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
Ian Elliott0b4d6242015-09-22 10:51:24 -060017 *
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060018 * Author: Ian Elliott <ian@lunarg.com>
Ian Elliott578e7e22016-01-05 14:03:16 -070019 * Author: Ian Elliott <ianelliott@google.com>
Ian Elliott0b4d6242015-09-22 10:51:24 -060020 */
21
22#ifndef SWAPCHAIN_H
23#define SWAPCHAIN_H
24
David Pinedo9316d3b2015-11-06 12:54:48 -070025#include "vulkan/vk_layer.h"
Tobin Ehlis711ff312015-10-29 12:58:13 -060026#include "vk_layer_config.h"
27#include "vk_layer_logging.h"
Courtney Goeltzenleuchtercf60e0a2015-10-08 17:07:25 -060028#include <vector>
Tobin Ehlis711ff312015-10-29 12:58:13 -060029#include <unordered_map>
Courtney Goeltzenleuchtercf60e0a2015-10-08 17:07:25 -060030
Ian Elliottb0f474c2015-09-25 15:50:55 -060031// Swapchain ERROR codes
Mark Lobodzinski3ae55162016-05-19 17:01:48 -060032enum SWAPCHAIN_ERROR {
Jon Ashburn5484e0c2016-03-08 17:48:44 -070033 SWAPCHAIN_INVALID_HANDLE, // Handle used that isn't currently valid
34 SWAPCHAIN_NULL_POINTER, // Pointer set to NULL, instead of being a valid pointer
35 SWAPCHAIN_EXT_NOT_ENABLED_BUT_USED, // Did not enable WSI extension, but called WSI function
36 SWAPCHAIN_DEL_OBJECT_BEFORE_CHILDREN, // Called vkDestroyDevice() before vkDestroySwapchainKHR()
37 SWAPCHAIN_CREATE_UNSUPPORTED_SURFACE, // Called vkCreateSwapchainKHR() with a pCreateInfo->surface that wasn't seen as supported
38 // by vkGetPhysicalDeviceSurfaceSupportKHR for the device
39 SWAPCHAIN_CREATE_SWAP_WITHOUT_QUERY, // Called vkCreateSwapchainKHR() without calling a query (e.g.
40 // vkGetPhysicalDeviceSurfaceCapabilitiesKHR())
Jon Ashburn5484e0c2016-03-08 17:48:44 -070041 SWAPCHAIN_CREATE_SWAP_OUT_OF_BOUNDS_EXTENTS, // Called vkCreateSwapchainKHR() with out-of-bounds imageExtent
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070042 SWAPCHAIN_CREATE_SWAP_EXTENTS_NO_MATCH_WIN, // Called vkCreateSwapchainKHR() with imageExtent that doesn't match window's extent
43 SWAPCHAIN_CREATE_SWAP_BAD_PRE_TRANSFORM, // Called vkCreateSwapchainKHR() with a non-supported preTransform
44 SWAPCHAIN_CREATE_SWAP_BAD_COMPOSITE_ALPHA, // Called vkCreateSwapchainKHR() with a non-supported compositeAlpha
David McFarlande22cfb02016-05-20 18:26:28 -030045 SWAPCHAIN_CREATE_SWAP_BAD_IMG_ARRAY_LAYERS, // Called vkCreateSwapchainKHR() with a non-supported imageArrayLayers
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -070046 SWAPCHAIN_CREATE_SWAP_BAD_IMG_USAGE_FLAGS, // Called vkCreateSwapchainKHR() with a non-supported imageUsageFlags
47 SWAPCHAIN_CREATE_SWAP_BAD_IMG_COLOR_SPACE, // Called vkCreateSwapchainKHR() with a non-supported imageColorSpace
48 SWAPCHAIN_CREATE_SWAP_BAD_IMG_FORMAT, // Called vkCreateSwapchainKHR() with a non-supported imageFormat
49 SWAPCHAIN_CREATE_SWAP_BAD_IMG_FMT_CLR_SP, // Called vkCreateSwapchainKHR() with a non-supported imageColorSpace
50 SWAPCHAIN_CREATE_SWAP_BAD_PRESENT_MODE, // Called vkCreateSwapchainKHR() with a non-supported presentMode
51 SWAPCHAIN_CREATE_SWAP_BAD_SHARING_MODE, // Called vkCreateSwapchainKHR() with a non-supported imageSharingMode
Jon Ashburn5484e0c2016-03-08 17:48:44 -070052 SWAPCHAIN_CREATE_SWAP_BAD_SHARING_VALUES, // Called vkCreateSwapchainKHR() with bad values when imageSharingMode is
53 // VK_SHARING_MODE_CONCURRENT
Jon Ashburn5484e0c2016-03-08 17:48:44 -070054 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 Elliottfdf3ffa2016-05-05 14:06:53 -060055 SWAPCHAIN_PRIOR_COUNT, // Query must be called first to get value of pCount, then called second time
Jon Ashburn5484e0c2016-03-08 17:48:44 -070056 SWAPCHAIN_INVALID_COUNT, // Second time a query called, the pCount value didn't match first time
57 SWAPCHAIN_WRONG_STYPE, // The sType for a struct has the wrong value
58 SWAPCHAIN_WRONG_NEXT, // The pNext for a struct is not NULL
59 SWAPCHAIN_ZERO_VALUE, // A value should be non-zero
Jon Ashburn5484e0c2016-03-08 17:48:44 -070060 SWAPCHAIN_DID_NOT_QUERY_QUEUE_FAMILIES, // A function using a queueFamilyIndex was called before
61 // vkGetPhysicalDeviceQueueFamilyProperties() was called
62 SWAPCHAIN_QUEUE_FAMILY_INDEX_TOO_LARGE, // A queueFamilyIndex value is not less than pQueueFamilyPropertyCount returned by
63 // vkGetPhysicalDeviceQueueFamilyProperties()
64 SWAPCHAIN_SURFACE_NOT_SUPPORTED_WITH_QUEUE, // A surface is not supported by a given queueFamilyIndex, as seen by
65 // vkGetPhysicalDeviceSurfaceSupportKHR()
Petros Bantolas2b40be72016-04-15 11:02:59 +010066 SWAPCHAIN_GET_SUPPORTED_DISPLAYS_WITHOUT_QUERY, // vkGetDisplayPlaneSupportedDisplaysKHR should be called after querying
67 // device display plane properties
68 SWAPCHAIN_PLANE_INDEX_TOO_LARGE, // a planeIndex value is larger than what vkGetDisplayPlaneSupportedDisplaysKHR returns
Mark Lobodzinski3ae55162016-05-19 17:01:48 -060069};
Ian Elliottb0f474c2015-09-25 15:50:55 -060070
Ian Elliott0b4d6242015-09-22 10:51:24 -060071// The following is for logging error messages:
Mark Lobodzinski9b482fa2016-08-08 09:38:42 -060072const char * swapchain_layer_name = "Swapchain";
73
Ian Elliott0b4d6242015-09-22 10:51:24 -060074#define LAYER_NAME (char *) "Swapchain"
Mark Lobodzinski9b482fa2016-08-08 09:38:42 -060075
Ian Elliott0b4d6242015-09-22 10:51:24 -060076// NOTE: The following struct's/typedef's are for keeping track of
77// info that is used for validating the WSI extensions.
78
79// Forward declarations:
Mark Lobodzinski3ae55162016-05-19 17:01:48 -060080struct SwpInstance;
81struct SwpSurface;
82struct SwpPhysicalDevice;
83struct SwpDevice;
84struct SwpSwapchain;
85struct SwpImage;
86struct SwpQueue;
Ian Elliott0b4d6242015-09-22 10:51:24 -060087
88// Create one of these for each VkInstance:
Mark Lobodzinski3ae55162016-05-19 17:01:48 -060089struct SwpInstance {
Ian Elliott0b4d6242015-09-22 10:51:24 -060090 // The actual handle for this VkInstance:
91 VkInstance instance;
92
Ian Elliott1f6bb802016-01-20 16:33:34 -070093 // Remember the VkSurfaceKHR's that are created for this VkInstance:
Jamie Madill6069c822016-12-15 09:35:36 -050094 std::unordered_map<VkSurfaceKHR, SwpSurface *> surfaces;
Ian Elliott1f6bb802016-01-20 16:33:34 -070095
Ian Elliott0b4d6242015-09-22 10:51:24 -060096 // When vkEnumeratePhysicalDevices is called, the VkPhysicalDevice's are
97 // remembered:
Jamie Madill6069c822016-12-15 09:35:36 -050098 std::unordered_map<const void *, SwpPhysicalDevice *> physicalDevices;
Ian Elliott0b4d6242015-09-22 10:51:24 -060099
Petros Bantolas2b40be72016-04-15 11:02:59 +0100100 // Set to true if VK_KHR_DISPLAY_EXTENSION_NAME was enabled for this VkInstance:
101 bool displayExtensionEnabled;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600102};
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700103
Ian Elliott1f6bb802016-01-20 16:33:34 -0700104// Create one of these for each VkSurfaceKHR:
Mark Lobodzinski3ae55162016-05-19 17:01:48 -0600105struct SwpSurface {
Ian Elliott1f6bb802016-01-20 16:33:34 -0700106 // The actual handle for this VkSurfaceKHR:
107 VkSurfaceKHR surface;
108
109 // VkInstance that this VkSurfaceKHR is associated with:
110 SwpInstance *pInstance;
111
112 // When vkCreateSwapchainKHR is called, the VkSwapchainKHR's are
113 // remembered:
Jamie Madill6069c822016-12-15 09:35:36 -0500114 std::unordered_map<VkSwapchainKHR, SwpSwapchain *> swapchains;
Ian Elliott680825b2016-01-21 12:42:19 -0700115
Ian Elliottc4db6952016-01-21 14:29:45 -0700116 // Value of pQueueFamilyPropertyCount that was returned by the
117 // vkGetPhysicalDeviceQueueFamilyProperties() function:
118 uint32_t numQueueFamilyIndexSupport;
119 // Array of VkBool32's that is intialized by the
120 // vkGetPhysicalDeviceSurfaceSupportKHR() function. First call for a given
121 // surface allocates and initializes this array to false for all
122 // queueFamilyIndex's (and sets numQueueFamilyIndexSupport to non-zero).
123 // All calls set the entry for a given queueFamilyIndex:
124 VkBool32 *pQueueFamilyIndexSupport;
Ian Elliott1f6bb802016-01-20 16:33:34 -0700125};
Ian Elliott0b4d6242015-09-22 10:51:24 -0600126
127// Create one of these for each VkPhysicalDevice within a VkInstance:
Mark Lobodzinski3ae55162016-05-19 17:01:48 -0600128struct SwpPhysicalDevice {
Ian Elliott0b4d6242015-09-22 10:51:24 -0600129 // The actual handle for this VkPhysicalDevice:
130 VkPhysicalDevice physicalDevice;
131
132 // Corresponding VkDevice (and info) to this VkPhysicalDevice:
133 SwpDevice *pDevice;
134
135 // VkInstance that this VkPhysicalDevice is associated with:
136 SwpInstance *pInstance;
137
Ian Elliottaeafe232016-01-20 10:50:33 -0700138 // Records results of vkGetPhysicalDeviceQueueFamilyProperties()'s
Ian Elliottc4db6952016-01-21 14:29:45 -0700139 // numOfQueueFamilies parameter when pQueueFamilyProperties is NULL:
Ian Elliottaeafe232016-01-20 10:50:33 -0700140 bool gotQueueFamilyPropertyCount;
Ian Elliottc4db6952016-01-21 14:29:45 -0700141 uint32_t numOfQueueFamilies;
Ian Elliottaeafe232016-01-20 10:50:33 -0700142
Ian Elliottc4db6952016-01-21 14:29:45 -0700143 // Record all surfaces that vkGetPhysicalDeviceSurfaceSupportKHR() was
144 // called for:
Jamie Madill6069c822016-12-15 09:35:36 -0500145 std::unordered_map<VkSurfaceKHR, SwpSurface *> supportedSurfaces;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600146
Petros Bantolas2b40be72016-04-15 11:02:59 +0100147 // Count returned by vkGetPhysicalDeviceDisplayPlanePropertiesKHR():
148 uint32_t displayPlanePropertyCount;
149 bool gotDisplayPlanePropertyCount;
Ian Elliott27d39c72015-11-20 16:39:34 -0700150};
151
152// Create one of these for each VkDevice within a VkInstance:
Mark Lobodzinski3ae55162016-05-19 17:01:48 -0600153struct SwpDevice {
Ian Elliott27d39c72015-11-20 16:39:34 -0700154 // The actual handle for this VkDevice:
155 VkDevice device;
156
157 // Corresponding VkPhysicalDevice (and info) to this VkDevice:
158 SwpPhysicalDevice *pPhysicalDevice;
159
Ian Elliott0b4d6242015-09-22 10:51:24 -0600160 // When vkCreateSwapchainKHR is called, the VkSwapchainKHR's are
161 // remembered:
Jamie Madill6069c822016-12-15 09:35:36 -0500162 std::unordered_map<VkSwapchainKHR, SwpSwapchain *> swapchains;
Ian Elliottc4db6952016-01-21 14:29:45 -0700163
164 // When vkGetDeviceQueue is called, the VkQueue's are remembered:
Jamie Madill6069c822016-12-15 09:35:36 -0500165 std::unordered_map<VkQueue, SwpQueue *> queues;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600166};
167
168// Create one of these for each VkImage within a VkSwapchainKHR:
Mark Lobodzinski3ae55162016-05-19 17:01:48 -0600169struct SwpImage {
Ian Elliott0b4d6242015-09-22 10:51:24 -0600170 // The actual handle for this VkImage:
171 VkImage image;
172
173 // Corresponding VkSwapchainKHR (and info) to this VkImage:
174 SwpSwapchain *pSwapchain;
175
Ian Elliotta5d13a92016-04-07 09:05:45 -0600176 // true if application acquired this image from vkAcquireNextImageKHR(),
177 // and hasn't yet called vkQueuePresentKHR() for it; otherwise false:
178 bool acquiredByApp;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600179};
180
181// Create one of these for each VkSwapchainKHR within a VkDevice:
Mark Lobodzinski3ae55162016-05-19 17:01:48 -0600182struct SwpSwapchain {
Ian Elliott0b4d6242015-09-22 10:51:24 -0600183 // The actual handle for this VkSwapchainKHR:
184 VkSwapchainKHR swapchain;
185
186 // Corresponding VkDevice (and info) to this VkSwapchainKHR:
187 SwpDevice *pDevice;
188
Ian Elliottf7f8ff02015-12-30 14:55:41 -0700189 // Corresponding VkSurfaceKHR to this VkSwapchainKHR:
Ian Elliott1f6bb802016-01-20 16:33:34 -0700190 SwpSurface *pSurface;
Ian Elliottf7f8ff02015-12-30 14:55:41 -0700191
Ian Elliott0b4d6242015-09-22 10:51:24 -0600192 // When vkGetSwapchainImagesKHR is called, the VkImage's are
193 // remembered:
194 uint32_t imageCount;
Ian Elliott0b4d6242015-09-22 10:51:24 -0600195};
196
Ian Elliottc4db6952016-01-21 14:29:45 -0700197// Create one of these for each VkQueue within a VkDevice:
Mark Lobodzinski3ae55162016-05-19 17:01:48 -0600198struct SwpQueue {
Ian Elliottc4db6952016-01-21 14:29:45 -0700199 // The actual handle for this VkQueue:
200 VkQueue queue;
201
202 // Corresponding VkDevice (and info) to this VkSwapchainKHR:
203 SwpDevice *pDevice;
204
205 // Which queueFamilyIndex this VkQueue is associated with:
206 uint32_t queueFamilyIndex;
207};
208
Tobin Ehlis711ff312015-10-29 12:58:13 -0600209struct layer_data {
Chia-I Wua6737532016-04-28 16:04:15 +0800210 VkInstance instance;
211
Tobin Ehlis711ff312015-10-29 12:58:13 -0600212 debug_report_data *report_data;
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700213 std::vector<VkDebugReportCallbackEXT> logging_callback;
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700214 VkLayerDispatchTable *device_dispatch_table;
215 VkLayerInstanceDispatchTable *instance_dispatch_table;
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600216
217 // The following are for keeping track of the temporary callbacks that can
218 // be used in vkCreateInstance and vkDestroyInstance:
219 uint32_t num_tmp_callbacks;
220 VkDebugReportCallbackCreateInfoEXT *tmp_dbg_create_infos;
221 VkDebugReportCallbackEXT *tmp_callbacks;
222
Tobin Ehlis711ff312015-10-29 12:58:13 -0600223 // NOTE: The following are for keeping track of info that is used for
224 // validating the WSI extensions.
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700225 std::unordered_map<void *, SwpInstance> instanceMap;
226 std::unordered_map<VkSurfaceKHR, SwpSurface> surfaceMap;
Tobin Ehlis711ff312015-10-29 12:58:13 -0600227 std::unordered_map<void *, SwpPhysicalDevice> physicalDeviceMap;
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700228 std::unordered_map<void *, SwpDevice> deviceMap;
229 std::unordered_map<VkSwapchainKHR, SwpSwapchain> swapchainMap;
230 std::unordered_map<void *, SwpQueue> queueMap;
Tobin Ehlis711ff312015-10-29 12:58:13 -0600231
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600232 layer_data()
233 : report_data(nullptr), device_dispatch_table(nullptr), instance_dispatch_table(nullptr), num_tmp_callbacks(0),
234 tmp_dbg_create_infos(nullptr), tmp_callbacks(nullptr){};
Tobin Ehlis711ff312015-10-29 12:58:13 -0600235};
236
Ian Elliott0b4d6242015-09-22 10:51:24 -0600237#endif // SWAPCHAIN_H