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 | |
| 15 | #include "SkSLCodeGenerator.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 16 | #include "SkSLStringStream.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 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" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 36 | #include "ir/SkSLSetting.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 37 | #include "ir/SkSLStatement.h" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 38 | #include "ir/SkSLSwitchStatement.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 39 | #include "ir/SkSLSwizzle.h" |
| 40 | #include "ir/SkSLTernaryExpression.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 41 | #include "ir/SkSLVarDeclarations.h" |
| 42 | #include "ir/SkSLVarDeclarationsStatement.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 43 | #include "ir/SkSLVariableReference.h" |
| 44 | #include "ir/SkSLWhileStatement.h" |
| 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 | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 80 | , fContext(*context) {} |
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 | bool generateCode() override; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 83 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 84 | protected: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 85 | void write(const char* s); |
| 86 | |
| 87 | void writeLine(); |
| 88 | |
| 89 | void writeLine(const char* s); |
| 90 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 91 | void write(const String& s); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 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 | 88d99c6 | 2017-08-16 16:41:30 -0400 | [diff] [blame] | 97 | virtual bool usesPrecisionModifiers() const; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 98 | |
Ethan Nicholas | 88d99c6 | 2017-08-16 16:41:30 -0400 | [diff] [blame] | 99 | virtual String getTypeName(const Type& type); |
| 100 | |
| 101 | void writeType(const Type& type); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 102 | |
| 103 | void writeExtension(const Extension& ext); |
| 104 | |
| 105 | void writeInterfaceBlock(const InterfaceBlock& intf); |
| 106 | |
| 107 | void writeFunctionStart(const FunctionDeclaration& f); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 108 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 109 | void writeFunctionDeclaration(const FunctionDeclaration& f); |
| 110 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 111 | virtual void writeFunction(const FunctionDefinition& f); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 112 | |
| 113 | void writeLayout(const Layout& layout); |
| 114 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 115 | void writeModifiers(const Modifiers& modifiers, bool globalContext); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 116 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 117 | void writeGlobalVars(const VarDeclaration& vs); |
| 118 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 119 | virtual void writeVarInitializer(const Variable& var, const Expression& value); |
| 120 | |
Ethan Nicholas | 88d99c6 | 2017-08-16 16:41:30 -0400 | [diff] [blame] | 121 | const char* getTypePrecision(const Type& type); |
| 122 | |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 123 | void writeTypePrecision(const Type& type); |
| 124 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 125 | void writeVarDeclarations(const VarDeclarations& decl, bool global); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 126 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 127 | void writeFragCoord(); |
| 128 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 129 | virtual void writeVariableReference(const VariableReference& ref); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 130 | |
| 131 | void writeExpression(const Expression& expr, Precedence parentPrecedence); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 132 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 133 | void writeIntrinsicCall(const FunctionCall& c); |
| 134 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 135 | void writeMinAbsHack(Expression& absExpr, Expression& otherExpr); |
| 136 | |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 137 | virtual void writeFunctionCall(const FunctionCall& c); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 138 | |
Ethan Nicholas | 88d99c6 | 2017-08-16 16:41:30 -0400 | [diff] [blame] | 139 | void writeConstructor(const Constructor& c, Precedence parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 140 | |
| 141 | void writeFieldAccess(const FieldAccess& f); |
| 142 | |
| 143 | void writeSwizzle(const Swizzle& swizzle); |
| 144 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 145 | static Precedence GetBinaryPrecedence(Token::Kind op); |
| 146 | |
| 147 | virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 148 | |
| 149 | void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence); |
| 150 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 151 | virtual void writeIndexExpression(const IndexExpression& expr); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 152 | |
| 153 | void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence); |
| 154 | |
| 155 | void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence); |
| 156 | |
| 157 | void writeBoolLiteral(const BoolLiteral& b); |
| 158 | |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 159 | virtual void writeIntLiteral(const IntLiteral& i); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 160 | |
| 161 | void writeFloatLiteral(const FloatLiteral& f); |
| 162 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 163 | virtual void writeSetting(const Setting& s); |
| 164 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 165 | void writeStatement(const Statement& s); |
| 166 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 167 | void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements); |
| 168 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 169 | void writeBlock(const Block& b); |
| 170 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 171 | virtual void writeIfStatement(const IfStatement& stmt); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 172 | |
| 173 | void writeForStatement(const ForStatement& f); |
| 174 | |
| 175 | void writeWhileStatement(const WhileStatement& w); |
| 176 | |
| 177 | void writeDoStatement(const DoStatement& d); |
| 178 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 179 | virtual void writeSwitchStatement(const SwitchStatement& s); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 180 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 181 | void writeReturnStatement(const ReturnStatement& r); |
| 182 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 183 | virtual void writeProgramElement(const ProgramElement& e); |
| 184 | |
| 185 | const char* fLineEnding; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 186 | const Context& fContext; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 187 | StringStream fHeader; |
| 188 | String fFunctionHeader; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 189 | Program::Kind fProgramKind; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 190 | int fVarCount = 0; |
| 191 | int fIndentation = 0; |
| 192 | bool fAtLineStart = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 193 | // Keeps track of which struct types we have written. Given that we are unlikely to ever write |
| 194 | // 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] | 195 | // fancier. |
| 196 | std::vector<const Type*> fWrittenStructs; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 197 | // true if we have run into usages of dFdx / dFdy |
| 198 | bool fFoundDerivatives = false; |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 199 | bool fFoundImageDecl = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 200 | bool fSetupFragPositionGlobal = false; |
| 201 | bool fSetupFragPositionLocal = false; |
| 202 | |
| 203 | typedef CodeGenerator INHERITED; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | #endif |