1
2/****************************************************************************
3**
4** Copyright (C) 2016 The Qt Company Ltd.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtQuick module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "qsgdefaultinternalrectanglenode_p.h"
42
43#include <QtQuick/qsgvertexcolormaterial.h>
44#include <QtQuick/qsgtexturematerial.h>
45
46#include <QtQuick/private/qsgcontext_p.h>
47
48#include <QtCore/qmath.h>
49#include <QtCore/qvarlengtharray.h>
50
51QT_BEGIN_NAMESPACE
52
53class SmoothColorMaterialShader : public QSGMaterialShader
54{
55public:
56 SmoothColorMaterialShader();
57
58 void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect) override;
59 char const *const *attributeNames() const override;
60
61private:
62 void initialize() override;
63
64 int m_matrixLoc;
65 int m_opacityLoc;
66 int m_pixelSizeLoc;
67};
68
69SmoothColorMaterialShader::SmoothColorMaterialShader()
70{
71 setShaderSourceFile(type: QOpenGLShader::Vertex, QStringLiteral(":/qt-project.org/scenegraph/shaders/smoothcolor.vert"));
72 setShaderSourceFile(type: QOpenGLShader::Fragment, QStringLiteral(":/qt-project.org/scenegraph/shaders/smoothcolor.frag"));
73}
74
75void SmoothColorMaterialShader::updateState(const RenderState &state, QSGMaterial *, QSGMaterial *oldEffect)
76{
77 if (state.isOpacityDirty())
78 program()->setUniformValue(location: m_opacityLoc, value: state.opacity());
79
80 if (state.isMatrixDirty())
81 program()->setUniformValue(location: m_matrixLoc, value: state.combinedMatrix());
82
83 if (oldEffect == nullptr) {
84 // The viewport is constant, so set the pixel size uniform only once.
85 QRect r = state.viewportRect();
86 program()->setUniformValue(location: m_pixelSizeLoc, x: 2.0f / r.width(), y: 2.0f / r.height());
87 }
88}
89
90char const *const *SmoothColorMaterialShader::attributeNames() const
91{
92 static char const *const attributes[] = {
93 "vertex",
94 "vertexColor",
95 "vertexOffset",
96 nullptr
97 };
98 return attributes;
99}
100
101void SmoothColorMaterialShader::initialize()
102{
103 m_matrixLoc = program()->uniformLocation(name: "matrix");
104 m_opacityLoc = program()->uniformLocation(name: "opacity");
105 m_pixelSizeLoc = program()->uniformLocation(name: "pixelSize");
106}
107
108
109class SmoothColorMaterialRhiShader : public QSGMaterialRhiShader
110{
111public:
112 SmoothColorMaterialRhiShader();
113
114 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
115};
116
117SmoothColorMaterialRhiShader::SmoothColorMaterialRhiShader()
118{
119 setShaderFileName(stage: VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/smoothcolor.vert.qsb"));
120 setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/smoothcolor.frag.qsb"));
121}
122
123bool SmoothColorMaterialRhiShader::updateUniformData(RenderState &state, QSGMaterial *, QSGMaterial *oldMaterial)
124{
125 bool changed = false;
126 QByteArray *buf = state.uniformData();
127
128 if (state.isMatrixDirty()) {
129 const QMatrix4x4 m = state.combinedMatrix();
130 memcpy(dest: buf->data(), src: m.constData(), n: 64);
131 changed = true;
132 }
133
134 if (oldMaterial == nullptr) {
135 // The viewport is constant, so set the pixel size uniform only once.
136 const QRect r = state.viewportRect();
137 const QVector2D v(2.0f / r.width(), 2.0f / r.height());
138 Q_ASSERT(sizeof(v) == 8);
139 memcpy(dest: buf->data() + 64, src: &v, n: 8);
140 changed = true;
141 }
142
143 if (state.isOpacityDirty()) {
144 const float opacity = state.opacity();
145 memcpy(dest: buf->data() + 72, src: &opacity, n: 4);
146 changed = true;
147 }
148
149 return changed;
150}
151
152
153QSGSmoothColorMaterial::QSGSmoothColorMaterial()
154{
155 setFlag(flags: RequiresFullMatrixExceptTranslate, on: true);
156 setFlag(flags: Blending, on: true);
157 setFlag(flags: SupportsRhiShader, on: true);
158}
159
160int QSGSmoothColorMaterial::compare(const QSGMaterial *) const
161{
162 // all state in vertex attributes -> all smoothcolor materials are equal
163 return 0;
164}
165
166QSGMaterialType *QSGSmoothColorMaterial::type() const
167{
168 static QSGMaterialType type;
169 return &type;
170}
171
172QSGMaterialShader *QSGSmoothColorMaterial::createShader() const
173{
174 if (flags().testFlag(flag: RhiShaderWanted))
175 return new SmoothColorMaterialRhiShader;
176 else
177 return new SmoothColorMaterialShader;
178}
179
180QSGDefaultInternalRectangleNode::QSGDefaultInternalRectangleNode()
181{
182 setMaterial(&m_material);
183}
184
185void QSGDefaultInternalRectangleNode::updateMaterialAntialiasing()
186{
187 if (m_antialiasing)
188 setMaterial(&m_smoothMaterial);
189 else
190 setMaterial(&m_material);
191}
192
193void QSGDefaultInternalRectangleNode::updateMaterialBlending(QSGNode::DirtyState *state)
194{
195 // smoothed material is always blended, so no change in material state
196 if (material() == &m_material) {
197 bool wasBlending = (m_material.flags() & QSGMaterial::Blending);
198 bool isBlending = (m_gradient_stops.size() > 0 && !m_gradient_is_opaque)
199 || (m_color.alpha() < 255 && m_color.alpha() != 0)
200 || (m_pen_width > 0 && m_border_color.alpha() < 255);
201 if (wasBlending != isBlending) {
202 m_material.setFlag(flags: QSGMaterial::Blending, on: isBlending);
203 *state |= QSGNode::DirtyMaterial;
204 }
205 }
206}
207
208QT_END_NAMESPACE
209

source code of qtdeclarative/src/quick/scenegraph/qsgdefaultinternalrectanglenode.cpp