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 ** 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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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 glDisable(GL_CULL_FACE);
90 void QSGCustomMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
92 Q_ASSERT(newEffect != 0);
94 const QSGShaderEffectMaterial *material = static_cast<const QSGShaderEffectMaterial *>(newEffect);
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;
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()));
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");
119 functions->glActiveTexture(GL_TEXTURE0 + i);
120 provider->texture()->bind();
123 if (material->m_source.respectsOpacity)
124 m_program.setUniformValue(m_opacityLoc, state.opacity());
126 for (int i = 0; i < material->m_uniformValues.count(); ++i) {
127 const QVariant &v = material->m_uniformValues.at(i).second;
130 case QVariant::Color:
131 m_program.setUniformValue(m_uniformLocs.at(i), qt_premultiply_color(qvariant_cast<QColor>(v)));
133 case QVariant::Double:
134 m_program.setUniformValue(m_uniformLocs.at(i), (float) qvariant_cast<double>(v));
136 case QVariant::Transform:
137 m_program.setUniformValue(m_uniformLocs.at(i), qvariant_cast<QTransform>(v));
140 m_program.setUniformValue(m_uniformLocs.at(i), v.toInt());
143 m_program.setUniformValue(m_uniformLocs.at(i), GLint(v.toBool()));
146 case QVariant::SizeF:
147 m_program.setUniformValue(m_uniformLocs.at(i), v.toSizeF());
149 case QVariant::Point:
150 case QVariant::PointF:
151 m_program.setUniformValue(m_uniformLocs.at(i), v.toPointF());
154 case QVariant::RectF:
156 QRectF r = v.toRectF();
157 m_program.setUniformValue(m_uniformLocs.at(i), r.x(), r.y(), r.width(), r.height());
160 case QVariant::Vector3D:
161 m_program.setUniformValue(m_uniformLocs.at(i), qvariant_cast<QVector3D>(v));
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);
175 case QSGShaderEffectMaterial::BackFaceCulling:
176 glEnable(GL_CULL_FACE);
180 glDisable(GL_CULL_FACE);
185 if ((state.isMatrixDirty()) && material->m_source.respectsMatrix)
186 m_program.setUniformValue(m_matrixLoc, state.combinedMatrix());
189 char const *const *QSGCustomMaterialShader::attributeNames() const
191 return m_attributeNames.constData();
194 void QSGCustomMaterialShader::initialize()
196 m_opacityLoc = m_program.uniformLocation("qt_Opacity");
197 m_matrixLoc = m_program.uniformLocation("qt_ModelViewProjectionMatrix");
200 const char *QSGCustomMaterialShader::vertexShader() const
202 return m_key.vertexCode.constData();
205 const char *QSGCustomMaterialShader::fragmentShader() const
207 return m_key.fragmentCode.constData();
211 bool QSGShaderEffectMaterialKey::operator == (const QSGShaderEffectMaterialKey &other) const
213 return vertexCode == other.vertexCode && fragmentCode == other.fragmentCode && className == other.className;
216 uint qHash(const QSGShaderEffectMaterialKey &key)
218 return qHash(qMakePair(qMakePair(key.vertexCode, key.fragmentCode), key.className));
222 QHash<QSGShaderEffectMaterialKey, QSharedPointer<QSGMaterialType> > QSGShaderEffectMaterial::materialMap;
224 QSGShaderEffectMaterial::QSGShaderEffectMaterial()
225 : m_cullMode(NoCulling)
227 setFlag(Blending, true);
230 QSGMaterialType *QSGShaderEffectMaterial::type() const
232 return m_type.data();
235 QSGMaterialShader *QSGShaderEffectMaterial::createShader() const
237 return new QSGCustomMaterialShader(m_source, m_source.attributeNames);
240 int QSGShaderEffectMaterial::compare(const QSGMaterial *other) const
242 return this - static_cast<const QSGShaderEffectMaterial *>(other);
245 void QSGShaderEffectMaterial::setCullMode(QSGShaderEffectMaterial::CullMode face)
250 QSGShaderEffectMaterial::CullMode QSGShaderEffectMaterial::cullMode() const
255 void QSGShaderEffectMaterial::setProgramSource(const QSGShaderEffectProgram &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);
265 void QSGShaderEffectMaterial::setUniforms(const QVector<QPair<QByteArray, QVariant> > &uniformValues)
267 m_uniformValues = uniformValues;
270 void QSGShaderEffectMaterial::setTextureProviders(const QVector<QPair<QByteArray, QPointer<QSGItem> > > &textures)
272 m_textures = textures;
275 const QVector<QPair<QByteArray, QPointer<QSGItem> > > &QSGShaderEffectMaterial::textureProviders() const
280 void QSGShaderEffectMaterial::updateTextures() const
282 for (int i = 0; i < m_textures.size(); ++i) {
283 QSGItem *item = m_textures.at(i).second;
285 QSGTextureProvider *provider = QSGTextureProvider::from(item);
287 QSGTexture *texture = provider->texture();
289 qWarning("QSGShaderEffectMaterial: no texture from %s [%s]",
290 qPrintable(item->objectName()),
291 item->metaObject()->className());
293 if (QSGDynamicTexture *t = qobject_cast<QSGDynamicTexture *>(provider->texture())) {
302 QSGShaderEffectNode::QSGShaderEffectNode()
304 QSGNode::setFlag(UsePreprocess, true);
307 QSGShaderEffectNode::~QSGShaderEffectNode()
311 void QSGShaderEffectNode::markDirtyTexture()
313 markDirty(DirtyMaterial);
316 void QSGShaderEffectNode::preprocess()
318 Q_ASSERT(material());
319 static_cast<QSGShaderEffectMaterial *>(material())->updateTextures();