blob: 99e7d47505c72d6bbf1c61dadea3f23f4938c748 [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
15#include "SkSLCodeGenerator.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040016#include "SkSLStringStream.h"
ethannicholasf789b382016-08-03 12:43:36 -070017#include "ir/SkSLBinaryExpression.h"
18#include "ir/SkSLBoolLiteral.h"
19#include "ir/SkSLConstructor.h"
20#include "ir/SkSLDoStatement.h"
21#include "ir/SkSLExtension.h"
22#include "ir/SkSLFloatLiteral.h"
23#include "ir/SkSLIfStatement.h"
24#include "ir/SkSLIndexExpression.h"
25#include "ir/SkSLInterfaceBlock.h"
26#include "ir/SkSLIntLiteral.h"
27#include "ir/SkSLFieldAccess.h"
28#include "ir/SkSLForStatement.h"
29#include "ir/SkSLFunctionCall.h"
30#include "ir/SkSLFunctionDeclaration.h"
31#include "ir/SkSLFunctionDefinition.h"
32#include "ir/SkSLPrefixExpression.h"
33#include "ir/SkSLPostfixExpression.h"
34#include "ir/SkSLProgramElement.h"
35#include "ir/SkSLReturnStatement.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040036#include "ir/SkSLSetting.h"
ethannicholasf789b382016-08-03 12:43:36 -070037#include "ir/SkSLStatement.h"
Ethan Nicholasaf197692017-02-27 13:26:45 -050038#include "ir/SkSLSwitchStatement.h"
ethannicholasf789b382016-08-03 12:43:36 -070039#include "ir/SkSLSwizzle.h"
40#include "ir/SkSLTernaryExpression.h"
ethannicholas22f939e2016-10-13 13:25:34 -070041#include "ir/SkSLVarDeclarations.h"
42#include "ir/SkSLVarDeclarationsStatement.h"
ethannicholasf789b382016-08-03 12:43:36 -070043#include "ir/SkSLVariableReference.h"
44#include "ir/SkSLWhileStatement.h"
45
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:
ethannicholasf789b382016-08-03 12:43:36 -070086 void write(const char* s);
87
88 void writeLine();
89
90 void writeLine(const char* s);
91
Ethan Nicholas0df1b042017-03-31 13:56:23 -040092 void write(const String& s);
ethannicholasf789b382016-08-03 12:43:36 -070093
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070094 void write(StringFragment s);
95
Ethan Nicholas0df1b042017-03-31 13:56:23 -040096 void writeLine(const String& s);
ethannicholasf789b382016-08-03 12:43:36 -070097
Ethan Nicholas762466e2017-06-29 10:03:38 -040098 virtual void writeHeader();
99
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400100 virtual bool usesPrecisionModifiers() const;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400101
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400102 virtual String getTypeName(const Type& type);
103
104 void writeType(const Type& type);
ethannicholasf789b382016-08-03 12:43:36 -0700105
Ethan Nicholas88f6d372018-07-27 10:03:46 -0400106 void writeExtension(const String& name);
107
108 void writeExtension(const String& name, bool require);
ethannicholasf789b382016-08-03 12:43:36 -0700109
110 void writeInterfaceBlock(const InterfaceBlock& intf);
111
112 void writeFunctionStart(const FunctionDeclaration& f);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400113
ethannicholasf789b382016-08-03 12:43:36 -0700114 void writeFunctionDeclaration(const FunctionDeclaration& f);
115
Ethan Nicholas762466e2017-06-29 10:03:38 -0400116 virtual void writeFunction(const FunctionDefinition& f);
ethannicholasf789b382016-08-03 12:43:36 -0700117
118 void writeLayout(const Layout& layout);
119
ethannicholas5961bc92016-10-12 06:39:56 -0700120 void writeModifiers(const Modifiers& modifiers, bool globalContext);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500121
Ethan Nicholascd700e92018-08-24 16:43:57 -0400122 virtual void writeInputVars();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000123
Ethan Nicholas762466e2017-06-29 10:03:38 -0400124 virtual void writeVarInitializer(const Variable& var, const Expression& value);
125
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400126 const char* getTypePrecision(const Type& type);
127
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400128 void writeTypePrecision(const Type& type);
129
ethannicholas5961bc92016-10-12 06:39:56 -0700130 void writeVarDeclarations(const VarDeclarations& decl, bool global);
ethannicholasf789b382016-08-03 12:43:36 -0700131
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500132 void writeFragCoord();
133
Ethan Nicholas762466e2017-06-29 10:03:38 -0400134 virtual void writeVariableReference(const VariableReference& ref);
ethannicholasf789b382016-08-03 12:43:36 -0700135
136 void writeExpression(const Expression& expr, Precedence parentPrecedence);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500137
ethannicholasf789b382016-08-03 12:43:36 -0700138 void writeIntrinsicCall(const FunctionCall& c);
139
ethannicholas5961bc92016-10-12 06:39:56 -0700140 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
141
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500142 void writeDeterminantHack(const Expression& mat);
143
144 void writeInverseHack(const Expression& mat);
145
146 void writeTransposeHack(const Expression& mat);
147
148 void writeInverseSqrtHack(const Expression& x);
149
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400150 virtual void writeFunctionCall(const FunctionCall& c);
ethannicholasf789b382016-08-03 12:43:36 -0700151
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400152 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700153
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400154 virtual void writeFieldAccess(const FieldAccess& f);
ethannicholasf789b382016-08-03 12:43:36 -0700155
Ethan Nicholas82399462017-10-16 12:35:44 -0400156 virtual void writeSwizzle(const Swizzle& swizzle);
ethannicholasf789b382016-08-03 12:43:36 -0700157
Ethan Nicholas762466e2017-06-29 10:03:38 -0400158 static Precedence GetBinaryPrecedence(Token::Kind op);
159
160 virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
Adrienne Walkerc02165f2018-08-21 11:08:11 -0700161 void writeShortCircuitWorkaroundExpression(const BinaryExpression& b,
162 Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700163
164 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
165
Ethan Nicholas762466e2017-06-29 10:03:38 -0400166 virtual void writeIndexExpression(const IndexExpression& expr);
ethannicholasf789b382016-08-03 12:43:36 -0700167
168 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
169
170 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
171
172 void writeBoolLiteral(const BoolLiteral& b);
173
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400174 virtual void writeIntLiteral(const IntLiteral& i);
ethannicholasf789b382016-08-03 12:43:36 -0700175
176 void writeFloatLiteral(const FloatLiteral& f);
177
Ethan Nicholas762466e2017-06-29 10:03:38 -0400178 virtual void writeSetting(const Setting& s);
179
ethannicholasf789b382016-08-03 12:43:36 -0700180 void writeStatement(const Statement& s);
181
Ethan Nicholascb670962017-04-20 19:31:52 -0400182 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
183
ethannicholasf789b382016-08-03 12:43:36 -0700184 void writeBlock(const Block& b);
185
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400186 virtual void writeIfStatement(const IfStatement& stmt);
ethannicholasf789b382016-08-03 12:43:36 -0700187
188 void writeForStatement(const ForStatement& f);
189
190 void writeWhileStatement(const WhileStatement& w);
191
192 void writeDoStatement(const DoStatement& d);
193
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400194 virtual void writeSwitchStatement(const SwitchStatement& s);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500195
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400196 virtual void writeReturnStatement(const ReturnStatement& r);
ethannicholasf789b382016-08-03 12:43:36 -0700197
Ethan Nicholas762466e2017-06-29 10:03:38 -0400198 virtual void writeProgramElement(const ProgramElement& e);
199
200 const char* fLineEnding;
ethannicholasf789b382016-08-03 12:43:36 -0700201 const Context& fContext;
Ethan Nicholas88f6d372018-07-27 10:03:46 -0400202 StringStream fExtensions;
203 StringStream fGlobals;
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500204 StringStream fExtraFunctions;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400205 String fFunctionHeader;
ethannicholas5961bc92016-10-12 06:39:56 -0700206 Program::Kind fProgramKind;
ethannicholasddb37d62016-10-20 09:54:00 -0700207 int fVarCount = 0;
208 int fIndentation = 0;
209 bool fAtLineStart = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500210 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
211 // more than one or two structs per shader, a simple linear search will be faster than anything
ethannicholasf789b382016-08-03 12:43:36 -0700212 // fancier.
213 std::vector<const Type*> fWrittenStructs;
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500214 std::set<String> fWrittenIntrinsics;
ethannicholasddb37d62016-10-20 09:54:00 -0700215 // true if we have run into usages of dFdx / dFdy
216 bool fFoundDerivatives = false;
Brian Salomon2a51de82016-11-16 12:06:01 -0500217 bool fFoundImageDecl = false;
Brian Osman4b2f9152018-04-17 11:19:57 -0400218 bool fFoundExternalSamplerDecl = false;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600219 bool fFoundGSInvocations = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500220 bool fSetupFragPositionGlobal = false;
221 bool fSetupFragPositionLocal = false;
Brian Salomondba65f92018-01-22 08:43:38 -0500222 bool fSetupFragCoordWorkaround = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500223
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400224 // We map function names to function class so we can quickly deal with function calls that need
225 // extra processing
226 enum class FunctionClass {
Adrienne Walker92b161f2018-08-22 10:41:52 -0700227 kAbs,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400228 kAtan,
229 kDeterminant,
230 kDerivative,
Chris Daltona7086182018-11-16 09:33:43 -0500231 kFMA,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400232 kFract,
233 kInverse,
234 kInverseSqrt,
235 kMin,
Adrienne Walker2f4c09b2018-08-22 16:04:57 -0700236 kPow,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400237 kSaturate,
238 kTexture,
239 kTranspose
240 };
241 static std::unordered_map<StringFragment, FunctionClass>* fFunctionClasses;
242
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500243 typedef CodeGenerator INHERITED;
ethannicholasf789b382016-08-03 12:43:36 -0700244};
245
246}
247
248#endif