1 | // Copyright (C) 2017 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 | #ifndef QVULKANINSTANCE_H |
5 | #define QVULKANINSTANCE_H |
6 | |
7 | #include <QtGui/qtguiglobal.h> |
8 | |
9 | #if 0 |
10 | #pragma qt_no_master_include |
11 | #pragma qt_sync_skip_header_check |
12 | #endif |
13 | |
14 | #if QT_CONFIG(vulkan) || defined(Q_QDOC) |
15 | |
16 | #ifndef VK_NO_PROTOTYPES |
17 | #define VK_NO_PROTOTYPES |
18 | #endif |
19 | #if !defined(Q_QDOC) && __has_include(<vulkan/vulkan.h>) |
20 | #include <vulkan/vulkan.h> |
21 | #else |
22 | // QT_CONFIG(vulkan) implies vulkan.h being available at Qt build time, but it |
23 | // does not guarantee vulkan.h is available at *application* build time. Both |
24 | // for qdoc and for apps built on systems without Vulkan SDK we provide a set |
25 | // of typedefs to keep things compiling since this header may be included from |
26 | // Qt Quick and elsewhere just to get types like VkImage and friends defined. |
27 | |
28 | typedef void* PFN_vkVoidFunction; |
29 | // non-dispatchable handles (64-bit regardless of arch) |
30 | typedef quint64 VkSurfaceKHR; |
31 | typedef quint64 VkImage; |
32 | typedef quint64 VkImageView; |
33 | // dispatchable handles (32 or 64-bit depending on arch) |
34 | typedef void* VkInstance; |
35 | typedef void* VkPhysicalDevice; |
36 | typedef void* VkDevice; |
37 | // enums |
38 | typedef int VkResult; |
39 | typedef int VkFormat; |
40 | typedef int VkImageLayout; |
41 | typedef int VkDebugReportFlagsEXT; |
42 | typedef int VkDebugReportObjectTypeEXT; |
43 | #endif |
44 | |
45 | // QVulkanInstance itself is only applicable if vulkan.h is available, or if |
46 | // it's qdoc. An application that is built on a vulkan.h-less system against a |
47 | // Vulkan-enabled Qt gets the dummy typedefs but not QVulkan*. |
48 | #if __has_include(<vulkan/vulkan.h>) || defined(Q_QDOC) |
49 | |
50 | #include <QtCore/qbytearraylist.h> |
51 | #include <QtCore/qdebug.h> |
52 | #include <QtCore/qhashfunctions.h> |
53 | #include <QtCore/qlist.h> |
54 | #include <QtCore/qscopedpointer.h> |
55 | #include <QtCore/qversionnumber.h> |
56 | |
57 | QT_BEGIN_NAMESPACE |
58 | |
59 | class QVulkanInstancePrivate; |
60 | class QPlatformVulkanInstance; |
61 | class QVulkanFunctions; |
62 | class QVulkanDeviceFunctions; |
63 | class QWindow; |
64 | |
65 | struct QVulkanLayer |
66 | { |
67 | QByteArray name; |
68 | uint32_t version; |
69 | QVersionNumber specVersion; |
70 | QByteArray description; |
71 | }; |
72 | Q_DECLARE_TYPEINFO(QVulkanLayer, Q_RELOCATABLE_TYPE); |
73 | |
74 | inline bool operator==(const QVulkanLayer &lhs, const QVulkanLayer &rhs) noexcept |
75 | { |
76 | return lhs.name == rhs.name && lhs.version == rhs.version && lhs.specVersion == rhs.specVersion; |
77 | } |
78 | inline bool operator!=(const QVulkanLayer &lhs, const QVulkanLayer &rhs) noexcept |
79 | { return !(lhs == rhs); } |
80 | |
81 | inline size_t qHash(const QVulkanLayer &key, size_t seed = 0) noexcept |
82 | { |
83 | QtPrivate::QHashCombine hash; |
84 | seed = hash(seed, key.name); |
85 | seed = hash(seed, key.version); |
86 | seed = hash(seed, key.specVersion); |
87 | return seed; |
88 | } |
89 | |
90 | struct QVulkanExtension |
91 | { |
92 | QByteArray name; |
93 | uint32_t version; |
94 | }; |
95 | Q_DECLARE_TYPEINFO(QVulkanExtension, Q_RELOCATABLE_TYPE); |
96 | |
97 | inline bool operator==(const QVulkanExtension &lhs, const QVulkanExtension &rhs) noexcept |
98 | { |
99 | return lhs.name == rhs.name && lhs.version == rhs.version; |
100 | } |
101 | inline bool operator!=(const QVulkanExtension &lhs, const QVulkanExtension &rhs) noexcept |
102 | { return !(lhs == rhs); } |
103 | |
104 | inline size_t qHash(const QVulkanExtension &key, size_t seed = 0) noexcept |
105 | { |
106 | QtPrivate::QHashCombine hash; |
107 | seed = hash(seed, key.name); |
108 | seed = hash(seed, key.version); |
109 | return seed; |
110 | } |
111 | |
112 | #ifndef QT_NO_DEBUG_STREAM |
113 | Q_GUI_EXPORT QDebug operator<<(QDebug, const QVulkanLayer &); |
114 | Q_GUI_EXPORT QDebug operator<<(QDebug, const QVulkanExtension &); |
115 | #endif |
116 | |
117 | template<typename T> |
118 | class QVulkanInfoVector : public QList<T> |
119 | { |
120 | public: |
121 | bool contains(const QByteArray &name) const { |
122 | return std::any_of(this->cbegin(), this->cend(), [&](const T &entry) { |
123 | return entry.name == name; }); |
124 | } |
125 | bool contains(const QByteArray &name, int minVersion) const { |
126 | return std::any_of(this->cbegin(), this->cend(), [&](const T &entry) { |
127 | return entry.name == name && entry.version >= minVersion; }); |
128 | } |
129 | }; |
130 | |
131 | class Q_GUI_EXPORT QVulkanInstance |
132 | { |
133 | public: |
134 | QVulkanInstance(); |
135 | ~QVulkanInstance(); |
136 | |
137 | enum Flag { |
138 | NoDebugOutputRedirect = 0x01, |
139 | NoPortabilityDrivers = 0x02 |
140 | }; |
141 | Q_DECLARE_FLAGS(Flags, Flag) |
142 | |
143 | // ### Qt 7: remove non-const overloads |
144 | QVulkanInfoVector<QVulkanLayer> supportedLayers(); |
145 | inline QVulkanInfoVector<QVulkanLayer> supportedLayers() const |
146 | { return const_cast<QVulkanInstance*>(this)->supportedLayers(); } |
147 | QVulkanInfoVector<QVulkanExtension> supportedExtensions(); |
148 | inline QVulkanInfoVector<QVulkanExtension> supportedExtensions() const |
149 | { return const_cast<QVulkanInstance*>(this)->supportedExtensions(); } |
150 | QVersionNumber supportedApiVersion() const; |
151 | |
152 | void setVkInstance(VkInstance existingVkInstance); |
153 | |
154 | void setFlags(Flags flags); |
155 | void setLayers(const QByteArrayList &layers); |
156 | void setExtensions(const QByteArrayList &extensions); |
157 | void setApiVersion(const QVersionNumber &vulkanVersion); |
158 | |
159 | bool create(); |
160 | void destroy(); |
161 | bool isValid() const; |
162 | VkResult errorCode() const; |
163 | |
164 | VkInstance vkInstance() const; |
165 | |
166 | Flags flags() const; |
167 | QByteArrayList layers() const; |
168 | QByteArrayList extensions() const; |
169 | QVersionNumber apiVersion() const; |
170 | |
171 | PFN_vkVoidFunction getInstanceProcAddr(const char *name); |
172 | |
173 | QPlatformVulkanInstance *handle() const; |
174 | |
175 | QVulkanFunctions *functions() const; |
176 | QVulkanDeviceFunctions *deviceFunctions(VkDevice device); |
177 | void resetDeviceFunctions(VkDevice device); |
178 | |
179 | static VkSurfaceKHR surfaceForWindow(QWindow *window); |
180 | |
181 | bool supportsPresent(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex, QWindow *window); |
182 | |
183 | void presentAboutToBeQueued(QWindow *window); |
184 | void presentQueued(QWindow *window); |
185 | |
186 | typedef bool (*DebugFilter)(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, uint64_t object, |
187 | size_t location, int32_t messageCode, const char *pLayerPrefix, const char *pMessage); |
188 | void installDebugOutputFilter(DebugFilter filter); |
189 | void removeDebugOutputFilter(DebugFilter filter); |
190 | |
191 | enum DebugMessageSeverityFlag { |
192 | VerboseSeverity = 0x01, |
193 | InfoSeverity = 0x02, |
194 | WarningSeverity = 0x04, |
195 | ErrorSeverity = 0x08 |
196 | }; |
197 | Q_DECLARE_FLAGS(DebugMessageSeverityFlags, DebugMessageSeverityFlag) |
198 | |
199 | enum DebugMessageTypeFlag { |
200 | GeneralMessage = 0x01, |
201 | ValidationMessage = 0x02, |
202 | PerformanceMessage = 0x04 |
203 | }; |
204 | Q_DECLARE_FLAGS(DebugMessageTypeFlags, DebugMessageTypeFlag) |
205 | |
206 | using DebugUtilsFilter = std::function<bool(DebugMessageSeverityFlags severity, DebugMessageTypeFlags type, const void *message)>; |
207 | void installDebugOutputFilter(DebugUtilsFilter filter); |
208 | void clearDebugOutputFilters(); |
209 | |
210 | private: |
211 | friend class QVulkanInstancePrivate; |
212 | QScopedPointer<QVulkanInstancePrivate> d_ptr; |
213 | Q_DISABLE_COPY(QVulkanInstance) |
214 | }; |
215 | |
216 | Q_DECLARE_OPERATORS_FOR_FLAGS(QVulkanInstance::Flags) |
217 | Q_DECLARE_OPERATORS_FOR_FLAGS(QVulkanInstance::DebugMessageTypeFlags) |
218 | Q_DECLARE_OPERATORS_FOR_FLAGS(QVulkanInstance::DebugMessageSeverityFlags) |
219 | |
220 | QT_END_NAMESPACE |
221 | |
222 | #endif // __has_include(<vulkan/vulkan.h>) || defined(Q_QDOC) |
223 | |
224 | #endif // QT_CONFIG(vulkan) || defined(Q_QDOC) |
225 | |
226 | #endif // QVULKANINSTANCE_H |
227 | |