Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(476)

Side by Side Diff: src/sksl/SkSLCompiler.cpp

Issue 1984363002: initial checkin of SkSL compiler (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: fixed CMake build Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/sksl/SkSLCompiler.h ('k') | src/sksl/SkSLErrorReporter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 auto types = std::shared_ptr<SymbolTable>(new SymbolTable(*this));
43 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(types, *this));
44 fIRGenerator = new IRGenerator(symbols, *this);
45 fTypes = types;
46 #define ADD_TYPE(t) types->add(k ## t ## _Type->fName, k ## t ## _Type)
47 ADD_TYPE(Void);
48 ADD_TYPE(Float);
49 ADD_TYPE(Vec2);
50 ADD_TYPE(Vec3);
51 ADD_TYPE(Vec4);
52 ADD_TYPE(Double);
53 ADD_TYPE(DVec2);
54 ADD_TYPE(DVec3);
55 ADD_TYPE(DVec4);
56 ADD_TYPE(Int);
57 ADD_TYPE(IVec2);
58 ADD_TYPE(IVec3);
59 ADD_TYPE(IVec4);
60 ADD_TYPE(UInt);
61 ADD_TYPE(UVec2);
62 ADD_TYPE(UVec3);
63 ADD_TYPE(UVec4);
64 ADD_TYPE(Bool);
65 ADD_TYPE(BVec2);
66 ADD_TYPE(BVec3);
67 ADD_TYPE(BVec4);
68 ADD_TYPE(Mat2x2);
69 ADD_TYPE(Mat2x3);
70 ADD_TYPE(Mat2x4);
71 ADD_TYPE(Mat3x2);
72 ADD_TYPE(Mat3x3);
73 ADD_TYPE(Mat3x4);
74 ADD_TYPE(Mat4x2);
75 ADD_TYPE(Mat4x3);
76 ADD_TYPE(Mat4x4);
77 ADD_TYPE(GenType);
78 ADD_TYPE(GenDType);
79 ADD_TYPE(GenIType);
80 ADD_TYPE(GenUType);
81 ADD_TYPE(GenBType);
82 ADD_TYPE(Mat);
83 ADD_TYPE(Vec);
84 ADD_TYPE(GVec);
85 ADD_TYPE(GVec2);
86 ADD_TYPE(GVec3);
87 ADD_TYPE(GVec4);
88 ADD_TYPE(DVec);
89 ADD_TYPE(IVec);
90 ADD_TYPE(UVec);
91 ADD_TYPE(BVec);
92
93 ADD_TYPE(Sampler1D);
94 ADD_TYPE(Sampler2D);
95 ADD_TYPE(Sampler3D);
96 ADD_TYPE(SamplerCube);
97 ADD_TYPE(Sampler2DRect);
98 ADD_TYPE(Sampler1DArray);
99 ADD_TYPE(Sampler2DArray);
100 ADD_TYPE(SamplerCubeArray);
101 ADD_TYPE(SamplerBuffer);
102 ADD_TYPE(Sampler2DMS);
103 ADD_TYPE(Sampler2DMSArray);
104
105 ADD_TYPE(GSampler1D);
106 ADD_TYPE(GSampler2D);
107 ADD_TYPE(GSampler3D);
108 ADD_TYPE(GSamplerCube);
109 ADD_TYPE(GSampler2DRect);
110 ADD_TYPE(GSampler1DArray);
111 ADD_TYPE(GSampler2DArray);
112 ADD_TYPE(GSamplerCubeArray);
113 ADD_TYPE(GSamplerBuffer);
114 ADD_TYPE(GSampler2DMS);
115 ADD_TYPE(GSampler2DMSArray);
116
117 ADD_TYPE(Sampler1DShadow);
118 ADD_TYPE(Sampler2DShadow);
119 ADD_TYPE(SamplerCubeShadow);
120 ADD_TYPE(Sampler2DRectShadow);
121 ADD_TYPE(Sampler1DArrayShadow);
122 ADD_TYPE(Sampler2DArrayShadow);
123 ADD_TYPE(SamplerCubeArrayShadow);
124 ADD_TYPE(GSampler2DArrayShadow);
125 ADD_TYPE(GSamplerCubeArrayShadow);
126
127 std::vector<std::unique_ptr<ProgramElement>> ignored;
128 this->internalConvertProgram(SKSL_INCLUDE, &ignored);
129 ASSERT(!fErrorCount);
130 }
131
132 Compiler::~Compiler() {
133 delete fIRGenerator;
134 }
135
136 void Compiler::internalConvertProgram(std::string text,
137 std::vector<std::unique_ptr<ProgramElement >>* result) {
138 Parser parser(text, *fTypes, *this);
139 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file();
140 if (fErrorCount) {
141 return;
142 }
143 for (size_t i = 0; i < parsed.size(); i++) {
144 ASTDeclaration& decl = *parsed[i];
145 switch (decl.fKind) {
146 case ASTDeclaration::kVar_Kind: {
147 std::unique_ptr<VarDeclaration> s = fIRGenerator->convertVarDecl aration(
148 (ASTVar Declaration&) decl,
149 Variabl e::kGlobal_Storage);
150 if (s) {
151 result->push_back(std::move(s));
152 }
153 break;
154 }
155 case ASTDeclaration::kFunction_Kind: {
156 std::unique_ptr<FunctionDefinition> f = fIRGenerator->convertFun ction(
157 ( ASTFunction&) decl);
158 if (f) {
159 result->push_back(std::move(f));
160 }
161 break;
162 }
163 case ASTDeclaration::kInterfaceBlock_Kind: {
164 std::unique_ptr<InterfaceBlock> i = fIRGenerator->convertInterfa ceBlock(
165 (ASTInt erfaceBlock&) decl);
166 if (i) {
167 result->push_back(std::move(i));
168 }
169 break;
170 }
171 case ASTDeclaration::kExtension_Kind: {
172 std::unique_ptr<Extension> e = fIRGenerator->convertExtension((A STExtension&) decl);
173 if (e) {
174 result->push_back(std::move(e));
175 }
176 break;
177 }
178 default:
179 ABORT("unsupported declaration: %s\n", decl.description().c_str( ));
180 }
181 }
182 }
183
184 std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, std::strin g text) {
185 fErrorText = "";
186 fErrorCount = 0;
187 fIRGenerator->pushSymbolTable();
188 std::vector<std::unique_ptr<ProgramElement>> result;
189 switch (kind) {
190 case Program::kVertex_Kind:
191 this->internalConvertProgram(SKSL_VERT_INCLUDE, &result);
192 break;
193 case Program::kFragment_Kind:
194 this->internalConvertProgram(SKSL_FRAG_INCLUDE, &result);
195 break;
196 }
197 this->internalConvertProgram(text, &result);
198 fIRGenerator->popSymbolTable();
199 this->writeErrorCount();
200 return std::unique_ptr<Program>(new Program(kind, std::move(result)));;
201 }
202
203 void Compiler::error(Position position, std::string msg) {
204 fErrorCount++;
205 fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n" ;
206 }
207
208 std::string Compiler::errorText() {
209 std::string result = fErrorText;
210 return result;
211 }
212
213 void Compiler::writeErrorCount() {
214 if (fErrorCount) {
215 fErrorText += to_string(fErrorCount) + " error";
216 if (fErrorCount > 1) {
217 fErrorText += "s";
218 }
219 fErrorText += "\n";
220 }
221 }
222
223 #include <fstream>
224 bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::ostream& out) {
225 auto program = this->convertProgram(kind, text);
226 if (fErrorCount == 0) {
227 SkSL::SPIRVCodeGenerator cg;
228 cg.generateCode(*program.get(), out);
229 ASSERT(!out.rdstate());
230 }
231 return fErrorCount == 0;
232 }
233
234 bool Compiler::toSPIRV(Program::Kind kind, std::string text, std::string* out) {
235 std::stringstream buffer;
236 bool result = this->toSPIRV(kind, text, buffer);
237 if (result) {
238 *out = buffer.str();
239 }
240 return fErrorCount == 0;
241 }
242
243 } // namespace
OLDNEW
« no previous file with comments | « src/sksl/SkSLCompiler.h ('k') | src/sksl/SkSLErrorReporter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698