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
152private:
153 QShaderPrivate *d;
154 friend struct QShaderPrivate;
155 friend Q_GUI_EXPORT bool operator==(const QShader &, const QShader &) Q_DECL_NOTHROW;
156 friend Q_GUI_EXPORT uint qHash(const QShader &, uint) Q_DECL_NOTHROW;
157#ifndef QT_NO_DEBUG_STREAM
158 friend Q_GUI_EXPORT QDebug operator<<(QDebug, const QShader &);
159#endif
160};
161
162class Q_GUI_EXPORT QShaderKey
163{
164public:
165 QShaderKey() = default;
166 QShaderKey(QShader::Source s,
167 const QShaderVersion &sver,
168 QShader::Variant svar = QShader::StandardShader);
169
170 QShader::Source source() const { return m_source; }
171 void setSource(QShader::Source s) { m_source = s; }
172
173 QShaderVersion sourceVersion() const { return m_sourceVersion; }
174 void setSourceVersion(const QShaderVersion &sver) { m_sourceVersion = sver; }
175
176 QShader::Variant sourceVariant() const { return m_sourceVariant; }
177 void setSourceVariant(QShader::Variant svar) { m_sourceVariant = svar; }
178
179private:
180 QShader::Source m_source = QShader::SpirvShader;
181 QShaderVersion m_sourceVersion;
182 QShader::Variant m_sourceVariant = QShader::StandardShader;
183};
184
185Q_DECLARE_TYPEINFO(QShaderKey, Q_MOVABLE_TYPE);
186
187Q_GUI_EXPORT bool operator==(const QShader &lhs, const QShader &rhs) Q_DECL_NOTHROW;
188Q_GUI_EXPORT uint qHash(const QShader &s, uint seed = 0) Q_DECL_NOTHROW;
189
190inline bool operator!=(const QShader &lhs, const QShader &rhs) Q_DECL_NOTHROW
191{
192 return !(lhs == rhs);
193}
194
195Q_GUI_EXPORT bool operator==(const QShaderVersion &lhs, const QShaderVersion &rhs) Q_DECL_NOTHROW;
196Q_GUI_EXPORT bool operator==(const QShaderKey &lhs, const QShaderKey &rhs) Q_DECL_NOTHROW;
197Q_GUI_EXPORT bool operator==(const QShaderCode &lhs, const QShaderCode &rhs) Q_DECL_NOTHROW;
198
199inline bool operator!=(const QShaderVersion &lhs, const QShaderVersion &rhs) Q_DECL_NOTHROW
200{
201 return !(lhs == rhs);
202}
203
204inline bool operator!=(const QShaderKey &lhs, const QShaderKey &rhs) Q_DECL_NOTHROW
205{
206 return !(lhs == rhs);
207}
208
209inline bool operator!=(const QShaderCode &lhs, const QShaderCode &rhs) Q_DECL_NOTHROW
210{
211 return !(lhs == rhs);
212}
213
214Q_GUI_EXPORT uint qHash(const QShaderKey &k, uint seed = 0) Q_DECL_NOTHROW;
215
216#ifndef QT_NO_DEBUG_STREAM
217Q_GUI_EXPORT QDebug operator<<(QDebug, const QShader &);
218Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QShaderKey &k);
219Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QShaderVersion &v);
220#endif
221
222QT_END_NAMESPACE
223
224#endif
225