File indexing completed on 2024-05-05 16:16:42
0001 /* 0002 * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "BarChartMaterial.h" 0008 0009 BarChartMaterial::BarChartMaterial() 0010 { 0011 setFlag(QSGMaterial::Blending); 0012 } 0013 0014 BarChartMaterial::~BarChartMaterial() 0015 { 0016 } 0017 0018 QSGMaterialType *BarChartMaterial::type() const 0019 { 0020 static QSGMaterialType type; 0021 return &type; 0022 } 0023 0024 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0025 QSGMaterialShader *BarChartMaterial::createShader() const 0026 #else 0027 QSGMaterialShader *BarChartMaterial::createShader(QSGRendererInterface::RenderMode) const 0028 #endif 0029 { 0030 return new BarChartShader(); 0031 } 0032 0033 int BarChartMaterial::compare(const QSGMaterial *other) const 0034 { 0035 auto material = static_cast<const BarChartMaterial *>(other); 0036 0037 /* clang-format off */ 0038 if (material->aspect == aspect 0039 && qFuzzyCompare(material->radius, radius) 0040 && material->backgroundColor == backgroundColor) { /* clang-format on */ 0041 return 0; 0042 } 0043 0044 return QSGMaterial::compare(other); 0045 } 0046 0047 BarChartShader::BarChartShader() 0048 { 0049 setShaders(QStringLiteral("barchart.vert"), QStringLiteral("barchart.frag")); 0050 } 0051 0052 BarChartShader::~BarChartShader() 0053 { 0054 } 0055 0056 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0057 const char *const *BarChartShader::attributeNames() const 0058 { 0059 static const char *const names[] = {"in_vertex", "in_uv", "in_color", "in_value", nullptr}; 0060 return names; 0061 } 0062 0063 void BarChartShader::initialize() 0064 { 0065 QSGMaterialShader::initialize(); 0066 m_matrixLocation = program()->uniformLocation("matrix"); 0067 m_opacityLocation = program()->uniformLocation("opacity"); 0068 m_aspectLocation = program()->uniformLocation("aspect"); 0069 m_backgroundColorLocation = program()->uniformLocation("backgroundColor"); 0070 m_radiusLocation = program()->uniformLocation("radius"); 0071 } 0072 0073 void BarChartShader::updateState(const QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) 0074 { 0075 if (state.isMatrixDirty()) { 0076 program()->setUniformValue(m_matrixLocation, state.combinedMatrix()); 0077 } 0078 if (state.isOpacityDirty()) { 0079 program()->setUniformValue(m_opacityLocation, state.opacity()); 0080 } 0081 0082 if (!oldMaterial || newMaterial->compare(oldMaterial) != 0) { 0083 BarChartMaterial *material = static_cast<BarChartMaterial *>(newMaterial); 0084 program()->setUniformValue(m_aspectLocation, material->aspect); 0085 program()->setUniformValue(m_backgroundColorLocation, material->backgroundColor); 0086 program()->setUniformValue(m_radiusLocation, material->radius); 0087 } 0088 } 0089 #else 0090 bool BarChartShader::updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) 0091 { 0092 bool changed = false; 0093 QByteArray *buf = state.uniformData(); 0094 Q_ASSERT(buf->size() >= 96); 0095 0096 if (state.isMatrixDirty()) { 0097 const QMatrix4x4 m = state.combinedMatrix(); 0098 memcpy(buf->data(), m.constData(), 64); 0099 changed = true; 0100 } 0101 0102 if (state.isOpacityDirty()) { 0103 const float opacity = state.opacity(); 0104 memcpy(buf->data() + 72, &opacity, 4); 0105 changed = true; 0106 } 0107 0108 if (!oldMaterial || newMaterial->compare(oldMaterial) != 0) { 0109 const auto material = static_cast<BarChartMaterial *>(newMaterial); 0110 memcpy(buf->data() + 64, &material->aspect, 8); 0111 memcpy(buf->data() + 76, &material->radius, 4); 0112 float c[4]; 0113 material->backgroundColor.getRgbF(&c[0], &c[1], &c[2], &c[3]); 0114 memcpy(buf->data() + 80, c, 16); 0115 changed = true; 0116 } 0117 0118 return changed; 0119 } 0120 #endif