blob: 31683dd9d715c55309bb1f38ab9fe6085e4e4dad [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "src/sksl/ir/SkSLVariableReference.h"
43#include "src/sksl/ir/SkSLWhileStatement.h"
ethannicholasf789b382016-08-03 12:43:36 -070044
45namespace SkSL {
46
47#define kLast_Capability SpvCapabilityMultiViewport
48
ethannicholasf789b382016-08-03 12:43:36 -070049/**
50 * Converts a Program into GLSL code.
51 */
52class GLSLCodeGenerator : public CodeGenerator {
53public:
54 enum Precedence {
55 kParentheses_Precedence = 1,
56 kPostfix_Precedence = 2,
57 kPrefix_Precedence = 3,
58 kMultiplicative_Precedence = 4,
59 kAdditive_Precedence = 5,
60 kShift_Precedence = 6,
61 kRelational_Precedence = 7,
62 kEquality_Precedence = 8,
63 kBitwiseAnd_Precedence = 9,
64 kBitwiseXor_Precedence = 10,
65 kBitwiseOr_Precedence = 11,
66 kLogicalAnd_Precedence = 12,
67 kLogicalXor_Precedence = 13,
68 kLogicalOr_Precedence = 14,
69 kTernary_Precedence = 15,
70 kAssignment_Precedence = 16,
71 kSequence_Precedence = 17,
Ethan Nicholas4b330df2017-05-17 10:52:55 -040072 kTopLevel_Precedence = kSequence_Precedence
ethannicholasf789b382016-08-03 12:43:36 -070073 };
74
Ethan Nicholas941e7e22016-12-12 15:33:30 -050075 GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas0df1b042017-03-31 13:56:23 -040076 OutputStream* out)
Ethan Nicholas941e7e22016-12-12 15:33:30 -050077 : INHERITED(program, errors, out)
Ethan Nicholas762466e2017-06-29 10:03:38 -040078 , fLineEnding("\n")
Ethan Nicholasa45e1a72018-08-31 16:56:52 -040079 , fContext(*context)
80 , fProgramKind(program->fKind) {}
ethannicholasf789b382016-08-03 12:43:36 -070081
Ethan Nicholas762466e2017-06-29 10:03:38 -040082 bool generateCode() override;
ethannicholasf789b382016-08-03 12:43:36 -070083
Ethan Nicholas762466e2017-06-29 10:03:38 -040084protected:
ethannicholasf789b382016-08-03 12:43:36 -070085 void write(const char* s);
86
87 void writeLine();
88
89 void writeLine(const char* s);
90
Ethan Nicholas0df1b042017-03-31 13:56:23 -040091 void write(const String& s);
ethannicholasf789b382016-08-03 12:43:36 -070092
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070093 void write(StringFragment s);
94
Ethan Nicholas0df1b042017-03-31 13:56:23 -040095 void writeLine(const String& s);
ethannicholasf789b382016-08-03 12:43:36 -070096
Ethan Nicholas762466e2017-06-29 10:03:38 -040097 virtual void writeHeader();
98
Ethan Nicholasf7b88202017-09-18 14:10:39 -040099 virtual bool usesPrecisionModifiers() const;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400100
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400101 virtual String getTypeName(const Type& type);
102
103 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
Ethan Nicholas762466e2017-06-29 10:03:38 -0400115 virtual void writeFunction(const FunctionDefinition& f);
ethannicholasf789b382016-08-03 12:43:36 -0700116
117 void writeLayout(const Layout& layout);
118
ethannicholas5961bc92016-10-12 06:39:56 -0700119 void writeModifiers(const Modifiers& modifiers, bool globalContext);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500120
Ethan Nicholascd700e92018-08-24 16:43:57 -0400121 virtual void writeInputVars();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000122
Ethan Nicholas762466e2017-06-29 10:03:38 -0400123 virtual void writeVarInitializer(const Variable& var, const Expression& value);
124
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400125 const char* getTypePrecision(const Type& type);
126
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400127 void writeTypePrecision(const Type& type);
128
Brian Osmanc0213602020-10-06 14:43:32 -0400129 void writeVarDeclaration(const VarDeclaration& var, bool global);
ethannicholasf789b382016-08-03 12:43:36 -0700130
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500131 void writeFragCoord();
132
Ethan Nicholas762466e2017-06-29 10:03:38 -0400133 virtual void writeVariableReference(const VariableReference& ref);
ethannicholasf789b382016-08-03 12:43:36 -0700134
135 void writeExpression(const Expression& expr, Precedence parentPrecedence);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500136
ethannicholasf789b382016-08-03 12:43:36 -0700137 void writeIntrinsicCall(const FunctionCall& c);
138
ethannicholas5961bc92016-10-12 06:39:56 -0700139 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
140
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500141 void writeDeterminantHack(const Expression& mat);
142
143 void writeInverseHack(const Expression& mat);
144
145 void writeTransposeHack(const Expression& mat);
146
147 void writeInverseSqrtHack(const Expression& x);
148
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400149 virtual void writeFunctionCall(const FunctionCall& c);
ethannicholasf789b382016-08-03 12:43:36 -0700150
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400151 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700152
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400153 virtual void writeFieldAccess(const FieldAccess& f);
ethannicholasf789b382016-08-03 12:43:36 -0700154
Ethan Nicholas82399462017-10-16 12:35:44 -0400155 virtual void writeSwizzle(const Swizzle& swizzle);
ethannicholasf789b382016-08-03 12:43:36 -0700156
Ethan Nicholas762466e2017-06-29 10:03:38 -0400157 static Precedence GetBinaryPrecedence(Token::Kind op);
158
159 virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
Adrienne Walkerc02165f2018-08-21 11:08:11 -0700160 void writeShortCircuitWorkaroundExpression(const BinaryExpression& b,
161 Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700162
163 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
164
Ethan Nicholas762466e2017-06-29 10:03:38 -0400165 virtual void writeIndexExpression(const IndexExpression& expr);
ethannicholasf789b382016-08-03 12:43:36 -0700166
167 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
168
169 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
170
171 void writeBoolLiteral(const BoolLiteral& b);
172
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400173 virtual void writeIntLiteral(const IntLiteral& i);
ethannicholasf789b382016-08-03 12:43:36 -0700174
175 void writeFloatLiteral(const FloatLiteral& f);
176
Ethan Nicholas762466e2017-06-29 10:03:38 -0400177 virtual void writeSetting(const Setting& s);
178
ethannicholasf789b382016-08-03 12:43:36 -0700179 void writeStatement(const Statement& s);
180
181 void writeBlock(const Block& b);
182
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400183 virtual void writeIfStatement(const IfStatement& stmt);
ethannicholasf789b382016-08-03 12:43:36 -0700184
185 void writeForStatement(const ForStatement& f);
186
187 void writeWhileStatement(const WhileStatement& w);
188
189 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 Nicholas941e7e22016-12-12 15:33:30 -0500207 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
208 // more than one or two structs per shader, a simple linear search will be faster than anything
ethannicholasf789b382016-08-03 12:43:36 -0700209 // fancier.
210 std::vector<const Type*> fWrittenStructs;
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500211 std::set<String> fWrittenIntrinsics;
ethannicholasddb37d62016-10-20 09:54:00 -0700212 // true if we have run into usages of dFdx / dFdy
213 bool fFoundDerivatives = false;
Brian Osman4b2f9152018-04-17 11:19:57 -0400214 bool fFoundExternalSamplerDecl = false;
Brian Salomon67529b22019-08-13 15:31:04 -0400215 bool fFoundRectSamplerDecl = false;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600216 bool fFoundGSInvocations = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500217 bool fSetupFragPositionGlobal = false;
218 bool fSetupFragPositionLocal = false;
Brian Salomondba65f92018-01-22 08:43:38 -0500219 bool fSetupFragCoordWorkaround = false;
Ethan Nicholas13863662019-07-29 13:05:15 -0400220 // if non-empty, replace all texture / texture2D / textureProj / etc. calls with this name
221 String fTextureFunctionOverride;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500222
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400223 // We map function names to function class so we can quickly deal with function calls that need
224 // extra processing
225 enum class FunctionClass {
Adrienne Walker92b161f2018-08-22 10:41:52 -0700226 kAbs,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400227 kAtan,
228 kDeterminant,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700229 kDFdx,
230 kDFdy,
231 kFwidth,
Chris Daltona7086182018-11-16 09:33:43 -0500232 kFMA,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400233 kFract,
234 kInverse,
235 kInverseSqrt,
236 kMin,
Adrienne Walker2f4c09b2018-08-22 16:04:57 -0700237 kPow,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400238 kSaturate,
239 kTexture,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -0400240 kTranspose
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400241 };
242 static std::unordered_map<StringFragment, FunctionClass>* fFunctionClasses;
243
John Stiles7571f9e2020-09-02 22:42:33 -0400244 using INHERITED = CodeGenerator;
ethannicholasf789b382016-08-03 12:43:36 -0700245};
246
John Stilesa6841be2020-08-06 14:11:56 -0400247} // namespace SkSL
ethannicholasf789b382016-08-03 12:43:36 -0700248
249#endif