blob: 9a336283f7c6e5a8f09463f3e4a9f679cfe1a517 [file] [log] [blame]
Ian Elliott329da012015-09-22 10:51:24 -06001/*
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 * Authors:
25 * Ian Elliott <ian@lunarg.com>
26 */
27
28#ifndef SWAPCHAIN_H
29#define SWAPCHAIN_H
30
31#include <stdio.h>
32#include <string.h>
33#include <unordered_map>
34#include "vk_loader_platform.h"
35#include "vk_layer.h"
36#include "vk_layer_config.h"
37#include "vk_layer_logging.h"
38#include "vk_layer_extension_utils.h"
39
40using namespace std;
41
Ian Elliottf81c2562015-09-25 15:50:55 -060042
43// Swapchain ERROR codes
44typedef enum _SWAPCHAIN_ERROR
45{
46 SWAPCHAIN_INVALID_HANDLE, // Handle used that isn't currently valid
47 SWAPCHAIN_EXT_NOT_ENABLED_BUT_USED, // Did not enable WSI extension, but called WSI function
48 SWAPCHAIN_DEL_DEVICE_BEFORE_SWAPCHAINS, // Called vkDestroyDevice() before vkDestroySwapchainKHR()
49 SWAPCHAIN_CREATE_SWAP_WITHOUT_QUERY, // Called vkCreateSwapchainKHR() without calling a query (e.g. vkGetSurfacePropertiesKHR())
50 SWAPCHAIN_CREATE_SWAP_BAD_MIN_IMG_COUNT, // Called vkCreateSwapchainKHR() with out-of-bounds minImageCount
51 SWAPCHAIN_CREATE_SWAP_OUT_OF_BOUNDS_EXTENTS,// Called vkCreateSwapchainKHR() with out-of-bounds imageExtent
52 SWAPCHAIN_CREATE_SWAP_EXTENTS_NO_MATCH_WIN, // Called vkCreateSwapchainKHR() with imageExtent that doesn't match window's extent
53 SWAPCHAIN_CREATE_SWAP_BAD_PRE_TRANSFORM, // Called vkCreateSwapchainKHR() with a non-supported preTransform
54 SWAPCHAIN_CREATE_SWAP_BAD_IMG_ARRAY_SIZE, // Called vkCreateSwapchainKHR() with a non-supported imageArraySize
55 SWAPCHAIN_CREATE_SWAP_BAD_IMG_USAGE_FLAGS, // Called vkCreateSwapchainKHR() with a non-supported imageUsageFlags
56 SWAPCHAIN_CREATE_SWAP_BAD_IMG_COLOR_SPACE, // Called vkCreateSwapchainKHR() with a non-supported imageColorSpace
57 SWAPCHAIN_CREATE_SWAP_BAD_IMG_FORMAT, // Called vkCreateSwapchainKHR() with a non-supported imageFormat
58 SWAPCHAIN_CREATE_SWAP_BAD_IMG_FMT_CLR_SP, // Called vkCreateSwapchainKHR() with a non-supported imageColorSpace
59 SWAPCHAIN_CREATE_SWAP_BAD_PRESENT_MODE, // Called vkCreateSwapchainKHR() with a non-supported presentMode
60 SWAPCHAIN_DESTROY_SWAP_DIFF_DEVICE, // Called vkDestroySwapchainKHR() with a different VkDevice than vkCreateSwapchainKHR()
61 SWAPCHAIN_APP_OWNS_TOO_MANY_IMAGES, // vkAcquireNextImageKHR() asked for more images than are available
62 SWAPCHAIN_INDEX_TOO_LARGE, // Index is too large for swapchain
Ian Elliottf81c2562015-09-25 15:50:55 -060063 SWAPCHAIN_INDEX_NOT_IN_USE, // vkQueuePresentKHR() given index that is not owned by app
64} SWAPCHAIN_ERROR;
65
66
Ian Elliott329da012015-09-22 10:51:24 -060067// The following is for logging error messages:
Cody Northrop73bb6572015-09-28 15:09:32 -060068struct layer_data {
69 debug_report_data *report_data;
Ian Elliott329da012015-09-22 10:51:24 -060070 VkDbgMsgCallback logging_callback;
Cody Northrop73bb6572015-09-28 15:09:32 -060071
72 layer_data() :
73 report_data(nullptr),
74 logging_callback(nullptr)
75 {};
76};
77
Ian Elliott329da012015-09-22 10:51:24 -060078#define LAYER_NAME (char *) "Swapchain"
79#define LOG_ERROR_NON_VALID_OBJ(objType, type, obj) \
Cody Northrop73bb6572015-09-28 15:09:32 -060080 log_msg(mydata.report_data, VK_DBG_REPORT_ERROR_BIT, (objType), \
Ian Elliottf81c2562015-09-25 15:50:55 -060081 (uint64_t) (obj), 0, SWAPCHAIN_INVALID_HANDLE, LAYER_NAME, \
Ian Elliott329da012015-09-22 10:51:24 -060082 "%s() called with a non-valid %s.", __FUNCTION__, (obj))
83
Ian Elliottf81c2562015-09-25 15:50:55 -060084#define LOG_ERROR(objType, type, obj, enm, fmt, ...) \
Cody Northrop73bb6572015-09-28 15:09:32 -060085 log_msg(mydata.report_data, VK_DBG_REPORT_ERROR_BIT, (objType), \
Ian Elliottf81c2562015-09-25 15:50:55 -060086 (uint64_t) (obj), 0, (enm), LAYER_NAME, (fmt), __VA_ARGS__)
87#define LOG_PERF_WARNING(objType, type, obj, enm, fmt, ...) \
Cody Northrop73bb6572015-09-28 15:09:32 -060088 log_msg(mydata.report_data, VK_DBG_REPORT_PERF_WARN_BIT, (objType), \
Ian Elliottf81c2562015-09-25 15:50:55 -060089 (uint64_t) (obj), 0, (enm), LAYER_NAME, (fmt), __VA_ARGS__)
Ian Elliott329da012015-09-22 10:51:24 -060090
91
92// NOTE: The following struct's/typedef's are for keeping track of
93// info that is used for validating the WSI extensions.
94
95// Forward declarations:
96struct _SwpInstance;
97struct _SwpPhysicalDevice;
98struct _SwpDevice;
99struct _SwpSwapchain;
100struct _SwpImage;
101
102typedef _SwpInstance SwpInstance;
103typedef _SwpPhysicalDevice SwpPhysicalDevice;
104typedef _SwpDevice SwpDevice;
105typedef _SwpSwapchain SwpSwapchain;
106typedef _SwpImage SwpImage;
107
108// Create one of these for each VkInstance:
109struct _SwpInstance {
110 // The actual handle for this VkInstance:
111 VkInstance instance;
112
113 // When vkEnumeratePhysicalDevices is called, the VkPhysicalDevice's are
114 // remembered:
115 unordered_map<const void*, SwpPhysicalDevice*> physicalDevices;
116
117 // Set to true if "VK_EXT_KHR_swapchain" was enabled for this VkInstance:
118 bool swapchainExtensionEnabled;
119
120 // TODO: Add additional booleans for platform-specific extensions:
121};
122
123// Create one of these for each VkPhysicalDevice within a VkInstance:
124struct _SwpPhysicalDevice {
125 // The actual handle for this VkPhysicalDevice:
126 VkPhysicalDevice physicalDevice;
127
128 // Corresponding VkDevice (and info) to this VkPhysicalDevice:
129 SwpDevice *pDevice;
130
131 // VkInstance that this VkPhysicalDevice is associated with:
132 SwpInstance *pInstance;
133
134 // Which queueFamilyIndices support presenting with WSI swapchains:
135 unordered_map<uint32_t, VkBool32> queueFamilyIndexSupport;
136};
137
138// Create one of these for each VkDevice within a VkInstance:
139struct _SwpDevice {
140 // The actual handle for this VkDevice:
141 VkDevice device;
142
143 // Corresponding VkPhysicalDevice (and info) to this VkDevice:
144 SwpPhysicalDevice *pPhysicalDevice;
145
146 // Set to true if "VK_EXT_KHR_device_swapchain" was enabled:
147 bool deviceSwapchainExtensionEnabled;
148
149// TODO: Record/use this info per-surface, not per-device, once a
150// non-dispatchable surface object is added to WSI:
151 // Results of vkGetSurfacePropertiesKHR():
152 bool gotSurfaceProperties;
153 VkSurfacePropertiesKHR surfaceProperties;
154
155// TODO: Record/use this info per-surface, not per-device, once a
156// non-dispatchable surface object is added to WSI:
157 // Count and VkSurfaceFormatKHR's returned by vkGetSurfaceFormatsKHR():
158 uint32_t surfaceFormatCount;
159 VkSurfaceFormatKHR* pSurfaceFormats;
160
161// TODO: Record/use this info per-surface, not per-device, once a
162// non-dispatchable surface object is added to WSI:
163 // Count and VkPresentModeKHR's returned by vkGetSurfacePresentModesKHR():
164 uint32_t presentModeCount;
165 VkPresentModeKHR* pPresentModes;
166
167 // When vkCreateSwapchainKHR is called, the VkSwapchainKHR's are
168 // remembered:
169 unordered_map<uint64_t, SwpSwapchain*> swapchains;
170};
171
172// Create one of these for each VkImage within a VkSwapchainKHR:
173struct _SwpImage {
174 // The actual handle for this VkImage:
175 VkImage image;
176
177 // Corresponding VkSwapchainKHR (and info) to this VkImage:
178 SwpSwapchain *pSwapchain;
179
180 // true if application got this image from vkAcquireNextImageKHR(), and
181 // hasn't yet called vkQueuePresentKHR() for it; otherwise false:
182 bool ownedByApp;
183};
184
185// Create one of these for each VkSwapchainKHR within a VkDevice:
186struct _SwpSwapchain {
187 // The actual handle for this VkSwapchainKHR:
188 VkSwapchainKHR swapchain;
189
190 // Corresponding VkDevice (and info) to this VkSwapchainKHR:
191 SwpDevice *pDevice;
192
193 // When vkGetSwapchainImagesKHR is called, the VkImage's are
194 // remembered:
195 uint32_t imageCount;
196 unordered_map<int, SwpImage> images;
197};
198
199#endif // SWAPCHAIN_H