summaryrefslogtreecommitdiffstats
path: root/src/render
diff options
context:
space:
mode:
authorPaul Lemire <paul.lemire@kdab.com>2014-10-16 17:44:37 +0200
committerSean Harmer <sean.harmer@kdab.com>2014-10-18 13:50:25 +0200
commitb46d416987a4f62a3bcc6e4038e6b949c5b9af4b (patch)
tree341874d8340051786bb1223de5d63c8f84ba7405 /src/render
parent16749ae5dbab2caa67d816a6778cdfd92e9510ac (diff)
QShaderProgram: remove source file properties, replace by helper method
Only the source code QByteArray properties remain. The user is still able to load a shader from a source file by using the shaderFromSource helper method and assigning what it returns to one of the QByteArray source code properties. Change-Id: I5246498fa680ec74d095d7a45f0b1bce239efc13 Task-number: QTBUG-41536 Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/render')
-rw-r--r--src/render/backend/renderer.cpp5
-rw-r--r--src/render/backend/rendershader.cpp32
-rw-r--r--src/render/backend/rendershader_p.h1
-rw-r--r--src/render/frontend/qshaderprogram.cpp159
-rw-r--r--src/render/frontend/qshaderprogram.h38
-rw-r--r--src/render/io/gltfparser.cpp5
6 files changed, 24 insertions, 216 deletions
diff --git a/src/render/backend/renderer.cpp b/src/render/backend/renderer.cpp
index 05bbf96fc..86e3602b4 100644
--- a/src/render/backend/renderer.cpp
+++ b/src/render/backend/renderer.cpp
@@ -92,6 +92,7 @@
#include <QLibraryInfo>
#include <QPluginLoader>
#include <QDir>
+#include <QUrl>
// For Debug purposes only
#include <QThread>
@@ -158,8 +159,8 @@ void Renderer::buildDefaultTechnique()
m_defaultTechnique->setObjectName(QStringLiteral("default-technique"));
QShaderProgram* defaultShader = new QShaderProgram;
- defaultShader->setVertexShaderSourceFile(QStringLiteral(":/shaders/diffuse.vert"));
- defaultShader->setFragmentShaderSourceFile(QStringLiteral(":/shaders/diffuse.frag"));
+ defaultShader->setVertexShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/diffuse.vert"))));
+ defaultShader->setFragmentShaderCode(QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/diffuse.frag"))));
defaultShader->setObjectName(QStringLiteral("DefaultShader"));
QRenderPass* basicPass = new QRenderPass;
diff --git a/src/render/backend/rendershader.cpp b/src/render/backend/rendershader.cpp
index 6c0073861..acba247a1 100644
--- a/src/render/backend/rendershader.cpp
+++ b/src/render/backend/rendershader.cpp
@@ -61,7 +61,6 @@ RenderShader::RenderShader()
, m_isLoaded(false)
{
m_shaderCode.resize(static_cast<int>(QShaderProgram::Compute) + 1);
- m_shaderSourceFiles.resize(static_cast<int>(QShaderProgram::Compute) + 1);
}
RenderShader::~RenderShader()
@@ -80,7 +79,6 @@ void RenderShader::updateFromPeer(QNode *peer)
for (int i = QShaderProgram::Vertex; i <= QShaderProgram::Compute; ++i) {
m_shaderCode[i].clear();
- m_shaderSourceFiles[i].clear();
}
m_isLoaded = false;
@@ -88,7 +86,6 @@ void RenderShader::updateFromPeer(QNode *peer)
for (int i = QShaderProgram::Vertex; i <= QShaderProgram::Compute; ++i) {
QShaderProgram::ShaderType type = static_cast<const QShaderProgram::ShaderType>(i);
m_shaderCode[i] = shader->shaderCode(type);
- m_shaderSourceFiles[i] = shader->shaderSourceFile(type);
}
}
@@ -126,24 +123,6 @@ void RenderShader::sceneChangeEvent(const QSceneChangePtr &e)
} else if (propertyChange->propertyName() == QByteArrayLiteral("computeSourceCode")) {
m_shaderCode[QShaderProgram::Compute] = propertyValue.toByteArray();
m_isLoaded = false;
- } else if (propertyChange->propertyName() == QByteArrayLiteral("vertexSourceFile")) {
- m_shaderSourceFiles[QShaderProgram::Vertex] = propertyValue.toString();
- m_isLoaded = false;
- } else if (propertyChange->propertyName() == QByteArrayLiteral("fragmentSourceFile")) {
- m_shaderSourceFiles[QShaderProgram::Fragment] = propertyValue.toString();
- m_isLoaded = false;
- } else if (propertyChange->propertyName() == QByteArrayLiteral("tessellationControlSourceFile")) {
- m_shaderSourceFiles[QShaderProgram::TessellationControl] = propertyValue.toString();
- m_isLoaded = false;
- } else if (propertyChange->propertyName() == QByteArrayLiteral("tessellationEvaluationSourceFile")) {
- m_shaderSourceFiles[QShaderProgram::TessellationEvaluation] = propertyValue.toString();
- m_isLoaded = false;
- } else if (propertyChange->propertyName() == QByteArrayLiteral("geometrySourceFile")) {
- m_shaderSourceFiles[QShaderProgram::Geometry] = propertyValue.toString();
- m_isLoaded = false;
- } else if (propertyChange->propertyName() == QByteArrayLiteral("computeSourceFile")) {
- m_shaderSourceFiles[QShaderProgram::Compute] = propertyValue.toString();
- m_isLoaded = false;
}
}
}
@@ -204,17 +183,6 @@ QOpenGLShaderProgram* RenderShader::createProgram(QGraphicsContext *context)
for (int i = QShaderProgram::Vertex; i <= QShaderProgram::Compute; ++i) {
QShaderProgram::ShaderType type = static_cast<const QShaderProgram::ShaderType>(i);
- const QString sourceFile = m_shaderSourceFiles[type];
- if (!sourceFile.isEmpty()) {
- QFile f(sourceFile);
- if (!f.exists()) {
- qWarning() << "Couldn't read shader source file:" << sourceFile;
- } else {
- f.open(QIODevice::ReadOnly | QIODevice::Text);
- m_shaderCode[type] = f.readAll();
- }
- }
-
// Compile shaders
if (!m_shaderCode[type].isEmpty()) {
if (!p->addShaderFromSourceCode(shaderType(type), m_shaderCode[type]))
diff --git a/src/render/backend/rendershader_p.h b/src/render/backend/rendershader_p.h
index fe9332bd9..128ae1473 100644
--- a/src/render/backend/rendershader_p.h
+++ b/src/render/backend/rendershader_p.h
@@ -94,7 +94,6 @@ private:
QHash<QString, int> m_fragOutputs;
QVector<QByteArray> m_shaderCode;
- QVector<QString> m_shaderSourceFiles;
bool m_isLoaded;
diff --git a/src/render/frontend/qshaderprogram.cpp b/src/render/frontend/qshaderprogram.cpp
index 8139e38a8..ccf98472c 100644
--- a/src/render/frontend/qshaderprogram.cpp
+++ b/src/render/frontend/qshaderprogram.cpp
@@ -44,6 +44,7 @@
#include <Qt3DCore/qscenepropertychange.h>
#include <QDebug>
#include <QFile>
+#include <QUrl>
QT_BEGIN_NAMESPACE
@@ -58,12 +59,6 @@ void QShaderProgram::copy(const QNode *ref)
{
QNode::copy(ref);
const QShaderProgram *prog = static_cast<const QShaderProgram*>(ref);
- d_func()->m_vertexSourceFile = prog->d_func()->m_vertexSourceFile;
- d_func()->m_tessControlSourceFile = prog->d_func()->m_tessControlSourceFile;
- d_func()->m_tessEvalSourceFile = prog->d_func()->m_tessEvalSourceFile;
- d_func()->m_geometrySourceFile = prog->d_func()->m_geometrySourceFile;
- d_func()->m_fragmentSourceFile = prog->d_func()->m_fragmentSourceFile;
- d_func()->m_computeSourceFile = prog->d_func()->m_computeSourceFile;
d_func()->m_vertexShaderCode = prog->d_func()->m_vertexShaderCode;
d_func()->m_tessControlShaderCode = prog->d_func()->m_tessControlShaderCode;
@@ -83,143 +78,6 @@ QShaderProgram::QShaderProgram(QShaderProgramPrivate &dd, QNode *parent)
{
}
-void QShaderProgram::setVertexShaderSourceFile(const QString& vertexShaderSourceFile)
-{
- Q_D(QShaderProgram);
- if (vertexShaderSourceFile != d->m_vertexSourceFile) {
- d->m_vertexSourceFile = vertexShaderSourceFile;
- emit vertexShaderSourceFileChanged();
- }
-}
-
-QString QShaderProgram::vertexShaderSourceFile() const
-{
- Q_D(const QShaderProgram);
- return d->m_vertexSourceFile;
-}
-
-void QShaderProgram::setTessellationControlShaderSourceFile(const QString &tessellationControlShaderSourceFile)
-{
- Q_D(QShaderProgram);
- if (tessellationControlShaderSourceFile != d->m_tessControlSourceFile) {
- d->m_tessControlSourceFile = tessellationControlShaderSourceFile;
- emit tessellationControlShaderSourceFileChanged();
- }
-}
-
-QString QShaderProgram::tessellationControlShaderSourceFile() const
-{
- Q_D(const QShaderProgram);
- return d->m_tessControlSourceFile;
-}
-
-void QShaderProgram::setTessellationEvaluationShaderSourceFile(const QString &tessellationEvaluationShaderSourceFile)
-{
- Q_D(QShaderProgram);
- if (tessellationEvaluationShaderSourceFile != d->m_tessEvalSourceFile) {
- d->m_tessEvalSourceFile = tessellationEvaluationShaderSourceFile;
- emit tessellationEvaluationShaderSourceFileChanged();
- }
-}
-
-QString QShaderProgram::tessellationEvaluationShaderSourceFile() const
-{
- Q_D(const QShaderProgram);
- return d->m_tessEvalSourceFile;
-}
-
-void QShaderProgram::setGeometryShaderSourceFile(const QString &geometryShaderSourceFile)
-{
- Q_D(QShaderProgram);
- if (geometryShaderSourceFile != d->m_geometrySourceFile) {
- d->m_geometrySourceFile = geometryShaderSourceFile;
- emit geometryShaderSourceFileChanged();
- }
-}
-
-QString QShaderProgram::geometryShaderSourceFile() const
-{
- Q_D(const QShaderProgram);
- return d->m_geometrySourceFile;
-}
-
-void QShaderProgram::setFragmentShaderSourceFile(const QString& fragmentShaderSourceFile)
-{
- Q_D(QShaderProgram);
- if (fragmentShaderSourceFile != d->m_fragmentSourceFile) {
- d->m_fragmentSourceFile = fragmentShaderSourceFile;
- emit fragmentShaderSourceFileChanged();
- }
-}
-
-QString QShaderProgram::fragmentShaderSourceFile() const
-{
- Q_D(const QShaderProgram);
- return d->m_fragmentSourceFile;
-}
-
-void QShaderProgram::setComputeShaderSourceFile(const QString &computeShaderSourceFile)
-{
- Q_D(QShaderProgram);
- if (computeShaderSourceFile != d->m_computeSourceFile) {
- d->m_computeSourceFile = computeShaderSourceFile;
- emit computeShaderSourceFileChanged();
- }
-}
-
-QString QShaderProgram::computeShaderSourceFile() const
-{
- Q_D(const QShaderProgram);
- return d->m_computeSourceFile;
-}
-
-void QShaderProgram::setShaderSourceFile(ShaderType type, const QString &sourceFile)
-{
- switch (type) {
- case Vertex:
- setVertexShaderSourceFile(sourceFile);
- break;
- case TessellationControl:
- setTessellationControlShaderSourceFile(sourceFile);
- break;
- case TessellationEvaluation:
- setTessellationEvaluationShaderSourceFile(sourceFile);
- break;
- case Geometry:
- setGeometryShaderSourceFile(sourceFile);
- break;
- case Fragment:
- setFragmentShaderSourceFile(sourceFile);
- break;
- case Compute:
- setComputeShaderSourceFile(sourceFile);
- break;
- default:
- Q_UNREACHABLE();
- }
-}
-
-QString QShaderProgram::shaderSourceFile(ShaderType type) const
-{
- Q_D(const QShaderProgram);
- switch (type) {
- case Vertex:
- return d->m_vertexSourceFile;
- case TessellationControl:
- return d->m_tessControlSourceFile;
- case TessellationEvaluation:
- return d->m_tessEvalSourceFile;
- case Geometry:
- return d->m_geometrySourceFile;
- case Fragment:
- return d->m_fragmentSourceFile;
- case Compute:
- return d->m_computeSourceFile;
- default:
- Q_UNREACHABLE();
- }
-}
-
/*!
* Sets the vertexShader from raw data in \a vertexShader.
*/
@@ -365,6 +223,21 @@ QByteArray QShaderProgram::shaderCode(ShaderType type) const
}
}
+QByteArray QShaderProgram::loadSource(const QUrl &sourceUrl)
+{
+ // TO DO: Handle remote path
+ // Expect a file for now
+ // Resources file are a bit tricky to handle, there may be a nicer way to do that.
+ QString filePath = sourceUrl.toString().replace(QStringLiteral("qrc"), QStringLiteral(""));
+
+ QFile f(filePath);
+ if (!f.exists())
+ qWarning() << "Couldn't read shader source file:" << sourceUrl;
+ else
+ f.open(QIODevice::ReadOnly | QIODevice::Text);
+ return f.readAll();
+}
+
} // of namespace Qt3D
QT_END_NAMESPACE
diff --git a/src/render/frontend/qshaderprogram.h b/src/render/frontend/qshaderprogram.h
index 63f0398a0..7de2a89d6 100644
--- a/src/render/frontend/qshaderprogram.h
+++ b/src/render/frontend/qshaderprogram.h
@@ -55,13 +55,6 @@ class QShaderProgramPrivate;
class QT3DRENDERERSHARED_EXPORT QShaderProgram : public QNode
{
Q_OBJECT
-
- Q_PROPERTY(QString vertexShaderSourceFile READ vertexShaderSourceFile WRITE setVertexShaderSourceFile NOTIFY vertexShaderSourceFileChanged)
- Q_PROPERTY(QString tessellationControlShaderSourceFile READ tessellationControlShaderSourceFile WRITE setTessellationControlShaderSourceFile NOTIFY tessellationControlShaderSourceFileChanged)
- Q_PROPERTY(QString tessellationEvaluationShaderSourceFile READ tessellationEvaluationShaderSourceFile WRITE setTessellationEvaluationShaderSourceFile NOTIFY tessellationEvaluationShaderSourceFileChanged)
- Q_PROPERTY(QString geometryShaderSourceFile READ geometryShaderSourceFile WRITE setGeometryShaderSourceFile NOTIFY geometryShaderSourceFileChanged)
- Q_PROPERTY(QString fragmentShaderSourceFile READ fragmentShaderSourceFile WRITE setFragmentShaderSourceFile NOTIFY fragmentShaderSourceFileChanged)
- Q_PROPERTY(QString computeShaderSourceFile READ computeShaderSourceFile WRITE setComputeShaderSourceFile NOTIFY computeShaderSourceFileChanged)
Q_PROPERTY(QByteArray vertexShaderCode READ vertexShaderCode WRITE setVertexShaderCode NOTIFY vertexShaderCodeChanged)
Q_PROPERTY(QByteArray tessellationControlShaderCode READ tessellationControlShaderCode WRITE setTessellationControlShaderCode NOTIFY tessellationControlShaderCodeChanged)
Q_PROPERTY(QByteArray tessellationEvaluationShaderCode READ tessellationEvaluationShaderCode WRITE setTessellationEvaluationShaderCode NOTIFY tessellationEvaluationShaderCodeChanged)
@@ -82,28 +75,6 @@ public:
Compute
};
- // Source from file
- void setVertexShaderSourceFile(const QString &vertexShaderSourceFile);
- QString vertexShaderSourceFile() const;
-
- void setTessellationControlShaderSourceFile(const QString &tessellationControlShaderSourceFile);
- QString tessellationControlShaderSourceFile() const;
-
- void setTessellationEvaluationShaderSourceFile(const QString &tessellationEvaluationShaderSourceFile);
- QString tessellationEvaluationShaderSourceFile() const;
-
- void setGeometryShaderSourceFile(const QString &geometryShaderSourceFile);
- QString geometryShaderSourceFile() const;
-
- void setFragmentShaderSourceFile(const QString &fragmentShaderSourceFile);
- QString fragmentShaderSourceFile() const;
-
- void setComputeShaderSourceFile(const QString &computeShaderSourceFile);
- QString computeShaderSourceFile() const;
-
- void setShaderSourceFile(ShaderType type, const QString &sourceFile);
- QString shaderSourceFile(ShaderType type) const;
-
// Source code in-line
void setVertexShaderCode(const QByteArray &vertexShaderCode);
QByteArray vertexShaderCode() const;
@@ -126,14 +97,9 @@ public:
void setShaderCode(ShaderType type, const QByteArray &shaderCode);
QByteArray shaderCode(ShaderType type) const;
-Q_SIGNALS:
- void vertexShaderSourceFileChanged();
- void tessellationControlShaderSourceFileChanged();
- void tessellationEvaluationShaderSourceFileChanged();
- void geometryShaderSourceFileChanged();
- void fragmentShaderSourceFileChanged();
- void computeShaderSourceFileChanged();
+ Q_INVOKABLE static QByteArray loadSource(const QUrl &sourceUrl);
+Q_SIGNALS:
void vertexShaderCodeChanged();
void tessellationControlShaderCodeChanged();
void tessellationEvaluationShaderCodeChanged();
diff --git a/src/render/io/gltfparser.cpp b/src/render/io/gltfparser.cpp
index d5feb9464..497506538 100644
--- a/src/render/io/gltfparser.cpp
+++ b/src/render/io/gltfparser.cpp
@@ -69,6 +69,7 @@
#include <QColor>
#include <QVector2D>
#include <QVector3D>
+#include <QUrl>
// need to move these to somewhere common?
#include <Qt3DRenderer/private/renderstate_p.h>
@@ -751,8 +752,8 @@ void GLTFParser::processJSONProgram( QString id, QJsonObject jsonObj)
return;
}
- prog->setFragmentShaderSourceFile(m_shaderPaths[fragName]);
- prog->setVertexShaderSourceFile(m_shaderPaths[vertName]);
+ prog->setFragmentShaderCode(Qt3D::QShaderProgram::loadSource(QUrl(m_shaderPaths[fragName])));
+ prog->setVertexShaderCode(Qt3D::QShaderProgram::loadSource(QUrl(m_shaderPaths[vertName])));
m_programs[id] = prog;
}