SkSLCompiler.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #ifndef SKSL_COMPILER
  8. #define SKSL_COMPILER
  9. #include <set>
  10. #include <unordered_set>
  11. #include <vector>
  12. #include "src/sksl/SkSLByteCode.h"
  13. #include "src/sksl/SkSLCFGGenerator.h"
  14. #include "src/sksl/SkSLContext.h"
  15. #include "src/sksl/SkSLErrorReporter.h"
  16. #include "src/sksl/SkSLLexer.h"
  17. #include "src/sksl/ir/SkSLProgram.h"
  18. #include "src/sksl/ir/SkSLSymbolTable.h"
  19. #define SK_FRAGCOLOR_BUILTIN 10001
  20. #define SK_IN_BUILTIN 10002
  21. #define SK_INCOLOR_BUILTIN 10003
  22. #define SK_OUTCOLOR_BUILTIN 10004
  23. #define SK_TRANSFORMEDCOORDS2D_BUILTIN 10005
  24. #define SK_TEXTURESAMPLERS_BUILTIN 10006
  25. #define SK_OUT_BUILTIN 10007
  26. #define SK_LASTFRAGCOLOR_BUILTIN 10008
  27. #define SK_MAIN_X_BUILTIN 10009
  28. #define SK_MAIN_Y_BUILTIN 10010
  29. #define SK_WIDTH_BUILTIN 10011
  30. #define SK_HEIGHT_BUILTIN 10012
  31. #define SK_FRAGCOORD_BUILTIN 15
  32. #define SK_CLOCKWISE_BUILTIN 17
  33. #define SK_VERTEXID_BUILTIN 42
  34. #define SK_INSTANCEID_BUILTIN 43
  35. #define SK_CLIPDISTANCE_BUILTIN 3
  36. #define SK_INVOCATIONID_BUILTIN 8
  37. #define SK_POSITION_BUILTIN 0
  38. namespace SkSL {
  39. class IRGenerator;
  40. /**
  41. * Main compiler entry point. This is a traditional compiler design which first parses the .sksl
  42. * file into an abstract syntax tree (a tree of ASTNodes), then performs semantic analysis to
  43. * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce
  44. * compiled output.
  45. *
  46. * See the README for information about SkSL.
  47. */
  48. class SK_API Compiler : public ErrorReporter {
  49. public:
  50. static constexpr const char* RTADJUST_NAME = "sk_RTAdjust";
  51. static constexpr const char* PERVERTEX_NAME = "sk_PerVertex";
  52. enum Flags {
  53. kNone_Flags = 0,
  54. // permits static if/switch statements to be used with non-constant tests. This is used when
  55. // producing H and CPP code; the static tests don't have to have constant values *yet*, but
  56. // the generated code will contain a static test which then does have to be a constant.
  57. kPermitInvalidStaticTests_Flag = 1,
  58. };
  59. struct FormatArg {
  60. enum class Kind {
  61. kInput,
  62. kOutput,
  63. kUniform,
  64. kChildProcessor
  65. };
  66. FormatArg(Kind kind)
  67. : fKind(kind) {}
  68. FormatArg(Kind kind, int index)
  69. : fKind(kind)
  70. , fIndex(index) {}
  71. Kind fKind;
  72. int fIndex;
  73. };
  74. Compiler(Flags flags = kNone_Flags);
  75. ~Compiler() override;
  76. Compiler(const Compiler&) = delete;
  77. Compiler& operator=(const Compiler&) = delete;
  78. /**
  79. * Registers an ExternalValue as a top-level symbol which is visible in the global namespace.
  80. */
  81. void registerExternalValue(ExternalValue* value);
  82. std::unique_ptr<Program> convertProgram(Program::Kind kind, String text,
  83. const Program::Settings& settings);
  84. bool optimize(Program& program);
  85. std::unique_ptr<Program> specialize(Program& program,
  86. const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs);
  87. bool toSPIRV(Program& program, OutputStream& out);
  88. bool toSPIRV(Program& program, String* out);
  89. bool toGLSL(Program& program, OutputStream& out);
  90. bool toGLSL(Program& program, String* out);
  91. bool toMetal(Program& program, OutputStream& out);
  92. bool toMetal(Program& program, String* out);
  93. bool toCPP(Program& program, String name, OutputStream& out);
  94. bool toH(Program& program, String name, OutputStream& out);
  95. std::unique_ptr<ByteCode> toByteCode(Program& program);
  96. bool toPipelineStage(const Program& program, String* out,
  97. std::vector<FormatArg>* outFormatArgs);
  98. /**
  99. * Takes ownership of the given symbol. It will be destroyed when the compiler is destroyed.
  100. */
  101. Symbol* takeOwnership(std::unique_ptr<Symbol> symbol);
  102. void error(int offset, String msg) override;
  103. String errorText();
  104. void writeErrorCount();
  105. int errorCount() override {
  106. return fErrorCount;
  107. }
  108. Context& context() {
  109. return *fContext;
  110. }
  111. static const char* OperatorName(Token::Kind token);
  112. static bool IsAssignment(Token::Kind token);
  113. private:
  114. void processIncludeFile(Program::Kind kind, const char* src, size_t length,
  115. std::shared_ptr<SymbolTable> base,
  116. std::vector<std::unique_ptr<ProgramElement>>* outElements,
  117. std::shared_ptr<SymbolTable>* outSymbolTable);
  118. void addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
  119. DefinitionMap* definitions);
  120. void addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions);
  121. void scanCFG(CFG* cfg, BlockId block, std::set<BlockId>* workList);
  122. void computeDataFlow(CFG* cfg);
  123. /**
  124. * Simplifies the expression pointed to by iter (in both the IR and CFG structures), if
  125. * possible.
  126. */
  127. void simplifyExpression(DefinitionMap& definitions,
  128. BasicBlock& b,
  129. std::vector<BasicBlock::Node>::iterator* iter,
  130. std::unordered_set<const Variable*>* undefinedVariables,
  131. bool* outUpdated,
  132. bool* outNeedsRescan);
  133. /**
  134. * Simplifies the statement pointed to by iter (in both the IR and CFG structures), if
  135. * possible.
  136. */
  137. void simplifyStatement(DefinitionMap& definitions,
  138. BasicBlock& b,
  139. std::vector<BasicBlock::Node>::iterator* iter,
  140. std::unordered_set<const Variable*>* undefinedVariables,
  141. bool* outUpdated,
  142. bool* outNeedsRescan);
  143. void scanCFG(FunctionDefinition& f);
  144. Position position(int offset);
  145. std::shared_ptr<SymbolTable> fGpuSymbolTable;
  146. std::vector<std::unique_ptr<ProgramElement>> fVertexInclude;
  147. std::shared_ptr<SymbolTable> fVertexSymbolTable;
  148. std::vector<std::unique_ptr<ProgramElement>> fFragmentInclude;
  149. std::shared_ptr<SymbolTable> fFragmentSymbolTable;
  150. std::vector<std::unique_ptr<ProgramElement>> fGeometryInclude;
  151. std::shared_ptr<SymbolTable> fGeometrySymbolTable;
  152. std::vector<std::unique_ptr<ProgramElement>> fPipelineInclude;
  153. std::shared_ptr<SymbolTable> fPipelineSymbolTable;
  154. std::vector<std::unique_ptr<ProgramElement>> fInterpreterInclude;
  155. std::shared_ptr<SymbolTable> fInterpreterSymbolTable;
  156. std::shared_ptr<SymbolTable> fTypes;
  157. IRGenerator* fIRGenerator;
  158. int fFlags;
  159. const String* fSource;
  160. std::shared_ptr<Context> fContext;
  161. int fErrorCount;
  162. String fErrorText;
  163. };
  164. } // namespace
  165. #endif