/
vk_swapchain.cpp
383 lines (332 loc) · 12.8 KB
/
vk_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
#include "vk_swapchain.h"
#include "vk_objects.h"
#include "c_cvars.h"
#include "version.h"
#include "v_video.h"
#include "vk_framebuffer.h"
CVAR(Bool, vk_hdr, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG);
void I_GetVulkanDrawableSize(int *width, int *height);
VulkanSwapChain::VulkanSwapChain(VulkanDevice *device) : device(device)
{
}
VulkanSwapChain::~VulkanSwapChain()
{
ReleaseResources();
}
uint32_t VulkanSwapChain::AcquireImage(int width, int height, VulkanSemaphore *semaphore, VulkanFence *fence)
{
auto vsync = static_cast<VulkanFrameBuffer*>(screen)->cur_vsync;
if (lastSwapWidth != width || lastSwapHeight != height || lastVsync != vsync || lastHdr != vk_hdr || !swapChain)
{
Recreate();
lastSwapWidth = width;
lastSwapHeight = height;
lastVsync = vsync;
lastHdr = vk_hdr;
}
uint32_t imageIndex;
while (true)
{
if (!swapChain)
{
imageIndex = 0xffffffff;
break;
}
VkResult result = vkAcquireNextImageKHR(device->device, swapChain, 1'000'000'000, semaphore ? semaphore->semaphore : VK_NULL_HANDLE, fence ? fence->fence : VK_NULL_HANDLE, &imageIndex);
if (result == VK_SUCCESS)
{
break;
}
else if (result == VK_SUBOPTIMAL_KHR || result == VK_ERROR_SURFACE_LOST_KHR)
{
// Force the recreate to happen next frame.
// The spec is not very clear about what happens to the semaphore or the acquired image if the swapchain is recreated before the image is released with a call to vkQueuePresentKHR.
lastSwapWidth = 0;
lastSwapHeight = 0;
break;
}
else if (result == VK_ERROR_OUT_OF_DATE_KHR)
{
Recreate();
}
else if (result == VK_NOT_READY || result == VK_TIMEOUT)
{
imageIndex = 0xffffffff;
break;
}
else if (result == VK_ERROR_OUT_OF_HOST_MEMORY || result == VK_ERROR_OUT_OF_DEVICE_MEMORY)
{
VulkanError("vkAcquireNextImageKHR failed: out of memory");
}
else if (result == VK_ERROR_DEVICE_LOST)
{
VulkanError("vkAcquireNextImageKHR failed: device lost");
}
else
{
VulkanError("vkAcquireNextImageKHR failed");
}
}
return imageIndex;
}
void VulkanSwapChain::QueuePresent(uint32_t imageIndex, VulkanSemaphore *semaphore)
{
VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = semaphore ? 1 : 0;
presentInfo.pWaitSemaphores = semaphore ? &semaphore->semaphore : VK_NULL_HANDLE;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &swapChain;
presentInfo.pImageIndices = &imageIndex;
presentInfo.pResults = nullptr;
VkResult result = vkQueuePresentKHR(device->presentQueue, &presentInfo);
if (result == VK_SUBOPTIMAL_KHR || result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_ERROR_SURFACE_LOST_KHR)
{
lastSwapWidth = 0;
lastSwapHeight = 0;
}
else if (result == VK_ERROR_OUT_OF_HOST_MEMORY || result == VK_ERROR_OUT_OF_DEVICE_MEMORY)
{
// The spec says we can recover from this.
// However, if we are out of memory it is better to crash now than in some other weird place further away from the source of the problem.
VulkanError("vkQueuePresentKHR failed: out of memory");
}
else if (result == VK_ERROR_DEVICE_LOST)
{
VulkanError("vkQueuePresentKHR failed: device lost");
}
else if (result != VK_SUCCESS)
{
VulkanError("vkQueuePresentKHR failed");
}
}
void VulkanSwapChain::Recreate()
{
ReleaseViews();
swapChainImages.clear();
VkSwapchainKHR oldSwapChain = swapChain;
CreateSwapChain(oldSwapChain);
if (oldSwapChain)
vkDestroySwapchainKHR(device->device, oldSwapChain, nullptr);
if (swapChain)
{
GetImages();
CreateViews();
}
}
bool VulkanSwapChain::CreateSwapChain(VkSwapchainKHR oldSwapChain)
{
SelectFormat();
SelectPresentMode();
int width, height;
I_GetVulkanDrawableSize(&width, &height);
VkSurfaceCapabilitiesKHR surfaceCapabilities = GetSurfaceCapabilities();
actualExtent = { static_cast<uint32_t>(width), static_cast<uint32_t>(height) };
actualExtent.width = std::max(surfaceCapabilities.minImageExtent.width, std::min(surfaceCapabilities.maxImageExtent.width, actualExtent.width));
actualExtent.height = std::max(surfaceCapabilities.minImageExtent.height, std::min(surfaceCapabilities.maxImageExtent.height, actualExtent.height));
if (actualExtent.width == 0 || actualExtent.height == 0)
{
swapChain = VK_NULL_HANDLE;
return false;
}
uint32_t imageCount = surfaceCapabilities.minImageCount + 1;
if (surfaceCapabilities.maxImageCount > 0 && imageCount > surfaceCapabilities.maxImageCount)
imageCount = surfaceCapabilities.maxImageCount;
// When vsync is on we only want two images. This creates a slight performance penalty in exchange for reduced input latency (less mouse lag).
// When vsync is off we want three images as it allows us to generate new images even during the vertical blanking period where one entry is being used by the presentation engine.
if (swapChainPresentMode == VK_PRESENT_MODE_MAILBOX_KHR || swapChainPresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR)
imageCount = std::min(imageCount, (uint32_t)3);
else
imageCount = std::min(imageCount, (uint32_t)2);
VkSwapchainCreateInfoKHR swapChainCreateInfo = {};
swapChainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapChainCreateInfo.surface = device->surface;
swapChainCreateInfo.minImageCount = imageCount;
swapChainCreateInfo.imageFormat = swapChainFormat.format;
swapChainCreateInfo.imageColorSpace = swapChainFormat.colorSpace;
swapChainCreateInfo.imageExtent = actualExtent;
swapChainCreateInfo.imageArrayLayers = 1;
swapChainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
uint32_t queueFamilyIndices[] = { (uint32_t)device->graphicsFamily, (uint32_t)device->presentFamily };
if (device->graphicsFamily != device->presentFamily)
{
swapChainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
swapChainCreateInfo.queueFamilyIndexCount = 2;
swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
}
else
{
swapChainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapChainCreateInfo.queueFamilyIndexCount = 0;
swapChainCreateInfo.pQueueFamilyIndices = nullptr;
}
swapChainCreateInfo.preTransform = surfaceCapabilities.currentTransform;
swapChainCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; // If alpha channel is passed on to the DWM or not
swapChainCreateInfo.presentMode = swapChainPresentMode;
swapChainCreateInfo.clipped = VK_TRUE;
swapChainCreateInfo.oldSwapchain = oldSwapChain;
VkResult result = vkCreateSwapchainKHR(device->device, &swapChainCreateInfo, nullptr, &swapChain);
if (result != VK_SUCCESS)
{
swapChain = VK_NULL_HANDLE;
return false;
}
return true;
}
void VulkanSwapChain::CreateViews()
{
framebuffers.resize(swapChainImages.size());
swapChainImageViews.reserve(swapChainImages.size());
for (size_t i = 0; i < swapChainImages.size(); i++)
{
device->SetDebugObjectName("SwapChainImage", (uint64_t)swapChainImages[i], VK_OBJECT_TYPE_IMAGE);
VkImageViewCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = swapChainImages[i];
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = swapChainFormat.format;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;
VkImageView view;
VkResult result = vkCreateImageView(device->device, &createInfo, nullptr, &view);
CheckVulkanError(result, "Could not create image view for swapchain image");
device->SetDebugObjectName("SwapChainImageView", (uint64_t)view, VK_OBJECT_TYPE_IMAGE_VIEW);
swapChainImageViews.push_back(view);
}
}
void VulkanSwapChain::SelectFormat()
{
std::vector<VkSurfaceFormatKHR> surfaceFormats = GetSurfaceFormats();
if (surfaceFormats.empty())
VulkanError("No surface formats supported");
if (surfaceFormats.size() == 1 && surfaceFormats.front().format == VK_FORMAT_UNDEFINED)
{
swapChainFormat.format = VK_FORMAT_B8G8R8A8_UNORM;
swapChainFormat.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
return;
}
if (vk_hdr)
{
for (const auto &format : surfaceFormats)
{
if (format.format == VK_FORMAT_R16G16B16A16_SFLOAT && format.colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT)
{
swapChainFormat = format;
return;
}
}
}
for (const auto &format : surfaceFormats)
{
if (format.format == VK_FORMAT_B8G8R8A8_UNORM && format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
{
swapChainFormat = format;
return;
}
}
swapChainFormat = surfaceFormats.front();
}
void VulkanSwapChain::SelectPresentMode()
{
std::vector<VkPresentModeKHR> presentModes = GetPresentModes();
if (presentModes.empty())
VulkanError("No surface present modes supported");
swapChainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
auto vsync = static_cast<VulkanFrameBuffer*>(screen)->cur_vsync;
if (vsync)
{
bool supportsFifoRelaxed = std::find(presentModes.begin(), presentModes.end(), VK_PRESENT_MODE_FIFO_RELAXED_KHR) != presentModes.end();
if (supportsFifoRelaxed)
swapChainPresentMode = VK_PRESENT_MODE_FIFO_RELAXED_KHR;
}
else
{
bool supportsMailbox = std::find(presentModes.begin(), presentModes.end(), VK_PRESENT_MODE_MAILBOX_KHR) != presentModes.end();
bool supportsImmediate = std::find(presentModes.begin(), presentModes.end(), VK_PRESENT_MODE_IMMEDIATE_KHR) != presentModes.end();
if (supportsMailbox)
swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
else if (supportsImmediate)
swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
}
}
void VulkanSwapChain::SetHdrMetadata()
{
if (swapChainFormat.colorSpace != VK_COLOR_SPACE_HDR10_ST2084_EXT)
return;
// Mastering display with HDR10_ST2084 color primaries and D65 white point,
// maximum luminance of 1000 nits and minimum luminance of 0.001 nits;
// content has maximum luminance of 2000 nits and maximum frame average light level (MaxFALL) of 500 nits.
VkHdrMetadataEXT metadata = {};
metadata.sType = VK_STRUCTURE_TYPE_HDR_METADATA_EXT;
metadata.displayPrimaryRed.x = 0.708f;
metadata.displayPrimaryRed.y = 0.292f;
metadata.displayPrimaryGreen.x = 0.170f;
metadata.displayPrimaryGreen.y = 0.797f;
metadata.displayPrimaryBlue.x = 0.131f;
metadata.displayPrimaryBlue.y = 0.046f;
metadata.whitePoint.x = 0.3127f;
metadata.whitePoint.y = 0.3290f;
metadata.maxLuminance = 1000.0f;
metadata.minLuminance = 0.001f;
metadata.maxContentLightLevel = 2000.0f;
metadata.maxFrameAverageLightLevel = 500.0f;
vkSetHdrMetadataEXT(device->device, 1, &swapChain, &metadata);
}
void VulkanSwapChain::GetImages()
{
uint32_t imageCount;
VkResult result = vkGetSwapchainImagesKHR(device->device, swapChain, &imageCount, nullptr);
CheckVulkanError(result, "vkGetSwapchainImagesKHR failed");
swapChainImages.resize(imageCount);
result = vkGetSwapchainImagesKHR(device->device, swapChain, &imageCount, swapChainImages.data());
CheckVulkanError(result, "vkGetSwapchainImagesKHR failed (2)");
}
void VulkanSwapChain::ReleaseViews()
{
framebuffers.clear();
for (auto &view : swapChainImageViews)
{
vkDestroyImageView(device->device, view, nullptr);
}
swapChainImageViews.clear();
}
void VulkanSwapChain::ReleaseResources()
{
ReleaseViews();
if (swapChain)
vkDestroySwapchainKHR(device->device, swapChain, nullptr);
}
VkSurfaceCapabilitiesKHR VulkanSwapChain::GetSurfaceCapabilities()
{
VkSurfaceCapabilitiesKHR surfaceCapabilities;
VkResult result = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device->PhysicalDevice.Device, device->surface, &surfaceCapabilities);
CheckVulkanError(result, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR failed");
return surfaceCapabilities;
}
std::vector<VkSurfaceFormatKHR> VulkanSwapChain::GetSurfaceFormats()
{
uint32_t surfaceFormatCount = 0;
VkResult result = vkGetPhysicalDeviceSurfaceFormatsKHR(device->PhysicalDevice.Device, device->surface, &surfaceFormatCount, nullptr);
CheckVulkanError(result, "vkGetPhysicalDeviceSurfaceFormatsKHR failed");
if (surfaceFormatCount == 0)
return {};
std::vector<VkSurfaceFormatKHR> surfaceFormats(surfaceFormatCount);
result = vkGetPhysicalDeviceSurfaceFormatsKHR(device->PhysicalDevice.Device, device->surface, &surfaceFormatCount, surfaceFormats.data());
CheckVulkanError(result, "vkGetPhysicalDeviceSurfaceFormatsKHR failed");
return surfaceFormats;
}
std::vector<VkPresentModeKHR> VulkanSwapChain::GetPresentModes()
{
uint32_t presentModeCount = 0;
VkResult result = vkGetPhysicalDeviceSurfacePresentModesKHR(device->PhysicalDevice.Device, device->surface, &presentModeCount, nullptr);
CheckVulkanError(result, "vkGetPhysicalDeviceSurfacePresentModesKHR failed");
if (presentModeCount == 0)
return {};
std::vector<VkPresentModeKHR> presentModes(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(device->PhysicalDevice.Device, device->surface, &presentModeCount, presentModes.data());
CheckVulkanError(result, "vkGetPhysicalDeviceSurfacePresentModesKHR failed");
return presentModes;
}