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