1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQuick module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
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 https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QSGSIMPLEMATERIAL_H |
41 | #define QSGSIMPLEMATERIAL_H |
42 | |
43 | #include <QtQuick/qsgmaterial.h> |
44 | |
45 | QT_BEGIN_NAMESPACE |
46 | |
47 | #if QT_DEPRECATED_SINCE(5, 15) |
48 | |
49 | template <typename State> |
50 | class QSGSimpleMaterialShader : public QSGMaterialShader |
51 | { |
52 | public: |
53 | void initialize() override { |
54 | QSGMaterialShader::initialize(); |
55 | #if QT_CONFIG(opengl) |
56 | m_id_matrix = program()->uniformLocation(uniformMatrixName()); |
57 | if (m_id_matrix < 0) { |
58 | qFatal(msg: "QSGSimpleMaterialShader does not implement 'uniform highp mat4 %s;' in its vertex shader" , |
59 | uniformMatrixName()); |
60 | } |
61 | |
62 | const char *opacity = uniformOpacityName(); |
63 | if (opacity) { |
64 | m_id_opacity = program()->uniformLocation(uniformOpacityName()); |
65 | if (m_id_opacity < 0) { |
66 | qFatal(msg: "QSGSimpleMaterialShader does not implement 'uniform lowp float %s' in its fragment shader" , |
67 | uniformOpacityName()); |
68 | } |
69 | } else { |
70 | m_id_opacity = -1; |
71 | } |
72 | #endif |
73 | resolveUniforms(); |
74 | } |
75 | |
76 | // ### Qt 6: make both virtual and fix docs |
77 | const char *uniformMatrixName() const { return "qt_Matrix" ; } |
78 | const char *uniformOpacityName() const { return "qt_Opacity" ; } |
79 | |
80 | void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
81 | |
82 | QT_DEPRECATED_X("QSGSimpleMaterialShader is going to be removed in Qt 6.0. Use QSGMaterialShader instead." ) |
83 | virtual void updateState(const State *newState, const State *oldState) = 0; |
84 | |
85 | virtual void resolveUniforms() {} |
86 | |
87 | virtual QList<QByteArray> attributes() const = 0; |
88 | |
89 | char const *const *attributeNames() const override |
90 | { |
91 | if (m_attribute_pointers.size()) |
92 | return m_attribute_pointers.constData(); |
93 | |
94 | QList<QByteArray> names = attributes(); |
95 | |
96 | // Calculate the total number of bytes needed, so we don't get rellocs and |
97 | // bad pointers while copying over the individual names. |
98 | // Add an extra byte pr entry for the '\0' char. |
99 | int total = 0; |
100 | for (int i=0; i<names.size(); ++i) |
101 | total += names.at(i).size() + 1; |
102 | m_attribute_name_data.reserve(asize: total); |
103 | |
104 | // Copy over the names |
105 | for (int i=0; i<names.size(); ++i) { |
106 | m_attribute_pointers << m_attribute_name_data.constData() + m_attribute_name_data.size(); |
107 | m_attribute_name_data.append(a: names.at(i)); |
108 | m_attribute_name_data.append(c: '\0'); |
109 | } |
110 | |
111 | // Append the "null" terminator |
112 | m_attribute_pointers << 0; |
113 | |
114 | return m_attribute_pointers.constData(); |
115 | } |
116 | |
117 | private: |
118 | int m_id_matrix; |
119 | int m_id_opacity; |
120 | |
121 | mutable QByteArray m_attribute_name_data; |
122 | mutable QVector<const char *> m_attribute_pointers; |
123 | }; |
124 | |
125 | #define QSG_DECLARE_SIMPLE_SHADER(Shader, State) \ |
126 | static QSGMaterialShader *createShader() \ |
127 | { \ |
128 | return new Shader; \ |
129 | } \ |
130 | public: \ |
131 | static QSGSimpleMaterial<State> *createMaterial() \ |
132 | { \ |
133 | return new QSGSimpleMaterial<State>(createShader); \ |
134 | } |
135 | |
136 | |
137 | typedef QSGMaterialShader *(*PtrShaderCreateFunc)(); |
138 | |
139 | |
140 | template <typename State> |
141 | class QSGSimpleMaterial : public QSGMaterial |
142 | { |
143 | public: |
144 | #ifndef Q_CLANG_QDOC |
145 | QT_DEPRECATED_X("QSGSimpleMaterial is going to be removed in Qt 6.0. Use QSGMaterial instead." ) |
146 | QSGSimpleMaterial(const State &aState, PtrShaderCreateFunc func) |
147 | : m_state(aState) |
148 | , m_func(func) |
149 | { |
150 | } |
151 | |
152 | QT_DEPRECATED_X("QSGSimpleMaterial is going to be removed in Qt 6.0. Use QSGMaterial instead." ) |
153 | QSGSimpleMaterial(PtrShaderCreateFunc func) |
154 | : m_func(func) |
155 | { |
156 | } |
157 | |
158 | QSGMaterialShader *createShader() const override { return m_func(); } |
159 | QSGMaterialType *type() const override { return &m_type; } |
160 | |
161 | State *state() { return &m_state; } |
162 | const State *state() const { return &m_state; } |
163 | #endif |
164 | |
165 | private: |
166 | static QSGMaterialType m_type; |
167 | State m_state; |
168 | PtrShaderCreateFunc m_func; |
169 | }; |
170 | |
171 | #define QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State) \ |
172 | static QSGMaterialShader *createShader() \ |
173 | { \ |
174 | return new Shader; \ |
175 | } \ |
176 | public: \ |
177 | static QSGSimpleMaterialComparableMaterial<State> *createMaterial() \ |
178 | { \ |
179 | return new QSGSimpleMaterialComparableMaterial<State>(createShader); \ |
180 | } |
181 | |
182 | template <typename State> |
183 | class QSGSimpleMaterialComparableMaterial : public QSGSimpleMaterial<State> |
184 | { |
185 | |
186 | public: |
187 | QSGSimpleMaterialComparableMaterial(const State &state, PtrShaderCreateFunc func) |
188 | : QSGSimpleMaterial<State>(state, func) {} |
189 | |
190 | QSGSimpleMaterialComparableMaterial(PtrShaderCreateFunc func) |
191 | : QSGSimpleMaterial<State>(func) {} |
192 | |
193 | int compare(const QSGMaterial *other) const override { |
194 | return QSGSimpleMaterialComparableMaterial<State>::state()->compare(static_cast<const QSGSimpleMaterialComparableMaterial<State> *>(other)->state()); |
195 | } |
196 | }; |
197 | |
198 | |
199 | template <typename State> |
200 | QSGMaterialType QSGSimpleMaterial<State>::m_type; |
201 | |
202 | |
203 | template <typename State> |
204 | Q_INLINE_TEMPLATE void QSGSimpleMaterialShader<State>::updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
205 | { |
206 | #if QT_CONFIG(opengl) |
207 | if (state.isMatrixDirty()) |
208 | program()->setUniformValue(m_id_matrix, state.combinedMatrix()); |
209 | if (state.isOpacityDirty() && m_id_opacity >= 0) |
210 | program()->setUniformValue(m_id_opacity, state.opacity()); |
211 | #else |
212 | Q_UNUSED(state) |
213 | #endif |
214 | State *ns = static_cast<QSGSimpleMaterial<State> *>(newMaterial)->state(); |
215 | State *old = nullptr; |
216 | if (oldMaterial) |
217 | old = static_cast<QSGSimpleMaterial<State> *>(oldMaterial)->state(); |
218 | updateState(ns, old); |
219 | } |
220 | |
221 | #endif |
222 | |
223 | QT_END_NAMESPACE |
224 | |
225 | |
226 | #endif |
227 | |