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" |
| 16 | #include "ir/SkSLBinaryExpression.h" |
| 17 | #include "ir/SkSLBoolLiteral.h" |
| 18 | #include "ir/SkSLConstructor.h" |
| 19 | #include "ir/SkSLDoStatement.h" |
| 20 | #include "ir/SkSLExtension.h" |
| 21 | #include "ir/SkSLFloatLiteral.h" |
| 22 | #include "ir/SkSLIfStatement.h" |
| 23 | #include "ir/SkSLIndexExpression.h" |
| 24 | #include "ir/SkSLInterfaceBlock.h" |
| 25 | #include "ir/SkSLIntLiteral.h" |
| 26 | #include "ir/SkSLFieldAccess.h" |
| 27 | #include "ir/SkSLForStatement.h" |
| 28 | #include "ir/SkSLFunctionCall.h" |
| 29 | #include "ir/SkSLFunctionDeclaration.h" |
| 30 | #include "ir/SkSLFunctionDefinition.h" |
| 31 | #include "ir/SkSLPrefixExpression.h" |
| 32 | #include "ir/SkSLPostfixExpression.h" |
| 33 | #include "ir/SkSLProgramElement.h" |
| 34 | #include "ir/SkSLReturnStatement.h" |
| 35 | #include "ir/SkSLStatement.h" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 36 | #include "ir/SkSLSwitchStatement.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 37 | #include "ir/SkSLSwizzle.h" |
| 38 | #include "ir/SkSLTernaryExpression.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 39 | #include "ir/SkSLVarDeclarations.h" |
| 40 | #include "ir/SkSLVarDeclarationsStatement.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 41 | #include "ir/SkSLVariableReference.h" |
| 42 | #include "ir/SkSLWhileStatement.h" |
| 43 | |
| 44 | namespace SkSL { |
| 45 | |
| 46 | #define kLast_Capability SpvCapabilityMultiViewport |
| 47 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Converts a Program into GLSL code. |
| 50 | */ |
| 51 | class GLSLCodeGenerator : public CodeGenerator { |
| 52 | public: |
| 53 | enum Precedence { |
| 54 | kParentheses_Precedence = 1, |
| 55 | kPostfix_Precedence = 2, |
| 56 | kPrefix_Precedence = 3, |
| 57 | kMultiplicative_Precedence = 4, |
| 58 | kAdditive_Precedence = 5, |
| 59 | kShift_Precedence = 6, |
| 60 | kRelational_Precedence = 7, |
| 61 | kEquality_Precedence = 8, |
| 62 | kBitwiseAnd_Precedence = 9, |
| 63 | kBitwiseXor_Precedence = 10, |
| 64 | kBitwiseOr_Precedence = 11, |
| 65 | kLogicalAnd_Precedence = 12, |
| 66 | kLogicalXor_Precedence = 13, |
| 67 | kLogicalOr_Precedence = 14, |
| 68 | kTernary_Precedence = 15, |
| 69 | kAssignment_Precedence = 16, |
| 70 | kSequence_Precedence = 17, |
| 71 | kTopLevel_Precedence = 18 |
| 72 | }; |
| 73 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 74 | GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 75 | OutputStream* out) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 76 | : INHERITED(program, errors, out) |
| 77 | , fContext(*context) {} |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 78 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 79 | virtual bool generateCode() override; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | void write(const char* s); |
| 83 | |
| 84 | void writeLine(); |
| 85 | |
| 86 | void writeLine(const char* s); |
| 87 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 88 | void write(const String& s); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 89 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 90 | void writeLine(const String& s); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 91 | |
| 92 | void writeType(const Type& type); |
| 93 | |
| 94 | void writeExtension(const Extension& ext); |
| 95 | |
| 96 | void writeInterfaceBlock(const InterfaceBlock& intf); |
| 97 | |
| 98 | void writeFunctionStart(const FunctionDeclaration& f); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 99 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 100 | void writeFunctionDeclaration(const FunctionDeclaration& f); |
| 101 | |
| 102 | void writeFunction(const FunctionDefinition& f); |
| 103 | |
| 104 | void writeLayout(const Layout& layout); |
| 105 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 106 | void writeModifiers(const Modifiers& modifiers, bool globalContext); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 107 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 108 | void writeGlobalVars(const VarDeclaration& vs); |
| 109 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 110 | void writeVarDeclarations(const VarDeclarations& decl, bool global); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 111 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 112 | void writeFragCoord(); |
| 113 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 114 | void writeVariableReference(const VariableReference& ref); |
| 115 | |
| 116 | void writeExpression(const Expression& expr, Precedence parentPrecedence); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 117 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 118 | void writeIntrinsicCall(const FunctionCall& c); |
| 119 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 120 | void writeMinAbsHack(Expression& absExpr, Expression& otherExpr); |
| 121 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 122 | void writeFunctionCall(const FunctionCall& c); |
| 123 | |
| 124 | void writeConstructor(const Constructor& c); |
| 125 | |
| 126 | void writeFieldAccess(const FieldAccess& f); |
| 127 | |
| 128 | void writeSwizzle(const Swizzle& swizzle); |
| 129 | |
| 130 | void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence); |
| 131 | |
| 132 | void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence); |
| 133 | |
| 134 | void writeIndexExpression(const IndexExpression& expr); |
| 135 | |
| 136 | void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence); |
| 137 | |
| 138 | void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence); |
| 139 | |
| 140 | void writeBoolLiteral(const BoolLiteral& b); |
| 141 | |
| 142 | void writeIntLiteral(const IntLiteral& i); |
| 143 | |
| 144 | void writeFloatLiteral(const FloatLiteral& f); |
| 145 | |
| 146 | void writeStatement(const Statement& s); |
| 147 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame^] | 148 | void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements); |
| 149 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 150 | void writeBlock(const Block& b); |
| 151 | |
| 152 | void writeIfStatement(const IfStatement& stmt); |
| 153 | |
| 154 | void writeForStatement(const ForStatement& f); |
| 155 | |
| 156 | void writeWhileStatement(const WhileStatement& w); |
| 157 | |
| 158 | void writeDoStatement(const DoStatement& d); |
| 159 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 160 | void writeSwitchStatement(const SwitchStatement& s); |
| 161 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 162 | void writeReturnStatement(const ReturnStatement& r); |
| 163 | |
| 164 | const Context& fContext; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 165 | StringStream fHeader; |
| 166 | String fFunctionHeader; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 167 | Program::Kind fProgramKind; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 168 | int fVarCount = 0; |
| 169 | int fIndentation = 0; |
| 170 | bool fAtLineStart = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 171 | // Keeps track of which struct types we have written. Given that we are unlikely to ever write |
| 172 | // 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] | 173 | // fancier. |
| 174 | std::vector<const Type*> fWrittenStructs; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 175 | // true if we have run into usages of dFdx / dFdy |
| 176 | bool fFoundDerivatives = false; |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 177 | bool fFoundImageDecl = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 178 | bool fSetupFragPositionGlobal = false; |
| 179 | bool fSetupFragPositionLocal = false; |
| 180 | |
| 181 | typedef CodeGenerator INHERITED; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | } |
| 185 | |
| 186 | #endif |