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(*this)); | |
|
dogben
2016/06/23 17:43:11
This seems unhygienic. Could we either add a dummy
ethannicholas
2016/06/24 21:23:08
These didn't used to be shared, and I failed to fi
| |
| 47 static auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *t his)); | |
|
dogben
2016/06/23 17:43:12
Why are types and symbols in different SymbolTable
ethannicholas
2016/06/24 21:23:08
The Parser uses the types symboltable to determine
| |
| 48 fIRGenerator = new IRGenerator(symbols, *this); | |
| 49 fTypes = types; | |
| 50 if (!initialized) { | |
| 51 #else | |
| 52 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this)); | |
| 53 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this)); | |
| 54 fIRGenerator = new IRGenerator(symbols, *this); | |
| 55 fTypes = types; | |
| 56 #endif | |
| 57 #define ADD_TYPE(t) types->add(k ## t ## _Type->fName, k ## t ## _Type) | |
| 58 ADD_TYPE(Void); | |
| 59 ADD_TYPE(Float); | |
| 60 ADD_TYPE(Vec2); | |
| 61 ADD_TYPE(Vec3); | |
| 62 ADD_TYPE(Vec4); | |
| 63 ADD_TYPE(Double); | |
| 64 ADD_TYPE(DVec2); | |
| 65 ADD_TYPE(DVec3); | |
| 66 ADD_TYPE(DVec4); | |
| 67 ADD_TYPE(Int); | |
| 68 ADD_TYPE(IVec2); | |
| 69 ADD_TYPE(IVec3); | |
| 70 ADD_TYPE(IVec4); | |
| 71 ADD_TYPE(UInt); | |
| 72 ADD_TYPE(UVec2); | |
| 73 ADD_TYPE(UVec3); | |
| 74 ADD_TYPE(UVec4); | |
| 75 ADD_TYPE(Bool); | |
| 76 ADD_TYPE(BVec2); | |
| 77 ADD_TYPE(BVec3); | |
| 78 ADD_TYPE(BVec4); | |
| 79 ADD_TYPE(Mat2x2); | |
| 80 ADD_TYPE(Mat2x3); | |
| 81 ADD_TYPE(Mat2x4); | |
| 82 ADD_TYPE(Mat3x2); | |
| 83 ADD_TYPE(Mat3x3); | |
| 84 ADD_TYPE(Mat3x4); | |
| 85 ADD_TYPE(Mat4x2); | |
| 86 ADD_TYPE(Mat4x3); | |
| 87 ADD_TYPE(Mat4x4); | |
| 88 ADD_TYPE(GenType); | |
| 89 ADD_TYPE(GenDType); | |
| 90 ADD_TYPE(GenIType); | |
| 91 ADD_TYPE(GenUType); | |
| 92 ADD_TYPE(GenBType); | |
| 93 ADD_TYPE(Mat); | |
| 94 ADD_TYPE(Vec); | |
| 95 ADD_TYPE(GVec); | |
| 96 ADD_TYPE(GVec2); | |
| 97 ADD_TYPE(GVec3); | |
| 98 ADD_TYPE(GVec4); | |
| 99 ADD_TYPE(DVec); | |
| 100 ADD_TYPE(IVec); | |
| 101 ADD_TYPE(UVec); | |
| 102 ADD_TYPE(BVec); | |
| 103 | |
| 104 ADD_TYPE(Sampler1D); | |
| 105 ADD_TYPE(Sampler2D); | |
| 106 ADD_TYPE(Sampler3D); | |
| 107 ADD_TYPE(SamplerCube); | |
| 108 ADD_TYPE(Sampler2DRect); | |
| 109 ADD_TYPE(Sampler1DArray); | |
| 110 ADD_TYPE(Sampler2DArray); | |
| 111 ADD_TYPE(SamplerCubeArray); | |
| 112 ADD_TYPE(SamplerBuffer); | |
| 113 ADD_TYPE(Sampler2DMS); | |
| 114 ADD_TYPE(Sampler2DMSArray); | |
| 115 | |
| 116 ADD_TYPE(GSampler1D); | |
| 117 ADD_TYPE(GSampler2D); | |
| 118 ADD_TYPE(GSampler3D); | |
| 119 ADD_TYPE(GSamplerCube); | |
| 120 ADD_TYPE(GSampler2DRect); | |
| 121 ADD_TYPE(GSampler1DArray); | |
| 122 ADD_TYPE(GSampler2DArray); | |
| 123 ADD_TYPE(GSamplerCubeArray); | |
| 124 ADD_TYPE(GSamplerBuffer); | |
| 125 ADD_TYPE(GSampler2DMS); | |
| 126 ADD_TYPE(GSampler2DMSArray); | |
| 127 | |
| 128 ADD_TYPE(Sampler1DShadow); | |
| 129 ADD_TYPE(Sampler2DShadow); | |
| 130 ADD_TYPE(SamplerCubeShadow); | |
| 131 ADD_TYPE(Sampler2DRectShadow); | |
| 132 ADD_TYPE(Sampler1DArrayShadow); | |
| 133 ADD_TYPE(Sampler2DArrayShadow); | |
| 134 ADD_TYPE(SamplerCubeArrayShadow); | |
| 135 ADD_TYPE(GSampler2DArrayShadow); | |
| 136 ADD_TYPE(GSamplerCubeArrayShadow); | |
| 137 | |
| 138 std::vector<std::unique_ptr<ProgramElement>> ignored; | |
| 139 ASSERT_RESULT(this->internalConvertProgram(SKSL_INCLUDE, &ignored)); | |
| 140 #ifdef SKIA | |
| 141 initialized = true; | |
| 142 } | |
| 143 mutex.release(); | |
| 144 #endif | |
| 145 } | |
| 146 | |
| 147 Compiler::~Compiler() { | |
| 148 delete fIRGenerator; | |
| 149 } | |
| 150 | |
| 151 bool Compiler::internalConvertProgram(std::string text, | |
| 152 std::vector<std::unique_ptr<ProgramElement >>* result) { | |
| 153 Parser parser(text, *fTypes, *this); | |
| 154 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); | |
| 155 for (size_t i = 0; i < parsed.size(); i++) { | |
| 156 ASTDeclaration& decl = *parsed[i]; | |
| 157 switch (decl.fKind) { | |
| 158 case ASTDeclaration::kVar_Kind: { | |
| 159 std::unique_ptr<VarDeclaration> s = fIRGenerator->convertVarDecl aration( | |
| 160 (ASTVar Declaration&) decl, | |
|
dogben
2016/06/23 17:43:11
nit: odd indentation, several places in this metho
| |
| 161 Variabl e::kGlobal_Storage); | |
| 162 if (s) { | |
| 163 result->push_back(std::move(s)); | |
| 164 } | |
| 165 break; | |
| 166 } | |
| 167 case ASTDeclaration::kFunction_Kind: { | |
| 168 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFun ction( | |
| 169 ( ASTFunction&) decl); | |
| 170 if (f) { | |
| 171 result->push_back(std::move(f)); | |
| 172 } | |
| 173 break; | |
| 174 } | |
| 175 case ASTDeclaration::kInterfaceBlock_Kind: { | |
| 176 std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfa ceBlock( | |
| 177 (ASTInt erfaceBlock&) decl); | |
| 178 if (i) { | |
| 179 result->push_back(std::move(i)); | |
| 180 } | |
| 181 break; | |
| 182 } | |
| 183 case ASTDeclaration::kExtension_Kind: { | |
| 184 std::unique_ptr<Extension> e = fIRGenerator->convertExtension((A STExtension&) decl); | |
| 185 if (e) { | |
| 186 result->push_back(std::move(e)); | |
| 187 } | |
| 188 break; | |
| 189 } | |
| 190 default: | |
| 191 ABORT("unsupported declaration: %s\n", decl.description().c_str( )); | |
| 192 } | |
| 193 } | |
| 194 return true; | |
|
dogben
2016/06/23 17:43:11
return fErrorCount == 0? Or make return type void?
| |
| 195 } | |
| 196 | |
| 197 std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, std::strin g text) { | |
| 198 fErrorText = ""; | |
| 199 fErrorCount = 0; | |
| 200 fIRGenerator->pushSymbolTable(); | |
| 201 std::vector<std::unique_ptr<ProgramElement>> result; | |
| 202 switch (kind) { | |
| 203 case Program::kVertex_Kind: | |
| 204 this->internalConvertProgram(SKSL_VERT_INCLUDE, &result); | |
| 205 break; | |
| 206 case Program::kFragment_Kind: | |
| 207 this->internalConvertProgram(SKSL_FRAG_INCLUDE, &result); | |
| 208 break; | |
| 209 } | |
| 210 this->internalConvertProgram(text, &result); | |
| 211 fIRGenerator->popSymbolTable(); | |
| 212 this->writeErrorCount(); | |
| 213 return std::unique_ptr<Program>(new Program(kind, std::move(result)));; | |
| 214 } | |
| 215 | |
| 216 void Compiler::error(Position position, std::string msg) { | |
| 217 fErrorCount++; | |
| 218 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n" ; | |
| 219 } | |
| 220 | |
| 221 std::string Compiler::errorText() { | |
| 222 std::string result = fErrorText; | |
|
dogben
2016/06/23 17:43:11
nit: "return fErrorText;" is equivalent.
| |
| 223 return result; | |
| 224 } | |
| 225 | |
| 226 void Compiler::writeErrorCount() { | |
| 227 if (fErrorCount) { | |
| 228 fErrorText += to_string(fErrorCount) + " error"; | |
| 229 if (fErrorCount > 1) { | |
| 230 fErrorText += "s"; | |
| 231 } | |
| 232 fErrorText += "\n"; | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::ostream& out) { | |
| 237 auto program = this->convertProgram(kind, text); | |
| 238 if (fErrorCount == 0) { | |
| 239 auto cg = std::unique_ptr<SkSL::CodeGenerator>(new SkSL::SPIRVCodeGenera tor()); | |
| 240 cg->generateCode(*program.get(), out); | |
|
dogben
2016/06/23 17:43:11
nit: ".get()" is unnecessary
| |
| 241 ASSERT(!out.rdstate()); | |
|
dogben
2016/06/23 17:43:11
nit: caller's responsibility to check this, and er
| |
| 242 } | |
| 243 return fErrorCount == 0; | |
| 244 } | |
| 245 | |
| 246 bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::string* out) { | |
| 247 std::stringstream buffer; | |
| 248 bool result = this->toSPIRV(kind, text, buffer); | |
| 249 if (result) { | |
| 250 *out = buffer.str(); | |
| 251 } | |
| 252 return fErrorCount == 0; | |
| 253 } | |
| 254 | |
| 255 } // namespace | |
| OLD | NEW |