SkSLSPIRVCodeGenerator.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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_SPIRVCODEGENERATOR
  8. #define SKSL_SPIRVCODEGENERATOR
  9. #include <stack>
  10. #include <tuple>
  11. #include <unordered_map>
  12. #include "src/sksl/SkSLCodeGenerator.h"
  13. #include "src/sksl/SkSLMemoryLayout.h"
  14. #include "src/sksl/SkSLStringStream.h"
  15. #include "src/sksl/ir/SkSLBinaryExpression.h"
  16. #include "src/sksl/ir/SkSLBoolLiteral.h"
  17. #include "src/sksl/ir/SkSLConstructor.h"
  18. #include "src/sksl/ir/SkSLDoStatement.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/SkSLStatement.h"
  34. #include "src/sksl/ir/SkSLSwitchStatement.h"
  35. #include "src/sksl/ir/SkSLSwizzle.h"
  36. #include "src/sksl/ir/SkSLTernaryExpression.h"
  37. #include "src/sksl/ir/SkSLVarDeclarations.h"
  38. #include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
  39. #include "src/sksl/ir/SkSLVariableReference.h"
  40. #include "src/sksl/ir/SkSLWhileStatement.h"
  41. #include "src/sksl/spirv.h"
  42. union ConstantValue {
  43. ConstantValue(int64_t i)
  44. : fInt(i) {}
  45. ConstantValue(double d)
  46. : fDouble(d) {}
  47. bool operator==(const ConstantValue& other) const {
  48. return fInt == other.fInt;
  49. }
  50. int64_t fInt;
  51. double fDouble;
  52. };
  53. enum class ConstantType {
  54. kInt,
  55. kUInt,
  56. kShort,
  57. kUShort,
  58. kFloat,
  59. kDouble,
  60. kHalf,
  61. };
  62. namespace std {
  63. template <>
  64. struct hash<std::pair<ConstantValue, ConstantType>> {
  65. size_t operator()(const std::pair<ConstantValue, ConstantType>& key) const {
  66. return key.first.fInt ^ (int) key.second;
  67. }
  68. };
  69. }
  70. namespace SkSL {
  71. #define kLast_Capability SpvCapabilityMultiViewport
  72. /**
  73. * Converts a Program into a SPIR-V binary.
  74. */
  75. class SPIRVCodeGenerator : public CodeGenerator {
  76. public:
  77. class LValue {
  78. public:
  79. virtual ~LValue() {}
  80. // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
  81. // by a pointer (e.g. vector swizzles), returns 0.
  82. virtual SpvId getPointer() = 0;
  83. virtual SpvId load(OutputStream& out) = 0;
  84. virtual void store(SpvId value, OutputStream& out) = 0;
  85. };
  86. SPIRVCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
  87. OutputStream* out)
  88. : INHERITED(program, errors, out)
  89. , fContext(*context)
  90. , fDefaultLayout(MemoryLayout::k140_Standard)
  91. , fCapabilities(0)
  92. , fIdCount(1)
  93. , fBoolTrue(0)
  94. , fBoolFalse(0)
  95. , fSetupFragPosition(false)
  96. , fCurrentBlock(0)
  97. , fSynthetics(nullptr, errors) {
  98. this->setupIntrinsics();
  99. }
  100. bool generateCode() override;
  101. private:
  102. enum IntrinsicKind {
  103. kGLSL_STD_450_IntrinsicKind,
  104. kSPIRV_IntrinsicKind,
  105. kSpecial_IntrinsicKind
  106. };
  107. enum SpecialIntrinsic {
  108. kAtan_SpecialIntrinsic,
  109. kClamp_SpecialIntrinsic,
  110. kMax_SpecialIntrinsic,
  111. kMin_SpecialIntrinsic,
  112. kMix_SpecialIntrinsic,
  113. kMod_SpecialIntrinsic,
  114. kDFdy_SpecialIntrinsic,
  115. kSaturate_SpecialIntrinsic,
  116. kSubpassLoad_SpecialIntrinsic,
  117. kTexture_SpecialIntrinsic,
  118. };
  119. enum class Precision {
  120. kLow,
  121. kHigh,
  122. };
  123. void setupIntrinsics();
  124. SpvId nextId();
  125. Type getActualType(const Type& type);
  126. SpvId getType(const Type& type);
  127. SpvId getType(const Type& type, const MemoryLayout& layout);
  128. SpvId getImageType(const Type& type);
  129. SpvId getFunctionType(const FunctionDeclaration& function);
  130. SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
  131. SpvId getPointerType(const Type& type, const MemoryLayout& layout,
  132. SpvStorageClass_ storageClass);
  133. void writePrecisionModifier(Precision precision, SpvId id);
  134. void writePrecisionModifier(const Type& type, SpvId id);
  135. std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
  136. void writeLayout(const Layout& layout, SpvId target);
  137. void writeLayout(const Layout& layout, SpvId target, int member);
  138. void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
  139. void writeProgramElement(const ProgramElement& pe, OutputStream& out);
  140. SpvId writeInterfaceBlock(const InterfaceBlock& intf);
  141. SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
  142. SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
  143. SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
  144. void writeGlobalVars(Program::Kind kind, const VarDeclarations& v, OutputStream& out);
  145. void writeVarDeclarations(const VarDeclarations& decl, OutputStream& out);
  146. SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
  147. std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
  148. SpvId writeExpression(const Expression& expr, OutputStream& out);
  149. SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
  150. SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
  151. void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
  152. SpvId signedInst, SpvId unsignedInst,
  153. const std::vector<SpvId>& args, OutputStream& out);
  154. /**
  155. * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
  156. * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
  157. * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
  158. * vec2, vec3).
  159. */
  160. std::vector<SpvId> vectorize(const std::vector<std::unique_ptr<Expression>>& args,
  161. OutputStream& out);
  162. SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
  163. SpvId writeConstantVector(const Constructor& c);
  164. SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
  165. SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
  166. SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
  167. /**
  168. * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
  169. * entries equal to zero.
  170. */
  171. void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
  172. /**
  173. * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
  174. * source matrix are filled with zero; entries which do not exist in the destination matrix are
  175. * ignored.
  176. */
  177. void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
  178. OutputStream& out);
  179. void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
  180. std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
  181. OutputStream& out);
  182. SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
  183. SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
  184. SpvId writeArrayConstructor(const Constructor& c, OutputStream& out);
  185. SpvId writeConstructor(const Constructor& c, OutputStream& out);
  186. SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
  187. SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
  188. /**
  189. * Folds the potentially-vector result of a logical operation down to a single bool. If
  190. * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
  191. * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
  192. * returns the original id value.
  193. */
  194. SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
  195. SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
  196. SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
  197. SpvOp_ mergeOperator, OutputStream& out);
  198. SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
  199. SpvOp_ floatOperator, SpvOp_ intOperator,
  200. OutputStream& out);
  201. SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
  202. SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
  203. SpvOp_ ifBool, OutputStream& out);
  204. SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
  205. SpvOp_ ifUInt, OutputStream& out);
  206. SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op,
  207. const Type& rightType, SpvId rhs, const Type& resultType,
  208. OutputStream& out);
  209. SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
  210. SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
  211. SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
  212. SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
  213. SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
  214. SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
  215. SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
  216. SpvId writeBoolLiteral(const BoolLiteral& b);
  217. SpvId writeIntLiteral(const IntLiteral& i);
  218. SpvId writeFloatLiteral(const FloatLiteral& f);
  219. void writeStatement(const Statement& s, OutputStream& out);
  220. void writeBlock(const Block& b, OutputStream& out);
  221. void writeIfStatement(const IfStatement& stmt, OutputStream& out);
  222. void writeForStatement(const ForStatement& f, OutputStream& out);
  223. void writeWhileStatement(const WhileStatement& w, OutputStream& out);
  224. void writeDoStatement(const DoStatement& d, OutputStream& out);
  225. void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
  226. void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
  227. void writeCapabilities(OutputStream& out);
  228. void writeInstructions(const Program& program, OutputStream& out);
  229. void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
  230. void writeWord(int32_t word, OutputStream& out);
  231. void writeString(const char* string, size_t length, OutputStream& out);
  232. void writeLabel(SpvId id, OutputStream& out);
  233. void writeInstruction(SpvOp_ opCode, OutputStream& out);
  234. void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
  235. void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
  236. void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
  237. void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
  238. OutputStream& out);
  239. void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
  240. void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
  241. OutputStream& out);
  242. void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
  243. OutputStream& out);
  244. void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
  245. int32_t word5, OutputStream& out);
  246. void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
  247. int32_t word5, int32_t word6, OutputStream& out);
  248. void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
  249. int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
  250. void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
  251. int32_t word5, int32_t word6, int32_t word7, int32_t word8,
  252. OutputStream& out);
  253. void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
  254. const Context& fContext;
  255. const MemoryLayout fDefaultLayout;
  256. uint64_t fCapabilities;
  257. SpvId fIdCount;
  258. SpvId fGLSLExtendedInstructions;
  259. typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
  260. std::unordered_map<String, Intrinsic> fIntrinsicMap;
  261. std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
  262. std::unordered_map<const Variable*, SpvId> fVariableMap;
  263. std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
  264. std::unordered_map<String, SpvId> fImageTypeMap;
  265. std::unordered_map<String, SpvId> fTypeMap;
  266. StringStream fCapabilitiesBuffer;
  267. StringStream fGlobalInitializersBuffer;
  268. StringStream fConstantBuffer;
  269. StringStream fExtraGlobalsBuffer;
  270. StringStream fExternalFunctionsBuffer;
  271. StringStream fVariableBuffer;
  272. StringStream fNameBuffer;
  273. StringStream fDecorationBuffer;
  274. SpvId fBoolTrue;
  275. SpvId fBoolFalse;
  276. std::unordered_map<std::pair<ConstantValue, ConstantType>, SpvId> fNumberConstants;
  277. // The constant float2(0, 1), used in swizzling
  278. SpvId fConstantZeroOneVector = 0;
  279. bool fSetupFragPosition;
  280. // label of the current block, or 0 if we are not in a block
  281. SpvId fCurrentBlock;
  282. std::stack<SpvId> fBreakTarget;
  283. std::stack<SpvId> fContinueTarget;
  284. SpvId fRTHeightStructId = (SpvId) -1;
  285. SpvId fRTHeightFieldIndex = (SpvId) -1;
  286. // holds variables synthesized during output, for lifetime purposes
  287. SymbolTable fSynthetics;
  288. int fSkInCount = 1;
  289. friend class PointerLValue;
  290. friend class SwizzleLValue;
  291. typedef CodeGenerator INHERITED;
  292. };
  293. }
  294. #endif