SkSLGLSLCodeGenerator.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_GLSLCODEGENERATOR
  8. #define SKSL_GLSLCODEGENERATOR
  9. #include <stack>
  10. #include <tuple>
  11. #include <unordered_map>
  12. #include "src/sksl/SkSLCodeGenerator.h"
  13. #include "src/sksl/SkSLStringStream.h"
  14. #include "src/sksl/ir/SkSLBinaryExpression.h"
  15. #include "src/sksl/ir/SkSLBoolLiteral.h"
  16. #include "src/sksl/ir/SkSLConstructor.h"
  17. #include "src/sksl/ir/SkSLDoStatement.h"
  18. #include "src/sksl/ir/SkSLExtension.h"
  19. #include "src/sksl/ir/SkSLFieldAccess.h"
  20. #include "src/sksl/ir/SkSLFloatLiteral.h"
  21. #include "src/sksl/ir/SkSLForStatement.h"
  22. #include "src/sksl/ir/SkSLFunctionCall.h"
  23. #include "src/sksl/ir/SkSLFunctionDeclaration.h"
  24. #include "src/sksl/ir/SkSLFunctionDefinition.h"
  25. #include "src/sksl/ir/SkSLIfStatement.h"
  26. #include "src/sksl/ir/SkSLIndexExpression.h"
  27. #include "src/sksl/ir/SkSLIntLiteral.h"
  28. #include "src/sksl/ir/SkSLInterfaceBlock.h"
  29. #include "src/sksl/ir/SkSLPostfixExpression.h"
  30. #include "src/sksl/ir/SkSLPrefixExpression.h"
  31. #include "src/sksl/ir/SkSLProgramElement.h"
  32. #include "src/sksl/ir/SkSLReturnStatement.h"
  33. #include "src/sksl/ir/SkSLSetting.h"
  34. #include "src/sksl/ir/SkSLStatement.h"
  35. #include "src/sksl/ir/SkSLSwitchStatement.h"
  36. #include "src/sksl/ir/SkSLSwizzle.h"
  37. #include "src/sksl/ir/SkSLTernaryExpression.h"
  38. #include "src/sksl/ir/SkSLVarDeclarations.h"
  39. #include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
  40. #include "src/sksl/ir/SkSLVariableReference.h"
  41. #include "src/sksl/ir/SkSLWhileStatement.h"
  42. namespace SkSL {
  43. #define kLast_Capability SpvCapabilityMultiViewport
  44. /**
  45. * Converts a Program into GLSL code.
  46. */
  47. class GLSLCodeGenerator : public CodeGenerator {
  48. public:
  49. enum Precedence {
  50. kParentheses_Precedence = 1,
  51. kPostfix_Precedence = 2,
  52. kPrefix_Precedence = 3,
  53. kMultiplicative_Precedence = 4,
  54. kAdditive_Precedence = 5,
  55. kShift_Precedence = 6,
  56. kRelational_Precedence = 7,
  57. kEquality_Precedence = 8,
  58. kBitwiseAnd_Precedence = 9,
  59. kBitwiseXor_Precedence = 10,
  60. kBitwiseOr_Precedence = 11,
  61. kLogicalAnd_Precedence = 12,
  62. kLogicalXor_Precedence = 13,
  63. kLogicalOr_Precedence = 14,
  64. kTernary_Precedence = 15,
  65. kAssignment_Precedence = 16,
  66. kSequence_Precedence = 17,
  67. kTopLevel_Precedence = kSequence_Precedence
  68. };
  69. GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
  70. OutputStream* out)
  71. : INHERITED(program, errors, out)
  72. , fLineEnding("\n")
  73. , fContext(*context)
  74. , fProgramKind(program->fKind) {}
  75. bool generateCode() override;
  76. protected:
  77. void write(const char* s);
  78. void writeLine();
  79. void writeLine(const char* s);
  80. void write(const String& s);
  81. void write(StringFragment s);
  82. void writeLine(const String& s);
  83. virtual void writeHeader();
  84. virtual bool usesPrecisionModifiers() const;
  85. virtual String getTypeName(const Type& type);
  86. void writeType(const Type& type);
  87. void writeExtension(const String& name);
  88. void writeExtension(const String& name, bool require);
  89. void writeInterfaceBlock(const InterfaceBlock& intf);
  90. void writeFunctionStart(const FunctionDeclaration& f);
  91. void writeFunctionDeclaration(const FunctionDeclaration& f);
  92. virtual void writeFunction(const FunctionDefinition& f);
  93. void writeLayout(const Layout& layout);
  94. void writeModifiers(const Modifiers& modifiers, bool globalContext);
  95. virtual void writeInputVars();
  96. virtual void writeVarInitializer(const Variable& var, const Expression& value);
  97. const char* getTypePrecision(const Type& type);
  98. void writeTypePrecision(const Type& type);
  99. void writeVarDeclarations(const VarDeclarations& decl, bool global);
  100. void writeFragCoord();
  101. virtual void writeVariableReference(const VariableReference& ref);
  102. void writeExpression(const Expression& expr, Precedence parentPrecedence);
  103. void writeIntrinsicCall(const FunctionCall& c);
  104. void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
  105. void writeDeterminantHack(const Expression& mat);
  106. void writeInverseHack(const Expression& mat);
  107. void writeTransposeHack(const Expression& mat);
  108. void writeInverseSqrtHack(const Expression& x);
  109. virtual void writeFunctionCall(const FunctionCall& c);
  110. void writeConstructor(const Constructor& c, Precedence parentPrecedence);
  111. virtual void writeFieldAccess(const FieldAccess& f);
  112. virtual void writeSwizzle(const Swizzle& swizzle);
  113. static Precedence GetBinaryPrecedence(Token::Kind op);
  114. virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
  115. void writeShortCircuitWorkaroundExpression(const BinaryExpression& b,
  116. Precedence parentPrecedence);
  117. void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
  118. virtual void writeIndexExpression(const IndexExpression& expr);
  119. void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
  120. void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
  121. void writeBoolLiteral(const BoolLiteral& b);
  122. virtual void writeIntLiteral(const IntLiteral& i);
  123. void writeFloatLiteral(const FloatLiteral& f);
  124. virtual void writeSetting(const Setting& s);
  125. void writeStatement(const Statement& s);
  126. void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
  127. void writeBlock(const Block& b);
  128. virtual void writeIfStatement(const IfStatement& stmt);
  129. void writeForStatement(const ForStatement& f);
  130. void writeWhileStatement(const WhileStatement& w);
  131. void writeDoStatement(const DoStatement& d);
  132. virtual void writeSwitchStatement(const SwitchStatement& s);
  133. virtual void writeReturnStatement(const ReturnStatement& r);
  134. virtual void writeProgramElement(const ProgramElement& e);
  135. const char* fLineEnding;
  136. const Context& fContext;
  137. StringStream fExtensions;
  138. StringStream fGlobals;
  139. StringStream fExtraFunctions;
  140. String fFunctionHeader;
  141. Program::Kind fProgramKind;
  142. int fVarCount = 0;
  143. int fIndentation = 0;
  144. bool fAtLineStart = false;
  145. // Keeps track of which struct types we have written. Given that we are unlikely to ever write
  146. // more than one or two structs per shader, a simple linear search will be faster than anything
  147. // fancier.
  148. std::vector<const Type*> fWrittenStructs;
  149. std::set<String> fWrittenIntrinsics;
  150. // true if we have run into usages of dFdx / dFdy
  151. bool fFoundDerivatives = false;
  152. bool fFoundExternalSamplerDecl = false;
  153. bool fFoundGSInvocations = false;
  154. bool fSetupFragPositionGlobal = false;
  155. bool fSetupFragPositionLocal = false;
  156. bool fSetupFragCoordWorkaround = false;
  157. // We map function names to function class so we can quickly deal with function calls that need
  158. // extra processing
  159. enum class FunctionClass {
  160. kAbs,
  161. kAtan,
  162. kDeterminant,
  163. kDFdx,
  164. kDFdy,
  165. kFwidth,
  166. kFMA,
  167. kFract,
  168. kInverse,
  169. kInverseSqrt,
  170. kMin,
  171. kPow,
  172. kSaturate,
  173. kTexture,
  174. kTranspose
  175. };
  176. static std::unordered_map<StringFragment, FunctionClass>* fFunctionClasses;
  177. typedef CodeGenerator INHERITED;
  178. };
  179. }
  180. #endif