diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2016-02-11 08:47:00 +0100 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2016-02-21 10:36:29 +0000 |
commit | 2fd869c0f36e821d0bfdb3a4edbb344a0d4190b9 (patch) | |
tree | 9b442a6a50ac34ccdeb620295df0fa0402648f51 /src/render/materialsystem | |
parent | 1e409bf2e5e7e21a25d9913ea044fe08db6ff787 (diff) |
RenderViews: use int comparison rather than string for uniforms filtering
Provides a non negligible gain in performance.
Change-Id: I1165d1fcd044e3bdebf286539da2f5316f764540
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/render/materialsystem')
-rw-r--r-- | src/render/materialsystem/parameter.cpp | 14 | ||||
-rw-r--r-- | src/render/materialsystem/parameter_p.h | 2 | ||||
-rw-r--r-- | src/render/materialsystem/parametermapping.cpp | 15 | ||||
-rw-r--r-- | src/render/materialsystem/parametermapping_p.h | 4 | ||||
-rw-r--r-- | src/render/materialsystem/shader.cpp | 69 | ||||
-rw-r--r-- | src/render/materialsystem/shader_p.h | 18 |
6 files changed, 106 insertions, 16 deletions
diff --git a/src/render/materialsystem/parameter.cpp b/src/render/materialsystem/parameter.cpp index db3f3df19..5933f1d52 100644 --- a/src/render/materialsystem/parameter.cpp +++ b/src/render/materialsystem/parameter.cpp @@ -47,6 +47,7 @@ #include <Qt3DRender/private/buffer_p.h> #include <Qt3DRender/private/managers_p.h> +#include <Qt3DRender/private/stringtoint_p.h> QT_BEGIN_NAMESPACE @@ -57,6 +58,7 @@ namespace Render { Parameter::Parameter() : QBackendNode() + , m_nameId(-1) { } @@ -64,6 +66,7 @@ void Parameter::updateFromPeer(Qt3DCore::QNode *peer) { QParameter *param = static_cast<QParameter *>(peer); m_name = param->name(); + m_nameId = StringToInt::lookupId(m_name); m_value = static_cast<QParameterPrivate *>(QNodePrivate::get(param))->m_backendValue; } @@ -72,10 +75,12 @@ void Parameter::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) QScenePropertyChangePtr propertyChange = qSharedPointerCast<QScenePropertyChange>(e); if (e->type() == NodeUpdated) { - if (propertyChange->propertyName() == QByteArrayLiteral("name")) + if (propertyChange->propertyName() == QByteArrayLiteral("name")) { m_name = propertyChange->value().toString(); - else if (propertyChange->propertyName() == QByteArrayLiteral("value")) + m_nameId = StringToInt::lookupId(m_name); + } else if (propertyChange->propertyName() == QByteArrayLiteral("value")) { m_value = propertyChange->value(); + } } } @@ -89,6 +94,11 @@ QVariant Parameter::value() const return m_value; } +int Parameter::nameId() const +{ + return m_nameId; +} + } // namespace Render } // namespace Qt3DRender diff --git a/src/render/materialsystem/parameter_p.h b/src/render/materialsystem/parameter_p.h index 281e1b9e9..e3e2fd663 100644 --- a/src/render/materialsystem/parameter_p.h +++ b/src/render/materialsystem/parameter_p.h @@ -74,10 +74,12 @@ public: QString name() const; QVariant value() const; + int nameId() const; private: QString m_name; QVariant m_value; + int m_nameId; }; } // namespace Render diff --git a/src/render/materialsystem/parametermapping.cpp b/src/render/materialsystem/parametermapping.cpp index 06dd14836..574025030 100644 --- a/src/render/materialsystem/parametermapping.cpp +++ b/src/render/materialsystem/parametermapping.cpp @@ -38,6 +38,7 @@ ****************************************************************************/ #include "parametermapping_p.h" +#include <Qt3DRender/private/stringtoint_p.h> QT_BEGIN_NAMESPACE @@ -46,6 +47,8 @@ namespace Render { ParameterMapping::ParameterMapping() : m_bindingType(QParameterMapping::Uniform) + , m_parameterNameId(-1) + , m_shaderVariableNameId(-1) { } @@ -53,6 +56,8 @@ ParameterMapping::ParameterMapping(QParameterMapping *mapping) : m_id(mapping ? mapping->id() : Qt3DCore::QNodeId()) , m_parameterName(mapping ? mapping->parameterName() : QString()) , m_shaderVariableName(mapping ? mapping->shaderVariableName() : QString()) + , m_parameterNameId(StringToInt::lookupId(m_parameterName)) + , m_shaderVariableNameId(StringToInt::lookupId(m_shaderVariableName)) , m_bindingType(mapping ? mapping->bindingType() : QParameterMapping::Uniform) { } @@ -77,6 +82,16 @@ QString ParameterMapping::shaderVariableName() const return m_shaderVariableName; } +int ParameterMapping::parameterNameId() const +{ + return m_parameterNameId; +} + +int ParameterMapping::shaderVariableNameId() const +{ + return m_shaderVariableNameId; +} + QParameterMapping::Binding ParameterMapping::bindingType() const { return m_bindingType; diff --git a/src/render/materialsystem/parametermapping_p.h b/src/render/materialsystem/parametermapping_p.h index 734fa23ed..975fed18e 100644 --- a/src/render/materialsystem/parametermapping_p.h +++ b/src/render/materialsystem/parametermapping_p.h @@ -70,12 +70,16 @@ public: Qt3DCore::QNodeId id() const; QString parameterName() const; QString shaderVariableName() const; + int parameterNameId() const; + int shaderVariableNameId() const; QParameterMapping::Binding bindingType() const; private: Qt3DCore::QNodeId m_id; QString m_parameterName; QString m_shaderVariableName; + int m_parameterNameId; + int m_shaderVariableNameId; QParameterMapping::Binding m_bindingType; }; diff --git a/src/render/materialsystem/shader.cpp b/src/render/materialsystem/shader.cpp index 396533da3..c5359d41b 100644 --- a/src/render/materialsystem/shader.cpp +++ b/src/render/materialsystem/shader.cpp @@ -48,6 +48,7 @@ #include <Qt3DRender/private/graphicscontext_p.h> #include <Qt3DRender/private/attachmentpack_p.h> #include <Qt3DCore/qscenepropertychange.h> +#include <Qt3DRender/private/stringtoint_p.h> QT_BEGIN_NAMESPACE @@ -188,7 +189,7 @@ QHash<QString, ShaderUniform> Shader::activeUniformsForUniformBlock(int blockInd return m_uniformBlockIndexToShaderUniforms.value(blockIndex); } -ShaderUniformBlock Shader::uniformBlock(int blockIndex) +ShaderUniformBlock Shader::uniformBlockForBlockIndex(int blockIndex) { for (int i = 0, m = m_uniformBlocks.size(); i < m; ++i) { if (m_uniformBlocks[i].m_index == blockIndex) { @@ -198,7 +199,17 @@ ShaderUniformBlock Shader::uniformBlock(int blockIndex) return ShaderUniformBlock(); } -ShaderUniformBlock Shader::uniformBlock(const QString &blockName) +ShaderUniformBlock Shader::uniformBlockForBlockNameId(int blockNameId) +{ + for (int i = 0, m = m_uniformBlocks.size(); i < m; ++i) { + if (m_uniformBlocks[i].m_nameId == blockNameId) { + return m_uniformBlocks[i]; + } + } + return ShaderUniformBlock(); +} + +ShaderUniformBlock Shader::uniformBlockForBlockName(const QString &blockName) { for (int i = 0, m = m_uniformBlocks.size(); i < m; ++i) { if (m_uniformBlocks[i].m_name == blockName) { @@ -208,7 +219,7 @@ ShaderUniformBlock Shader::uniformBlock(const QString &blockName) return ShaderUniformBlock(); } -ShaderStorageBlock Shader::storageBlock(int blockIndex) +ShaderStorageBlock Shader::storageBlockForBlockIndex(int blockIndex) { for (int i = 0, m = m_shaderStorageBlockNames.size(); i < m; ++i) { if (m_shaderStorageBlocks[i].m_index == blockIndex) @@ -217,7 +228,16 @@ ShaderStorageBlock Shader::storageBlock(int blockIndex) return ShaderStorageBlock(); } -ShaderStorageBlock Shader::storageBlock(const QString &blockName) +ShaderStorageBlock Shader::storageBlockForBlockNameId(int blockNameId) +{ + for (int i = 0, m = m_shaderStorageBlockNames.size(); i < m; ++i) { + if (m_shaderStorageBlocks[i].m_nameId == blockNameId) + return m_shaderStorageBlocks[i]; + } + return ShaderStorageBlock(); +} + +ShaderStorageBlock Shader::storageBlockForBlockName(const QString &blockName) { for (int i = 0, m = m_shaderStorageBlockNames.size(); i < m; ++i) { if (m_shaderStorageBlocks[i].m_name == blockName) @@ -243,11 +263,11 @@ QOpenGLShaderProgram *Shader::getOrCreateProgram(GraphicsContext *ctx) void Shader::updateUniforms(GraphicsContext *ctx, const ShaderParameterPack &pack) { - const QHash<QString, const QUniformValue* > &values = pack.uniforms(); - const QHash<QString, const QUniformValue* >::const_iterator valueEnd = values.constEnd(); + const PackUniformHash values = pack.uniforms(); + const PackUniformHash::const_iterator valueEnd = values.constEnd(); Q_FOREACH (const ShaderUniform &uniform, m_uniforms) { - QHash<QString, const QUniformValue* >::const_iterator valueIt = values.constFind(uniform.m_name); + PackUniformHash::const_iterator valueIt = values.constFind(uniform.m_nameId); if (valueIt != valueEnd) valueIt.value()->apply(ctx, uniform); } @@ -262,6 +282,21 @@ void Shader::setFragOutputs(const QHash<QString, int> &fragOutputs) updateDNA(); } +QVector<int> Shader::uniformsNamesIds() const +{ + return m_uniformsNamesIds; +} + +QVector<int> Shader::uniformBlockNamesIds() const +{ + return m_uniformBlockNamesIds; +} + +QVector<int> Shader::storageBlockNamesIds() const +{ + return m_shaderStorageBlockNamesIds; +} + static QOpenGLShader::ShaderType shaderType(QShaderProgram::ShaderType type) { switch (type) { @@ -347,10 +382,13 @@ void Shader::initializeUniforms(const QVector<ShaderUniform> &uniformsDescriptio { m_uniforms = uniformsDescription; m_uniformsNames.resize(uniformsDescription.size()); + m_uniformsNamesIds.resize(uniformsDescription.size()); QHash<QString, ShaderUniform> activeUniformsInDefaultBlock; for (int i = 0, m = uniformsDescription.size(); i < m; i++) { - m_uniformsNames[i] = uniformsDescription[i].m_name; + m_uniformsNames[i] = m_uniforms[i].m_name; + m_uniforms[i].m_nameId = StringToInt::lookupId(m_uniformsNames[i]); + m_uniformsNamesIds[i] = m_uniforms[i].m_nameId; if (uniformsDescription[i].m_blockIndex == -1) { // Uniform is in default block qCDebug(Shaders) << "Active Uniform in Default Block " << uniformsDescription[i].m_name << uniformsDescription[i].m_blockIndex; activeUniformsInDefaultBlock.insert(uniformsDescription[i].m_name, uniformsDescription[i]); @@ -373,8 +411,11 @@ void Shader::initializeUniformBlocks(const QVector<ShaderUniformBlock> &uniformB { m_uniformBlocks = uniformBlockDescription; m_uniformBlockNames.resize(uniformBlockDescription.size()); + m_uniformBlockNamesIds.resize(uniformBlockDescription.size()); for (int i = 0, m = uniformBlockDescription.size(); i < m; ++i) { - m_uniformBlockNames[i] = uniformBlockDescription[i].m_name; + m_uniformBlockNames[i] = m_uniformBlocks[i].m_name; + m_uniformBlockNamesIds[i] = StringToInt::lookupId(m_uniformBlockNames[i]); + m_uniformBlocks[i].m_nameId = m_uniformBlockNamesIds[i]; qCDebug(Shaders) << "Initializing Uniform Block {" << m_uniformBlockNames[i] << "}"; // Find all active uniforms for the shader block @@ -405,9 +446,12 @@ void Shader::initializeShaderStorageBlocks(const QVector<ShaderStorageBlock> &sh { m_shaderStorageBlocks = shaderStorageBlockDescription; m_shaderStorageBlockNames.resize(shaderStorageBlockDescription.size()); + m_shaderStorageBlockNamesIds.resize(shaderStorageBlockDescription.size()); for (int i = 0, m = shaderStorageBlockDescription.size(); i < m; ++i) { - m_shaderStorageBlockNames[i] = shaderStorageBlockDescription[i].m_name; + m_shaderStorageBlockNames[i] = m_shaderStorageBlocks[i].m_name; + m_shaderStorageBlockNamesIds[i] = StringToInt::lookupId(m_shaderStorageBlockNames[i]); + m_shaderStorageBlocks[i].m_nameId =m_shaderStorageBlockNamesIds[i]; qCDebug(Shaders) << "Initializing Shader Storage Block {" << m_shaderStorageBlockNames[i] << "}"; } } @@ -421,14 +465,19 @@ void Shader::initialize(const Shader &other) { Q_ASSERT(m_dna == other.m_dna); m_program = other.m_program; + m_uniformsNamesIds = other.m_uniformsNamesIds; m_uniformsNames = other.m_uniformsNames; m_uniforms = other.m_uniforms; m_attributesNames = other.m_attributesNames; m_attributes = other.m_attributes; + m_uniformBlockNamesIds = other.m_uniformBlockNamesIds; m_uniformBlockNames = other.m_uniformBlockNames; m_uniformBlocks = other.m_uniformBlocks; m_uniformBlockIndexToShaderUniforms = other.m_uniformBlockIndexToShaderUniforms; m_fragOutputs = other.m_fragOutputs; + m_shaderStorageBlockNamesIds = other.m_shaderStorageBlockNamesIds; + m_shaderStorageBlockNames = other.m_shaderStorageBlockNames; + m_shaderStorageBlocks = other.m_shaderStorageBlocks; m_isLoaded = other.m_isLoaded; } diff --git a/src/render/materialsystem/shader_p.h b/src/render/materialsystem/shader_p.h index 88694fd98..794ccc77d 100644 --- a/src/render/materialsystem/shader_p.h +++ b/src/render/materialsystem/shader_p.h @@ -84,6 +84,10 @@ public: void updateUniforms(GraphicsContext *ctx, const ShaderParameterPack &pack); void setFragOutputs(const QHash<QString, int> &fragOutputs); + QVector<int> uniformsNamesIds() const; + QVector<int> uniformBlockNamesIds() const; + QVector<int> storageBlockNamesIds() const; + QVector<QString> uniformsNames() const; QVector<QString> attributesNames() const; QVector<QString> uniformBlockNames() const; @@ -100,11 +104,14 @@ public: QVector<ShaderStorageBlock> storageBlocks() const; QHash<QString, ShaderUniform> activeUniformsForUniformBlock(int blockIndex) const; - ShaderUniformBlock uniformBlock(int blockIndex); - ShaderUniformBlock uniformBlock(const QString &blockName); - ShaderStorageBlock storageBlock(int blockIndex); - ShaderStorageBlock storageBlock(const QString &blockName); + ShaderUniformBlock uniformBlockForBlockIndex(int blockNameId); + ShaderUniformBlock uniformBlockForBlockNameId(int blockIndex); + ShaderUniformBlock uniformBlockForBlockName(const QString &blockName); + + ShaderStorageBlock storageBlockForBlockIndex(int blockIndex); + ShaderStorageBlock storageBlockForBlockNameId(int blockNameId); + ShaderStorageBlock storageBlockForBlockName(const QString &blockName); private: QOpenGLShaderProgram *m_program; @@ -113,16 +120,19 @@ private: QOpenGLShaderProgram *createDefaultProgram(); QVector<QString> m_uniformsNames; + QVector<int> m_uniformsNamesIds; QVector<ShaderUniform> m_uniforms; QVector<QString> m_attributesNames; QVector<ShaderAttribute> m_attributes; QVector<QString> m_uniformBlockNames; + QVector<int> m_uniformBlockNamesIds; QVector<ShaderUniformBlock> m_uniformBlocks; QHash<int, QHash<QString, ShaderUniform> > m_uniformBlockIndexToShaderUniforms; QVector<QString> m_shaderStorageBlockNames; + QVector<int> m_shaderStorageBlockNamesIds; QVector<ShaderStorageBlock> m_shaderStorageBlocks; QHash<QString, int> m_fragOutputs; |