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