Rename Qt Quick-specific classes to QQuick*
[profile/ivi/qtdeclarative.git] / src / declarative / items / qquickshadereffectnode.cpp
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 #include <private/qquickshadereffectnode_p.h>
43
44 #include "qquickshadereffectmesh_p.h"
45 #include <private/qsgtextureprovider_p.h>
46 #include <private/qsgrenderer_p.h>
47
48 QT_BEGIN_NAMESPACE
49
50 class QQuickCustomMaterialShader : public QSGMaterialShader
51 {
52 public:
53     QQuickCustomMaterialShader(const QQuickShaderEffectMaterialKey &key, const QVector<QByteArray> &attributes);
54     virtual void deactivate();
55     virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
56     virtual char const *const *attributeNames() const;
57
58 protected:
59     friend class QQuickShaderEffectNode;
60
61     virtual void initialize();
62     virtual const char *vertexShader() const;
63     virtual const char *fragmentShader() const;
64
65     const QQuickShaderEffectMaterialKey m_key;
66     QVector<const char *> m_attributeNames;
67     const QVector<QByteArray> m_attributes;
68
69     QVector<int> m_uniformLocs;
70     int m_opacityLoc;
71     int m_matrixLoc;
72     uint m_textureIndicesSet;
73 };
74
75 QQuickCustomMaterialShader::QQuickCustomMaterialShader(const QQuickShaderEffectMaterialKey &key, const QVector<QByteArray> &attributes)
76     : m_key(key)
77     , m_attributes(attributes)
78     , m_textureIndicesSet(false)
79 {
80     for (int i = 0; i < attributes.count(); ++i)
81         m_attributeNames.append(attributes.at(i).constData());
82     m_attributeNames.append(0);
83 }
84
85 void QQuickCustomMaterialShader::deactivate()
86 {
87     QSGMaterialShader::deactivate();
88     glDisable(GL_CULL_FACE);
89 }
90
91 void QQuickCustomMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
92 {
93     Q_ASSERT(newEffect != 0);
94
95     const QQuickShaderEffectMaterial *material = static_cast<const QQuickShaderEffectMaterial *>(newEffect);
96
97     if (!m_textureIndicesSet) {
98         for (int i = 0; i < material->m_textures.size(); ++i)
99             program()->setUniformValue(material->m_textures.at(i).first.constData(), i);
100         m_textureIndicesSet = true;
101     }
102
103     if (m_uniformLocs.size() != material->m_uniformValues.size()) {
104         m_uniformLocs.reserve(material->m_uniformValues.size());
105         for (int i = 0; i < material->m_uniformValues.size(); ++i) {
106             const QByteArray &name = material->m_uniformValues.at(i).first;
107             m_uniformLocs.append(program()->uniformLocation(name.constData()));
108         }
109     }
110
111     QOpenGLFunctions *functions = state.context()->functions();
112     for (int i = material->m_textures.size() - 1; i >= 0; --i) {
113         functions->glActiveTexture(GL_TEXTURE0 + i);
114         if (QSGTextureProvider *provider = material->m_textures.at(i).second) {
115             if (QSGTexture *texture = provider->texture()) {
116                 texture->bind();
117                 continue;
118             }
119         }
120         qWarning("ShaderEffectItem: source or provider missing when binding textures");
121         glBindTexture(GL_TEXTURE_2D, 0);
122     }
123
124     if (material->m_source.respectsOpacity)
125         program()->setUniformValue(m_opacityLoc, state.opacity());
126
127     for (int i = 0; i < material->m_uniformValues.count(); ++i) {
128         const QVariant &v = material->m_uniformValues.at(i).second;
129
130         switch (v.type()) {
131         case QVariant::Color:
132             program()->setUniformValue(m_uniformLocs.at(i), qt_premultiply_color(qvariant_cast<QColor>(v)));
133             break;
134         case QVariant::Double:
135             program()->setUniformValue(m_uniformLocs.at(i), (float) qvariant_cast<double>(v));
136             break;
137         case QVariant::Transform:
138             program()->setUniformValue(m_uniformLocs.at(i), qvariant_cast<QTransform>(v));
139             break;
140         case QVariant::Int:
141             program()->setUniformValue(m_uniformLocs.at(i), v.toInt());
142             break;
143         case QVariant::Bool:
144             program()->setUniformValue(m_uniformLocs.at(i), GLint(v.toBool()));
145             break;
146         case QVariant::Size:
147         case QVariant::SizeF:
148             program()->setUniformValue(m_uniformLocs.at(i), v.toSizeF());
149             break;
150         case QVariant::Point:
151         case QVariant::PointF:
152             program()->setUniformValue(m_uniformLocs.at(i), v.toPointF());
153             break;
154         case QVariant::Rect:
155         case QVariant::RectF:
156             {
157                 QRectF r = v.toRectF();
158                 program()->setUniformValue(m_uniformLocs.at(i), r.x(), r.y(), r.width(), r.height());
159             }
160             break;
161         case QVariant::Vector3D:
162             program()->setUniformValue(m_uniformLocs.at(i), qvariant_cast<QVector3D>(v));
163             break;
164         default:
165             break;
166         }
167     }
168
169     const QQuickShaderEffectMaterial *oldMaterial = static_cast<const QQuickShaderEffectMaterial *>(oldEffect);
170     if (oldEffect == 0 || material->cullMode() != oldMaterial->cullMode()) {
171         switch (material->cullMode()) {
172         case QQuickShaderEffectMaterial::FrontFaceCulling:
173             glEnable(GL_CULL_FACE);
174             glCullFace(GL_FRONT);
175             break;
176         case QQuickShaderEffectMaterial::BackFaceCulling:
177             glEnable(GL_CULL_FACE);
178             glCullFace(GL_BACK);
179             break;
180         default:
181             glDisable(GL_CULL_FACE);
182             break;
183         }
184     }
185
186     if ((state.isMatrixDirty()) && material->m_source.respectsMatrix)
187         program()->setUniformValue(m_matrixLoc, state.combinedMatrix());
188 }
189
190 char const *const *QQuickCustomMaterialShader::attributeNames() const
191 {
192     return m_attributeNames.constData();
193 }
194
195 void QQuickCustomMaterialShader::initialize()
196 {
197     m_opacityLoc = program()->uniformLocation("qt_Opacity");
198     m_matrixLoc = program()->uniformLocation("qt_Matrix");
199     // TODO: Remove after grace period.
200     if (m_matrixLoc == -1)
201         m_matrixLoc = program()->uniformLocation("qt_ModelViewProjectionMatrix");
202 }
203
204 const char *QQuickCustomMaterialShader::vertexShader() const
205 {
206     return m_key.vertexCode.constData();
207 }
208
209 const char *QQuickCustomMaterialShader::fragmentShader() const
210 {
211     return m_key.fragmentCode.constData();
212 }
213
214
215 bool QQuickShaderEffectMaterialKey::operator == (const QQuickShaderEffectMaterialKey &other) const
216 {
217     return vertexCode == other.vertexCode && fragmentCode == other.fragmentCode && className == other.className;
218 }
219
220 uint qHash(const QQuickShaderEffectMaterialKey &key)
221 {
222     return qHash(qMakePair(qMakePair(key.vertexCode, key.fragmentCode), key.className));
223 }
224
225
226 QHash<QQuickShaderEffectMaterialKey, QSharedPointer<QSGMaterialType> > QQuickShaderEffectMaterial::materialMap;
227
228 QQuickShaderEffectMaterial::QQuickShaderEffectMaterial()
229     : m_cullMode(NoCulling)
230 {
231     setFlag(Blending, true);
232 }
233
234 QSGMaterialType *QQuickShaderEffectMaterial::type() const
235 {
236     return m_type.data();
237 }
238
239 QSGMaterialShader *QQuickShaderEffectMaterial::createShader() const
240 {
241     return new QQuickCustomMaterialShader(m_source, m_source.attributeNames);
242 }
243
244 int QQuickShaderEffectMaterial::compare(const QSGMaterial *other) const
245 {
246     return this - static_cast<const QQuickShaderEffectMaterial *>(other);
247 }
248
249 void QQuickShaderEffectMaterial::setCullMode(QQuickShaderEffectMaterial::CullMode face)
250 {
251     m_cullMode = face;
252 }
253
254 QQuickShaderEffectMaterial::CullMode QQuickShaderEffectMaterial::cullMode() const
255 {
256     return m_cullMode;
257 }
258
259 void QQuickShaderEffectMaterial::setProgramSource(const QQuickShaderEffectProgram &source)
260 {
261     m_source = source;
262     m_type = materialMap.value(m_source);
263     if (m_type.isNull()) {
264         m_type = QSharedPointer<QSGMaterialType>(new QSGMaterialType);
265         materialMap.insert(m_source, m_type);
266     }
267 }
268
269 void QQuickShaderEffectMaterial::setUniforms(const QVector<QPair<QByteArray, QVariant> > &uniformValues)
270 {
271     m_uniformValues = uniformValues;
272 }
273
274 void QQuickShaderEffectMaterial::setTextureProviders(const QVector<QPair<QByteArray, QSGTextureProvider *> > &textures)
275 {
276     m_textures = textures;
277 }
278
279 const QVector<QPair<QByteArray, QSGTextureProvider *> > &QQuickShaderEffectMaterial::textureProviders() const
280 {
281     return m_textures;
282 }
283
284 void QQuickShaderEffectMaterial::updateTextures() const
285 {
286     for (int i = 0; i < m_textures.size(); ++i) {
287         if (QSGTextureProvider *provider = m_textures.at(i).second) {
288             if (QSGDynamicTexture *texture = qobject_cast<QSGDynamicTexture *>(provider->texture()))
289                 texture->updateTexture();
290         }
291     }
292 }
293
294
295 QQuickShaderEffectNode::QQuickShaderEffectNode()
296 {
297     QSGNode::setFlag(UsePreprocess, true);
298     setMaterial(&m_material);
299 }
300
301 QQuickShaderEffectNode::~QQuickShaderEffectNode()
302 {
303 }
304
305 void QQuickShaderEffectNode::markDirtyTexture()
306 {
307     markDirty(DirtyMaterial);
308 }
309
310 void QQuickShaderEffectNode::preprocess()
311 {
312     Q_ASSERT(material());
313     static_cast<QQuickShaderEffectMaterial *>(material())->updateTextures();
314 }
315
316 QT_END_NAMESPACE