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 #endif
48 
49 #if QT_CONFIG(vulkan) || defined(Q_CLANG_QDOC)
50 
51 #ifndef VK_NO_PROTOTYPES
52 #define VK_NO_PROTOTYPES
53 #endif
54 #ifndef Q_CLANG_QDOC
55 #include <vulkan/vulkan.h>
56 #else
57 typedef void* PFN_vkVoidFunction;
58 typedef unsigned long VkSurfaceKHR;
59 typedef unsigned long VkImage;
60 typedef unsigned long VkImageView;
61 typedef void* VkInstance;
62 typedef void* VkPhysicalDevice;
63 typedef void* VkDevice;
64 typedef int VkResult;
65 #endif
66 
67 #include <QtCore/qhashfunctions.h>
68 #include <QtCore/qscopedpointer.h>
69 #include <QtCore/qvector.h>
70 #include <QtCore/qbytearraylist.h>
71 #include <QtCore/qversionnumber.h>
72 #include <QtCore/qdebug.h>
73 
74 QT_BEGIN_NAMESPACE
75 
76 class QVulkanInstancePrivate;
77 class QPlatformVulkanInstance;
78 class QVulkanFunctions;
79 class QVulkanDeviceFunctions;
80 class QWindow;
81 
82 struct QVulkanLayer
83 {
84     QByteArray name;
85     uint32_t version;
86     QVersionNumber specVersion;
87     QByteArray description;
88 };
89 Q_DECLARE_TYPEINFO(QVulkanLayer, Q_MOVABLE_TYPE);
90 
91 inline bool operator==(const QVulkanLayer &lhs, const QVulkanLayer &rhs) noexcept
92 {
93     return lhs.name == rhs.name && lhs.version == rhs.version && lhs.specVersion == rhs.specVersion;
94 }
95 inline bool operator!=(const QVulkanLayer &lhs, const QVulkanLayer &rhs) noexcept
96 { return !(lhs == rhs); }
97 
98 inline uint qHash(const QVulkanLayer &key, uint seed = 0) noexcept
99 {
100     QtPrivate::QHashCombine hash;
101     seed = hash(seed, key.name);
102     seed = hash(seed, key.version);
103     seed = hash(seed, key.specVersion);
104     return seed;
105 }
106 
107 struct QVulkanExtension
108 {
109     QByteArray name;
110     uint32_t version;
111 };
112 Q_DECLARE_TYPEINFO(QVulkanExtension, Q_MOVABLE_TYPE);
113 
114 inline bool operator==(const QVulkanExtension &lhs, const QVulkanExtension &rhs) noexcept
115 {
116     return lhs.name == rhs.name && lhs.version == rhs.version;
117 }
118 inline bool operator!=(const QVulkanExtension &lhs, const QVulkanExtension &rhs) noexcept
119 { return !(lhs == rhs); }
120 
121 inline uint qHash(const QVulkanExtension &key, uint seed = 0) noexcept
122 {
123     QtPrivate::QHashCombine hash;
124     seed = hash(seed, key.name);
125     seed = hash(seed, key.version);
126     return seed;
127 }
128 
129 #ifndef QT_NO_DEBUG_STREAM
130 Q_GUI_EXPORT QDebug operator<<(QDebug, const QVulkanLayer &);
131 Q_GUI_EXPORT QDebug operator<<(QDebug, const QVulkanExtension &);
132 #endif
133 
134 template<typename T>
135 class QVulkanInfoVector : public QVector<T>
136 {
137 public:
contains(const QByteArray & name)138     bool contains(const QByteArray &name) const {
139         return std::any_of(this->cbegin(), this->cend(), [&](const T &entry) {
140             return entry.name == name; });
141     }
contains(const QByteArray & name,int minVersion)142     bool contains(const QByteArray &name, int minVersion) const {
143         return std::any_of(this->cbegin(), this->cend(), [&](const T &entry) {
144             return entry.name == name && entry.version >= minVersion; });
145     }
146 };
147 
148 class Q_GUI_EXPORT QVulkanInstance
149 {
150 public:
151     QVulkanInstance();
152     ~QVulkanInstance();
153 
154     enum Flag {
155         NoDebugOutputRedirect = 0x01
156     };
157     Q_DECLARE_FLAGS(Flags, Flag)
158 
159     QVulkanInfoVector<QVulkanLayer> supportedLayers();
160     QVulkanInfoVector<QVulkanExtension> supportedExtensions();
161 
162     void setVkInstance(VkInstance existingVkInstance);
163 
164     void setFlags(Flags flags);
165     void setLayers(const QByteArrayList &layers);
166     void setExtensions(const QByteArrayList &extensions);
167     void setApiVersion(const QVersionNumber &vulkanVersion);
168 
169     bool create();
170     void destroy();
171     bool isValid() const;
172     VkResult errorCode() const;
173 
174     VkInstance vkInstance() const;
175 
176     Flags flags() const;
177     QByteArrayList layers() const;
178     QByteArrayList extensions() const;
179     QVersionNumber apiVersion() const;
180 
181     PFN_vkVoidFunction getInstanceProcAddr(const char *name);
182 
183     QPlatformVulkanInstance *handle() const;
184 
185     QVulkanFunctions *functions() const;
186     QVulkanDeviceFunctions *deviceFunctions(VkDevice device);
187     void resetDeviceFunctions(VkDevice device);
188 
189     static VkSurfaceKHR surfaceForWindow(QWindow *window);
190 
191     bool supportsPresent(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex, QWindow *window);
192 
193     void presentAboutToBeQueued(QWindow *window);
194     void presentQueued(QWindow *window);
195 
196     typedef bool (*DebugFilter)(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, uint64_t object,
197                                 size_t location, int32_t messageCode, const char *pLayerPrefix, const char *pMessage);
198     void installDebugOutputFilter(DebugFilter filter);
199     void removeDebugOutputFilter(DebugFilter filter);
200 
201 private:
202     QScopedPointer<QVulkanInstancePrivate> d_ptr;
203     Q_DISABLE_COPY(QVulkanInstance)
204 };
205 
206 Q_DECLARE_OPERATORS_FOR_FLAGS(QVulkanInstance::Flags)
207 
208 QT_END_NAMESPACE
209 
210 #endif // QT_CONFIG(vulkan)
211 
212 #endif // QVULKANINSTANCE_H
213