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