1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QVULKANINSTANCE_H
41#define QVULKANINSTANCE_H
42
43#include <QtGui/qtguiglobal.h>
44
45#if 0
46#pragma qt_no_master_include
47#pragma qt_sync_skip_header_check
48#endif
49
50#if QT_CONFIG(vulkan) || defined(Q_CLANG_QDOC)
51
52#ifndef VK_NO_PROTOTYPES
53#define VK_NO_PROTOTYPES
54#endif
55#if !defined(Q_CLANG_QDOC) && __has_include(<vulkan/vulkan.h>)
56#include <vulkan/vulkan.h>
57#else
58// QT_CONFIG(vulkan) implies vulkan.h being available at Qt build time, but it
59// does not guarantee vulkan.h is available at *application* build time. Both
60// for qdoc and for apps built on systems without Vulkan SDK we provide a set
61// of typedefs to keep things compiling since this header may be included from
62// Qt Quick and elsewhere just to get types like VkImage and friends defined.
63
64typedef void* PFN_vkVoidFunction;
65// non-dispatchable handles (64-bit regardless of arch)
66typedef quint64 VkSurfaceKHR;
67typedef quint64 VkImage;
68typedef quint64 VkImageView;
69// dispatchable handles (32 or 64-bit depending on arch)
70typedef void* VkInstance;
71typedef void* VkPhysicalDevice;
72typedef void* VkDevice;
73// enums
74typedef int VkResult;
75typedef int VkImageLayout;
76typedef int VkDebugReportFlagsEXT;
77typedef int VkDebugReportObjectTypeEXT;
78#endif
79
80// QVulkanInstance itself is only applicable if vulkan.h is available, or if
81// it's qdoc. An application that is built on a vulkan.h-less system against a
82// Vulkan-enabled Qt gets the dummy typedefs but not QVulkan*.
83#if __has_include(<vulkan/vulkan.h>) || defined(Q_CLANG_QDOC)
84
85#include <QtCore/qbytearraylist.h>
86#include <QtCore/qdebug.h>
87#include <QtCore/qhashfunctions.h>
88#include <QtCore/qlist.h>
89#include <QtCore/qscopedpointer.h>
90#include <QtCore/qversionnumber.h>
91
92QT_BEGIN_NAMESPACE
93
94class QVulkanInstancePrivate;
95class QPlatformVulkanInstance;
96class QVulkanFunctions;
97class QVulkanDeviceFunctions;
98class QWindow;
99
100struct QVulkanLayer
101{
102 QByteArray name;
103 uint32_t version;
104 QVersionNumber specVersion;
105 QByteArray description;
106};
107Q_DECLARE_TYPEINFO(QVulkanLayer, Q_MOVABLE_TYPE);
108
109inline bool operator==(const QVulkanLayer &lhs, const QVulkanLayer &rhs) noexcept
110{
111 return lhs.name == rhs.name && lhs.version == rhs.version && lhs.specVersion == rhs.specVersion;
112}
113inline bool operator!=(const QVulkanLayer &lhs, const QVulkanLayer &rhs) noexcept
114{ return !(lhs == rhs); }
115
116inline size_t qHash(const QVulkanLayer &key, size_t seed = 0) noexcept
117{
118 QtPrivate::QHashCombine hash;
119 seed = hash(seed, key.name);
120 seed = hash(seed, key.version);
121 seed = hash(seed, key.specVersion);
122 return seed;
123}
124
125struct QVulkanExtension
126{
127 QByteArray name;
128 uint32_t version;
129};
130Q_DECLARE_TYPEINFO(QVulkanExtension, Q_MOVABLE_TYPE);
131
132inline bool operator==(const QVulkanExtension &lhs, const QVulkanExtension &rhs) noexcept
133{
134 return lhs.name == rhs.name && lhs.version == rhs.version;
135}
136inline bool operator!=(const QVulkanExtension &lhs, const QVulkanExtension &rhs) noexcept
137{ return !(lhs == rhs); }
138
139inline size_t qHash(const QVulkanExtension &key, size_t seed = 0) noexcept
140{
141 QtPrivate::QHashCombine hash;
142 seed = hash(seed, key.name);
143 seed = hash(seed, key.version);
144 return seed;
145}
146
147#ifndef QT_NO_DEBUG_STREAM
148Q_GUI_EXPORT QDebug operator<<(QDebug, const QVulkanLayer &);
149Q_GUI_EXPORT QDebug operator<<(QDebug, const QVulkanExtension &);
150#endif
151
152template<typename T>
153class QVulkanInfoVector : public QList<T>
154{
155public:
156 bool contains(const QByteArray &name) const {
157 return std::any_of(this->cbegin(), this->cend(), [&](const T &entry) {
158 return entry.name == name; });
159 }
160 bool contains(const QByteArray &name, int minVersion) const {
161 return std::any_of(this->cbegin(), this->cend(), [&](const T &entry) {
162 return entry.name == name && entry.version >= minVersion; });
163 }
164};
165
166class Q_GUI_EXPORT QVulkanInstance
167{
168public:
169 QVulkanInstance();
170 ~QVulkanInstance();
171
172 enum Flag {
173 NoDebugOutputRedirect = 0x01
174 };
175 Q_DECLARE_FLAGS(Flags, Flag)
176
177 QVulkanInfoVector<QVulkanLayer> supportedLayers();
178 QVulkanInfoVector<QVulkanExtension> supportedExtensions();
179
180 void setVkInstance(VkInstance existingVkInstance);
181
182 void setFlags(Flags flags);
183 void setLayers(const QByteArrayList &layers);
184 void setExtensions(const QByteArrayList &extensions);
185 void setApiVersion(const QVersionNumber &vulkanVersion);
186
187 bool create();
188 void destroy();
189 bool isValid() const;
190 VkResult errorCode() const;
191
192 VkInstance vkInstance() const;
193
194 Flags flags() const;
195 QByteArrayList layers() const;
196 QByteArrayList extensions() const;
197 QVersionNumber apiVersion() const;
198
199 PFN_vkVoidFunction getInstanceProcAddr(const char *name);
200
201 QPlatformVulkanInstance *handle() const;
202
203 QVulkanFunctions *functions() const;
204 QVulkanDeviceFunctions *deviceFunctions(VkDevice device);
205 void resetDeviceFunctions(VkDevice device);
206
207 static VkSurfaceKHR surfaceForWindow(QWindow *window);
208
209 bool supportsPresent(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex, QWindow *window);
210
211 void presentAboutToBeQueued(QWindow *window);
212 void presentQueued(QWindow *window);
213
214 typedef bool (*DebugFilter)(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, uint64_t object,
215 size_t location, int32_t messageCode, const char *pLayerPrefix, const char *pMessage);
216 void installDebugOutputFilter(DebugFilter filter);
217 void removeDebugOutputFilter(DebugFilter filter);
218
219private:
220 QScopedPointer<QVulkanInstancePrivate> d_ptr;
221 Q_DISABLE_COPY(QVulkanInstance)
222};
223
224Q_DECLARE_OPERATORS_FOR_FLAGS(QVulkanInstance::Flags)
225
226QT_END_NAMESPACE
227
228#endif // __has_include(<vulkan/vulkan.h>) || defined(Q_CLANG_QDOC)
229
230#endif // QT_CONFIG(vulkan) || defined(Q_CLANG_QDOC)
231
232#endif // QVULKANINSTANCE_H
233