blob: e20f65a2290766402aa4d00651a325fc07705191 [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
Brian Osman00185012021-02-04 16:07:11 -050011#include <set>
ethannicholasf789b382016-08-03 12:43:36 -070012#include <stack>
13#include <tuple>
14#include <unordered_map>
15
Ethan Nicholas24c17722021-03-09 13:10:59 -050016#include "include/private/SkSLProgramElement.h"
17#include "include/private/SkSLStatement.h"
Brian Osman00185012021-02-04 16:07:11 -050018#include "src/sksl/SkSLOperators.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/sksl/SkSLStringStream.h"
John Stiles3738ef52021-04-13 10:41:57 -040020#include "src/sksl/codegen/SkSLCodeGenerator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/sksl/ir/SkSLBinaryExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/sksl/ir/SkSLConstructor.h"
John Stiles3c74a532021-05-18 17:31:48 -040023#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
John Stilesfd7252f2021-04-04 22:24:40 -040024#include "src/sksl/ir/SkSLConstructorScalarCast.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/sksl/ir/SkSLDoStatement.h"
26#include "src/sksl/ir/SkSLExtension.h"
27#include "src/sksl/ir/SkSLFieldAccess.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/sksl/ir/SkSLForStatement.h"
29#include "src/sksl/ir/SkSLFunctionCall.h"
30#include "src/sksl/ir/SkSLFunctionDeclaration.h"
31#include "src/sksl/ir/SkSLFunctionDefinition.h"
John Stiles569249b2020-11-03 12:18:22 -050032#include "src/sksl/ir/SkSLFunctionPrototype.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/sksl/ir/SkSLIfStatement.h"
34#include "src/sksl/ir/SkSLIndexExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/sksl/ir/SkSLInterfaceBlock.h"
John Stiles7591d4b2021-09-13 13:32:06 -040036#include "src/sksl/ir/SkSLLiteral.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/sksl/ir/SkSLPostfixExpression.h"
38#include "src/sksl/ir/SkSLPrefixExpression.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/sksl/ir/SkSLReturnStatement.h"
40#include "src/sksl/ir/SkSLSetting.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/sksl/ir/SkSLSwitchStatement.h"
42#include "src/sksl/ir/SkSLSwizzle.h"
43#include "src/sksl/ir/SkSLTernaryExpression.h"
44#include "src/sksl/ir/SkSLVarDeclarations.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/sksl/ir/SkSLVariableReference.h"
ethannicholasf789b382016-08-03 12:43:36 -070046
47namespace SkSL {
48
ethannicholasf789b382016-08-03 12:43:36 -070049/**
50 * Converts a Program into GLSL code.
51 */
52class GLSLCodeGenerator : public CodeGenerator {
53public:
Ethan Nicholas3abc6c62021-08-13 11:20:09 -040054 GLSLCodeGenerator(const Context* context, const Program* program, OutputStream* out)
55 : INHERITED(context, program, out)
56 , fLineEnding("\n") {}
ethannicholasf789b382016-08-03 12:43:36 -070057
Ethan Nicholas762466e2017-06-29 10:03:38 -040058 bool generateCode() override;
ethannicholasf789b382016-08-03 12:43:36 -070059
Ethan Nicholas762466e2017-06-29 10:03:38 -040060protected:
John Stiles45990502021-02-16 10:55:27 -050061 using Precedence = Operator::Precedence;
Brian Osman00185012021-02-04 16:07:11 -050062
Ethan Nicholas962dec42021-06-10 13:06:39 -040063 void write(skstd::string_view s);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070064
Ethan Nicholas962dec42021-06-10 13:06:39 -040065 void writeLine(skstd::string_view s = skstd::string_view());
ethannicholasf789b382016-08-03 12:43:36 -070066
John Stilese8b5a732021-03-12 13:25:52 -050067 void finishLine();
68
Ethan Nicholas762466e2017-06-29 10:03:38 -040069 virtual void writeHeader();
70
Ethan Nicholasf7b88202017-09-18 14:10:39 -040071 virtual bool usesPrecisionModifiers() const;
Ethan Nicholas762466e2017-06-29 10:03:38 -040072
Ethan Nicholasf7b88202017-09-18 14:10:39 -040073 virtual String getTypeName(const Type& type);
74
Brian Osman02bc5222021-01-28 11:00:20 -050075 void writeStructDefinition(const StructDefinition& s);
John Stilesdc75a972020-11-25 16:24:55 -050076
Ethan Nicholasf7b88202017-09-18 14:10:39 -040077 void writeType(const Type& type);
ethannicholasf789b382016-08-03 12:43:36 -070078
Ethan Nicholas962dec42021-06-10 13:06:39 -040079 void writeExtension(skstd::string_view name, bool require = true);
ethannicholasf789b382016-08-03 12:43:36 -070080
81 void writeInterfaceBlock(const InterfaceBlock& intf);
82
83 void writeFunctionStart(const FunctionDeclaration& f);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040084
ethannicholasf789b382016-08-03 12:43:36 -070085 void writeFunctionDeclaration(const FunctionDeclaration& f);
86
John Stiles569249b2020-11-03 12:18:22 -050087 void writeFunctionPrototype(const FunctionPrototype& f);
88
Ethan Nicholas762466e2017-06-29 10:03:38 -040089 virtual void writeFunction(const FunctionDefinition& f);
ethannicholasf789b382016-08-03 12:43:36 -070090
91 void writeLayout(const Layout& layout);
92
ethannicholas5961bc92016-10-12 06:39:56 -070093 void writeModifiers(const Modifiers& modifiers, bool globalContext);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050094
Ethan Nicholascd700e92018-08-24 16:43:57 -040095 virtual void writeInputVars();
Ethan Nicholas82a62d22017-11-07 14:42:10 +000096
Ethan Nicholas762466e2017-06-29 10:03:38 -040097 virtual void writeVarInitializer(const Variable& var, const Expression& value);
98
Ethan Nicholasf7b88202017-09-18 14:10:39 -040099 const char* getTypePrecision(const Type& type);
100
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400101 void writeTypePrecision(const Type& type);
102
Brian Osmanc0213602020-10-06 14:43:32 -0400103 void writeVarDeclaration(const VarDeclaration& var, bool global);
ethannicholasf789b382016-08-03 12:43:36 -0700104
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500105 void writeFragCoord();
106
Ethan Nicholas762466e2017-06-29 10:03:38 -0400107 virtual void writeVariableReference(const VariableReference& ref);
ethannicholasf789b382016-08-03 12:43:36 -0700108
109 void writeExpression(const Expression& expr, Precedence parentPrecedence);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500110
ethannicholasf789b382016-08-03 12:43:36 -0700111 void writeIntrinsicCall(const FunctionCall& c);
112
ethannicholas5961bc92016-10-12 06:39:56 -0700113 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
114
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500115 void writeDeterminantHack(const Expression& mat);
116
117 void writeInverseHack(const Expression& mat);
118
119 void writeTransposeHack(const Expression& mat);
120
121 void writeInverseSqrtHack(const Expression& x);
122
John Stilesf061c592021-05-21 23:34:42 -0400123 void writeMatrixComparisonWorkaround(const BinaryExpression& x);
124
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400125 virtual void writeFunctionCall(const FunctionCall& c);
ethannicholasf789b382016-08-03 12:43:36 -0700126
John Stiles3c74a532021-05-18 17:31:48 -0400127 void writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c,
128 Precedence parentPrecedence);
129
John Stiles95680232021-04-22 15:05:20 -0400130 virtual void writeAnyConstructor(const AnyConstructor& c, Precedence parentPrecedence);
John Stiles626b62e2021-03-31 22:06:07 -0400131
John Stiles95680232021-04-22 15:05:20 -0400132 virtual void writeCastConstructor(const AnyConstructor& c, Precedence parentPrecedence);
John Stilesb14a8192021-04-05 11:40:46 -0400133
Michael Ludwig9094f2c2018-09-07 13:44:21 -0400134 virtual void writeFieldAccess(const FieldAccess& f);
ethannicholasf789b382016-08-03 12:43:36 -0700135
Ethan Nicholas82399462017-10-16 12:35:44 -0400136 virtual void writeSwizzle(const Swizzle& swizzle);
ethannicholasf789b382016-08-03 12:43:36 -0700137
Ethan Nicholas762466e2017-06-29 10:03:38 -0400138 virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
John Stiles933043b2021-03-31 15:23:16 -0400139
Adrienne Walkerc02165f2018-08-21 11:08:11 -0700140 void writeShortCircuitWorkaroundExpression(const BinaryExpression& b,
141 Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700142
John Stiles95680232021-04-22 15:05:20 -0400143 virtual void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
ethannicholasf789b382016-08-03 12:43:36 -0700144
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
John Stiles7591d4b2021-09-13 13:32:06 -0400151 virtual void writeLiteral(const Literal& l);
ethannicholasf789b382016-08-03 12:43:36 -0700152
Ethan Nicholas762466e2017-06-29 10:03:38 -0400153 virtual void writeSetting(const Setting& s);
154
ethannicholasf789b382016-08-03 12:43:36 -0700155 void writeStatement(const Statement& s);
156
157 void writeBlock(const Block& b);
158
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400159 virtual void writeIfStatement(const IfStatement& stmt);
ethannicholasf789b382016-08-03 12:43:36 -0700160
161 void writeForStatement(const ForStatement& f);
162
ethannicholasf789b382016-08-03 12:43:36 -0700163 void writeDoStatement(const DoStatement& d);
164
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400165 virtual void writeSwitchStatement(const SwitchStatement& s);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500166
Ethan Nicholasf1b14642018-08-09 16:18:07 -0400167 virtual void writeReturnStatement(const ReturnStatement& r);
ethannicholasf789b382016-08-03 12:43:36 -0700168
Ethan Nicholas762466e2017-06-29 10:03:38 -0400169 virtual void writeProgramElement(const ProgramElement& e);
170
John Stilesc1a98b82021-02-24 13:35:02 -0500171 const ShaderCapsClass& caps() const { return fContext.fCaps; }
172
Ethan Nicholas762466e2017-06-29 10:03:38 -0400173 const char* fLineEnding;
Ethan Nicholas88f6d372018-07-27 10:03:46 -0400174 StringStream fExtensions;
175 StringStream fGlobals;
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500176 StringStream fExtraFunctions;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400177 String fFunctionHeader;
ethannicholasddb37d62016-10-20 09:54:00 -0700178 int fVarCount = 0;
179 int fIndentation = 0;
180 bool fAtLineStart = false;
Ethan Nicholas6e6525c2018-01-03 17:03:56 -0500181 std::set<String> fWrittenIntrinsics;
ethannicholasddb37d62016-10-20 09:54:00 -0700182 // true if we have run into usages of dFdx / dFdy
183 bool fFoundDerivatives = false;
Brian Osman4b2f9152018-04-17 11:19:57 -0400184 bool fFoundExternalSamplerDecl = false;
Brian Salomon67529b22019-08-13 15:31:04 -0400185 bool fFoundRectSamplerDecl = false;
Brian Salomond8d85b92021-07-07 09:41:17 -0400186 bool fSetupClockwise = false;
187 bool fSetupFragPosition = false;
Brian Salomondba65f92018-01-22 08:43:38 -0500188 bool fSetupFragCoordWorkaround = false;
Ethan Nicholas13863662019-07-29 13:05:15 -0400189 // if non-empty, replace all texture / texture2D / textureProj / etc. calls with this name
190 String fTextureFunctionOverride;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500191
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400192 // We map function names to function class so we can quickly deal with function calls that need
193 // extra processing
194 enum class FunctionClass {
Adrienne Walker92b161f2018-08-22 10:41:52 -0700195 kAbs,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400196 kAtan,
197 kDeterminant,
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700198 kDFdx,
199 kDFdy,
200 kFwidth,
Chris Daltona7086182018-11-16 09:33:43 -0500201 kFMA,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400202 kFract,
203 kInverse,
204 kInverseSqrt,
205 kMin,
Adrienne Walker2f4c09b2018-08-22 16:04:57 -0700206 kPow,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400207 kSaturate,
208 kTexture,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -0400209 kTranspose
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400210 };
Ethan Nicholas962dec42021-06-10 13:06:39 -0400211 static std::unordered_map<skstd::string_view, FunctionClass>* fFunctionClasses;
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400212
John Stiles7571f9e2020-09-02 22:42:33 -0400213 using INHERITED = CodeGenerator;
ethannicholasf789b382016-08-03 12:43:36 -0700214};
215
John Stilesa6841be2020-08-06 14:11:56 -0400216} // namespace SkSL
ethannicholasf789b382016-08-03 12:43:36 -0700217
218#endif