Changeset View
Changeset View
Standalone View
Standalone View
src/scenegraph/shadowedrectanglematerial.cpp
- This file was added.
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 | | ||||
11 | ShadowedRectangleMaterial::ShadowedRectangleMaterial() | ||||
12 | { | ||||
13 | setFlag(QSGMaterial::Blending, true); | ||||
14 | } | ||||
15 | | ||||
16 | QSGMaterialShader* ShadowedRectangleMaterial::createShader() const | ||||
17 | { | ||||
18 | return new ElevatedRectangleShader{}; | ||||
19 | } | ||||
20 | | ||||
21 | QSGMaterialType* ShadowedRectangleMaterial::type() const | ||||
22 | { | ||||
23 | static QSGMaterialType type; | ||||
24 | return &type; | ||||
25 | } | ||||
26 | | ||||
27 | int ShadowedRectangleMaterial::compare(const QSGMaterial *other) const | ||||
28 | { | ||||
29 | auto material = static_cast<const ShadowedRectangleMaterial *>(other); | ||||
30 | | ||||
31 | if (material->color == color | ||||
32 | && material->shadowColor == shadowColor | ||||
33 | && material->offset == offset | ||||
34 | && material->aspect == aspect | ||||
35 | && qFuzzyCompare(material->size, size) | ||||
36 | && qFuzzyCompare(material->radius, radius)) { | ||||
37 | return 0; | ||||
38 | } | ||||
39 | | ||||
40 | return QSGMaterial::compare(other); | ||||
41 | } | ||||
42 | | ||||
43 | ElevatedRectangleShader::ElevatedRectangleShader() | ||||
44 | { | ||||
45 | auto header = QOpenGLContext::currentContext()->isOpenGLES() ? QStringLiteral("header_es.glsl") : QStringLiteral("header_desktop.glsl"); | ||||
46 | | ||||
47 | auto shaderRoot = QStringLiteral(":/org/kde/kirigami/shaders/"); | ||||
48 | | ||||
49 | setShaderSourceFiles(QOpenGLShader::Vertex, { | ||||
50 | shaderRoot + header, | ||||
51 | shaderRoot + QStringLiteral("shadowedrectangle.vert") | ||||
52 | }); | ||||
53 | | ||||
54 | setShaderSourceFiles(QOpenGLShader::Fragment, { | ||||
55 | shaderRoot + header, | ||||
56 | shaderRoot + QStringLiteral("shadowedrectangle.frag") | ||||
57 | }); | ||||
58 | } | ||||
59 | | ||||
60 | const char *const * ElevatedRectangleShader::attributeNames() const | ||||
61 | { | ||||
62 | static char const *const names[] = {"in_vertex", "in_uv", nullptr}; | ||||
63 | return names; | ||||
64 | } | ||||
65 | | ||||
66 | void ElevatedRectangleShader::initialize() | ||||
67 | { | ||||
68 | QSGMaterialShader::initialize(); | ||||
69 | m_matrixLocation = program()->uniformLocation("matrix"); | ||||
70 | m_aspectLocation = program()->uniformLocation("aspect"); | ||||
71 | m_opacityLocation = program()->uniformLocation("opacity"); | ||||
72 | m_sizeLocation = program()->uniformLocation("size"); | ||||
73 | m_radiusLocation = program()->uniformLocation("radius"); | ||||
74 | m_colorLocation = program()->uniformLocation("color"); | ||||
75 | m_shadowColorLocation = program()->uniformLocation("shadowColor"); | ||||
76 | m_offsetLocation = program()->uniformLocation("offset"); | ||||
77 | } | ||||
78 | | ||||
79 | void ElevatedRectangleShader::updateState(const QSGMaterialShader::RenderState& state, QSGMaterial* newMaterial, QSGMaterial* oldMaterial) | ||||
80 | { | ||||
81 | auto p = program(); | ||||
82 | | ||||
83 | if (state.isMatrixDirty()) { | ||||
84 | p->setUniformValue(m_matrixLocation, state.combinedMatrix()); | ||||
85 | } | ||||
86 | | ||||
87 | if (state.isOpacityDirty()) { | ||||
88 | p->setUniformValue(m_opacityLocation, state.opacity()); | ||||
89 | } | ||||
90 | | ||||
91 | if (!oldMaterial || newMaterial->compare(oldMaterial) != 0 || state.isCachedMaterialDataDirty()) { | ||||
92 | auto material = static_cast<ShadowedRectangleMaterial *>(newMaterial); | ||||
93 | p->setUniformValue(m_aspectLocation, material->aspect); | ||||
94 | p->setUniformValue(m_sizeLocation, material->size); | ||||
95 | p->setUniformValue(m_radiusLocation, material->radius); | ||||
96 | p->setUniformValue(m_colorLocation, material->color); | ||||
97 | p->setUniformValue(m_shadowColorLocation, material->shadowColor); | ||||
98 | p->setUniformValue(m_offsetLocation, material->offset); | ||||
99 | } | ||||
100 | } |