Initial import from qtquick2.
[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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
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     glDisable(GL_CULL_FACE);
88 }
89
90 void QSGCustomMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
91 {
92     Q_ASSERT(newEffect != 0);
93
94     const QSGShaderEffectMaterial *material = static_cast<const QSGShaderEffectMaterial *>(newEffect);
95
96     if (!m_textureIndicesSet) {
97         for (int i = 0; i < material->m_textures.size(); ++i)
98             m_program.setUniformValue(material->m_textures.at(i).first.constData(), i);
99         m_textureIndicesSet = true;
100     }
101
102     if (m_uniformLocs.size() != material->m_uniformValues.size()) {
103         m_uniformLocs.reserve(material->m_uniformValues.size());
104         for (int i = 0; i < material->m_uniformValues.size(); ++i) {
105             const QByteArray &name = material->m_uniformValues.at(i).first;
106             m_uniformLocs.append(m_program.uniformLocation(name.constData()));
107         }
108     }
109
110     QGLFunctions *functions = state.context()->functions();
111     for (int i = material->m_textures.size() - 1; i >= 0; --i) {
112         QPointer<QSGItem> source = material->m_textures.at(i).second;
113         QSGTextureProvider *provider = QSGTextureProvider::from(source);
114         QSGTexture *texture = provider ? provider->texture() : 0;
115         if (!source || !provider || !texture) {
116             qWarning("ShaderEffectItem: source or provider missing when binding textures");
117             continue;
118         }
119         functions->glActiveTexture(GL_TEXTURE0 + i);
120         provider->texture()->bind();
121     }
122
123     if (material->m_source.respectsOpacity)
124         m_program.setUniformValue(m_opacityLoc, state.opacity());
125
126     for (int i = 0; i < material->m_uniformValues.count(); ++i) {
127         const QVariant &v = material->m_uniformValues.at(i).second;
128
129         switch (v.type()) {
130         case QVariant::Color:
131             m_program.setUniformValue(m_uniformLocs.at(i), qt_premultiply_color(qvariant_cast<QColor>(v)));
132             break;
133         case QVariant::Double:
134             m_program.setUniformValue(m_uniformLocs.at(i), (float) qvariant_cast<double>(v));
135             break;
136         case QVariant::Transform:
137             m_program.setUniformValue(m_uniformLocs.at(i), qvariant_cast<QTransform>(v));
138             break;
139         case QVariant::Int:
140             m_program.setUniformValue(m_uniformLocs.at(i), v.toInt());
141             break;
142         case QVariant::Bool:
143             m_program.setUniformValue(m_uniformLocs.at(i), GLint(v.toBool()));
144             break;
145         case QVariant::Size:
146         case QVariant::SizeF:
147             m_program.setUniformValue(m_uniformLocs.at(i), v.toSizeF());
148             break;
149         case QVariant::Point:
150         case QVariant::PointF:
151             m_program.setUniformValue(m_uniformLocs.at(i), v.toPointF());
152             break;
153         case QVariant::Rect:
154         case QVariant::RectF:
155             {
156                 QRectF r = v.toRectF();
157                 m_program.setUniformValue(m_uniformLocs.at(i), r.x(), r.y(), r.width(), r.height());
158             }
159             break;
160         case QVariant::Vector3D:
161             m_program.setUniformValue(m_uniformLocs.at(i), qvariant_cast<QVector3D>(v));
162             break;
163         default:
164             break;
165         }
166     }
167
168     const QSGShaderEffectMaterial *oldMaterial = static_cast<const QSGShaderEffectMaterial *>(oldEffect);
169     if (oldEffect == 0 || material->cullMode() != oldMaterial->cullMode()) {
170         switch (material->cullMode()) {
171         case QSGShaderEffectMaterial::FrontFaceCulling:
172             glEnable(GL_CULL_FACE);
173             glCullFace(GL_FRONT);
174             break;
175         case QSGShaderEffectMaterial::BackFaceCulling:
176             glEnable(GL_CULL_FACE);
177             glCullFace(GL_BACK);
178             break;
179         default:
180             glDisable(GL_CULL_FACE);
181             break;
182         }
183     }
184
185     if ((state.isMatrixDirty()) && material->m_source.respectsMatrix)
186         m_program.setUniformValue(m_matrixLoc, state.combinedMatrix());
187 }
188
189 char const *const *QSGCustomMaterialShader::attributeNames() const
190 {
191     return m_attributeNames.constData();
192 }
193
194 void QSGCustomMaterialShader::initialize()
195 {
196     m_opacityLoc = m_program.uniformLocation("qt_Opacity");
197     m_matrixLoc = m_program.uniformLocation("qt_ModelViewProjectionMatrix");
198 }
199
200 const char *QSGCustomMaterialShader::vertexShader() const
201 {
202     return m_key.vertexCode.constData();
203 }
204
205 const char *QSGCustomMaterialShader::fragmentShader() const
206 {
207     return m_key.fragmentCode.constData();
208 }
209
210
211 bool QSGShaderEffectMaterialKey::operator == (const QSGShaderEffectMaterialKey &other) const
212 {
213     return vertexCode == other.vertexCode && fragmentCode == other.fragmentCode && className == other.className;
214 }
215
216 uint qHash(const QSGShaderEffectMaterialKey &key)
217 {
218     return qHash(qMakePair(qMakePair(key.vertexCode, key.fragmentCode), key.className));
219 }
220
221
222 QHash<QSGShaderEffectMaterialKey, QSharedPointer<QSGMaterialType> > QSGShaderEffectMaterial::materialMap;
223
224 QSGShaderEffectMaterial::QSGShaderEffectMaterial()
225     : m_cullMode(NoCulling)
226 {
227     setFlag(Blending, true);
228 }
229
230 QSGMaterialType *QSGShaderEffectMaterial::type() const
231 {
232     return m_type.data();
233 }
234
235 QSGMaterialShader *QSGShaderEffectMaterial::createShader() const
236 {
237     return new QSGCustomMaterialShader(m_source, m_source.attributeNames);
238 }
239
240 int QSGShaderEffectMaterial::compare(const QSGMaterial *other) const
241 {
242     return this - static_cast<const QSGShaderEffectMaterial *>(other);
243 }
244
245 void QSGShaderEffectMaterial::setCullMode(QSGShaderEffectMaterial::CullMode face)
246 {
247     m_cullMode = face;
248 }
249
250 QSGShaderEffectMaterial::CullMode QSGShaderEffectMaterial::cullMode() const
251 {
252     return m_cullMode;
253 }
254
255 void QSGShaderEffectMaterial::setProgramSource(const QSGShaderEffectProgram &source)
256 {
257     m_source = source;
258     m_type = materialMap.value(m_source);
259     if (m_type.isNull()) {
260         m_type = QSharedPointer<QSGMaterialType>(new QSGMaterialType);
261         materialMap.insert(m_source, m_type);
262     }
263 }
264
265 void QSGShaderEffectMaterial::setUniforms(const QVector<QPair<QByteArray, QVariant> > &uniformValues)
266 {
267     m_uniformValues = uniformValues;
268 }
269
270 void QSGShaderEffectMaterial::setTextureProviders(const QVector<QPair<QByteArray, QPointer<QSGItem> > > &textures)
271 {
272     m_textures = textures;
273 }
274
275 const QVector<QPair<QByteArray, QPointer<QSGItem> > > &QSGShaderEffectMaterial::textureProviders() const
276 {
277     return m_textures;
278 }
279
280 void QSGShaderEffectMaterial::updateTextures() const
281 {
282     for (int i = 0; i < m_textures.size(); ++i) {
283         QSGItem *item = m_textures.at(i).second;
284         if (item) {
285             QSGTextureProvider *provider = QSGTextureProvider::from(item);
286             if (provider) {
287                 QSGTexture *texture = provider->texture();
288                 if (!texture) {
289                     qWarning("QSGShaderEffectMaterial: no texture from %s [%s]",
290                              qPrintable(item->objectName()),
291                              item->metaObject()->className());
292                 }
293                 if (QSGDynamicTexture *t = qobject_cast<QSGDynamicTexture *>(provider->texture())) {
294                     t->updateTexture();
295                 }
296             }
297         }
298     }
299 }
300
301
302 QSGShaderEffectNode::QSGShaderEffectNode()
303 {
304     QSGNode::setFlag(UsePreprocess, true);
305 }
306
307 QSGShaderEffectNode::~QSGShaderEffectNode()
308 {
309 }
310
311 void QSGShaderEffectNode::markDirtyTexture()
312 {
313     markDirty(DirtyMaterial);
314 }
315
316 void QSGShaderEffectNode::preprocess()
317 {
318     Q_ASSERT(material());
319     static_cast<QSGShaderEffectMaterial *>(material())->updateTextures();
320 }
321
322 QT_END_NAMESPACE