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 | |
| 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" |
| 36 | #include "ir/SkSLSwizzle.h" |
| 37 | #include "ir/SkSLTernaryExpression.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 38 | #include "ir/SkSLVarDeclarations.h" |
| 39 | #include "ir/SkSLVarDeclarationsStatement.h" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 40 | #include "ir/SkSLVariableReference.h" |
| 41 | #include "ir/SkSLWhileStatement.h" |
| 42 | |
| 43 | namespace SkSL { |
| 44 | |
| 45 | #define kLast_Capability SpvCapabilityMultiViewport |
| 46 | |
| 47 | struct GLCaps { |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame^] | 48 | GLCaps() {} |
| 49 | |
| 50 | int fVersion = 400; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 51 | enum { |
| 52 | kGL_Standard, |
| 53 | kGLES_Standard |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame^] | 54 | } fStandard = kGL_Standard; |
| 55 | bool fIsCoreProfile = false; |
| 56 | bool fUsesPrecisionModifiers = false; |
| 57 | bool fMustDeclareFragmentShaderOutput = false; |
| 58 | bool fShaderDerivativeSupport = true; |
| 59 | // extension string to enable derivative support, or null if unnecessary |
| 60 | std::string fShaderDerivativeExtensionString; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 61 | // The Tegra3 compiler will sometimes never return if we have min(abs(x), y) |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame^] | 62 | bool fCanUseMinAndAbsTogether = true; |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 63 | // On Intel GPU there is an issue where it misinterprets an atan argument (second argument only, |
| 64 | // apparently) of the form "-<expr>" as an int, so we rewrite it as "-1.0 * <expr>" to avoid |
| 65 | // this problem |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame^] | 66 | bool fMustForceNegatedAtanParamToFloat = false; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /** |
| 70 | * Converts a Program into GLSL code. |
| 71 | */ |
| 72 | class GLSLCodeGenerator : public CodeGenerator { |
| 73 | public: |
| 74 | enum Precedence { |
| 75 | kParentheses_Precedence = 1, |
| 76 | kPostfix_Precedence = 2, |
| 77 | kPrefix_Precedence = 3, |
| 78 | kMultiplicative_Precedence = 4, |
| 79 | kAdditive_Precedence = 5, |
| 80 | kShift_Precedence = 6, |
| 81 | kRelational_Precedence = 7, |
| 82 | kEquality_Precedence = 8, |
| 83 | kBitwiseAnd_Precedence = 9, |
| 84 | kBitwiseXor_Precedence = 10, |
| 85 | kBitwiseOr_Precedence = 11, |
| 86 | kLogicalAnd_Precedence = 12, |
| 87 | kLogicalXor_Precedence = 13, |
| 88 | kLogicalOr_Precedence = 14, |
| 89 | kTernary_Precedence = 15, |
| 90 | kAssignment_Precedence = 16, |
| 91 | kSequence_Precedence = 17, |
| 92 | kTopLevel_Precedence = 18 |
| 93 | }; |
| 94 | |
| 95 | GLSLCodeGenerator(const Context* context, GLCaps caps) |
| 96 | : fContext(*context) |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame^] | 97 | , fCaps(caps) {} |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 98 | |
| 99 | void generateCode(const Program& program, std::ostream& out) override; |
| 100 | |
| 101 | private: |
| 102 | void write(const char* s); |
| 103 | |
| 104 | void writeLine(); |
| 105 | |
| 106 | void writeLine(const char* s); |
| 107 | |
| 108 | void write(const std::string& s); |
| 109 | |
| 110 | void writeLine(const std::string& s); |
| 111 | |
| 112 | void writeType(const Type& type); |
| 113 | |
| 114 | void writeExtension(const Extension& ext); |
| 115 | |
| 116 | void writeInterfaceBlock(const InterfaceBlock& intf); |
| 117 | |
| 118 | void writeFunctionStart(const FunctionDeclaration& f); |
| 119 | |
| 120 | void writeFunctionDeclaration(const FunctionDeclaration& f); |
| 121 | |
| 122 | void writeFunction(const FunctionDefinition& f); |
| 123 | |
| 124 | void writeLayout(const Layout& layout); |
| 125 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 126 | void writeModifiers(const Modifiers& modifiers, bool globalContext); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 127 | |
| 128 | void writeGlobalVars(const VarDeclaration& vs); |
| 129 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 130 | void writeVarDeclarations(const VarDeclarations& decl, bool global); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 131 | |
| 132 | void writeVariableReference(const VariableReference& ref); |
| 133 | |
| 134 | void writeExpression(const Expression& expr, Precedence parentPrecedence); |
| 135 | |
| 136 | void writeIntrinsicCall(const FunctionCall& c); |
| 137 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 138 | void writeMinAbsHack(Expression& absExpr, Expression& otherExpr); |
| 139 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 140 | void writeFunctionCall(const FunctionCall& c); |
| 141 | |
| 142 | void writeConstructor(const Constructor& c); |
| 143 | |
| 144 | void writeFieldAccess(const FieldAccess& f); |
| 145 | |
| 146 | void writeSwizzle(const Swizzle& swizzle); |
| 147 | |
| 148 | void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence); |
| 149 | |
| 150 | void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence); |
| 151 | |
| 152 | void writeIndexExpression(const IndexExpression& expr); |
| 153 | |
| 154 | void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence); |
| 155 | |
| 156 | void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence); |
| 157 | |
| 158 | void writeBoolLiteral(const BoolLiteral& b); |
| 159 | |
| 160 | void writeIntLiteral(const IntLiteral& i); |
| 161 | |
| 162 | void writeFloatLiteral(const FloatLiteral& f); |
| 163 | |
| 164 | void writeStatement(const Statement& s); |
| 165 | |
| 166 | void writeBlock(const Block& b); |
| 167 | |
| 168 | void writeIfStatement(const IfStatement& stmt); |
| 169 | |
| 170 | void writeForStatement(const ForStatement& f); |
| 171 | |
| 172 | void writeWhileStatement(const WhileStatement& w); |
| 173 | |
| 174 | void writeDoStatement(const DoStatement& d); |
| 175 | |
| 176 | void writeReturnStatement(const ReturnStatement& r); |
| 177 | |
| 178 | const Context& fContext; |
| 179 | const GLCaps fCaps; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame^] | 180 | std::ostream* fOut = nullptr; |
| 181 | std::stringstream fHeader; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 182 | std::string fFunctionHeader; |
| 183 | Program::Kind fProgramKind; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame^] | 184 | int fVarCount = 0; |
| 185 | int fIndentation = 0; |
| 186 | bool fAtLineStart = false; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 187 | // Keeps track of which struct types we have written. Given that we are unlikely to ever write |
| 188 | // more than one or two structs per shader, a simple linear search will be faster than anything |
| 189 | // fancier. |
| 190 | std::vector<const Type*> fWrittenStructs; |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame^] | 191 | // true if we have run into usages of dFdx / dFdy |
| 192 | bool fFoundDerivatives = false; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | } |
| 196 | |
| 197 | #endif |