| 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> |
| 11 #include <streambuf> | 11 #include <streambuf> |
| 12 | 12 |
| 13 #include "ast/SkSLASTPrecision.h" | |
| 14 #include "SkSLIRGenerator.h" | 13 #include "SkSLIRGenerator.h" |
| 15 #include "SkSLParser.h" | 14 #include "SkSLParser.h" |
| 16 #include "SkSLSPIRVCodeGenerator.h" | 15 #include "SkSLSPIRVCodeGenerator.h" |
| 17 #include "ir/SkSLExpression.h" | 16 #include "ir/SkSLExpression.h" |
| 18 #include "ir/SkSLIntLiteral.h" | 17 #include "ir/SkSLIntLiteral.h" |
| 19 #include "ir/SkSLModifiersDeclaration.h" | |
| 20 #include "ir/SkSLSymbolTable.h" | 18 #include "ir/SkSLSymbolTable.h" |
| 21 #include "ir/SkSLVarDeclaration.h" | 19 #include "ir/SkSLVarDeclaration.h" |
| 22 #include "SkMutex.h" | 20 #include "SkMutex.h" |
| 23 | 21 |
| 24 #define STRINGIFY(x) #x | 22 #define STRINGIFY(x) #x |
| 25 | 23 |
| 26 // include the built-in shader symbols as static strings | 24 // include the built-in shader symbols as static strings |
| 27 | 25 |
| 28 static const char* SKSL_INCLUDE = | 26 static std::string SKSL_INCLUDE = |
| 29 #include "sksl.include" | 27 #include "sksl.include" |
| 30 ; | 28 ; |
| 31 | 29 |
| 32 static const char* SKSL_VERT_INCLUDE = | 30 static std::string SKSL_VERT_INCLUDE = |
| 33 #include "sksl_vert.include" | 31 #include "sksl_vert.include" |
| 34 ; | 32 ; |
| 35 | 33 |
| 36 static const char* SKSL_FRAG_INCLUDE = | 34 static std::string SKSL_FRAG_INCLUDE = |
| 37 #include "sksl_frag.include" | 35 #include "sksl_frag.include" |
| 38 ; | 36 ; |
| 39 | 37 |
| 40 namespace SkSL { | 38 namespace SkSL { |
| 41 | 39 |
| 42 Compiler::Compiler() | 40 Compiler::Compiler() |
| 43 : fErrorCount(0) { | 41 : fErrorCount(0) { |
| 44 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this)); | 42 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this)); |
| 45 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this)); | 43 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this)); |
| 46 fIRGenerator = new IRGenerator(&fContext, symbols, *this); | 44 fIRGenerator = new IRGenerator(&fContext, symbols, *this); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 ADD_TYPE(GVec3); | 90 ADD_TYPE(GVec3); |
| 93 ADD_TYPE(GVec4); | 91 ADD_TYPE(GVec4); |
| 94 ADD_TYPE(DVec); | 92 ADD_TYPE(DVec); |
| 95 ADD_TYPE(IVec); | 93 ADD_TYPE(IVec); |
| 96 ADD_TYPE(UVec); | 94 ADD_TYPE(UVec); |
| 97 ADD_TYPE(BVec); | 95 ADD_TYPE(BVec); |
| 98 | 96 |
| 99 ADD_TYPE(Sampler1D); | 97 ADD_TYPE(Sampler1D); |
| 100 ADD_TYPE(Sampler2D); | 98 ADD_TYPE(Sampler2D); |
| 101 ADD_TYPE(Sampler3D); | 99 ADD_TYPE(Sampler3D); |
| 102 ADD_TYPE(SamplerExternalOES); | |
| 103 ADD_TYPE(SamplerCube); | 100 ADD_TYPE(SamplerCube); |
| 104 ADD_TYPE(Sampler2DRect); | 101 ADD_TYPE(Sampler2DRect); |
| 105 ADD_TYPE(Sampler1DArray); | 102 ADD_TYPE(Sampler1DArray); |
| 106 ADD_TYPE(Sampler2DArray); | 103 ADD_TYPE(Sampler2DArray); |
| 107 ADD_TYPE(SamplerCubeArray); | 104 ADD_TYPE(SamplerCubeArray); |
| 108 ADD_TYPE(SamplerBuffer); | 105 ADD_TYPE(SamplerBuffer); |
| 109 ADD_TYPE(Sampler2DMS); | 106 ADD_TYPE(Sampler2DMS); |
| 110 ADD_TYPE(Sampler2DMSArray); | 107 ADD_TYPE(Sampler2DMSArray); |
| 111 | 108 |
| 112 ADD_TYPE(GSampler1D); | 109 ADD_TYPE(GSampler1D); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 124 ADD_TYPE(Sampler1DShadow); | 121 ADD_TYPE(Sampler1DShadow); |
| 125 ADD_TYPE(Sampler2DShadow); | 122 ADD_TYPE(Sampler2DShadow); |
| 126 ADD_TYPE(SamplerCubeShadow); | 123 ADD_TYPE(SamplerCubeShadow); |
| 127 ADD_TYPE(Sampler2DRectShadow); | 124 ADD_TYPE(Sampler2DRectShadow); |
| 128 ADD_TYPE(Sampler1DArrayShadow); | 125 ADD_TYPE(Sampler1DArrayShadow); |
| 129 ADD_TYPE(Sampler2DArrayShadow); | 126 ADD_TYPE(Sampler2DArrayShadow); |
| 130 ADD_TYPE(SamplerCubeArrayShadow); | 127 ADD_TYPE(SamplerCubeArrayShadow); |
| 131 ADD_TYPE(GSampler2DArrayShadow); | 128 ADD_TYPE(GSampler2DArrayShadow); |
| 132 ADD_TYPE(GSamplerCubeArrayShadow); | 129 ADD_TYPE(GSamplerCubeArrayShadow); |
| 133 | 130 |
| 134 Modifiers::Flag ignored1; | 131 std::vector<std::unique_ptr<ProgramElement>> ignored; |
| 135 std::vector<std::unique_ptr<ProgramElement>> ignored2; | 132 this->internalConvertProgram(SKSL_INCLUDE, &ignored); |
| 136 this->internalConvertProgram(SKSL_INCLUDE, &ignored1, &ignored2); | |
| 137 ASSERT(!fErrorCount); | 133 ASSERT(!fErrorCount); |
| 138 } | 134 } |
| 139 | 135 |
| 140 Compiler::~Compiler() { | 136 Compiler::~Compiler() { |
| 141 delete fIRGenerator; | 137 delete fIRGenerator; |
| 142 } | 138 } |
| 143 | 139 |
| 144 void Compiler::internalConvertProgram(std::string text, | 140 void Compiler::internalConvertProgram(std::string text, |
| 145 Modifiers::Flag* defaultPrecision, | |
| 146 std::vector<std::unique_ptr<ProgramElement
>>* result) { | 141 std::vector<std::unique_ptr<ProgramElement
>>* result) { |
| 147 Parser parser(text, *fTypes, *this); | 142 Parser parser(text, *fTypes, *this); |
| 148 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); | 143 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); |
| 149 if (fErrorCount) { | 144 if (fErrorCount) { |
| 150 return; | 145 return; |
| 151 } | 146 } |
| 152 *defaultPrecision = Modifiers::kHighp_Flag; | |
| 153 for (size_t i = 0; i < parsed.size(); i++) { | 147 for (size_t i = 0; i < parsed.size(); i++) { |
| 154 ASTDeclaration& decl = *parsed[i]; | 148 ASTDeclaration& decl = *parsed[i]; |
| 155 switch (decl.fKind) { | 149 switch (decl.fKind) { |
| 156 case ASTDeclaration::kVar_Kind: { | 150 case ASTDeclaration::kVar_Kind: { |
| 157 std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDec
larations( | 151 std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDec
larations( |
| 158 (ASTVar
Declarations&) decl, | 152 (ASTVar
Declarations&) decl, |
| 159 Variabl
e::kGlobal_Storage); | 153 Variabl
e::kGlobal_Storage); |
| 160 if (s) { | 154 if (s) { |
| 161 result->push_back(std::move(s)); | 155 result->push_back(std::move(s)); |
| 162 } | 156 } |
| 163 break; | 157 break; |
| 164 } | 158 } |
| 165 case ASTDeclaration::kFunction_Kind: { | 159 case ASTDeclaration::kFunction_Kind: { |
| 166 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFun
ction( | 160 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFun
ction( |
| 167 (
ASTFunction&) decl); | 161 (
ASTFunction&) decl); |
| 168 if (f) { | 162 if (f) { |
| 169 result->push_back(std::move(f)); | 163 result->push_back(std::move(f)); |
| 170 } | 164 } |
| 171 break; | 165 break; |
| 172 } | 166 } |
| 173 case ASTDeclaration::kModifiers_Kind: { | |
| 174 std::unique_ptr<ModifiersDeclaration> f = fIRGenerator->convertM
odifiersDeclaration( | |
| 175 (ASTModifiers
Declaration&) decl); | |
| 176 if (f) { | |
| 177 result->push_back(std::move(f)); | |
| 178 } | |
| 179 break; | |
| 180 } | |
| 181 case ASTDeclaration::kInterfaceBlock_Kind: { | 167 case ASTDeclaration::kInterfaceBlock_Kind: { |
| 182 std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfa
ceBlock( | 168 std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfa
ceBlock( |
| 183 (ASTInt
erfaceBlock&) decl); | 169 (ASTInt
erfaceBlock&) decl); |
| 184 if (i) { | 170 if (i) { |
| 185 result->push_back(std::move(i)); | 171 result->push_back(std::move(i)); |
| 186 } | 172 } |
| 187 break; | 173 break; |
| 188 } | 174 } |
| 189 case ASTDeclaration::kExtension_Kind: { | 175 case ASTDeclaration::kExtension_Kind: { |
| 190 std::unique_ptr<Extension> e = fIRGenerator->convertExtension((A
STExtension&) decl); | 176 std::unique_ptr<Extension> e = fIRGenerator->convertExtension((A
STExtension&) decl); |
| 191 if (e) { | 177 if (e) { |
| 192 result->push_back(std::move(e)); | 178 result->push_back(std::move(e)); |
| 193 } | 179 } |
| 194 break; | 180 break; |
| 195 } | 181 } |
| 196 case ASTDeclaration::kPrecision_Kind: { | |
| 197 *defaultPrecision = ((ASTPrecision&) decl).fPrecision; | |
| 198 break; | |
| 199 } | |
| 200 default: | 182 default: |
| 201 ABORT("unsupported declaration: %s\n", decl.description().c_str(
)); | 183 ABORT("unsupported declaration: %s\n", decl.description().c_str(
)); |
| 202 } | 184 } |
| 203 } | 185 } |
| 204 } | 186 } |
| 205 | 187 |
| 206 std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, std::strin
g text) { | 188 std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, std::strin
g text) { |
| 207 fErrorText = ""; | 189 fErrorText = ""; |
| 208 fErrorCount = 0; | 190 fErrorCount = 0; |
| 209 fIRGenerator->pushSymbolTable(); | 191 fIRGenerator->pushSymbolTable(); |
| 210 std::vector<std::unique_ptr<ProgramElement>> elements; | 192 std::vector<std::unique_ptr<ProgramElement>> elements; |
| 211 Modifiers::Flag ignored; | |
| 212 switch (kind) { | 193 switch (kind) { |
| 213 case Program::kVertex_Kind: | 194 case Program::kVertex_Kind: |
| 214 this->internalConvertProgram(SKSL_VERT_INCLUDE, &ignored, &elements)
; | 195 this->internalConvertProgram(SKSL_VERT_INCLUDE, &elements); |
| 215 break; | 196 break; |
| 216 case Program::kFragment_Kind: | 197 case Program::kFragment_Kind: |
| 217 this->internalConvertProgram(SKSL_FRAG_INCLUDE, &ignored, &elements)
; | 198 this->internalConvertProgram(SKSL_FRAG_INCLUDE, &elements); |
| 218 break; | 199 break; |
| 219 } | 200 } |
| 220 Modifiers::Flag defaultPrecision; | 201 this->internalConvertProgram(text, &elements); |
| 221 this->internalConvertProgram(text, &defaultPrecision, &elements); | 202 auto result = std::unique_ptr<Program>(new Program(kind, std::move(elements)
, |
| 222 auto result = std::unique_ptr<Program>(new Program(kind, defaultPrecision, s
td::move(elements), | |
| 223 fIRGenerator->fSymbolTabl
e));; | 203 fIRGenerator->fSymbolTabl
e));; |
| 224 fIRGenerator->popSymbolTable(); | 204 fIRGenerator->popSymbolTable(); |
| 225 this->writeErrorCount(); | 205 this->writeErrorCount(); |
| 226 return result; | 206 return result; |
| 227 } | 207 } |
| 228 | 208 |
| 229 void Compiler::error(Position position, std::string msg) { | 209 void Compiler::error(Position position, std::string msg) { |
| 230 fErrorCount++; | 210 fErrorCount++; |
| 231 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"
; | 211 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"
; |
| 232 } | 212 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 std::string* out) { | 260 std::string* out) { |
| 281 std::stringstream buffer; | 261 std::stringstream buffer; |
| 282 bool result = this->toGLSL(kind, text, caps, buffer); | 262 bool result = this->toGLSL(kind, text, caps, buffer); |
| 283 if (result) { | 263 if (result) { |
| 284 *out = buffer.str(); | 264 *out = buffer.str(); |
| 285 } | 265 } |
| 286 return result; | 266 return result; |
| 287 } | 267 } |
| 288 | 268 |
| 289 } // namespace | 269 } // namespace |
| OLD | NEW |