Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsgflatcolormaterial.cpp
Go to the documentation of this file.
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
5#include <private/qsgmaterialshader_p.h>
6
8
10{
11public:
13
14 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
15
17};
18
20
22{
23 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/flatcolor.vert.qsb"), viewCount);
24 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/flatcolor.frag.qsb"), viewCount);
25}
26
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 const int shaderMatrixCount = newMaterial->viewCount();
37 const int matrixCount = qMin(state.projectionMatrixCount(), shaderMatrixCount);
38
39 for (int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
40 if (state.isMatrixDirty()) {
41 const QMatrix4x4 m = state.combinedMatrix(viewIndex);
42 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
43 changed = true;
44 }
45 }
46
47 const QColor &c = mat->color();
48 if (!oldMat || c != oldMat->color() || state.isOpacityDirty()) {
49 float r, g, b, a;
50 c.getRgbF(&r, &g, &b, &a);
51 const float opacity = state.opacity() * a;
52 QVector4D v(r * opacity, g * opacity, b * opacity, opacity);
53 memcpy(buf->data() + 64 * shaderMatrixCount, &v, 16);
54 changed = true;
55 }
56
57 return changed;
58}
59
60
61/*!
62 \class QSGFlatColorMaterial
63
64 \brief The QSGFlatColorMaterial class provides a convenient way of rendering
65 solid colored geometry in the scene graph.
66
67 \inmodule QtQuick
68 \ingroup qtquick-scenegraph-materials
69
70 \warning This utility class is only functional when running with the default
71 backend of the Qt Quick scenegraph.
72
73 The flat color material will fill every pixel in a geometry using
74 a solid color. The color can contain transparency.
75
76 The geometry to be rendered with a flat color material requires
77 vertices in attribute location 0 in the QSGGeometry object to render
78 correctly. The QSGGeometry::defaultAttributes_Point2D() returns an attribute
79 set compatible with this material.
80
81 The flat color material respects both current opacity and current matrix
82 when updating its rendering state.
83 */
84
85
86/*!
87 Constructs a new flat color material.
88
89 The default color is white.
90 */
91
92QSGFlatColorMaterial::QSGFlatColorMaterial() : m_color(QColor(255, 255, 255))
93{
94}
95
96/*!
97 \fn const QColor &QSGFlatColorMaterial::color() const
98
99 Returns this flat color material's color.
100
101 The default color is white.
102 */
103
104
105
106/*!
107 Sets this flat color material's color to \a color.
108 */
109
110void QSGFlatColorMaterial::setColor(const QColor &color)
111{
112 m_color = color;
113 setFlag(Blending, m_color.alpha() != 0xff);
114}
115
116
117
118/*!
119 \internal
120 */
121
122QSGMaterialType *QSGFlatColorMaterial::type() const
123{
124 return &FlatColorMaterialRhiShader::type;
125}
126
127
128
129/*!
130 \internal
131 */
132
133QSGMaterialShader *QSGFlatColorMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
134{
135 Q_UNUSED(renderMode);
136 return new FlatColorMaterialRhiShader(viewCount());
137}
138
139
140/*!
141 \internal
142 */
143
144int QSGFlatColorMaterial::compare(const QSGMaterial *other) const
145{
146 const QSGFlatColorMaterial *flat = static_cast<const QSGFlatColorMaterial *>(other);
147 return m_color.rgba() - flat->color().rgba();
148
149}
150
151QT_END_NAMESPACE
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
Combined button and popup list for selecting options.