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

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

Powered by Google App Engine
This is Rietveld 408576698