node.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <cstddef>
  7. #include <memory>
  8. #include <string>
  9. #include <tuple>
  10. #include <utility>
  11. #include <variant>
  12. #include <vector>
  13. #include "common/common_types.h"
  14. #include "video_core/engines/shader_bytecode.h"
  15. namespace VideoCommon::Shader {
  16. enum class OperationCode {
  17. Assign, /// (float& dest, float src) -> void
  18. Select, /// (MetaArithmetic, bool pred, float a, float b) -> float
  19. FAdd, /// (MetaArithmetic, float a, float b) -> float
  20. FMul, /// (MetaArithmetic, float a, float b) -> float
  21. FDiv, /// (MetaArithmetic, float a, float b) -> float
  22. FFma, /// (MetaArithmetic, float a, float b, float c) -> float
  23. FNegate, /// (MetaArithmetic, float a) -> float
  24. FAbsolute, /// (MetaArithmetic, float a) -> float
  25. FClamp, /// (MetaArithmetic, float value, float min, float max) -> float
  26. FMin, /// (MetaArithmetic, float a, float b) -> float
  27. FMax, /// (MetaArithmetic, float a, float b) -> float
  28. FCos, /// (MetaArithmetic, float a) -> float
  29. FSin, /// (MetaArithmetic, float a) -> float
  30. FExp2, /// (MetaArithmetic, float a) -> float
  31. FLog2, /// (MetaArithmetic, float a) -> float
  32. FInverseSqrt, /// (MetaArithmetic, float a) -> float
  33. FSqrt, /// (MetaArithmetic, float a) -> float
  34. FRoundEven, /// (MetaArithmetic, float a) -> float
  35. FFloor, /// (MetaArithmetic, float a) -> float
  36. FCeil, /// (MetaArithmetic, float a) -> float
  37. FTrunc, /// (MetaArithmetic, float a) -> float
  38. FCastInteger, /// (MetaArithmetic, int a) -> float
  39. FCastUInteger, /// (MetaArithmetic, uint a) -> float
  40. IAdd, /// (MetaArithmetic, int a, int b) -> int
  41. IMul, /// (MetaArithmetic, int a, int b) -> int
  42. IDiv, /// (MetaArithmetic, int a, int b) -> int
  43. INegate, /// (MetaArithmetic, int a) -> int
  44. IAbsolute, /// (MetaArithmetic, int a) -> int
  45. IMin, /// (MetaArithmetic, int a, int b) -> int
  46. IMax, /// (MetaArithmetic, int a, int b) -> int
  47. ICastFloat, /// (MetaArithmetic, float a) -> int
  48. ICastUnsigned, /// (MetaArithmetic, uint a) -> int
  49. ILogicalShiftLeft, /// (MetaArithmetic, int a, uint b) -> int
  50. ILogicalShiftRight, /// (MetaArithmetic, int a, uint b) -> int
  51. IArithmeticShiftRight, /// (MetaArithmetic, int a, uint b) -> int
  52. IBitwiseAnd, /// (MetaArithmetic, int a, int b) -> int
  53. IBitwiseOr, /// (MetaArithmetic, int a, int b) -> int
  54. IBitwiseXor, /// (MetaArithmetic, int a, int b) -> int
  55. IBitwiseNot, /// (MetaArithmetic, int a) -> int
  56. IBitfieldInsert, /// (MetaArithmetic, int base, int insert, int offset, int bits) -> int
  57. IBitfieldExtract, /// (MetaArithmetic, int value, int offset, int offset) -> int
  58. IBitCount, /// (MetaArithmetic, int) -> int
  59. UAdd, /// (MetaArithmetic, uint a, uint b) -> uint
  60. UMul, /// (MetaArithmetic, uint a, uint b) -> uint
  61. UDiv, /// (MetaArithmetic, uint a, uint b) -> uint
  62. UMin, /// (MetaArithmetic, uint a, uint b) -> uint
  63. UMax, /// (MetaArithmetic, uint a, uint b) -> uint
  64. UCastFloat, /// (MetaArithmetic, float a) -> uint
  65. UCastSigned, /// (MetaArithmetic, int a) -> uint
  66. ULogicalShiftLeft, /// (MetaArithmetic, uint a, uint b) -> uint
  67. ULogicalShiftRight, /// (MetaArithmetic, uint a, uint b) -> uint
  68. UArithmeticShiftRight, /// (MetaArithmetic, uint a, uint b) -> uint
  69. UBitwiseAnd, /// (MetaArithmetic, uint a, uint b) -> uint
  70. UBitwiseOr, /// (MetaArithmetic, uint a, uint b) -> uint
  71. UBitwiseXor, /// (MetaArithmetic, uint a, uint b) -> uint
  72. UBitwiseNot, /// (MetaArithmetic, uint a) -> uint
  73. UBitfieldInsert, /// (MetaArithmetic, uint base, uint insert, int offset, int bits) -> uint
  74. UBitfieldExtract, /// (MetaArithmetic, uint value, int offset, int offset) -> uint
  75. UBitCount, /// (MetaArithmetic, uint) -> uint
  76. HAdd, /// (MetaArithmetic, f16vec2 a, f16vec2 b) -> f16vec2
  77. HMul, /// (MetaArithmetic, f16vec2 a, f16vec2 b) -> f16vec2
  78. HFma, /// (MetaArithmetic, f16vec2 a, f16vec2 b, f16vec2 c) -> f16vec2
  79. HAbsolute, /// (f16vec2 a) -> f16vec2
  80. HNegate, /// (f16vec2 a, bool first, bool second) -> f16vec2
  81. HClamp, /// (f16vec2 src, float min, float max) -> f16vec2
  82. HUnpack, /// (Tegra::Shader::HalfType, T value) -> f16vec2
  83. HMergeF32, /// (f16vec2 src) -> float
  84. HMergeH0, /// (f16vec2 dest, f16vec2 src) -> f16vec2
  85. HMergeH1, /// (f16vec2 dest, f16vec2 src) -> f16vec2
  86. HPack2, /// (float a, float b) -> f16vec2
  87. LogicalAssign, /// (bool& dst, bool src) -> void
  88. LogicalAnd, /// (bool a, bool b) -> bool
  89. LogicalOr, /// (bool a, bool b) -> bool
  90. LogicalXor, /// (bool a, bool b) -> bool
  91. LogicalNegate, /// (bool a) -> bool
  92. LogicalPick2, /// (bool2 pair, uint index) -> bool
  93. LogicalAll2, /// (bool2 a) -> bool
  94. LogicalAny2, /// (bool2 a) -> bool
  95. LogicalFLessThan, /// (float a, float b) -> bool
  96. LogicalFEqual, /// (float a, float b) -> bool
  97. LogicalFLessEqual, /// (float a, float b) -> bool
  98. LogicalFGreaterThan, /// (float a, float b) -> bool
  99. LogicalFNotEqual, /// (float a, float b) -> bool
  100. LogicalFGreaterEqual, /// (float a, float b) -> bool
  101. LogicalFIsNan, /// (float a) -> bool
  102. LogicalILessThan, /// (int a, int b) -> bool
  103. LogicalIEqual, /// (int a, int b) -> bool
  104. LogicalILessEqual, /// (int a, int b) -> bool
  105. LogicalIGreaterThan, /// (int a, int b) -> bool
  106. LogicalINotEqual, /// (int a, int b) -> bool
  107. LogicalIGreaterEqual, /// (int a, int b) -> bool
  108. LogicalULessThan, /// (uint a, uint b) -> bool
  109. LogicalUEqual, /// (uint a, uint b) -> bool
  110. LogicalULessEqual, /// (uint a, uint b) -> bool
  111. LogicalUGreaterThan, /// (uint a, uint b) -> bool
  112. LogicalUNotEqual, /// (uint a, uint b) -> bool
  113. LogicalUGreaterEqual, /// (uint a, uint b) -> bool
  114. Logical2HLessThan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  115. Logical2HEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  116. Logical2HLessEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  117. Logical2HGreaterThan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  118. Logical2HNotEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  119. Logical2HGreaterEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  120. Logical2HLessThanWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  121. Logical2HEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  122. Logical2HLessEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  123. Logical2HGreaterThanWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  124. Logical2HNotEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  125. Logical2HGreaterEqualWithNan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  126. Texture, /// (MetaTexture, float[N] coords) -> float4
  127. TextureLod, /// (MetaTexture, float[N] coords) -> float4
  128. TextureGather, /// (MetaTexture, float[N] coords) -> float4
  129. TextureQueryDimensions, /// (MetaTexture, float a) -> float4
  130. TextureQueryLod, /// (MetaTexture, float[N] coords) -> float4
  131. TexelFetch, /// (MetaTexture, int[N], int) -> float4
  132. Branch, /// (uint branch_target) -> void
  133. PushFlowStack, /// (uint branch_target) -> void
  134. PopFlowStack, /// () -> void
  135. Exit, /// () -> void
  136. Discard, /// () -> void
  137. EmitVertex, /// () -> void
  138. EndPrimitive, /// () -> void
  139. YNegate, /// () -> float
  140. LocalInvocationIdX, /// () -> uint
  141. LocalInvocationIdY, /// () -> uint
  142. LocalInvocationIdZ, /// () -> uint
  143. WorkGroupIdX, /// () -> uint
  144. WorkGroupIdY, /// () -> uint
  145. WorkGroupIdZ, /// () -> uint
  146. Amount,
  147. };
  148. enum class InternalFlag {
  149. Zero = 0,
  150. Sign = 1,
  151. Carry = 2,
  152. Overflow = 3,
  153. Amount = 4,
  154. };
  155. enum class MetaStackClass {
  156. Ssy,
  157. Pbk,
  158. };
  159. class OperationNode;
  160. class ConditionalNode;
  161. class GprNode;
  162. class ImmediateNode;
  163. class InternalFlagNode;
  164. class PredicateNode;
  165. class AbufNode;
  166. class CbufNode;
  167. class LmemNode;
  168. class GmemNode;
  169. class CommentNode;
  170. using NodeData =
  171. std::variant<OperationNode, ConditionalNode, GprNode, ImmediateNode, InternalFlagNode,
  172. PredicateNode, AbufNode, CbufNode, LmemNode, GmemNode, CommentNode>;
  173. using Node = std::shared_ptr<NodeData>;
  174. using Node4 = std::array<Node, 4>;
  175. using NodeBlock = std::vector<Node>;
  176. class Sampler {
  177. public:
  178. /// This constructor is for bound samplers
  179. explicit Sampler(std::size_t offset, std::size_t index, Tegra::Shader::TextureType type,
  180. bool is_array, bool is_shadow)
  181. : offset{offset}, index{index}, type{type}, is_array{is_array}, is_shadow{is_shadow},
  182. is_bindless{false} {}
  183. /// This constructor is for bindless samplers
  184. explicit Sampler(u32 cbuf_index, u32 cbuf_offset, std::size_t index,
  185. Tegra::Shader::TextureType type, bool is_array, bool is_shadow)
  186. : offset{(static_cast<u64>(cbuf_index) << 32) | cbuf_offset}, index{index}, type{type},
  187. is_array{is_array}, is_shadow{is_shadow}, is_bindless{true} {}
  188. /// This constructor is for serialization/deserialization
  189. explicit Sampler(std::size_t offset, std::size_t index, Tegra::Shader::TextureType type,
  190. bool is_array, bool is_shadow, bool is_bindless)
  191. : offset{offset}, index{index}, type{type}, is_array{is_array}, is_shadow{is_shadow},
  192. is_bindless{is_bindless} {}
  193. std::size_t GetOffset() const {
  194. return offset;
  195. }
  196. std::size_t GetIndex() const {
  197. return index;
  198. }
  199. Tegra::Shader::TextureType GetType() const {
  200. return type;
  201. }
  202. bool IsArray() const {
  203. return is_array;
  204. }
  205. bool IsShadow() const {
  206. return is_shadow;
  207. }
  208. bool IsBindless() const {
  209. return is_bindless;
  210. }
  211. std::pair<u32, u32> GetBindlessCBuf() const {
  212. return {static_cast<u32>(offset >> 32), static_cast<u32>(offset)};
  213. }
  214. bool operator<(const Sampler& rhs) const {
  215. return std::tie(index, offset, type, is_array, is_shadow, is_bindless) <
  216. std::tie(rhs.index, rhs.offset, rhs.type, rhs.is_array, rhs.is_shadow,
  217. rhs.is_bindless);
  218. }
  219. private:
  220. /// Offset in TSC memory from which to read the sampler object, as specified by the sampling
  221. /// instruction.
  222. std::size_t offset{};
  223. std::size_t index{}; ///< Value used to index into the generated GLSL sampler array.
  224. Tegra::Shader::TextureType type{}; ///< The type used to sample this texture (Texture2D, etc)
  225. bool is_array{}; ///< Whether the texture is being sampled as an array texture or not.
  226. bool is_shadow{}; ///< Whether the texture is being sampled as a depth texture or not.
  227. bool is_bindless{}; ///< Whether this sampler belongs to a bindless texture or not.
  228. };
  229. struct GlobalMemoryBase {
  230. u32 cbuf_index{};
  231. u32 cbuf_offset{};
  232. bool operator<(const GlobalMemoryBase& rhs) const {
  233. return std::tie(cbuf_index, cbuf_offset) < std::tie(rhs.cbuf_index, rhs.cbuf_offset);
  234. }
  235. };
  236. /// Parameters describing an arithmetic operation
  237. struct MetaArithmetic {
  238. bool precise{}; ///< Whether the operation can be constraint or not
  239. };
  240. /// Parameters describing a texture sampler
  241. struct MetaTexture {
  242. const Sampler& sampler;
  243. Node array;
  244. Node depth_compare;
  245. std::vector<Node> aoffi;
  246. Node bias;
  247. Node lod;
  248. Node component{};
  249. u32 element{};
  250. };
  251. /// Parameters that modify an operation but are not part of any particular operand
  252. using Meta = std::variant<MetaArithmetic, MetaTexture, MetaStackClass, Tegra::Shader::HalfType>;
  253. /// Holds any kind of operation that can be done in the IR
  254. class OperationNode final {
  255. public:
  256. explicit OperationNode(OperationCode code) : OperationNode(code, Meta{}) {}
  257. explicit OperationNode(OperationCode code, Meta meta)
  258. : OperationNode(code, meta, std::vector<Node>{}) {}
  259. explicit OperationNode(OperationCode code, std::vector<Node> operands)
  260. : OperationNode(code, Meta{}, std::move(operands)) {}
  261. explicit OperationNode(OperationCode code, Meta meta, std::vector<Node> operands)
  262. : code{code}, meta{std::move(meta)}, operands{std::move(operands)} {}
  263. template <typename... Args>
  264. explicit OperationNode(OperationCode code, Meta meta, Args&&... operands)
  265. : code{code}, meta{std::move(meta)}, operands{operands...} {}
  266. OperationCode GetCode() const {
  267. return code;
  268. }
  269. const Meta& GetMeta() const {
  270. return meta;
  271. }
  272. std::size_t GetOperandsCount() const {
  273. return operands.size();
  274. }
  275. const Node& operator[](std::size_t operand_index) const {
  276. return operands.at(operand_index);
  277. }
  278. private:
  279. OperationCode code{};
  280. Meta meta{};
  281. std::vector<Node> operands;
  282. };
  283. /// Encloses inside any kind of node that returns a boolean conditionally-executed code
  284. class ConditionalNode final {
  285. public:
  286. explicit ConditionalNode(Node condition, std::vector<Node>&& code)
  287. : condition{std::move(condition)}, code{std::move(code)} {}
  288. const Node& GetCondition() const {
  289. return condition;
  290. }
  291. const std::vector<Node>& GetCode() const {
  292. return code;
  293. }
  294. private:
  295. Node condition; ///< Condition to be satisfied
  296. std::vector<Node> code; ///< Code to execute
  297. };
  298. /// A general purpose register
  299. class GprNode final {
  300. public:
  301. explicit constexpr GprNode(Tegra::Shader::Register index) : index{index} {}
  302. u32 GetIndex() const {
  303. return static_cast<u32>(index);
  304. }
  305. private:
  306. Tegra::Shader::Register index{};
  307. };
  308. /// A 32-bits value that represents an immediate value
  309. class ImmediateNode final {
  310. public:
  311. explicit constexpr ImmediateNode(u32 value) : value{value} {}
  312. u32 GetValue() const {
  313. return value;
  314. }
  315. private:
  316. u32 value{};
  317. };
  318. /// One of Maxwell's internal flags
  319. class InternalFlagNode final {
  320. public:
  321. explicit constexpr InternalFlagNode(InternalFlag flag) : flag{flag} {}
  322. InternalFlag GetFlag() const {
  323. return flag;
  324. }
  325. private:
  326. InternalFlag flag{};
  327. };
  328. /// A predicate register, it can be negated without additional nodes
  329. class PredicateNode final {
  330. public:
  331. explicit constexpr PredicateNode(Tegra::Shader::Pred index, bool negated)
  332. : index{index}, negated{negated} {}
  333. Tegra::Shader::Pred GetIndex() const {
  334. return index;
  335. }
  336. bool IsNegated() const {
  337. return negated;
  338. }
  339. private:
  340. Tegra::Shader::Pred index{};
  341. bool negated{};
  342. };
  343. /// Attribute buffer memory (known as attributes or varyings in GLSL terms)
  344. class AbufNode final {
  345. public:
  346. // Initialize for standard attributes (index is explicit).
  347. explicit AbufNode(Tegra::Shader::Attribute::Index index, u32 element, Node buffer = {})
  348. : buffer{std::move(buffer)}, index{index}, element{element} {}
  349. // Initialize for physical attributes (index is a variable value).
  350. explicit AbufNode(Node physical_address, Node buffer = {})
  351. : physical_address{std::move(physical_address)}, buffer{std::move(buffer)} {}
  352. Tegra::Shader::Attribute::Index GetIndex() const {
  353. return index;
  354. }
  355. u32 GetElement() const {
  356. return element;
  357. }
  358. const Node& GetBuffer() const {
  359. return buffer;
  360. }
  361. bool IsPhysicalBuffer() const {
  362. return static_cast<bool>(physical_address);
  363. }
  364. const Node& GetPhysicalAddress() const {
  365. return physical_address;
  366. }
  367. private:
  368. Node physical_address;
  369. Node buffer;
  370. Tegra::Shader::Attribute::Index index{};
  371. u32 element{};
  372. };
  373. /// Constant buffer node, usually mapped to uniform buffers in GLSL
  374. class CbufNode final {
  375. public:
  376. explicit CbufNode(u32 index, Node offset) : index{index}, offset{std::move(offset)} {}
  377. u32 GetIndex() const {
  378. return index;
  379. }
  380. const Node& GetOffset() const {
  381. return offset;
  382. }
  383. private:
  384. u32 index{};
  385. Node offset;
  386. };
  387. /// Local memory node
  388. class LmemNode final {
  389. public:
  390. explicit LmemNode(Node address) : address{std::move(address)} {}
  391. const Node& GetAddress() const {
  392. return address;
  393. }
  394. private:
  395. Node address;
  396. };
  397. /// Global memory node
  398. class GmemNode final {
  399. public:
  400. explicit GmemNode(Node real_address, Node base_address, const GlobalMemoryBase& descriptor)
  401. : real_address{std::move(real_address)}, base_address{std::move(base_address)},
  402. descriptor{descriptor} {}
  403. const Node& GetRealAddress() const {
  404. return real_address;
  405. }
  406. const Node& GetBaseAddress() const {
  407. return base_address;
  408. }
  409. const GlobalMemoryBase& GetDescriptor() const {
  410. return descriptor;
  411. }
  412. private:
  413. Node real_address;
  414. Node base_address;
  415. GlobalMemoryBase descriptor;
  416. };
  417. /// Commentary, can be dropped
  418. class CommentNode final {
  419. public:
  420. explicit CommentNode(std::string text) : text{std::move(text)} {}
  421. const std::string& GetText() const {
  422. return text;
  423. }
  424. private:
  425. std::string text;
  426. };
  427. } // namespace VideoCommon::Shader