Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / sksl / codegen / SkSLGLSLCodeGenerator.h
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8 #ifndef SKSL_GLSLCODEGENERATOR
9 #define SKSL_GLSLCODEGENERATOR
10
11 #include "include/sksl/SkSLOperator.h"
12 #include "src/sksl/SkSLContext.h"
13 #include "src/sksl/SkSLStringStream.h"
14 #include "src/sksl/codegen/SkSLCodeGenerator.h"
15
16 #include <set>
17 #include <string>
18 #include <string_view>
19
20 namespace SkSL {
21
22 class AnyConstructor;
23 class BinaryExpression;
24 class Block;
25 class ConstructorDiagonalMatrix;
26 class DoStatement;
27 class Expression;
28 class ExpressionStatement;
29 class FieldAccess;
30 class ForStatement;
31 class FunctionCall;
32 class FunctionDeclaration;
33 class FunctionDefinition;
34 class FunctionPrototype;
35 class IfStatement;
36 class InterfaceBlock;
37 class Literal;
38 class OutputStream;
39 class PostfixExpression;
40 class PrefixExpression;
41 class ProgramElement;
42 class ReturnStatement;
43 class Setting;
44 class Statement;
45 class StructDefinition;
46 class SwitchStatement;
47 class TernaryExpression;
48 class Type;
49 class VarDeclaration;
50 class Variable;
51 class VariableReference;
52 struct IndexExpression;
53 struct Layout;
54 struct Modifiers;
55 struct Program;
56 struct ShaderCaps;
57 struct Swizzle;
58
59 /**
60  * Converts a Program into GLSL code.
61  */
62 class GLSLCodeGenerator : public CodeGenerator {
63 public:
64     GLSLCodeGenerator(const Context* context, const Program* program, OutputStream* out)
65     : INHERITED(context, program, out) {}
66
67     bool generateCode() override;
68
69 protected:
70     using Precedence = Operator::Precedence;
71
72     void write(std::string_view s);
73
74     void writeLine(std::string_view s = std::string_view());
75
76     void finishLine();
77
78     virtual void writeHeader();
79
80     virtual bool usesPrecisionModifiers() const;
81
82     virtual std::string getTypeName(const Type& type);
83
84     void writeStructDefinition(const StructDefinition& s);
85
86     void writeType(const Type& type);
87
88     void writeExtension(std::string_view name, bool require = true);
89
90     void writeInterfaceBlock(const InterfaceBlock& intf);
91
92     void writeFunctionDeclaration(const FunctionDeclaration& f);
93
94     void writeFunctionPrototype(const FunctionPrototype& f);
95
96     virtual void writeFunction(const FunctionDefinition& f);
97
98     void writeLayout(const Layout& layout);
99
100     void writeModifiers(const Modifiers& modifiers, bool globalContext);
101
102     virtual void writeInputVars();
103
104     virtual void writeVarInitializer(const Variable& var, const Expression& value);
105
106     const char* getTypePrecision(const Type& type);
107
108     void writeTypePrecision(const Type& type);
109
110     void writeVarDeclaration(const VarDeclaration& var, bool global);
111
112     void writeFragCoord();
113
114     virtual void writeVariableReference(const VariableReference& ref);
115
116     void writeExpression(const Expression& expr, Precedence parentPrecedence);
117
118     void writeIntrinsicCall(const FunctionCall& c);
119
120     void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
121
122     void writeDeterminantHack(const Expression& mat);
123
124     void writeInverseHack(const Expression& mat);
125
126     void writeTransposeHack(const Expression& mat);
127
128     void writeInverseSqrtHack(const Expression& x);
129
130     void writeMatrixComparisonWorkaround(const BinaryExpression& x);
131
132     virtual void writeFunctionCall(const FunctionCall& c);
133
134     void writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c,
135                                         Precedence parentPrecedence);
136
137     virtual void writeAnyConstructor(const AnyConstructor& c, Precedence parentPrecedence);
138
139     virtual void writeCastConstructor(const AnyConstructor& c, Precedence parentPrecedence);
140
141     virtual void writeFieldAccess(const FieldAccess& f);
142
143     virtual void writeSwizzle(const Swizzle& swizzle);
144
145     virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
146
147     void writeShortCircuitWorkaroundExpression(const BinaryExpression& b,
148                                                Precedence parentPrecedence);
149
150     virtual void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
151
152     virtual void writeIndexExpression(const IndexExpression& expr);
153
154     void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
155
156     void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
157
158     virtual void writeLiteral(const Literal& l);
159
160     virtual void writeSetting(const Setting& s);
161
162     void writeStatement(const Statement& s);
163
164     void writeBlock(const Block& b);
165
166     virtual void writeIfStatement(const IfStatement& stmt);
167
168     void writeForStatement(const ForStatement& f);
169
170     void writeDoStatement(const DoStatement& d);
171
172     void writeExpressionStatement(const ExpressionStatement& s);
173
174     virtual void writeSwitchStatement(const SwitchStatement& s);
175
176     virtual void writeReturnStatement(const ReturnStatement& r);
177
178     virtual void writeProgramElement(const ProgramElement& e);
179
180     const ShaderCaps& caps() const { return fContext.fCaps; }
181
182     StringStream fExtensions;
183     StringStream fGlobals;
184     StringStream fExtraFunctions;
185     std::string fFunctionHeader;
186     int fVarCount = 0;
187     int fIndentation = 0;
188     bool fAtLineStart = false;
189     std::set<std::string> fWrittenIntrinsics;
190     // true if we have run into usages of dFdx / dFdy
191     bool fFoundDerivatives = false;
192     bool fFoundExternalSamplerDecl = false;
193     bool fFoundRectSamplerDecl = false;
194     bool fSetupClockwise = false;
195     bool fSetupFragPosition = false;
196     bool fSetupFragCoordWorkaround = false;
197
198     using INHERITED = CodeGenerator;
199 };
200
201 }  // namespace SkSL
202
203 #endif