| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "shaderparameterpack_p.h" |
| 5 | |
| 6 | #include <graphicscontext_p.h> |
| 7 | #include <Qt3DRender/private/texture_p.h> |
| 8 | |
| 9 | #include <QOpenGLShaderProgram> |
| 10 | #include <QDebug> |
| 11 | #include <QColor> |
| 12 | #include <QQuaternion> |
| 13 | #include <Qt3DRender/private/renderlogging_p.h> |
| 14 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | namespace Qt3DRender { |
| 18 | namespace Render { |
| 19 | namespace OpenGL { |
| 20 | |
| 21 | ShaderParameterPack::~ShaderParameterPack() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | void ShaderParameterPack::reserve(int uniformCount) |
| 26 | { |
| 27 | m_uniforms.reserve(count: uniformCount); |
| 28 | m_submissionUniformIndices.reserve(n: uniformCount); |
| 29 | } |
| 30 | |
| 31 | void ShaderParameterPack::setUniform(const int glslNameId, const UniformValue &val) |
| 32 | { |
| 33 | m_uniforms.insert(key: glslNameId, value: val); |
| 34 | } |
| 35 | |
| 36 | void ShaderParameterPack::setTexture(const int glslNameId, int uniformArrayIndex, Qt3DCore::QNodeId texId) |
| 37 | { |
| 38 | for (size_t t = 0; t < m_textures.size(); ++t) { |
| 39 | if (m_textures[t].glslNameId != glslNameId || m_textures[t].uniformArrayIndex != uniformArrayIndex) |
| 40 | continue; |
| 41 | |
| 42 | m_textures[t].nodeId = texId; |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | m_textures.push_back(x: NamedResource(glslNameId, texId, uniformArrayIndex, NamedResource::Texture)); |
| 47 | } |
| 48 | |
| 49 | void ShaderParameterPack::setImage(const int glslNameId, int uniformArrayIndex, Qt3DCore::QNodeId id) |
| 50 | { |
| 51 | for (qsizetype i=0, m = m_images.size(); i < m; ++i) { |
| 52 | if (m_images[i].glslNameId != glslNameId || m_images[i].uniformArrayIndex != uniformArrayIndex) |
| 53 | continue; |
| 54 | |
| 55 | m_images[i].nodeId = id; |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | m_images.push_back(x: NamedResource(glslNameId, id, uniformArrayIndex, NamedResource::Image)); |
| 60 | } |
| 61 | |
| 62 | // Contains Uniform Block Index and QNodeId of the ShaderData (UBO) |
| 63 | void ShaderParameterPack::setUniformBuffer(BlockToUBO blockToUBO) |
| 64 | { |
| 65 | const auto uEnd = m_uniformBuffers.end(); |
| 66 | auto it = std::find_if(first: m_uniformBuffers.begin(), last: uEnd, pred: [&] (const BlockToUBO &block) { |
| 67 | return blockToUBO.m_blockIndex == block.m_blockIndex; |
| 68 | }); |
| 69 | |
| 70 | if (it == uEnd) |
| 71 | m_uniformBuffers.push_back(x: std::move(blockToUBO)); |
| 72 | else |
| 73 | *it = std::move(blockToUBO); |
| 74 | } |
| 75 | |
| 76 | void ShaderParameterPack::setShaderStorageBuffer(BlockToSSBO blockToSSBO) |
| 77 | { |
| 78 | const auto uEnd = m_shaderStorageBuffers.end(); |
| 79 | auto it = std::find_if(first: m_shaderStorageBuffers.begin(), last: uEnd, pred: [&] (const BlockToSSBO &block) { |
| 80 | return blockToSSBO.m_blockIndex == block.m_blockIndex; |
| 81 | }); |
| 82 | |
| 83 | if (it == uEnd) |
| 84 | m_shaderStorageBuffers.push_back(x: std::move(blockToSSBO)); |
| 85 | else |
| 86 | *it = std::move(blockToSSBO); |
| 87 | } |
| 88 | |
| 89 | void ShaderParameterPack::setSubmissionUniformIndex(const int uniformIdx) |
| 90 | { |
| 91 | m_submissionUniformIndices.push_back(x: uniformIdx); |
| 92 | } |
| 93 | |
| 94 | } // namespace OpenGL |
| 95 | } // namespace Render |
| 96 | } // namespace Qt3DRender |
| 97 | |
| 98 | QT_END_NAMESPACE |
| 99 | |