ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 7 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 8 | #ifndef SKSL_GLSLCODEGENERATOR |
| 9 | #define SKSL_GLSLCODEGENERATOR |
| 10 | |
| 11 | #include <stack> |
| 12 | #include <tuple> |
| 13 | #include <unordered_map> |
| 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/sksl/SkSLCodeGenerator.h" |
| 16 | #include "src/sksl/SkSLStringStream.h" |
| 17 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 18 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 19 | #include "src/sksl/ir/SkSLConstructor.h" |
| 20 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 21 | #include "src/sksl/ir/SkSLExtension.h" |
| 22 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 23 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 24 | #include "src/sksl/ir/SkSLForStatement.h" |
| 25 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 26 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 27 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 28 | #include "src/sksl/ir/SkSLFunctionPrototype.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 30 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 31 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 32 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 33 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 34 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 35 | #include "src/sksl/ir/SkSLProgramElement.h" |
| 36 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 37 | #include "src/sksl/ir/SkSLSetting.h" |
| 38 | #include "src/sksl/ir/SkSLStatement.h" |
| 39 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 40 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 41 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 42 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 43 | #include "src/sksl/ir/SkSLVariableReference.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 44 | |
| 45 | namespace SkSL { |
| 46 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Converts a Program into GLSL code. |
| 49 | */ |
| 50 | class GLSLCodeGenerator : public CodeGenerator { |
| 51 | public: |
| 52 | enum Precedence { |
| 53 | kParentheses_Precedence = 1, |
| 54 | kPostfix_Precedence = 2, |
| 55 | kPrefix_Precedence = 3, |
| 56 | kMultiplicative_Precedence = 4, |
| 57 | kAdditive_Precedence = 5, |
| 58 | kShift_Precedence = 6, |
| 59 | kRelational_Precedence = 7, |
| 60 | kEquality_Precedence = 8, |
| 61 | kBitwiseAnd_Precedence = 9, |
| 62 | kBitwiseXor_Precedence = 10, |
| 63 | kBitwiseOr_Precedence = 11, |
| 64 | kLogicalAnd_Precedence = 12, |
| 65 | kLogicalXor_Precedence = 13, |
| 66 | kLogicalOr_Precedence = 14, |
| 67 | kTernary_Precedence = 15, |
| 68 | kAssignment_Precedence = 16, |
| 69 | kSequence_Precedence = 17, |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 70 | kTopLevel_Precedence = kSequence_Precedence |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 73 | GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 74 | OutputStream* out) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 75 | : INHERITED(program, errors, out) |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 76 | , fLineEnding("\n") |
Ethan Nicholas | a45e1a7 | 2018-08-31 16:56:52 -0400 | [diff] [blame] | 77 | , fContext(*context) |
| 78 | , fProgramKind(program->fKind) {} |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 79 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 80 | bool generateCode() override; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 81 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 82 | protected: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 83 | void write(const char* s); |
| 84 | |
| 85 | void writeLine(); |
| 86 | |
| 87 | void writeLine(const char* s); |
| 88 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 89 | void write(const String& s); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 90 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 91 | void write(StringFragment s); |
| 92 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 93 | void writeLine(const String& s); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 94 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 95 | virtual void writeHeader(); |
| 96 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 97 | virtual bool usesPrecisionModifiers() const; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 98 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 99 | virtual String getTypeName(const Type& type); |
| 100 | |
Brian Osman | 02bc522 | 2021-01-28 11:00:20 -0500 | [diff] [blame^] | 101 | void writeStructDefinition(const StructDefinition& s); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 102 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 103 | void writeType(const Type& type); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 104 | |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 105 | void writeExtension(const String& name); |
| 106 | |
| 107 | void writeExtension(const String& name, bool require); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 108 | |
| 109 | void writeInterfaceBlock(const InterfaceBlock& intf); |
| 110 | |
| 111 | void writeFunctionStart(const FunctionDeclaration& f); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 112 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 113 | void writeFunctionDeclaration(const FunctionDeclaration& f); |
| 114 | |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 115 | void writeFunctionPrototype(const FunctionPrototype& f); |
| 116 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 117 | virtual void writeFunction(const FunctionDefinition& f); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 118 | |
| 119 | void writeLayout(const Layout& layout); |
| 120 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 121 | void writeModifiers(const Modifiers& modifiers, bool globalContext); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 122 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 123 | virtual void writeInputVars(); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 124 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 125 | virtual void writeVarInitializer(const Variable& var, const Expression& value); |
| 126 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 127 | const char* getTypePrecision(const Type& type); |
| 128 | |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 129 | void writeTypePrecision(const Type& type); |
| 130 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 131 | void writeVarDeclaration(const VarDeclaration& var, bool global); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 132 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 133 | void writeFragCoord(); |
| 134 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 135 | virtual void writeVariableReference(const VariableReference& ref); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 136 | |
| 137 | void writeExpression(const Expression& expr, Precedence parentPrecedence); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 138 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 139 | void writeIntrinsicCall(const FunctionCall& c); |
| 140 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 141 | void writeMinAbsHack(Expression& absExpr, Expression& otherExpr); |
| 142 | |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 143 | void writeDeterminantHack(const Expression& mat); |
| 144 | |
| 145 | void writeInverseHack(const Expression& mat); |
| 146 | |
| 147 | void writeTransposeHack(const Expression& mat); |
| 148 | |
| 149 | void writeInverseSqrtHack(const Expression& x); |
| 150 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 151 | virtual void writeFunctionCall(const FunctionCall& c); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 152 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 153 | void writeConstructor(const Constructor& c, Precedence parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 154 | |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 155 | virtual void writeFieldAccess(const FieldAccess& f); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 156 | |
Ethan Nicholas | 8239946 | 2017-10-16 12:35:44 -0400 | [diff] [blame] | 157 | virtual void writeSwizzle(const Swizzle& swizzle); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 158 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 159 | static Precedence GetBinaryPrecedence(Token::Kind op); |
| 160 | |
| 161 | virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence); |
Adrienne Walker | c02165f | 2018-08-21 11:08:11 -0700 | [diff] [blame] | 162 | void writeShortCircuitWorkaroundExpression(const BinaryExpression& b, |
| 163 | Precedence parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 164 | |
| 165 | void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence); |
| 166 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 167 | virtual void writeIndexExpression(const IndexExpression& expr); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 168 | |
| 169 | void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence); |
| 170 | |
| 171 | void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence); |
| 172 | |
| 173 | void writeBoolLiteral(const BoolLiteral& b); |
| 174 | |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 175 | virtual void writeIntLiteral(const IntLiteral& i); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 176 | |
| 177 | void writeFloatLiteral(const FloatLiteral& f); |
| 178 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 179 | virtual void writeSetting(const Setting& s); |
| 180 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 181 | void writeStatement(const Statement& s); |
| 182 | |
| 183 | void writeBlock(const Block& b); |
| 184 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 185 | virtual void writeIfStatement(const IfStatement& stmt); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 186 | |
| 187 | void writeForStatement(const ForStatement& f); |
| 188 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 189 | void writeDoStatement(const DoStatement& d); |
| 190 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 191 | virtual void writeSwitchStatement(const SwitchStatement& s); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 192 | |
Ethan Nicholas | f1b1464 | 2018-08-09 16:18:07 -0400 | [diff] [blame] | 193 | virtual void writeReturnStatement(const ReturnStatement& r); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 194 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 195 | virtual void writeProgramElement(const ProgramElement& e); |
| 196 | |
| 197 | const char* fLineEnding; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 198 | const Context& fContext; |
Ethan Nicholas | 88f6d37 | 2018-07-27 10:03:46 -0400 | [diff] [blame] | 199 | StringStream fExtensions; |
| 200 | StringStream fGlobals; |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 201 | StringStream fExtraFunctions; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 202 | String fFunctionHeader; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 203 | Program::Kind fProgramKind; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 204 | int fVarCount = 0; |
| 205 | int fIndentation = 0; |
| 206 | bool fAtLineStart = false; |
Ethan Nicholas | 6e6525c | 2018-01-03 17:03:56 -0500 | [diff] [blame] | 207 | std::set<String> fWrittenIntrinsics; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 208 | // true if we have run into usages of dFdx / dFdy |
| 209 | bool fFoundDerivatives = false; |
Brian Osman | 4b2f915 | 2018-04-17 11:19:57 -0400 | [diff] [blame] | 210 | bool fFoundExternalSamplerDecl = false; |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 211 | bool fFoundRectSamplerDecl = false; |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 212 | bool fFoundGSInvocations = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 213 | bool fSetupFragPositionGlobal = false; |
| 214 | bool fSetupFragPositionLocal = false; |
Brian Salomon | dba65f9 | 2018-01-22 08:43:38 -0500 | [diff] [blame] | 215 | bool fSetupFragCoordWorkaround = false; |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 216 | // if non-empty, replace all texture / texture2D / textureProj / etc. calls with this name |
| 217 | String fTextureFunctionOverride; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 218 | |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 219 | // We map function names to function class so we can quickly deal with function calls that need |
| 220 | // extra processing |
| 221 | enum class FunctionClass { |
Adrienne Walker | 92b161f | 2018-08-22 10:41:52 -0700 | [diff] [blame] | 222 | kAbs, |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 223 | kAtan, |
| 224 | kDeterminant, |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 225 | kDFdx, |
| 226 | kDFdy, |
| 227 | kFwidth, |
Chris Dalton | a708618 | 2018-11-16 09:33:43 -0500 | [diff] [blame] | 228 | kFMA, |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 229 | kFract, |
| 230 | kInverse, |
| 231 | kInverseSqrt, |
| 232 | kMin, |
Adrienne Walker | 2f4c09b | 2018-08-22 16:04:57 -0700 | [diff] [blame] | 233 | kPow, |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 234 | kSaturate, |
| 235 | kTexture, |
Ethan Nicholas | 5a9a9b8 | 2019-09-20 12:59:22 -0400 | [diff] [blame] | 236 | kTranspose |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 237 | }; |
| 238 | static std::unordered_map<StringFragment, FunctionClass>* fFunctionClasses; |
| 239 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 240 | using INHERITED = CodeGenerator; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 243 | } // namespace SkSL |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 244 | |
| 245 | #endif |