• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SKSL_COMPILER
9 #define SKSL_COMPILER
10 
11 #include <set>
12 #include <vector>
13 #include "ir/SkSLProgram.h"
14 #include "ir/SkSLSymbolTable.h"
15 #include "SkSLCFGGenerator.h"
16 #include "SkSLContext.h"
17 #include "SkSLErrorReporter.h"
18 #include "SkSLIRGenerator.h"
19 
20 #define SK_FRAGCOLOR_BUILTIN    10001
21 #define SK_IN_BUILTIN           10002
22 #define SK_FRAGCOORD_BUILTIN       15
23 #define SK_VERTEXID_BUILTIN         5
24 #define SK_CLIPDISTANCE_BUILTIN     3
25 #define SK_INVOCATIONID_BUILTIN     8
26 
27 namespace SkSL {
28 
29 class IRGenerator;
30 
31 /**
32  * Main compiler entry point. This is a traditional compiler design which first parses the .sksl
33  * file into an abstract syntax tree (a tree of ASTNodes), then performs semantic analysis to
34  * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce
35  * compiled output.
36  *
37  * See the README for information about SkSL.
38  */
39 class Compiler : public ErrorReporter {
40 public:
41     Compiler();
42 
43     ~Compiler() override;
44 
45     std::unique_ptr<Program> convertProgram(Program::Kind kind, SkString text,
46                                             const Program::Settings& settings);
47 
48     bool toSPIRV(const Program& program, SkWStream& out);
49 
50     bool toSPIRV(const Program& program, SkString* out);
51 
52     bool toGLSL(const Program& program, SkWStream& out);
53 
54     bool toGLSL(const Program& program, SkString* out);
55 
56     void error(Position position, SkString msg) override;
57 
58     SkString errorText();
59 
60     void writeErrorCount();
61 
errorCount()62     int errorCount() override {
63         return fErrorCount;
64     }
65 
66 private:
67     void addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
68                        DefinitionMap* definitions);
69 
70     void addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions);
71 
72     void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList);
73 
74     void scanCFG(const FunctionDefinition& f);
75 
76     void internalConvertProgram(SkString text,
77                                 Modifiers::Flag* defaultPrecision,
78                                 std::vector<std::unique_ptr<ProgramElement>>* result);
79 
80     std::shared_ptr<SymbolTable> fTypes;
81     IRGenerator* fIRGenerator;
82     SkString fSkiaVertText; // FIXME store parsed version instead
83 
84     Context fContext;
85     int fErrorCount;
86     SkString fErrorText;
87 };
88 
89 } // namespace
90 
91 #endif
92