1 | // Copyright (C) 2022 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qvulkandefaultinstance_p.h" |
5 | #include <rhi/qrhi.h> |
6 | #include <QLoggingCategory> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | Q_LOGGING_CATEGORY(lcGuiVk, "qt.vulkan" ) |
11 | |
12 | static QVulkanInstance *s_vulkanInstance; |
13 | Q_CONSTINIT static QVulkanDefaultInstance::Flags s_vulkanInstanceFlags; |
14 | |
15 | QVulkanDefaultInstance::Flags QVulkanDefaultInstance::flags() |
16 | { |
17 | return s_vulkanInstanceFlags; |
18 | } |
19 | |
20 | // As always, calling this when hasInstance() is already true has no effect. (unless cleanup() is called) |
21 | void QVulkanDefaultInstance::setFlag(Flag flag, bool on) |
22 | { |
23 | s_vulkanInstanceFlags.setFlag(flag, on); |
24 | } |
25 | |
26 | bool QVulkanDefaultInstance::hasInstance() |
27 | { |
28 | return s_vulkanInstance != nullptr; |
29 | } |
30 | |
31 | QVulkanInstance *QVulkanDefaultInstance::instance() |
32 | { |
33 | if (s_vulkanInstance) |
34 | return s_vulkanInstance; |
35 | |
36 | s_vulkanInstance = new QVulkanInstance; |
37 | |
38 | // With a Vulkan implementation >= 1.1 we can check what |
39 | // vkEnumerateInstanceVersion() says and request 1.3/1.2/1.1 based on the |
40 | // result. To prevent future surprises, be conservative and ignore any > 1.3 |
41 | // versions for now. For 1.0 implementations nothing will be requested, the |
42 | // default 0 in VkApplicationInfo means 1.0. |
43 | // |
44 | // Vulkan 1.0 is actually sufficient for 99% of Qt Quick (3D)'s |
45 | // functionality. In addition, Vulkan implementations tend to enable 1.1+ |
46 | // functionality regardless of the VkInstance API request. However, the |
47 | // validation layer seems to take this fairly seriously, so we should be |
48 | // prepared for using 1.1+ features in a fully correct manner. This also |
49 | // helps custom Vulkan code in applications, which is not under out |
50 | // control; it is ideal if Vulkan 1.1+ versions are usable without |
51 | // requiring such applications to create their own QVulkanInstance just to |
52 | // be able to make an appropriate setApiVersion() call on it. |
53 | |
54 | const QVersionNumber supportedVersion = s_vulkanInstance->supportedApiVersion(); |
55 | if (supportedVersion >= QVersionNumber(1, 3)) |
56 | s_vulkanInstance->setApiVersion(QVersionNumber(1, 3)); |
57 | else if (supportedVersion >= QVersionNumber(1, 2)) |
58 | s_vulkanInstance->setApiVersion(QVersionNumber(1, 2)); |
59 | else if (supportedVersion >= QVersionNumber(1, 1)) |
60 | s_vulkanInstance->setApiVersion(QVersionNumber(1, 1)); |
61 | qCDebug(lcGuiVk) << "QVulkanDefaultInstance: Creating Vulkan instance" |
62 | << "Requesting Vulkan API" << s_vulkanInstance->apiVersion() |
63 | << "Instance-level version was reported as" << supportedVersion; |
64 | |
65 | if (s_vulkanInstanceFlags.testFlag(flag: EnableValidation)) |
66 | s_vulkanInstance->setLayers({ "VK_LAYER_KHRONOS_validation" }); |
67 | |
68 | s_vulkanInstance->setExtensions(QRhiVulkanInitParams::preferredInstanceExtensions()); |
69 | |
70 | if (!s_vulkanInstance->create()) { |
71 | qWarning(msg: "QVulkanDefaultInstance: Failed to create Vulkan instance" ); |
72 | delete s_vulkanInstance; |
73 | s_vulkanInstance = nullptr; |
74 | } |
75 | |
76 | return s_vulkanInstance; |
77 | } |
78 | |
79 | void QVulkanDefaultInstance::cleanup() |
80 | { |
81 | delete s_vulkanInstance; |
82 | s_vulkanInstance = nullptr; |
83 | } |
84 | |
85 | QT_END_NAMESPACE |
86 | |