1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsgflatcolormaterial.h"
5#include <private/qsgmaterialshader_p.h>
6
7QT_BEGIN_NAMESPACE
8
9class FlatColorMaterialRhiShader : public QSGMaterialShader
10{
11public:
12 FlatColorMaterialRhiShader();
13
14 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
15
16 static QSGMaterialType type;
17};
18
19QSGMaterialType FlatColorMaterialRhiShader::type;
20
21FlatColorMaterialRhiShader::FlatColorMaterialRhiShader()
22{
23 setShaderFileName(stage: VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/flatcolor.vert.qsb"));
24 setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/flatcolor.frag.qsb"));
25}
26
27bool FlatColorMaterialRhiShader::updateUniformData(RenderState &state,
28 QSGMaterial *newMaterial,
29 QSGMaterial *oldMaterial)
30{
31 Q_ASSERT(!oldMaterial || newMaterial->type() == oldMaterial->type());
32 QSGFlatColorMaterial *oldMat = static_cast<QSGFlatColorMaterial *>(oldMaterial);
33 QSGFlatColorMaterial *mat = static_cast<QSGFlatColorMaterial *>(newMaterial);
34 bool changed = false;
35 QByteArray *buf = state.uniformData();
36
37 if (state.isMatrixDirty()) {
38 const QMatrix4x4 m = state.combinedMatrix();
39 memcpy(dest: buf->data(), src: m.constData(), n: 64);
40 changed = true;
41 }
42
43 const QColor &c = mat->color();
44 if (!oldMat || c != oldMat->color() || state.isOpacityDirty()) {
45 float r, g, b, a;
46 c.getRgbF(r: &r, g: &g, b: &b, a: &a);
47 const float opacity = state.opacity() * a;
48 QVector4D v(r * opacity, g * opacity, b * opacity, opacity);
49 Q_ASSERT(sizeof(v) == 16);
50 memcpy(dest: buf->data() + 64, src: &v, n: 16);
51 changed = true;
52 }
53
54 return changed;
55}
56
57
58/*!
59 \class QSGFlatColorMaterial
60
61 \brief The QSGFlatColorMaterial class provides a convenient way of rendering
62 solid colored geometry in the scene graph.
63
64 \inmodule QtQuick
65 \ingroup qtquick-scenegraph-materials
66
67 \warning This utility class is only functional when running with the default
68 backend of the Qt Quick scenegraph.
69
70 The flat color material will fill every pixel in a geometry using
71 a solid color. The color can contain transparency.
72
73 The geometry to be rendered with a flat color material requires
74 vertices in attribute location 0 in the QSGGeometry object to render
75 correctly. The QSGGeometry::defaultAttributes_Point2D() returns an attribute
76 set compatible with this material.
77
78 The flat color material respects both current opacity and current matrix
79 when updating its rendering state.
80 */
81
82
83/*!
84 Constructs a new flat color material.
85
86 The default color is white.
87 */
88
89QSGFlatColorMaterial::QSGFlatColorMaterial() : m_color(QColor(255, 255, 255))
90{
91}
92
93/*!
94 \fn const QColor &QSGFlatColorMaterial::color() const
95
96 Returns this flat color material's color.
97
98 The default color is white.
99 */
100
101
102
103/*!
104 Sets this flat color material's color to \a color.
105 */
106
107void QSGFlatColorMaterial::setColor(const QColor &color)
108{
109 m_color = color;
110 setFlag(flags: Blending, on: m_color.alpha() != 0xff);
111}
112
113
114
115/*!
116 \internal
117 */
118
119QSGMaterialType *QSGFlatColorMaterial::type() const
120{
121 return &FlatColorMaterialRhiShader::type;
122}
123
124
125
126/*!
127 \internal
128 */
129
130QSGMaterialShader *QSGFlatColorMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
131{
132 Q_UNUSED(renderMode);
133 return new FlatColorMaterialRhiShader;
134}
135
136
137/*!
138 \internal
139 */
140
141int QSGFlatColorMaterial::compare(const QSGMaterial *other) const
142{
143 const QSGFlatColorMaterial *flat = static_cast<const QSGFlatColorMaterial *>(other);
144 return m_color.rgba() - flat->color().rgba();
145
146}
147
148QT_END_NAMESPACE
149

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