| 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 #include "SkSLCompiler.h" | 8 #include "SkSLCompiler.h" |
| 9 | 9 |
| 10 #include <fstream> | 10 #include <fstream> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 static std::string SKSL_FRAG_INCLUDE = | 34 static std::string SKSL_FRAG_INCLUDE = |
| 35 #include "sksl_frag.include" | 35 #include "sksl_frag.include" |
| 36 ; | 36 ; |
| 37 | 37 |
| 38 namespace SkSL { | 38 namespace SkSL { |
| 39 | 39 |
| 40 Compiler::Compiler() | 40 Compiler::Compiler() |
| 41 : fErrorCount(0) { | 41 : fErrorCount(0) { |
| 42 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this)); | 42 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this)); |
| 43 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this)); | 43 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this)); |
| 44 fIRGenerator = new IRGenerator(symbols, *this); | 44 fIRGenerator = new IRGenerator(&fContext, symbols, *this); |
| 45 fTypes = types; | 45 fTypes = types; |
| 46 #define ADD_TYPE(t) types->add(k ## t ## _Type->fName, k ## t ## _Type) | 46 #define ADD_TYPE(t) types->addWithoutOwnership(fContext.f ## t ## _Type->fNa
me, \ |
| 47 fContext.f ## t ## _Type.get(
)) |
| 47 ADD_TYPE(Void); | 48 ADD_TYPE(Void); |
| 48 ADD_TYPE(Float); | 49 ADD_TYPE(Float); |
| 49 ADD_TYPE(Vec2); | 50 ADD_TYPE(Vec2); |
| 50 ADD_TYPE(Vec3); | 51 ADD_TYPE(Vec3); |
| 51 ADD_TYPE(Vec4); | 52 ADD_TYPE(Vec4); |
| 52 ADD_TYPE(Double); | 53 ADD_TYPE(Double); |
| 53 ADD_TYPE(DVec2); | 54 ADD_TYPE(DVec2); |
| 54 ADD_TYPE(DVec3); | 55 ADD_TYPE(DVec3); |
| 55 ADD_TYPE(DVec4); | 56 ADD_TYPE(DVec4); |
| 56 ADD_TYPE(Int); | 57 ADD_TYPE(Int); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 default: | 179 default: |
| 179 ABORT("unsupported declaration: %s\n", decl.description().c_str(
)); | 180 ABORT("unsupported declaration: %s\n", decl.description().c_str(
)); |
| 180 } | 181 } |
| 181 } | 182 } |
| 182 } | 183 } |
| 183 | 184 |
| 184 std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, std::strin
g text) { | 185 std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, std::strin
g text) { |
| 185 fErrorText = ""; | 186 fErrorText = ""; |
| 186 fErrorCount = 0; | 187 fErrorCount = 0; |
| 187 fIRGenerator->pushSymbolTable(); | 188 fIRGenerator->pushSymbolTable(); |
| 188 std::vector<std::unique_ptr<ProgramElement>> result; | 189 std::vector<std::unique_ptr<ProgramElement>> elements; |
| 189 switch (kind) { | 190 switch (kind) { |
| 190 case Program::kVertex_Kind: | 191 case Program::kVertex_Kind: |
| 191 this->internalConvertProgram(SKSL_VERT_INCLUDE, &result); | 192 this->internalConvertProgram(SKSL_VERT_INCLUDE, &elements); |
| 192 break; | 193 break; |
| 193 case Program::kFragment_Kind: | 194 case Program::kFragment_Kind: |
| 194 this->internalConvertProgram(SKSL_FRAG_INCLUDE, &result); | 195 this->internalConvertProgram(SKSL_FRAG_INCLUDE, &elements); |
| 195 break; | 196 break; |
| 196 } | 197 } |
| 197 this->internalConvertProgram(text, &result); | 198 this->internalConvertProgram(text, &elements); |
| 199 auto result = std::unique_ptr<Program>(new Program(kind, std::move(elements)
, |
| 200 fIRGenerator->fSymbolTabl
e));; |
| 198 fIRGenerator->popSymbolTable(); | 201 fIRGenerator->popSymbolTable(); |
| 199 this->writeErrorCount(); | 202 this->writeErrorCount(); |
| 200 return std::unique_ptr<Program>(new Program(kind, std::move(result)));; | 203 return result; |
| 201 } | 204 } |
| 202 | 205 |
| 203 void Compiler::error(Position position, std::string msg) { | 206 void Compiler::error(Position position, std::string msg) { |
| 204 fErrorCount++; | 207 fErrorCount++; |
| 205 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"
; | 208 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"
; |
| 206 } | 209 } |
| 207 | 210 |
| 208 std::string Compiler::errorText() { | 211 std::string Compiler::errorText() { |
| 209 std::string result = fErrorText; | 212 std::string result = fErrorText; |
| 210 return result; | 213 return result; |
| 211 } | 214 } |
| 212 | 215 |
| 213 void Compiler::writeErrorCount() { | 216 void Compiler::writeErrorCount() { |
| 214 if (fErrorCount) { | 217 if (fErrorCount) { |
| 215 fErrorText += to_string(fErrorCount) + " error"; | 218 fErrorText += to_string(fErrorCount) + " error"; |
| 216 if (fErrorCount > 1) { | 219 if (fErrorCount > 1) { |
| 217 fErrorText += "s"; | 220 fErrorText += "s"; |
| 218 } | 221 } |
| 219 fErrorText += "\n"; | 222 fErrorText += "\n"; |
| 220 } | 223 } |
| 221 } | 224 } |
| 222 | 225 |
| 223 #include <fstream> | 226 #include <fstream> |
| 224 bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::ostream& out)
{ | 227 bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::ostream& out)
{ |
| 225 auto program = this->convertProgram(kind, text); | 228 auto program = this->convertProgram(kind, text); |
| 226 if (fErrorCount == 0) { | 229 if (fErrorCount == 0) { |
| 227 SkSL::SPIRVCodeGenerator cg; | 230 SkSL::SPIRVCodeGenerator cg(&fContext); |
| 228 cg.generateCode(*program.get(), out); | 231 cg.generateCode(*program.get(), out); |
| 229 ASSERT(!out.rdstate()); | 232 ASSERT(!out.rdstate()); |
| 230 } | 233 } |
| 231 return fErrorCount == 0; | 234 return fErrorCount == 0; |
| 232 } | 235 } |
| 233 | 236 |
| 234 bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::string* out) { | 237 bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::string* out) { |
| 235 std::stringstream buffer; | 238 std::stringstream buffer; |
| 236 bool result = this->toSPIRV(kind, text, buffer); | 239 bool result = this->toSPIRV(kind, text, buffer); |
| 237 if (result) { | 240 if (result) { |
| 238 *out = buffer.str(); | 241 *out = buffer.str(); |
| 239 } | 242 } |
| 240 return fErrorCount == 0; | 243 return fErrorCount == 0; |
| 241 } | 244 } |
| 242 | 245 |
| 243 } // namespace | 246 } // namespace |
| OLD | NEW |