1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Gui module
7**
8** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#ifndef QSHADERDESCRIPTION_H
38#define QSHADERDESCRIPTION_H
39
40//
41// W A R N I N G
42// -------------
43//
44// This file is not part of the Qt API. It exists for the convenience
45// of a number of Qt sources files. This header file may change from
46// version to version without notice, or even be removed.
47//
48// We mean it.
49//
50
51#include <QtGui/qtguiglobal.h>
52#include <QtCore/QString>
53#include <QtCore/QList>
54#include <array>
55
56QT_BEGIN_NAMESPACE
57
58struct QShaderDescriptionPrivate;
59class QDataStream;
60
61class Q_GUI_EXPORT QShaderDescription
62{
63public:
64 QShaderDescription();
65 QShaderDescription(const QShaderDescription &other);
66 QShaderDescription &operator=(const QShaderDescription &other);
67 ~QShaderDescription();
68 void detach();
69
70 bool isValid() const;
71
72 void serialize(QDataStream *stream) const;
73 QByteArray toJson() const;
74
75 static QShaderDescription deserialize(QDataStream *stream, int version);
76
77 enum VariableType {
78 Unknown = 0,
79
80 // do not reorder
81 Float,
82 Vec2,
83 Vec3,
84 Vec4,
85 Mat2,
86 Mat2x3,
87 Mat2x4,
88 Mat3,
89 Mat3x2,
90 Mat3x4,
91 Mat4,
92 Mat4x2,
93 Mat4x3,
94
95 Int,
96 Int2,
97 Int3,
98 Int4,
99
100 Uint,
101 Uint2,
102 Uint3,
103 Uint4,
104
105 Bool,
106 Bool2,
107 Bool3,
108 Bool4,
109
110 Double,
111 Double2,
112 Double3,
113 Double4,
114 DMat2,
115 DMat2x3,
116 DMat2x4,
117 DMat3,
118 DMat3x2,
119 DMat3x4,
120 DMat4,
121 DMat4x2,
122 DMat4x3,
123
124 Sampler1D,
125 Sampler2D,
126 Sampler2DMS,
127 Sampler3D,
128 SamplerCube,
129 Sampler1DArray,
130 Sampler2DArray,
131 Sampler2DMSArray,
132 Sampler3DArray,
133 SamplerCubeArray,
134 SamplerRect,
135 SamplerBuffer,
136
137 Image1D,
138 Image2D,
139 Image2DMS,
140 Image3D,
141 ImageCube,
142 Image1DArray,
143 Image2DArray,
144 Image2DMSArray,
145 Image3DArray,
146 ImageCubeArray,
147 ImageRect,
148 ImageBuffer,
149
150 Struct
151 };
152
153 enum ImageFormat {
154 // must match SPIR-V's ImageFormat
155 ImageFormatUnknown = 0,
156 ImageFormatRgba32f = 1,
157 ImageFormatRgba16f = 2,
158 ImageFormatR32f = 3,
159 ImageFormatRgba8 = 4,
160 ImageFormatRgba8Snorm = 5,
161 ImageFormatRg32f = 6,
162 ImageFormatRg16f = 7,
163 ImageFormatR11fG11fB10f = 8,
164 ImageFormatR16f = 9,
165 ImageFormatRgba16 = 10,
166 ImageFormatRgb10A2 = 11,
167 ImageFormatRg16 = 12,
168 ImageFormatRg8 = 13,
169 ImageFormatR16 = 14,
170 ImageFormatR8 = 15,
171 ImageFormatRgba16Snorm = 16,
172 ImageFormatRg16Snorm = 17,
173 ImageFormatRg8Snorm = 18,
174 ImageFormatR16Snorm = 19,
175 ImageFormatR8Snorm = 20,
176 ImageFormatRgba32i = 21,
177 ImageFormatRgba16i = 22,
178 ImageFormatRgba8i = 23,
179 ImageFormatR32i = 24,
180 ImageFormatRg32i = 25,
181 ImageFormatRg16i = 26,
182 ImageFormatRg8i = 27,
183 ImageFormatR16i = 28,
184 ImageFormatR8i = 29,
185 ImageFormatRgba32ui = 30,
186 ImageFormatRgba16ui = 31,
187 ImageFormatRgba8ui = 32,
188 ImageFormatR32ui = 33,
189 ImageFormatRgb10a2ui = 34,
190 ImageFormatRg32ui = 35,
191 ImageFormatRg16ui = 36,
192 ImageFormatRg8ui = 37,
193 ImageFormatR16ui = 38,
194 ImageFormatR8ui = 39
195 };
196
197 enum ImageFlag {
198 ReadOnlyImage = 1 << 0,
199 WriteOnlyImage = 1 << 1
200 };
201 Q_DECLARE_FLAGS(ImageFlags, ImageFlag)
202
203 // Optional data (like decorations) usually default to an otherwise invalid value (-1 or 0). This is intentional.
204
205 struct InOutVariable {
206 QByteArray name;
207 VariableType type = Unknown;
208 int location = -1;
209 int binding = -1;
210 int descriptorSet = -1;
211 ImageFormat imageFormat = ImageFormatUnknown;
212 ImageFlags imageFlags;
213 QList<int> arrayDims;
214 };
215
216 struct BlockVariable {
217 QByteArray name;
218 VariableType type = Unknown;
219 int offset = 0;
220 int size = 0;
221 QList<int> arrayDims;
222 int arrayStride = 0;
223 int matrixStride = 0;
224 bool matrixIsRowMajor = false;
225 QList<BlockVariable> structMembers;
226 };
227
228 struct UniformBlock {
229 QByteArray blockName;
230 QByteArray structName; // instanceName
231 int size = 0;
232 int binding = -1;
233 int descriptorSet = -1;
234 QList<BlockVariable> members;
235 };
236
237 struct PushConstantBlock {
238 QByteArray name;
239 int size = 0;
240 QList<BlockVariable> members;
241 };
242
243 struct StorageBlock {
244 QByteArray blockName;
245 QByteArray instanceName;
246 int knownSize = 0;
247 int binding = -1;
248 int descriptorSet = -1;
249 QList<BlockVariable> members;
250 };
251
252 QList<InOutVariable> inputVariables() const;
253 QList<InOutVariable> outputVariables() const;
254 QList<UniformBlock> uniformBlocks() const;
255 QList<PushConstantBlock> pushConstantBlocks() const;
256 QList<StorageBlock> storageBlocks() const;
257 QList<InOutVariable> combinedImageSamplers() const;
258 QList<InOutVariable> storageImages() const;
259
260 std::array<uint, 3> computeShaderLocalSize() const;
261
262private:
263 QShaderDescriptionPrivate *d;
264 friend struct QShaderDescriptionPrivate;
265#ifndef QT_NO_DEBUG_STREAM
266 friend Q_GUI_EXPORT QDebug operator<<(QDebug, const QShaderDescription &);
267#endif
268 friend Q_GUI_EXPORT bool operator==(const QShaderDescription &lhs, const QShaderDescription &rhs) noexcept;
269};
270
271Q_DECLARE_OPERATORS_FOR_FLAGS(QShaderDescription::ImageFlags)
272
273#ifndef QT_NO_DEBUG_STREAM
274Q_GUI_EXPORT QDebug operator<<(QDebug, const QShaderDescription &);
275Q_GUI_EXPORT QDebug operator<<(QDebug, const QShaderDescription::InOutVariable &);
276Q_GUI_EXPORT QDebug operator<<(QDebug, const QShaderDescription::BlockVariable &);
277Q_GUI_EXPORT QDebug operator<<(QDebug, const QShaderDescription::UniformBlock &);
278Q_GUI_EXPORT QDebug operator<<(QDebug, const QShaderDescription::PushConstantBlock &);
279Q_GUI_EXPORT QDebug operator<<(QDebug, const QShaderDescription::StorageBlock &);
280#endif
281
282Q_GUI_EXPORT bool operator==(const QShaderDescription &lhs, const QShaderDescription &rhs) noexcept;
283Q_GUI_EXPORT bool operator==(const QShaderDescription::InOutVariable &lhs, const QShaderDescription::InOutVariable &rhs) noexcept;
284Q_GUI_EXPORT bool operator==(const QShaderDescription::BlockVariable &lhs, const QShaderDescription::BlockVariable &rhs) noexcept;
285Q_GUI_EXPORT bool operator==(const QShaderDescription::UniformBlock &lhs, const QShaderDescription::UniformBlock &rhs) noexcept;
286Q_GUI_EXPORT bool operator==(const QShaderDescription::PushConstantBlock &lhs, const QShaderDescription::PushConstantBlock &rhs) noexcept;
287Q_GUI_EXPORT bool operator==(const QShaderDescription::StorageBlock &lhs, const QShaderDescription::StorageBlock &rhs) noexcept;
288
289inline bool operator!=(const QShaderDescription &lhs, const QShaderDescription &rhs) noexcept
290{
291 return !(lhs == rhs);
292}
293
294inline bool operator!=(const QShaderDescription::InOutVariable &lhs, const QShaderDescription::InOutVariable &rhs) noexcept
295{
296 return !(lhs == rhs);
297}
298
299inline bool operator!=(const QShaderDescription::BlockVariable &lhs, const QShaderDescription::BlockVariable &rhs) noexcept
300{
301 return !(lhs == rhs);
302}
303
304inline bool operator!=(const QShaderDescription::UniformBlock &lhs, const QShaderDescription::UniformBlock &rhs) noexcept
305{
306 return !(lhs == rhs);
307}
308
309inline bool operator!=(const QShaderDescription::PushConstantBlock &lhs, const QShaderDescription::PushConstantBlock &rhs) noexcept
310{
311 return !(lhs == rhs);
312}
313
314inline bool operator!=(const QShaderDescription::StorageBlock &lhs, const QShaderDescription::StorageBlock &rhs) noexcept
315{
316 return !(lhs == rhs);
317}
318
319QT_END_NAMESPACE
320
321#endif
322