Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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(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(std::string s); | |
| 97 | |
| 98 void writeLine(std::string s); | |
| 99 | |
| 100 void write(std::string& s); | |
|
dogben
2016/07/31 23:20:20
nit: seems like "void write(const std::string& s);
ethannicholas
2016/08/02 16:13:17
Fixed.
| |
| 101 | |
| 102 void writeLine(std::string& s); | |
| 103 | |
| 104 void writeStruct(const Type& type); | |
| 105 | |
| 106 void writeProgramElement(ProgramElement& pe); | |
|
dogben
2016/07/31 23:20:20
This method doesn't seem to be implemented or used
ethannicholas
2016/08/02 16:13:17
Fixed.
| |
| 107 | |
| 108 void writeExtension(Extension& ext); | |
|
dogben
2016/07/31 23:20:20
nit: seems odd that some methods take const ref an
ethannicholas
2016/08/02 16:13:18
Fixed.
| |
| 109 | |
| 110 void writeInterfaceBlock(InterfaceBlock& intf); | |
| 111 | |
| 112 void writeFunctionStart(const FunctionDeclaration& f); | |
| 113 | |
| 114 void writeFunctionDeclaration(const FunctionDeclaration& f); | |
| 115 | |
| 116 void writeFunction(const FunctionDefinition& f); | |
| 117 | |
| 118 void writeLayout(const Layout& layout); | |
| 119 | |
| 120 void writeModifiers(const Modifiers& modifiers); | |
| 121 | |
| 122 void writeGlobalVars(VarDeclaration& vs); | |
| 123 | |
| 124 void writeVarDeclaration(VarDeclaration& decl); | |
| 125 | |
| 126 void writeVariableReference(VariableReference& ref); | |
| 127 | |
| 128 void writeExpression(Expression& expr, Precedence parentPrecedence); | |
| 129 | |
| 130 void writeIntrinsicCall(FunctionCall& c); | |
| 131 | |
| 132 void writeFunctionCall(FunctionCall& c); | |
| 133 | |
| 134 void writeConstructor(Constructor& c); | |
| 135 | |
| 136 void writeFieldAccess(FieldAccess& f); | |
| 137 | |
| 138 void writeSwizzle(Swizzle& swizzle); | |
| 139 | |
| 140 void writeBinaryExpression(BinaryExpression& b, Precedence parentPrecedence) ; | |
| 141 | |
| 142 void writeTernaryExpression(TernaryExpression& t, Precedence parentPrecedenc e); | |
| 143 | |
| 144 void writeIndexExpression(IndexExpression& expr); | |
| 145 | |
| 146 void writePrefixExpression(PrefixExpression& p); | |
| 147 | |
| 148 void writePostfixExpression(PostfixExpression& p); | |
| 149 | |
| 150 void writeBoolLiteral(BoolLiteral& b); | |
| 151 | |
| 152 void writeIntLiteral(IntLiteral& i); | |
| 153 | |
| 154 void writeFloatLiteral(FloatLiteral& f); | |
| 155 | |
| 156 void writeStatement(Statement& s); | |
| 157 | |
| 158 void writeBlock(Block& b); | |
| 159 | |
| 160 void writeIfStatement(IfStatement& stmt); | |
| 161 | |
| 162 void writeForStatement(ForStatement& f); | |
| 163 | |
| 164 void writeWhileStatement(WhileStatement& w); | |
| 165 | |
| 166 void writeDoStatement(DoStatement& d); | |
| 167 | |
| 168 void writeReturnStatement(ReturnStatement& r); | |
| 169 | |
| 170 const Context& fContext; | |
| 171 const GLCaps fCaps; | |
| 172 std::ostream* fOut; | |
| 173 int fIndentation; | |
| 174 bool fAtLineStart; | |
| 175 }; | |
| 176 | |
| 177 } | |
| 178 | |
| 179 #endif | |
| OLD | NEW |