File indexing completed on 2024-05-12 15:42:39

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "shadowedborderrectanglematerial.h"
0008 
0009 #include <QOpenGLContext>
0010 
0011 QSGMaterialType ShadowedBorderRectangleMaterial::staticType;
0012 
0013 ShadowedBorderRectangleMaterial::ShadowedBorderRectangleMaterial()
0014 {
0015     setFlag(QSGMaterial::Blending, true);
0016 }
0017 
0018 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0019 QSGMaterialShader *ShadowedBorderRectangleMaterial::createShader() const
0020 #else
0021 QSGMaterialShader *ShadowedBorderRectangleMaterial::createShader(QSGRendererInterface::RenderMode) const
0022 #endif
0023 {
0024     return new ShadowedBorderRectangleShader{shaderType};
0025 }
0026 
0027 QSGMaterialType *ShadowedBorderRectangleMaterial::type() const
0028 {
0029     return &staticType;
0030 }
0031 
0032 int ShadowedBorderRectangleMaterial::compare(const QSGMaterial *other) const
0033 {
0034     auto material = static_cast<const ShadowedBorderRectangleMaterial *>(other);
0035 
0036     auto result = ShadowedRectangleMaterial::compare(other);
0037     /* clang-format off */
0038     if (result == 0
0039         && material->borderColor == borderColor
0040         && qFuzzyCompare(material->borderWidth, borderWidth)) { /* clang-format on */
0041         return 0;
0042     }
0043 
0044     return QSGMaterial::compare(other);
0045 }
0046 
0047 ShadowedBorderRectangleShader::ShadowedBorderRectangleShader(ShadowedRectangleMaterial::ShaderType shaderType)
0048     : ShadowedRectangleShader(shaderType)
0049 {
0050     setShader(shaderType, QStringLiteral("shadowedborderrectangle"));
0051 }
0052 
0053 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0054 void ShadowedBorderRectangleShader::initialize()
0055 {
0056     ShadowedRectangleShader::initialize();
0057     m_borderWidthLocation = program()->uniformLocation("borderWidth");
0058     m_borderColorLocation = program()->uniformLocation("borderColor");
0059 }
0060 
0061 void ShadowedBorderRectangleShader::updateState(const QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
0062 {
0063     ShadowedRectangleShader::updateState(state, newMaterial, oldMaterial);
0064 
0065     auto p = program();
0066 
0067     if (!oldMaterial || newMaterial->compare(oldMaterial) != 0 || state.isCachedMaterialDataDirty()) {
0068         auto material = static_cast<ShadowedBorderRectangleMaterial *>(newMaterial);
0069         p->setUniformValue(m_borderWidthLocation, material->borderWidth);
0070         p->setUniformValue(m_borderColorLocation, material->borderColor);
0071     }
0072 }
0073 #else
0074 bool ShadowedBorderRectangleShader::updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
0075 {
0076     bool changed = ShadowedRectangleShader::updateUniformData(state, newMaterial, oldMaterial);
0077     QByteArray *buf = state.uniformData();
0078     Q_ASSERT(buf->size() >= 160);
0079 
0080     if (!oldMaterial || newMaterial->compare(oldMaterial) != 0) {
0081         const auto material = static_cast<ShadowedBorderRectangleMaterial *>(newMaterial);
0082         memcpy(buf->data() + 136, &material->borderWidth, 8);
0083         float c[4];
0084         material->borderColor.getRgbF(&c[0], &c[1], &c[2], &c[3]);
0085         memcpy(buf->data() + 144, c, 16);
0086         changed = true;
0087     }
0088 
0089     return changed;
0090 }
0091 #endif