Line data Source code
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 <stack>
12 : #include <tuple>
13 : #include <unordered_map>
14 :
15 : #include "SkSLCodeGenerator.h"
16 : #include "ir/SkSLBinaryExpression.h"
17 : #include "ir/SkSLBoolLiteral.h"
18 : #include "ir/SkSLConstructor.h"
19 : #include "ir/SkSLDoStatement.h"
20 : #include "ir/SkSLExtension.h"
21 : #include "ir/SkSLFloatLiteral.h"
22 : #include "ir/SkSLIfStatement.h"
23 : #include "ir/SkSLIndexExpression.h"
24 : #include "ir/SkSLInterfaceBlock.h"
25 : #include "ir/SkSLIntLiteral.h"
26 : #include "ir/SkSLFieldAccess.h"
27 : #include "ir/SkSLForStatement.h"
28 : #include "ir/SkSLFunctionCall.h"
29 : #include "ir/SkSLFunctionDeclaration.h"
30 : #include "ir/SkSLFunctionDefinition.h"
31 : #include "ir/SkSLPrefixExpression.h"
32 : #include "ir/SkSLPostfixExpression.h"
33 : #include "ir/SkSLProgramElement.h"
34 : #include "ir/SkSLReturnStatement.h"
35 : #include "ir/SkSLStatement.h"
36 : #include "ir/SkSLSwitchStatement.h"
37 : #include "ir/SkSLSwizzle.h"
38 : #include "ir/SkSLTernaryExpression.h"
39 : #include "ir/SkSLVarDeclarations.h"
40 : #include "ir/SkSLVarDeclarationsStatement.h"
41 : #include "ir/SkSLVariableReference.h"
42 : #include "ir/SkSLWhileStatement.h"
43 :
44 : namespace SkSL {
45 :
46 : #define kLast_Capability SpvCapabilityMultiViewport
47 :
48 : /**
49 : * Converts a Program into GLSL code.
50 : */
51 0 : class GLSLCodeGenerator : public CodeGenerator {
52 : public:
53 : enum Precedence {
54 : kParentheses_Precedence = 1,
55 : kPostfix_Precedence = 2,
56 : kPrefix_Precedence = 3,
57 : kMultiplicative_Precedence = 4,
58 : kAdditive_Precedence = 5,
59 : kShift_Precedence = 6,
60 : kRelational_Precedence = 7,
61 : kEquality_Precedence = 8,
62 : kBitwiseAnd_Precedence = 9,
63 : kBitwiseXor_Precedence = 10,
64 : kBitwiseOr_Precedence = 11,
65 : kLogicalAnd_Precedence = 12,
66 : kLogicalXor_Precedence = 13,
67 : kLogicalOr_Precedence = 14,
68 : kTernary_Precedence = 15,
69 : kAssignment_Precedence = 16,
70 : kSequence_Precedence = 17,
71 : kTopLevel_Precedence = 18
72 : };
73 :
74 0 : GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
75 : OutputStream* out)
76 0 : : INHERITED(program, errors, out)
77 0 : , fContext(*context) {}
78 :
79 : virtual bool generateCode() override;
80 :
81 : private:
82 : void write(const char* s);
83 :
84 : void writeLine();
85 :
86 : void writeLine(const char* s);
87 :
88 : void write(const String& s);
89 :
90 : void writeLine(const String& s);
91 :
92 : void writeType(const Type& type);
93 :
94 : void writeExtension(const Extension& ext);
95 :
96 : void writeInterfaceBlock(const InterfaceBlock& intf);
97 :
98 : void writeFunctionStart(const FunctionDeclaration& f);
99 :
100 : void writeFunctionDeclaration(const FunctionDeclaration& f);
101 :
102 : void writeFunction(const FunctionDefinition& f);
103 :
104 : void writeLayout(const Layout& layout);
105 :
106 : void writeModifiers(const Modifiers& modifiers, bool globalContext);
107 :
108 : void writeGlobalVars(const VarDeclaration& vs);
109 :
110 : void writeVarDeclarations(const VarDeclarations& decl, bool global);
111 :
112 : void writeFragCoord();
113 :
114 : 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 writeFunctionCall(const FunctionCall& c);
123 :
124 : void writeConstructor(const Constructor& c);
125 :
126 : void writeFieldAccess(const FieldAccess& f);
127 :
128 : void writeSwizzle(const Swizzle& swizzle);
129 :
130 : void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
131 :
132 : void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
133 :
134 : void writeIndexExpression(const IndexExpression& expr);
135 :
136 : void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
137 :
138 : void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
139 :
140 : void writeBoolLiteral(const BoolLiteral& b);
141 :
142 : void writeIntLiteral(const IntLiteral& i);
143 :
144 : void writeFloatLiteral(const FloatLiteral& f);
145 :
146 : void writeStatement(const Statement& s);
147 :
148 : void writeBlock(const Block& b);
149 :
150 : void writeIfStatement(const IfStatement& stmt);
151 :
152 : void writeForStatement(const ForStatement& f);
153 :
154 : void writeWhileStatement(const WhileStatement& w);
155 :
156 : void writeDoStatement(const DoStatement& d);
157 :
158 : void writeSwitchStatement(const SwitchStatement& s);
159 :
160 : void writeReturnStatement(const ReturnStatement& r);
161 :
162 : const Context& fContext;
163 : StringStream fHeader;
164 : String fFunctionHeader;
165 : Program::Kind fProgramKind;
166 : int fVarCount = 0;
167 : int fIndentation = 0;
168 : bool fAtLineStart = false;
169 : // Keeps track of which struct types we have written. Given that we are unlikely to ever write
170 : // more than one or two structs per shader, a simple linear search will be faster than anything
171 : // fancier.
172 : std::vector<const Type*> fWrittenStructs;
173 : // true if we have run into usages of dFdx / dFdy
174 : bool fFoundDerivatives = false;
175 : bool fFoundImageDecl = false;
176 : bool fSetupFragPositionGlobal = false;
177 : bool fSetupFragPositionLocal = false;
178 :
179 : typedef CodeGenerator INHERITED;
180 : };
181 :
182 : }
183 :
184 : #endif
|