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 "SkSLStringStream.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/SkSLSetting.h" 37 #include "ir/SkSLStatement.h" 38 #include "ir/SkSLSwitchStatement.h" 39 #include "ir/SkSLSwizzle.h" 40 #include "ir/SkSLTernaryExpression.h" 41 #include "ir/SkSLVarDeclarations.h" 42 #include "ir/SkSLVarDeclarationsStatement.h" 43 #include "ir/SkSLVariableReference.h" 44 #include "ir/SkSLWhileStatement.h" 45 46 namespace SkSL { 47 48 #define kLast_Capability SpvCapabilityMultiViewport 49 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, 73 kTopLevel_Precedence = kSequence_Precedence 74 }; 75 GLSLCodeGenerator(const Context * context,const Program * program,ErrorReporter * errors,OutputStream * out)76 GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, 77 OutputStream* out) 78 : INHERITED(program, errors, out) 79 , fLineEnding("\n") 80 , fContext(*context) {} 81 82 bool generateCode() override; 83 84 protected: 85 void write(const char* s); 86 87 void writeLine(); 88 89 void writeLine(const char* s); 90 91 void write(const String& s); 92 93 void writeLine(const String& s); 94 95 virtual void writeHeader(); 96 97 virtual void writePrecisionModifier(); 98 99 void writeType(const Type& type); 100 101 void writeExtension(const Extension& ext); 102 103 void writeInterfaceBlock(const InterfaceBlock& intf); 104 105 void writeFunctionStart(const FunctionDeclaration& f); 106 107 void writeFunctionDeclaration(const FunctionDeclaration& f); 108 109 virtual void writeFunction(const FunctionDefinition& f); 110 111 void writeLayout(const Layout& layout); 112 113 void writeModifiers(const Modifiers& modifiers, bool globalContext); 114 115 void writeGlobalVars(const VarDeclaration& vs); 116 117 virtual void writeVarInitializer(const Variable& var, const Expression& value); 118 119 void writeVarDeclarations(const VarDeclarations& decl, bool global); 120 121 void writeFragCoord(); 122 123 virtual void writeVariableReference(const VariableReference& ref); 124 125 void writeExpression(const Expression& expr, Precedence parentPrecedence); 126 127 void writeIntrinsicCall(const FunctionCall& c); 128 129 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr); 130 131 virtual void writeFunctionCall(const FunctionCall& c); 132 133 void writeConstructor(const Constructor& c); 134 135 void writeFieldAccess(const FieldAccess& f); 136 137 void writeSwizzle(const Swizzle& swizzle); 138 139 static Precedence GetBinaryPrecedence(Token::Kind op); 140 141 virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence); 142 143 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence); 144 145 virtual void writeIndexExpression(const IndexExpression& expr); 146 147 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence); 148 149 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence); 150 151 void writeBoolLiteral(const BoolLiteral& b); 152 153 void writeIntLiteral(const IntLiteral& i); 154 155 void writeFloatLiteral(const FloatLiteral& f); 156 157 virtual void writeSetting(const Setting& s); 158 159 void writeStatement(const Statement& s); 160 161 void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements); 162 163 void writeBlock(const Block& b); 164 165 virtual void writeIfStatement(const IfStatement& stmt); 166 167 void writeForStatement(const ForStatement& f); 168 169 void writeWhileStatement(const WhileStatement& w); 170 171 void writeDoStatement(const DoStatement& d); 172 173 virtual void writeSwitchStatement(const SwitchStatement& s); 174 175 void writeReturnStatement(const ReturnStatement& r); 176 177 virtual void writeProgramElement(const ProgramElement& e); 178 179 const char* fLineEnding; 180 const Context& fContext; 181 StringStream fHeader; 182 String fFunctionHeader; 183 Program::Kind fProgramKind; 184 int fVarCount = 0; 185 int fIndentation = 0; 186 bool fAtLineStart = false; 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; 191 // true if we have run into usages of dFdx / dFdy 192 bool fFoundDerivatives = false; 193 bool fFoundImageDecl = false; 194 bool fSetupFragPositionGlobal = false; 195 bool fSetupFragPositionLocal = false; 196 197 typedef CodeGenerator INHERITED; 198 }; 199 200 } 201 202 #endif 203