blob: 5ed6104a09178defbe887d27e956c05d8941f2b2 [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 */
7
8#ifndef SKSL_GLSLCODEGENERATOR
9#define SKSL_GLSLCODEGENERATOR
10
11#include <stack>
12#include <tuple>
13#include <unordered_map>
14
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050015#include "glsl/GrGLSLCaps.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 Nicholas7ef4b742016-11-11 15:16:46 -050074 GLSLCodeGenerator(const Context* context, const GrGLSLCaps* caps)
ethannicholasf789b382016-08-03 12:43:36 -070075 : fContext(*context)
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050076 , fCaps(*caps) {}
ethannicholasf789b382016-08-03 12:43:36 -070077
78 void generateCode(const Program& program, std::ostream& out) override;
79
80private:
81 void write(const char* s);
82
83 void writeLine();
84
85 void writeLine(const char* s);
86
87 void write(const std::string& s);
88
89 void writeLine(const std::string& s);
90
91 void writeType(const Type& type);
92
93 void writeExtension(const Extension& ext);
94
95 void writeInterfaceBlock(const InterfaceBlock& intf);
96
97 void writeFunctionStart(const FunctionDeclaration& f);
98
99 void writeFunctionDeclaration(const FunctionDeclaration& f);
100
101 void writeFunction(const FunctionDefinition& f);
102
103 void writeLayout(const Layout& layout);
104
ethannicholas5961bc92016-10-12 06:39:56 -0700105 void writeModifiers(const Modifiers& modifiers, bool globalContext);
ethannicholasf789b382016-08-03 12:43:36 -0700106
107 void writeGlobalVars(const VarDeclaration& vs);
108
ethannicholas5961bc92016-10-12 06:39:56 -0700109 void writeVarDeclarations(const VarDeclarations& decl, bool global);
ethannicholasf789b382016-08-03 12:43:36 -0700110
111 void writeVariableReference(const VariableReference& ref);
112
113 void writeExpression(const Expression& expr, Precedence parentPrecedence);
114
115 void writeIntrinsicCall(const FunctionCall& c);
116
ethannicholas5961bc92016-10-12 06:39:56 -0700117 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
118
ethannicholasf789b382016-08-03 12:43:36 -0700119 void writeFunctionCall(const FunctionCall& c);
120
121 void writeConstructor(const Constructor& c);
122
123 void writeFieldAccess(const FieldAccess& f);
124
125 void writeSwizzle(const Swizzle& swizzle);
126
127 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
128
129 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
130
131 void writeIndexExpression(const IndexExpression& expr);
132
133 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
134
135 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
136
137 void writeBoolLiteral(const BoolLiteral& b);
138
139 void writeIntLiteral(const IntLiteral& i);
140
141 void writeFloatLiteral(const FloatLiteral& f);
142
143 void writeStatement(const Statement& s);
144
145 void writeBlock(const Block& b);
146
147 void writeIfStatement(const IfStatement& stmt);
148
149 void writeForStatement(const ForStatement& f);
150
151 void writeWhileStatement(const WhileStatement& w);
152
153 void writeDoStatement(const DoStatement& d);
154
155 void writeReturnStatement(const ReturnStatement& r);
156
157 const Context& fContext;
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500158 const GrGLSLCaps& fCaps;
ethannicholasddb37d62016-10-20 09:54:00 -0700159 std::ostream* fOut = nullptr;
160 std::stringstream fHeader;
ethannicholas5961bc92016-10-12 06:39:56 -0700161 std::string fFunctionHeader;
162 Program::Kind fProgramKind;
ethannicholasddb37d62016-10-20 09:54:00 -0700163 int fVarCount = 0;
164 int fIndentation = 0;
165 bool fAtLineStart = false;
ethannicholasf789b382016-08-03 12:43:36 -0700166 // Keeps track of which struct types we have written. Given that we are unlikely to ever write
167 // more than one or two structs per shader, a simple linear search will be faster than anything
168 // fancier.
169 std::vector<const Type*> fWrittenStructs;
ethannicholasddb37d62016-10-20 09:54:00 -0700170 // true if we have run into usages of dFdx / dFdy
171 bool fFoundDerivatives = false;
ethannicholasf789b382016-08-03 12:43:36 -0700172};
173
174}
175
176#endif