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