1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Quick 3D.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 or (at your option) any later version
20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 #ifndef QSSGCUSTOMMATERIAL_H
31 #define QSSGCUSTOMMATERIAL_H
32 
33 //
34 //  W A R N I N G
35 //  -------------
36 //
37 // This file is not part of the Qt API.  It exists purely as an
38 // implementation detail.  This header file may change from version to
39 // version without notice, or even be removed.
40 //
41 // We mean it.
42 //
43 
44 #include <QtQuick3D/private/qquick3dmaterial_p.h>
45 #include <QtCore/qvector.h>
46 
47 #include <QtQuick3DRender/private/qssgrenderbasetypes_p.h>
48 #include <QtQuick3DRuntimeRender/private/qssgrendergraphobject_p.h>
49 #include <QtQuick3DRuntimeRender/private/qssgrenderdynamicobjectsystemcommands_p.h>
50 
51 #include <QtQuick3D/private/qquick3dshaderutils_p.h>
52 
53 
54 QT_BEGIN_NAMESPACE
55 
56 class Q_QUICK3D_EXPORT QQuick3DCustomMaterial : public QQuick3DMaterial
57 {
58     Q_OBJECT
59     Q_PROPERTY(bool hasTransparency READ hasTransparency WRITE setHasTransparency NOTIFY hasTransparencyChanged)
60     Q_PROPERTY(bool hasRefraction READ hasRefraction WRITE setHasRefraction NOTIFY hasRefractionChanged)
61     Q_PROPERTY(bool alwaysDirty READ alwaysDirty WRITE setAlwaysDirty NOTIFY alwaysDirtyChanged)
62     Q_PROPERTY(QQuick3DShaderUtilsShaderInfo *shaderInfo READ shaderInfo WRITE setShaderInfo)
63     Q_PROPERTY(QQmlListProperty<QQuick3DShaderUtilsRenderPass> passes READ passes)
64 
65 public:
66     explicit QQuick3DCustomMaterial(QQuick3DObject *parent = nullptr);
67     ~QQuick3DCustomMaterial() override;
68 
69     bool hasTransparency() const;
70     bool hasRefraction() const;
71     bool alwaysDirty() const;
72 
73     QQuick3DShaderUtilsShaderInfo *shaderInfo() const;
74     QQmlListProperty<QQuick3DShaderUtilsRenderPass> passes();
75 
76 public Q_SLOTS:
77     void setHasTransparency(bool hasTransparency);
78     void setHasRefraction(bool hasRefraction);
79     void setShaderInfo(QQuick3DShaderUtilsShaderInfo *shaderInfo);
80     void setAlwaysDirty(bool alwaysDirty);
81 
82 Q_SIGNALS:
83     void hasTransparencyChanged(bool hasTransparency);
84     void hasRefractionChanged(bool hasRefraction);
85     void alwaysDirtyChanged(bool alwaysDirty);
86 
87 protected:
88     QSSGRenderGraphObject *updateSpatialNode(QSSGRenderGraphObject *node) override;
89     void markAllDirty() override;
90 
91 private Q_SLOTS:
92     void onPropertyDirty();
93     void onTextureDirty(QQuick3DShaderUtilsTextureInput *texture);
94 
95 private:
96     enum Dirty {
97         TextureDirty = 0x1,
98         PropertyDirty = 0x2
99     };
100 
101     // Passes
102     static void qmlAppendPass(QQmlListProperty<QQuick3DShaderUtilsRenderPass> *list, QQuick3DShaderUtilsRenderPass *pass);
103     static QQuick3DShaderUtilsRenderPass *qmlPassAt(QQmlListProperty<QQuick3DShaderUtilsRenderPass> *list, int index);
104     static int qmlPassCount(QQmlListProperty<QQuick3DShaderUtilsRenderPass> *list);
105     static void qmlPassClear(QQmlListProperty<QQuick3DShaderUtilsRenderPass> *list);
106 
markDirty(QQuick3DCustomMaterial::Dirty type)107     void markDirty(QQuick3DCustomMaterial::Dirty type)
108     {
109         if (!(m_dirtyAttributes & quint32(type))) {
110             m_dirtyAttributes |= quint32(type);
111             update();
112         }
113     }
114 
115     quint32 m_dirtyAttributes = 0xffffffff;
116     bool m_hasTransparency = false;
117     bool m_hasRefraction = false;
118     QQuick3DShaderUtilsShaderInfo *m_shaderInfo = nullptr;
119     QVector<QQuick3DShaderUtilsRenderPass *> m_passes;
120     bool m_alwaysDirty = false;
121 };
122 
123 QT_END_NAMESPACE
124 
125 #endif // QSSGCUSTOMMATERIAL_H
126