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 | */ |
| 7 | |
| 8 | #ifndef SKSL_GLSLCODEGENERATOR |
| 9 | #define SKSL_GLSLCODEGENERATOR |
| 10 | |
| 11 | #include <stack> |
| 12 | #include <tuple> |
| 13 | #include <unordered_map> |
| 14 | |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 15 | #include "glsl/GrGLSLCaps.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 16 | #include "SkSLCodeGenerator.h" |
| 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" |
| 36 | #include "ir/SkSLStatement.h" |
| 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 | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 74 | GLSLCodeGenerator(const Context* context, const GrGLSLCaps* caps) |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 75 | : fContext(*context) |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 76 | , fCaps(*caps) {} |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 77 | |
| 78 | void generateCode(const Program& program, std::ostream& out) override; |
| 79 | |
| 80 | private: |
| 81 | void write(const char* s); |
| 82 | |
| 83 | void writeLine(); |
| 84 | |
| 85 | void writeLine(const char* s); |
| 86 | |
| 87 | void write(const std::string& s); |
| 88 | |
| 89 | void writeLine(const std::string& s); |
| 90 | |
| 91 | void writeType(const Type& type); |
| 92 | |
| 93 | void writeExtension(const Extension& ext); |
| 94 | |
| 95 | void writeInterfaceBlock(const InterfaceBlock& intf); |
| 96 | |
| 97 | void writeFunctionStart(const FunctionDeclaration& f); |
| 98 | |
| 99 | void writeFunctionDeclaration(const FunctionDeclaration& f); |
| 100 | |
| 101 | void writeFunction(const FunctionDefinition& f); |
| 102 | |
| 103 | void writeLayout(const Layout& layout); |
| 104 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 105 | void writeModifiers(const Modifiers& modifiers, bool globalContext); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 106 | |
| 107 | void writeGlobalVars(const VarDeclaration& vs); |
| 108 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 109 | void writeVarDeclarations(const VarDeclarations& decl, bool global); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 110 | |
| 111 | void writeVariableReference(const VariableReference& ref); |
| 112 | |
| 113 | void writeExpression(const Expression& expr, Precedence parentPrecedence); |
| 114 | |
| 115 | void writeIntrinsicCall(const FunctionCall& c); |
| 116 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 117 | void writeMinAbsHack(Expression& absExpr, Expression& otherExpr); |
| 118 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 119 | void writeFunctionCall(const FunctionCall& c); |
| 120 | |
| 121 | void writeConstructor(const Constructor& c); |
| 122 | |
| 123 | void writeFieldAccess(const FieldAccess& f); |
| 124 | |
| 125 | void writeSwizzle(const Swizzle& swizzle); |
| 126 | |
| 127 | void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence); |
| 128 | |
| 129 | void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence); |
| 130 | |
| 131 | void writeIndexExpression(const IndexExpression& expr); |
| 132 | |
| 133 | void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence); |
| 134 | |
| 135 | void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence); |
| 136 | |
| 137 | void writeBoolLiteral(const BoolLiteral& b); |
| 138 | |
| 139 | void writeIntLiteral(const IntLiteral& i); |
| 140 | |
| 141 | void writeFloatLiteral(const FloatLiteral& f); |
| 142 | |
| 143 | void writeStatement(const Statement& s); |
| 144 | |
| 145 | void writeBlock(const Block& b); |
| 146 | |
| 147 | void writeIfStatement(const IfStatement& stmt); |
| 148 | |
| 149 | void writeForStatement(const ForStatement& f); |
| 150 | |
| 151 | void writeWhileStatement(const WhileStatement& w); |
| 152 | |
| 153 | void writeDoStatement(const DoStatement& d); |
| 154 | |
| 155 | void writeReturnStatement(const ReturnStatement& r); |
| 156 | |
| 157 | const Context& fContext; |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 158 | const GrGLSLCaps& fCaps; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 159 | std::ostream* fOut = nullptr; |
| 160 | std::stringstream fHeader; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 161 | std::string fFunctionHeader; |
| 162 | Program::Kind fProgramKind; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 163 | int fVarCount = 0; |
| 164 | int fIndentation = 0; |
| 165 | bool fAtLineStart = false; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 166 | // Keeps track of which struct types we have written. Given that we are unlikely to ever write |
| 167 | // more than one or two structs per shader, a simple linear search will be faster than anything |
| 168 | // fancier. |
| 169 | std::vector<const Type*> fWrittenStructs; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 170 | // true if we have run into usages of dFdx / dFdy |
| 171 | bool fFoundDerivatives = false; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | } |
| 175 | |
| 176 | #endif |