-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVulkanSwapchain.cpp
More file actions
231 lines (191 loc) · 7.42 KB
/
VulkanSwapchain.cpp
File metadata and controls
231 lines (191 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "VulkanSwapchain.h"
VkSurfaceFormatKHR chooseSwapSurfaceFormat(
const std::vector<VkSurfaceFormatKHR> &availableFormats)
{
for (const auto &availableFormat : availableFormats)
{
if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
{
return availableFormat;
}
}
throw std::runtime_error("failed to support SRGB colorspace");
// TODO: Can also return other image formats but need to do manual gamma correction
// return availableFormats[0];
}
VkPresentModeKHR chooseSwapPresentMode(
const std::vector<VkPresentModeKHR> &availablePresentModes)
{
for (const auto &availablePresentMode : availablePresentModes)
{
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR)
{
return availablePresentMode;
}
}
return VK_PRESENT_MODE_FIFO_KHR;
}
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities, GLFWwindow *window)
{
if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max())
{
return capabilities.currentExtent;
}
else
{
int width, height;
glfwGetFramebufferSize(window, &width, &height);
VkExtent2D actualExtent{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
actualExtent.width = std::clamp(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
actualExtent.height = std::clamp(actualExtent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
return actualExtent;
}
}
/*
* VulkanSwapchain functions
*/
void VulkanSwapchain::setContext(VkInstance instance, VulkanDevice *vulkanDevice, GLFWwindow *window)
{
m_instance = instance;
m_window = window;
m_vulkanDevice = vulkanDevice;
}
void VulkanSwapchain::createSurface(VkInstance instance, GLFWwindow *window)
{
if (glfwCreateWindowSurface(instance, window, nullptr, &m_surface) != VK_SUCCESS)
{
throw std::runtime_error("failed to create window surface!");
}
}
void VulkanSwapchain::createSwapchain()
{
assert(m_instance);
assert(m_window);
assert(m_vulkanDevice);
assert(m_surface);
// Store the current swapchain handle so we can use it later on to ease up recreation
// VkSwapchainKHR oldSwapchain{m_swapchain};
if (m_swapchain != VK_NULL_HANDLE) {
for (auto imageView : m_imageViews)
{
vkDestroyImageView(m_vulkanDevice->logicalDevice, imageView, nullptr);
}
vkDestroySwapchainKHR(m_vulkanDevice->logicalDevice, m_swapchain, nullptr);
}
SurfaceSupportDetails swapChainSupport{querySurfaceSupport(m_vulkanDevice->physicalDevice)};
VkSurfaceFormatKHR surfaceFormat{chooseSwapSurfaceFormat(swapChainSupport.formats)};
VkPresentModeKHR presentMode{chooseSwapPresentMode(swapChainSupport.presentModes)};
VkExtent2D extent{chooseSwapExtent(swapChainSupport.capabilities, m_window)};
uint32_t imageCount{swapChainSupport.capabilities.minImageCount + 1};
if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount)
{
imageCount = swapChainSupport.capabilities.maxImageCount;
}
VkSwapchainCreateInfoKHR createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.surface = m_surface;
createInfo.minImageCount = imageCount;
createInfo.imageFormat = surfaceFormat.format;
createInfo.imageColorSpace = surfaceFormat.colorSpace;
createInfo.imageExtent = extent;
createInfo.imageArrayLayers = 1;
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
uint32_t queueFamilyIndices[]{m_vulkanDevice->queueFamilyIndices.graphics.value(),
m_vulkanDevice->queueFamilyIndices.present.value()};
if (queueFamilyIndices[0] != queueFamilyIndices[1])
{
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
createInfo.queueFamilyIndexCount = 2;
createInfo.pQueueFamilyIndices = queueFamilyIndices;
}
else
{
// If same queue families, use exclusive mode since concurrent mode
// needs at least 2 distinct queue families Requires ownership
// transfer for exclusive mode, if used for >2 distinct queue
// families
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
}
createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createInfo.presentMode = presentMode;
createInfo.clipped = VK_TRUE;
// Setting oldSwapchain to the saved handle of previous swapchain aids in resource reuse
// and makes sure that we can still present already acquired images
// createInfo.oldSwapchain = oldSwapchain;
if (vkCreateSwapchainKHR(m_vulkanDevice->logicalDevice, &createInfo, nullptr, &m_swapchain) != VK_SUCCESS)
{
throw std::runtime_error("failed to create swap chain!");
}
// If an existing swapchain is re-created, destroy the old swapchain.
// This also cleans up all presentable images.
/*if (oldSwapchain != VK_NULL_HANDLE)
{
for (auto imageView : m_imageViews)
{
vkDestroyImageView(m_vulkanDevice->logicalDevice, imageView, nullptr);
}
vkDestroySwapchainKHR(m_vulkanDevice->logicalDevice, oldSwapchain, nullptr);
}*/
// Get number of presentable images first, since we only specified minImageCount earlier on, there could be more presentable images
vkGetSwapchainImagesKHR(m_vulkanDevice->logicalDevice, m_swapchain, &imageCount, nullptr);
m_images.resize(imageCount);
vkGetSwapchainImagesKHR(m_vulkanDevice->logicalDevice, m_swapchain, &imageCount, m_images.data());
m_format = surfaceFormat.format;
m_extent = extent;
m_imageViews.resize(imageCount);
for (uint32_t i{0}; i < imageCount; ++i)
{
VkImageViewCreateInfo viewInfo{};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = m_images[i];
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = m_format;
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
if (vkCreateImageView(m_vulkanDevice->logicalDevice, &viewInfo, nullptr, &m_imageViews[i]) != VK_SUCCESS)
{
throw std::runtime_error("failed to create image view!");
}
}
}
SurfaceSupportDetails VulkanSwapchain::querySurfaceSupport(VkPhysicalDevice physicalDevice)
{
SurfaceSupportDetails details{};
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, m_surface, &details.capabilities);
uint32_t formatCount{};
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, m_surface, &formatCount, nullptr);
if (formatCount != 0)
{
details.formats.resize(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, m_surface, &formatCount, details.formats.data());
}
else
{
throw std::runtime_error("failed to find any supported image formats by surface");
}
uint32_t presentModeCount{};
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, m_surface, &presentModeCount, nullptr);
if (presentModeCount != 0)
{
details.presentModes.resize(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, m_surface, &presentModeCount, details.presentModes.data());
}
else
{
throw std::runtime_error("failed to find any supported presentation modes by surface");
}
return details;
}
void VulkanSwapchain::cleanup()
{
for (auto imageView : m_imageViews)
{
vkDestroyImageView(m_vulkanDevice->logicalDevice, imageView, nullptr);
}
vkDestroySwapchainKHR(m_vulkanDevice->logicalDevice, m_swapchain, nullptr);
vkDestroySurfaceKHR(m_instance, m_surface, nullptr);
}