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 QSHADER_P_H
38#define QSHADER_P_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 <private/qshaderdescription_p.h>
53
54QT_BEGIN_NAMESPACE
55
56struct QShaderPrivate;
57class QShaderKey;
58
59class Q_GUI_EXPORT QShaderVersion
60{
61public:
62 enum Flag {
63 GlslEs = 0x01
64 };
65 Q_DECLARE_FLAGS(Flags, Flag)
66
67 QShaderVersion() = default;
68 QShaderVersion(int v, Flags f = Flags());
69
70 int version() const { return m_version; }
71 void setVersion(int v) { m_version = v; }
72
73 Flags flags() const { return m_flags; }
74 void setFlags(Flags f) { m_flags = f; }
75
76private:
77 int m_version = 100;
78 Flags m_flags;
79};
80
81Q_DECLARE_OPERATORS_FOR_FLAGS(QShaderVersion::Flags)
82Q_DECLARE_TYPEINFO(QShaderVersion, Q_MOVABLE_TYPE);
83
84class Q_GUI_EXPORT QShaderCode
85{
86public:
87 QShaderCode() = default;
88 QShaderCode(const QByteArray &code, const QByteArray &entry = QByteArray());
89
90 QByteArray shader() const { return m_shader; }
91 void setShader(const QByteArray &code) { m_shader = code; }
92
93 QByteArray entryPoint() const { return m_entryPoint; }
94 void setEntryPoint(const QByteArray &entry) { m_entryPoint = entry; }
95
96private:
97 QByteArray m_shader;
98 QByteArray m_entryPoint;
99};
100
101Q_DECLARE_TYPEINFO(QShaderCode, Q_MOVABLE_TYPE);
102
103class Q_GUI_EXPORT QShader
104{
105public:
106 enum Stage {
107 VertexStage = 0,
108 TessellationControlStage,
109 TessellationEvaluationStage,
110 GeometryStage,
111 FragmentStage,
112 ComputeStage
113 };
114
115 enum Source {
116 SpirvShader = 0,
117 GlslShader,
118 HlslShader,
119 DxbcShader, // fxc
120 MslShader,
121 DxilShader, // dxc
122 MetalLibShader // xcrun metal + xcrun metallib
123 };
124
125 enum Variant {
126 StandardShader = 0,
127 BatchableVertexShader
128 };
129
130 QShader();
131 QShader(const QShader &other);
132 QShader &operator=(const QShader &other);
133 ~QShader();
134 void detach();
135
136 bool isValid() const;
137
138 Stage stage() const;
139 void setStage(Stage stage);
140
141 QShaderDescription description() const;
142 void setDescription(const QShaderDescription &desc);
143
144 QVector<QShaderKey> availableShaders() const;
145 QShaderCode shader(const QShaderKey &key) const;
146 void setShader(const QShaderKey &key, const QShaderCode &shader);
147 void removeShader(const QShaderKey &key);
148
149 QByteArray serialized() const;
150 static QShader fromSerialized(const QByteArray &data);
151
152 using NativeResourceBindingMap = QHash<int, QPair<int, int> >; // binding -> native_binding[, native_binding]
153 const NativeResourceBindingMap *nativeResourceBindingMap(const QShaderKey &key) const;
154 void setResourceBindingMap(const QShaderKey &key, const NativeResourceBindingMap &map);
155 void removeResourceBindingMap(const QShaderKey &key);
156
157private:
158 QShaderPrivate *d;
159 friend struct QShaderPrivate;
160 friend Q_GUI_EXPORT bool operator==(const QShader &, const QShader &) Q_DECL_NOTHROW;
161 friend Q_GUI_EXPORT uint qHash(const QShader &, uint) Q_DECL_NOTHROW;
162#ifndef QT_NO_DEBUG_STREAM
163 friend Q_GUI_EXPORT QDebug operator<<(QDebug, const QShader &);
164#endif
165};
166
167class Q_GUI_EXPORT QShaderKey
168{
169public:
170 QShaderKey() = default;
171 QShaderKey(QShader::Source s,
172 const QShaderVersion &sver,
173 QShader::Variant svar = QShader::StandardShader);
174
175 QShader::Source source() const { return m_source; }
176 void setSource(QShader::Source s) { m_source = s; }
177
178 QShaderVersion sourceVersion() const { return m_sourceVersion; }
179 void setSourceVersion(const QShaderVersion &sver) { m_sourceVersion = sver; }
180
181 QShader::Variant sourceVariant() const { return m_sourceVariant; }
182 void setSourceVariant(QShader::Variant svar) { m_sourceVariant = svar; }
183
184private:
185 QShader::Source m_source = QShader::SpirvShader;
186 QShaderVersion m_sourceVersion;
187 QShader::Variant m_sourceVariant = QShader::StandardShader;
188};
189
190Q_DECLARE_TYPEINFO(QShaderKey, Q_MOVABLE_TYPE);
191
192Q_GUI_EXPORT bool operator==(const QShader &lhs, const QShader &rhs) Q_DECL_NOTHROW;
193Q_GUI_EXPORT uint qHash(const QShader &s, uint seed = 0) Q_DECL_NOTHROW;
194
195inline bool operator!=(const QShader &lhs, const QShader &rhs) Q_DECL_NOTHROW
196{
197 return !(lhs == rhs);
198}
199
200Q_GUI_EXPORT bool operator==(const QShaderVersion &lhs, const QShaderVersion &rhs) Q_DECL_NOTHROW;
201Q_GUI_EXPORT bool operator==(const QShaderKey &lhs, const QShaderKey &rhs) Q_DECL_NOTHROW;
202Q_GUI_EXPORT bool operator==(const QShaderCode &lhs, const QShaderCode &rhs) Q_DECL_NOTHROW;
203
204inline bool operator!=(const QShaderVersion &lhs, const QShaderVersion &rhs) Q_DECL_NOTHROW
205{
206 return !(lhs == rhs);
207}
208
209inline bool operator!=(const QShaderKey &lhs, const QShaderKey &rhs) Q_DECL_NOTHROW
210{
211 return !(lhs == rhs);
212}
213
214inline bool operator!=(const QShaderCode &lhs, const QShaderCode &rhs) Q_DECL_NOTHROW
215{
216 return !(lhs == rhs);
217}
218
219Q_GUI_EXPORT uint qHash(const QShaderKey &k, uint seed = 0) Q_DECL_NOTHROW;
220
221#ifndef QT_NO_DEBUG_STREAM
222Q_GUI_EXPORT QDebug operator<<(QDebug, const QShader &);
223Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QShaderKey &k);
224Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QShaderVersion &v);
225#endif
226
227QT_END_NAMESPACE
228
229#endif
230

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