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