blob: 0ae2c5c585da0da9cc116aa4b0415e5d118c7ecd [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
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050015#include "SkStream.h"
ethannicholasf789b382016-08-03 12:43:36 -070016#include "SkSLCodeGenerator.h"
17#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"
36#include "ir/SkSLStatement.h"
37#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,
75 SkWStream* out)
76 : 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 Nicholas9e1138d2016-11-21 10:39:35 -050088 void write(const SkString& s);
ethannicholasf789b382016-08-03 12:43:36 -070089
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050090 void writeLine(const SkString& 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);
99
100 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
148 void writeBlock(const Block& b);
149
150 void writeIfStatement(const IfStatement& stmt);
151
152 void writeForStatement(const ForStatement& f);
153
154 void writeWhileStatement(const WhileStatement& w);
155
156 void writeDoStatement(const DoStatement& d);
157
158 void writeReturnStatement(const ReturnStatement& r);
159
160 const Context& fContext;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500161 SkDynamicMemoryWStream fHeader;
162 SkString fFunctionHeader;
ethannicholas5961bc92016-10-12 06:39:56 -0700163 Program::Kind fProgramKind;
ethannicholasddb37d62016-10-20 09:54:00 -0700164 int fVarCount = 0;
165 int fIndentation = 0;
166 bool fAtLineStart = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500167 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
168 // more than one or two structs per shader, a simple linear search will be faster than anything
ethannicholasf789b382016-08-03 12:43:36 -0700169 // fancier.
170 std::vector<const Type*> fWrittenStructs;
ethannicholasddb37d62016-10-20 09:54:00 -0700171 // true if we have run into usages of dFdx / dFdy
172 bool fFoundDerivatives = false;
Brian Salomon2a51de82016-11-16 12:06:01 -0500173 bool fFoundImageDecl = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500174 bool fSetupFragPositionGlobal = false;
175 bool fSetupFragPositionLocal = false;
176
177 typedef CodeGenerator INHERITED;
ethannicholasf789b382016-08-03 12:43:36 -0700178};
179
180}
181
182#endif