blob: 9977cf8dcf8c53ff0b673169d43c3820f67d703d [file] [log] [blame]
ethannicholasf789b382016-08-03 12:43:36 -07001/*
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 */
Ethan Nicholas941e7e22016-12-12 15:33:30 -05007
ethannicholasf789b382016-08-03 12:43:36 -07008#ifndef SKSL_GLSLCODEGENERATOR
9#define SKSL_GLSLCODEGENERATOR
10
11#include <stack>
12#include <tuple>
13#include <unordered_map>
14
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/sksl/SkSLCodeGenerator.h"
16#include "src/sksl/SkSLStringStream.h"
17#include "src/sksl/ir/SkSLBinaryExpression.h"
18#include "src/sksl/ir/SkSLBoolLiteral.h"
19#include "src/sksl/ir/SkSLConstructor.h"
20#include "src/sksl/ir/SkSLDoStatement.h"
21#include "src/sksl/ir/SkSLExtension.h"
22#include "src/sksl/ir/SkSLFieldAccess.h"
23#include "src/sksl/ir/SkSLFloatLiteral.h"
24#include "src/sksl/ir/SkSLForStatement.h"
25#include "src/sksl/ir/SkSLFunctionCall.h"
26#include "src/sksl/ir/SkSLFunctionDeclaration.h"
27#include "src/sksl/ir/SkSLFunctionDefinition.h"
28#include "src/sksl/ir/SkSLIfStatement.h"
29#include "src/sksl/ir/SkSLIndexExpression.h"
30#include "src/sksl/ir/SkSLIntLiteral.h"
31#include "src/sksl/ir/SkSLInterfaceBlock.h"
32#include "src/sksl/ir/SkSLPostfixExpression.h"
33#include "src/sksl/ir/SkSLPrefixExpression.h"
34#include "src/sksl/ir/SkSLProgramElement.h"
35#include "src/sksl/ir/SkSLReturnStatement.h"
36#include "src/sksl/ir/SkSLSetting.h"
37#include "src/sksl/ir/SkSLStatement.h"
38#include "src/sksl/ir/SkSLSwitchStatement.h"
39#include "src/sksl/ir/SkSLSwizzle.h"
40#include "src/sksl/ir/SkSLTernaryExpression.h"
41#include "src/sksl/ir/SkSLVarDeclarations.h"
42#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
43#include "src/sksl/ir/SkSLVariableReference.h"
44#include "src/sksl/ir/SkSLWhileStatement.h"
ethannicholasf789b382016-08-03 12:43:36 -070045
46namespace SkSL {
47
48#define kLast_Capability SpvCapabilityMultiViewport
49
ethannicholasf789b382016-08-03 12:43:36 -070050/**
51 * Converts a Program into GLSL code.
52 */
53class GLSLCodeGenerator : public CodeGenerator {
54public:
55 enum Precedence {
56 kParentheses_Precedence = 1,
57 kPostfix_Precedence = 2,
58 kPrefix_Precedence = 3,
59 kMultiplicative_Precedence = 4,
60 kAdditive_Precedence = 5,
61 kShift_Precedence = 6,
62 kRelational_Precedence = 7,
63 kEquality_Precedence = 8,
64 kBitwiseAnd_Precedence = 9,
65 kBitwiseXor_Precedence = 10,
66 kBitwiseOr_Precedence = 11,
67 kLogicalAnd_Precedence = 12,
68 kLogicalXor_Precedence = 13,
69 kLogicalOr_Precedence = 14,
70 kTernary_Precedence = 15,
71 kAssignment_Precedence = 16,
72 kSequence_Precedence = 17,
Ethan Nicholas4b330df2017-05-17 10:52:55 -040073 kTopLevel_Precedence = kSequence_Precedence
ethannicholasf789b382016-08-03 12:43:36 -070074 };
75
Ethan Nicholas941e7e22016-12-12 15:33:30 -050076 GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas0df1b042017-03-31 13:56:23 -040077 OutputStream* out)
Ethan Nicholas941e7e22016-12-12 15:33:30 -050078 : INHERITED(program, errors, out)
Ethan Nicholas762466e2017-06-29 10:03:38 -040079 , fLineEnding("\n")
Ethan Nicholasa45e1a72018-08-31 16:56:52 -040080 , fContext(*context)
81 , fProgramKind(program->fKind) {}
ethannicholasf789b382016-08-03 12:43:36 -070082
Ethan Nicholas762466e2017-06-29 10:03:38 -040083 bool generateCode() override;
ethannicholasf789b382016-08-03 12:43:36 -070084
Ethan Nicholas762466e2017-06-29 10:03:38 -040085protected:
Ethan Nicholase455f652019-09-13 12:52:55 -040086 enum class SwizzleOrder {
87 MASK_FIRST,
88 CONSTANTS_FIRST
89 };
90
ethannicholasf789b382016-08-03 12:43:36 -070091 void write(const char* s);
92
93 void writeLine();
94
95 void writeLine(const char* s);
96
Ethan Nicholas0df1b042017-03-31 13:56:23 -040097 void write(const String& s);
ethannicholasf789b382016-08-03 12:43:36 -070098
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070099 void write(StringFragment s);
100
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400101 void writeLine(const String& s);
ethannicholasf789b382016-08-03 12:43:36 -0700102
Ethan Nicholas762466e2017-06-29 10:03:38 -0400103 virtual void writeHeader();
104
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400105 virtual bool usesPrecisionModifiers() const;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400106
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400107 virtual String getTypeName(const Type& type);
108
109 void writeType(const Type& type);
ethannicholasf789b382016-08-03 12:43:36 -0700110
Ethan Nicholas88f6d372018-07-27 10:03:46 -0400111 void writeExtension(const String& name);
112
113 void writeExtension(const String& name, bool require);
ethannicholasf789b382016-08-03 12:43:36 -0700114
115 void writeInterfaceBlock(const InterfaceBlock& intf);
116
117 void writeFunctionStart(const FunctionDeclaration& f);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400118
ethannicholasf789b382016-08-03 12:43:36 -0700119 void writeFunctionDeclaration(const FunctionDeclaration& f);
120
Ethan Nicholas762466e2017-06-29 10:03:38 -0400121 virtual void writeFunction(const FunctionDefinition& f);
ethannicholasf789b382016-08-03 12:43:36 -0700122
123 void writeLayout(const Layout& layout);
124
ethannicholas5961bc92016-10-12 06:39:56 -0700125 void writeModifiers(const Modifiers& modifiers, bool globalContext);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500126
Ethan Nicholascd700e92018-08-24 16:43:57 -0400127 virtual void writeInputVars();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000128
Ethan Nicholas762466e2017-06-29 10:03:38 -0400129 virtual void writeVarInitializer(const Variable& var, const Expression& value);
130
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400131 const char* getTypePrecision(const Type& type);
132
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400133 void writeTypePrecision(const Type& type);
134
ethannicholas5961bc92016-10-12 06:39:56 -0700135 void writeVarDeclarations(const VarDeclarations& decl, bool global);
ethannicholasf789b382016-08-03 12:43:36 -0700136
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500137 void writeFragCoord();
138
Ethan Nicholas762466e2017-06-29 10:03:38 -0400139 virtual void writeVariableReference(const VariableReference& ref);
ethannicholasf789b382016-08-03 12:43:36 -0700140
141 void writeExpression(const Expression& expr, Precedence parentPrecedence);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500142
ethannicholasf789b382016-08-03 12:43:36 -0700143 void writeIntrinsicCall(const FunctionCall& c);
144
ethannicholas5961bc92016-10-12 06:39:56 -0700145 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
146
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500147 void writeDeterminantHack(const Expression& mat);
148
149 void writeInverseHack(const Expression& mat);
150
151 void writeTransposeHack(const Expression& mat);
152
153 void writeInverseSqrtHack(const Expression& x);
154
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400155 virtual void writeFunctionCall(const FunctionCall& c);
ethannicholasf789b382016-08-03 12:43:36 -0700156
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400157 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700158
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400159 virtual void writeFieldAccess(const FieldAccess& f);
ethannicholasf789b382016-08-03 12:43:36 -0700160
Ethan Nicholase455f652019-09-13 12:52:55 -0400161 void writeConstantSwizzle(const Swizzle& swizzle, const String& constants);
162
163 void writeSwizzleMask(const Swizzle& swizzle, const String& mask);
164
165 void writeSwizzleConstructor(const Swizzle& swizzle, const String& constants,
166 const String& mask, SwizzleOrder order);
167
168 void writeSwizzleConstructor(const Swizzle& swizzle, const String& constants,
169 const String& mask, const String& reswizzle);
Ethan Nicholas82399462017-10-16 12:35:44 -0400170 virtual void writeSwizzle(const Swizzle& swizzle);
ethannicholasf789b382016-08-03 12:43:36 -0700171
Ethan Nicholas762466e2017-06-29 10:03:38 -0400172 static Precedence GetBinaryPrecedence(Token::Kind op);
173
174 virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
Adrienne Walkerc02165f2018-08-21 11:08:11 -0700175 void writeShortCircuitWorkaroundExpression(const BinaryExpression& b,
176 Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700177
178 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
179
Ethan Nicholas762466e2017-06-29 10:03:38 -0400180 virtual void writeIndexExpression(const IndexExpression& expr);
ethannicholasf789b382016-08-03 12:43:36 -0700181
182 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
183
184 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
185
186 void writeBoolLiteral(const BoolLiteral& b);
187
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400188 virtual void writeIntLiteral(const IntLiteral& i);
ethannicholasf789b382016-08-03 12:43:36 -0700189
190 void writeFloatLiteral(const FloatLiteral& f);
191
Ethan Nicholas762466e2017-06-29 10:03:38 -0400192 virtual void writeSetting(const Setting& s);
193
ethannicholasf789b382016-08-03 12:43:36 -0700194 void writeStatement(const Statement& s);
195
Ethan Nicholascb670962017-04-20 19:31:52 -0400196 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
197
ethannicholasf789b382016-08-03 12:43:36 -0700198 void writeBlock(const Block& b);
199
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400200 virtual void writeIfStatement(const IfStatement& stmt);
ethannicholasf789b382016-08-03 12:43:36 -0700201
202 void writeForStatement(const ForStatement& f);
203
204 void writeWhileStatement(const WhileStatement& w);
205
206 void writeDoStatement(const DoStatement& d);
207
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400208 virtual void writeSwitchStatement(const SwitchStatement& s);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500209
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400210 virtual void writeReturnStatement(const ReturnStatement& r);
ethannicholasf789b382016-08-03 12:43:36 -0700211
Ethan Nicholas762466e2017-06-29 10:03:38 -0400212 virtual void writeProgramElement(const ProgramElement& e);
213
214 const char* fLineEnding;
ethannicholasf789b382016-08-03 12:43:36 -0700215 const Context& fContext;
Ethan Nicholas88f6d372018-07-27 10:03:46 -0400216 StringStream fExtensions;
217 StringStream fGlobals;
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500218 StringStream fExtraFunctions;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400219 String fFunctionHeader;
ethannicholas5961bc92016-10-12 06:39:56 -0700220 Program::Kind fProgramKind;
ethannicholasddb37d62016-10-20 09:54:00 -0700221 int fVarCount = 0;
222 int fIndentation = 0;
223 bool fAtLineStart = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500224 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
225 // more than one or two structs per shader, a simple linear search will be faster than anything
ethannicholasf789b382016-08-03 12:43:36 -0700226 // fancier.
227 std::vector<const Type*> fWrittenStructs;
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500228 std::set<String> fWrittenIntrinsics;
ethannicholasddb37d62016-10-20 09:54:00 -0700229 // true if we have run into usages of dFdx / dFdy
230 bool fFoundDerivatives = false;
Brian Osman4b2f9152018-04-17 11:19:57 -0400231 bool fFoundExternalSamplerDecl = false;
Brian Salomon67529b22019-08-13 15:31:04 -0400232 bool fFoundRectSamplerDecl = false;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600233 bool fFoundGSInvocations = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500234 bool fSetupFragPositionGlobal = false;
235 bool fSetupFragPositionLocal = false;
Brian Salomondba65f92018-01-22 08:43:38 -0500236 bool fSetupFragCoordWorkaround = false;
Ethan Nicholas13863662019-07-29 13:05:15 -0400237 // if non-empty, replace all texture / texture2D / textureProj / etc. calls with this name
238 String fTextureFunctionOverride;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500239
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400240 // We map function names to function class so we can quickly deal with function calls that need
241 // extra processing
242 enum class FunctionClass {
Adrienne Walker92b161f2018-08-22 10:41:52 -0700243 kAbs,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400244 kAtan,
245 kDeterminant,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700246 kDFdx,
247 kDFdy,
248 kFwidth,
Chris Daltona7086182018-11-16 09:33:43 -0500249 kFMA,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400250 kFract,
251 kInverse,
252 kInverseSqrt,
253 kMin,
Adrienne Walker2f4c09b2018-08-22 16:04:57 -0700254 kPow,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400255 kSaturate,
256 kTexture,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -0400257 kTranspose
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400258 };
259 static std::unordered_map<StringFragment, FunctionClass>* fFunctionClasses;
260
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500261 typedef CodeGenerator INHERITED;
ethannicholasf789b382016-08-03 12:43:36 -0700262};
263
264}
265
266#endif