blob: 2b308e3702d43091e476fffa9e4e0698c8b4f873 [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 Nicholas941e7e22016-12-12 15:33:30 -050080 , fContext(*context) {}
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
105 void writeExtension(const Extension& ext);
106
107 void writeInterfaceBlock(const InterfaceBlock& intf);
108
109 void writeFunctionStart(const FunctionDeclaration& f);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400110
ethannicholasf789b382016-08-03 12:43:36 -0700111 void writeFunctionDeclaration(const FunctionDeclaration& f);
112
Ethan Nicholas762466e2017-06-29 10:03:38 -0400113 virtual void writeFunction(const FunctionDefinition& f);
ethannicholasf789b382016-08-03 12:43:36 -0700114
115 void writeLayout(const Layout& layout);
116
ethannicholas5961bc92016-10-12 06:39:56 -0700117 void writeModifiers(const Modifiers& modifiers, bool globalContext);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500118
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000119 void writeGlobalVars(const VarDeclaration& vs);
120
Ethan Nicholas762466e2017-06-29 10:03:38 -0400121 virtual void writeVarInitializer(const Variable& var, const Expression& value);
122
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400123 const char* getTypePrecision(const Type& type);
124
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400125 void writeTypePrecision(const Type& type);
126
ethannicholas5961bc92016-10-12 06:39:56 -0700127 void writeVarDeclarations(const VarDeclarations& decl, bool global);
ethannicholasf789b382016-08-03 12:43:36 -0700128
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500129 void writeFragCoord();
130
Ethan Nicholas762466e2017-06-29 10:03:38 -0400131 virtual void writeVariableReference(const VariableReference& ref);
ethannicholasf789b382016-08-03 12:43:36 -0700132
133 void writeExpression(const Expression& expr, Precedence parentPrecedence);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500134
ethannicholasf789b382016-08-03 12:43:36 -0700135 void writeIntrinsicCall(const FunctionCall& c);
136
ethannicholas5961bc92016-10-12 06:39:56 -0700137 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
138
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500139 void writeDeterminantHack(const Expression& mat);
140
141 void writeInverseHack(const Expression& mat);
142
143 void writeTransposeHack(const Expression& mat);
144
145 void writeInverseSqrtHack(const Expression& x);
146
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400147 virtual void writeFunctionCall(const FunctionCall& c);
ethannicholasf789b382016-08-03 12:43:36 -0700148
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400149 void writeConstructor(const Constructor& c, Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700150
151 void writeFieldAccess(const FieldAccess& f);
152
Ethan Nicholas82399462017-10-16 12:35:44 -0400153 virtual void writeSwizzle(const Swizzle& swizzle);
ethannicholasf789b382016-08-03 12:43:36 -0700154
Ethan Nicholas762466e2017-06-29 10:03:38 -0400155 static Precedence GetBinaryPrecedence(Token::Kind op);
156
157 virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700158
159 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
160
Ethan Nicholas762466e2017-06-29 10:03:38 -0400161 virtual void writeIndexExpression(const IndexExpression& expr);
ethannicholasf789b382016-08-03 12:43:36 -0700162
163 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
164
165 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
166
167 void writeBoolLiteral(const BoolLiteral& b);
168
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400169 virtual void writeIntLiteral(const IntLiteral& i);
ethannicholasf789b382016-08-03 12:43:36 -0700170
171 void writeFloatLiteral(const FloatLiteral& f);
172
Ethan Nicholas762466e2017-06-29 10:03:38 -0400173 virtual void writeSetting(const Setting& s);
174
ethannicholasf789b382016-08-03 12:43:36 -0700175 void writeStatement(const Statement& s);
176
Ethan Nicholascb670962017-04-20 19:31:52 -0400177 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
178
ethannicholasf789b382016-08-03 12:43:36 -0700179 void writeBlock(const Block& b);
180
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400181 virtual void writeIfStatement(const IfStatement& stmt);
ethannicholasf789b382016-08-03 12:43:36 -0700182
183 void writeForStatement(const ForStatement& f);
184
185 void writeWhileStatement(const WhileStatement& w);
186
187 void writeDoStatement(const DoStatement& d);
188
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400189 virtual void writeSwitchStatement(const SwitchStatement& s);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500190
ethannicholasf789b382016-08-03 12:43:36 -0700191 void writeReturnStatement(const ReturnStatement& r);
192
Ethan Nicholas762466e2017-06-29 10:03:38 -0400193 virtual void writeProgramElement(const ProgramElement& e);
194
195 const char* fLineEnding;
ethannicholasf789b382016-08-03 12:43:36 -0700196 const Context& fContext;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400197 StringStream fHeader;
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500198 StringStream fExtraFunctions;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400199 String fFunctionHeader;
ethannicholas5961bc92016-10-12 06:39:56 -0700200 Program::Kind fProgramKind;
ethannicholasddb37d62016-10-20 09:54:00 -0700201 int fVarCount = 0;
202 int fIndentation = 0;
203 bool fAtLineStart = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500204 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
205 // more than one or two structs per shader, a simple linear search will be faster than anything
ethannicholasf789b382016-08-03 12:43:36 -0700206 // fancier.
207 std::vector<const Type*> fWrittenStructs;
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500208 std::set<String> fWrittenIntrinsics;
ethannicholasddb37d62016-10-20 09:54:00 -0700209 // true if we have run into usages of dFdx / dFdy
210 bool fFoundDerivatives = false;
Brian Salomon2a51de82016-11-16 12:06:01 -0500211 bool fFoundImageDecl = 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 Nicholas941e7e22016-12-12 15:33:30 -0500216
217 typedef CodeGenerator INHERITED;
ethannicholasf789b382016-08-03 12:43:36 -0700218};
219
220}
221
222#endif