diff options
author | Laszlo Agocs <laszlo.agocs@qt.io> | 2016-06-27 16:10:04 +0200 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@qt.io> | 2016-06-29 11:59:46 +0000 |
commit | 12bff27aa6f1caac9bfacf365c542cf5da6e3148 (patch) | |
tree | 32a0ec27aabb1b73c3fd62f7dcec768bc24b4eb9 /src/quick/items/qquickopenglshadereffect.cpp | |
parent | a174ed2a996bf6311e849c8d8f31bbe307c6838b (diff) |
Add support for reading GLSL from files with ShaderEffect
An enabler for using file selectors instead of writing GraphicsInfo-based
conditions in the vertexShader and fragmentShader properties.
[ChangeLog][QtQuick] ShaderEffect with OpenGL now supports reading GLSL
shader sources from local files and from the resource system. Whenever a
fragmentShader or vertexShader property is a valid such URL, the value is
treated as a file specification instead of actual source code.
Change-Id: I9df2a397adc8f82f1eb6b83d27e9e3d9d1a02f46
Reviewed-by: Andy Nichols <andy.nichols@qt.io>
Diffstat (limited to 'src/quick/items/qquickopenglshadereffect.cpp')
-rw-r--r-- | src/quick/items/qquickopenglshadereffect.cpp | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/quick/items/qquickopenglshadereffect.cpp b/src/quick/items/qquickopenglshadereffect.cpp index 312721ed04..1e66971fdb 100644 --- a/src/quick/items/qquickopenglshadereffect.cpp +++ b/src/quick/items/qquickopenglshadereffect.cpp @@ -51,6 +51,7 @@ #include "qquickshadereffectsource_p.h" #include "qquickshadereffectmesh_p.h" +#include <QtQml/qqmlfile.h> #include <QtCore/qsignalmapper.h> QT_BEGIN_NAMESPACE @@ -321,6 +322,20 @@ void QQuickOpenGLShaderEffectCommon::updateShader(QQuickItem *item, Key::ShaderT if (shaderType == Key::VertexShader) attributes.clear(); + // A qrc or file URL means the shader source is to be read from the specified file. + QUrl srcUrl(QString::fromUtf8(source.sourceCode[shaderType])); + if (!srcUrl.scheme().compare(QLatin1String("qrc"), Qt::CaseInsensitive) || srcUrl.isLocalFile()) { + const QString fn = QQmlFile::urlToLocalFileOrQrc(srcUrl); + QFile f(fn); + if (f.open(QIODevice::ReadOnly | QIODevice::Text)) { + source.sourceCode[shaderType] = f.readAll(); + f.close(); + } else { + qWarning("ShaderEffect: Failed to read %s", qPrintable(fn)); + source.sourceCode[shaderType] = QByteArray(); + } + } + const QByteArray &code = source.sourceCode[shaderType]; if (code.isEmpty()) { // Optimize for default code. |