/src/qtbase/src/gui/rhi/qshader.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
21 | | QT_BEGIN_NAMESPACE |
22 | | |
23 | | struct QShaderPrivate; |
24 | | class QShaderKey; |
25 | | |
26 | | #ifdef Q_OS_INTEGRITY |
27 | | class QShaderVersion; |
28 | | size_t qHash(const QShaderVersion &, size_t = 0) noexcept; |
29 | | #endif |
30 | | |
31 | | class Q_GUI_EXPORT QShaderVersion |
32 | | { |
33 | | public: |
34 | | enum Flag { |
35 | | GlslEs = 0x01 |
36 | | }; |
37 | | Q_DECLARE_FLAGS(Flags, Flag) |
38 | | |
39 | 0 | QShaderVersion() = default; |
40 | | QShaderVersion(int v, Flags f = Flags()); |
41 | | |
42 | 0 | int version() const { return m_version; } |
43 | 0 | void setVersion(int v) { m_version = v; } |
44 | | |
45 | 0 | Flags flags() const { return m_flags; } |
46 | 0 | void setFlags(Flags f) { m_flags = f; } |
47 | | |
48 | | private: |
49 | | int m_version = 100; |
50 | | Flags m_flags; |
51 | | }; |
52 | | |
53 | | Q_DECLARE_OPERATORS_FOR_FLAGS(QShaderVersion::Flags) |
54 | | Q_DECLARE_TYPEINFO(QShaderVersion, Q_RELOCATABLE_TYPE); |
55 | | |
56 | | class QShaderCode; |
57 | | Q_GUI_EXPORT size_t qHash(const QShaderCode &, size_t = 0) noexcept; |
58 | | |
59 | | class Q_GUI_EXPORT QShaderCode |
60 | | { |
61 | | public: |
62 | 0 | QShaderCode() = default; |
63 | | QShaderCode(const QByteArray &code, const QByteArray &entry = QByteArray()); |
64 | | |
65 | 0 | QByteArray shader() const { return m_shader; } |
66 | 0 | void setShader(const QByteArray &code) { m_shader = code; } |
67 | | |
68 | 0 | QByteArray entryPoint() const { return m_entryPoint; } |
69 | 0 | void setEntryPoint(const QByteArray &entry) { m_entryPoint = entry; } |
70 | | |
71 | | private: |
72 | | friend Q_GUI_EXPORT size_t qHash(const QShaderCode &, size_t) noexcept; |
73 | | |
74 | | QByteArray m_shader; |
75 | | QByteArray m_entryPoint; |
76 | | }; |
77 | | |
78 | | Q_DECLARE_TYPEINFO(QShaderCode, Q_RELOCATABLE_TYPE); |
79 | | |
80 | | class Q_GUI_EXPORT QShader |
81 | | { |
82 | | public: |
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 | | HdrCapableFragmentShader, |
110 | | }; |
111 | | |
112 | | enum class SerializedFormatVersion { |
113 | | Latest = 0, |
114 | | Qt_6_5, |
115 | | Qt_6_4 |
116 | | }; |
117 | | |
118 | | QShader(); |
119 | | QShader(const QShader &other); |
120 | | QShader &operator=(const QShader &other); |
121 | 0 | QShader(QShader &&other) noexcept : d(std::exchange(other.d, nullptr)) {} |
122 | | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QShader) |
123 | | ~QShader(); |
124 | | |
125 | 0 | void swap(QShader &other) noexcept { qt_ptr_swap(d, other.d); } |
126 | | void detach(); |
127 | | |
128 | | bool isValid() const; |
129 | | |
130 | | Stage stage() const; |
131 | | void setStage(Stage stage); |
132 | | |
133 | | QShaderDescription description() const; |
134 | | void setDescription(const QShaderDescription &desc); |
135 | | |
136 | | QList<QShaderKey> availableShaders() const; |
137 | | QShaderCode shader(const QShaderKey &key) const; |
138 | | void setShader(const QShaderKey &key, const QShaderCode &shader); |
139 | | void removeShader(const QShaderKey &key); |
140 | | |
141 | | QByteArray serialized(SerializedFormatVersion version = SerializedFormatVersion::Latest) const; |
142 | | static QShader fromSerialized(const QByteArray &data); |
143 | | |
144 | | using NativeResourceBindingMap = QMap<int, std::pair<int, int>>; // binding -> native_binding[, native_binding] |
145 | | NativeResourceBindingMap nativeResourceBindingMap(const QShaderKey &key) const; |
146 | | void setResourceBindingMap(const QShaderKey &key, const NativeResourceBindingMap &map); |
147 | | void removeResourceBindingMap(const QShaderKey &key); |
148 | | |
149 | | struct SeparateToCombinedImageSamplerMapping { |
150 | | QByteArray combinedSamplerName; |
151 | | int textureBinding; |
152 | | int samplerBinding; |
153 | | }; |
154 | | using SeparateToCombinedImageSamplerMappingList = QList<SeparateToCombinedImageSamplerMapping>; |
155 | | SeparateToCombinedImageSamplerMappingList separateToCombinedImageSamplerMappingList(const QShaderKey &key) const; |
156 | | void setSeparateToCombinedImageSamplerMappingList(const QShaderKey &key, |
157 | | const SeparateToCombinedImageSamplerMappingList &list); |
158 | | void removeSeparateToCombinedImageSamplerMappingList(const QShaderKey &key); |
159 | | |
160 | | struct NativeShaderInfo { |
161 | | int flags = 0; |
162 | | QMap<int, int> extraBufferBindings; |
163 | | }; |
164 | | NativeShaderInfo nativeShaderInfo(const QShaderKey &key) const; |
165 | | void setNativeShaderInfo(const QShaderKey &key, const NativeShaderInfo &info); |
166 | | void removeNativeShaderInfo(const QShaderKey &key); |
167 | | |
168 | | private: |
169 | | QShaderPrivate *d; |
170 | | friend struct QShaderPrivate; |
171 | | friend Q_GUI_EXPORT bool operator==(const QShader &, const QShader &) noexcept; |
172 | | friend Q_GUI_EXPORT size_t qHash(const QShader &, size_t) noexcept; |
173 | | #ifndef QT_NO_DEBUG_STREAM |
174 | | friend Q_GUI_EXPORT QDebug operator<<(QDebug, const QShader &); |
175 | | #endif |
176 | | }; |
177 | | |
178 | | class Q_GUI_EXPORT QShaderKey |
179 | | { |
180 | | public: |
181 | 0 | QShaderKey() = default; |
182 | | QShaderKey(QShader::Source s, |
183 | | const QShaderVersion &sver, |
184 | | QShader::Variant svar = QShader::StandardShader); |
185 | | |
186 | 0 | QShader::Source source() const { return m_source; } |
187 | 0 | void setSource(QShader::Source s) { m_source = s; } |
188 | | |
189 | 0 | QShaderVersion sourceVersion() const { return m_sourceVersion; } |
190 | 0 | void setSourceVersion(const QShaderVersion &sver) { m_sourceVersion = sver; } |
191 | | |
192 | 0 | QShader::Variant sourceVariant() const { return m_sourceVariant; } |
193 | 0 | void setSourceVariant(QShader::Variant svar) { m_sourceVariant = svar; } |
194 | | |
195 | | private: |
196 | | QShader::Source m_source = QShader::SpirvShader; |
197 | | QShaderVersion m_sourceVersion; |
198 | | QShader::Variant m_sourceVariant = QShader::StandardShader; |
199 | | }; |
200 | | |
201 | | Q_DECLARE_TYPEINFO(QShaderKey, Q_RELOCATABLE_TYPE); |
202 | | |
203 | | Q_GUI_EXPORT bool operator==(const QShader &lhs, const QShader &rhs) noexcept; |
204 | | Q_GUI_EXPORT size_t qHash(const QShader &s, size_t seed = 0) noexcept; |
205 | | |
206 | | inline bool operator!=(const QShader &lhs, const QShader &rhs) noexcept |
207 | 0 | { |
208 | 0 | return !(lhs == rhs); |
209 | 0 | } |
210 | | |
211 | | Q_GUI_EXPORT bool operator==(const QShaderVersion &lhs, const QShaderVersion &rhs) noexcept; |
212 | | Q_GUI_EXPORT bool operator<(const QShaderVersion &lhs, const QShaderVersion &rhs) noexcept; |
213 | | Q_GUI_EXPORT bool operator==(const QShaderKey &lhs, const QShaderKey &rhs) noexcept; |
214 | | Q_GUI_EXPORT bool operator<(const QShaderKey &lhs, const QShaderKey &rhs) noexcept; |
215 | | Q_GUI_EXPORT bool operator==(const QShaderCode &lhs, const QShaderCode &rhs) noexcept; |
216 | | |
217 | | inline bool operator!=(const QShaderVersion &lhs, const QShaderVersion &rhs) noexcept |
218 | 0 | { |
219 | 0 | return !(lhs == rhs); |
220 | 0 | } |
221 | | |
222 | | inline bool operator!=(const QShaderKey &lhs, const QShaderKey &rhs) noexcept |
223 | 0 | { |
224 | 0 | return !(lhs == rhs); |
225 | 0 | } |
226 | | |
227 | | inline bool operator!=(const QShaderCode &lhs, const QShaderCode &rhs) noexcept |
228 | 0 | { |
229 | 0 | return !(lhs == rhs); |
230 | 0 | } |
231 | | |
232 | | Q_GUI_EXPORT size_t qHash(const QShaderKey &k, size_t seed = 0) noexcept; |
233 | | |
234 | | #ifndef QT_NO_DEBUG_STREAM |
235 | | Q_GUI_EXPORT QDebug operator<<(QDebug, const QShader &); |
236 | | Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QShaderKey &k); |
237 | | Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QShaderVersion &v); |
238 | | #endif |
239 | | |
240 | | QT_END_NAMESPACE |
241 | | |
242 | | #endif |