blob: 97e60381463e6c8f9a2c572cfec7d072a0bb65e0 [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
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"
36#include "ir/SkSLSwizzle.h"
37#include "ir/SkSLTernaryExpression.h"
ethannicholas22f939e2016-10-13 13:25:34 -070038#include "ir/SkSLVarDeclarations.h"
39#include "ir/SkSLVarDeclarationsStatement.h"
ethannicholasf789b382016-08-03 12:43:36 -070040#include "ir/SkSLVariableReference.h"
41#include "ir/SkSLWhileStatement.h"
42
43namespace SkSL {
44
45#define kLast_Capability SpvCapabilityMultiViewport
46
47struct GLCaps {
ethannicholasddb37d62016-10-20 09:54:00 -070048 GLCaps() {}
49
50 int fVersion = 400;
ethannicholasf789b382016-08-03 12:43:36 -070051 enum {
52 kGL_Standard,
53 kGLES_Standard
ethannicholasddb37d62016-10-20 09:54:00 -070054 } fStandard = kGL_Standard;
55 bool fIsCoreProfile = false;
56 bool fUsesPrecisionModifiers = false;
57 bool fMustDeclareFragmentShaderOutput = false;
58 bool fShaderDerivativeSupport = true;
59 // extension string to enable derivative support, or null if unnecessary
60 std::string fShaderDerivativeExtensionString;
ethannicholas5961bc92016-10-12 06:39:56 -070061 // The Tegra3 compiler will sometimes never return if we have min(abs(x), y)
ethannicholasddb37d62016-10-20 09:54:00 -070062 bool fCanUseMinAndAbsTogether = true;
ethannicholasad146f62016-10-14 06:40:02 -070063 // On Intel GPU there is an issue where it misinterprets an atan argument (second argument only,
64 // apparently) of the form "-<expr>" as an int, so we rewrite it as "-1.0 * <expr>" to avoid
65 // this problem
ethannicholasddb37d62016-10-20 09:54:00 -070066 bool fMustForceNegatedAtanParamToFloat = false;
ethannicholasf789b382016-08-03 12:43:36 -070067};
68
69/**
70 * Converts a Program into GLSL code.
71 */
72class GLSLCodeGenerator : public CodeGenerator {
73public:
74 enum Precedence {
75 kParentheses_Precedence = 1,
76 kPostfix_Precedence = 2,
77 kPrefix_Precedence = 3,
78 kMultiplicative_Precedence = 4,
79 kAdditive_Precedence = 5,
80 kShift_Precedence = 6,
81 kRelational_Precedence = 7,
82 kEquality_Precedence = 8,
83 kBitwiseAnd_Precedence = 9,
84 kBitwiseXor_Precedence = 10,
85 kBitwiseOr_Precedence = 11,
86 kLogicalAnd_Precedence = 12,
87 kLogicalXor_Precedence = 13,
88 kLogicalOr_Precedence = 14,
89 kTernary_Precedence = 15,
90 kAssignment_Precedence = 16,
91 kSequence_Precedence = 17,
92 kTopLevel_Precedence = 18
93 };
94
95 GLSLCodeGenerator(const Context* context, GLCaps caps)
96 : fContext(*context)
ethannicholasddb37d62016-10-20 09:54:00 -070097 , fCaps(caps) {}
ethannicholasf789b382016-08-03 12:43:36 -070098
99 void generateCode(const Program& program, std::ostream& out) override;
100
101private:
102 void write(const char* s);
103
104 void writeLine();
105
106 void writeLine(const char* s);
107
108 void write(const std::string& s);
109
110 void writeLine(const std::string& s);
111
112 void writeType(const Type& type);
113
114 void writeExtension(const Extension& ext);
115
116 void writeInterfaceBlock(const InterfaceBlock& intf);
117
118 void writeFunctionStart(const FunctionDeclaration& f);
119
120 void writeFunctionDeclaration(const FunctionDeclaration& f);
121
122 void writeFunction(const FunctionDefinition& f);
123
124 void writeLayout(const Layout& layout);
125
ethannicholas5961bc92016-10-12 06:39:56 -0700126 void writeModifiers(const Modifiers& modifiers, bool globalContext);
ethannicholasf789b382016-08-03 12:43:36 -0700127
128 void writeGlobalVars(const VarDeclaration& vs);
129
ethannicholas5961bc92016-10-12 06:39:56 -0700130 void writeVarDeclarations(const VarDeclarations& decl, bool global);
ethannicholasf789b382016-08-03 12:43:36 -0700131
132 void writeVariableReference(const VariableReference& ref);
133
134 void writeExpression(const Expression& expr, Precedence parentPrecedence);
135
136 void writeIntrinsicCall(const FunctionCall& c);
137
ethannicholas5961bc92016-10-12 06:39:56 -0700138 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
139
ethannicholasf789b382016-08-03 12:43:36 -0700140 void writeFunctionCall(const FunctionCall& c);
141
142 void writeConstructor(const Constructor& c);
143
144 void writeFieldAccess(const FieldAccess& f);
145
146 void writeSwizzle(const Swizzle& swizzle);
147
148 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
149
150 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
151
152 void writeIndexExpression(const IndexExpression& expr);
153
154 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
155
156 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
157
158 void writeBoolLiteral(const BoolLiteral& b);
159
160 void writeIntLiteral(const IntLiteral& i);
161
162 void writeFloatLiteral(const FloatLiteral& f);
163
164 void writeStatement(const Statement& s);
165
166 void writeBlock(const Block& b);
167
168 void writeIfStatement(const IfStatement& stmt);
169
170 void writeForStatement(const ForStatement& f);
171
172 void writeWhileStatement(const WhileStatement& w);
173
174 void writeDoStatement(const DoStatement& d);
175
176 void writeReturnStatement(const ReturnStatement& r);
177
178 const Context& fContext;
179 const GLCaps fCaps;
ethannicholasddb37d62016-10-20 09:54:00 -0700180 std::ostream* fOut = nullptr;
181 std::stringstream fHeader;
ethannicholas5961bc92016-10-12 06:39:56 -0700182 std::string fFunctionHeader;
183 Program::Kind fProgramKind;
ethannicholasddb37d62016-10-20 09:54:00 -0700184 int fVarCount = 0;
185 int fIndentation = 0;
186 bool fAtLineStart = false;
ethannicholasf789b382016-08-03 12:43:36 -0700187 // 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
189 // 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;
ethannicholasf789b382016-08-03 12:43:36 -0700193};
194
195}
196
197#endif