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 #include "SkSLCompiler.h" | |
| 9 | |
| 10 #include <fstream> | |
| 11 #include <streambuf> | |
| 12 | |
| 13 #include "SkSLIRGenerator.h" | |
| 14 #include "SkSLParser.h" | |
| 15 #include "SkSLSPIRVCodeGenerator.h" | |
| 16 #include "ir/SkSLExpression.h" | |
| 17 #include "ir/SkSLIntLiteral.h" | |
| 18 #include "ir/SkSLSymbolTable.h" | |
| 19 #include "ir/SkSLVarDeclaration.h" | |
| 20 #include "SkMutex.h" | |
| 21 | |
| 22 #define STRINGIFY(x) #x | |
| 23 | |
| 24 // include the built-in shader symbols as static strings | |
| 25 | |
| 26 static std::string SKSL_INCLUDE = | |
| 27 #include "sksl.include" | |
| 28 ; | |
| 29 | |
| 30 static std::string SKSL_VERT_INCLUDE = | |
| 31 #include "sksl_vert.include" | |
| 32 ; | |
| 33 | |
| 34 static std::string SKSL_FRAG_INCLUDE = | |
| 35 #include "sksl_frag.include" | |
| 36 ; | |
| 37 | |
| 38 namespace SkSL { | |
| 39 | |
| 40 Compiler::Compiler() | |
| 41 : fErrorCount(0) { | |
| 42 #ifdef SKIA | |
| 43 SK_DECLARE_STATIC_MUTEX(mutex); | |
| 44 mutex.acquire(); | |
| 45 static bool initialized = false; | |
| 46 static auto types = std::shared_ptr<SymbolTable>(new SymbolTable(nullptr)); | |
|
dogben
2016/06/28 02:14:54
I can't figure out what this is doing. I don't see
ethannicholas
2016/06/30 15:19:28
Well, it *used* to have a constructor that worked
| |
| 47 // the parser uses the 'types' symboltable to distinguish between types and other identifiers, | |
| 48 // so we need to store other symbols (such as global functions) in another s ymboltable | |
| 49 static auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, nu llptr)); | |
| 50 fIRGenerator = new IRGenerator(symbols, *this); | |
| 51 fTypes = types; | |
| 52 if (!initialized) { | |
| 53 #else | |
| 54 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this)); | |
| 55 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this)); | |
| 56 fIRGenerator = new IRGenerator(symbols, *this); | |
| 57 fTypes = types; | |
| 58 #endif | |
| 59 #define ADD_TYPE(t) types->add(k ## t ## _Type->fName, k ## t ## _Type) | |
| 60 ADD_TYPE(Void); | |
| 61 ADD_TYPE(Float); | |
| 62 ADD_TYPE(Vec2); | |
| 63 ADD_TYPE(Vec3); | |
| 64 ADD_TYPE(Vec4); | |
| 65 ADD_TYPE(Double); | |
| 66 ADD_TYPE(DVec2); | |
| 67 ADD_TYPE(DVec3); | |
| 68 ADD_TYPE(DVec4); | |
| 69 ADD_TYPE(Int); | |
| 70 ADD_TYPE(IVec2); | |
| 71 ADD_TYPE(IVec3); | |
| 72 ADD_TYPE(IVec4); | |
| 73 ADD_TYPE(UInt); | |
| 74 ADD_TYPE(UVec2); | |
| 75 ADD_TYPE(UVec3); | |
| 76 ADD_TYPE(UVec4); | |
| 77 ADD_TYPE(Bool); | |
| 78 ADD_TYPE(BVec2); | |
| 79 ADD_TYPE(BVec3); | |
| 80 ADD_TYPE(BVec4); | |
| 81 ADD_TYPE(Mat2x2); | |
| 82 ADD_TYPE(Mat2x3); | |
| 83 ADD_TYPE(Mat2x4); | |
| 84 ADD_TYPE(Mat3x2); | |
| 85 ADD_TYPE(Mat3x3); | |
| 86 ADD_TYPE(Mat3x4); | |
| 87 ADD_TYPE(Mat4x2); | |
| 88 ADD_TYPE(Mat4x3); | |
| 89 ADD_TYPE(Mat4x4); | |
| 90 ADD_TYPE(GenType); | |
| 91 ADD_TYPE(GenDType); | |
| 92 ADD_TYPE(GenIType); | |
| 93 ADD_TYPE(GenUType); | |
| 94 ADD_TYPE(GenBType); | |
| 95 ADD_TYPE(Mat); | |
| 96 ADD_TYPE(Vec); | |
| 97 ADD_TYPE(GVec); | |
| 98 ADD_TYPE(GVec2); | |
| 99 ADD_TYPE(GVec3); | |
| 100 ADD_TYPE(GVec4); | |
| 101 ADD_TYPE(DVec); | |
| 102 ADD_TYPE(IVec); | |
| 103 ADD_TYPE(UVec); | |
| 104 ADD_TYPE(BVec); | |
| 105 | |
| 106 ADD_TYPE(Sampler1D); | |
| 107 ADD_TYPE(Sampler2D); | |
| 108 ADD_TYPE(Sampler3D); | |
| 109 ADD_TYPE(SamplerCube); | |
| 110 ADD_TYPE(Sampler2DRect); | |
| 111 ADD_TYPE(Sampler1DArray); | |
| 112 ADD_TYPE(Sampler2DArray); | |
| 113 ADD_TYPE(SamplerCubeArray); | |
| 114 ADD_TYPE(SamplerBuffer); | |
| 115 ADD_TYPE(Sampler2DMS); | |
| 116 ADD_TYPE(Sampler2DMSArray); | |
| 117 | |
| 118 ADD_TYPE(GSampler1D); | |
| 119 ADD_TYPE(GSampler2D); | |
| 120 ADD_TYPE(GSampler3D); | |
| 121 ADD_TYPE(GSamplerCube); | |
| 122 ADD_TYPE(GSampler2DRect); | |
| 123 ADD_TYPE(GSampler1DArray); | |
| 124 ADD_TYPE(GSampler2DArray); | |
| 125 ADD_TYPE(GSamplerCubeArray); | |
| 126 ADD_TYPE(GSamplerBuffer); | |
| 127 ADD_TYPE(GSampler2DMS); | |
| 128 ADD_TYPE(GSampler2DMSArray); | |
| 129 | |
| 130 ADD_TYPE(Sampler1DShadow); | |
| 131 ADD_TYPE(Sampler2DShadow); | |
| 132 ADD_TYPE(SamplerCubeShadow); | |
| 133 ADD_TYPE(Sampler2DRectShadow); | |
| 134 ADD_TYPE(Sampler1DArrayShadow); | |
| 135 ADD_TYPE(Sampler2DArrayShadow); | |
| 136 ADD_TYPE(SamplerCubeArrayShadow); | |
| 137 ADD_TYPE(GSampler2DArrayShadow); | |
| 138 ADD_TYPE(GSamplerCubeArrayShadow); | |
| 139 | |
| 140 std::vector<std::unique_ptr<ProgramElement>> ignored; | |
| 141 ASSERT_RESULT(this->internalConvertProgram(SKSL_INCLUDE, &ignored)); | |
| 142 #ifdef SKIA | |
| 143 initialized = true; | |
| 144 } | |
| 145 mutex.release(); | |
| 146 #endif | |
| 147 } | |
| 148 | |
| 149 Compiler::~Compiler() { | |
| 150 delete fIRGenerator; | |
| 151 } | |
| 152 | |
| 153 bool Compiler::internalConvertProgram(std::string text, | |
| 154 std::vector<std::unique_ptr<ProgramElement >>* result) { | |
| 155 Parser parser(text, *fTypes, *this); | |
| 156 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); | |
| 157 for (size_t i = 0; i < parsed.size(); i++) { | |
| 158 ASTDeclaration& decl = *parsed[i]; | |
| 159 switch (decl.fKind) { | |
| 160 case ASTDeclaration::kVar_Kind: { | |
| 161 std::unique_ptr<VarDeclaration> s = fIRGenerator->convertVarDecl aration( | |
| 162 (ASTVar Declaration&) decl, | |
| 163 Variabl e::kGlobal_Storage); | |
| 164 if (s) { | |
| 165 result->push_back(std::move(s)); | |
| 166 } | |
| 167 break; | |
| 168 } | |
| 169 case ASTDeclaration::kFunction_Kind: { | |
| 170 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFun ction( | |
| 171 ( ASTFunction&) decl); | |
| 172 if (f) { | |
| 173 result->push_back(std::move(f)); | |
| 174 } | |
| 175 break; | |
| 176 } | |
| 177 case ASTDeclaration::kInterfaceBlock_Kind: { | |
| 178 std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfa ceBlock( | |
| 179 (ASTInt erfaceBlock&) decl); | |
| 180 if (i) { | |
| 181 result->push_back(std::move(i)); | |
| 182 } | |
| 183 break; | |
| 184 } | |
| 185 case ASTDeclaration::kExtension_Kind: { | |
| 186 std::unique_ptr<Extension> e = fIRGenerator->convertExtension((A STExtension&) decl); | |
| 187 if (e) { | |
| 188 result->push_back(std::move(e)); | |
| 189 } | |
| 190 break; | |
| 191 } | |
| 192 default: | |
| 193 ABORT("unsupported declaration: %s\n", decl.description().c_str( )); | |
| 194 } | |
| 195 } | |
| 196 return true; | |
| 197 } | |
| 198 | |
| 199 std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, std::strin g text) { | |
| 200 fErrorText = ""; | |
| 201 fErrorCount = 0; | |
| 202 fIRGenerator->pushSymbolTable(); | |
| 203 std::vector<std::unique_ptr<ProgramElement>> result; | |
| 204 switch (kind) { | |
| 205 case Program::kVertex_Kind: | |
| 206 this->internalConvertProgram(SKSL_VERT_INCLUDE, &result); | |
| 207 break; | |
| 208 case Program::kFragment_Kind: | |
| 209 this->internalConvertProgram(SKSL_FRAG_INCLUDE, &result); | |
| 210 break; | |
| 211 } | |
| 212 this->internalConvertProgram(text, &result); | |
| 213 fIRGenerator->popSymbolTable(); | |
| 214 this->writeErrorCount(); | |
| 215 return std::unique_ptr<Program>(new Program(kind, std::move(result)));; | |
| 216 } | |
| 217 | |
| 218 void Compiler::error(Position position, std::string msg) { | |
| 219 fErrorCount++; | |
| 220 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n" ; | |
| 221 } | |
| 222 | |
| 223 std::string Compiler::errorText() { | |
| 224 std::string result = fErrorText; | |
| 225 return result; | |
| 226 } | |
| 227 | |
| 228 void Compiler::writeErrorCount() { | |
| 229 if (fErrorCount) { | |
| 230 fErrorText += to_string(fErrorCount) + " error"; | |
| 231 if (fErrorCount > 1) { | |
| 232 fErrorText += "s"; | |
| 233 } | |
| 234 fErrorText += "\n"; | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::ostream& out) { | |
| 239 auto program = this->convertProgram(kind, text); | |
| 240 if (fErrorCount == 0) { | |
| 241 SkSL::SPIRVCodeGenerator cg; | |
| 242 cg.generateCode(*program.get(), out); | |
| 243 ASSERT(!out.rdstate()); | |
| 244 } | |
| 245 return fErrorCount == 0; | |
| 246 } | |
| 247 | |
| 248 bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::string* out) { | |
| 249 std::stringstream buffer; | |
| 250 bool result = this->toSPIRV(kind, text, buffer); | |
| 251 if (result) { | |
| 252 *out = buffer.str(); | |
| 253 } | |
| 254 return fErrorCount == 0; | |
| 255 } | |
| 256 | |
| 257 } // namespace | |
| OLD | NEW |