5#include "qvulkanfunctions.h"
7#include <QLoggingCategory>
10#include <QCoreApplication>
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
209
210
211
212
213QVulkanWindow::QVulkanWindow(QWindow *parent)
214 : QWindow(*(
new QVulkanWindowPrivate), parent)
216 setSurfaceType(QSurface::VulkanSurface);
220
221
222QVulkanWindow::~QVulkanWindow()
226QVulkanWindowPrivate::~QVulkanWindowPrivate()
235
236
237
238
239
240
241
242
245
246
247
248
249
250
251void QVulkanWindow::setFlags(Flags flags)
254 if (d->status != QVulkanWindowPrivate::StatusUninitialized) {
255 qWarning(
"QVulkanWindow: Attempted to set flags when already initialized");
262
263
264QVulkanWindow::Flags QVulkanWindow::flags()
const
266 Q_D(
const QVulkanWindow);
271
272
273
274
275QList<VkPhysicalDeviceProperties> QVulkanWindow::availablePhysicalDevices()
278 if (!d->physDevs.isEmpty() && !d->physDevProps.isEmpty())
279 return d->physDevProps;
281 QVulkanInstance *inst = vulkanInstance();
283 qWarning(
"QVulkanWindow: Attempted to call availablePhysicalDevices() without a QVulkanInstance");
284 return d->physDevProps;
287 QVulkanFunctions *f = inst->functions();
289 VkResult err = f->vkEnumeratePhysicalDevices(inst->vkInstance(), &count,
nullptr);
290 if (err != VK_SUCCESS) {
291 qWarning(
"QVulkanWindow: Failed to get physical device count: %d", err);
292 return d->physDevProps;
295 qCDebug(lcGuiVk,
"%d physical devices", count);
297 return d->physDevProps;
299 QList<VkPhysicalDevice> devs(count);
300 err = f->vkEnumeratePhysicalDevices(inst->vkInstance(), &count, devs.data());
301 if (err != VK_SUCCESS) {
302 qWarning(
"QVulkanWindow: Failed to enumerate physical devices: %d", err);
303 return d->physDevProps;
307 d->physDevProps.resize(count);
308 for (uint32_t i = 0; i < count; ++i) {
309 VkPhysicalDeviceProperties *p = &d->physDevProps[i];
310 f->vkGetPhysicalDeviceProperties(d->physDevs.at(i), p);
311 qCDebug(lcGuiVk,
"Physical device [%d]: name '%s' version %d.%d.%d", i, p->deviceName,
312 VK_VERSION_MAJOR(p->driverVersion), VK_VERSION_MINOR(p->driverVersion),
313 VK_VERSION_PATCH(p->driverVersion));
316 return d->physDevProps;
320
321
322
323
324
325
326
327
328
329void QVulkanWindow::setPhysicalDeviceIndex(
int idx)
332 if (d->status != QVulkanWindowPrivate::StatusUninitialized) {
333 qWarning(
"QVulkanWindow: Attempted to set physical device when already initialized");
336 const int count = availablePhysicalDevices().size();
337 if (idx < 0 || idx >= count) {
338 qWarning(
"QVulkanWindow: Invalid physical device index %d (total physical devices: %d)", idx, count);
341 d->physDevIndex = idx;
345
346
347
348
349
350QVulkanInfoVector<QVulkanExtension> QVulkanWindow::supportedDeviceExtensions()
354 availablePhysicalDevices();
356 if (d->physDevs.isEmpty()) {
357 qWarning(
"QVulkanWindow: No physical devices found");
358 return QVulkanInfoVector<QVulkanExtension>();
361 VkPhysicalDevice physDev = d->physDevs.at(d->physDevIndex);
362 if (d->supportedDevExtensions.contains(physDev))
363 return d->supportedDevExtensions.value(physDev);
365 QVulkanFunctions *f = vulkanInstance()->functions();
367 VkResult err = f->vkEnumerateDeviceExtensionProperties(physDev,
nullptr, &count,
nullptr);
368 if (err == VK_SUCCESS) {
369 QList<VkExtensionProperties> extProps(count);
370 err = f->vkEnumerateDeviceExtensionProperties(physDev,
nullptr, &count, extProps.data());
371 if (err == VK_SUCCESS) {
372 QVulkanInfoVector<QVulkanExtension> exts;
373 for (
const VkExtensionProperties &prop : extProps) {
374 QVulkanExtension ext;
375 ext.name = prop.extensionName;
376 ext.version = prop.specVersion;
379 d->supportedDevExtensions.insert(physDev, exts);
380 qCDebug(lcGuiVk) <<
"Supported device extensions:" << exts;
385 qWarning(
"QVulkanWindow: Failed to query device extension count: %d", err);
386 return QVulkanInfoVector<QVulkanExtension>();
390
391
392
393
394
395
396
397
398
399
400
401void QVulkanWindow::setDeviceExtensions(
const QByteArrayList &extensions)
404 if (d->status != QVulkanWindowPrivate::StatusUninitialized) {
405 qWarning(
"QVulkanWindow: Attempted to set device extensions when already initialized");
408 d->requestedDevExtensions = extensions;
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436void QVulkanWindow::setPreferredColorFormats(
const QList<VkFormat> &formats)
439 if (d->status != QVulkanWindowPrivate::StatusUninitialized) {
440 qWarning(
"QVulkanWindow: Attempted to set preferred color format when already initialized");
443 d->requestedColorFormats = formats;
449} q_vk_sampleCounts[] = {
451 { VK_SAMPLE_COUNT_1_BIT, 1 },
452 { VK_SAMPLE_COUNT_2_BIT, 2 },
453 { VK_SAMPLE_COUNT_4_BIT, 4 },
454 { VK_SAMPLE_COUNT_8_BIT, 8 },
455 { VK_SAMPLE_COUNT_16_BIT, 16 },
456 { VK_SAMPLE_COUNT_32_BIT, 32 },
457 { VK_SAMPLE_COUNT_64_BIT, 64 }
461
462
463
464
465
466
467
468
469
470
471
472QList<
int> QVulkanWindow::supportedSampleCounts()
474 Q_D(
const QVulkanWindow);
477 availablePhysicalDevices();
479 if (d->physDevs.isEmpty()) {
480 qWarning(
"QVulkanWindow: No physical devices found");
484 const VkPhysicalDeviceLimits *limits = &d->physDevProps[d->physDevIndex].limits;
485 VkSampleCountFlags color = limits->framebufferColorSampleCounts;
486 VkSampleCountFlags depth = limits->framebufferDepthSampleCounts;
487 VkSampleCountFlags stencil = limits->framebufferStencilSampleCounts;
489 for (
const auto &qvk_sampleCount : q_vk_sampleCounts) {
490 if ((color & qvk_sampleCount.mask)
491 && (depth & qvk_sampleCount.mask)
492 && (stencil & qvk_sampleCount.mask))
494 result.append(qvk_sampleCount.count);
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522void QVulkanWindow::setSampleCount(
int sampleCount)
525 if (d->status != QVulkanWindowPrivate::StatusUninitialized) {
526 qWarning(
"QVulkanWindow: Attempted to set sample count when already initialized");
531 sampleCount = qBound(1, sampleCount, 64);
533 if (!supportedSampleCounts().contains(sampleCount)) {
534 qWarning(
"QVulkanWindow: Attempted to set unsupported sample count %d", sampleCount);
538 for (
const auto &qvk_sampleCount : q_vk_sampleCounts) {
539 if (qvk_sampleCount.count == sampleCount) {
540 d->sampleCount = qvk_sampleCount.mask;
548void QVulkanWindowPrivate::init()
551 Q_ASSERT(status == StatusUninitialized);
553 qCDebug(lcGuiVk,
"QVulkanWindow init");
555 inst = q->vulkanInstance();
557 qWarning(
"QVulkanWindow: Attempted to initialize without a QVulkanInstance");
560 status = StatusFailRetry;
565 renderer = q->createRenderer();
567 surface = QVulkanInstance::surfaceForWindow(q);
568 if (surface == VK_NULL_HANDLE) {
569 qWarning(
"QVulkanWindow: Failed to retrieve Vulkan surface for window");
570 status = StatusFailRetry;
574 q->availablePhysicalDevices();
576 if (physDevs.isEmpty()) {
577 qWarning(
"QVulkanWindow: No physical devices found");
582 if (physDevIndex < 0 || physDevIndex >= physDevs.size()) {
583 qWarning(
"QVulkanWindow: Invalid physical device index; defaulting to 0");
586 qCDebug(lcGuiVk,
"Using physical device [%d]", physDevIndex);
590 renderer->preInitResources();
592 VkPhysicalDevice physDev = physDevs.at(physDevIndex);
593 QVulkanFunctions *f = inst->functions();
595 uint32_t queueCount = 0;
596 f->vkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount,
nullptr);
597 QList<VkQueueFamilyProperties> queueFamilyProps(queueCount);
598 f->vkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueCount, queueFamilyProps.data());
599 gfxQueueFamilyIdx = uint32_t(-1);
600 presQueueFamilyIdx = uint32_t(-1);
601 for (
int i = 0; i < queueFamilyProps.size(); ++i) {
602 const bool supportsPresent = inst->supportsPresent(physDev, i, q);
603 qCDebug(lcGuiVk,
"queue family %d: flags=0x%x count=%d supportsPresent=%d", i,
604 queueFamilyProps[i].queueFlags, queueFamilyProps[i].queueCount, supportsPresent);
605 if (gfxQueueFamilyIdx == uint32_t(-1)
606 && (queueFamilyProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
608 gfxQueueFamilyIdx = i;
610 if (gfxQueueFamilyIdx != uint32_t(-1)) {
611 presQueueFamilyIdx = gfxQueueFamilyIdx;
613 qCDebug(lcGuiVk,
"No queue with graphics+present; trying separate queues");
614 for (
int i = 0; i < queueFamilyProps.size(); ++i) {
615 if (gfxQueueFamilyIdx == uint32_t(-1) && (queueFamilyProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT))
616 gfxQueueFamilyIdx = i;
617 if (presQueueFamilyIdx == uint32_t(-1) && inst->supportsPresent(physDev, i, q))
618 presQueueFamilyIdx = i;
621 if (gfxQueueFamilyIdx == uint32_t(-1)) {
622 qWarning(
"QVulkanWindow: No graphics queue family found");
626 if (presQueueFamilyIdx == uint32_t(-1)) {
627 qWarning(
"QVulkanWindow: No present queue family found");
633 if (qEnvironmentVariableIsSet(
"QT_VK_PRESENT_QUEUE_INDEX"))
634 presQueueFamilyIdx = qEnvironmentVariableIntValue(
"QT_VK_PRESENT_QUEUE_INDEX");
636 qCDebug(lcGuiVk,
"Using queue families: graphics = %u present = %u", gfxQueueFamilyIdx, presQueueFamilyIdx);
638 QList<VkDeviceQueueCreateInfo> queueInfo;
639 queueInfo.reserve(2);
640 const float prio[] = { 0 };
641 VkDeviceQueueCreateInfo addQueueInfo;
642 memset(&addQueueInfo, 0,
sizeof(addQueueInfo));
643 addQueueInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
644 addQueueInfo.queueFamilyIndex = gfxQueueFamilyIdx;
645 addQueueInfo.queueCount = 1;
646 addQueueInfo.pQueuePriorities = prio;
647 queueInfo.append(addQueueInfo);
648 if (gfxQueueFamilyIdx != presQueueFamilyIdx) {
649 addQueueInfo.queueFamilyIndex = presQueueFamilyIdx;
650 addQueueInfo.queueCount = 1;
651 addQueueInfo.pQueuePriorities = prio;
652 queueInfo.append(addQueueInfo);
654 if (queueCreateInfoModifier) {
655 queueCreateInfoModifier(queueFamilyProps.constData(), queueCount, queueInfo);
656 bool foundGfxQueue =
false;
657 bool foundPresQueue =
false;
658 for (
const VkDeviceQueueCreateInfo& createInfo : std::as_const(queueInfo)) {
659 foundGfxQueue |= createInfo.queueFamilyIndex == gfxQueueFamilyIdx;
660 foundPresQueue |= createInfo.queueFamilyIndex == presQueueFamilyIdx;
662 if (!foundGfxQueue) {
663 qWarning(
"QVulkanWindow: Graphics queue missing after call to queueCreateInfoModifier");
667 if (!foundPresQueue) {
668 qWarning(
"QVulkanWindow: Present queue missing after call to queueCreateInfoModifier");
676 QList<
const char *> devExts;
677 QVulkanInfoVector<QVulkanExtension> supportedExtensions = q->supportedDeviceExtensions();
678 QByteArrayList reqExts = requestedDevExtensions;
679 reqExts.append(
"VK_KHR_swapchain");
681 QByteArray envExts = qgetenv(
"QT_VULKAN_DEVICE_EXTENSIONS");
682 if (!envExts.isEmpty()) {
683 QByteArrayList envExtList = envExts.split(
';');
684 for (
auto ext : reqExts)
685 envExtList.removeAll(ext);
686 reqExts.append(envExtList);
689 for (
const QByteArray &ext : reqExts) {
690 if (supportedExtensions.contains(ext))
691 devExts.append(ext.constData());
693 qCDebug(lcGuiVk) <<
"Enabling device extensions:" << devExts;
695 VkDeviceCreateInfo devInfo;
696 memset(&devInfo, 0,
sizeof(devInfo));
697 devInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
698 devInfo.queueCreateInfoCount = queueInfo.size();
699 devInfo.pQueueCreateInfos = queueInfo.constData();
700 devInfo.enabledExtensionCount = devExts.size();
701 devInfo.ppEnabledExtensionNames = devExts.constData();
703 VkPhysicalDeviceFeatures features = {};
704 VkPhysicalDeviceFeatures2 features2 = {};
705 if (enabledFeatures2Modifier) {
706 features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
707 enabledFeatures2Modifier(features2);
708 devInfo.pNext = &features2;
709 }
else if (enabledFeaturesModifier) {
710 enabledFeaturesModifier(features);
711 devInfo.pEnabledFeatures = &features;
715 f->vkGetPhysicalDeviceFeatures(physDev, &features);
716 features.robustBufferAccess = VK_FALSE;
717 devInfo.pEnabledFeatures = &features;
724 uint32_t apiVersion = physDevProps[physDevIndex].apiVersion;
725 if (VK_VERSION_MAJOR(apiVersion) == 1
726 && VK_VERSION_MINOR(apiVersion) == 0
727 && VK_VERSION_PATCH(apiVersion) <= 13)
730 const QByteArray stdValName = QByteArrayLiteral(
"VK_LAYER_KHRONOS_validation");
731 const char *stdValNamePtr = stdValName.constData();
732 if (inst->layers().contains(stdValName)) {
734 VkResult err = f->vkEnumerateDeviceLayerProperties(physDev, &count,
nullptr);
735 if (err == VK_SUCCESS) {
736 QList<VkLayerProperties> layerProps(count);
737 err = f->vkEnumerateDeviceLayerProperties(physDev, &count, layerProps.data());
738 if (err == VK_SUCCESS) {
739 for (
const VkLayerProperties &prop : layerProps) {
740 if (!strncmp(prop.layerName, stdValNamePtr, stdValName.size())) {
741 devInfo.enabledLayerCount = 1;
742 devInfo.ppEnabledLayerNames = &stdValNamePtr;
751 VkResult err = f->vkCreateDevice(physDev, &devInfo,
nullptr, &dev);
752 if (err == VK_ERROR_DEVICE_LOST) {
753 qWarning(
"QVulkanWindow: Physical device lost");
755 renderer->physicalDeviceLost();
758 physDevProps.clear();
759 status = StatusUninitialized;
760 qCDebug(lcGuiVk,
"Attempting to restart in 2 seconds");
761 QTimer::singleShot(2000, q, [
this]() { ensureStarted(); });
764 if (err != VK_SUCCESS) {
765 qWarning(
"QVulkanWindow: Failed to create device: %d", err);
770 devFuncs = inst->deviceFunctions(dev);
773 devFuncs->vkGetDeviceQueue(dev, gfxQueueFamilyIdx, 0, &gfxQueue);
774 if (gfxQueueFamilyIdx == presQueueFamilyIdx)
775 presQueue = gfxQueue;
777 devFuncs->vkGetDeviceQueue(dev, presQueueFamilyIdx, 0, &presQueue);
779 VkCommandPoolCreateInfo poolInfo;
780 memset(&poolInfo, 0,
sizeof(poolInfo));
781 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
782 poolInfo.queueFamilyIndex = gfxQueueFamilyIdx;
783 err = devFuncs->vkCreateCommandPool(dev, &poolInfo,
nullptr, &cmdPool);
784 if (err != VK_SUCCESS) {
785 qWarning(
"QVulkanWindow: Failed to create command pool: %d", err);
789 if (gfxQueueFamilyIdx != presQueueFamilyIdx) {
790 poolInfo.queueFamilyIndex = presQueueFamilyIdx;
791 err = devFuncs->vkCreateCommandPool(dev, &poolInfo,
nullptr, &presCmdPool);
792 if (err != VK_SUCCESS) {
793 qWarning(
"QVulkanWindow: Failed to create command pool for present queue: %d", err);
799 hostVisibleMemIndex = 0;
800 VkPhysicalDeviceMemoryProperties physDevMemProps;
801 bool hostVisibleMemIndexSet =
false;
802 f->vkGetPhysicalDeviceMemoryProperties(physDev, &physDevMemProps);
803 for (uint32_t i = 0; i < physDevMemProps.memoryTypeCount; ++i) {
804 const VkMemoryType *memType = physDevMemProps.memoryTypes;
805 qCDebug(lcGuiVk,
"memtype %d: flags=0x%x", i, memType[i].propertyFlags);
808 const int hostVisibleAndCoherent = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
809 if ((memType[i].propertyFlags & hostVisibleAndCoherent) == hostVisibleAndCoherent) {
810 if (!hostVisibleMemIndexSet
811 || (memType[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT)) {
812 hostVisibleMemIndexSet =
true;
813 hostVisibleMemIndex = i;
817 qCDebug(lcGuiVk,
"Picked memtype %d for host visible memory", hostVisibleMemIndex);
818 deviceLocalMemIndex = 0;
819 for (uint32_t i = 0; i < physDevMemProps.memoryTypeCount; ++i) {
820 const VkMemoryType *memType = physDevMemProps.memoryTypes;
822 if (memType[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
823 deviceLocalMemIndex = i;
827 qCDebug(lcGuiVk,
"Picked memtype %d for device local memory", deviceLocalMemIndex);
829 if (!vkGetPhysicalDeviceSurfaceCapabilitiesKHR || !vkGetPhysicalDeviceSurfaceFormatsKHR) {
830 vkGetPhysicalDeviceSurfaceCapabilitiesKHR =
reinterpret_cast<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR>(
831 inst->getInstanceProcAddr(
"vkGetPhysicalDeviceSurfaceCapabilitiesKHR"));
832 vkGetPhysicalDeviceSurfaceFormatsKHR =
reinterpret_cast<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR>(
833 inst->getInstanceProcAddr(
"vkGetPhysicalDeviceSurfaceFormatsKHR"));
834 if (!vkGetPhysicalDeviceSurfaceCapabilitiesKHR || !vkGetPhysicalDeviceSurfaceFormatsKHR) {
835 qWarning(
"QVulkanWindow: Physical device surface queries not available");
846 uint32_t formatCount = 0;
847 vkGetPhysicalDeviceSurfaceFormatsKHR(physDev, surface, &formatCount,
nullptr);
848 QList<VkSurfaceFormatKHR> formats(formatCount);
850 vkGetPhysicalDeviceSurfaceFormatsKHR(physDev, surface, &formatCount, formats.data());
852 colorFormat = VK_FORMAT_B8G8R8A8_UNORM;
853 colorSpace = VkColorSpaceKHR(0);
856 if (!formats.isEmpty() && formats[0].format != VK_FORMAT_UNDEFINED) {
857 colorFormat = formats[0].format;
858 colorSpace = formats[0].colorSpace;
862 if (!formats.isEmpty() && !requestedColorFormats.isEmpty()) {
863 for (VkFormat reqFmt : std::as_const(requestedColorFormats)) {
864 auto r = std::find_if(formats.cbegin(), formats.cend(),
865 [reqFmt](
const VkSurfaceFormatKHR &sfmt) {
return sfmt.format == reqFmt; });
866 if (r != formats.cend()) {
867 colorFormat = r->format;
868 colorSpace = r->colorSpace;
874 const VkFormat dsFormatCandidates[] = {
875 VK_FORMAT_D24_UNORM_S8_UINT,
876 VK_FORMAT_D32_SFLOAT_S8_UINT,
877 VK_FORMAT_D16_UNORM_S8_UINT
879 const int dsFormatCandidateCount =
sizeof(dsFormatCandidates) /
sizeof(VkFormat);
881 while (dsFormatIdx < dsFormatCandidateCount) {
882 dsFormat = dsFormatCandidates[dsFormatIdx];
883 VkFormatProperties fmtProp;
884 f->vkGetPhysicalDeviceFormatProperties(physDev, dsFormat, &fmtProp);
885 if (fmtProp.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
889 if (dsFormatIdx == dsFormatCandidateCount)
890 qWarning(
"QVulkanWindow: Failed to find an optimal depth-stencil format");
892 qCDebug(lcGuiVk,
"Color format: %d Depth-stencil format: %d", colorFormat, dsFormat);
894 if (!createDefaultRenderPass())
898 renderer->initResources();
900 status = StatusDeviceReady;
903void QVulkanWindowPrivate::reset()
908 qCDebug(lcGuiVk,
"QVulkanWindow reset");
910 devFuncs->vkDeviceWaitIdle(dev);
913 renderer->releaseResources();
914 devFuncs->vkDeviceWaitIdle(dev);
917 if (defaultRenderPass) {
918 devFuncs->vkDestroyRenderPass(dev, defaultRenderPass,
nullptr);
919 defaultRenderPass = VK_NULL_HANDLE;
923 devFuncs->vkDestroyCommandPool(dev, cmdPool,
nullptr);
924 cmdPool = VK_NULL_HANDLE;
928 devFuncs->vkDestroyCommandPool(dev, presCmdPool,
nullptr);
929 presCmdPool = VK_NULL_HANDLE;
932 if (frameGrabImage) {
933 devFuncs->vkDestroyImage(dev, frameGrabImage,
nullptr);
934 frameGrabImage = VK_NULL_HANDLE;
937 if (frameGrabImageMem) {
938 devFuncs->vkFreeMemory(dev, frameGrabImageMem,
nullptr);
939 frameGrabImageMem = VK_NULL_HANDLE;
943 devFuncs->vkDestroyDevice(dev,
nullptr);
944 inst->resetDeviceFunctions(dev);
945 dev = VK_NULL_HANDLE;
946 vkCreateSwapchainKHR =
nullptr;
949 surface = VK_NULL_HANDLE;
951 status = StatusUninitialized;
954bool QVulkanWindowPrivate::createDefaultRenderPass()
956 VkAttachmentDescription attDesc[3];
957 memset(attDesc, 0,
sizeof(attDesc));
959 const bool msaa = sampleCount > VK_SAMPLE_COUNT_1_BIT;
962 attDesc[0].format = colorFormat;
963 attDesc[0].samples = VK_SAMPLE_COUNT_1_BIT;
964 attDesc[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
965 attDesc[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
966 attDesc[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
967 attDesc[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
968 attDesc[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
969 attDesc[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
971 attDesc[1].format = dsFormat;
972 attDesc[1].samples = sampleCount;
973 attDesc[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
974 attDesc[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
975 attDesc[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
976 attDesc[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
977 attDesc[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
978 attDesc[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
982 attDesc[2].format = colorFormat;
983 attDesc[2].samples = sampleCount;
984 attDesc[2].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
985 attDesc[2].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
986 attDesc[2].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
987 attDesc[2].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
988 attDesc[2].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
989 attDesc[2].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
992 VkAttachmentReference colorRef = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL };
993 VkAttachmentReference resolveRef = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL };
994 VkAttachmentReference dsRef = { 1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL };
996 VkSubpassDescription subPassDesc;
997 memset(&subPassDesc, 0,
sizeof(subPassDesc));
998 subPassDesc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
999 subPassDesc.colorAttachmentCount = 1;
1000 subPassDesc.pColorAttachments = &colorRef;
1001 subPassDesc.pDepthStencilAttachment = &dsRef;
1003 VkRenderPassCreateInfo rpInfo;
1004 memset(&rpInfo, 0,
sizeof(rpInfo));
1005 rpInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
1006 rpInfo.attachmentCount = 2;
1007 rpInfo.pAttachments = attDesc;
1008 rpInfo.subpassCount = 1;
1009 rpInfo.pSubpasses = &subPassDesc;
1012 colorRef.attachment = 2;
1013 subPassDesc.pResolveAttachments = &resolveRef;
1014 rpInfo.attachmentCount = 3;
1017 VkResult err = devFuncs->vkCreateRenderPass(dev, &rpInfo,
nullptr, &defaultRenderPass);
1018 if (err != VK_SUCCESS) {
1019 qWarning(
"QVulkanWindow: Failed to create renderpass: %d", err);
1026void QVulkanWindowPrivate::recreateSwapChain()
1029 Q_ASSERT(status >= StatusDeviceReady);
1031 swapChainImageSize = q->size() * q->devicePixelRatio();
1033 if (swapChainImageSize.isEmpty())
1036 QVulkanInstance *inst = q->vulkanInstance();
1037 QVulkanFunctions *f = inst->functions();
1038 devFuncs->vkDeviceWaitIdle(dev);
1040 if (!vkCreateSwapchainKHR) {
1041 vkCreateSwapchainKHR =
reinterpret_cast<PFN_vkCreateSwapchainKHR>(f->vkGetDeviceProcAddr(dev,
"vkCreateSwapchainKHR"));
1042 vkDestroySwapchainKHR =
reinterpret_cast<PFN_vkDestroySwapchainKHR>(f->vkGetDeviceProcAddr(dev,
"vkDestroySwapchainKHR"));
1043 vkGetSwapchainImagesKHR =
reinterpret_cast<PFN_vkGetSwapchainImagesKHR>(f->vkGetDeviceProcAddr(dev,
"vkGetSwapchainImagesKHR"));
1044 vkAcquireNextImageKHR =
reinterpret_cast<PFN_vkAcquireNextImageKHR>(f->vkGetDeviceProcAddr(dev,
"vkAcquireNextImageKHR"));
1045 vkQueuePresentKHR =
reinterpret_cast<PFN_vkQueuePresentKHR>(f->vkGetDeviceProcAddr(dev,
"vkQueuePresentKHR"));
1048 VkPhysicalDevice physDev = physDevs.at(physDevIndex);
1049 VkSurfaceCapabilitiesKHR surfaceCaps;
1050 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physDev, surface, &surfaceCaps);
1051 uint32_t reqBufferCount;
1052 if (surfaceCaps.maxImageCount == 0)
1053 reqBufferCount = qMax<uint32_t>(2, surfaceCaps.minImageCount);
1055 reqBufferCount = qMax(qMin<uint32_t>(surfaceCaps.maxImageCount, 3), surfaceCaps.minImageCount);
1057 VkExtent2D bufferSize = surfaceCaps.currentExtent;
1058 if (bufferSize.width == uint32_t(-1)) {
1059 Q_ASSERT(bufferSize.height == uint32_t(-1));
1060 bufferSize.width = swapChainImageSize.width();
1061 bufferSize.height = swapChainImageSize.height();
1063 swapChainImageSize = QSize(bufferSize.width, bufferSize.height);
1066 VkSurfaceTransformFlagBitsKHR preTransform =
1067 (surfaceCaps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
1068 ? VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
1069 : surfaceCaps.currentTransform;
1071 VkCompositeAlphaFlagBitsKHR compositeAlpha =
1072 (surfaceCaps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)
1073 ? VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
1074 : VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1076 if (q->requestedFormat().hasAlpha()) {
1077 if (surfaceCaps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR)
1078 compositeAlpha = VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
1079 else if (surfaceCaps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR)
1080 compositeAlpha = VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR;
1083 VkImageUsageFlags usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1084 swapChainSupportsReadBack = (surfaceCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT);
1085 if (swapChainSupportsReadBack)
1086 usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1088 VkSwapchainKHR oldSwapChain = swapChain;
1089 VkSwapchainCreateInfoKHR swapChainInfo;
1090 memset(&swapChainInfo, 0,
sizeof(swapChainInfo));
1091 swapChainInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
1092 swapChainInfo.surface = surface;
1093 swapChainInfo.minImageCount = reqBufferCount;
1094 swapChainInfo.imageFormat = colorFormat;
1095 swapChainInfo.imageColorSpace = colorSpace;
1096 swapChainInfo.imageExtent = bufferSize;
1097 swapChainInfo.imageArrayLayers = 1;
1098 swapChainInfo.imageUsage = usage;
1099 swapChainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
1100 swapChainInfo.preTransform = preTransform;
1101 swapChainInfo.compositeAlpha = compositeAlpha;
1102 swapChainInfo.presentMode = presentMode;
1103 swapChainInfo.clipped =
true;
1104 swapChainInfo.oldSwapchain = oldSwapChain;
1106 qCDebug(lcGuiVk,
"Creating new swap chain of %d buffers, size %dx%d", reqBufferCount, bufferSize.width, bufferSize.height);
1108 VkSwapchainKHR newSwapChain;
1109 VkResult err = vkCreateSwapchainKHR(dev, &swapChainInfo,
nullptr, &newSwapChain);
1110 if (err != VK_SUCCESS) {
1111 qWarning(
"QVulkanWindow: Failed to create swap chain: %d", err);
1118 swapChain = newSwapChain;
1120 uint32_t actualSwapChainBufferCount = 0;
1121 err = vkGetSwapchainImagesKHR(dev, swapChain, &actualSwapChainBufferCount,
nullptr);
1122 if (err != VK_SUCCESS || actualSwapChainBufferCount < 2) {
1123 qWarning(
"QVulkanWindow: Failed to get swapchain images: %d (count=%d)", err, actualSwapChainBufferCount);
1127 qCDebug(lcGuiVk,
"Actual swap chain buffer count: %d (supportsReadback=%d)",
1128 actualSwapChainBufferCount, swapChainSupportsReadBack);
1129 if (actualSwapChainBufferCount > MAX_SWAPCHAIN_BUFFER_COUNT) {
1130 qWarning(
"QVulkanWindow: Too many swapchain buffers (%d)", actualSwapChainBufferCount);
1133 swapChainBufferCount = actualSwapChainBufferCount;
1135 VkImage swapChainImages[MAX_SWAPCHAIN_BUFFER_COUNT];
1136 err = vkGetSwapchainImagesKHR(dev, swapChain, &actualSwapChainBufferCount, swapChainImages);
1137 if (err != VK_SUCCESS) {
1138 qWarning(
"QVulkanWindow: Failed to get swapchain images: %d", err);
1142 if (!createTransientImage(dsFormat,
1143 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
1144 VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
1153 const bool msaa = sampleCount > VK_SAMPLE_COUNT_1_BIT;
1154 VkImage msaaImages[MAX_SWAPCHAIN_BUFFER_COUNT];
1155 VkImageView msaaViews[MAX_SWAPCHAIN_BUFFER_COUNT];
1158 if (!createTransientImage(colorFormat,
1159 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
1160 VK_IMAGE_ASPECT_COLOR_BIT,
1164 swapChainBufferCount))
1170 VkFenceCreateInfo fenceInfo = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
nullptr, VK_FENCE_CREATE_SIGNALED_BIT };
1172 for (
int i = 0; i < swapChainBufferCount; ++i) {
1173 ImageResources &image(imageRes[i]);
1174 image.image = swapChainImages[i];
1177 image.msaaImage = msaaImages[i];
1178 image.msaaImageView = msaaViews[i];
1181 VkImageViewCreateInfo imgViewInfo;
1182 memset(&imgViewInfo, 0,
sizeof(imgViewInfo));
1183 imgViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
1184 imgViewInfo.image = swapChainImages[i];
1185 imgViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
1186 imgViewInfo.format = colorFormat;
1187 imgViewInfo.components.r = VK_COMPONENT_SWIZZLE_R;
1188 imgViewInfo.components.g = VK_COMPONENT_SWIZZLE_G;
1189 imgViewInfo.components.b = VK_COMPONENT_SWIZZLE_B;
1190 imgViewInfo.components.a = VK_COMPONENT_SWIZZLE_A;
1191 imgViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1192 imgViewInfo.subresourceRange.levelCount = imgViewInfo.subresourceRange.layerCount = 1;
1193 err = devFuncs->vkCreateImageView(dev, &imgViewInfo,
nullptr, &image.imageView);
1194 if (err != VK_SUCCESS) {
1195 qWarning(
"QVulkanWindow: Failed to create swapchain image view %d: %d", i, err);
1199 VkImageView views[3] = { image.imageView,
1201 msaa ? image.msaaImageView : VK_NULL_HANDLE };
1202 VkFramebufferCreateInfo fbInfo;
1203 memset(&fbInfo, 0,
sizeof(fbInfo));
1204 fbInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
1205 fbInfo.renderPass = defaultRenderPass;
1206 fbInfo.attachmentCount = msaa ? 3 : 2;
1207 fbInfo.pAttachments = views;
1208 fbInfo.width = swapChainImageSize.width();
1209 fbInfo.height = swapChainImageSize.height();
1211 VkResult err = devFuncs->vkCreateFramebuffer(dev, &fbInfo,
nullptr, &image.fb);
1212 if (err != VK_SUCCESS) {
1213 qWarning(
"QVulkanWindow: Failed to create framebuffer: %d", err);
1217 if (gfxQueueFamilyIdx != presQueueFamilyIdx) {
1219 VkCommandBufferAllocateInfo cmdBufInfo = {
1220 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
nullptr, presCmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1 };
1221 err = devFuncs->vkAllocateCommandBuffers(dev, &cmdBufInfo, &image.presTransCmdBuf);
1222 if (err != VK_SUCCESS) {
1223 qWarning(
"QVulkanWindow: Failed to allocate acquire-on-present-queue command buffer: %d", err);
1226 VkCommandBufferBeginInfo cmdBufBeginInfo = {
1227 VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
nullptr,
1228 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
nullptr };
1229 err = devFuncs->vkBeginCommandBuffer(image.presTransCmdBuf, &cmdBufBeginInfo);
1230 if (err != VK_SUCCESS) {
1231 qWarning(
"QVulkanWindow: Failed to begin acquire-on-present-queue command buffer: %d", err);
1234 VkImageMemoryBarrier presTrans;
1235 memset(&presTrans, 0,
sizeof(presTrans));
1236 presTrans.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1237 presTrans.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
1238 presTrans.oldLayout = presTrans.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
1239 presTrans.srcQueueFamilyIndex = gfxQueueFamilyIdx;
1240 presTrans.dstQueueFamilyIndex = presQueueFamilyIdx;
1241 presTrans.image = image.image;
1242 presTrans.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1243 presTrans.subresourceRange.levelCount = presTrans.subresourceRange.layerCount = 1;
1244 devFuncs->vkCmdPipelineBarrier(image.presTransCmdBuf,
1245 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1246 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1247 0, 0,
nullptr, 0,
nullptr,
1249 err = devFuncs->vkEndCommandBuffer(image.presTransCmdBuf);
1250 if (err != VK_SUCCESS) {
1251 qWarning(
"QVulkanWindow: Failed to end acquire-on-present-queue command buffer: %d", err);
1259 VkSemaphoreCreateInfo semInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
nullptr, 0 };
1260 for (
int i = 0; i < frameLag; ++i) {
1261 FrameResources &frame(frameRes[i]);
1263 frame.imageAcquired =
false;
1264 frame.imageSemWaitable =
false;
1266 devFuncs->vkCreateSemaphore(dev, &semInfo,
nullptr, &frame.imageSem);
1267 devFuncs->vkCreateSemaphore(dev, &semInfo,
nullptr, &frame.drawSem);
1268 if (gfxQueueFamilyIdx != presQueueFamilyIdx)
1269 devFuncs->vkCreateSemaphore(dev, &semInfo,
nullptr, &frame.presTransSem);
1271 err = devFuncs->vkCreateFence(dev, &fenceInfo,
nullptr, &frame.cmdFence);
1272 if (err != VK_SUCCESS) {
1273 qWarning(
"QVulkanWindow: Failed to create command buffer fence: %d", err);
1276 frame.cmdFenceWaitable =
true;
1282 renderer->initSwapChainResources();
1284 status = StatusReady;
1287uint32_t QVulkanWindowPrivate::chooseTransientImageMemType(VkImage img, uint32_t startIndex)
1289 VkPhysicalDeviceMemoryProperties physDevMemProps;
1290 inst->functions()->vkGetPhysicalDeviceMemoryProperties(physDevs[physDevIndex], &physDevMemProps);
1292 VkMemoryRequirements memReq;
1293 devFuncs->vkGetImageMemoryRequirements(dev, img, &memReq);
1294 uint32_t memTypeIndex = uint32_t(-1);
1296 if (memReq.memoryTypeBits) {
1298 const VkMemoryType *memType = physDevMemProps.memoryTypes;
1299 bool foundDevLocal =
false;
1300 for (uint32_t i = startIndex; i < physDevMemProps.memoryTypeCount; ++i) {
1301 if (memReq.memoryTypeBits & (1 << i)) {
1302 if (memType[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
1303 if (!foundDevLocal) {
1304 foundDevLocal =
true;
1307 if (memType[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) {
1316 return memTypeIndex;
1321 return (v + byteAlign - 1) & ~(byteAlign - 1);
1324bool QVulkanWindowPrivate::createTransientImage(VkFormat format,
1325 VkImageUsageFlags usage,
1326 VkImageAspectFlags aspectMask,
1328 VkDeviceMemory *mem,
1332 VkMemoryRequirements memReq;
1335 Q_ASSERT(count > 0);
1336 for (
int i = 0; i < count; ++i) {
1337 VkImageCreateInfo imgInfo;
1338 memset(&imgInfo, 0,
sizeof(imgInfo));
1339 imgInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1340 imgInfo.imageType = VK_IMAGE_TYPE_2D;
1341 imgInfo.format = format;
1342 imgInfo.extent.width = swapChainImageSize.width();
1343 imgInfo.extent.height = swapChainImageSize.height();
1344 imgInfo.extent.depth = 1;
1345 imgInfo.mipLevels = imgInfo.arrayLayers = 1;
1346 imgInfo.samples = sampleCount;
1347 imgInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
1348 imgInfo.usage = usage | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT;
1350 err = devFuncs->vkCreateImage(dev, &imgInfo,
nullptr, images + i);
1351 if (err != VK_SUCCESS) {
1352 qWarning(
"QVulkanWindow: Failed to create image: %d", err);
1359 devFuncs->vkGetImageMemoryRequirements(dev, images[i], &memReq);
1362 VkMemoryAllocateInfo memInfo;
1363 memset(&memInfo, 0,
sizeof(memInfo));
1364 memInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1365 memInfo.allocationSize = aligned(memReq.size, memReq.alignment) * count;
1367 uint32_t startIndex = 0;
1369 memInfo.memoryTypeIndex = chooseTransientImageMemType(images[0], startIndex);
1370 if (memInfo.memoryTypeIndex == uint32_t(-1)) {
1371 qWarning(
"QVulkanWindow: No suitable memory type found");
1374 startIndex = memInfo.memoryTypeIndex + 1;
1375 qCDebug(lcGuiVk,
"Allocating %u bytes for transient image (memtype %u)",
1376 uint32_t(memInfo.allocationSize), memInfo.memoryTypeIndex);
1377 err = devFuncs->vkAllocateMemory(dev, &memInfo,
nullptr, mem);
1378 if (err != VK_SUCCESS && err != VK_ERROR_OUT_OF_DEVICE_MEMORY) {
1379 qWarning(
"QVulkanWindow: Failed to allocate image memory: %d", err);
1382 }
while (err != VK_SUCCESS);
1384 VkDeviceSize ofs = 0;
1385 for (
int i = 0; i < count; ++i) {
1386 err = devFuncs->vkBindImageMemory(dev, images[i], *mem, ofs);
1387 if (err != VK_SUCCESS) {
1388 qWarning(
"QVulkanWindow: Failed to bind image memory: %d", err);
1391 ofs += aligned(memReq.size, memReq.alignment);
1393 VkImageViewCreateInfo imgViewInfo;
1394 memset(&imgViewInfo, 0,
sizeof(imgViewInfo));
1395 imgViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
1396 imgViewInfo.image = images[i];
1397 imgViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
1398 imgViewInfo.format = format;
1399 imgViewInfo.components.r = VK_COMPONENT_SWIZZLE_R;
1400 imgViewInfo.components.g = VK_COMPONENT_SWIZZLE_G;
1401 imgViewInfo.components.b = VK_COMPONENT_SWIZZLE_B;
1402 imgViewInfo.components.a = VK_COMPONENT_SWIZZLE_A;
1403 imgViewInfo.subresourceRange.aspectMask = aspectMask;
1404 imgViewInfo.subresourceRange.levelCount = imgViewInfo.subresourceRange.layerCount = 1;
1406 err = devFuncs->vkCreateImageView(dev, &imgViewInfo,
nullptr, views + i);
1407 if (err != VK_SUCCESS) {
1408 qWarning(
"QVulkanWindow: Failed to create image view: %d", err);
1416void QVulkanWindowPrivate::releaseSwapChain()
1418 if (!dev || !swapChain)
1421 qCDebug(lcGuiVk,
"Releasing swapchain");
1423 devFuncs->vkDeviceWaitIdle(dev);
1426 renderer->releaseSwapChainResources();
1427 devFuncs->vkDeviceWaitIdle(dev);
1430 for (
int i = 0; i < frameLag; ++i) {
1431 FrameResources &frame(frameRes[i]);
1433 devFuncs->vkFreeCommandBuffers(dev, cmdPool, 1, &frame.cmdBuf);
1434 frame.cmdBuf = VK_NULL_HANDLE;
1436 if (frame.imageSem) {
1437 devFuncs->vkDestroySemaphore(dev, frame.imageSem,
nullptr);
1438 frame.imageSem = VK_NULL_HANDLE;
1440 if (frame.drawSem) {
1441 devFuncs->vkDestroySemaphore(dev, frame.drawSem,
nullptr);
1442 frame.drawSem = VK_NULL_HANDLE;
1444 if (frame.presTransSem) {
1445 devFuncs->vkDestroySemaphore(dev, frame.presTransSem,
nullptr);
1446 frame.presTransSem = VK_NULL_HANDLE;
1448 if (frame.cmdFence) {
1449 if (frame.cmdFenceWaitable)
1450 devFuncs->vkWaitForFences(dev, 1, &frame.cmdFence, VK_TRUE, UINT64_MAX);
1451 devFuncs->vkDestroyFence(dev, frame.cmdFence,
nullptr);
1452 frame.cmdFence = VK_NULL_HANDLE;
1453 frame.cmdFenceWaitable =
false;
1457 for (
int i = 0; i < swapChainBufferCount; ++i) {
1458 ImageResources &image(imageRes[i]);
1460 devFuncs->vkDestroyFramebuffer(dev, image.fb,
nullptr);
1461 image.fb = VK_NULL_HANDLE;
1463 if (image.imageView) {
1464 devFuncs->vkDestroyImageView(dev, image.imageView,
nullptr);
1465 image.imageView = VK_NULL_HANDLE;
1467 if (image.presTransCmdBuf) {
1468 devFuncs->vkFreeCommandBuffers(dev, presCmdPool, 1, &image.presTransCmdBuf);
1469 image.presTransCmdBuf = VK_NULL_HANDLE;
1471 if (image.msaaImageView) {
1472 devFuncs->vkDestroyImageView(dev, image.msaaImageView,
nullptr);
1473 image.msaaImageView = VK_NULL_HANDLE;
1475 if (image.msaaImage) {
1476 devFuncs->vkDestroyImage(dev, image.msaaImage,
nullptr);
1477 image.msaaImage = VK_NULL_HANDLE;
1482 devFuncs->vkFreeMemory(dev, msaaImageMem,
nullptr);
1483 msaaImageMem = VK_NULL_HANDLE;
1487 devFuncs->vkDestroyImageView(dev, dsView,
nullptr);
1488 dsView = VK_NULL_HANDLE;
1491 devFuncs->vkDestroyImage(dev, dsImage,
nullptr);
1492 dsImage = VK_NULL_HANDLE;
1495 devFuncs->vkFreeMemory(dev, dsMem,
nullptr);
1496 dsMem = VK_NULL_HANDLE;
1500 vkDestroySwapchainKHR(dev, swapChain,
nullptr);
1501 swapChain = VK_NULL_HANDLE;
1504 if (status == StatusReady)
1505 status = StatusDeviceReady;
1509
1510
1511void QVulkanWindow::exposeEvent(QExposeEvent *)
1518 if (!d->flags.testFlag(PersistentResources)) {
1519 d->releaseSwapChain();
1525void QVulkanWindowPrivate::ensureStarted()
1528 if (status == QVulkanWindowPrivate::StatusFailRetry)
1529 status = QVulkanWindowPrivate::StatusUninitialized;
1530 if (status == QVulkanWindowPrivate::StatusUninitialized) {
1532 if (status == QVulkanWindowPrivate::StatusDeviceReady)
1533 recreateSwapChain();
1535 if (status == QVulkanWindowPrivate::StatusReady)
1540
1541
1542void QVulkanWindow::resizeEvent(QResizeEvent *)
1548
1549
1550bool QVulkanWindow::event(QEvent *e)
1554 switch (e->type()) {
1556 case QEvent::UpdateRequest:
1564 case QEvent::PlatformSurface:
1565 if (
static_cast<QPlatformSurfaceEvent *>(e)->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed) {
1566 d->releaseSwapChain();
1575 return QWindow::event(e);
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1598
1599
1600
1601
1602
1603
1604void QVulkanWindow::setQueueCreateInfoModifier(
const QueueCreateInfoModifier &modifier)
1607 d->queueCreateInfoModifier = modifier;
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644void QVulkanWindow::setEnabledFeaturesModifier(
const EnabledFeaturesModifier &modifier)
1647 d->enabledFeaturesModifier = modifier;
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1678
1679
1680
1681
1682
1683void QVulkanWindow::setEnabledFeaturesModifier(EnabledFeatures2Modifier modifier)
1686 d->enabledFeatures2Modifier = std::move(modifier);
1690
1691
1692
1693
1694
1695
1696bool QVulkanWindow::isValid()
const
1698 Q_D(
const QVulkanWindow);
1699 return d->status == QVulkanWindowPrivate::StatusReady;
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713QVulkanWindowRenderer *QVulkanWindow::createRenderer()
1719
1720
1721QVulkanWindowRenderer::~QVulkanWindowRenderer()
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741void QVulkanWindowRenderer::preInitResources()
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760void QVulkanWindowRenderer::initResources()
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782void QVulkanWindowRenderer::initSwapChainResources()
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805void QVulkanWindowRenderer::releaseSwapChainResources()
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821void QVulkanWindowRenderer::releaseResources()
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866void QVulkanWindowRenderer::physicalDeviceLost()
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885void QVulkanWindowRenderer::logicalDeviceLost()
1889QSize QVulkanWindowPrivate::surfacePixelSize()
const
1891 Q_Q(
const QVulkanWindow);
1892 VkSurfaceCapabilitiesKHR surfaceCaps = {};
1893 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physDevs.at(physDevIndex), surface, &surfaceCaps);
1894 VkExtent2D bufferSize = surfaceCaps.currentExtent;
1895 if (bufferSize.width == uint32_t(-1)) {
1896 Q_ASSERT(bufferSize.height == uint32_t(-1));
1897 return q->size() * q->devicePixelRatio();
1899 return QSize(
int(bufferSize.width),
int(bufferSize.height));
1902void QVulkanWindowPrivate::beginFrame()
1904 if (!swapChain || framePending)
1908 if (swapChainImageSize != surfacePixelSize()) {
1909 recreateSwapChain();
1915 FrameResources &frame(frameRes[currentFrame]);
1916 if (frame.cmdFenceWaitable) {
1917 devFuncs->vkWaitForFences(dev, 1, &frame.cmdFence, VK_TRUE, UINT64_MAX);
1918 devFuncs->vkResetFences(dev, 1, &frame.cmdFence);
1919 frame.cmdFenceWaitable =
false;
1923 if (!frame.imageAcquired) {
1924 VkResult err = vkAcquireNextImageKHR(dev, swapChain, UINT64_MAX,
1925 frame.imageSem, VK_NULL_HANDLE, ¤tImage);
1926 if (err == VK_SUCCESS || err == VK_SUBOPTIMAL_KHR) {
1927 frame.imageSemWaitable =
true;
1928 frame.imageAcquired =
true;
1929 }
else if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1930 recreateSwapChain();
1934 if (!checkDeviceLost(err))
1935 qWarning(
"QVulkanWindow: Failed to acquire next swapchain image: %d", err);
1943 devFuncs->vkFreeCommandBuffers(dev, cmdPool, 1, &frame.cmdBuf);
1944 frame.cmdBuf =
nullptr;
1947 VkCommandBufferAllocateInfo cmdBufInfo = {
1948 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
nullptr, cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1 };
1949 VkResult err = devFuncs->vkAllocateCommandBuffers(dev, &cmdBufInfo, &frame.cmdBuf);
1950 if (err != VK_SUCCESS) {
1951 if (!checkDeviceLost(err))
1952 qWarning(
"QVulkanWindow: Failed to allocate frame command buffer: %d", err);
1956 VkCommandBufferBeginInfo cmdBufBeginInfo = {
1957 VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
nullptr, 0,
nullptr };
1958 err = devFuncs->vkBeginCommandBuffer(frame.cmdBuf, &cmdBufBeginInfo);
1959 if (err != VK_SUCCESS) {
1960 if (!checkDeviceLost(err))
1961 qWarning(
"QVulkanWindow: Failed to begin frame command buffer: %d", err);
1966 frameGrabTargetImage = QImage(swapChainImageSize, QImage::Format_RGBA8888);
1968 ImageResources &image(imageRes[currentImage]);
1970 framePending =
true;
1971 renderer->startNextFrame();
1974 VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
1975 VkClearDepthStencilValue clearDS = { 1.0f, 0 };
1976 VkClearValue clearValues[3];
1977 memset(clearValues, 0,
sizeof(clearValues));
1978 clearValues[0].color = clearValues[2].color = clearColor;
1979 clearValues[1].depthStencil = clearDS;
1981 VkRenderPassBeginInfo rpBeginInfo;
1982 memset(&rpBeginInfo, 0,
sizeof(rpBeginInfo));
1983 rpBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
1984 rpBeginInfo.renderPass = defaultRenderPass;
1985 rpBeginInfo.framebuffer = image.fb;
1986 rpBeginInfo.renderArea.extent.width = swapChainImageSize.width();
1987 rpBeginInfo.renderArea.extent.height = swapChainImageSize.height();
1988 rpBeginInfo.clearValueCount = sampleCount > VK_SAMPLE_COUNT_1_BIT ? 3 : 2;
1989 rpBeginInfo.pClearValues = clearValues;
1990 devFuncs->vkCmdBeginRenderPass(frame.cmdBuf, &rpBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
1991 devFuncs->vkCmdEndRenderPass(frame.cmdBuf);
1997void QVulkanWindowPrivate::endFrame()
2001 FrameResources &frame(frameRes[currentFrame]);
2002 ImageResources &image(imageRes[currentImage]);
2004 if (gfxQueueFamilyIdx != presQueueFamilyIdx && !frameGrabbing) {
2007 VkImageMemoryBarrier presTrans;
2008 memset(&presTrans, 0,
sizeof(presTrans));
2009 presTrans.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
2010 presTrans.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
2011 presTrans.oldLayout = presTrans.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
2012 presTrans.srcQueueFamilyIndex = gfxQueueFamilyIdx;
2013 presTrans.dstQueueFamilyIndex = presQueueFamilyIdx;
2014 presTrans.image = image.image;
2015 presTrans.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
2016 presTrans.subresourceRange.levelCount = presTrans.subresourceRange.layerCount = 1;
2017 devFuncs->vkCmdPipelineBarrier(frame.cmdBuf,
2018 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
2019 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
2020 0, 0,
nullptr, 0,
nullptr,
2028 VkResult err = devFuncs->vkEndCommandBuffer(frame.cmdBuf);
2029 if (err != VK_SUCCESS) {
2030 if (!checkDeviceLost(err))
2031 qWarning(
"QVulkanWindow: Failed to end frame command buffer: %d", err);
2036 VkSubmitInfo submitInfo;
2037 memset(&submitInfo, 0,
sizeof(submitInfo));
2038 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
2039 submitInfo.commandBufferCount = 1;
2040 submitInfo.pCommandBuffers = &frame.cmdBuf;
2041 if (frame.imageSemWaitable) {
2042 submitInfo.waitSemaphoreCount = 1;
2043 submitInfo.pWaitSemaphores = &frame.imageSem;
2045 if (!frameGrabbing) {
2046 submitInfo.signalSemaphoreCount = 1;
2047 submitInfo.pSignalSemaphores = &frame.drawSem;
2049 VkPipelineStageFlags psf = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
2050 submitInfo.pWaitDstStageMask = &psf;
2052 Q_ASSERT(!frame.cmdFenceWaitable);
2054 err = devFuncs->vkQueueSubmit(gfxQueue, 1, &submitInfo, frame.cmdFence);
2055 if (err == VK_SUCCESS) {
2056 frame.imageSemWaitable =
false;
2057 frame.cmdFenceWaitable =
true;
2059 if (!checkDeviceLost(err))
2060 qWarning(
"QVulkanWindow: Failed to submit to graphics queue: %d", err);
2065 if (frameGrabbing) {
2066 finishBlockingReadback();
2067 frameGrabbing =
false;
2070 emit q->frameGrabbed(frameGrabTargetImage);
2074 if (gfxQueueFamilyIdx != presQueueFamilyIdx) {
2076 submitInfo.pWaitSemaphores = &frame.drawSem;
2077 submitInfo.pSignalSemaphores = &frame.presTransSem;
2078 submitInfo.pCommandBuffers = &image.presTransCmdBuf;
2079 err = devFuncs->vkQueueSubmit(presQueue, 1, &submitInfo, VK_NULL_HANDLE);
2080 if (err != VK_SUCCESS) {
2081 if (!checkDeviceLost(err))
2082 qWarning(
"QVulkanWindow: Failed to submit to present queue: %d", err);
2088 VkPresentInfoKHR presInfo;
2089 memset(&presInfo, 0,
sizeof(presInfo));
2090 presInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
2091 presInfo.swapchainCount = 1;
2092 presInfo.pSwapchains = &swapChain;
2093 presInfo.pImageIndices = ¤tImage;
2094 presInfo.waitSemaphoreCount = 1;
2095 presInfo.pWaitSemaphores = gfxQueueFamilyIdx == presQueueFamilyIdx ? &frame.drawSem : &frame.presTransSem;
2099 inst->presentAboutToBeQueued(q);
2101 err = vkQueuePresentKHR(presQueue, &presInfo);
2102 if (err != VK_SUCCESS) {
2103 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
2104 recreateSwapChain();
2107 }
else if (err != VK_SUBOPTIMAL_KHR) {
2108 if (!checkDeviceLost(err))
2109 qWarning(
"QVulkanWindow: Failed to present: %d", err);
2114 frame.imageAcquired =
false;
2116 inst->presentQueued(q);
2118 currentFrame = (currentFrame + 1) % frameLag;
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134void QVulkanWindow::frameReady()
2136 Q_ASSERT_X(QThread::isMainThread(),
2137 "QVulkanWindow",
"frameReady() can only be called from the GUI (main) thread");
2141 if (!d->framePending) {
2142 qWarning(
"QVulkanWindow: frameReady() called without a corresponding startNextFrame()");
2146 d->framePending =
false;
2151bool QVulkanWindowPrivate::checkDeviceLost(VkResult err)
2153 if (err == VK_ERROR_DEVICE_LOST) {
2154 qWarning(
"QVulkanWindow: Device lost");
2156 renderer->logicalDeviceLost();
2157 qCDebug(lcGuiVk,
"Releasing all resources due to device lost");
2160 qCDebug(lcGuiVk,
"Restarting");
2167void QVulkanWindowPrivate::addReadback()
2169 VkImageCreateInfo imageInfo;
2170 memset(&imageInfo, 0,
sizeof(imageInfo));
2171 imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
2172 imageInfo.imageType = VK_IMAGE_TYPE_2D;
2173 imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
2174 imageInfo.extent.width = frameGrabTargetImage.width();
2175 imageInfo.extent.height = frameGrabTargetImage.height();
2176 imageInfo.extent.depth = 1;
2177 imageInfo.mipLevels = 1;
2178 imageInfo.arrayLayers = 1;
2179 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
2180 imageInfo.tiling = VK_IMAGE_TILING_LINEAR;
2181 imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2182 imageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
2184 VkResult err = devFuncs->vkCreateImage(dev, &imageInfo,
nullptr, &frameGrabImage);
2185 if (err != VK_SUCCESS) {
2186 qWarning(
"QVulkanWindow: Failed to create image for readback: %d", err);
2190 VkMemoryRequirements memReq;
2191 devFuncs->vkGetImageMemoryRequirements(dev, frameGrabImage, &memReq);
2193 VkMemoryAllocateInfo allocInfo = {
2194 VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
2200 err = devFuncs->vkAllocateMemory(dev, &allocInfo,
nullptr, &frameGrabImageMem);
2201 if (err != VK_SUCCESS) {
2202 qWarning(
"QVulkanWindow: Failed to allocate memory for readback image: %d", err);
2206 err = devFuncs->vkBindImageMemory(dev, frameGrabImage, frameGrabImageMem, 0);
2207 if (err != VK_SUCCESS) {
2208 qWarning(
"QVulkanWindow: Failed to bind readback image memory: %d", err);
2212 FrameResources &frame(frameRes[currentFrame]);
2213 ImageResources &image(imageRes[currentImage]);
2215 VkImageMemoryBarrier barrier;
2216 memset(&barrier, 0,
sizeof(barrier));
2217 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
2218 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
2219 barrier.subresourceRange.levelCount = barrier.subresourceRange.layerCount = 1;
2221 barrier.oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
2222 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
2223 barrier.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
2224 barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
2225 barrier.image = image.image;
2227 devFuncs->vkCmdPipelineBarrier(frame.cmdBuf,
2228 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
2229 VK_PIPELINE_STAGE_TRANSFER_BIT,
2230 0, 0,
nullptr, 0,
nullptr,
2233 barrier.oldLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
2234 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
2235 barrier.srcAccessMask = 0;
2236 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
2237 barrier.image = frameGrabImage;
2239 devFuncs->vkCmdPipelineBarrier(frame.cmdBuf,
2240 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
2241 VK_PIPELINE_STAGE_TRANSFER_BIT,
2242 0, 0,
nullptr, 0,
nullptr,
2245 VkImageCopy copyInfo;
2246 memset(©Info, 0,
sizeof(copyInfo));
2247 copyInfo.srcSubresource.aspectMask = copyInfo.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
2248 copyInfo.srcSubresource.layerCount = copyInfo.dstSubresource.layerCount = 1;
2249 copyInfo.extent.width = frameGrabTargetImage.width();
2250 copyInfo.extent.height = frameGrabTargetImage.height();
2251 copyInfo.extent.depth = 1;
2253 devFuncs->vkCmdCopyImage(frame.cmdBuf, image.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2254 frameGrabImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©Info);
2256 barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
2257 barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
2258 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
2259 barrier.dstAccessMask = VK_ACCESS_HOST_READ_BIT;
2260 barrier.image = frameGrabImage;
2262 devFuncs->vkCmdPipelineBarrier(frame.cmdBuf,
2263 VK_PIPELINE_STAGE_TRANSFER_BIT,
2264 VK_PIPELINE_STAGE_HOST_BIT,
2265 0, 0,
nullptr, 0,
nullptr,
2269void QVulkanWindowPrivate::finishBlockingReadback()
2273 FrameResources &frame(frameRes[currentFrame]);
2274 if (frame.cmdFenceWaitable) {
2275 devFuncs->vkWaitForFences(dev, 1, &frame.cmdFence, VK_TRUE, UINT64_MAX);
2276 devFuncs->vkResetFences(dev, 1, &frame.cmdFence);
2277 frame.cmdFenceWaitable =
false;
2280 VkImageSubresource subres = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0 };
2281 VkSubresourceLayout layout;
2282 devFuncs->vkGetImageSubresourceLayout(dev, frameGrabImage, &subres, &layout);
2285 VkResult err = devFuncs->vkMapMemory(dev, frameGrabImageMem, layout.offset, layout.size, 0,
reinterpret_cast<
void **>(&p));
2286 if (err != VK_SUCCESS) {
2287 qWarning(
"QVulkanWindow: Failed to map readback image memory after transfer: %d", err);
2291 for (
int y = 0; y < frameGrabTargetImage.height(); ++y) {
2292 memcpy(frameGrabTargetImage.scanLine(y), p, frameGrabTargetImage.width() * 4);
2293 p += layout.rowPitch;
2296 devFuncs->vkUnmapMemory(dev, frameGrabImageMem);
2298 devFuncs->vkDestroyImage(dev, frameGrabImage,
nullptr);
2299 frameGrabImage = VK_NULL_HANDLE;
2300 devFuncs->vkFreeMemory(dev, frameGrabImageMem,
nullptr);
2301 frameGrabImageMem = VK_NULL_HANDLE;
2305
2306
2307
2308
2309
2310
2311VkPhysicalDevice QVulkanWindow::physicalDevice()
const
2313 Q_D(
const QVulkanWindow);
2314 if (d->physDevIndex < d->physDevs.size())
2315 return d->physDevs[d->physDevIndex];
2316 qWarning(
"QVulkanWindow: Physical device not available");
2317 return VK_NULL_HANDLE;
2321
2322
2323
2324
2325
2326
2327const VkPhysicalDeviceProperties *QVulkanWindow::physicalDeviceProperties()
const
2329 Q_D(
const QVulkanWindow);
2330 if (d->physDevIndex < d->physDevProps.size())
2331 return &d->physDevProps[d->physDevIndex];
2332 qWarning(
"QVulkanWindow: Physical device properties not available");
2337
2338
2339
2340
2341
2342
2343VkDevice QVulkanWindow::device()
const
2345 Q_D(
const QVulkanWindow);
2350
2351
2352
2353
2354
2355
2356VkQueue QVulkanWindow::graphicsQueue()
const
2358 Q_D(
const QVulkanWindow);
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373uint32_t QVulkanWindow::graphicsQueueFamilyIndex()
const
2375 Q_D(
const QVulkanWindow);
2376 return d->gfxQueueFamilyIdx;
2380
2381
2382
2383
2384
2385
2386VkCommandPool QVulkanWindow::graphicsCommandPool()
const
2388 Q_D(
const QVulkanWindow);
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402uint32_t QVulkanWindow::hostVisibleMemoryIndex()
const
2404 Q_D(
const QVulkanWindow);
2405 return d->hostVisibleMemIndex;
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420uint32_t QVulkanWindow::deviceLocalMemoryIndex()
const
2422 Q_D(
const QVulkanWindow);
2423 return d->deviceLocalMemIndex;
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444VkRenderPass QVulkanWindow::defaultRenderPass()
const
2446 Q_D(
const QVulkanWindow);
2447 return d->defaultRenderPass;
2451
2452
2453
2454
2455
2456
2457
2458
2459VkFormat QVulkanWindow::colorFormat()
const
2461 Q_D(
const QVulkanWindow);
2462 return d->colorFormat;
2466
2467
2468
2469
2470
2471
2472VkFormat QVulkanWindow::depthStencilFormat()
const
2474 Q_D(
const QVulkanWindow);
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501QSize QVulkanWindow::swapChainImageSize()
const
2503 Q_D(
const QVulkanWindow);
2504 return d->swapChainImageSize;
2508
2509
2510
2511
2512
2513
2514
2515VkCommandBuffer QVulkanWindow::currentCommandBuffer()
const
2517 Q_D(
const QVulkanWindow);
2518 if (!d->framePending) {
2519 qWarning(
"QVulkanWindow: Attempted to call currentCommandBuffer() without an active frame");
2520 return VK_NULL_HANDLE;
2522 return d->frameRes[d->currentFrame].cmdBuf;
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544VkFramebuffer QVulkanWindow::currentFramebuffer()
const
2546 Q_D(
const QVulkanWindow);
2547 if (!d->framePending) {
2548 qWarning(
"QVulkanWindow: Attempted to call currentFramebuffer() without an active frame");
2549 return VK_NULL_HANDLE;
2551 return d->imageRes[d->currentImage].fb;
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575int QVulkanWindow::currentFrame()
const
2577 Q_D(
const QVulkanWindow);
2578 if (!d->framePending)
2579 qWarning(
"QVulkanWindow: Attempted to call currentFrame() without an active frame");
2580 return d->currentFrame;
2584
2585
2586
2587
2588
2591
2592
2593
2594
2595
2596
2597
2598
2599int QVulkanWindow::concurrentFrameCount()
const
2601 Q_D(
const QVulkanWindow);
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616int QVulkanWindow::swapChainImageCount()
const
2618 Q_D(
const QVulkanWindow);
2619 return d->swapChainBufferCount;
2623
2624
2625
2626
2627
2628int QVulkanWindow::currentSwapChainImageIndex()
const
2630 Q_D(
const QVulkanWindow);
2631 if (!d->framePending)
2632 qWarning(
"QVulkanWindow: Attempted to call currentSwapChainImageIndex() without an active frame");
2633 return d->currentImage;
2637
2638
2639
2640
2641
2642
2643
2644
2645VkImage QVulkanWindow::swapChainImage(
int idx)
const
2647 Q_D(
const QVulkanWindow);
2648 return idx >= 0 && idx < d->swapChainBufferCount ? d->imageRes[idx].image : VK_NULL_HANDLE;
2652
2653
2654
2655
2656
2657
2658
2659
2660VkImageView QVulkanWindow::swapChainImageView(
int idx)
const
2662 Q_D(
const QVulkanWindow);
2663 return idx >= 0 && idx < d->swapChainBufferCount ? d->imageRes[idx].imageView : VK_NULL_HANDLE;
2667
2668
2669
2670
2671
2672
2673VkImage QVulkanWindow::depthStencilImage()
const
2675 Q_D(
const QVulkanWindow);
2680
2681
2682
2683
2684
2685
2686VkImageView QVulkanWindow::depthStencilImageView()
const
2688 Q_D(
const QVulkanWindow);
2693
2694
2695
2696
2697
2698
2699
2700VkSampleCountFlagBits QVulkanWindow::sampleCountFlagBits()
const
2702 Q_D(
const QVulkanWindow);
2703 return d->sampleCount;
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716VkImage QVulkanWindow::msaaColorImage(
int idx)
const
2718 Q_D(
const QVulkanWindow);
2719 return idx >= 0 && idx < d->swapChainBufferCount ? d->imageRes[idx].msaaImage : VK_NULL_HANDLE;
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732VkImageView QVulkanWindow::msaaColorImageView(
int idx)
const
2734 Q_D(
const QVulkanWindow);
2735 return idx >= 0 && idx < d->swapChainBufferCount ? d->imageRes[idx].msaaImageView : VK_NULL_HANDLE;
2739
2740
2741
2742
2743
2744
2745
2746bool QVulkanWindow::supportsGrab()
const
2748 Q_D(
const QVulkanWindow);
2749 return d->swapChainSupportsReadBack;
2753
2754
2755
2756
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783QImage QVulkanWindow::grab()
2786 if (!d->swapChain) {
2787 qWarning(
"QVulkanWindow: Attempted to call grab() without a swapchain");
2790 if (d->framePending) {
2791 qWarning(
"QVulkanWindow: Attempted to call grab() while a frame is still pending");
2794 if (!d->swapChainSupportsReadBack) {
2795 qWarning(
"QVulkanWindow: Attempted to call grab() with a swapchain that does not support usage as transfer source");
2799 d->frameGrabbing =
true;
2802 if (d->colorFormat == VK_FORMAT_B8G8R8A8_UNORM)
2803 d->frameGrabTargetImage = std::move(d->frameGrabTargetImage).rgbSwapped();
2805 return d->frameGrabTargetImage;
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819QMatrix4x4 QVulkanWindow::clipCorrectionMatrix()
2822 if (d->m_clipCorrect.isIdentity()) {
2824 d->m_clipCorrect = QMatrix4x4(1.0f, 0.0f, 0.0f, 0.0f,
2825 0.0f, -1.0f, 0.0f, 0.0f,
2826 0.0f, 0.0f, 0.5f, 0.5f,
2827 0.0f, 0.0f, 0.0f, 1.0f);
2829 return d->m_clipCorrect;
2834#include "moc_qvulkanwindow.cpp"
VkSampleCountFlagBits mask
static VkDeviceSize aligned(VkDeviceSize v, VkDeviceSize byteAlign)