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/qsgshadereffectnode_p.h>
44 #include "qsgshadereffectmesh_p.h"
45 #include <private/qsgtextureprovider_p.h>
46 #include <private/qsgrenderer_p.h>
50 class QSGCustomMaterialShader : public QSGMaterialShader
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;
59 friend class QSGShaderEffectNode;
61 virtual void initialize();
62 virtual const char *vertexShader() const;
63 virtual const char *fragmentShader() const;
65 const QSGShaderEffectMaterialKey m_key;
66 QVector<const char *> m_attributeNames;
67 const QVector<QByteArray> m_attributes;
69 QVector<int> m_uniformLocs;
72 uint m_textureIndicesSet;
75 QSGCustomMaterialShader::QSGCustomMaterialShader(const QSGShaderEffectMaterialKey &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 QSGCustomMaterialShader::deactivate()
87 QSGMaterialShader::deactivate();
88 glDisable(GL_CULL_FACE);
91 void QSGCustomMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
93 Q_ASSERT(newEffect != 0);
95 const QSGShaderEffectMaterial *material = static_cast<const QSGShaderEffectMaterial *>(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 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");
120 functions->glActiveTexture(GL_TEXTURE0 + i);
121 provider->texture()->bind();
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 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);
176 case QSGShaderEffectMaterial::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 *QSGCustomMaterialShader::attributeNames() const
192 return m_attributeNames.constData();
195 void QSGCustomMaterialShader::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 *QSGCustomMaterialShader::vertexShader() const
206 return m_key.vertexCode.constData();
209 const char *QSGCustomMaterialShader::fragmentShader() const
211 return m_key.fragmentCode.constData();
215 bool QSGShaderEffectMaterialKey::operator == (const QSGShaderEffectMaterialKey &other) const
217 return vertexCode == other.vertexCode && fragmentCode == other.fragmentCode && className == other.className;
220 uint qHash(const QSGShaderEffectMaterialKey &key)
222 return qHash(qMakePair(qMakePair(key.vertexCode, key.fragmentCode), key.className));
226 QHash<QSGShaderEffectMaterialKey, QSharedPointer<QSGMaterialType> > QSGShaderEffectMaterial::materialMap;
228 QSGShaderEffectMaterial::QSGShaderEffectMaterial()
229 : m_cullMode(NoCulling)
231 setFlag(Blending, true);
234 QSGMaterialType *QSGShaderEffectMaterial::type() const
236 return m_type.data();
239 QSGMaterialShader *QSGShaderEffectMaterial::createShader() const
241 return new QSGCustomMaterialShader(m_source, m_source.attributeNames);
244 int QSGShaderEffectMaterial::compare(const QSGMaterial *other) const
246 return this - static_cast<const QSGShaderEffectMaterial *>(other);
249 void QSGShaderEffectMaterial::setCullMode(QSGShaderEffectMaterial::CullMode face)
254 QSGShaderEffectMaterial::CullMode QSGShaderEffectMaterial::cullMode() const
259 void QSGShaderEffectMaterial::setProgramSource(const QSGShaderEffectProgram &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 QSGShaderEffectMaterial::setUniforms(const QVector<QPair<QByteArray, QVariant> > &uniformValues)
271 m_uniformValues = uniformValues;
274 void QSGShaderEffectMaterial::setTextureProviders(const QVector<QPair<QByteArray, QPointer<QSGItem> > > &textures)
276 m_textures = textures;
279 const QVector<QPair<QByteArray, QPointer<QSGItem> > > &QSGShaderEffectMaterial::textureProviders() const
284 void QSGShaderEffectMaterial::updateTextures() const
286 for (int i = 0; i < m_textures.size(); ++i) {
287 QSGItem *item = m_textures.at(i).second;
289 QSGTextureProvider *provider = QSGTextureProvider::from(item);
291 QSGTexture *texture = provider->texture();
293 qWarning("QSGShaderEffectMaterial: no texture from %s [%s]",
294 qPrintable(item->objectName()),
295 item->metaObject()->className());
297 if (QSGDynamicTexture *t = qobject_cast<QSGDynamicTexture *>(provider->texture())) {
306 QSGShaderEffectNode::QSGShaderEffectNode()
308 QSGNode::setFlag(UsePreprocess, true);
311 QSGShaderEffectNode::~QSGShaderEffectNode()
315 void QSGShaderEffectNode::markDirtyTexture()
317 markDirty(DirtyMaterial);
320 void QSGShaderEffectNode::preprocess()
322 Q_ASSERT(material());
323 static_cast<QSGShaderEffectMaterial *>(material())->updateTextures();