Fluid 1.3.0
Material Design for QtQuick and Qml
Loading...
Searching...
No Matches
elevationmaterial.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
2// SPDX-FileCopyrightText: 2024-2025 hypengw <hypengwip@gmail.com>
3// SPDX-License-Identifier: MPL-2.0
4
5#include <mutex>
6
7#include <QImage>
8#include <QQuickWindow>
9#include <QSGTexture>
10
11#include "elevationmaterial.h"
12#include "skia_shadow.h"
13
14using namespace Qt::StringLiterals;
15
17
19 : m_fadeoff_texture(nullptr)
20{
21 setFlag(QSGMaterial::Blending, true);
22}
23
25{
26 delete m_fadeoff_texture;
27}
28
29QSGMaterialShader *ElevationMaterial::createShader(QSGRendererInterface::RenderMode) const
30{
31 return new ElevationShader{ };
32}
33
34QSGMaterialType *ElevationMaterial::type() const
35{
36 static QSGMaterialType staticType;
37 return &staticType;
38}
39
40int ElevationMaterial::compare(const QSGMaterial *other) const
41{
42 // auto material = static_cast<const ElevationMaterial *>(other);
43 return QSGMaterial::compare(other);
44}
45
47{
48 return m_fadeoff_texture;
49}
51{
52 const int width = 128;
53 const int height = 1;
54 // Create a grayscale image (R8 equivalent)
55 QImage image(width, height, QImage::Format_Grayscale8);
56
57 // Fill the image with values for the red channel (e.g., gradient)
58 uchar *row = image.scanLine(0);
59 for (int i = 0; i < 128; i++) {
60 float d = Skia::SK_Scalar1 - i / 127.0f;
61 row[i] = Skia::SkScalarRoundToInt(((float)std::exp(-4 * d * d) - 0.018f) * 255);
62 }
63
64 // Create the texture from the grayscale image
65 QSGTexture *texture = win->createTextureFromImage(image);
66 m_fadeoff_texture = texture;
67}
68
70{
71 setShaderFileName(QSGMaterialShader::VertexStage, ":/Fluid/assets/shaders/shadow.vert.qsb"_L1);
72 setShaderFileName(QSGMaterialShader::FragmentStage,
73 ":/Fluid/assets/shaders/shadow.frag.qsb"_L1);
74}
75
76bool ElevationShader::updateUniformData(RenderState &state, QSGMaterial *newMaterial,
77 QSGMaterial *oldMaterial)
78{
79 Q_UNUSED(newMaterial);
80 Q_UNUSED(oldMaterial);
81
82 bool changed = false;
83 QByteArray *buf = state.uniformData();
84 Q_ASSERT(buf->size() >= 68);
85
86 if (state.isMatrixDirty()) {
87 const QMatrix4x4 m = state.combinedMatrix();
88 memcpy(buf->data(), m.constData(), 64);
89 changed = true;
90 }
91
92 if (state.isOpacityDirty()) {
93 const float opacity = std::pow(state.opacity(), 3);
94 memcpy(buf->data() + 64, &opacity, 4);
95 changed = true;
96 }
97 return changed;
98}
99
100void ElevationShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
101 QSGMaterial *newMaterial, QSGMaterial *)
102{
103 auto mat = static_cast<ElevationMaterial *>(newMaterial);
104 if (binding == 1) {
105 *texture = mat->strength_texture();
106 texture[0]->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
107 }
108}
109
110} // namespace Fluid::SceneGraph
QSGMaterialType * type() const override
int compare(const QSGMaterial *other) const override
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode) const override
void init_fadeoff_texture(QQuickWindow *win)
bool updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
void updateSampledImage(RenderState &, int binding, QSGTexture **texture, QSGMaterial *newMaterial, QSGMaterial *) override
constexpr auto SK_Scalar1
Definition skia_shadow.h:71
constexpr auto SkScalarRoundToInt(double x)
Definition skia_shadow.h:52