blob: 5716bde70dcfa65628fc216d243a477610179d3b [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
97 virtual void writePrecisionModifier();
98
ethannicholasf789b382016-08-03 12:43:36 -070099 void writeType(const Type& type);
100
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
ethannicholas5961bc92016-10-12 06:39:56 -0700119 void writeVarDeclarations(const VarDeclarations& decl, bool global);
ethannicholasf789b382016-08-03 12:43:36 -0700120
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500121 void writeFragCoord();
122
Ethan Nicholas762466e2017-06-29 10:03:38 -0400123 virtual void writeVariableReference(const VariableReference& ref);
ethannicholasf789b382016-08-03 12:43:36 -0700124
125 void writeExpression(const Expression& expr, Precedence parentPrecedence);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500126
ethannicholasf789b382016-08-03 12:43:36 -0700127 void writeIntrinsicCall(const FunctionCall& c);
128
ethannicholas5961bc92016-10-12 06:39:56 -0700129 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
130
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400131 virtual void writeFunctionCall(const FunctionCall& c);
ethannicholasf789b382016-08-03 12:43:36 -0700132
133 void writeConstructor(const Constructor& c);
134
135 void writeFieldAccess(const FieldAccess& f);
136
137 void writeSwizzle(const Swizzle& swizzle);
138
Ethan Nicholas762466e2017-06-29 10:03:38 -0400139 static Precedence GetBinaryPrecedence(Token::Kind op);
140
141 virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700142
143 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
144
Ethan Nicholas762466e2017-06-29 10:03:38 -0400145 virtual void writeIndexExpression(const IndexExpression& expr);
ethannicholasf789b382016-08-03 12:43:36 -0700146
147 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
148
149 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
150
151 void writeBoolLiteral(const BoolLiteral& b);
152
153 void writeIntLiteral(const IntLiteral& i);
154
155 void writeFloatLiteral(const FloatLiteral& f);
156
Ethan Nicholas762466e2017-06-29 10:03:38 -0400157 virtual void writeSetting(const Setting& s);
158
ethannicholasf789b382016-08-03 12:43:36 -0700159 void writeStatement(const Statement& s);
160
Ethan Nicholascb670962017-04-20 19:31:52 -0400161 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
162
ethannicholasf789b382016-08-03 12:43:36 -0700163 void writeBlock(const Block& b);
164
165 void writeIfStatement(const IfStatement& stmt);
166
167 void writeForStatement(const ForStatement& f);
168
169 void writeWhileStatement(const WhileStatement& w);
170
171 void writeDoStatement(const DoStatement& d);
172
Ethan Nicholasaf197692017-02-27 13:26:45 -0500173 void writeSwitchStatement(const SwitchStatement& s);
174
ethannicholasf789b382016-08-03 12:43:36 -0700175 void writeReturnStatement(const ReturnStatement& r);
176
Ethan Nicholas762466e2017-06-29 10:03:38 -0400177 virtual void writeProgramElement(const ProgramElement& e);
178
179 const char* fLineEnding;
ethannicholasf789b382016-08-03 12:43:36 -0700180 const Context& fContext;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400181 StringStream fHeader;
182 String fFunctionHeader;
ethannicholas5961bc92016-10-12 06:39:56 -0700183 Program::Kind fProgramKind;
ethannicholasddb37d62016-10-20 09:54:00 -0700184 int fVarCount = 0;
185 int fIndentation = 0;
186 bool fAtLineStart = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500187 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
188 // more than one or two structs per shader, a simple linear search will be faster than anything
ethannicholasf789b382016-08-03 12:43:36 -0700189 // fancier.
190 std::vector<const Type*> fWrittenStructs;
ethannicholasddb37d62016-10-20 09:54:00 -0700191 // true if we have run into usages of dFdx / dFdy
192 bool fFoundDerivatives = false;
Brian Salomon2a51de82016-11-16 12:06:01 -0500193 bool fFoundImageDecl = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500194 bool fSetupFragPositionGlobal = false;
195 bool fSetupFragPositionLocal = false;
196
197 typedef CodeGenerator INHERITED;
ethannicholasf789b382016-08-03 12:43:36 -0700198};
199
200}
201
202#endif