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 "qsgvertexcolormaterial.h"
5
6QT_BEGIN_NAMESPACE
7
8class QSGVertexColorMaterialRhiShader : public QSGMaterialShader
9{
10public:
11 QSGVertexColorMaterialRhiShader();
12
13 bool updateUniformData(RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect) override;
14};
15
16QSGVertexColorMaterialRhiShader::QSGVertexColorMaterialRhiShader()
17{
18 setShaderFileName(stage: VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/vertexcolor.vert.qsb"));
19 setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/vertexcolor.frag.qsb"));
20}
21
22bool QSGVertexColorMaterialRhiShader::updateUniformData(RenderState &state,
23 QSGMaterial * /*newEffect*/,
24 QSGMaterial * /*oldEffect*/)
25{
26 bool changed = false;
27 QByteArray *buf = state.uniformData();
28
29 if (state.isMatrixDirty()) {
30 const QMatrix4x4 m = state.combinedMatrix();
31 memcpy(dest: buf->data(), src: m.constData(), n: 64);
32 changed = true;
33 }
34
35 if (state.isOpacityDirty()) {
36 const float opacity = state.opacity();
37 memcpy(dest: buf->data() + 64, src: &opacity, n: 4);
38 changed = true;
39 }
40
41 return changed;
42}
43
44
45/*!
46 \class QSGVertexColorMaterial
47 \brief The QSGVertexColorMaterial class provides a convenient way of rendering per-vertex
48 colored geometry in the scene graph.
49
50 \inmodule QtQuick
51 \ingroup qtquick-scenegraph-materials
52
53 \warning This utility class is only functional when running with the
54 default backend of the Qt Quick scenegraph.
55
56 The vertex color material will give each vertex in a geometry a color. Pixels between
57 vertices will be linearly interpolated. The colors can contain transparency.
58
59 The geometry to be rendered with vertex color must have the following layout. Attribute
60 position 0 must contain vertices. Attribute position 1 must contain colors, a tuple of
61 4 values with RGBA layout. Both floats in the range of 0 to 1 and unsigned bytes in
62 the range 0 to 255 are valid for the color values.
63
64 \note The rendering pipeline expects pixels with premultiplied alpha.
65
66 QSGGeometry::defaultAttributes_ColoredPoint2D() can be used to construct an attribute
67 set that is compatible with this material.
68
69 The vertex color material respects both current opacity and current matrix when
70 updating it's rendering state.
71 */
72
73
74/*!
75 Creates a new vertex color material.
76 */
77
78QSGVertexColorMaterial::QSGVertexColorMaterial()
79{
80 setFlag(flags: Blending, on: true);
81}
82
83
84/*!
85 int QSGVertexColorMaterial::compare() const
86
87 As the vertex color material has all its state in the vertex attributes,
88 all materials will be equal.
89
90 \internal
91 */
92
93int QSGVertexColorMaterial::compare(const QSGMaterial * /* other */) const
94{
95 return 0;
96}
97
98/*!
99 \internal
100 */
101
102QSGMaterialType *QSGVertexColorMaterial::type() const
103{
104 static QSGMaterialType type;
105 return &type;
106}
107
108
109
110/*!
111 \internal
112 */
113
114QSGMaterialShader *QSGVertexColorMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
115{
116 Q_UNUSED(renderMode);
117 return new QSGVertexColorMaterialRhiShader;
118}
119
120QT_END_NAMESPACE
121

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