blob: e10299aa142c92d7a535bfca79291bd862a55a3a [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 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
Brian Salomon1d816b92017-08-17 11:07:59 -040097 virtual void writePrecisionModifier();
Ethan Nicholas762466e2017-06-29 10:03:38 -040098
Brian Salomon1d816b92017-08-17 11:07:59 -040099 virtual void writeType(const Type& type);
ethannicholasf789b382016-08-03 12:43:36 -0700100
101 void writeExtension(const Extension& ext);
102
103 void writeInterfaceBlock(const InterfaceBlock& intf);
104
105 void writeFunctionStart(const FunctionDeclaration& f);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400106
ethannicholasf789b382016-08-03 12:43:36 -0700107 void writeFunctionDeclaration(const FunctionDeclaration& f);
108
Ethan Nicholas762466e2017-06-29 10:03:38 -0400109 virtual void writeFunction(const FunctionDefinition& f);
ethannicholasf789b382016-08-03 12:43:36 -0700110
111 void writeLayout(const Layout& layout);
112
ethannicholas5961bc92016-10-12 06:39:56 -0700113 void writeModifiers(const Modifiers& modifiers, bool globalContext);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500114
ethannicholasf789b382016-08-03 12:43:36 -0700115 void writeGlobalVars(const VarDeclaration& vs);
116
Ethan Nicholas762466e2017-06-29 10:03:38 -0400117 virtual void writeVarInitializer(const Variable& var, const Expression& value);
118
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400119 void writeTypePrecision(const Type& type);
120
ethannicholas5961bc92016-10-12 06:39:56 -0700121 void writeVarDeclarations(const VarDeclarations& decl, bool global);
ethannicholasf789b382016-08-03 12:43:36 -0700122
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500123 void writeFragCoord();
124
Ethan Nicholas762466e2017-06-29 10:03:38 -0400125 virtual void writeVariableReference(const VariableReference& ref);
ethannicholasf789b382016-08-03 12:43:36 -0700126
127 void writeExpression(const Expression& expr, Precedence parentPrecedence);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500128
ethannicholasf789b382016-08-03 12:43:36 -0700129 void writeIntrinsicCall(const FunctionCall& c);
130
ethannicholas5961bc92016-10-12 06:39:56 -0700131 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
132
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400133 virtual void writeFunctionCall(const FunctionCall& c);
ethannicholasf789b382016-08-03 12:43:36 -0700134
Brian Salomon1d816b92017-08-17 11:07:59 -0400135 void writeConstructor(const Constructor& c);
ethannicholasf789b382016-08-03 12:43:36 -0700136
137 void writeFieldAccess(const FieldAccess& f);
138
139 void writeSwizzle(const Swizzle& swizzle);
140
Ethan Nicholas762466e2017-06-29 10:03:38 -0400141 static Precedence GetBinaryPrecedence(Token::Kind op);
142
143 virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700144
145 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
146
Ethan Nicholas762466e2017-06-29 10:03:38 -0400147 virtual void writeIndexExpression(const IndexExpression& expr);
ethannicholasf789b382016-08-03 12:43:36 -0700148
149 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
150
151 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
152
153 void writeBoolLiteral(const BoolLiteral& b);
154
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400155 virtual void writeIntLiteral(const IntLiteral& i);
ethannicholasf789b382016-08-03 12:43:36 -0700156
157 void writeFloatLiteral(const FloatLiteral& f);
158
Ethan Nicholas762466e2017-06-29 10:03:38 -0400159 virtual void writeSetting(const Setting& s);
160
ethannicholasf789b382016-08-03 12:43:36 -0700161 void writeStatement(const Statement& s);
162
Ethan Nicholascb670962017-04-20 19:31:52 -0400163 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
164
ethannicholasf789b382016-08-03 12:43:36 -0700165 void writeBlock(const Block& b);
166
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400167 virtual void writeIfStatement(const IfStatement& stmt);
ethannicholasf789b382016-08-03 12:43:36 -0700168
169 void writeForStatement(const ForStatement& f);
170
171 void writeWhileStatement(const WhileStatement& w);
172
173 void writeDoStatement(const DoStatement& d);
174
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400175 virtual void writeSwitchStatement(const SwitchStatement& s);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500176
ethannicholasf789b382016-08-03 12:43:36 -0700177 void writeReturnStatement(const ReturnStatement& r);
178
Ethan Nicholas762466e2017-06-29 10:03:38 -0400179 virtual void writeProgramElement(const ProgramElement& e);
180
181 const char* fLineEnding;
ethannicholasf789b382016-08-03 12:43:36 -0700182 const Context& fContext;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400183 StringStream fHeader;
184 String fFunctionHeader;
ethannicholas5961bc92016-10-12 06:39:56 -0700185 Program::Kind fProgramKind;
ethannicholasddb37d62016-10-20 09:54:00 -0700186 int fVarCount = 0;
187 int fIndentation = 0;
188 bool fAtLineStart = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500189 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
190 // more than one or two structs per shader, a simple linear search will be faster than anything
ethannicholasf789b382016-08-03 12:43:36 -0700191 // fancier.
192 std::vector<const Type*> fWrittenStructs;
ethannicholasddb37d62016-10-20 09:54:00 -0700193 // true if we have run into usages of dFdx / dFdy
194 bool fFoundDerivatives = false;
Brian Salomon2a51de82016-11-16 12:06:01 -0500195 bool fFoundImageDecl = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500196 bool fSetupFragPositionGlobal = false;
197 bool fSetupFragPositionLocal = false;
198
199 typedef CodeGenerator INHERITED;
ethannicholasf789b382016-08-03 12:43:36 -0700200};
201
202}
203
204#endif