Kirigami2

shadowedrectanglematerial.cpp
1/*
2 * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "shadowedrectanglematerial.h"
8
9#include <QOpenGLContext>
10
11QSGMaterialType ShadowedRectangleMaterial::staticType;
12
13ShadowedRectangleMaterial::ShadowedRectangleMaterial()
14{
16}
17
18QSGMaterialShader *ShadowedRectangleMaterial::createShader(QSGRendererInterface::RenderMode) const
19{
20 return new ShadowedRectangleShader{shaderType};
21}
22
23QSGMaterialType *ShadowedRectangleMaterial::type() const
24{
25 return &staticType;
26}
27
28int ShadowedRectangleMaterial::compare(const QSGMaterial *other) const
29{
30 auto material = static_cast<const ShadowedRectangleMaterial *>(other);
31 /* clang-format off */
32 if (material->color == color
33 && material->shadowColor == shadowColor
34 && material->offset == offset
35 && material->aspect == aspect
36 && qFuzzyCompare(material->size, size)
37 && qFuzzyCompare(material->radius, radius)) { /* clang-format on */
38 return 0;
39 }
40
41 return QSGMaterial::compare(other);
42}
43
44ShadowedRectangleShader::ShadowedRectangleShader(ShadowedRectangleMaterial::ShaderType shaderType)
45{
46 setShader(shaderType, QStringLiteral("shadowedrectangle"));
47}
48
49bool ShadowedRectangleShader::updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
50{
51 bool changed = false;
52 QByteArray *buf = state.uniformData();
53 Q_ASSERT(buf->size() >= 160);
54
55 if (state.isMatrixDirty()) {
56 const QMatrix4x4 m = state.combinedMatrix();
57 memcpy(buf->data(), m.constData(), 64);
58 changed = true;
59 }
60
61 if (state.isOpacityDirty()) {
62 const float opacity = state.opacity();
63 memcpy(buf->data() + 72, &opacity, 4);
64 changed = true;
65 }
66
67 if (!oldMaterial || newMaterial->compare(oldMaterial) != 0) {
68 const auto material = static_cast<ShadowedRectangleMaterial *>(newMaterial);
69 memcpy(buf->data() + 64, &material->aspect, 8);
70 memcpy(buf->data() + 76, &material->size, 4);
71 memcpy(buf->data() + 80, &material->radius, 16);
72 float c[4];
73 material->color.getRgbF(&c[0], &c[1], &c[2], &c[3]);
74 memcpy(buf->data() + 96, c, 16);
75 material->shadowColor.getRgbF(&c[0], &c[1], &c[2], &c[3]);
76 memcpy(buf->data() + 112, c, 16);
77 memcpy(buf->data() + 128, &material->offset, 8);
78 changed = true;
79 }
80
81 return changed;
82}
83
84void ShadowedRectangleShader::setShader(ShadowedRectangleMaterial::ShaderType shaderType, const QString &shader)
85{
86 const auto shaderRoot = QStringLiteral(":/qt/qml/org/kde/kirigami/primitives/shaders/");
87
88 setShaderFileName(QSGMaterialShader::VertexStage, shaderRoot + QStringLiteral("shadowedrectangle.vert.qsb"));
89
90 auto shaderFile = shader;
91 if (shaderType == ShadowedRectangleMaterial::ShaderType::LowPower) {
92 shaderFile += QStringLiteral("_lowpower");
93 }
94 setShaderFileName(QSGMaterialShader::FragmentStage, shaderRoot + shaderFile + QStringLiteral(".frag.qsb"));
95}
A material rendering a rectangle with a shadow.
char * data()
qsizetype size() const const
const float * constData() const const
virtual int compare(const QSGMaterial *other) const const
void setFlag(Flags flags, bool on)
void setShaderFileName(Stage stage, const QString &filename)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:53:34 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.