Extract Particles code to an independent library
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickshadereffectnode_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQUICKSHADEREFFECTNODE_P_H
43 #define QQUICKSHADEREFFECTNODE_P_H
44
45 #include <QtQuick/qsgnode.h>
46 #include <QtQuick/qsgmaterial.h>
47 #include <QtQuick/qsgtextureprovider.h>
48 #include <QtQuick/qquickitem.h>
49 #include <private/qtquickglobal_p.h>
50
51 #include <QtCore/qsharedpointer.h>
52 #include <QtCore/qpointer.h>
53
54 QT_BEGIN_HEADER
55
56 QT_BEGIN_NAMESPACE
57
58 struct QQuickShaderEffectMaterialKey {
59     enum ShaderType
60     {
61         VertexShader,
62         FragmentShader,
63         ShaderTypeCount
64     };
65
66     QByteArray sourceCode[ShaderTypeCount];
67     const char *className;
68
69     bool operator == (const QQuickShaderEffectMaterialKey &other) const;
70 };
71
72 uint qHash(const QQuickShaderEffectMaterialKey &key);
73
74
75 class QQuickCustomMaterialShader;
76 class QQuickShaderEffectNode;
77 class Q_QUICK_PRIVATE_EXPORT QQuickShaderEffectMaterial : public QSGMaterial
78 {
79 public:
80     struct UniformData
81     {
82         enum SpecialType { None, Sampler, Opacity, Matrix };
83
84         QByteArray name;
85         QVariant value;
86         SpecialType specialType;
87     };
88
89     enum CullMode
90     {
91         NoCulling,
92         BackFaceCulling,
93         FrontFaceCulling
94     };
95
96     explicit QQuickShaderEffectMaterial(QQuickShaderEffectNode *node = 0);
97     virtual QSGMaterialType *type() const;
98     virtual QSGMaterialShader *createShader() const;
99     virtual int compare(const QSGMaterial *other) const;
100
101     QVector<QByteArray> attributes;
102     QVector<UniformData> uniforms[QQuickShaderEffectMaterialKey::ShaderTypeCount];
103     QVector<QPair<QByteArray, QSGTextureProvider *> > textureProviders;
104     CullMode cullMode;
105
106     void setProgramSource(const QQuickShaderEffectMaterialKey &source);
107     void updateTextures() const;
108     void invalidateTextureProvider(QSGTextureProvider *provider);
109
110 protected:
111     friend class QQuickCustomMaterialShader;
112
113     // The type pointer needs to be unique. It is not safe to let the type object be part of the
114     // QQuickShaderEffectMaterial, since it can be deleted and a new one constructed on top of the old
115     // one. The new QQuickShaderEffectMaterial would then get the same type pointer as the old one, and
116     // CustomMaterialShaders based on the old one would incorrectly be used together with the new
117     // one. To guarantee that the type pointer is unique, the type object must live as long as
118     // there are any CustomMaterialShaders of that type.
119     QSharedPointer<QSGMaterialType> m_type;
120     QQuickShaderEffectMaterialKey m_source;
121
122     QQuickShaderEffectNode *m_node;
123     bool m_emittedLogChanged;
124
125     static QHash<QQuickShaderEffectMaterialKey, QSharedPointer<QSGMaterialType> > materialMap;
126 };
127
128
129 class QSGShaderEffectMesh;
130
131 class Q_QUICK_PRIVATE_EXPORT QQuickShaderEffectNode : public QObject, public QSGGeometryNode
132 {
133     Q_OBJECT
134 public:
135     QQuickShaderEffectNode();
136     virtual ~QQuickShaderEffectNode();
137
138     virtual void preprocess();
139
140 Q_SIGNALS:
141     void logAndStatusChanged(const QString &, int status);
142
143 private Q_SLOTS:
144     void markDirtyTexture();
145     void textureProviderDestroyed(QObject *object);
146 };
147
148 QT_END_NAMESPACE
149
150 QT_END_HEADER
151
152 #endif // QQUICKSHADEREFFECTNODE_P_H