1// Copyright (C) 2023 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 QSHADER_H
5#define QSHADER_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is part of the RHI API, with limited compatibility guarantees.
12// Usage of this API may make your code source and binary incompatible with
13// future versions of Qt.
14//
15
16#include <QtGui/qtguiglobal.h>
17#include <QtCore/qhash.h>
18#include <QtCore/qmap.h>
19#include <rhi/qshaderdescription.h>
20
21QT_BEGIN_NAMESPACE
22
23struct QShaderPrivate;
24class QShaderKey;
25
26#ifdef Q_OS_INTEGRITY
27 class QShaderVersion;
28 size_t qHash(const QShaderVersion &, size_t = 0) noexcept;
29#endif
30
31class Q_GUI_EXPORT QShaderVersion
32{
33public:
34 enum Flag {
35 GlslEs = 0x01
36 };
37 Q_DECLARE_FLAGS(Flags, Flag)
38
39 QShaderVersion() = default;
40 QShaderVersion(int v, Flags f = Flags());
41
42 int version() const { return m_version; }
43 void setVersion(int v) { m_version = v; }
44
45 Flags flags() const { return m_flags; }
46 void setFlags(Flags f) { m_flags = f; }
47
48private:
49 int m_version = 100;
50 Flags m_flags;
51};
52
53Q_DECLARE_OPERATORS_FOR_FLAGS(QShaderVersion::Flags)
54Q_DECLARE_TYPEINFO(QShaderVersion, Q_RELOCATABLE_TYPE);
55
56class QShaderCode;
57Q_GUI_EXPORT size_t qHash(const QShaderCode &, size_t = 0) noexcept;
58
59class Q_GUI_EXPORT QShaderCode
60{
61public:
62 QShaderCode() = default;
63 QShaderCode(const QByteArray &code, const QByteArray &entry = QByteArray());
64
65 QByteArray shader() const { return m_shader; }
66 void setShader(const QByteArray &code) { m_shader = code; }
67
68 QByteArray entryPoint() const { return m_entryPoint; }
69 void setEntryPoint(const QByteArray &entry) { m_entryPoint = entry; }
70
71private:
72 friend Q_GUI_EXPORT size_t qHash(const QShaderCode &, size_t) noexcept;
73
74 QByteArray m_shader;
75 QByteArray m_entryPoint;
76};
77
78Q_DECLARE_TYPEINFO(QShaderCode, Q_RELOCATABLE_TYPE);
79
80class Q_GUI_EXPORT QShader
81{
82public:
83 enum Stage {
84 VertexStage = 0,
85 TessellationControlStage,
86 TessellationEvaluationStage,
87 GeometryStage,
88 FragmentStage,
89 ComputeStage
90 };
91
92 enum Source {
93 SpirvShader = 0,
94 GlslShader,
95 HlslShader,
96 DxbcShader, // fxc
97 MslShader,
98 DxilShader, // dxc
99 MetalLibShader, // xcrun metal + xcrun metallib
100 WgslShader
101 };
102
103 enum Variant {
104 StandardShader = 0,
105 BatchableVertexShader,
106 UInt16IndexedVertexAsComputeShader,
107 UInt32IndexedVertexAsComputeShader,
108 NonIndexedVertexAsComputeShader
109 };
110
111 enum class SerializedFormatVersion {
112 Latest = 0,
113 Qt_6_5,
114 Qt_6_4
115 };
116
117 QShader();
118 QShader(const QShader &other);
119 QShader &operator=(const QShader &other);
120 ~QShader();
121 void detach();
122
123 bool isValid() const;
124
125 Stage stage() const;
126 void setStage(Stage stage);
127
128 QShaderDescription description() const;
129 void setDescription(const QShaderDescription &desc);
130
131 QList<QShaderKey> availableShaders() const;
132 QShaderCode shader(const QShaderKey &key) const;
133 void setShader(const QShaderKey &key, const QShaderCode &shader);
134 void removeShader(const QShaderKey &key);
135
136 QByteArray serialized(SerializedFormatVersion version = SerializedFormatVersion::Latest) const;
137 static QShader fromSerialized(const QByteArray &data);
138
139 using NativeResourceBindingMap = QMap<int, QPair<int, int> >; // binding -> native_binding[, native_binding]
140 NativeResourceBindingMap nativeResourceBindingMap(const QShaderKey &key) const;
141 void setResourceBindingMap(const QShaderKey &key, const NativeResourceBindingMap &map);
142 void removeResourceBindingMap(const QShaderKey &key);
143
144 struct SeparateToCombinedImageSamplerMapping {
145 QByteArray combinedSamplerName;
146 int textureBinding;
147 int samplerBinding;
148 };
149 using SeparateToCombinedImageSamplerMappingList = QList<SeparateToCombinedImageSamplerMapping>;
150 SeparateToCombinedImageSamplerMappingList separateToCombinedImageSamplerMappingList(const QShaderKey &key) const;
151 void setSeparateToCombinedImageSamplerMappingList(const QShaderKey &key,
152 const SeparateToCombinedImageSamplerMappingList &list);
153 void removeSeparateToCombinedImageSamplerMappingList(const QShaderKey &key);
154
155 struct NativeShaderInfo {
156 int flags = 0;
157 QMap<int, int> extraBufferBindings;
158 };
159 NativeShaderInfo nativeShaderInfo(const QShaderKey &key) const;
160 void setNativeShaderInfo(const QShaderKey &key, const NativeShaderInfo &info);
161 void removeNativeShaderInfo(const QShaderKey &key);
162
163private:
164 QShaderPrivate *d;
165 friend struct QShaderPrivate;
166 friend Q_GUI_EXPORT bool operator==(const QShader &, const QShader &) noexcept;
167 friend Q_GUI_EXPORT size_t qHash(const QShader &, size_t) noexcept;
168#ifndef QT_NO_DEBUG_STREAM
169 friend Q_GUI_EXPORT QDebug operator<<(QDebug, const QShader &);
170#endif
171};
172
173class Q_GUI_EXPORT QShaderKey
174{
175public:
176 QShaderKey() = default;
177 QShaderKey(QShader::Source s,
178 const QShaderVersion &sver,
179 QShader::Variant svar = QShader::StandardShader);
180
181 QShader::Source source() const { return m_source; }
182 void setSource(QShader::Source s) { m_source = s; }
183
184 QShaderVersion sourceVersion() const { return m_sourceVersion; }
185 void setSourceVersion(const QShaderVersion &sver) { m_sourceVersion = sver; }
186
187 QShader::Variant sourceVariant() const { return m_sourceVariant; }
188 void setSourceVariant(QShader::Variant svar) { m_sourceVariant = svar; }
189
190private:
191 QShader::Source m_source = QShader::SpirvShader;
192 QShaderVersion m_sourceVersion;
193 QShader::Variant m_sourceVariant = QShader::StandardShader;
194};
195
196Q_DECLARE_TYPEINFO(QShaderKey, Q_RELOCATABLE_TYPE);
197
198Q_GUI_EXPORT bool operator==(const QShader &lhs, const QShader &rhs) noexcept;
199Q_GUI_EXPORT size_t qHash(const QShader &s, size_t seed = 0) noexcept;
200
201inline bool operator!=(const QShader &lhs, const QShader &rhs) noexcept
202{
203 return !(lhs == rhs);
204}
205
206Q_GUI_EXPORT bool operator==(const QShaderVersion &lhs, const QShaderVersion &rhs) noexcept;
207Q_GUI_EXPORT bool operator<(const QShaderVersion &lhs, const QShaderVersion &rhs) noexcept;
208Q_GUI_EXPORT bool operator==(const QShaderKey &lhs, const QShaderKey &rhs) noexcept;
209Q_GUI_EXPORT bool operator<(const QShaderKey &lhs, const QShaderKey &rhs) noexcept;
210Q_GUI_EXPORT bool operator==(const QShaderCode &lhs, const QShaderCode &rhs) noexcept;
211
212inline bool operator!=(const QShaderVersion &lhs, const QShaderVersion &rhs) noexcept
213{
214 return !(lhs == rhs);
215}
216
217inline bool operator!=(const QShaderKey &lhs, const QShaderKey &rhs) noexcept
218{
219 return !(lhs == rhs);
220}
221
222inline bool operator!=(const QShaderCode &lhs, const QShaderCode &rhs) noexcept
223{
224 return !(lhs == rhs);
225}
226
227Q_GUI_EXPORT size_t qHash(const QShaderKey &k, size_t seed = 0) noexcept;
228
229#ifndef QT_NO_DEBUG_STREAM
230Q_GUI_EXPORT QDebug operator<<(QDebug, const QShader &);
231Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QShaderKey &k);
232Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QShaderVersion &v);
233#endif
234
235QT_END_NAMESPACE
236
237#endif
238

source code of qtbase/src/gui/rhi/qshader.h