OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SKSL_SPIRVCODEGENERATOR | 8 #ifndef SKSL_SPIRVCODEGENERATOR |
9 #define SKSL_SPIRVCODEGENERATOR | 9 #define SKSL_SPIRVCODEGENERATOR |
10 | 10 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 SPIRVCodeGenerator(const Context* context) | 64 SPIRVCodeGenerator(const Context* context) |
65 : fContext(*context) | 65 : fContext(*context) |
66 , fCapabilities(1 << SpvCapabilityShader) | 66 , fCapabilities(1 << SpvCapabilityShader) |
67 , fIdCount(1) | 67 , fIdCount(1) |
68 , fBoolTrue(0) | 68 , fBoolTrue(0) |
69 , fBoolFalse(0) | 69 , fBoolFalse(0) |
70 , fCurrentBlock(0) { | 70 , fCurrentBlock(0) { |
71 this->setupIntrinsics(); | 71 this->setupIntrinsics(); |
72 } | 72 } |
73 | 73 |
74 void generateCode(Program& program, std::ostream& out) override; | 74 void generateCode(const Program& program, std::ostream& out) override; |
75 | 75 |
76 private: | 76 private: |
77 enum IntrinsicKind { | 77 enum IntrinsicKind { |
78 kGLSL_STD_450_IntrinsicKind, | 78 kGLSL_STD_450_IntrinsicKind, |
79 kSPIRV_IntrinsicKind, | 79 kSPIRV_IntrinsicKind, |
80 kSpecial_IntrinsicKind | 80 kSpecial_IntrinsicKind |
81 }; | 81 }; |
82 | 82 |
83 enum SpecialIntrinsic { | 83 enum SpecialIntrinsic { |
84 kAtan_SpecialIntrinsic, | 84 kAtan_SpecialIntrinsic, |
85 kTexture_SpecialIntrinsic, | 85 kTexture_SpecialIntrinsic, |
86 kTexture2D_SpecialIntrinsic, | 86 kTexture2D_SpecialIntrinsic, |
87 kTextureProj_SpecialIntrinsic | 87 kTextureProj_SpecialIntrinsic |
88 }; | 88 }; |
89 | 89 |
90 void setupIntrinsics(); | 90 void setupIntrinsics(); |
91 | 91 |
92 SpvId nextId(); | 92 SpvId nextId(); |
93 | 93 |
94 SpvId getType(const Type& type); | 94 SpvId getType(const Type& type); |
95 | 95 |
96 SpvId getFunctionType(const FunctionDeclaration& function); | 96 SpvId getFunctionType(const FunctionDeclaration& function); |
97 | 97 |
98 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass); | 98 SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass); |
99 | 99 |
100 std::vector<SpvId> getAccessChain(Expression& expr, std::ostream& out); | 100 std::vector<SpvId> getAccessChain(const Expression& expr, std::ostream& out)
; |
101 | 101 |
102 void writeLayout(const Layout& layout, SpvId target); | 102 void writeLayout(const Layout& layout, SpvId target); |
103 | 103 |
104 void writeLayout(const Layout& layout, SpvId target, int member); | 104 void writeLayout(const Layout& layout, SpvId target, int member); |
105 | 105 |
106 void writeStruct(const Type& type, SpvId resultId); | 106 void writeStruct(const Type& type, SpvId resultId); |
107 | 107 |
108 void writeProgramElement(ProgramElement& pe, std::ostream& out); | 108 void writeProgramElement(const ProgramElement& pe, std::ostream& out); |
109 | 109 |
110 SpvId writeInterfaceBlock(InterfaceBlock& intf); | 110 SpvId writeInterfaceBlock(const InterfaceBlock& intf); |
111 | 111 |
112 SpvId writeFunctionStart(const FunctionDeclaration& f, std::ostream& out); | 112 SpvId writeFunctionStart(const FunctionDeclaration& f, std::ostream& out); |
113 | 113 |
114 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, std::ostream& o
ut); | 114 SpvId writeFunctionDeclaration(const FunctionDeclaration& f, std::ostream& o
ut); |
115 | 115 |
116 SpvId writeFunction(const FunctionDefinition& f, std::ostream& out); | 116 SpvId writeFunction(const FunctionDefinition& f, std::ostream& out); |
117 | 117 |
118 void writeGlobalVars(VarDeclaration& v, std::ostream& out); | 118 void writeGlobalVars(const VarDeclaration& v, std::ostream& out); |
119 | 119 |
120 void writeVarDeclaration(VarDeclaration& decl, std::ostream& out); | 120 void writeVarDeclaration(const VarDeclaration& decl, std::ostream& out); |
121 | 121 |
122 SpvId writeVariableReference(VariableReference& ref, std::ostream& out); | 122 SpvId writeVariableReference(const VariableReference& ref, std::ostream& out
); |
123 | 123 |
124 std::unique_ptr<LValue> getLValue(Expression& value, std::ostream& out); | 124 std::unique_ptr<LValue> getLValue(const Expression& value, std::ostream& out
); |
125 | 125 |
126 SpvId writeExpression(Expression& expr, std::ostream& out); | 126 SpvId writeExpression(const Expression& expr, std::ostream& out); |
127 | 127 |
128 SpvId writeIntrinsicCall(FunctionCall& c, std::ostream& out); | 128 SpvId writeIntrinsicCall(const FunctionCall& c, std::ostream& out); |
129 | 129 |
130 SpvId writeFunctionCall(FunctionCall& c, std::ostream& out); | 130 SpvId writeFunctionCall(const FunctionCall& c, std::ostream& out); |
131 | 131 |
132 SpvId writeSpecialIntrinsic(FunctionCall& c, SpecialIntrinsic kind, std::ost
ream& out); | 132 SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, st
d::ostream& out); |
133 | 133 |
134 SpvId writeConstantVector(Constructor& c); | 134 SpvId writeConstantVector(const Constructor& c); |
135 | 135 |
136 SpvId writeFloatConstructor(Constructor& c, std::ostream& out); | 136 SpvId writeFloatConstructor(const Constructor& c, std::ostream& out); |
137 | 137 |
138 SpvId writeIntConstructor(Constructor& c, std::ostream& out); | 138 SpvId writeIntConstructor(const Constructor& c, std::ostream& out); |
139 | 139 |
140 SpvId writeMatrixConstructor(Constructor& c, std::ostream& out); | 140 SpvId writeMatrixConstructor(const Constructor& c, std::ostream& out); |
141 | 141 |
142 SpvId writeVectorConstructor(Constructor& c, std::ostream& out); | 142 SpvId writeVectorConstructor(const Constructor& c, std::ostream& out); |
143 | 143 |
144 SpvId writeConstructor(Constructor& c, std::ostream& out); | 144 SpvId writeConstructor(const Constructor& c, std::ostream& out); |
145 | 145 |
146 SpvId writeFieldAccess(FieldAccess& f, std::ostream& out); | 146 SpvId writeFieldAccess(const FieldAccess& f, std::ostream& out); |
147 | 147 |
148 SpvId writeSwizzle(Swizzle& swizzle, std::ostream& out); | 148 SpvId writeSwizzle(const Swizzle& swizzle, std::ostream& out); |
149 | 149 |
150 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType,
SpvId lhs, | 150 SpvId writeBinaryOperation(const Type& resultType, const Type& operandType,
SpvId lhs, |
151 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ i
fUInt, | 151 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ i
fUInt, |
152 SpvOp_ ifBool, std::ostream& out); | 152 SpvOp_ ifBool, std::ostream& out); |
153 | 153 |
154 SpvId writeBinaryOperation(BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ if
Int, SpvOp_ ifUInt, | 154 SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, Spv
Op_ ifInt, |
155 std::ostream& out); | 155 SpvOp_ ifUInt, std::ostream& out); |
156 | 156 |
157 SpvId writeBinaryExpression(BinaryExpression& b, std::ostream& out); | 157 SpvId writeBinaryExpression(const BinaryExpression& b, std::ostream& out); |
158 | 158 |
159 SpvId writeTernaryExpression(TernaryExpression& t, std::ostream& out); | 159 SpvId writeTernaryExpression(const TernaryExpression& t, std::ostream& out); |
160 | 160 |
161 SpvId writeIndexExpression(IndexExpression& expr, std::ostream& out); | 161 SpvId writeIndexExpression(const IndexExpression& expr, std::ostream& out); |
162 | 162 |
163 SpvId writeLogicalAnd(BinaryExpression& b, std::ostream& out); | 163 SpvId writeLogicalAnd(const BinaryExpression& b, std::ostream& out); |
164 | 164 |
165 SpvId writeLogicalOr(BinaryExpression& o, std::ostream& out); | 165 SpvId writeLogicalOr(const BinaryExpression& o, std::ostream& out); |
166 | 166 |
167 SpvId writePrefixExpression(PrefixExpression& p, std::ostream& out); | 167 SpvId writePrefixExpression(const PrefixExpression& p, std::ostream& out); |
168 | 168 |
169 SpvId writePostfixExpression(PostfixExpression& p, std::ostream& out); | 169 SpvId writePostfixExpression(const PostfixExpression& p, std::ostream& out); |
170 | 170 |
171 SpvId writeBoolLiteral(BoolLiteral& b); | 171 SpvId writeBoolLiteral(const BoolLiteral& b); |
172 | 172 |
173 SpvId writeIntLiteral(IntLiteral& i); | 173 SpvId writeIntLiteral(const IntLiteral& i); |
174 | 174 |
175 SpvId writeFloatLiteral(FloatLiteral& f); | 175 SpvId writeFloatLiteral(const FloatLiteral& f); |
176 | 176 |
177 void writeStatement(Statement& s, std::ostream& out); | 177 void writeStatement(const Statement& s, std::ostream& out); |
178 | 178 |
179 void writeBlock(Block& b, std::ostream& out); | 179 void writeBlock(const Block& b, std::ostream& out); |
180 | 180 |
181 void writeIfStatement(IfStatement& stmt, std::ostream& out); | 181 void writeIfStatement(const IfStatement& stmt, std::ostream& out); |
182 | 182 |
183 void writeForStatement(ForStatement& f, std::ostream& out); | 183 void writeForStatement(const ForStatement& f, std::ostream& out); |
184 | 184 |
185 void writeReturnStatement(ReturnStatement& r, std::ostream& out); | 185 void writeReturnStatement(const ReturnStatement& r, std::ostream& out); |
186 | 186 |
187 void writeCapabilities(std::ostream& out); | 187 void writeCapabilities(std::ostream& out); |
188 | 188 |
189 void writeInstructions(Program& program, std::ostream& out); | 189 void writeInstructions(const Program& program, std::ostream& out); |
190 | 190 |
191 void writeOpCode(SpvOp_ opCode, int length, std::ostream& out); | 191 void writeOpCode(SpvOp_ opCode, int length, std::ostream& out); |
192 | 192 |
193 void writeWord(int32_t word, std::ostream& out); | 193 void writeWord(int32_t word, std::ostream& out); |
194 | 194 |
195 void writeString(const char* string, std::ostream& out); | 195 void writeString(const char* string, std::ostream& out); |
196 | 196 |
197 void writeLabel(SpvId id, std::ostream& out); | 197 void writeLabel(SpvId id, std::ostream& out); |
198 | 198 |
199 void writeInstruction(SpvOp_ opCode, std::ostream& out); | 199 void writeInstruction(SpvOp_ opCode, std::ostream& out); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 std::stack<SpvId> fBreakTarget; | 258 std::stack<SpvId> fBreakTarget; |
259 std::stack<SpvId> fContinueTarget; | 259 std::stack<SpvId> fContinueTarget; |
260 | 260 |
261 friend class PointerLValue; | 261 friend class PointerLValue; |
262 friend class SwizzleLValue; | 262 friend class SwizzleLValue; |
263 }; | 263 }; |
264 | 264 |
265 } | 265 } |
266 | 266 |
267 #endif | 267 #endif |
OLD | NEW |