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#include <QSGTexture>
13
14#include "uniformdatastream.h"
15
16/**
17 * A material rendering a rectangle with a shadow.
18 *
19 * This material uses a distance field shader to render a rectangle with a
20 * shadow below it, optionally with rounded corners.
21 */
22class ShaderMaterial : public QSGMaterial
23{
24public:
25 ShaderMaterial(const QString &name);
26 ShaderMaterial(QSGMaterialType *type);
27
28 QString name() const;
29
30 QSGMaterialShader *createShader(QSGRendererInterface::RenderMode) const override;
31 QSGMaterialType *type() const override;
32 int compare(const QSGMaterial *other) const override;
33
34 void setUniformBufferSize(qsizetype size);
35 std::span<char> uniformData();
36
37 QSGTexture *texture(int binding);
38 void setTexture(int binding, QSGTexture *texture);
39
40 QSGTexture::Filtering textureFiltering(int binding) const;
41 void setTextureFiltering(int binding, QSGTexture::Filtering filtering);
42
43 static QString nameForType(QSGMaterialType *type);
44 static QSGMaterialType *typeForName(const QString &name);
45
46private:
47 QString m_name;
48 QSGMaterialType *m_type;
49
50 QByteArray m_uniformData;
51 QHash<int, QSGTexture *> m_textures;
52
53 inline static std::unordered_map<QString, std::unique_ptr<QSGMaterialType>> s_materialTypes;
54};
55
56class ShaderMaterialShader : public QSGMaterialShader
57{
58public:
59 ShaderMaterialShader(const QString &shaderName);
60
61 bool updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
62 void updateSampledImage(QSGMaterialShader::RenderState &state, //
63 int binding,
64 QSGTexture **texture,
65 QSGMaterial *newMaterial,
66 QSGMaterial *oldMaterial) override;
67};
68

source code of kirigami/src/primitives/scenegraph/shadermaterial.h