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 std::string SKSL_INCLUDE = | 26 static std::string SKSL_INCLUDE = |
29 #include "sksl.include" | 27 #include "sksl.include" |
(...skipping 62 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 printf("%s", errorText().c_str()); | |
138 ASSERT(!fErrorCount); | 133 ASSERT(!fErrorCount); |
139 } | 134 } |
140 | 135 |
141 Compiler::~Compiler() { | 136 Compiler::~Compiler() { |
142 delete fIRGenerator; | 137 delete fIRGenerator; |
143 } | 138 } |
144 | 139 |
145 void Compiler::internalConvertProgram(std::string text, | 140 void Compiler::internalConvertProgram(std::string text, |
146 Modifiers::Flag* defaultPrecision, | |
147 std::vector<std::unique_ptr<ProgramElement
>>* result) { | 141 std::vector<std::unique_ptr<ProgramElement
>>* result) { |
148 Parser parser(text, *fTypes, *this); | 142 Parser parser(text, *fTypes, *this); |
149 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); | 143 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file(); |
150 if (fErrorCount) { | 144 if (fErrorCount) { |
151 return; | 145 return; |
152 } | 146 } |
153 *defaultPrecision = Modifiers::kHighp_Flag; | |
154 for (size_t i = 0; i < parsed.size(); i++) { | 147 for (size_t i = 0; i < parsed.size(); i++) { |
155 ASTDeclaration& decl = *parsed[i]; | 148 ASTDeclaration& decl = *parsed[i]; |
156 switch (decl.fKind) { | 149 switch (decl.fKind) { |
157 case ASTDeclaration::kVar_Kind: { | 150 case ASTDeclaration::kVar_Kind: { |
158 std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDec
larations( | 151 std::unique_ptr<VarDeclarations> s = fIRGenerator->convertVarDec
larations( |
159 (ASTVar
Declarations&) decl, | 152 (ASTVar
Declarations&) decl, |
160 Variabl
e::kGlobal_Storage); | 153 Variabl
e::kGlobal_Storage); |
161 if (s) { | 154 if (s) { |
162 result->push_back(std::move(s)); | 155 result->push_back(std::move(s)); |
163 } | 156 } |
164 break; | 157 break; |
165 } | 158 } |
166 case ASTDeclaration::kFunction_Kind: { | 159 case ASTDeclaration::kFunction_Kind: { |
167 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFun
ction( | 160 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFun
ction( |
168 (
ASTFunction&) decl); | 161 (
ASTFunction&) decl); |
169 if (f) { | 162 if (f) { |
170 result->push_back(std::move(f)); | 163 result->push_back(std::move(f)); |
171 } | 164 } |
172 break; | 165 break; |
173 } | 166 } |
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 } | |
182 case ASTDeclaration::kInterfaceBlock_Kind: { | 167 case ASTDeclaration::kInterfaceBlock_Kind: { |
183 std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfa
ceBlock( | 168 std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfa
ceBlock( |
184 (ASTInt
erfaceBlock&) decl); | 169 (ASTInt
erfaceBlock&) decl); |
185 if (i) { | 170 if (i) { |
186 result->push_back(std::move(i)); | 171 result->push_back(std::move(i)); |
187 } | 172 } |
188 break; | 173 break; |
189 } | 174 } |
190 case ASTDeclaration::kExtension_Kind: { | 175 case ASTDeclaration::kExtension_Kind: { |
191 std::unique_ptr<Extension> e = fIRGenerator->convertExtension((A
STExtension&) decl); | 176 std::unique_ptr<Extension> e = fIRGenerator->convertExtension((A
STExtension&) decl); |
192 if (e) { | 177 if (e) { |
193 result->push_back(std::move(e)); | 178 result->push_back(std::move(e)); |
194 } | 179 } |
195 break; | 180 break; |
196 } | 181 } |
197 case ASTDeclaration::kPrecision_Kind: { | |
198 *defaultPrecision = ((ASTPrecision&) decl).fPrecision; | |
199 break; | |
200 } | |
201 default: | 182 default: |
202 ABORT("unsupported declaration: %s\n", decl.description().c_str(
)); | 183 ABORT("unsupported declaration: %s\n", decl.description().c_str(
)); |
203 } | 184 } |
204 } | 185 } |
205 } | 186 } |
206 | 187 |
207 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) { |
208 fErrorText = ""; | 189 fErrorText = ""; |
209 fErrorCount = 0; | 190 fErrorCount = 0; |
210 fIRGenerator->pushSymbolTable(); | 191 fIRGenerator->pushSymbolTable(); |
211 std::vector<std::unique_ptr<ProgramElement>> elements; | 192 std::vector<std::unique_ptr<ProgramElement>> elements; |
212 Modifiers::Flag ignored; | |
213 switch (kind) { | 193 switch (kind) { |
214 case Program::kVertex_Kind: | 194 case Program::kVertex_Kind: |
215 this->internalConvertProgram(SKSL_VERT_INCLUDE, &ignored, &elements)
; | 195 this->internalConvertProgram(SKSL_VERT_INCLUDE, &elements); |
216 break; | 196 break; |
217 case Program::kFragment_Kind: | 197 case Program::kFragment_Kind: |
218 this->internalConvertProgram(SKSL_FRAG_INCLUDE, &ignored, &elements)
; | 198 this->internalConvertProgram(SKSL_FRAG_INCLUDE, &elements); |
219 break; | 199 break; |
220 } | 200 } |
221 Modifiers::Flag defaultPrecision; | 201 this->internalConvertProgram(text, &elements); |
222 this->internalConvertProgram(text, &defaultPrecision, &elements); | 202 auto result = std::unique_ptr<Program>(new Program(kind, std::move(elements)
, |
223 auto result = std::unique_ptr<Program>(new Program(kind, defaultPrecision, s
td::move(elements), | |
224 fIRGenerator->fSymbolTabl
e));; | 203 fIRGenerator->fSymbolTabl
e));; |
225 fIRGenerator->popSymbolTable(); | 204 fIRGenerator->popSymbolTable(); |
226 this->writeErrorCount(); | 205 this->writeErrorCount(); |
227 return result; | 206 return result; |
228 } | 207 } |
229 | 208 |
230 void Compiler::error(Position position, std::string msg) { | 209 void Compiler::error(Position position, std::string msg) { |
231 fErrorCount++; | 210 fErrorCount++; |
232 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"
; | 211 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"
; |
233 } | 212 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 std::string* out) { | 260 std::string* out) { |
282 std::stringstream buffer; | 261 std::stringstream buffer; |
283 bool result = this->toGLSL(kind, text, caps, buffer); | 262 bool result = this->toGLSL(kind, text, caps, buffer); |
284 if (result) { | 263 if (result) { |
285 *out = buffer.str(); | 264 *out = buffer.str(); |
286 } | 265 } |
287 return result; | 266 return result; |
288 } | 267 } |
289 | 268 |
290 } // namespace | 269 } // namespace |
OLD | NEW |