-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvkc_swapchain.cpp
395 lines (318 loc) · 16.4 KB
/
vkc_swapchain.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "vkc_swapchain.h"
/**
* Initialize with empty fields.
*/
VkcSwapchain::VkcSwapchain()
{
handle = VK_NULL_HANDLE;
renderPass = VK_NULL_HANDLE;
clearValues[0] = {0.8f, 0.8f, 1.0f, 1.0f};
clearValues[1] = {0.0f, 0};
imageCount = 0;
}
/**
* Initialize the swapchain.
*/
VkcSwapchain::VkcSwapchain(VkSurfaceKHR surface, const VkcDevice *device) : VkcSwapchain()
{
createSwapchain(surface, device);
createImages();
createRenderPass();
createFramebuffers();
}
/**
* Initialize the swapchain using existent renderpass.
*/
VkcSwapchain::VkcSwapchain(VkRenderPass renderPass, VkSurfaceKHR surface, const VkcDevice *device) : VkcSwapchain()
{
createSwapchain(surface, device);
createImages();
this->renderPass = renderPass;
createFramebuffers();
}
/**
* Destroy the swapchain.
*/
VkcSwapchain::~VkcSwapchain()
{
if (device->logical != VK_NULL_HANDLE)
{
if (renderPass != VK_NULL_HANDLE)
{
vkDestroyRenderPass(device->logical, renderPass, nullptr);
}
while (framebuffers.size() > 0)
{
if (framebuffers[0] != VK_NULL_HANDLE)
{
vkDestroyFramebuffer(device->logical, framebuffers[0], nullptr);
}
framebuffers.removeFirst();
}
while (colorImages.size() > 0)
{
colorImages[0]->destroy(device);
colorImages.removeFirst();
}
depthStencilImage.destroy(device);
if (handle != VK_NULL_HANDLE)
{
vkDestroySwapchainKHR(device->logical, handle, nullptr);
}
}
}
/**
* Create the swapchain.
*/
void VkcSwapchain::createSwapchain(VkSurfaceKHR surface, const VkcDevice *device)
{
// Fill data fields.
this->device = device;
// Get surface format number.
uint32_t formatCount;
vkGetPhysicalDeviceSurfaceFormatsKHR(device->physical, surface, &formatCount, nullptr);
// Get surface formats.
surfaceFormats.clear();
surfaceFormats.resize(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(device->physical, surface, &formatCount, surfaceFormats.data());
if (surfaceFormats.size() == 1 && surfaceFormats[0].format == VK_FORMAT_UNDEFINED)
{
surfaceFormats[0].format = VK_FORMAT_R8G8B8A8_UNORM;
}
for (int i = 1; i < surfaceFormats.count(); i++)
{
if (surfaceFormats[i].format == VK_FORMAT_R8G8B8A8_UNORM)
{
surfaceFormats.move(i, 0);
break;
}
}
// Get surface capabilities.
VkSurfaceCapabilitiesKHR surfaceCapabilities;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device->physical, surface, &surfaceCapabilities);
extent = surfaceCapabilities.currentExtent;
VkSurfaceTransformFlagBitsKHR preTransform = surfaceCapabilities.currentTransform;
if (preTransform & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
// Get present mode number.
uint32_t modeCount;
vkGetPhysicalDeviceSurfacePresentModesKHR(device->physical, surface, &modeCount, nullptr);
// Get present modes.
QVector<VkPresentModeKHR> presentModes;
presentModes.resize(modeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(device->physical, surface, &modeCount, presentModes.data());
VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
imageCount = 2;
for (int i = 0; i < presentModes.size(); i++)
{
if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR)
{
presentMode = VK_PRESENT_MODE_MAILBOX_KHR;
imageCount = 3;
break;
}
}
if (imageCount < surfaceCapabilities.minImageCount)
{
imageCount = surfaceCapabilities.minImageCount;
}
if (imageCount > surfaceCapabilities.maxImageCount && surfaceCapabilities.maxImageCount != 0)
{
imageCount = surfaceCapabilities.maxImageCount;
}
// Get queue families.
QVector<uint32_t> queueFamilies;
device->getQueueFamilies(queueFamilies);
// Fill swap chain info.
VkSwapchainCreateInfoKHR swapchainInfo =
{
VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkSwapchainCreateFlagsKHR flags;
surface, // VkSurfaceKHR surface;
imageCount, // uint32_t minImageCount;
surfaceFormats[0].format, // VkFormat imageFormat;
surfaceFormats[0].colorSpace, // VkColorSpaceKHR imageColorSpace;
extent, // VkExtent2D imageExtent;
1, // uint32_t imageArrayLayers;
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | // VkImageUsageFlags imageUsage;
VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode imageSharingMode;
(uint32_t)queueFamilies.count(), // uint32_t queueFamilyIndexCount;
queueFamilies.data(), // const uint32_t* pQueueFamilyIndices;
preTransform, // VkSurfaceTransformFlagBitsKHR preTransform;
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, // VkCompositeAlphaFlagBitsKHR compositeAlpha;
presentMode, // VkPresentModeKHR presentMode;
VK_TRUE, // VkBool32 clipped;
VK_NULL_HANDLE // VkSwapchainKHR oldSwapchain;
};
// Create swap chain.
vkCreateSwapchainKHR(device->logical, &swapchainInfo, nullptr, &handle);
}
/**
* Create the swapchain images.
*/
void VkcSwapchain::createImages()
{
// Get actual image number.
vkGetSwapchainImagesKHR(device->logical, handle, &imageCount, nullptr);
// Get images.
QVector<VkImage> images;
images.resize(imageCount);
colorImages.resize(imageCount);
vkGetSwapchainImagesKHR(device->logical, handle, &imageCount, images.data());
// Create color images.
for (uint32_t i = 0; i < imageCount; i++)
{
MgImageInfo colorImageInfo =
{
VK_IMAGE_TYPE_2D, // VkImageType type;
{ // VkExtent3D extent;
extent.width, // uint32_t width;
extent.height, // uint32_t height;
1 // uint32_t depth;
},
surfaceFormats[0].format, // VkFormat format;
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout layout;
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // VkImageUsageFlags usage;
{ // VkImageSubresourceRange resourceRange;
VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags aspectMask;
0, // uint32_t baseMipLevel;
1, // uint32_t levelCount;
0, // uint32_t baseArrayLayer;
1, // uint32_t layerCount;
},
images[i], // const VkImage image;
true, // bool createView;
true // bool createSampler;
};
colorImages[i] = new MgImage();
colorImages[i]->create(device, &colorImageInfo);
}
MgImageInfo depthStencilImageInfo =
{
VK_IMAGE_TYPE_2D, // VkImageType type;
{ // VkExtent3D extent;
extent.width, // uint32_t width;
extent.height, // uint32_t height;
1 // uint32_t depth;
},
VK_FORMAT_D32_SFLOAT_S8_UINT, // VkFormat format;
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // VkImageLayout layout;
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, // VkImageUsageFlags usage;
{ // VkImageSubresourceRange resourceRange;
VK_IMAGE_ASPECT_DEPTH_BIT | // VkImageAspectFlags aspectMask;
VK_IMAGE_ASPECT_STENCIL_BIT,
0, // uint32_t baseMipLevel;
1, // uint32_t levelCount;
0, // uint32_t baseArrayLayer;
1, // uint32_t layerCount;
},
VK_NULL_HANDLE, // const VkImage image;
true, // bool createView;
true // bool createSampler;
};
// Create depth image.
depthStencilImage.create(device, &depthStencilImageInfo);
}
/**
* Creates the render pass.
*/
void VkcSwapchain::createRenderPass()
{
// Fill attachment descriptions.
VkAttachmentDescription attachmentDescription[2] =
{
{
0, // VkAttachmentDescriptionFlags flags;
surfaceFormats[0].format, // VkFormat format;
VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples;
VK_ATTACHMENT_LOAD_OP_CLEAR, // VkAttachmentLoadOp loadOp;
VK_ATTACHMENT_STORE_OP_STORE, // VkAttachmentStoreOp storeOp;
VK_ATTACHMENT_LOAD_OP_CLEAR, // VkAttachmentLoadOp stencilLoadOp;
VK_ATTACHMENT_STORE_OP_STORE, // VkAttachmentStoreOp stencilStoreOp;
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout initialLayout;
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout finalLayout;
},
{
0, // VkAttachmentDescriptionFlags flags;
depthStencilImage.info.format, // VkFormat format;
VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples;
VK_ATTACHMENT_LOAD_OP_CLEAR, // VkAttachmentLoadOp loadOp;
VK_ATTACHMENT_STORE_OP_STORE, // VkAttachmentStoreOp storeOp;
VK_ATTACHMENT_LOAD_OP_CLEAR, // VkAttachmentLoadOp stencilLoadOp;
VK_ATTACHMENT_STORE_OP_STORE, // VkAttachmentStoreOp stencilStoreOp;
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // VkImageLayout initialLayout;
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // VkImageLayout finalLayout;
}
};
// Fill attachment references.
VkAttachmentReference attachmentReference[2] =
{
{
0, // uint32_t attachment;
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL // VkImageLayout layout;
},
{
1, // uint32_t attachment;
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, // VkImageLayout layout;
}
};
// Fill subpass description.
VkSubpassDescription subpassDescription =
{
0, // VkSubpassDescriptionFlags flags;
VK_PIPELINE_BIND_POINT_GRAPHICS, // VkPipelineBindPouint32_t pipelineBindPoint;
0, // uint32_t inputAttachmentCount;
nullptr, // const VkAttachmentReference* pInputAttachments;
1, // uint32_t colorAttachmentCount;
&attachmentReference[0], // const VkAttachmentReference* pColorAttachments;
nullptr, // const VkAttachmentReference* pResolveAttachments;
&attachmentReference[1], // const VkAttachmentReference* pDepthStencilAttachment;
0, // uint32_t preserveAttachmentCount;
nullptr, // const uint32_t* pPreserveAttachments;
};
// Fill render pass create info.
VkRenderPassCreateInfo renderPassInfo =
{
VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkRenderPassCreateFlags flags;
2, // uint32_t attachmentCount;
attachmentDescription, // const VkAttachmentDescription* pAttachments;
1, // uint32_t subpassCount;
&subpassDescription, // const VkSubpassDescription* pSubpasses;
0, // uint32_t dependencyCount;
nullptr // const VkSubpassDependency* pDependencies;
};
// Create render pass.
vkCreateRenderPass(device->logical, &renderPassInfo, nullptr, &renderPass);
}
/**
* Creates the framebuffers.
*/
void VkcSwapchain::createFramebuffers()
{
// Fill framebuffer info.
VkImageView attachments[2];
attachments[1] = depthStencilImage.view;
VkFramebufferCreateInfo framebufferInfo =
{
VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, // VkStructureType sType;
nullptr, // const void* pNext;
0, // VkFramebufferCreateFlags flags;
renderPass, // VkRenderPass renderPass;
2, // uint32_t attachmentCount;
attachments, // const VkImageView* pAttachments;
extent.width, // uint32_t width;
extent.height, // uint32_t height;
1 // uint32_t layers;
};
// Create a framebuffer for each image in the swap chain.
framebuffers.resize(imageCount);
for (uint32_t i = 0; i < imageCount; i++)
{
attachments[0] = colorImages[i]->view;
vkCreateFramebuffer(device->logical, &framebufferInfo, nullptr, &framebuffers[i]);
}
}