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#include "qsgflatcolormaterial.h"
41#include <private/qsgmaterialshader_p.h>
42#if QT_CONFIG(opengl)
43# include <qopenglshaderprogram.h>
44#endif
45
46QT_BEGIN_NAMESPACE
47
48class FlatColorMaterialShader : public QSGMaterialShader
49{
50public:
51 FlatColorMaterialShader();
52
53 void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect) override;
54 char const *const *attributeNames() const override;
55
56 static QSGMaterialType type;
57
58private:
59 void initialize() override;
60#if QT_CONFIG(opengl)
61 int m_matrix_id;
62 int m_color_id;
63#endif
64};
65
66QSGMaterialType FlatColorMaterialShader::type;
67
68FlatColorMaterialShader::FlatColorMaterialShader()
69 : QSGMaterialShader(*new QSGMaterialShaderPrivate)
70{
71#if QT_CONFIG(opengl)
72 setShaderSourceFile(type: QOpenGLShader::Vertex, QStringLiteral(":/qt-project.org/scenegraph/shaders/flatcolor.vert"));
73 setShaderSourceFile(type: QOpenGLShader::Fragment, QStringLiteral(":/qt-project.org/scenegraph/shaders/flatcolor.frag"));
74#endif
75}
76
77void FlatColorMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
78{
79#if QT_CONFIG(opengl)
80 Q_ASSERT(oldEffect == nullptr || newEffect->type() == oldEffect->type());
81 QSGFlatColorMaterial *oldMaterial = static_cast<QSGFlatColorMaterial *>(oldEffect);
82 QSGFlatColorMaterial *newMaterial = static_cast<QSGFlatColorMaterial *>(newEffect);
83
84 const QColor &c = newMaterial->color();
85
86 if (oldMaterial == nullptr || c != oldMaterial->color() || state.isOpacityDirty()) {
87 float opacity = state.opacity() * c.alphaF();
88 QVector4D v(c.redF() * opacity,
89 c.greenF() * opacity,
90 c.blueF() * opacity,
91 opacity);
92 program()->setUniformValue(location: m_color_id, value: v);
93 }
94
95 if (state.isMatrixDirty())
96 program()->setUniformValue(location: m_matrix_id, value: state.combinedMatrix());
97#else
98 Q_UNUSED(state)
99 Q_UNUSED(newEffect)
100 Q_UNUSED(oldEffect)
101#endif
102}
103
104char const *const *FlatColorMaterialShader::attributeNames() const
105{
106 static char const *const attr[] = { "vCoord", nullptr };
107 return attr;
108}
109
110void FlatColorMaterialShader::initialize()
111{
112#if QT_CONFIG(opengl)
113 m_matrix_id = program()->uniformLocation(name: "matrix");
114 m_color_id = program()->uniformLocation(name: "color");
115#endif
116}
117
118
119class FlatColorMaterialRhiShader : public QSGMaterialRhiShader
120{
121public:
122 FlatColorMaterialRhiShader();
123
124 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
125};
126
127FlatColorMaterialRhiShader::FlatColorMaterialRhiShader()
128{
129 setShaderFileName(stage: VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/flatcolor.vert.qsb"));
130 setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/flatcolor.frag.qsb"));
131}
132
133bool FlatColorMaterialRhiShader::updateUniformData(RenderState &state,
134 QSGMaterial *newMaterial,
135 QSGMaterial *oldMaterial)
136{
137 Q_ASSERT(!oldMaterial || newMaterial->type() == oldMaterial->type());
138 QSGFlatColorMaterial *oldMat = static_cast<QSGFlatColorMaterial *>(oldMaterial);
139 QSGFlatColorMaterial *mat = static_cast<QSGFlatColorMaterial *>(newMaterial);
140 bool changed = false;
141 QByteArray *buf = state.uniformData();
142
143 if (state.isMatrixDirty()) {
144 const QMatrix4x4 m = state.combinedMatrix();
145 memcpy(dest: buf->data(), src: m.constData(), n: 64);
146 changed = true;
147 }
148
149 const QColor &c = mat->color();
150 if (!oldMat || c != oldMat->color() || state.isOpacityDirty()) {
151 const float opacity = state.opacity() * c.alphaF();
152 QVector4D v(c.redF() * opacity,
153 c.greenF() * opacity,
154 c.blueF() * opacity,
155 opacity);
156 Q_ASSERT(sizeof(v) == 16);
157 memcpy(dest: buf->data() + 64, src: &v, n: 16);
158 changed = true;
159 }
160
161 return changed;
162}
163
164
165/*!
166 \class QSGFlatColorMaterial
167
168 \brief The QSGFlatColorMaterial class provides a convenient way of rendering
169 solid colored geometry in the scene graph.
170
171 \inmodule QtQuick
172 \ingroup qtquick-scenegraph-materials
173
174 \warning This utility class is only functional when running with the default
175 backend of the Qt Quick scenegraph.
176
177 The flat color material will fill every pixel in a geometry using
178 a solid color. The color can contain transparency.
179
180 The geometry to be rendered with a flat color material requires
181 vertices in attribute location 0 in the QSGGeometry object to render
182 correctly. The QSGGeometry::defaultAttributes_Point2D() returns an attribute
183 set compatible with this material.
184
185 The flat color material respects both current opacity and current matrix
186 when updating its rendering state.
187 */
188
189
190/*!
191 Constructs a new flat color material.
192
193 The default color is white.
194 */
195
196QSGFlatColorMaterial::QSGFlatColorMaterial() : m_color(QColor(255, 255, 255))
197{
198 setFlag(flags: SupportsRhiShader, on: true);
199}
200
201/*!
202 \fn const QColor &QSGFlatColorMaterial::color() const
203
204 Returns this flat color material's color.
205
206 The default color is white.
207 */
208
209
210
211/*!
212 Sets this flat color material's color to \a color.
213 */
214
215void QSGFlatColorMaterial::setColor(const QColor &color)
216{
217 m_color = color;
218 setFlag(flags: Blending, on: m_color.alpha() != 0xff);
219}
220
221
222
223/*!
224 \internal
225 */
226
227QSGMaterialType *QSGFlatColorMaterial::type() const
228{
229 return &FlatColorMaterialShader::type;
230}
231
232
233
234/*!
235 \internal
236 */
237
238QSGMaterialShader *QSGFlatColorMaterial::createShader() const
239{
240 if (flags().testFlag(flag: RhiShaderWanted))
241 return new FlatColorMaterialRhiShader;
242 else
243 return new FlatColorMaterialShader;
244}
245
246
247/*!
248 \internal
249 */
250
251int QSGFlatColorMaterial::compare(const QSGMaterial *other) const
252{
253 const QSGFlatColorMaterial *flat = static_cast<const QSGFlatColorMaterial *>(other);
254 return m_color.rgba() - flat->color().rgba();
255
256}
257
258QT_END_NAMESPACE
259

source code of qtdeclarative/src/quick/scenegraph/util/qsgflatcolormaterial.cpp