diff options
Diffstat (limited to 'src/render/renderers/opengl/renderer')
3 files changed, 67 insertions, 28 deletions
diff --git a/src/render/renderers/opengl/renderer/renderview.cpp b/src/render/renderers/opengl/renderer/renderview.cpp index cf4a930bc..ebb1ee7ab 100644 --- a/src/render/renderers/opengl/renderer/renderview.cpp +++ b/src/render/renderers/opengl/renderer/renderview.cpp @@ -328,14 +328,14 @@ struct AdjacentSubRangeFinder<QSortPolicy::Texture> static bool adjacentSubRange(RenderCommand *a, RenderCommand *b) { // Two renderCommands are adjacent if one contains all the other command's textures - QVector<ShaderParameterPack::NamedTexture> texturesA = a->m_parameterPack.textures(); - QVector<ShaderParameterPack::NamedTexture> texturesB = b->m_parameterPack.textures(); + QVector<ShaderParameterPack::NamedResource> texturesA = a->m_parameterPack.textures(); + QVector<ShaderParameterPack::NamedResource> texturesB = b->m_parameterPack.textures(); if (texturesB.size() > texturesA.size()) qSwap(texturesA, texturesB); // textureB.size() is always <= textureA.size() - for (const ShaderParameterPack::NamedTexture &texB : qAsConst(texturesB)) { + for (const ShaderParameterPack::NamedResource &texB : qAsConst(texturesB)) { if (!texturesA.contains(texB)) return false; } @@ -421,8 +421,8 @@ struct SubRangeSorter<QSortPolicy::Texture> static void sortSubRange(CommandIt begin, const CommandIt end) { std::stable_sort(begin, end, [] (RenderCommand *a, RenderCommand *b) { - QVector<ShaderParameterPack::NamedTexture> texturesA = a->m_parameterPack.textures(); - QVector<ShaderParameterPack::NamedTexture> texturesB = b->m_parameterPack.textures(); + QVector<ShaderParameterPack::NamedResource> texturesA = a->m_parameterPack.textures(); + QVector<ShaderParameterPack::NamedResource> texturesB = b->m_parameterPack.textures(); const int originalTextureASize = texturesA.size(); const bool isSuperior = originalTextureASize > texturesB.size(); @@ -432,7 +432,7 @@ struct SubRangeSorter<QSortPolicy::Texture> int identicalTextureCount = 0; - for (const ShaderParameterPack::NamedTexture &texB : qAsConst(texturesB)) { + for (const ShaderParameterPack::NamedResource &texB : qAsConst(texturesB)) { if (texturesA.contains(texB)) ++identicalTextureCount; } @@ -829,23 +829,35 @@ void RenderView::updateMatrices() void RenderView::setUniformValue(ShaderParameterPack &uniformPack, int nameId, const UniformValue &value) const { // At this point a uniform value can only be a scalar type - // or a Qt3DCore::QNodeId corresponding to a Texture + // or a Qt3DCore::QNodeId corresponding to a Texture or Image // ShaderData/Buffers would be handled as UBO/SSBO and would therefore // not be in the default uniform block if (value.valueType() == UniformValue::NodeId) { const Qt3DCore::QNodeId *nodeIds = value.constData<Qt3DCore::QNodeId>(); const int uniformArraySize = value.byteSize() / sizeof(Qt3DCore::QNodeId); + UniformValue::ValueType resourceType = UniformValue::TextureValue; + for (int i = 0; i < uniformArraySize; ++i) { - const Qt3DCore::QNodeId texId = nodeIds[i]; - const Texture *tex = m_manager->textureManager()->lookupResource(texId); - if (tex != nullptr) - uniformPack.setTexture(nameId, i, texId); + const Qt3DCore::QNodeId resourceId = nodeIds[i]; + + const Texture *tex = m_manager->textureManager()->lookupResource(resourceId); + if (tex != nullptr) { + uniformPack.setTexture(nameId, i, resourceId); + } else { + const ShaderImage *img = m_manager->shaderImageManager()->lookupResource(resourceId); + if (img != nullptr) { + resourceType = UniformValue::ShaderImageValue; + uniformPack.setImage(nameId, i, resourceId); + } + } } - UniformValue textureValue(uniformArraySize * sizeof(int), UniformValue::TextureValue); - std::fill(textureValue.data<int>(), textureValue.data<int>() + uniformArraySize, -1); - uniformPack.setUniform(nameId, textureValue); + // This uniform will be overridden in SubmissionContext::setParameters + // and -1 values will be replaced by valid Texture or Image units + UniformValue uniformValue(uniformArraySize * sizeof(int), resourceType); + std::fill(uniformValue.data<int>(), uniformValue.data<int>() + uniformArraySize, -1); + uniformPack.setUniform(nameId, uniformValue); } else { uniformPack.setUniform(nameId, value); } diff --git a/src/render/renderers/opengl/renderer/shaderparameterpack.cpp b/src/render/renderers/opengl/renderer/shaderparameterpack.cpp index f78e45a5e..13d05cac1 100644 --- a/src/render/renderers/opengl/renderer/shaderparameterpack.cpp +++ b/src/render/renderers/opengl/renderer/shaderparameterpack.cpp @@ -71,11 +71,24 @@ void ShaderParameterPack::setTexture(const int glslNameId, int uniformArrayIndex if (m_textures[t].glslNameId != glslNameId || m_textures[t].uniformArrayIndex != uniformArrayIndex) continue; - m_textures[t].texId = texId; + m_textures[t].nodeId = texId; return; } - m_textures.append(NamedTexture(glslNameId, texId, uniformArrayIndex)); + m_textures.append(NamedResource(glslNameId, texId, uniformArrayIndex, NamedResource::Texture)); +} + +void ShaderParameterPack::setImage(const int glslNameId, int uniformArrayIndex, Qt3DCore::QNodeId id) +{ + for (int i=0, m = m_images.size(); i < m; ++i) { + if (m_images[i].glslNameId != glslNameId || m_images[i].uniformArrayIndex != uniformArrayIndex) + continue; + + m_images[i].nodeId = id; + return; + } + + m_images.append(NamedResource(glslNameId, id, uniformArrayIndex, NamedResource::Image)); } // Contains Uniform Block Index and QNodeId of the ShaderData (UBO) diff --git a/src/render/renderers/opengl/renderer/shaderparameterpack_p.h b/src/render/renderers/opengl/renderer/shaderparameterpack_p.h index d1fbe936f..a5aee6ac4 100644 --- a/src/render/renderers/opengl/renderer/shaderparameterpack_p.h +++ b/src/render/renderers/opengl/renderer/shaderparameterpack_p.h @@ -98,6 +98,8 @@ public: void setUniform(const int glslNameId, const UniformValue &val); void setTexture(const int glslNameId, int uniformArrayIndex, Qt3DCore::QNodeId id); + void setImage(const int glslNameId, int uniformArrayIndex, Qt3DCore::QNodeId id); + void setUniformBuffer(BlockToUBO blockToUBO); void setShaderStorageBuffer(BlockToSSBO blockToSSBO); void setSubmissionUniform(const ShaderUniform &uniform); @@ -106,47 +108,59 @@ public: inline const PackUniformHash &uniforms() const { return m_uniforms; } UniformValue uniform(const int glslNameId) const { return m_uniforms.value(glslNameId); } - struct NamedTexture + + struct NamedResource { - NamedTexture() {} - NamedTexture(const int glslNameId, Qt3DCore::QNodeId texId, int uniformArrayIndex) + enum Type { + Texture = 0, + Image + }; + + NamedResource() {} + NamedResource(const int glslNameId, Qt3DCore::QNodeId texId, + int uniformArrayIndex, Type type) : glslNameId(glslNameId) - , texId(texId) + , nodeId(texId) , uniformArrayIndex(uniformArrayIndex) + , type(type) { } int glslNameId; - Qt3DCore::QNodeId texId; + Qt3DCore::QNodeId nodeId; int uniformArrayIndex; + Type type; - bool operator==(const NamedTexture &other) const + bool operator==(const NamedResource &other) const { return glslNameId == other.glslNameId && - texId == other.texId && - uniformArrayIndex == other.uniformArrayIndex; + nodeId == other.nodeId && + uniformArrayIndex == other.uniformArrayIndex && + type == other.type; } - bool operator!=(const NamedTexture &other) const + bool operator!=(const NamedResource &other) const { return !(*this == other); } }; - inline QVector<NamedTexture> textures() const { return m_textures; } + inline QVector<NamedResource> textures() const { return m_textures; } + inline QVector<NamedResource> images() const { return m_images; } inline QVector<BlockToUBO> uniformBuffers() const { return m_uniformBuffers; } inline QVector<BlockToSSBO> shaderStorageBuffers() const { return m_shaderStorageBuffers; } inline QVector<ShaderUniform> submissionUniforms() const { return m_submissionUniforms; } private: PackUniformHash m_uniforms; - QVector<NamedTexture> m_textures; + QVector<NamedResource> m_textures; + QVector<NamedResource> m_images; QVector<BlockToUBO> m_uniformBuffers; QVector<BlockToSSBO> m_shaderStorageBuffers; QVector<ShaderUniform> m_submissionUniforms; friend class RenderView; }; -QT3D_DECLARE_TYPEINFO_2(Qt3DRender, Render, ShaderParameterPack::NamedTexture, Q_PRIMITIVE_TYPE) +QT3D_DECLARE_TYPEINFO_2(Qt3DRender, Render, ShaderParameterPack::NamedResource, Q_PRIMITIVE_TYPE) } // namespace Render } // namespace Qt3DRender |