-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swapchain.cpp
451 lines (393 loc) · 14.2 KB
/
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "Swapchain.h"
namespace MelonRenderer
{
VkCommandBuffer& Swapchain::GetCommandBuffer()
{
return m_commandBuffers[m_imageIndex];
}
VkFramebuffer& Swapchain::GetFramebuffer()
{
return m_framebuffers[m_imageIndex];
}
VkImage Swapchain::GetImage()
{
return m_outputImages[m_imageIndex];
}
void Swapchain::AddAttachment(VkImageView attachment)
{
m_attachments.emplace_back(attachment);
}
VkExtent2D Swapchain::GetExtent()
{
return m_extent;
}
bool Swapchain::AquireNextImage()
{
VkResult result;
for (;;)
{
result = vkWaitForFences(Device::Get().m_device, 1, &m_fences[m_imageIndex], VK_TRUE, 100);
if (result == VK_SUCCESS)
{
break;
}
else if (result == VK_TIMEOUT)
{
continue;
}
Logger::Log("Could not wait for fences before acquiring image.");
return false;
}
result = vkAcquireNextImageKHR(Device::Get().m_device, m_swapchain, UINT64_MAX, m_presentCompleteSemaphores[(m_imageIndex+1)%m_swapchainSize],
nullptr, &m_imageIndex);
if ((result != VK_SUCCESS) && (result != VK_SUBOPTIMAL_KHR))
{
Logger::Log("Could not aquire next image.");
return false;
}
//TODO: if VK_ERROR_OUT_OF_DATE_KHR, swapchain has to be recreated
return true;
}
bool Swapchain::PresentImage(VkFence* drawFence)
{
VkResult result = vkResetFences(Device::Get().m_device, 1, &m_fences[m_imageIndex]);
if (result != VK_SUCCESS)
{
Logger::Log("Could not reset fences before submitting cmd buffer to queue.");
return false;
}
VkPipelineStageFlags pipelineStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
VkSubmitInfo submitInfo[1] = {};
submitInfo[0].pNext = nullptr;
submitInfo[0].sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo[0].waitSemaphoreCount = 1;
submitInfo[0].pWaitSemaphores = &m_presentCompleteSemaphores[m_imageIndex];
submitInfo[0].pWaitDstStageMask = &pipelineStageFlags;
submitInfo[0].commandBufferCount = 1;
const VkCommandBuffer cmd[] = { m_commandBuffers[m_imageIndex] };
submitInfo[0].pCommandBuffers = cmd;
submitInfo[0].signalSemaphoreCount = 1;
submitInfo[0].pSignalSemaphores = &m_renderCompleteSemaphores[m_imageIndex];
result = vkQueueSubmit(Device::Get().m_multipurposeQueue, 1, submitInfo, m_fences[m_imageIndex]);
if (result != VK_SUCCESS)
{
Logger::Log("Could not submit draw queue.");
return false;
}
VkPresentInfoKHR presentInfo;
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.pNext = nullptr;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &m_swapchain;
presentInfo.pImageIndices = &m_imageIndex;
presentInfo.pWaitSemaphores = &m_renderCompleteSemaphores[m_imageIndex];
presentInfo.waitSemaphoreCount = 1;
presentInfo.pResults = nullptr;
//wait for command buffer to be finished, if fence set
if (drawFence != nullptr)
{
result = vkWaitForFences(Device::Get().m_device, 1, drawFence, VK_TRUE, UINT64_MAX);
if (result != VK_SUCCESS)
{
Logger::Log("Could not wait for draw fences.");
return false;
}
}
result = vkQueuePresentKHR(Device::Get().m_multipurposeQueue, &presentInfo);
if (result != VK_SUCCESS)
{
Logger::Log("Could not present the draw queue.");
return false;
}
return true;
}
bool Swapchain::CreateFramebuffers()
{
VkFramebufferCreateInfo framebufferCreateInfo = {};
framebufferCreateInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferCreateInfo.pNext = nullptr;
framebufferCreateInfo.flags = 0;
framebufferCreateInfo.renderPass = *m_renderpass;
framebufferCreateInfo.attachmentCount = static_cast<uint32_t>(m_attachments.size());
framebufferCreateInfo.pAttachments = m_attachments.data();
framebufferCreateInfo.width = m_extent.width;
framebufferCreateInfo.height = m_extent.height;
framebufferCreateInfo.layers = 1;
//the first attachment always is the color output attachment
for (int i = 0; i < m_swapchainSize; i++)
{
m_attachments[0] = m_outputImageViews[i];
VkResult result = vkCreateFramebuffer(Device::Get().m_device, &framebufferCreateInfo, nullptr, &m_framebuffers[i]);
if (result != VK_SUCCESS)
{
Logger::Log("Could not create framebuffer.");
return false;
}
}
return true;
}
bool Swapchain::CreateCommandBufferPool(VkCommandPool& commandPool, VkCommandPoolCreateFlags flags)
{
VkCommandPoolCreateInfo cmdBufferPoolInfo = {};
cmdBufferPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
cmdBufferPoolInfo.pNext = NULL;
cmdBufferPoolInfo.queueFamilyIndex = m_queueFamilyIndex;
cmdBufferPoolInfo.flags = flags;
VkResult result = vkCreateCommandPool(Device::Get().m_device, &cmdBufferPoolInfo, NULL, &commandPool);
if (result != VK_SUCCESS)
{
Logger::Log("Could not create command pool.");
return false;
}
return true;
}
bool Swapchain::CreateCommandBuffer(VkCommandPool& commandPool, VkCommandBuffer& commandBuffer)
{
VkCommandBufferAllocateInfo cmdBufferInfo = {};
cmdBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
cmdBufferInfo.pNext = NULL;
cmdBufferInfo.commandPool = commandPool;
cmdBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmdBufferInfo.commandBufferCount = 1;
VkResult result = vkAllocateCommandBuffers(Device::Get().m_device, &cmdBufferInfo, &commandBuffer);
if (result != VK_SUCCESS)
{
Logger::Log("Could not create command buffer.");
return false;
}
return true;
}
bool Swapchain::CreateCommandPoolsAndBuffers()
{
for (int i = 0; i < m_swapchainSize; i++)
{
CreateCommandBufferPool(m_commandPools[i], VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);
CreateCommandBuffer(m_commandPools[i], m_commandBuffers[i]);
}
return true;
}
bool Swapchain::CreateSemaphores()
{
//TODO: signal first semaphore so that the first frame does not output a validation error because it has no way to be signaled
VkSemaphoreCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
for (int i = 0; i < m_swapchainSize; i++)
{
VkResult result = vkCreateSemaphore(Device::Get().m_device, &info, nullptr, &m_renderCompleteSemaphores[i]);
if (result != VK_SUCCESS)
{
Logger::Log("Could not create render complete semaphore.");
return false;
}
result = vkCreateSemaphore(Device::Get().m_device, &info, nullptr, &m_presentCompleteSemaphores[i]);
if (result != VK_SUCCESS)
{
Logger::Log("Could not create render complete semaphore.");
return false;
}
}
return true;
}
bool Swapchain::CreateFences()
{
VkFenceCreateInfo fenceInfo = {};
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceInfo.pNext = nullptr;
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
for (int i = 0; i < m_swapchainSize; i++)
{
VkResult result = vkCreateFence(Device::Get().m_device, &fenceInfo, nullptr, &m_fences[i]);
if (result != VK_SUCCESS)
{
Logger::Log("Could not create render complete semaphore.");
return false;
}
}
return true;
}
bool Swapchain::CreateSwapchain(VkPhysicalDevice& device, VkRenderPass* renderPass, const OutputSurface& outputSurface, VkExtent2D& extent)
{
m_outputSurface = outputSurface;
m_renderpass = renderPass;
uint32_t desiredNumberOfImages = 2;
if (desiredNumberOfImages < m_outputSurface.capabilites.minImageCount)
{
desiredNumberOfImages = m_outputSurface.capabilites.minImageCount;
Logger::Log("Desired number of images too low, using minimum instead.");
}
if (desiredNumberOfImages > m_outputSurface.capabilites.maxImageCount)
{
desiredNumberOfImages = m_outputSurface.capabilites.maxImageCount;
Logger::Log("Desired number of images too high, using maximum instead.");
}
if (extent.width < m_outputSurface.capabilites.minImageExtent.width)
{
extent.width = m_outputSurface.capabilites.minImageExtent.width;
Logger::Log("Desired width of images too low, using minimum instead.");
}
else if (extent.width > m_outputSurface.capabilites.maxImageExtent.width)
{
extent.width = m_outputSurface.capabilites.minImageExtent.width;
Logger::Log("Desired width of images too high, using maximum instead.");
}
if (extent.height < m_outputSurface.capabilites.minImageExtent.height)
{
extent.height = m_outputSurface.capabilites.minImageExtent.height;
Logger::Log("Desired height of images too low, using minimum instead.");
}
else if (extent.height > m_outputSurface.capabilites.maxImageExtent.height)
{
extent.height = m_outputSurface.capabilites.minImageExtent.height;
Logger::Log("Desired height of images too high, using maximum instead.");
}
uint32_t formatCount = 0;
VkResult result = vkGetPhysicalDeviceSurfaceFormatsKHR(device, m_outputSurface.surface, &formatCount, nullptr);
if (result != VK_SUCCESS)
{
Logger::Log("Could not get number of physical device surface formats.");
return false;
}
std::vector<VkSurfaceFormatKHR> surfaceFormats{ formatCount };
result = vkGetPhysicalDeviceSurfaceFormatsKHR(device, m_outputSurface.surface, &formatCount, &surfaceFormats[0]);
if (result != VK_SUCCESS)
{
Logger::Log("Could not get physical device surface formats.");
return false;
}
VkFormat imageFormat = surfaceFormats[0].format;
for (auto& surfaceFormat : surfaceFormats)
{
if (surfaceFormat.format == VK_FORMAT_R8G8B8A8_UNORM)
{
imageFormat = surfaceFormat.format;
break;
}
}
Device::Get().m_format = imageFormat;
VkColorSpaceKHR imageColorSpace = surfaceFormats[0].colorSpace;
for (auto& surfaceFormat : surfaceFormats)
{
if (surfaceFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
{
imageColorSpace = surfaceFormat.colorSpace;
break;
}
}
//store values for later use
m_extent = extent;
VkSwapchainCreateInfoKHR swapchainCreateInfo = {};
swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchainCreateInfo.pNext = nullptr;
swapchainCreateInfo.surface = m_outputSurface.surface;
swapchainCreateInfo.minImageCount = desiredNumberOfImages;
swapchainCreateInfo.imageFormat = imageFormat;
swapchainCreateInfo.imageColorSpace = imageColorSpace;
swapchainCreateInfo.imageExtent = extent;
swapchainCreateInfo.imageArrayLayers = 1;
swapchainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchainCreateInfo.queueFamilyIndexCount = 0;
swapchainCreateInfo.pQueueFamilyIndices = nullptr;
swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchainCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapchainCreateInfo.presentMode = m_presentMode;
swapchainCreateInfo.clipped = VK_TRUE;
if (m_oldSwapchain != VK_NULL_HANDLE)
swapchainCreateInfo.oldSwapchain = m_oldSwapchain;
else
swapchainCreateInfo.oldSwapchain = VK_NULL_HANDLE;
VkBool32 deviceSupported;
vkGetPhysicalDeviceSurfaceSupportKHR(device, m_queueFamilyIndex, m_outputSurface.surface, &deviceSupported);
if (!deviceSupported)
{
Logger::Log("Presentation surface not supported by physical device.");
return false;
}
result = vkCreateSwapchainKHR(Device::Get().m_device, &swapchainCreateInfo, nullptr, &m_swapchain);
if (result != VK_SUCCESS)
{
Logger::Log("Could not create swapchain.");
return false;
}
// driver may create more images than requested
result = vkGetSwapchainImagesKHR(Device::Get().m_device, m_swapchain, &m_swapchainSize, nullptr);
if (result != VK_SUCCESS)
{
Logger::Log("Could not get number of swapchain images.");
return false;
}
m_outputImages.resize(m_swapchainSize);
m_outputImageViews.resize(m_swapchainSize);
m_framebuffers.resize(m_swapchainSize);
m_commandPools.resize(m_swapchainSize);
m_commandBuffers.resize(m_swapchainSize);
m_renderCompleteSemaphores.resize(m_swapchainSize);
m_presentCompleteSemaphores.resize(m_swapchainSize);
m_fences.resize(m_swapchainSize);
result = vkGetSwapchainImagesKHR(Device::Get().m_device, m_swapchain, &m_swapchainSize, &m_outputImages[0]);
if (result != VK_SUCCESS)
{
Logger::Log("Could not enumerate swapchain images.");
return false;
}
VkImageViewCreateInfo colorImageView = {};
colorImageView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
colorImageView.pNext = NULL;
colorImageView.flags = 0;
//colorImageView.image = m_outputImages[i] //in for loop below
colorImageView.viewType = VK_IMAGE_VIEW_TYPE_2D;
colorImageView.format = imageFormat;
colorImageView.components.r = VK_COMPONENT_SWIZZLE_R;
colorImageView.components.g = VK_COMPONENT_SWIZZLE_G;
colorImageView.components.b = VK_COMPONENT_SWIZZLE_B;
colorImageView.components.a = VK_COMPONENT_SWIZZLE_A;
colorImageView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
colorImageView.subresourceRange.baseMipLevel = 0;
colorImageView.subresourceRange.levelCount = 1;
colorImageView.subresourceRange.baseArrayLayer = 0;
colorImageView.subresourceRange.layerCount = 1;
for (unsigned int i = 0; i < m_swapchainSize; i++)
{
colorImageView.image = m_outputImages[i];
result = vkCreateImageView(Device::Get().m_device, &colorImageView, nullptr, &m_outputImageViews[i]);
if (result != VK_SUCCESS)
{
Logger::Log("Could not create image view.");
return false;
}
}
CreateFramebuffers();
CreateCommandPoolsAndBuffers();
CreateSemaphores();
CreateFences();
return true;
}
bool Swapchain::CleanupSwapchain(bool preserveSwapchain)
{
vkDeviceWaitIdle(Device::Get().m_device);
for (int i = 0; i < m_swapchainSize; i++) {
vkDestroyImageView(Device::Get().m_device, m_outputImageViews[i], nullptr);
vkDestroyImage(Device::Get().m_device, m_outputImages[i], nullptr);
vkDestroyFramebuffer(Device::Get().m_device, m_framebuffers[i], nullptr);
vkFreeCommandBuffers(Device::Get().m_device, m_commandPools[i], 1, &m_commandBuffers[i]);
vkDestroyCommandPool(Device::Get().m_device, m_commandPools[i], nullptr);
vkDestroySemaphore(Device::Get().m_device, m_renderCompleteSemaphores[i], nullptr);
vkDestroySemaphore(Device::Get().m_device, m_presentCompleteSemaphores[i], nullptr);
vkDestroyFence(Device::Get().m_device, m_fences[i], nullptr);
}
if (preserveSwapchain)
{
m_oldSwapchain = m_swapchain;
m_swapchain = VK_NULL_HANDLE;
}
else
{
vkDestroySwapchainKHR(Device::Get().m_device, m_swapchain, nullptr);
}
return true;
}
Swapchain::~Swapchain()
{
CleanupSwapchain(false);
}
}