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