1 /****************************************************************************
2 **
3 ** Copyright (C) 2008-2012 NVIDIA Corporation.
4 ** Copyright (C) 2019 The Qt Company Ltd.
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of Qt Quick 3D.
8 **
9 ** $QT_BEGIN_LICENSE:GPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU
20 ** General Public License version 3 or (at your option) any later version
21 ** approved by the KDE Free Qt Foundation. The licenses are as published by
22 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
23 ** included in the packaging of this file. Please review the following
24 ** information to ensure the GNU General Public License requirements will
25 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
26 **
27 ** $QT_END_LICENSE$
28 **
29 ****************************************************************************/
30 
31 #ifndef QSSG_RENDER_SHADER_CODE_GENERATOR_H
32 #define QSSG_RENDER_SHADER_CODE_GENERATOR_H
33 
34 //
35 //  W A R N I N G
36 //  -------------
37 //
38 // This file is not part of the Qt API.  It exists purely as an
39 // implementation detail.  This header file may change from version to
40 // version without notice, or even be removed.
41 //
42 // We mean it.
43 //
44 
45 #include <QtQuick3DRender/private/qssgrenderbasetypes_p.h>
46 
47 QT_BEGIN_NAMESPACE
48 
49 typedef QPair<QByteArray, QByteArray> TParamPair;
50 typedef QPair<QByteArray, TParamPair> TConstantBufferParamPair;
51 typedef QVector<TConstantBufferParamPair> TConstantBufferParamArray;
52 typedef QHash<QByteArray, QByteArray> TStrTableStrMap;
53 
54 struct QSSGShaderCodeGeneratorBase
55 {
56     enum Enum {
57         Unknown = 0,
58         Lighting,
59         ViewVector,
60         WorldNormal,
61         WorldPosition,
62         EnvMapReflection,
63         UVCoords,
64     };
65     QSet<quint32> m_codes; // set of enums we have included.
66     QSet<QByteArray> m_includes;
67     TStrTableStrMap m_uniforms;
68     TStrTableStrMap m_constantBuffers;
69     TConstantBufferParamArray m_constantBufferParams;
70     TStrTableStrMap m_attributes;
71     QByteArray m_finalShaderBuilder;
72     QByteArray m_codeBuilder;
73     QSSGRenderContextType m_renderContextType;
74 
75     QSSGShaderCodeGeneratorBase(const QSSGRenderContextType &ctxType);
76     virtual ~QSSGShaderCodeGeneratorBase();
77     virtual TStrTableStrMap &getVaryings() = 0;
78     void begin();
79     void append(const QByteArray &data);
80     // don't add the newline
81     void addConstantBuffer(const QByteArray &name, const QByteArray &layout);
82     void addConstantBufferParam(const QByteArray &cbName, const QByteArray &paramName, const QByteArray &type);
83     void addUniform(const QByteArray &name, const QByteArray &type);
84     void addAttribute(const QByteArray &name, const QByteArray &type);
85     void addVarying(const QByteArray &name, const QByteArray &type);
86     void addLocalVariable(const QByteArray &name, const QByteArray &type, int tabCount = 1);
87     void addInclude(const QByteArray &name);
88     bool hasCode(Enum value);
89     void setCode(Enum value);
90     void setupWorldPosition();
91     void generateViewVector();
92     void generateWorldNormal();
93     void generateEnvMapReflection(QSSGShaderCodeGeneratorBase &inFragmentShader);
94     void generateUVCoords();
95     void generateTextureSwizzle(QSSGRenderTextureSwizzleMode swizzleMode, QByteArray &texSwizzle, QByteArray &lookupSwizzle);
96     void generateShadedWireframeBase();
97     void addLighting();
98     QByteArray buildShaderSource();
99     QSSGShaderCodeGeneratorBase &operator<<(const QByteArray &data);
100 
101 protected:
102     virtual void addShaderItemMap(const QByteArray &itemType, const TStrTableStrMap &itemMap);
103     void addShaderConstantBufferItemMap(const QByteArray &itemType, const TStrTableStrMap &cbMap, TConstantBufferParamArray cbParamsArray);
104 };
105 
106 struct QSSGShaderVertexCodeGenerator : public QSSGShaderCodeGeneratorBase
107 {
108     TStrTableStrMap m_varyings;
109     QSSGShaderVertexCodeGenerator(const QSSGRenderContextType &ctxType);
110     TStrTableStrMap &getVaryings() override;
111 };
112 
113 struct QSSGShaderTessControlCodeGenerator : public QSSGShaderCodeGeneratorBase
114 {
115     QSSGShaderVertexCodeGenerator &m_vertGenerator;
116     TStrTableStrMap m_varyings;
117     QSSGShaderTessControlCodeGenerator(QSSGShaderVertexCodeGenerator &vert, const QSSGRenderContextType &ctxType);
118 
119     void addShaderItemMap(const QByteArray &itemType, const TStrTableStrMap &itemMap) override;
120     TStrTableStrMap &getVaryings() override;
121 };
122 
123 struct QSSGShaderTessEvalCodeGenerator : public QSSGShaderCodeGeneratorBase
124 {
125     QSSGShaderTessControlCodeGenerator &m_tessControlGenerator;
126     bool m_hasGeometryStage = true;
127 
128     QSSGShaderTessEvalCodeGenerator(QSSGShaderTessControlCodeGenerator &tc, const QSSGRenderContextType &ctxType);
129 
130     void addShaderItemMap(const QByteArray &itemType, const TStrTableStrMap &itemMap) override;
131     TStrTableStrMap &getVaryings() override;
132     virtual void setGeometryStage(bool hasGeometryStage);
133 };
134 
135 struct QSSGShaderGeometryCodeGenerator : public QSSGShaderCodeGeneratorBase
136 {
137     QSSGShaderVertexCodeGenerator &m_vertGenerator;
138     bool m_hasTessellationStage;
139 
140     QSSGShaderGeometryCodeGenerator(QSSGShaderVertexCodeGenerator &vert, const QSSGRenderContextType &ctxType);
141 
142     void addShaderItemMap(const QByteArray &itemType, const TStrTableStrMap &itemMap) override;
143     TStrTableStrMap &getVaryings() override;
144     virtual void setTessellationStage(bool hasTessellationStage);
145 };
146 
147 struct QSSGShaderFragmentCodeGenerator : public QSSGShaderCodeGeneratorBase
148 {
149     QSSGShaderVertexCodeGenerator &m_vertGenerator;
150     QSSGShaderFragmentCodeGenerator(QSSGShaderVertexCodeGenerator &vert, const QSSGRenderContextType &ctxType);
151     TStrTableStrMap &getVaryings() override;
152 };
153 QT_END_NAMESPACE
154 
155 #endif
156