blob: 65be7dc09e4042ebdb655b16817fbd1551f69cc0 [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"
16#include "ir/SkSLBinaryExpression.h"
17#include "ir/SkSLBoolLiteral.h"
18#include "ir/SkSLConstructor.h"
19#include "ir/SkSLDoStatement.h"
20#include "ir/SkSLExtension.h"
21#include "ir/SkSLFloatLiteral.h"
22#include "ir/SkSLIfStatement.h"
23#include "ir/SkSLIndexExpression.h"
24#include "ir/SkSLInterfaceBlock.h"
25#include "ir/SkSLIntLiteral.h"
26#include "ir/SkSLFieldAccess.h"
27#include "ir/SkSLForStatement.h"
28#include "ir/SkSLFunctionCall.h"
29#include "ir/SkSLFunctionDeclaration.h"
30#include "ir/SkSLFunctionDefinition.h"
31#include "ir/SkSLPrefixExpression.h"
32#include "ir/SkSLPostfixExpression.h"
33#include "ir/SkSLProgramElement.h"
34#include "ir/SkSLReturnStatement.h"
35#include "ir/SkSLStatement.h"
Ethan Nicholasaf197692017-02-27 13:26:45 -050036#include "ir/SkSLSwitchStatement.h"
ethannicholasf789b382016-08-03 12:43:36 -070037#include "ir/SkSLSwizzle.h"
38#include "ir/SkSLTernaryExpression.h"
ethannicholas22f939e2016-10-13 13:25:34 -070039#include "ir/SkSLVarDeclarations.h"
40#include "ir/SkSLVarDeclarationsStatement.h"
ethannicholasf789b382016-08-03 12:43:36 -070041#include "ir/SkSLVariableReference.h"
42#include "ir/SkSLWhileStatement.h"
43
44namespace SkSL {
45
46#define kLast_Capability SpvCapabilityMultiViewport
47
ethannicholasf789b382016-08-03 12:43:36 -070048/**
49 * Converts a Program into GLSL code.
50 */
51class GLSLCodeGenerator : public CodeGenerator {
52public:
53 enum Precedence {
54 kParentheses_Precedence = 1,
55 kPostfix_Precedence = 2,
56 kPrefix_Precedence = 3,
57 kMultiplicative_Precedence = 4,
58 kAdditive_Precedence = 5,
59 kShift_Precedence = 6,
60 kRelational_Precedence = 7,
61 kEquality_Precedence = 8,
62 kBitwiseAnd_Precedence = 9,
63 kBitwiseXor_Precedence = 10,
64 kBitwiseOr_Precedence = 11,
65 kLogicalAnd_Precedence = 12,
66 kLogicalXor_Precedence = 13,
67 kLogicalOr_Precedence = 14,
68 kTernary_Precedence = 15,
69 kAssignment_Precedence = 16,
70 kSequence_Precedence = 17,
71 kTopLevel_Precedence = 18
72 };
73
Ethan Nicholas941e7e22016-12-12 15:33:30 -050074 GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
Ethan Nicholas0df1b042017-03-31 13:56:23 -040075 OutputStream* out)
Ethan Nicholas941e7e22016-12-12 15:33:30 -050076 : INHERITED(program, errors, out)
77 , fContext(*context) {}
ethannicholasf789b382016-08-03 12:43:36 -070078
Ethan Nicholas941e7e22016-12-12 15:33:30 -050079 virtual bool generateCode() override;
ethannicholasf789b382016-08-03 12:43:36 -070080
81private:
82 void write(const char* s);
83
84 void writeLine();
85
86 void writeLine(const char* s);
87
Ethan Nicholas0df1b042017-03-31 13:56:23 -040088 void write(const String& s);
ethannicholasf789b382016-08-03 12:43:36 -070089
Ethan Nicholas0df1b042017-03-31 13:56:23 -040090 void writeLine(const String& s);
ethannicholasf789b382016-08-03 12:43:36 -070091
92 void writeType(const Type& type);
93
94 void writeExtension(const Extension& ext);
95
96 void writeInterfaceBlock(const InterfaceBlock& intf);
97
98 void writeFunctionStart(const FunctionDeclaration& f);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040099
ethannicholasf789b382016-08-03 12:43:36 -0700100 void writeFunctionDeclaration(const FunctionDeclaration& f);
101
102 void writeFunction(const FunctionDefinition& f);
103
104 void writeLayout(const Layout& layout);
105
ethannicholas5961bc92016-10-12 06:39:56 -0700106 void writeModifiers(const Modifiers& modifiers, bool globalContext);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500107
ethannicholasf789b382016-08-03 12:43:36 -0700108 void writeGlobalVars(const VarDeclaration& vs);
109
ethannicholas5961bc92016-10-12 06:39:56 -0700110 void writeVarDeclarations(const VarDeclarations& decl, bool global);
ethannicholasf789b382016-08-03 12:43:36 -0700111
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500112 void writeFragCoord();
113
ethannicholasf789b382016-08-03 12:43:36 -0700114 void writeVariableReference(const VariableReference& ref);
115
116 void writeExpression(const Expression& expr, Precedence parentPrecedence);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500117
ethannicholasf789b382016-08-03 12:43:36 -0700118 void writeIntrinsicCall(const FunctionCall& c);
119
ethannicholas5961bc92016-10-12 06:39:56 -0700120 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
121
ethannicholasf789b382016-08-03 12:43:36 -0700122 void writeFunctionCall(const FunctionCall& c);
123
124 void writeConstructor(const Constructor& c);
125
126 void writeFieldAccess(const FieldAccess& f);
127
128 void writeSwizzle(const Swizzle& swizzle);
129
130 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
131
132 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
133
134 void writeIndexExpression(const IndexExpression& expr);
135
136 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
137
138 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
139
140 void writeBoolLiteral(const BoolLiteral& b);
141
142 void writeIntLiteral(const IntLiteral& i);
143
144 void writeFloatLiteral(const FloatLiteral& f);
145
146 void writeStatement(const Statement& s);
147
Ethan Nicholascb670962017-04-20 19:31:52 -0400148 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
149
ethannicholasf789b382016-08-03 12:43:36 -0700150 void writeBlock(const Block& b);
151
152 void writeIfStatement(const IfStatement& stmt);
153
154 void writeForStatement(const ForStatement& f);
155
156 void writeWhileStatement(const WhileStatement& w);
157
158 void writeDoStatement(const DoStatement& d);
159
Ethan Nicholasaf197692017-02-27 13:26:45 -0500160 void writeSwitchStatement(const SwitchStatement& s);
161
ethannicholasf789b382016-08-03 12:43:36 -0700162 void writeReturnStatement(const ReturnStatement& r);
163
164 const Context& fContext;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400165 StringStream fHeader;
166 String fFunctionHeader;
ethannicholas5961bc92016-10-12 06:39:56 -0700167 Program::Kind fProgramKind;
ethannicholasddb37d62016-10-20 09:54:00 -0700168 int fVarCount = 0;
169 int fIndentation = 0;
170 bool fAtLineStart = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500171 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
172 // more than one or two structs per shader, a simple linear search will be faster than anything
ethannicholasf789b382016-08-03 12:43:36 -0700173 // fancier.
174 std::vector<const Type*> fWrittenStructs;
ethannicholasddb37d62016-10-20 09:54:00 -0700175 // true if we have run into usages of dFdx / dFdy
176 bool fFoundDerivatives = false;
Brian Salomon2a51de82016-11-16 12:06:01 -0500177 bool fFoundImageDecl = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500178 bool fSetupFragPositionGlobal = false;
179 bool fSetupFragPositionLocal = false;
180
181 typedef CodeGenerator INHERITED;
ethannicholasf789b382016-08-03 12:43:36 -0700182};
183
184}
185
186#endif