blob: 917c04f791bc42782edbef0ad1c9311a5e41ad7d [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"
John Stiles569249b2020-11-03 12:18:22 -050028#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/sksl/ir/SkSLIfStatement.h"
30#include "src/sksl/ir/SkSLIndexExpression.h"
31#include "src/sksl/ir/SkSLIntLiteral.h"
32#include "src/sksl/ir/SkSLInterfaceBlock.h"
33#include "src/sksl/ir/SkSLPostfixExpression.h"
34#include "src/sksl/ir/SkSLPrefixExpression.h"
35#include "src/sksl/ir/SkSLProgramElement.h"
36#include "src/sksl/ir/SkSLReturnStatement.h"
37#include "src/sksl/ir/SkSLSetting.h"
38#include "src/sksl/ir/SkSLStatement.h"
39#include "src/sksl/ir/SkSLSwitchStatement.h"
40#include "src/sksl/ir/SkSLSwizzle.h"
41#include "src/sksl/ir/SkSLTernaryExpression.h"
42#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/sksl/ir/SkSLVariableReference.h"
ethannicholasf789b382016-08-03 12:43:36 -070044
45namespace SkSL {
46
ethannicholasf789b382016-08-03 12:43:36 -070047/**
48 * Converts a Program into GLSL code.
49 */
50class GLSLCodeGenerator : public CodeGenerator {
51public:
52 enum Precedence {
53 kParentheses_Precedence = 1,
54 kPostfix_Precedence = 2,
55 kPrefix_Precedence = 3,
56 kMultiplicative_Precedence = 4,
57 kAdditive_Precedence = 5,
58 kShift_Precedence = 6,
59 kRelational_Precedence = 7,
60 kEquality_Precedence = 8,
61 kBitwiseAnd_Precedence = 9,
62 kBitwiseXor_Precedence = 10,
63 kBitwiseOr_Precedence = 11,
64 kLogicalAnd_Precedence = 12,
65 kLogicalXor_Precedence = 13,
66 kLogicalOr_Precedence = 14,
67 kTernary_Precedence = 15,
68 kAssignment_Precedence = 16,
69 kSequence_Precedence = 17,
Ethan Nicholas4b330df2017-05-17 10:52:55 -040070 kTopLevel_Precedence = kSequence_Precedence
ethannicholasf789b382016-08-03 12:43:36 -070071 };
72
Ethan Nicholas941e7e22016-12-12 15:33:30 -050073 GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas0df1b042017-03-31 13:56:23 -040074 OutputStream* out)
Ethan Nicholas941e7e22016-12-12 15:33:30 -050075 : INHERITED(program, errors, out)
Ethan Nicholas762466e2017-06-29 10:03:38 -040076 , fLineEnding("\n")
Ethan Nicholasa45e1a72018-08-31 16:56:52 -040077 , fContext(*context)
78 , fProgramKind(program->fKind) {}
ethannicholasf789b382016-08-03 12:43:36 -070079
Ethan Nicholas762466e2017-06-29 10:03:38 -040080 bool generateCode() override;
ethannicholasf789b382016-08-03 12:43:36 -070081
Ethan Nicholas762466e2017-06-29 10:03:38 -040082protected:
ethannicholasf789b382016-08-03 12:43:36 -070083 void write(const char* s);
84
85 void writeLine();
86
87 void writeLine(const char* s);
88
Ethan Nicholas0df1b042017-03-31 13:56:23 -040089 void write(const String& s);
ethannicholasf789b382016-08-03 12:43:36 -070090
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070091 void write(StringFragment s);
92
Ethan Nicholas0df1b042017-03-31 13:56:23 -040093 void writeLine(const String& s);
ethannicholasf789b382016-08-03 12:43:36 -070094
Ethan Nicholas762466e2017-06-29 10:03:38 -040095 virtual void writeHeader();
96
Ethan Nicholasf7b88202017-09-18 14:10:39 -040097 virtual bool usesPrecisionModifiers() const;
Ethan Nicholas762466e2017-06-29 10:03:38 -040098
Ethan Nicholasf7b88202017-09-18 14:10:39 -040099 virtual String getTypeName(const Type& type);
100
Brian Osman02bc5222021-01-28 11:00:20 -0500101 void writeStructDefinition(const StructDefinition& s);
John Stilesdc75a972020-11-25 16:24:55 -0500102
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400103 void writeType(const Type& type);
ethannicholasf789b382016-08-03 12:43:36 -0700104
Ethan Nicholas88f6d372018-07-27 10:03:46 -0400105 void writeExtension(const String& name);
106
107 void writeExtension(const String& name, bool require);
ethannicholasf789b382016-08-03 12:43:36 -0700108
109 void writeInterfaceBlock(const InterfaceBlock& intf);
110
111 void writeFunctionStart(const FunctionDeclaration& f);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400112
ethannicholasf789b382016-08-03 12:43:36 -0700113 void writeFunctionDeclaration(const FunctionDeclaration& f);
114
John Stiles569249b2020-11-03 12:18:22 -0500115 void writeFunctionPrototype(const FunctionPrototype& f);
116
Ethan Nicholas762466e2017-06-29 10:03:38 -0400117 virtual void writeFunction(const FunctionDefinition& f);
ethannicholasf789b382016-08-03 12:43:36 -0700118
119 void writeLayout(const Layout& layout);
120
ethannicholas5961bc92016-10-12 06:39:56 -0700121 void writeModifiers(const Modifiers& modifiers, bool globalContext);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500122
Ethan Nicholascd700e92018-08-24 16:43:57 -0400123 virtual void writeInputVars();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000124
Ethan Nicholas762466e2017-06-29 10:03:38 -0400125 virtual void writeVarInitializer(const Variable& var, const Expression& value);
126
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400127 const char* getTypePrecision(const Type& type);
128
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400129 void writeTypePrecision(const Type& type);
130
Brian Osmanc0213602020-10-06 14:43:32 -0400131 void writeVarDeclaration(const VarDeclaration& var, bool global);
ethannicholasf789b382016-08-03 12:43:36 -0700132
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500133 void writeFragCoord();
134
Ethan Nicholas762466e2017-06-29 10:03:38 -0400135 virtual void writeVariableReference(const VariableReference& ref);
ethannicholasf789b382016-08-03 12:43:36 -0700136
137 void writeExpression(const Expression& expr, Precedence parentPrecedence);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500138
ethannicholasf789b382016-08-03 12:43:36 -0700139 void writeIntrinsicCall(const FunctionCall& c);
140
ethannicholas5961bc92016-10-12 06:39:56 -0700141 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
142
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500143 void writeDeterminantHack(const Expression& mat);
144
145 void writeInverseHack(const Expression& mat);
146
147 void writeTransposeHack(const Expression& mat);
148
149 void writeInverseSqrtHack(const Expression& x);
150
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400151 virtual void writeFunctionCall(const FunctionCall& c);
ethannicholasf789b382016-08-03 12:43:36 -0700152
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400153 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700154
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400155 virtual void writeFieldAccess(const FieldAccess& f);
ethannicholasf789b382016-08-03 12:43:36 -0700156
Ethan Nicholas82399462017-10-16 12:35:44 -0400157 virtual void writeSwizzle(const Swizzle& swizzle);
ethannicholasf789b382016-08-03 12:43:36 -0700158
Ethan Nicholas762466e2017-06-29 10:03:38 -0400159 static Precedence GetBinaryPrecedence(Token::Kind op);
160
161 virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
Adrienne Walkerc02165f2018-08-21 11:08:11 -0700162 void writeShortCircuitWorkaroundExpression(const BinaryExpression& b,
163 Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700164
165 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
166
Ethan Nicholas762466e2017-06-29 10:03:38 -0400167 virtual void writeIndexExpression(const IndexExpression& expr);
ethannicholasf789b382016-08-03 12:43:36 -0700168
169 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
170
171 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
172
173 void writeBoolLiteral(const BoolLiteral& b);
174
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400175 virtual void writeIntLiteral(const IntLiteral& i);
ethannicholasf789b382016-08-03 12:43:36 -0700176
177 void writeFloatLiteral(const FloatLiteral& f);
178
Ethan Nicholas762466e2017-06-29 10:03:38 -0400179 virtual void writeSetting(const Setting& s);
180
ethannicholasf789b382016-08-03 12:43:36 -0700181 void writeStatement(const Statement& s);
182
183 void writeBlock(const Block& b);
184
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400185 virtual void writeIfStatement(const IfStatement& stmt);
ethannicholasf789b382016-08-03 12:43:36 -0700186
187 void writeForStatement(const ForStatement& f);
188
ethannicholasf789b382016-08-03 12:43:36 -0700189 void writeDoStatement(const DoStatement& d);
190
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400191 virtual void writeSwitchStatement(const SwitchStatement& s);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500192
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400193 virtual void writeReturnStatement(const ReturnStatement& r);
ethannicholasf789b382016-08-03 12:43:36 -0700194
Ethan Nicholas762466e2017-06-29 10:03:38 -0400195 virtual void writeProgramElement(const ProgramElement& e);
196
197 const char* fLineEnding;
ethannicholasf789b382016-08-03 12:43:36 -0700198 const Context& fContext;
Ethan Nicholas88f6d372018-07-27 10:03:46 -0400199 StringStream fExtensions;
200 StringStream fGlobals;
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500201 StringStream fExtraFunctions;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400202 String fFunctionHeader;
ethannicholas5961bc92016-10-12 06:39:56 -0700203 Program::Kind fProgramKind;
ethannicholasddb37d62016-10-20 09:54:00 -0700204 int fVarCount = 0;
205 int fIndentation = 0;
206 bool fAtLineStart = false;
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500207 std::set<String> fWrittenIntrinsics;
ethannicholasddb37d62016-10-20 09:54:00 -0700208 // true if we have run into usages of dFdx / dFdy
209 bool fFoundDerivatives = false;
Brian Osman4b2f9152018-04-17 11:19:57 -0400210 bool fFoundExternalSamplerDecl = false;
Brian Salomon67529b22019-08-13 15:31:04 -0400211 bool fFoundRectSamplerDecl = false;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600212 bool fFoundGSInvocations = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500213 bool fSetupFragPositionGlobal = false;
214 bool fSetupFragPositionLocal = false;
Brian Salomondba65f92018-01-22 08:43:38 -0500215 bool fSetupFragCoordWorkaround = false;
Ethan Nicholas13863662019-07-29 13:05:15 -0400216 // if non-empty, replace all texture / texture2D / textureProj / etc. calls with this name
217 String fTextureFunctionOverride;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500218
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400219 // We map function names to function class so we can quickly deal with function calls that need
220 // extra processing
221 enum class FunctionClass {
Adrienne Walker92b161f2018-08-22 10:41:52 -0700222 kAbs,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400223 kAtan,
224 kDeterminant,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700225 kDFdx,
226 kDFdy,
227 kFwidth,
Chris Daltona7086182018-11-16 09:33:43 -0500228 kFMA,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400229 kFract,
230 kInverse,
231 kInverseSqrt,
232 kMin,
Adrienne Walker2f4c09b2018-08-22 16:04:57 -0700233 kPow,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400234 kSaturate,
235 kTexture,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -0400236 kTranspose
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400237 };
238 static std::unordered_map<StringFragment, FunctionClass>* fFunctionClasses;
239
John Stiles7571f9e2020-09-02 22:42:33 -0400240 using INHERITED = CodeGenerator;
ethannicholasf789b382016-08-03 12:43:36 -0700241};
242
John Stilesa6841be2020-08-06 14:11:56 -0400243} // namespace SkSL
ethannicholasf789b382016-08-03 12:43:36 -0700244
245#endif