Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qvulkaninstance.h
Go to the documentation of this file.
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
28typedef void* PFN_vkVoidFunction;
29// non-dispatchable handles (64-bit regardless of arch)
30typedef quint64 VkSurfaceKHR;
31typedef quint64 VkImage;
32typedef quint64 VkImageView;
33// dispatchable handles (32 or 64-bit depending on arch)
34typedef void* VkInstance;
35typedef void* VkPhysicalDevice;
36typedef void* VkDevice;
37// enums
38typedef int VkResult;
39typedef int VkFormat;
40typedef int VkImageLayout;
41typedef int VkDebugReportFlagsEXT;
42typedef 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/qhashfunctions.h>
52#include <QtCore/qlist.h>
53#include <QtCore/qscopedpointer.h>
54#include <QtCore/qversionnumber.h>
55
56QT_BEGIN_NAMESPACE
57
58class QDebug;
59
60class QVulkanInstancePrivate;
61class QPlatformVulkanInstance;
62class QVulkanFunctions;
63class QVulkanDeviceFunctions;
64class QWindow;
65
66struct QVulkanLayer
67{
68 QByteArray name;
69 uint32_t version;
70 QVersionNumber specVersion;
71 QByteArray description;
72};
73Q_DECLARE_TYPEINFO(QVulkanLayer, Q_RELOCATABLE_TYPE);
74
75inline bool operator==(const QVulkanLayer &lhs, const QVulkanLayer &rhs) noexcept
76{
77 return lhs.name == rhs.name && lhs.version == rhs.version && lhs.specVersion == rhs.specVersion;
78}
79inline bool operator!=(const QVulkanLayer &lhs, const QVulkanLayer &rhs) noexcept
80{ return !(lhs == rhs); }
81
82inline size_t qHash(const QVulkanLayer &key, size_t seed = 0) noexcept
83{
84 QtPrivate::QHashCombine hash(seed);
85 seed = hash(seed, key.name);
86 seed = hash(seed, key.version);
87 seed = hash(seed, key.specVersion);
88 return seed;
89}
90
91struct QVulkanExtension
92{
93 QByteArray name;
94 uint32_t version;
95};
96Q_DECLARE_TYPEINFO(QVulkanExtension, Q_RELOCATABLE_TYPE);
97
98inline bool operator==(const QVulkanExtension &lhs, const QVulkanExtension &rhs) noexcept
99{
100 return lhs.name == rhs.name && lhs.version == rhs.version;
101}
102inline bool operator!=(const QVulkanExtension &lhs, const QVulkanExtension &rhs) noexcept
103{ return !(lhs == rhs); }
104
105inline size_t qHash(const QVulkanExtension &key, size_t seed = 0) noexcept
106{
107 QtPrivate::QHashCombine hash(seed);
108 seed = hash(seed, key.name);
109 seed = hash(seed, key.version);
110 return seed;
111}
112
113#ifndef QT_NO_DEBUG_STREAM
114Q_GUI_EXPORT QDebug operator<<(QDebug, const QVulkanLayer &);
115Q_GUI_EXPORT QDebug operator<<(QDebug, const QVulkanExtension &);
116#endif
117
118template<typename T>
119class QVulkanInfoVector : public QList<T>
120{
121public:
122 bool contains(const QByteArray &name) const {
123 return std::any_of(this->cbegin(), this->cend(), [&](const T &entry) {
124 return entry.name == name; });
125 }
126 bool contains(const QByteArray &name, int minVersion) const {
127 return std::any_of(this->cbegin(), this->cend(), [&](const T &entry) {
128 return entry.name == name && entry.version >= minVersion; });
129 }
130};
131
132class Q_GUI_EXPORT QVulkanInstance
133{
134public:
135 QVulkanInstance();
136 ~QVulkanInstance();
137
138 enum Flag {
139 NoDebugOutputRedirect = 0x01,
140 NoPortabilityDrivers = 0x02
141 };
142 Q_DECLARE_FLAGS(Flags, Flag)
143
144 // ### Qt 7: remove non-const overloads
145 QVulkanInfoVector<QVulkanLayer> supportedLayers();
146 inline QVulkanInfoVector<QVulkanLayer> supportedLayers() const
147 { return const_cast<QVulkanInstance*>(this)->supportedLayers(); }
148 QVulkanInfoVector<QVulkanExtension> supportedExtensions();
149 inline QVulkanInfoVector<QVulkanExtension> supportedExtensions() const
150 { return const_cast<QVulkanInstance*>(this)->supportedExtensions(); }
151 QVersionNumber supportedApiVersion() const;
152
153 void setVkInstance(VkInstance existingVkInstance);
154
155 void setFlags(Flags flags);
156 void setLayers(const QByteArrayList &layers);
157 void setExtensions(const QByteArrayList &extensions);
158 void setApiVersion(const QVersionNumber &vulkanVersion);
159
160 bool create();
161 void destroy();
162 bool isValid() const;
163 VkResult errorCode() const;
164
165 VkInstance vkInstance() const;
166
167 Flags flags() const;
168 QByteArrayList layers() const;
169 QByteArrayList extensions() const;
170 QVersionNumber apiVersion() const;
171
172 PFN_vkVoidFunction getInstanceProcAddr(const char *name);
173
174 QPlatformVulkanInstance *handle() const;
175
176 QVulkanFunctions *functions() const;
177 QVulkanDeviceFunctions *deviceFunctions(VkDevice device);
178 void resetDeviceFunctions(VkDevice device);
179
180 static VkSurfaceKHR surfaceForWindow(QWindow *window);
181
182 bool supportsPresent(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex, QWindow *window);
183
184 void presentAboutToBeQueued(QWindow *window);
185 void presentQueued(QWindow *window);
186
187 typedef bool (*DebugFilter)(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, uint64_t object,
188 size_t location, int32_t messageCode, const char *pLayerPrefix, const char *pMessage);
189 void installDebugOutputFilter(DebugFilter filter);
190 void removeDebugOutputFilter(DebugFilter filter);
191
192 enum DebugMessageSeverityFlag {
193 VerboseSeverity = 0x01,
194 InfoSeverity = 0x02,
195 WarningSeverity = 0x04,
196 ErrorSeverity = 0x08
197 };
198 Q_DECLARE_FLAGS(DebugMessageSeverityFlags, DebugMessageSeverityFlag)
199
200 enum DebugMessageTypeFlag {
201 GeneralMessage = 0x01,
202 ValidationMessage = 0x02,
203 PerformanceMessage = 0x04
204 };
205 Q_DECLARE_FLAGS(DebugMessageTypeFlags, DebugMessageTypeFlag)
206
207 using DebugUtilsFilter = std::function<bool(DebugMessageSeverityFlags severity, DebugMessageTypeFlags type, const void *message)>;
208 void installDebugOutputFilter(DebugUtilsFilter filter);
209 void clearDebugOutputFilters();
210
211private:
212 friend class QVulkanInstancePrivate;
213 QScopedPointer<QVulkanInstancePrivate> d_ptr;
214 Q_DISABLE_COPY(QVulkanInstance)
215};
216
217Q_DECLARE_OPERATORS_FOR_FLAGS(QVulkanInstance::Flags)
218Q_DECLARE_OPERATORS_FOR_FLAGS(QVulkanInstance::DebugMessageTypeFlags)
219Q_DECLARE_OPERATORS_FOR_FLAGS(QVulkanInstance::DebugMessageSeverityFlags)
220
221QT_END_NAMESPACE
222
223#endif // __has_include(<vulkan/vulkan.h>) || defined(Q_QDOC)
224
225#endif // QT_CONFIG(vulkan) || defined(Q_QDOC)
226
227#endif // QVULKANINSTANCE_H
A collection of static helper functions for Vulkan support.