1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QQUICKOPENGLSHADEREFFECTNODE_P_H
41 #define QQUICKOPENGLSHADEREFFECTNODE_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <private/qtquickglobal_p.h>
55 
56 QT_REQUIRE_CONFIG(quick_shadereffect);
57 
58 #include <QtQuick/qsgnode.h>
59 #include <QtQuick/qsgmaterial.h>
60 #include <QtQuick/qsgtextureprovider.h>
61 #include <QtQuick/qquickitem.h>
62 #include <private/qtquickglobal_p.h>
63 #include <private/qquickshadereffect_p.h>
64 
65 #include <QtCore/qsharedpointer.h>
66 #include <QtCore/qpointer.h>
67 
68 QT_BEGIN_NAMESPACE
69 
70 struct QQuickOpenGLShaderEffectMaterialKey {
71     enum ShaderType
72     {
73         VertexShader,
74         FragmentShader,
75         ShaderTypeCount
76     };
77 
78     QByteArray sourceCode[ShaderTypeCount];
79 
80     bool operator == (const QQuickOpenGLShaderEffectMaterialKey &other) const;
81     bool operator != (const QQuickOpenGLShaderEffectMaterialKey &other) const;
82 };
83 
84 uint qHash(const QQuickOpenGLShaderEffectMaterialKey &key);
85 
86 class QQuickCustomMaterialShader;
87 class QQuickOpenGLShaderEffectNode;
88 class Q_QUICK_PRIVATE_EXPORT QQuickOpenGLShaderEffectMaterial : public QSGMaterial
89 {
90 public:
91     struct UniformData
92     {
93         enum SpecialType { None, Sampler, SamplerExternal, SubRect, Opacity, Matrix };
94 
95         QByteArray name;
96         QVariant value;
97         int propertyIndex = -1;
98         SpecialType specialType;
99 
100         bool operator == (const UniformData &other) const;
101 
setValueFromPropertyUniformData102         void setValueFromProperty(QObject *item, const QMetaObject *itemMetaObject)
103         {
104             if (propertyIndex == -1) {
105                 value = item->property(name);
106             } else {
107                 value = itemMetaObject->property(propertyIndex).read(item);
108             }
109         }
110     };
111 
112     explicit QQuickOpenGLShaderEffectMaterial(QQuickOpenGLShaderEffectNode *node = nullptr);
113     QSGMaterialType *type() const override;
114     QSGMaterialShader *createShader() const override;
115     int compare(const QSGMaterial *other) const override;
116 
117     QVector<QByteArray> attributes;
118     QVector<UniformData> uniforms[QQuickOpenGLShaderEffectMaterialKey::ShaderTypeCount];
119     QVector<QSGTextureProvider *> textureProviders;
120     QQuickShaderEffect::CullMode cullMode;
121     bool geometryUsesTextureSubRect;
122 
123     void setProgramSource(const QQuickOpenGLShaderEffectMaterialKey &source);
124     void updateTextures() const;
125     void invalidateTextureProvider(const QObject *provider);
126 
127     static void cleanupMaterialCache();
128 
129 protected:
130     friend class QQuickCustomMaterialShader;
131 
132     // Each material needs a unique type to ensure that the renderer has a one
133     // and exactly one GL program for every unique set of shader sources.
134     // setProgramSource() stores the sources in a cache along with the right
135     // type. The type is cleaned up in cleanupMaterialCache() which is called
136     // when the GL context is shut down.
137     QSGMaterialType *m_type;
138     QQuickOpenGLShaderEffectMaterialKey m_source;
139 
140     QQuickOpenGLShaderEffectNode *m_node;
141     bool m_emittedLogChanged;
142 };
143 
144 
145 class QSGShaderEffectMesh;
146 
147 class Q_QUICK_PRIVATE_EXPORT QQuickOpenGLShaderEffectNode : public QObject, public QSGGeometryNode
148 {
149     Q_OBJECT
150 public:
151     QQuickOpenGLShaderEffectNode();
152     ~QQuickOpenGLShaderEffectNode() override;
153 
154     void preprocess() override;
155 
156 Q_SIGNALS:
157     void logAndStatusChanged(const QString &, int status);
158     void dirtyTexture();
159 
160 private Q_SLOTS:
161     void markDirtyTexture();
162     void textureProviderDestroyed(QObject *object);
163 };
164 
165 QT_END_NAMESPACE
166 
167 #endif // QQUICKOPENGLSHADEREFFECTNODE_P_H
168