1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <private/qquickshadereffectnode_p.h>
44 #include "qquickshadereffectmesh_p.h"
45 #include <private/qsgtextureprovider_p.h>
46 #include <private/qsgrenderer_p.h>
50 class QQuickCustomMaterialShader : public QSGMaterialShader
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;
59 friend class QQuickShaderEffectNode;
61 virtual void initialize();
62 virtual const char *vertexShader() const;
63 virtual const char *fragmentShader() const;
65 const QQuickShaderEffectMaterialKey m_key;
66 QVector<const char *> m_attributeNames;
67 const QVector<QByteArray> m_attributes;
69 QVector<int> m_uniformLocs;
72 uint m_textureIndicesSet;
75 QQuickCustomMaterialShader::QQuickCustomMaterialShader(const QQuickShaderEffectMaterialKey &key, const QVector<QByteArray> &attributes)
77 , m_attributes(attributes)
78 , m_textureIndicesSet(false)
80 for (int i = 0; i < attributes.count(); ++i)
81 m_attributeNames.append(attributes.at(i).constData());
82 m_attributeNames.append(0);
85 void QQuickCustomMaterialShader::deactivate()
87 QSGMaterialShader::deactivate();
88 glDisable(GL_CULL_FACE);
91 void QQuickCustomMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
93 Q_ASSERT(newEffect != 0);
95 const QQuickShaderEffectMaterial *material = static_cast<const QQuickShaderEffectMaterial *>(newEffect);
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;
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()));
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()) {
120 qWarning("ShaderEffectItem: source or provider missing when binding textures");
121 glBindTexture(GL_TEXTURE_2D, 0);
124 if (material->m_source.respectsOpacity)
125 program()->setUniformValue(m_opacityLoc, state.opacity());
127 for (int i = 0; i < material->m_uniformValues.count(); ++i) {
128 const QVariant &v = material->m_uniformValues.at(i).second;
131 case QVariant::Color:
132 program()->setUniformValue(m_uniformLocs.at(i), qt_premultiply_color(qvariant_cast<QColor>(v)));
134 case QVariant::Double:
135 program()->setUniformValue(m_uniformLocs.at(i), (float) qvariant_cast<double>(v));
137 case QVariant::Transform:
138 program()->setUniformValue(m_uniformLocs.at(i), qvariant_cast<QTransform>(v));
141 program()->setUniformValue(m_uniformLocs.at(i), v.toInt());
144 program()->setUniformValue(m_uniformLocs.at(i), GLint(v.toBool()));
147 case QVariant::SizeF:
148 program()->setUniformValue(m_uniformLocs.at(i), v.toSizeF());
150 case QVariant::Point:
151 case QVariant::PointF:
152 program()->setUniformValue(m_uniformLocs.at(i), v.toPointF());
155 case QVariant::RectF:
157 QRectF r = v.toRectF();
158 program()->setUniformValue(m_uniformLocs.at(i), r.x(), r.y(), r.width(), r.height());
161 case QVariant::Vector3D:
162 program()->setUniformValue(m_uniformLocs.at(i), qvariant_cast<QVector3D>(v));
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);
176 case QQuickShaderEffectMaterial::BackFaceCulling:
177 glEnable(GL_CULL_FACE);
181 glDisable(GL_CULL_FACE);
186 if ((state.isMatrixDirty()) && material->m_source.respectsMatrix)
187 program()->setUniformValue(m_matrixLoc, state.combinedMatrix());
190 char const *const *QQuickCustomMaterialShader::attributeNames() const
192 return m_attributeNames.constData();
195 void QQuickCustomMaterialShader::initialize()
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");
204 const char *QQuickCustomMaterialShader::vertexShader() const
206 return m_key.vertexCode.constData();
209 const char *QQuickCustomMaterialShader::fragmentShader() const
211 return m_key.fragmentCode.constData();
215 bool QQuickShaderEffectMaterialKey::operator == (const QQuickShaderEffectMaterialKey &other) const
217 return vertexCode == other.vertexCode && fragmentCode == other.fragmentCode && className == other.className;
220 uint qHash(const QQuickShaderEffectMaterialKey &key)
222 return qHash(qMakePair(qMakePair(key.vertexCode, key.fragmentCode), key.className));
226 QHash<QQuickShaderEffectMaterialKey, QSharedPointer<QSGMaterialType> > QQuickShaderEffectMaterial::materialMap;
228 QQuickShaderEffectMaterial::QQuickShaderEffectMaterial()
229 : m_cullMode(NoCulling)
231 setFlag(Blending, true);
234 QSGMaterialType *QQuickShaderEffectMaterial::type() const
236 return m_type.data();
239 QSGMaterialShader *QQuickShaderEffectMaterial::createShader() const
241 return new QQuickCustomMaterialShader(m_source, m_source.attributeNames);
244 int QQuickShaderEffectMaterial::compare(const QSGMaterial *other) const
246 return this - static_cast<const QQuickShaderEffectMaterial *>(other);
249 void QQuickShaderEffectMaterial::setCullMode(QQuickShaderEffectMaterial::CullMode face)
254 QQuickShaderEffectMaterial::CullMode QQuickShaderEffectMaterial::cullMode() const
259 void QQuickShaderEffectMaterial::setProgramSource(const QQuickShaderEffectProgram &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);
269 void QQuickShaderEffectMaterial::setUniforms(const QVector<QPair<QByteArray, QVariant> > &uniformValues)
271 m_uniformValues = uniformValues;
274 void QQuickShaderEffectMaterial::setTextureProviders(const QVector<QPair<QByteArray, QSGTextureProvider *> > &textures)
276 m_textures = textures;
279 const QVector<QPair<QByteArray, QSGTextureProvider *> > &QQuickShaderEffectMaterial::textureProviders() const
284 void QQuickShaderEffectMaterial::updateTextures() const
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();
295 QQuickShaderEffectNode::QQuickShaderEffectNode()
297 QSGNode::setFlag(UsePreprocess, true);
298 setMaterial(&m_material);
301 QQuickShaderEffectNode::~QQuickShaderEffectNode()
305 void QQuickShaderEffectNode::markDirtyTexture()
307 markDirty(DirtyMaterial);
310 void QQuickShaderEffectNode::preprocess()
312 Q_ASSERT(material());
313 static_cast<QQuickShaderEffectMaterial *>(material())->updateTextures();