-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwapchainManager.cpp
187 lines (159 loc) · 6.07 KB
/
SwapchainManager.cpp
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
#include "SwapchainManager.hpp"
SwapchainManager::SwapchainManager(const VkPhysicalDevice& phyDevice, const VkDevice& device,
const std::vector<const char*>& deviceExtensions, const QueueFamilyIndices& indices,
VkSurfaceKHR& surface, uint32_t frameBufferWidth, uint32_t frameBufferHeight,
VkSampleCountFlagBits sampleCount)
:
surface(surface),
physicalDevice(phyDevice),
device(device),
extensions(deviceExtensions),
indices(indices),
framebufferWidth(frameBufferWidth),
framebufferHeight(frameBufferHeight),
sampleCount(sampleCount)
{
create();
depthBuffer = std::make_unique<DepthBuffer>(physicalDevice, this->device, imageExtent,sampleCount);
msaa = std::make_unique<Msaa>(physicalDevice, this->device, sampleCount, imageExtent, imageFormat);
renderpass = std::make_unique<RenderPass>(device, imageFormat, sampleCount, depthBuffer->getDepthFormat());
framebuffer = std::make_unique<Framebuffer>(imageViews, msaa->getImageView(), depthBuffer->getImageView(),
renderpass->getRenderPass(), imageExtent, device);
}
void SwapchainManager::cleanup()
{
framebuffer->cleanup();
msaa->cleanup();
depthBuffer->cleanup();
for (uint32_t i = 0; i < imagesCount; i++) {
vkDestroyImageView(device, imageViews[i], nullptr);
}
vkDestroySwapchainKHR(device, swapChain, nullptr);
renderpass->cleanup();
}
const VkFormat& SwapchainManager::getImageFormat()
{
return imageFormat;
}
const VkExtent2D SwapchainManager::getImageExtent()
{
return imageExtent;
}
const std::vector<VkImageView> SwapchainManager::getImageViews()
{
return imageViews;
}
const VkSwapchainKHR& SwapchainManager::getSwapchain()
{
return swapChain;
}
Msaa& SwapchainManager::getMsaa()
{
return *msaa;
}
DepthBuffer& SwapchainManager::getDepthBuffer()
{
return *depthBuffer;
}
RenderPass& SwapchainManager::getRenderPass()
{
return *renderpass;
}
Framebuffer& SwapchainManager::getFramebuffer()
{
return *framebuffer;
}
void SwapchainManager::create()
{
SwapChainSupportDetails swapchainSupport = querySwapChainSupport(physicalDevice, surface);
VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapchainSupport.formats);
VkPresentModeKHR presentMode = chooseSwapPresentMode(swapchainSupport.presents);
VkExtent2D extent = chooseSwapExtent(swapchainSupport.capabilities);
uint32_t imageCount = swapchainSupport.capabilities.minImageCount + 1;
if (swapchainSupport.capabilities.maxImageCount > 0 && imageCount > swapchainSupport.capabilities.maxImageCount) {
imageCount = swapchainSupport.capabilities.maxImageCount;
}
VkSwapchainCreateInfoKHR swapCreateInfo{};
swapCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapCreateInfo.surface = surface;
swapCreateInfo.minImageCount = imageCount;
swapCreateInfo.imageFormat = surfaceFormat.format;
swapCreateInfo.imageColorSpace = surfaceFormat.colorSpace;
swapCreateInfo.imageExtent = extent;
swapCreateInfo.imageArrayLayers = 1;
swapCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() };
if (indices.graphicsFamily != indices.presentFamily) {
swapCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
swapCreateInfo.queueFamilyIndexCount = 2;
swapCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
}
else {
swapCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
}
swapCreateInfo.preTransform = swapchainSupport.capabilities.currentTransform;
swapCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapCreateInfo.presentMode = presentMode;
swapCreateInfo.clipped = VK_TRUE;
swapCreateInfo.oldSwapchain = VK_NULL_HANDLE;
if (vkCreateSwapchainKHR(device, &swapCreateInfo, nullptr, &swapChain) != VK_SUCCESS) {
throw std::runtime_error("failed to create swapchain");
}
imageFormat = surfaceFormat.format;
imageExtent = extent;
createImages(imagesCount, surfaceFormat);
createImageViews();
}
void SwapchainManager::recreate(uint32_t frameBufferWidth, uint32_t frameBufferHeight)
{
this->framebufferWidth = frameBufferWidth;
this->framebufferHeight = frameBufferHeight;
create();
msaa->create(frameBufferWidth, frameBufferHeight);
depthBuffer->create(frameBufferWidth, frameBufferHeight);
renderpass->create(imageFormat, sampleCount, depthBuffer->getDepthFormat(), device);
framebuffer->create(imageViews, msaa->getImageView(), depthBuffer->getImageView(),
renderpass->getRenderPass(), imageExtent);
}
VkSurfaceFormatKHR SwapchainManager::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;
}
}
return availableFormats[0];
}
VkPresentModeKHR SwapchainManager::chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& avaiablePresentModes)
{
for (const auto& presentMode : avaiablePresentModes) {
if (presentMode == VK_PRESENT_MODE_MAILBOX_KHR)
return VK_PRESENT_MODE_IMMEDIATE_KHR;
}
return VK_PRESENT_MODE_IMMEDIATE_KHR;
}
VkExtent2D SwapchainManager::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities)
{
if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()) {
return capabilities.currentExtent;
}
else {
VkExtent2D actualExtent = { framebufferWidth,framebufferHeight };
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;
}
}
void SwapchainManager::createImages(uint32_t& imageCount, VkSurfaceFormatKHR& surfaceFormat)
{
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
images.resize(imageCount);
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, images.data());
}
void SwapchainManager::createImageViews()
{
imageViews.resize(images.size());
for (size_t i = 0; i < imageViews.size(); i++) {
imageViews[i] = createImageView(images[i], imageFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1, device);
}
}