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 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 15 | #include "SkStream.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 | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame^] | 74 | GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
| 75 | SkWStream* out) |
| 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 | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 88 | void write(const SkString& s); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 89 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 90 | void writeLine(const SkString& 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); |
| 99 | |
| 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 | |
| 148 | void writeBlock(const Block& b); |
| 149 | |
| 150 | void writeIfStatement(const IfStatement& stmt); |
| 151 | |
| 152 | void writeForStatement(const ForStatement& f); |
| 153 | |
| 154 | void writeWhileStatement(const WhileStatement& w); |
| 155 | |
| 156 | void writeDoStatement(const DoStatement& d); |
| 157 | |
| 158 | void writeReturnStatement(const ReturnStatement& r); |
| 159 | |
| 160 | const Context& fContext; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 161 | SkDynamicMemoryWStream fHeader; |
| 162 | SkString fFunctionHeader; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 163 | Program::Kind fProgramKind; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 164 | int fVarCount = 0; |
| 165 | int fIndentation = 0; |
| 166 | bool fAtLineStart = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame^] | 167 | // Keeps track of which struct types we have written. Given that we are unlikely to ever write |
| 168 | // 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] | 169 | // fancier. |
| 170 | std::vector<const Type*> fWrittenStructs; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 171 | // true if we have run into usages of dFdx / dFdy |
| 172 | bool fFoundDerivatives = false; |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 173 | bool fFoundImageDecl = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame^] | 174 | bool fSetupFragPositionGlobal = false; |
| 175 | bool fSetupFragPositionLocal = false; |
| 176 | |
| 177 | typedef CodeGenerator INHERITED; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | } |
| 181 | |
| 182 | #endif |