| 1 | /* |
| 2 | * SPDX-FileCopyrightText: 2025 Arjen Hiemstra <ahiemstra@heimr.nl> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <QColor> |
| 10 | #include <QSGMaterial> |
| 11 | #include <QSGMaterialShader> |
| 12 | |
| 13 | #include "uniformdatastream.h" |
| 14 | |
| 15 | /** |
| 16 | * A material rendering a rectangle with a shadow. |
| 17 | * |
| 18 | * This material uses a distance field shader to render a rectangle with a |
| 19 | * shadow below it, optionally with rounded corners. |
| 20 | */ |
| 21 | class ShaderMaterial : public QSGMaterial |
| 22 | { |
| 23 | public: |
| 24 | ShaderMaterial(const QString &name); |
| 25 | ShaderMaterial(QSGMaterialType *type); |
| 26 | |
| 27 | QString name() const; |
| 28 | |
| 29 | QSGMaterialShader *createShader(QSGRendererInterface::RenderMode) const override; |
| 30 | QSGMaterialType *type() const override; |
| 31 | int compare(const QSGMaterial *other) const override; |
| 32 | |
| 33 | void setUniformBufferSize(qsizetype size); |
| 34 | std::span<char> uniformData(); |
| 35 | |
| 36 | QSGTexture *texture(int binding); |
| 37 | void setTexture(int binding, QSGTexture *texture); |
| 38 | |
| 39 | static QString nameForType(QSGMaterialType *type); |
| 40 | static QSGMaterialType *typeForName(const QString &name); |
| 41 | |
| 42 | private: |
| 43 | QString m_name; |
| 44 | QSGMaterialType *m_type; |
| 45 | |
| 46 | QByteArray m_uniformData; |
| 47 | QHash<int, QSGTexture *> m_textures; |
| 48 | |
| 49 | inline static QHash<QString, QSGMaterialType *> s_materialTypes; |
| 50 | }; |
| 51 | |
| 52 | class ShaderMaterialShader : public QSGMaterialShader |
| 53 | { |
| 54 | public: |
| 55 | ShaderMaterialShader(const QString &shaderName); |
| 56 | |
| 57 | bool updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
| 58 | void updateSampledImage(QSGMaterialShader::RenderState &state, // |
| 59 | int binding, |
| 60 | QSGTexture **texture, |
| 61 | QSGMaterial *newMaterial, |
| 62 | QSGMaterial *oldMaterial) override; |
| 63 | }; |
| 64 | |