| 1 | // Copyright (C) 2008-2012 NVIDIA Corporation. |
| 2 | // Copyright (C) 2019 The Qt Company Ltd. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QSSG_RENDER_SHADER_CODE_GENERATOR_V2_H |
| 6 | #define QSSG_RENDER_SHADER_CODE_GENERATOR_V2_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists purely as an |
| 13 | // implementation detail. This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include <QtQuick3DRuntimeRender/private/qtquick3druntimerenderglobal_p.h> |
| 20 | #include <QtQuick3DRuntimeRender/private/qssgrendershadercache_p.h> |
| 21 | |
| 22 | #include <QtCore/qstring.h> |
| 23 | |
| 24 | QT_BEGIN_NAMESPACE |
| 25 | |
| 26 | enum class QSSGShaderGeneratorStage |
| 27 | { |
| 28 | None = 0, |
| 29 | Vertex = 1, |
| 30 | Fragment = 1 << 1, |
| 31 | StageCount = 2, |
| 32 | }; |
| 33 | |
| 34 | Q_DECLARE_FLAGS(QSSGShaderGeneratorStageFlags, QSSGShaderGeneratorStage) |
| 35 | Q_DECLARE_OPERATORS_FOR_FLAGS(QSSGShaderGeneratorStageFlags) |
| 36 | |
| 37 | struct QSSGStageGeneratorBase; |
| 38 | class QSSGRenderContextInterface; |
| 39 | class QSSGShaderLibraryManager; |
| 40 | |
| 41 | class QSSGShaderResourceMergeContext; |
| 42 | |
| 43 | struct Q_QUICK3DRUNTIMERENDER_EXPORT QSSGStageGeneratorBase |
| 44 | { |
| 45 | enum class ShaderItemType { |
| 46 | VertexInput, |
| 47 | Input, |
| 48 | Output, |
| 49 | Uniform |
| 50 | }; |
| 51 | |
| 52 | enum class ShaderItemMapFlag { |
| 53 | Flat = 0x01 |
| 54 | }; |
| 55 | Q_DECLARE_FLAGS(ShaderItemMapFlags, ShaderItemMapFlag) |
| 56 | |
| 57 | // Using QMap intentionally - being sorted by key when iterating is helpful |
| 58 | // to get the same ordered list of vertex inputs, uniforms, etc. on every |
| 59 | // run, which in turn helps shader (disk) cache efficiency due to not |
| 60 | // generating a different shader string just because QHash decided to |
| 61 | // iterate entries in a different order. |
| 62 | typedef QMap<QByteArray, QByteArray> TStrTableStrMap; |
| 63 | typedef QMap<QByteArray, QPair<quint32, QByteArray>> TStrTableSizedStrMap; |
| 64 | |
| 65 | typedef QPair<QByteArray, QByteArray> TParamPair; |
| 66 | typedef QPair<QByteArray, TParamPair> TConstantBufferParamPair; |
| 67 | typedef QVector<TConstantBufferParamPair> TConstantBufferParamArray; |
| 68 | |
| 69 | TStrTableStrMap m_incoming; |
| 70 | TStrTableStrMap *m_outgoing = nullptr; |
| 71 | TStrTableStrMap m_flatIncoming; |
| 72 | TStrTableStrMap *m_flatOutgoing = nullptr; |
| 73 | QSet<QByteArray> m_includes; |
| 74 | TStrTableStrMap m_uniforms; |
| 75 | TStrTableSizedStrMap m_uniformArrays; |
| 76 | TStrTableStrMap m_constantBuffers; |
| 77 | TConstantBufferParamArray m_constantBufferParams; |
| 78 | QByteArray m_codeBuilder; |
| 79 | QByteArray m_finalBuilder; |
| 80 | QSSGShaderGeneratorStage m_stage; |
| 81 | QSSGShaderGeneratorStageFlags m_enabledStages; |
| 82 | QList<QByteArray> m_addedFunctions; |
| 83 | TStrTableStrMap m_addedDefinitions; |
| 84 | QSSGShaderResourceMergeContext *m_mergeContext = nullptr; |
| 85 | |
| 86 | explicit QSSGStageGeneratorBase(QSSGShaderGeneratorStage inStage); |
| 87 | virtual ~QSSGStageGeneratorBase() = default; |
| 88 | |
| 89 | virtual void begin(QSSGShaderGeneratorStageFlags inEnabledStages); |
| 90 | |
| 91 | virtual void addIncoming(const QByteArray &name, const QByteArray &type); |
| 92 | virtual void addOutgoing(const QByteArray &name, const QByteArray &type); |
| 93 | |
| 94 | virtual void addFlatIncoming(const QByteArray &name, const QByteArray &type); |
| 95 | virtual void addFlatOutgoing(const QByteArray &name, const QByteArray &type); |
| 96 | |
| 97 | virtual void addUniform(const QByteArray &name, const QByteArray &type); |
| 98 | |
| 99 | virtual void addUniformArray(const QByteArray &name, const QByteArray &type, quint32 size); |
| 100 | |
| 101 | virtual void addConstantBuffer(const QByteArray &name, const QByteArray &layout); |
| 102 | virtual void addConstantBufferParam(const QByteArray &cbName, const QByteArray ¶mName, const QByteArray &type); |
| 103 | |
| 104 | virtual QSSGStageGeneratorBase &operator<<(const QByteArray &data); |
| 105 | virtual void append(const QByteArray &data); |
| 106 | QSSGShaderGeneratorStage stage() const; |
| 107 | |
| 108 | void addShaderPass2Marker(ShaderItemType itemType); |
| 109 | |
| 110 | void addShaderItemMap(ShaderItemType itemType, const TStrTableStrMap &itemMap, ShaderItemMapFlags flags = {}); |
| 111 | |
| 112 | virtual void addShaderIncomingMap(); |
| 113 | |
| 114 | virtual void addShaderUniformMap(); |
| 115 | |
| 116 | virtual void addShaderOutgoingMap(); |
| 117 | |
| 118 | virtual void addShaderConstantBufferItemMap(const QByteArray &itemType, const TStrTableStrMap &cbMap, TConstantBufferParamArray cbParamsArray); |
| 119 | |
| 120 | virtual void appendShaderCode() final; |
| 121 | |
| 122 | virtual void addInclude(const QByteArray &name) final; |
| 123 | |
| 124 | void buildShaderSourcePass1(QSSGShaderResourceMergeContext *mergeContext); |
| 125 | |
| 126 | QByteArray buildShaderSourcePass2(QSSGShaderResourceMergeContext *mergeContext); |
| 127 | |
| 128 | virtual void addFunction(const QByteArray &functionName) final; |
| 129 | |
| 130 | virtual void addDefinition(const QByteArray &name, const QByteArray &value) final; |
| 131 | }; |
| 132 | |
| 133 | Q_DECLARE_OPERATORS_FOR_FLAGS(QSSGStageGeneratorBase::ShaderItemMapFlags) |
| 134 | |
| 135 | struct Q_QUICK3DRUNTIMERENDER_EXPORT QSSGVertexShaderGenerator final : public QSSGStageGeneratorBase |
| 136 | { |
| 137 | QSSGVertexShaderGenerator(); |
| 138 | }; |
| 139 | |
| 140 | struct Q_QUICK3DRUNTIMERENDER_EXPORT QSSGFragmentShaderGenerator final : public QSSGStageGeneratorBase |
| 141 | { |
| 142 | QSSGFragmentShaderGenerator(); |
| 143 | void addShaderIncomingMap() override; |
| 144 | void addShaderOutgoingMap() override; |
| 145 | }; |
| 146 | |
| 147 | class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGProgramGenerator |
| 148 | { |
| 149 | Q_DISABLE_COPY(QSSGProgramGenerator) |
| 150 | public: |
| 151 | QSSGProgramGenerator() = default; |
| 152 | QSSGVertexShaderGenerator m_vs; |
| 153 | QSSGFragmentShaderGenerator m_fs; |
| 154 | |
| 155 | QSSGShaderGeneratorStageFlags m_enabledStages; |
| 156 | |
| 157 | static constexpr QSSGShaderGeneratorStageFlags defaultFlags() { return QSSGShaderGeneratorStageFlags(QSSGShaderGeneratorStage::Vertex | QSSGShaderGeneratorStage::Fragment); } |
| 158 | |
| 159 | void linkStages(); |
| 160 | |
| 161 | void beginProgram(QSSGShaderGeneratorStageFlags inEnabledStages = defaultFlags()); |
| 162 | |
| 163 | QSSGShaderGeneratorStageFlags getEnabledStages() const; |
| 164 | |
| 165 | QSSGStageGeneratorBase &internalGetStage(QSSGShaderGeneratorStage inStage); |
| 166 | // get the stage or nullptr if it has not been created. |
| 167 | QSSGStageGeneratorBase *getStage(QSSGShaderGeneratorStage inStage); |
| 168 | |
| 169 | void registerShaderMetaDataFromSource(QSSGShaderResourceMergeContext *mergeContext, |
| 170 | const QByteArray &contents, |
| 171 | QSSGShaderGeneratorStage stage); |
| 172 | |
| 173 | QSSGRhiShaderPipelinePtr compileGeneratedRhiShader(const QByteArray &inMaterialInfoString, |
| 174 | const QSSGShaderFeatures &inFeatureSet, |
| 175 | QSSGShaderLibraryManager &shaderLibraryManager, |
| 176 | QSSGShaderCache &theCache, |
| 177 | QSSGRhiShaderPipeline::StageFlags stageFlags, |
| 178 | int viewCount, |
| 179 | bool perTargetCompilation); |
| 180 | }; |
| 181 | |
| 182 | QT_END_NAMESPACE |
| 183 | #endif |
| 184 | |