8bd781ce5841ca0fba84783d994a29fab70282c8
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgshadereffectnode.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/qsgshadereffectnode_p.h>
43
44 #include "qsgshadereffectmesh_p.h"
45 #include <private/qsgtextureprovider_p.h>
46 #include <private/qsgrenderer_p.h>
47
48 QT_BEGIN_NAMESPACE
49
50 class QSGCustomMaterialShader : public QSGMaterialShader
51 {
52 public:
53     QSGCustomMaterialShader(const QSGShaderEffectMaterialKey &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 QSGShaderEffectNode;
60
61     virtual void initialize();
62     virtual const char *vertexShader() const;
63     virtual const char *fragmentShader() const;
64
65     const QSGShaderEffectMaterialKey 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 QSGCustomMaterialShader::QSGCustomMaterialShader(const QSGShaderEffectMaterialKey &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 QSGCustomMaterialShader::deactivate()
86 {
87     QSGMaterialShader::deactivate();
88     glDisable(GL_CULL_FACE);
89 }
90
91 void QSGCustomMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
92 {
93     Q_ASSERT(newEffect != 0);
94
95     const QSGShaderEffectMaterial *material = static_cast<const QSGShaderEffectMaterial *>(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     QGLFunctions *functions = state.context()->functions();
112     for (int i = material->m_textures.size() - 1; i >= 0; --i) {
113         QPointer<QSGItem> source = material->m_textures.at(i).second;
114         QSGTextureProvider *provider = QSGTextureProvider::from(source);
115         QSGTexture *texture = provider ? provider->texture() : 0;
116         if (!source || !provider || !texture) {
117             qWarning("ShaderEffectItem: source or provider missing when binding textures");
118             continue;
119         }
120         functions->glActiveTexture(GL_TEXTURE0 + i);
121         provider->texture()->bind();
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 QSGShaderEffectMaterial *oldMaterial = static_cast<const QSGShaderEffectMaterial *>(oldEffect);
170     if (oldEffect == 0 || material->cullMode() != oldMaterial->cullMode()) {
171         switch (material->cullMode()) {
172         case QSGShaderEffectMaterial::FrontFaceCulling:
173             glEnable(GL_CULL_FACE);
174             glCullFace(GL_FRONT);
175             break;
176         case QSGShaderEffectMaterial::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 *QSGCustomMaterialShader::attributeNames() const
191 {
192     return m_attributeNames.constData();
193 }
194
195 void QSGCustomMaterialShader::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 *QSGCustomMaterialShader::vertexShader() const
205 {
206     return m_key.vertexCode.constData();
207 }
208
209 const char *QSGCustomMaterialShader::fragmentShader() const
210 {
211     return m_key.fragmentCode.constData();
212 }
213
214
215 bool QSGShaderEffectMaterialKey::operator == (const QSGShaderEffectMaterialKey &other) const
216 {
217     return vertexCode == other.vertexCode && fragmentCode == other.fragmentCode && className == other.className;
218 }
219
220 uint qHash(const QSGShaderEffectMaterialKey &key)
221 {
222     return qHash(qMakePair(qMakePair(key.vertexCode, key.fragmentCode), key.className));
223 }
224
225
226 QHash<QSGShaderEffectMaterialKey, QSharedPointer<QSGMaterialType> > QSGShaderEffectMaterial::materialMap;
227
228 QSGShaderEffectMaterial::QSGShaderEffectMaterial()
229     : m_cullMode(NoCulling)
230 {
231     setFlag(Blending, true);
232 }
233
234 QSGMaterialType *QSGShaderEffectMaterial::type() const
235 {
236     return m_type.data();
237 }
238
239 QSGMaterialShader *QSGShaderEffectMaterial::createShader() const
240 {
241     return new QSGCustomMaterialShader(m_source, m_source.attributeNames);
242 }
243
244 int QSGShaderEffectMaterial::compare(const QSGMaterial *other) const
245 {
246     return this - static_cast<const QSGShaderEffectMaterial *>(other);
247 }
248
249 void QSGShaderEffectMaterial::setCullMode(QSGShaderEffectMaterial::CullMode face)
250 {
251     m_cullMode = face;
252 }
253
254 QSGShaderEffectMaterial::CullMode QSGShaderEffectMaterial::cullMode() const
255 {
256     return m_cullMode;
257 }
258
259 void QSGShaderEffectMaterial::setProgramSource(const QSGShaderEffectProgram &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 QSGShaderEffectMaterial::setUniforms(const QVector<QPair<QByteArray, QVariant> > &uniformValues)
270 {
271     m_uniformValues = uniformValues;
272 }
273
274 void QSGShaderEffectMaterial::setTextureProviders(const QVector<QPair<QByteArray, QPointer<QSGItem> > > &textures)
275 {
276     m_textures = textures;
277 }
278
279 const QVector<QPair<QByteArray, QPointer<QSGItem> > > &QSGShaderEffectMaterial::textureProviders() const
280 {
281     return m_textures;
282 }
283
284 void QSGShaderEffectMaterial::updateTextures() const
285 {
286     for (int i = 0; i < m_textures.size(); ++i) {
287         QSGItem *item = m_textures.at(i).second;
288         if (item) {
289             QSGTextureProvider *provider = QSGTextureProvider::from(item);
290             if (provider) {
291                 QSGTexture *texture = provider->texture();
292                 if (!texture) {
293                     qWarning("QSGShaderEffectMaterial: no texture from %s [%s]",
294                              qPrintable(item->objectName()),
295                              item->metaObject()->className());
296                 }
297                 if (QSGDynamicTexture *t = qobject_cast<QSGDynamicTexture *>(provider->texture())) {
298                     t->updateTexture();
299                 }
300             }
301         }
302     }
303 }
304
305
306 QSGShaderEffectNode::QSGShaderEffectNode()
307 {
308     QSGNode::setFlag(UsePreprocess, true);
309 }
310
311 QSGShaderEffectNode::~QSGShaderEffectNode()
312 {
313 }
314
315 void QSGShaderEffectNode::markDirtyTexture()
316 {
317     markDirty(DirtyMaterial);
318 }
319
320 void QSGShaderEffectNode::preprocess()
321 {
322     Q_ASSERT(material());
323     static_cast<QSGShaderEffectMaterial *>(material())->updateTextures();
324 }
325
326 QT_END_NAMESPACE