| 1 | // Copyright (C) 2020 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 | #ifndef QT3DRENDER_RENDER_RHI_GLSHADER_P_H |
| 5 | #define QT3DRENDER_RENDER_RHI_GLSHADER_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of other Qt classes. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <shadervariables_p.h> |
| 19 | #include <shaderparameterpack_p.h> |
| 20 | #include <Qt3DRender/qshaderprogram.h> |
| 21 | #include <QMutex> |
| 22 | #include <QtGui/private/qshader_p.h> |
| 23 | #include <rhi/qrhi.h> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | namespace Qt3DRender { |
| 28 | |
| 29 | namespace Render { |
| 30 | |
| 31 | namespace Rhi { |
| 32 | |
| 33 | class Q_AUTOTEST_EXPORT RHIShader |
| 34 | { |
| 35 | public: |
| 36 | |
| 37 | struct UBO_Member |
| 38 | { |
| 39 | int nameId; |
| 40 | QShaderDescription::BlockVariable blockVariable; |
| 41 | std::vector<UBO_Member> structMembers; |
| 42 | }; |
| 43 | |
| 44 | struct UBO_Block |
| 45 | { |
| 46 | ShaderUniformBlock block; |
| 47 | std::vector<UBO_Member> members; |
| 48 | }; |
| 49 | |
| 50 | RHIShader(); |
| 51 | |
| 52 | bool isLoaded() const { return m_isLoaded; } |
| 53 | void setLoaded(bool loaded) { m_isLoaded = loaded; } |
| 54 | |
| 55 | void setFragOutputs(QHash<QString, int> fragOutputs); |
| 56 | const QHash<QString, int> &fragOutputs() const; |
| 57 | |
| 58 | inline const std::vector<int> &uniformsNamesIds() const { return m_uniformsNamesIds; } |
| 59 | inline const std::vector<int> &standardUniformNameIds() const { return m_standardUniformNamesIds; } |
| 60 | inline const std::vector<int> &uniformBlockNamesIds() const { return m_uniformBlockNamesIds; } |
| 61 | inline const std::vector<int> &storageBlockNamesIds() const { return m_shaderStorageBlockNamesIds; } |
| 62 | inline const std::vector<int> &attributeNamesIds() const { return m_attributeNamesIds; } |
| 63 | |
| 64 | const std::vector<QString> &uniformsNames() const; |
| 65 | const std::vector<QString> &attributesNames() const; |
| 66 | const std::vector<QString> &uniformBlockNames() const; |
| 67 | const std::vector<QString> &storageBlockNames() const; |
| 68 | const std::vector<QString> &samplerNames() const; |
| 69 | const std::vector<QString> &imagesNames() const; |
| 70 | |
| 71 | inline const std::vector<ShaderUniform> &uniforms() const { return m_uniforms; } |
| 72 | inline const std::vector<ShaderAttribute> &attributes() const { return m_attributes; } |
| 73 | inline const std::vector<ShaderUniformBlock> &uniformBlocks() const { return m_uniformBlocks; } |
| 74 | inline const std::vector<ShaderStorageBlock> &storageBlocks() const { return m_shaderStorageBlocks; } |
| 75 | inline const std::vector<ShaderAttribute> &samplers() const { return m_samplers; } |
| 76 | inline const std::vector<ShaderAttribute> &images() const { return m_images; } |
| 77 | |
| 78 | QHash<QString, ShaderUniform> activeUniformsForUniformBlock(int blockIndex) const; |
| 79 | |
| 80 | ShaderUniformBlock uniformBlockForBlockIndex(int blockNameId) const noexcept; |
| 81 | ShaderUniformBlock uniformBlockForBlockNameId(int blockIndex) const noexcept; |
| 82 | ShaderUniformBlock uniformBlockForBlockName(const QString &blockName) const noexcept; |
| 83 | |
| 84 | ShaderUniformBlock uniformBlockForInstanceName(const QString &instanceName) const noexcept; |
| 85 | ShaderUniformBlock uniformBlockForInstanceNameId(int instanceNameId) const noexcept; |
| 86 | |
| 87 | ShaderStorageBlock storageBlockForBlockIndex(int blockIndex) const noexcept; |
| 88 | ShaderStorageBlock storageBlockForBlockNameId(int blockNameId) const noexcept; |
| 89 | ShaderStorageBlock storageBlockForBlockName(const QString &blockName) const noexcept; |
| 90 | |
| 91 | enum ParameterKind { Uniform, UBO, SSBO, Struct }; |
| 92 | ParameterKind categorizeVariable(int nameId) const noexcept; |
| 93 | |
| 94 | bool hasUniform(int nameId) const noexcept; |
| 95 | bool hasActiveVariables() const noexcept; |
| 96 | |
| 97 | void setShaderCode(std::vector<QByteArray> shaderCode); |
| 98 | const std::vector<QByteArray> &shaderCode() const; |
| 99 | |
| 100 | const QShader &shaderStage(QShader::Stage stage) const noexcept { return m_stages[stage]; } |
| 101 | const std::vector<UBO_Block> &uboBlocks() const { return m_uboBlocks; } |
| 102 | |
| 103 | const QSet<QString> &unqualifiedUniformNames() const noexcept |
| 104 | { |
| 105 | return m_unqualifiedUniformNames; |
| 106 | } |
| 107 | |
| 108 | void introspect(); |
| 109 | |
| 110 | private: |
| 111 | bool m_isLoaded; |
| 112 | QShader m_stages[6]; |
| 113 | |
| 114 | std::vector<QString> m_uniformsNames; |
| 115 | std::vector<int> m_uniformsNamesIds; |
| 116 | std::vector<int> m_standardUniformNamesIds; |
| 117 | std::vector<ShaderUniform> m_uniforms; |
| 118 | |
| 119 | std::vector<QString> m_attributesNames; |
| 120 | std::vector<int> m_attributeNamesIds; |
| 121 | std::vector<ShaderAttribute> m_attributes; |
| 122 | |
| 123 | std::vector<QString> m_uniformBlockNames; |
| 124 | std::vector<int> m_uniformBlockNamesIds; |
| 125 | std::vector<ShaderUniformBlock> m_uniformBlocks; |
| 126 | QHash<int, QHash<QString, ShaderUniform>> m_uniformBlockIndexToShaderUniforms; |
| 127 | QSet<QString> m_unqualifiedUniformNames; |
| 128 | |
| 129 | std::vector<QString> m_shaderStorageBlockNames; |
| 130 | std::vector<int> m_shaderStorageBlockNamesIds; |
| 131 | std::vector<ShaderStorageBlock> m_shaderStorageBlocks; |
| 132 | |
| 133 | std::vector<QString> m_samplerNames; |
| 134 | std::vector<int> m_samplerIds; |
| 135 | std::vector<ShaderAttribute> m_samplers; |
| 136 | |
| 137 | std::vector<QString> m_imageNames; |
| 138 | std::vector<int> m_imageIds; |
| 139 | std::vector<ShaderAttribute> m_images; |
| 140 | |
| 141 | std::vector<QString> m_structNames; |
| 142 | std::vector<int> m_structNamesIds; |
| 143 | |
| 144 | QHash<QString, int> m_fragOutputs; |
| 145 | std::vector<QByteArray> m_shaderCode; |
| 146 | |
| 147 | // Private so that only SubmissionContext can call it |
| 148 | friend class SubmissionContext; |
| 149 | void initializeAttributes(std::vector<Qt3DRender::Render::Rhi::ShaderAttribute> attributesDescription); |
| 150 | void initializeUniformBlocks(std::vector<ShaderUniformBlock> uniformBlockDescription); |
| 151 | void initializeShaderStorageBlocks(std::vector<ShaderStorageBlock> shaderStorageBlockDescription); |
| 152 | void initializeSamplers(std::vector<ShaderAttribute> samplerDescription); |
| 153 | void initializeImages(std::vector<ShaderAttribute> imageDescription); |
| 154 | void recordAllUniforms(UBO_Member &uboMember, QString parentName); |
| 155 | |
| 156 | std::vector<UBO_Block> m_uboBlocks; |
| 157 | |
| 158 | mutable QMutex m_mutex; |
| 159 | QMetaObject::Connection m_contextConnection; |
| 160 | }; |
| 161 | |
| 162 | } // Rhi |
| 163 | |
| 164 | } // Render |
| 165 | |
| 166 | } // Qt3DRender |
| 167 | |
| 168 | QT_END_NAMESPACE |
| 169 | |
| 170 | #endif // QT3DRENDER_RENDER_RHI_GLSHADER_P_H |
| 171 | |