1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include <private/qquickshadereffectnode_p.h>
44 #include "qquickshadereffectmesh_p.h"
45 #include "qquickshadereffect_p.h"
46 #include <QtQuick/qsgtextureprovider.h>
47 #include <QtQuick/private/qsgrenderer_p.h>
51 class QQuickCustomMaterialShader : public QSGMaterialShader
54 QQuickCustomMaterialShader(const QQuickShaderEffectMaterialKey &key, const QVector<QByteArray> &attributes);
55 virtual void deactivate();
56 virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
57 virtual char const *const *attributeNames() const;
60 friend class QQuickShaderEffectNode;
62 virtual void compile();
63 virtual const char *vertexShader() const;
64 virtual const char *fragmentShader() const;
66 const QQuickShaderEffectMaterialKey m_key;
67 QVector<const char *> m_attributeNames;
68 const QVector<QByteArray> m_attributes;
72 QVector<int> m_uniformLocs[QQuickShaderEffectMaterialKey::ShaderTypeCount];
73 uint m_initialized : 1;
76 QQuickCustomMaterialShader::QQuickCustomMaterialShader(const QQuickShaderEffectMaterialKey &key, const QVector<QByteArray> &attributes)
78 , m_attributes(attributes)
80 , m_initialized(false)
82 for (int i = 0; i < attributes.count(); ++i)
83 m_attributeNames.append(attributes.at(i).constData());
84 m_attributeNames.append(0);
87 void QQuickCustomMaterialShader::deactivate()
89 QSGMaterialShader::deactivate();
90 glDisable(GL_CULL_FACE);
93 void QQuickCustomMaterialShader::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
95 typedef QQuickShaderEffectMaterial::UniformData UniformData;
97 Q_ASSERT(newEffect != 0);
99 QQuickShaderEffectMaterial *material = static_cast<QQuickShaderEffectMaterial *>(newEffect);
100 if (!material->m_emittedLogChanged && material->m_node) {
101 material->m_emittedLogChanged = true;
102 emit material->m_node->logAndStatusChanged(m_log, m_compiled ? QQuickShaderEffect::Compiled
103 : QQuickShaderEffect::Error);
106 int textureProviderIndex = 0;
107 if (!m_initialized) {
108 for (int shaderType = 0; shaderType < QQuickShaderEffectMaterialKey::ShaderTypeCount; ++shaderType) {
109 Q_ASSERT(m_uniformLocs[shaderType].isEmpty());
110 m_uniformLocs[shaderType].reserve(material->uniforms[shaderType].size());
111 for (int i = 0; i < material->uniforms[shaderType].size(); ++i) {
112 const UniformData &d = material->uniforms[shaderType].at(i);
113 QByteArray name = d.name;
114 if (d.specialType == UniformData::Sampler) {
115 program()->setUniformValue(d.name.constData(), textureProviderIndex++);
116 // We don't need to store the sampler uniform locations, since their values
117 // only need to be set once. Look for the "qt_SubRect_" uniforms instead.
118 // These locations are used when binding the textures later.
119 name = "qt_SubRect_" + name;
121 m_uniformLocs[shaderType].append(program()->uniformLocation(name.constData()));
124 m_initialized = true;
125 textureProviderIndex = 0;
128 QOpenGLFunctions *functions = state.context()->functions();
129 for (int shaderType = 0; shaderType < QQuickShaderEffectMaterialKey::ShaderTypeCount; ++shaderType) {
130 for (int i = 0; i < material->uniforms[shaderType].size(); ++i) {
131 const UniformData &d = material->uniforms[shaderType].at(i);
132 int loc = m_uniformLocs[shaderType].at(i);
133 if (d.specialType == UniformData::Sampler) {
134 int idx = textureProviderIndex++;
135 functions->glActiveTexture(GL_TEXTURE0 + idx);
136 if (QSGTextureProvider *provider = material->textureProviders.at(idx)) {
137 if (QSGTexture *texture = provider->texture()) {
139 QRectF r = texture->normalizedTextureSubRect();
140 program()->setUniformValue(loc, r.x(), r.y(), r.width(), r.height());
141 } else if (texture->isAtlasTexture()) {
142 texture = texture->removedFromAtlas();
148 qWarning("ShaderEffect: source or provider missing when binding textures");
149 glBindTexture(GL_TEXTURE_2D, 0);
150 } else if (d.specialType == UniformData::Opacity) {
151 program()->setUniformValue(loc, state.opacity());
152 } else if (d.specialType == UniformData::Matrix) {
153 if (state.isMatrixDirty())
154 program()->setUniformValue(loc, state.combinedMatrix());
155 } else if (d.specialType == UniformData::None) {
156 switch (d.value.type()) {
157 case QMetaType::QColor:
158 program()->setUniformValue(loc, qt_premultiply_color(qvariant_cast<QColor>(d.value)));
160 case QMetaType::Float:
161 program()->setUniformValue(loc, qvariant_cast<float>(d.value));
163 case QMetaType::Double:
164 program()->setUniformValue(loc, (float) qvariant_cast<double>(d.value));
166 case QMetaType::QTransform:
167 program()->setUniformValue(loc, qvariant_cast<QTransform>(d.value));
170 program()->setUniformValue(loc, d.value.toInt());
172 case QMetaType::Bool:
173 program()->setUniformValue(loc, GLint(d.value.toBool()));
175 case QMetaType::QSize:
176 case QMetaType::QSizeF:
177 program()->setUniformValue(loc, d.value.toSizeF());
179 case QMetaType::QPoint:
180 case QMetaType::QPointF:
181 program()->setUniformValue(loc, d.value.toPointF());
183 case QMetaType::QRect:
184 case QMetaType::QRectF:
186 QRectF r = d.value.toRectF();
187 program()->setUniformValue(loc, r.x(), r.y(), r.width(), r.height());
190 case QMetaType::QVector3D:
191 program()->setUniformValue(loc, qvariant_cast<QVector3D>(d.value));
193 case QMetaType::QVector4D:
194 program()->setUniformValue(loc, qvariant_cast<QVector4D>(d.value));
202 functions->glActiveTexture(GL_TEXTURE0);
204 const QQuickShaderEffectMaterial *oldMaterial = static_cast<const QQuickShaderEffectMaterial *>(oldEffect);
205 if (oldEffect == 0 || material->cullMode != oldMaterial->cullMode) {
206 switch (material->cullMode) {
207 case QQuickShaderEffectMaterial::FrontFaceCulling:
208 glEnable(GL_CULL_FACE);
209 glCullFace(GL_FRONT);
211 case QQuickShaderEffectMaterial::BackFaceCulling:
212 glEnable(GL_CULL_FACE);
216 glDisable(GL_CULL_FACE);
222 char const *const *QQuickCustomMaterialShader::attributeNames() const
224 return m_attributeNames.constData();
227 void QQuickCustomMaterialShader::compile()
229 Q_ASSERT_X(!program()->isLinked(), "QQuickCustomMaterialShader::compile()", "Compile called multiple times!");
233 if (!program()->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShader())) {
234 m_log += QLatin1String("*** Vertex shader ***\n");
235 m_log += program()->log();
238 if (!program()->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShader())) {
239 m_log += QLatin1String("*** Fragment shader ***\n");
240 m_log += program()->log();
244 char const *const *attr = attributeNames();
246 int maxVertexAttribs = 0;
247 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs);
249 while (attrCount < maxVertexAttribs && attr[attrCount])
251 if (attr[attrCount]) {
252 qWarning("List of attribute names is too long.\n"
253 "Maximum number of attributes on this hardware is %i.\n"
254 "Vertex shader:\n%s\n"
255 "Fragment shader:\n%s\n",
256 maxVertexAttribs, vertexShader(), fragmentShader());
262 for (int i = 0; i < attrCount; ++i) {
264 for (int i = 0; attr[i]; ++i) {
267 program()->bindAttributeLocation(attr[i], i);
269 m_compiled = program()->link();
270 m_log += program()->log();
273 static const char *fallbackVertexShader =
274 "uniform highp mat4 qt_Matrix;"
275 "attribute highp vec4 v;"
276 "void main() { gl_Position = qt_Matrix * v; }";
277 static const char *fallbackFragmentShader =
278 "void main() { gl_FragColor = vec4(1., 0., 1., 1.); }";
281 qWarning("QQuickCustomMaterialShader: Shader compilation failed:");
282 qWarning() << program()->log();
284 program()->removeAllShaders();
285 program()->addShaderFromSourceCode(QOpenGLShader::Vertex, fallbackVertexShader);
286 program()->addShaderFromSourceCode(QOpenGLShader::Fragment, fallbackFragmentShader);
288 for (int i = 0; i < attrCount; ++i) {
290 for (int i = 0; attr[i]; ++i) {
292 if (qstrcmp(attr[i], qtPositionAttributeName()) == 0)
293 program()->bindAttributeLocation("v", i);
299 const char *QQuickCustomMaterialShader::vertexShader() const
301 return m_key.sourceCode[QQuickShaderEffectMaterialKey::VertexShader].constData();
304 const char *QQuickCustomMaterialShader::fragmentShader() const
306 return m_key.sourceCode[QQuickShaderEffectMaterialKey::FragmentShader].constData();
310 bool QQuickShaderEffectMaterialKey::operator == (const QQuickShaderEffectMaterialKey &other) const
312 if (className != other.className)
314 for (int shaderType = 0; shaderType < ShaderTypeCount; ++shaderType) {
315 if (sourceCode[shaderType] != other.sourceCode[shaderType])
321 uint qHash(const QQuickShaderEffectMaterialKey &key)
323 uint hash = qHash((void *)key.className);
324 typedef QQuickShaderEffectMaterialKey Key;
325 for (int shaderType = 0; shaderType < Key::ShaderTypeCount; ++shaderType)
326 hash = hash * 31337 + qHash(key.sourceCode[shaderType]);
331 QHash<QQuickShaderEffectMaterialKey, QSharedPointer<QSGMaterialType> > QQuickShaderEffectMaterial::materialMap;
333 QQuickShaderEffectMaterial::QQuickShaderEffectMaterial(QQuickShaderEffectNode *node)
334 : cullMode(NoCulling)
336 , m_emittedLogChanged(false)
338 setFlag(Blending | RequiresFullMatrix, true);
341 QSGMaterialType *QQuickShaderEffectMaterial::type() const
343 return m_type.data();
346 QSGMaterialShader *QQuickShaderEffectMaterial::createShader() const
348 return new QQuickCustomMaterialShader(m_source, attributes);
351 int QQuickShaderEffectMaterial::compare(const QSGMaterial *other) const
353 return this - static_cast<const QQuickShaderEffectMaterial *>(other);
356 void QQuickShaderEffectMaterial::setProgramSource(const QQuickShaderEffectMaterialKey &source)
359 m_emittedLogChanged = false;
360 m_type = materialMap.value(m_source);
361 if (m_type.isNull()) {
362 m_type = QSharedPointer<QSGMaterialType>(new QSGMaterialType);
363 materialMap.insert(m_source, m_type);
367 void QQuickShaderEffectMaterial::updateTextures() const
369 for (int i = 0; i < textureProviders.size(); ++i) {
370 if (QSGTextureProvider *provider = textureProviders.at(i)) {
371 if (QSGDynamicTexture *texture = qobject_cast<QSGDynamicTexture *>(provider->texture()))
372 texture->updateTexture();
377 void QQuickShaderEffectMaterial::invalidateTextureProvider(QSGTextureProvider *provider)
379 for (int i = 0; i < textureProviders.size(); ++i) {
380 if (provider == textureProviders.at(i))
381 textureProviders[i] = 0;
386 QQuickShaderEffectNode::QQuickShaderEffectNode()
388 QSGNode::setFlag(UsePreprocess, true);
390 #ifdef QML_RUNTIME_TESTING
391 description = QLatin1String("shadereffect");
395 QQuickShaderEffectNode::~QQuickShaderEffectNode()
399 void QQuickShaderEffectNode::markDirtyTexture()
401 markDirty(DirtyMaterial);
404 void QQuickShaderEffectNode::textureProviderDestroyed(QObject *object)
406 Q_ASSERT(material());
407 static_cast<QQuickShaderEffectMaterial *>(material())->invalidateTextureProvider(static_cast<QSGTextureProvider *>(object));
410 void QQuickShaderEffectNode::preprocess()
412 Q_ASSERT(material());
413 static_cast<QQuickShaderEffectMaterial *>(material())->updateTextures();