Move QSGShaderEffectMaterial into the QSGShaderEffectNode. Now owned by Render Thread
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgshadereffectnode_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef SHADEREFFECTNODE_H
43 #define SHADEREFFECTNODE_H
44
45 #include "qsgnode.h"
46 #include "qsgmaterial.h"
47 #include <private/qsgtextureprovider_p.h>
48 #include <qsgitem.h>
49
50 #include <QtCore/qsharedpointer.h>
51 #include <QtCore/qpointer.h>
52
53 QT_BEGIN_HEADER
54
55 QT_BEGIN_NAMESPACE
56
57 QT_MODULE(Declarative)
58
59 struct QSGShaderEffectMaterialKey {
60     QByteArray vertexCode;
61     QByteArray fragmentCode;
62     const char *className;
63
64     bool operator == (const QSGShaderEffectMaterialKey &other) const;
65 };
66
67 uint qHash(const QSGShaderEffectMaterialKey &key);
68
69 // TODO: Implement support for multisampling.
70 struct QSGShaderEffectProgram : public QSGShaderEffectMaterialKey
71 {
72     QSGShaderEffectProgram() : respectsOpacity(false), respectsMatrix(false) {}
73
74     QVector<QByteArray> attributeNames;
75     QSet<QByteArray> uniformNames;
76
77     uint respectsOpacity : 1;
78     uint respectsMatrix : 1;
79 };
80
81
82 class QSGCustomMaterialShader;
83 class QSGShaderEffectMaterial : public QSGMaterial
84 {
85 public:
86     enum CullMode
87     {
88         NoCulling,
89         BackFaceCulling,
90         FrontFaceCulling
91     };
92
93     QSGShaderEffectMaterial();
94     virtual QSGMaterialType *type() const;
95     virtual QSGMaterialShader *createShader() const;
96     virtual int compare(const QSGMaterial *other) const;
97
98     void setCullMode(CullMode face);
99     CullMode cullMode() const;
100
101     void setProgramSource(const QSGShaderEffectProgram &);
102     void setUniforms(const QVector<QPair<QByteArray, QVariant> > &uniformValues);
103     void setTextureProviders(const QVector<QPair<QByteArray, QPointer<QSGItem> > > &textures);
104     const QVector<QPair<QByteArray, QPointer<QSGItem> > > &textureProviders() const;
105     void updateTextures() const;
106
107 protected:
108     friend class QSGShaderEffect;
109     friend class QSGCustomMaterialShader;
110
111     // The type pointer needs to be unique. It is not safe to let the type object be part of the
112     // QSGShaderEffectMaterial, since it can be deleted and a new one constructed on top of the old
113     // one. The new QSGShaderEffectMaterial would then get the same type pointer as the old one, and
114     // CustomMaterialShaders based on the old one would incorrectly be used together with the new
115     // one. To guarantee that the type pointer is unique, the type object must live as long as
116     // there are any CustomMaterialShaders of that type.
117     QSharedPointer<QSGMaterialType> m_type;
118
119     QSGShaderEffectProgram m_source;
120     QVector<QPair<QByteArray, QVariant> > m_uniformValues;
121     QVector<QPair<QByteArray, QPointer<QSGItem> > > m_textures;
122     CullMode m_cullMode;
123
124     static QHash<QSGShaderEffectMaterialKey, QSharedPointer<QSGMaterialType> > materialMap;
125 };
126
127
128 class QSGShaderEffectMesh;
129
130 class QSGShaderEffectNode : public QObject, public QSGGeometryNode
131 {
132     Q_OBJECT
133 public:
134     QSGShaderEffectNode();
135     virtual ~QSGShaderEffectNode();
136
137     virtual void preprocess();
138
139     QSGShaderEffectMaterial *shaderMaterial() { return &m_material; }
140
141 private Q_SLOTS:
142     void markDirtyTexture();
143
144 private:
145     QSGShaderEffectMaterial m_material;
146
147
148 };
149
150 QT_END_NAMESPACE
151
152 QT_END_HEADER
153
154 #endif // SHADEREFFECTNODE_H