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 "SkStream.h" 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/SkSLSwitchStatement.h" 38 #include "ir/SkSLSwizzle.h" 39 #include "ir/SkSLTernaryExpression.h" 40 #include "ir/SkSLVarDeclarations.h" 41 #include "ir/SkSLVarDeclarationsStatement.h" 42 #include "ir/SkSLVariableReference.h" 43 #include "ir/SkSLWhileStatement.h" 44 45 namespace SkSL { 46 47 #define kLast_Capability SpvCapabilityMultiViewport 48 49 /** 50 * Converts a Program into GLSL code. 51 */ 52 class GLSLCodeGenerator : public CodeGenerator { 53 public: 54 enum Precedence { 55 kParentheses_Precedence = 1, 56 kPostfix_Precedence = 2, 57 kPrefix_Precedence = 3, 58 kMultiplicative_Precedence = 4, 59 kAdditive_Precedence = 5, 60 kShift_Precedence = 6, 61 kRelational_Precedence = 7, 62 kEquality_Precedence = 8, 63 kBitwiseAnd_Precedence = 9, 64 kBitwiseXor_Precedence = 10, 65 kBitwiseOr_Precedence = 11, 66 kLogicalAnd_Precedence = 12, 67 kLogicalXor_Precedence = 13, 68 kLogicalOr_Precedence = 14, 69 kTernary_Precedence = 15, 70 kAssignment_Precedence = 16, 71 kSequence_Precedence = 17, 72 kTopLevel_Precedence = 18 73 }; 74 GLSLCodeGenerator(const Context * context,const Program * program,ErrorReporter * errors,SkWStream * out)75 GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, 76 SkWStream* out) 77 : INHERITED(program, errors, out) 78 , fContext(*context) {} 79 80 virtual bool generateCode() override; 81 82 private: 83 void write(const char* s); 84 85 void writeLine(); 86 87 void writeLine(const char* s); 88 89 void write(const SkString& s); 90 91 void writeLine(const SkString& s); 92 93 void writeType(const Type& type); 94 95 void writeExtension(const Extension& ext); 96 97 void writeInterfaceBlock(const InterfaceBlock& intf); 98 99 void writeFunctionStart(const FunctionDeclaration& f); 100 101 void writeFunctionDeclaration(const FunctionDeclaration& f); 102 103 void writeFunction(const FunctionDefinition& f); 104 105 void writeLayout(const Layout& layout); 106 107 void writeModifiers(const Modifiers& modifiers, bool globalContext); 108 109 void writeGlobalVars(const VarDeclaration& vs); 110 111 void writeVarDeclarations(const VarDeclarations& decl, bool global); 112 113 void writeFragCoord(); 114 115 void writeVariableReference(const VariableReference& ref); 116 117 void writeExpression(const Expression& expr, Precedence parentPrecedence); 118 119 void writeIntrinsicCall(const FunctionCall& c); 120 121 void writeMinAbsHack(Expression& absExpr, Expression& otherExpr); 122 123 void writeFunctionCall(const FunctionCall& c); 124 125 void writeConstructor(const Constructor& c); 126 127 void writeFieldAccess(const FieldAccess& f); 128 129 void writeSwizzle(const Swizzle& swizzle); 130 131 void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence); 132 133 void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence); 134 135 void writeIndexExpression(const IndexExpression& expr); 136 137 void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence); 138 139 void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence); 140 141 void writeBoolLiteral(const BoolLiteral& b); 142 143 void writeIntLiteral(const IntLiteral& i); 144 145 void writeFloatLiteral(const FloatLiteral& f); 146 147 void writeStatement(const Statement& s); 148 149 void writeBlock(const Block& b); 150 151 void writeIfStatement(const IfStatement& stmt); 152 153 void writeForStatement(const ForStatement& f); 154 155 void writeWhileStatement(const WhileStatement& w); 156 157 void writeDoStatement(const DoStatement& d); 158 159 void writeSwitchStatement(const SwitchStatement& s); 160 161 void writeReturnStatement(const ReturnStatement& r); 162 163 const Context& fContext; 164 SkDynamicMemoryWStream fHeader; 165 SkString fFunctionHeader; 166 Program::Kind fProgramKind; 167 int fVarCount = 0; 168 int fIndentation = 0; 169 bool fAtLineStart = false; 170 // Keeps track of which struct types we have written. Given that we are unlikely to ever write 171 // more than one or two structs per shader, a simple linear search will be faster than anything 172 // fancier. 173 std::vector<const Type*> fWrittenStructs; 174 // true if we have run into usages of dFdx / dFdy 175 bool fFoundDerivatives = false; 176 bool fFoundImageDecl = false; 177 bool fSetupFragPositionGlobal = false; 178 bool fSetupFragPositionLocal = false; 179 180 typedef CodeGenerator INHERITED; 181 }; 182 183 } 184 185 #endif 186