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 
54 QT_BEGIN_NAMESPACE
55 
56 struct QShaderPrivate;
57 class QShaderKey;
58 
59 class Q_GUI_EXPORT QShaderVersion
60 {
61 public:
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 
version()70     int version() const { return m_version; }
setVersion(int v)71     void setVersion(int v) { m_version = v; }
72 
flags()73     Flags flags() const { return m_flags; }
setFlags(Flags f)74     void setFlags(Flags f) { m_flags = f; }
75 
76 private:
77     int m_version = 100;
78     Flags m_flags;
79 };
80 
81 Q_DECLARE_OPERATORS_FOR_FLAGS(QShaderVersion::Flags)
82 Q_DECLARE_TYPEINFO(QShaderVersion, Q_MOVABLE_TYPE);
83 
84 class Q_GUI_EXPORT QShaderCode
85 {
86 public:
87     QShaderCode() = default;
88     QShaderCode(const QByteArray &code, const QByteArray &entry = QByteArray());
89 
shader()90     QByteArray shader() const { return m_shader; }
setShader(const QByteArray & code)91     void setShader(const QByteArray &code) { m_shader = code; }
92 
entryPoint()93     QByteArray entryPoint() const { return m_entryPoint; }
setEntryPoint(const QByteArray & entry)94     void setEntryPoint(const QByteArray &entry) { m_entryPoint = entry; }
95 
96 private:
97     QByteArray m_shader;
98     QByteArray m_entryPoint;
99 };
100 
101 Q_DECLARE_TYPEINFO(QShaderCode, Q_MOVABLE_TYPE);
102 
103 class Q_GUI_EXPORT QShader
104 {
105 public:
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 
157 private:
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 
167 class Q_GUI_EXPORT QShaderKey
168 {
169 public:
170     QShaderKey() = default;
171     QShaderKey(QShader::Source s,
172                const QShaderVersion &sver,
173                QShader::Variant svar = QShader::StandardShader);
174 
source()175     QShader::Source source() const { return m_source; }
setSource(QShader::Source s)176     void setSource(QShader::Source s) { m_source = s; }
177 
sourceVersion()178     QShaderVersion sourceVersion() const { return m_sourceVersion; }
setSourceVersion(const QShaderVersion & sver)179     void setSourceVersion(const QShaderVersion &sver) { m_sourceVersion = sver; }
180 
sourceVariant()181     QShader::Variant sourceVariant() const { return m_sourceVariant; }
setSourceVariant(QShader::Variant svar)182     void setSourceVariant(QShader::Variant svar) { m_sourceVariant = svar; }
183 
184 private:
185     QShader::Source m_source = QShader::SpirvShader;
186     QShaderVersion m_sourceVersion;
187     QShader::Variant m_sourceVariant = QShader::StandardShader;
188 };
189 
190 Q_DECLARE_TYPEINFO(QShaderKey, Q_MOVABLE_TYPE);
191 
192 Q_GUI_EXPORT bool operator==(const QShader &lhs, const QShader &rhs) Q_DECL_NOTHROW;
193 Q_GUI_EXPORT uint qHash(const QShader &s, uint seed = 0) Q_DECL_NOTHROW;
194 
195 inline bool operator!=(const QShader &lhs, const QShader &rhs) Q_DECL_NOTHROW
196 {
197     return !(lhs == rhs);
198 }
199 
200 Q_GUI_EXPORT bool operator==(const QShaderVersion &lhs, const QShaderVersion &rhs) Q_DECL_NOTHROW;
201 Q_GUI_EXPORT bool operator==(const QShaderKey &lhs, const QShaderKey &rhs) Q_DECL_NOTHROW;
202 Q_GUI_EXPORT bool operator==(const QShaderCode &lhs, const QShaderCode &rhs) Q_DECL_NOTHROW;
203 
204 inline bool operator!=(const QShaderVersion &lhs, const QShaderVersion &rhs) Q_DECL_NOTHROW
205 {
206     return !(lhs == rhs);
207 }
208 
209 inline bool operator!=(const QShaderKey &lhs, const QShaderKey &rhs) Q_DECL_NOTHROW
210 {
211     return !(lhs == rhs);
212 }
213 
214 inline bool operator!=(const QShaderCode &lhs, const QShaderCode &rhs) Q_DECL_NOTHROW
215 {
216     return !(lhs == rhs);
217 }
218 
219 Q_GUI_EXPORT uint qHash(const QShaderKey &k, uint seed = 0) Q_DECL_NOTHROW;
220 
221 #ifndef QT_NO_DEBUG_STREAM
222 Q_GUI_EXPORT QDebug operator<<(QDebug, const QShader &);
223 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QShaderKey &k);
224 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QShaderVersion &v);
225 #endif
226 
227 QT_END_NAMESPACE
228 
229 #endif
230