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
24QT_BEGIN_NAMESPACE
25
26enum class QSSGShaderGeneratorStage
27{
28 None = 0,
29 Vertex = 1,
30 Fragment = 1 << 1,
31 StageCount = 2,
32};
33
34Q_DECLARE_FLAGS(QSSGShaderGeneratorStageFlags, QSSGShaderGeneratorStage)
35Q_DECLARE_OPERATORS_FOR_FLAGS(QSSGShaderGeneratorStageFlags)
36
37struct QSSGStageGeneratorBase;
38class QSSGRenderContextInterface;
39class QSSGShaderLibraryManager;
40
41class QSSGShaderResourceMergeContext;
42
43struct Q_QUICK3DRUNTIMERENDER_EXPORT QSSGStageGeneratorBase
44{
45 enum class ShaderItemType {
46 VertexInput,
47 Input,
48 Output,
49 Uniform
50 };
51
52 // Using QMap intentionally - being sorted by key when iterating is helpful
53 // to get the same ordered list of vertex inputs, uniforms, etc. on every
54 // run, which in turn helps shader (disk) cache efficiency due to not
55 // generating a different shader string just because QHash decided to
56 // iterate entries in a different order.
57 typedef QMap<QByteArray, QByteArray> TStrTableStrMap;
58 typedef QMap<QByteArray, QPair<quint32, QByteArray>> TStrTableSizedStrMap;
59
60 typedef QPair<QByteArray, QByteArray> TParamPair;
61 typedef QPair<QByteArray, TParamPair> TConstantBufferParamPair;
62 typedef QVector<TConstantBufferParamPair> TConstantBufferParamArray;
63
64 TStrTableStrMap m_incoming;
65 TStrTableStrMap *m_outgoing;
66 QSet<QByteArray> m_includes;
67 TStrTableStrMap m_uniforms;
68 TStrTableSizedStrMap m_uniformArrays;
69 TStrTableStrMap m_constantBuffers;
70 TConstantBufferParamArray m_constantBufferParams;
71 QByteArray m_codeBuilder;
72 QByteArray m_finalBuilder;
73 QSSGShaderGeneratorStage m_stage;
74 QSSGShaderGeneratorStageFlags m_enabledStages;
75 QList<QByteArray> m_addedFunctions;
76 TStrTableStrMap m_addedDefinitions;
77 QSSGShaderResourceMergeContext *m_mergeContext = nullptr;
78
79 explicit QSSGStageGeneratorBase(QSSGShaderGeneratorStage inStage);
80 virtual ~QSSGStageGeneratorBase() = default;
81
82 virtual void begin(QSSGShaderGeneratorStageFlags inEnabledStages);
83
84 virtual void addIncoming(const QByteArray &name, const QByteArray &type);
85
86 virtual void addOutgoing(const QByteArray &name, const QByteArray &type);
87
88 virtual void addUniform(const QByteArray &name, const QByteArray &type);
89
90 virtual void addUniformArray(const QByteArray &name, const QByteArray &type, quint32 size);
91
92 virtual void addConstantBuffer(const QByteArray &name, const QByteArray &layout);
93 virtual void addConstantBufferParam(const QByteArray &cbName, const QByteArray &paramName, const QByteArray &type);
94
95 virtual QSSGStageGeneratorBase &operator<<(const QByteArray &data);
96 virtual void append(const QByteArray &data);
97 QSSGShaderGeneratorStage stage() const;
98
99 void addShaderPass2Marker(ShaderItemType itemType);
100
101 void addShaderItemMap(ShaderItemType itemType,
102 const TStrTableStrMap &itemMap,
103 const QByteArray &inItemSuffix = QByteArray());
104
105 void addShaderItemMap(ShaderItemType itemType,
106 const TStrTableSizedStrMap &itemMap);
107
108 virtual void addShaderIncomingMap();
109
110 virtual void addShaderUniformMap();
111
112 virtual void addShaderOutgoingMap();
113
114 virtual void addShaderConstantBufferItemMap(const QByteArray &itemType, const TStrTableStrMap &cbMap, TConstantBufferParamArray cbParamsArray);
115
116 virtual void appendShaderCode() final;
117
118 virtual void addInclude(const QByteArray &name) final;
119
120 void buildShaderSourcePass1(QSSGShaderResourceMergeContext *mergeContext);
121
122 QByteArray buildShaderSourcePass2(QSSGShaderResourceMergeContext *mergeContext);
123
124 virtual void addFunction(const QByteArray &functionName) final;
125
126 virtual void addDefinition(const QByteArray &name, const QByteArray &value) final;
127};
128
129struct Q_QUICK3DRUNTIMERENDER_EXPORT QSSGVertexShaderGenerator final : public QSSGStageGeneratorBase
130{
131 QSSGVertexShaderGenerator();
132};
133
134struct Q_QUICK3DRUNTIMERENDER_EXPORT QSSGFragmentShaderGenerator final : public QSSGStageGeneratorBase
135{
136 QSSGFragmentShaderGenerator();
137 void addShaderIncomingMap() override;
138 void addShaderOutgoingMap() override;
139};
140
141class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGProgramGenerator
142{
143 Q_DISABLE_COPY(QSSGProgramGenerator)
144public:
145 QSSGProgramGenerator() = default;
146 QSSGVertexShaderGenerator m_vs;
147 QSSGFragmentShaderGenerator m_fs;
148
149 QSSGShaderGeneratorStageFlags m_enabledStages;
150
151 static constexpr QSSGShaderGeneratorStageFlags defaultFlags() { return QSSGShaderGeneratorStageFlags(QSSGShaderGeneratorStage::Vertex | QSSGShaderGeneratorStage::Fragment); }
152
153 void linkStages();
154
155 void beginProgram(QSSGShaderGeneratorStageFlags inEnabledStages = defaultFlags());
156
157 QSSGShaderGeneratorStageFlags getEnabledStages() const;
158
159 QSSGStageGeneratorBase &internalGetStage(QSSGShaderGeneratorStage inStage);
160 // get the stage or nullptr if it has not been created.
161 QSSGStageGeneratorBase *getStage(QSSGShaderGeneratorStage inStage);
162
163 void registerShaderMetaDataFromSource(QSSGShaderResourceMergeContext *mergeContext,
164 const QByteArray &contents,
165 QSSGShaderGeneratorStage stage);
166
167 QSSGRhiShaderPipelinePtr compileGeneratedRhiShader(const QByteArray &inMaterialInfoString,
168 const QSSGShaderFeatures &inFeatureSet,
169 QSSGShaderLibraryManager &shaderLibraryManager,
170 QSSGShaderCache &theCache,
171 QSSGRhiShaderPipeline::StageFlags stageFlags);
172};
173
174QT_END_NAMESPACE
175#endif
176

source code of qtquick3d/src/runtimerender/qssgrendershadercodegenerator_p.h