aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/coreapi
diff options
context:
space:
mode:
authorSean Harmer <sean.harmer@kdab.com>2013-10-26 18:48:56 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-10-31 12:54:28 +0100
commit426f6aa672b94d8324321bc6c6d4f1a358eebedc (patch)
tree8af3e5b0aa2fdcdb7655672669f5ae277151cc3d /src/quick/scenegraph/coreapi
parent34c85bb56c92316a6ce1c79d25f9653fec14791c (diff)
Refactor shaders into seprate GLSL source files
The default implementation of QSGShaderMaterial::vertexShader() and fragmentShader() now loads the GLSL source from a list of source files that can be specified via the setShaderSourceFile() or setShaderSourceFiles() functions. Multiple shader source files for each shader stage are supported. Each source file will be read in the order specified and concatenated together before being compiled. The other places where Qt Quick 2 loads shader source code have been adapted to use the new QSGShaderSourceBuilder, which is also used internally by QSGMaterial. This puts Qt Quick 2 into a better state ready to support OpenGL core profile and to load different shaders based upon OpenGL version, profile, GPU vendor, platform, etc. Change-Id: I1a66213c2ce788413168eb48c7bc5317e61988a2 Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
Diffstat (limited to 'src/quick/scenegraph/coreapi')
-rw-r--r--src/quick/scenegraph/coreapi/qsgbatchrenderer.cpp23
-rw-r--r--src/quick/scenegraph/coreapi/qsgmaterial.cpp61
-rw-r--r--src/quick/scenegraph/coreapi/qsgmaterial.h15
-rw-r--r--src/quick/scenegraph/coreapi/qsgmaterialshader_p.h61
-rw-r--r--src/quick/scenegraph/coreapi/qsgrenderer.cpp15
5 files changed, 142 insertions, 33 deletions
diff --git a/src/quick/scenegraph/coreapi/qsgbatchrenderer.cpp b/src/quick/scenegraph/coreapi/qsgbatchrenderer.cpp
index 7a62dccc0f..e7c611029d 100644
--- a/src/quick/scenegraph/coreapi/qsgbatchrenderer.cpp
+++ b/src/quick/scenegraph/coreapi/qsgbatchrenderer.cpp
@@ -40,6 +40,7 @@
****************************************************************************/
#include "qsgbatchrenderer_p.h"
+#include <private/qsgshadersourcebuilder_p.h>
#include <QtCore/QElapsedTimer>
@@ -2322,23 +2323,11 @@ void Renderer::renderRenderNode(Batch *batch)
if (!m_shaderManager->blitProgram) {
m_shaderManager->blitProgram = new QOpenGLShaderProgram();
- const char *vs =
- "attribute highp vec4 av; "
- "attribute highp vec2 at; "
- "varying highp vec2 t; "
- "void main() { "
- " gl_Position = av; "
- " t = at; "
- "} ";
- const char *fs =
- "uniform lowp sampler2D tex; "
- "varying highp vec2 t; "
- "void main() { "
- " gl_FragColor = texture2D(tex, t); "
- "} ";
-
- m_shaderManager->blitProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vs);
- m_shaderManager->blitProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, fs);
+
+ QSGShaderSourceBuilder::initializeProgramFromFiles(
+ m_shaderManager->blitProgram,
+ QStringLiteral(":/scenegraph/shaders/rendernode.vert"),
+ QStringLiteral(":/scenegraph/shaders/rendernode.frag"));
m_shaderManager->blitProgram->bindAttributeLocation("av", 0);
m_shaderManager->blitProgram->bindAttributeLocation("at", 1);
m_shaderManager->blitProgram->link();
diff --git a/src/quick/scenegraph/coreapi/qsgmaterial.cpp b/src/quick/scenegraph/coreapi/qsgmaterial.cpp
index d186823958..686d1438b4 100644
--- a/src/quick/scenegraph/coreapi/qsgmaterial.cpp
+++ b/src/quick/scenegraph/coreapi/qsgmaterial.cpp
@@ -41,9 +41,21 @@
#include "qsgmaterial.h"
#include "qsgrenderer_p.h"
+#include "qsgmaterialshader_p.h"
+#include <private/qsgshadersourcebuilder_p.h>
QT_BEGIN_NAMESPACE
+const char *QSGMaterialShaderPrivate::loadShaderSource(QOpenGLShader::ShaderType type) const
+{
+ QStringList files = m_sourceFiles[type];
+ QSGShaderSourceBuilder builder;
+ Q_FOREACH (const QString &file, files)
+ builder.appendSourceFile(file);
+ m_sources[type] = builder.source();
+ return m_sources[type].constData();
+}
+
#ifndef QT_NO_DEBUG
static bool qsg_leak_check = !qgetenv("QML_LEAK_CHECK").isEmpty();
#endif
@@ -165,14 +177,21 @@ static bool qsg_leak_check = !qgetenv("QML_LEAK_CHECK").isEmpty();
Creates a new QSGMaterialShader.
*/
QSGMaterialShader::QSGMaterialShader()
+ : d_ptr(new QSGMaterialShaderPrivate)
+{
+}
+
+QSGMaterialShader::QSGMaterialShader(QSGMaterialShaderPrivate &dd)
+ : d_ptr(&dd)
{
- Q_UNUSED(m_reserved);
}
/*!
- \fn QSGMaterialShader::~QSGMaterialShader()
\internal
*/
+QSGMaterialShader::~QSGMaterialShader()
+{
+}
/*!
\fn char const *const *QSGMaterialShader::attributeNames() const
@@ -194,6 +213,11 @@ QSGMaterialShader::QSGMaterialShader()
The contents returned from this function should never change.
*/
+const char *QSGMaterialShader::vertexShader() const
+{
+ Q_D(const QSGMaterialShader);
+ return d->loadShaderSource(QOpenGLShader::Vertex);
+}
/*!
@@ -204,6 +228,11 @@ QSGMaterialShader::QSGMaterialShader()
The contents returned from this function should never change.
*/
+const char *QSGMaterialShader::fragmentShader() const
+{
+ Q_D(const QSGMaterialShader);
+ return d->loadShaderSource(QOpenGLShader::Fragment);
+}
/*!
@@ -274,7 +303,35 @@ void QSGMaterialShader::updateState(const RenderState & /* state */, QSGMaterial
{
}
+/*!
+ Sets the GLSL source file for the shader stage \a type to \a sourceFile. The
+ default implementation of the vertexShader() and fragmentShader() functions
+ will load the source files set by this function.
+
+ This function is useful when you have a single source file for a given shader
+ stage. If your shader consists of multiple source files then use
+ setShaderSourceFiles()
+
+ \sa setShaderSourceFiles(), vertexShader(), fragmentShader()
+ */
+void QSGMaterialShader::setShaderSourceFile(QOpenGLShader::ShaderType type, const QString &sourceFile)
+{
+ Q_D(QSGMaterialShader);
+ d->m_sourceFiles[type] = (QStringList() << sourceFile);
+}
+
+/*!
+ Sets the GLSL source files for the shader stage \a type to \a sourceFiles. The
+ default implementation of the vertexShader() and fragmentShader() functions
+ will load the source files set by this function in the order given.
+ \sa setShaderSourceFile(), vertexShader(), fragmentShader()
+ */
+void QSGMaterialShader::setShaderSourceFiles(QOpenGLShader::ShaderType type, const QStringList &sourceFiles)
+{
+ Q_D(QSGMaterialShader);
+ d->m_sourceFiles[type] = sourceFiles;
+}
/*!
This function is called when the shader is initialized to compile the
diff --git a/src/quick/scenegraph/coreapi/qsgmaterial.h b/src/quick/scenegraph/coreapi/qsgmaterial.h
index f3ef62d3e5..bfe570ca02 100644
--- a/src/quick/scenegraph/coreapi/qsgmaterial.h
+++ b/src/quick/scenegraph/coreapi/qsgmaterial.h
@@ -48,6 +48,7 @@
QT_BEGIN_NAMESPACE
class QSGMaterial;
+class QSGMaterialShaderPrivate;
namespace QSGBatchRenderer {
class ShaderManager;
@@ -88,7 +89,7 @@ public:
};
QSGMaterialShader();
- virtual ~QSGMaterialShader() {};
+ virtual ~QSGMaterialShader();
virtual void activate();
virtual void deactivate();
@@ -99,18 +100,24 @@ public:
inline QOpenGLShaderProgram *program() { return &m_program; }
protected:
+ Q_DECLARE_PRIVATE(QSGMaterialShader)
+ QSGMaterialShader(QSGMaterialShaderPrivate &dd);
+
friend class QSGContext;
friend class QSGBatchRenderer::ShaderManager;
+ void setShaderSourceFile(QOpenGLShader::ShaderType type, const QString &sourceFile);
+ void setShaderSourceFiles(QOpenGLShader::ShaderType type, const QStringList &sourceFiles);
+
virtual void compile();
virtual void initialize() { }
- virtual const char *vertexShader() const = 0;
- virtual const char *fragmentShader() const = 0;
+ virtual const char *vertexShader() const;
+ virtual const char *fragmentShader() const;
private:
QOpenGLShaderProgram m_program;
- void *m_reserved;
+ QScopedPointer<QSGMaterialShaderPrivate> d_ptr;
};
struct QSGMaterialType { };
diff --git a/src/quick/scenegraph/coreapi/qsgmaterialshader_p.h b/src/quick/scenegraph/coreapi/qsgmaterialshader_p.h
new file mode 100644
index 0000000000..fc8a35c41d
--- /dev/null
+++ b/src/quick/scenegraph/coreapi/qsgmaterialshader_p.h
@@ -0,0 +1,61 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtQuick module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QSGMATERIALSHADER_P_H
+#define QSGMATERIALSHADER_P_H
+
+#include <private/qtquickglobal_p.h>
+#include <QOpenGLShader>
+
+QT_BEGIN_NAMESPACE
+
+class Q_QUICK_PRIVATE_EXPORT QSGMaterialShaderPrivate
+{
+public:
+ const char *loadShaderSource(QOpenGLShader::ShaderType type) const;
+
+ QHash<QOpenGLShader::ShaderType, QStringList> m_sourceFiles;
+ mutable QHash<QOpenGLShader::ShaderType, QByteArray> m_sources;
+};
+
+QT_END_NAMESPACE
+
+#endif // QSGMATERIALSHADER_P_H
diff --git a/src/quick/scenegraph/coreapi/qsgrenderer.cpp b/src/quick/scenegraph/coreapi/qsgrenderer.cpp
index 7d982cee36..3c9c353bd8 100644
--- a/src/quick/scenegraph/coreapi/qsgrenderer.cpp
+++ b/src/quick/scenegraph/coreapi/qsgrenderer.cpp
@@ -46,6 +46,7 @@
#include "qsggeometry_p.h"
#include <private/qsgadaptationlayer_p.h>
+#include <private/qsgshadersourcebuilder_p.h>
#include <QOpenGLShaderProgram>
#include <qopenglframebufferobject.h>
@@ -481,16 +482,10 @@ QSGRenderer::ClipType QSGRenderer::updateStencilClip(const QSGClipNode *clip)
} else {
if (!(clipType & StencilClip)) {
if (!m_clip_program.isLinked()) {
- m_clip_program.addShaderFromSourceCode(QOpenGLShader::Vertex,
- "attribute highp vec4 vCoord; \n"
- "uniform highp mat4 matrix; \n"
- "void main() { \n"
- " gl_Position = matrix * vCoord; \n"
- "}");
- m_clip_program.addShaderFromSourceCode(QOpenGLShader::Fragment,
- "void main() { \n"
- " gl_FragColor = vec4(0.81, 0.83, 0.12, 1.0); \n" // Trolltech green ftw!
- "}");
+ QSGShaderSourceBuilder::initializeProgramFromFiles(
+ &m_clip_program,
+ QStringLiteral(":/scenegraph/shaders/stencilclip.vert"),
+ QStringLiteral(":/scenegraph/shaders/stencilclip.frag"));
m_clip_program.bindAttributeLocation("vCoord", 0);
m_clip_program.link();
m_clip_matrix_id = m_clip_program.uniformLocation("matrix");