diff options
author | Laszlo Agocs <laszlo.agocs@theqtcompany.com> | 2015-11-27 13:21:57 +0100 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@theqtcompany.com> | 2015-11-27 15:15:32 +0000 |
commit | 7e8b79eab01d2e9f987ad4120f698d56342e6247 (patch) | |
tree | 75b9bd859151dfa26bc3f4060a121dc9b976b7c0 /src/render/materialsystem/qshaderprogram.cpp | |
parent | 303101facc6f2f88b64c578d047de46f2508e822 (diff) |
Add support for include in shader sources
Implement our own custom include pragma. This is essential for
introducing features that involve common snippets of code,
for example lighting.
Change-Id: Ia94d2492b399b5fb879ff2729654962677334a29
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/render/materialsystem/qshaderprogram.cpp')
-rw-r--r-- | src/render/materialsystem/qshaderprogram.cpp | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/src/render/materialsystem/qshaderprogram.cpp b/src/render/materialsystem/qshaderprogram.cpp index da0de2422..6a2dc310d 100644 --- a/src/render/materialsystem/qshaderprogram.cpp +++ b/src/render/materialsystem/qshaderprogram.cpp @@ -40,6 +40,7 @@ #include <Qt3DCore/private/qurlhelper_p.h> #include <QDebug> #include <QFile> +#include <QFileInfo> #include <QUrl> QT_BEGIN_NAMESPACE @@ -227,17 +228,35 @@ QByteArray QShaderProgram::shaderCode(ShaderType type) const } } +static QByteArray deincludify(const QString &filePath) +{ + QFile f(filePath); + if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) { + qWarning() << "Could not read shader source file:" << f.fileName(); + return QByteArray(); + } + + QByteArray contents = f.readAll(); + QByteArrayList lines = contents.split('\n'); + const QByteArray includeDirective = QByteArrayLiteral("#pragma include"); + for (int i = 0; i < lines.count(); ++i) { + if (lines[i].startsWith(includeDirective)) { + QByteArray includeFileName = lines[i].mid(includeDirective.count() + 1); + lines.removeAt(i); + QByteArray includedContents = deincludify(QFileInfo(filePath).absolutePath() + QChar('/') + includeFileName); + lines.insert(i, includedContents); + QString lineDirective = QString(QStringLiteral("#line %1")).arg(i + 2); + lines.insert(i + 1, lineDirective.toUtf8()); + } + } + + return lines.join('\n'); +} + QByteArray QShaderProgram::loadSource(const QUrl &sourceUrl) { // TO DO: Handle remote path - QString filePath = Qt3DCore::QUrlHelper::urlToLocalFileOrQrc(sourceUrl); - - QFile f(filePath); - if (!f.exists()) - qWarning() << "Couldn't read shader source file:" << sourceUrl; - else - f.open(QIODevice::ReadOnly | QIODevice::Text); - return f.readAll(); + return deincludify(Qt3DCore::QUrlHelper::urlToLocalFileOrQrc(sourceUrl)); } } // of namespace Qt3DRender |