shader_ir.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. // Copyright 2018 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 <cstring>
  7. #include <map>
  8. #include <optional>
  9. #include <set>
  10. #include <string>
  11. #include <tuple>
  12. #include <variant>
  13. #include <vector>
  14. #include "common/common_types.h"
  15. #include "video_core/engines/maxwell_3d.h"
  16. #include "video_core/engines/shader_bytecode.h"
  17. #include "video_core/engines/shader_header.h"
  18. namespace VideoCommon::Shader {
  19. class OperationNode;
  20. class ConditionalNode;
  21. class GprNode;
  22. class ImmediateNode;
  23. class InternalFlagNode;
  24. class PredicateNode;
  25. class AbufNode; ///< Attribute buffer
  26. class CbufNode; ///< Constant buffer
  27. class LmemNode; ///< Local memory
  28. class GmemNode; ///< Global memory
  29. class CommentNode;
  30. using ProgramCode = std::vector<u64>;
  31. using NodeData =
  32. std::variant<OperationNode, ConditionalNode, GprNode, ImmediateNode, InternalFlagNode,
  33. PredicateNode, AbufNode, CbufNode, LmemNode, GmemNode, CommentNode>;
  34. using Node = const NodeData*;
  35. using Node4 = std::array<Node, 4>;
  36. using NodeBlock = std::vector<Node>;
  37. constexpr u32 MAX_PROGRAM_LENGTH = 0x1000;
  38. enum class OperationCode {
  39. Assign, /// (float& dest, float src) -> void
  40. Select, /// (MetaArithmetic, bool pred, float a, float b) -> float
  41. FAdd, /// (MetaArithmetic, float a, float b) -> float
  42. FMul, /// (MetaArithmetic, float a, float b) -> float
  43. FDiv, /// (MetaArithmetic, float a, float b) -> float
  44. FFma, /// (MetaArithmetic, float a, float b, float c) -> float
  45. FNegate, /// (MetaArithmetic, float a) -> float
  46. FAbsolute, /// (MetaArithmetic, float a) -> float
  47. FClamp, /// (MetaArithmetic, float value, float min, float max) -> float
  48. FMin, /// (MetaArithmetic, float a, float b) -> float
  49. FMax, /// (MetaArithmetic, float a, float b) -> float
  50. FCos, /// (MetaArithmetic, float a) -> float
  51. FSin, /// (MetaArithmetic, float a) -> float
  52. FExp2, /// (MetaArithmetic, float a) -> float
  53. FLog2, /// (MetaArithmetic, float a) -> float
  54. FInverseSqrt, /// (MetaArithmetic, float a) -> float
  55. FSqrt, /// (MetaArithmetic, float a) -> float
  56. FRoundEven, /// (MetaArithmetic, float a) -> float
  57. FFloor, /// (MetaArithmetic, float a) -> float
  58. FCeil, /// (MetaArithmetic, float a) -> float
  59. FTrunc, /// (MetaArithmetic, float a) -> float
  60. FCastInteger, /// (MetaArithmetic, int a) -> float
  61. FCastUInteger, /// (MetaArithmetic, uint a) -> float
  62. IAdd, /// (MetaArithmetic, int a, int b) -> int
  63. IMul, /// (MetaArithmetic, int a, int b) -> int
  64. IDiv, /// (MetaArithmetic, int a, int b) -> int
  65. INegate, /// (MetaArithmetic, int a) -> int
  66. IAbsolute, /// (MetaArithmetic, int a) -> int
  67. IMin, /// (MetaArithmetic, int a, int b) -> int
  68. IMax, /// (MetaArithmetic, int a, int b) -> int
  69. ICastFloat, /// (MetaArithmetic, float a) -> int
  70. ICastUnsigned, /// (MetaArithmetic, uint a) -> int
  71. ILogicalShiftLeft, /// (MetaArithmetic, int a, uint b) -> int
  72. ILogicalShiftRight, /// (MetaArithmetic, int a, uint b) -> int
  73. IArithmeticShiftRight, /// (MetaArithmetic, int a, uint b) -> int
  74. IBitwiseAnd, /// (MetaArithmetic, int a, int b) -> int
  75. IBitwiseOr, /// (MetaArithmetic, int a, int b) -> int
  76. IBitwiseXor, /// (MetaArithmetic, int a, int b) -> int
  77. IBitwiseNot, /// (MetaArithmetic, int a) -> int
  78. IBitfieldInsert, /// (MetaArithmetic, int base, int insert, int offset, int bits) -> int
  79. IBitfieldExtract, /// (MetaArithmetic, int value, int offset, int offset) -> int
  80. IBitCount, /// (MetaArithmetic, int) -> int
  81. UAdd, /// (MetaArithmetic, uint a, uint b) -> uint
  82. UMul, /// (MetaArithmetic, uint a, uint b) -> uint
  83. UDiv, /// (MetaArithmetic, uint a, uint b) -> uint
  84. UMin, /// (MetaArithmetic, uint a, uint b) -> uint
  85. UMax, /// (MetaArithmetic, uint a, uint b) -> uint
  86. UCastFloat, /// (MetaArithmetic, float a) -> uint
  87. UCastSigned, /// (MetaArithmetic, int a) -> uint
  88. ULogicalShiftLeft, /// (MetaArithmetic, uint a, uint b) -> uint
  89. ULogicalShiftRight, /// (MetaArithmetic, uint a, uint b) -> uint
  90. UArithmeticShiftRight, /// (MetaArithmetic, uint a, uint b) -> uint
  91. UBitwiseAnd, /// (MetaArithmetic, uint a, uint b) -> uint
  92. UBitwiseOr, /// (MetaArithmetic, uint a, uint b) -> uint
  93. UBitwiseXor, /// (MetaArithmetic, uint a, uint b) -> uint
  94. UBitwiseNot, /// (MetaArithmetic, uint a) -> uint
  95. UBitfieldInsert, /// (MetaArithmetic, uint base, uint insert, int offset, int bits) -> uint
  96. UBitfieldExtract, /// (MetaArithmetic, uint value, int offset, int offset) -> uint
  97. UBitCount, /// (MetaArithmetic, uint) -> uint
  98. HAdd, /// (MetaHalfArithmetic, f16vec2 a, f16vec2 b) -> f16vec2
  99. HMul, /// (MetaHalfArithmetic, f16vec2 a, f16vec2 b) -> f16vec2
  100. HFma, /// (MetaHalfArithmetic, f16vec2 a, f16vec2 b, f16vec2 c) -> f16vec2
  101. HAbsolute, /// (f16vec2 a) -> f16vec2
  102. HNegate, /// (f16vec2 a, bool first, bool second) -> f16vec2
  103. HMergeF32, /// (f16vec2 src) -> float
  104. HMergeH0, /// (f16vec2 dest, f16vec2 src) -> f16vec2
  105. HMergeH1, /// (f16vec2 dest, f16vec2 src) -> f16vec2
  106. HPack2, /// (float a, float b) -> f16vec2
  107. LogicalAssign, /// (bool& dst, bool src) -> void
  108. LogicalAnd, /// (bool a, bool b) -> bool
  109. LogicalOr, /// (bool a, bool b) -> bool
  110. LogicalXor, /// (bool a, bool b) -> bool
  111. LogicalNegate, /// (bool a) -> bool
  112. LogicalPick2, /// (bool2 pair, uint index) -> bool
  113. LogicalAll2, /// (bool2 a) -> bool
  114. LogicalAny2, /// (bool2 a) -> bool
  115. LogicalFLessThan, /// (float a, float b) -> bool
  116. LogicalFEqual, /// (float a, float b) -> bool
  117. LogicalFLessEqual, /// (float a, float b) -> bool
  118. LogicalFGreaterThan, /// (float a, float b) -> bool
  119. LogicalFNotEqual, /// (float a, float b) -> bool
  120. LogicalFGreaterEqual, /// (float a, float b) -> bool
  121. LogicalFIsNan, /// (float a) -> bool
  122. LogicalILessThan, /// (int a, int b) -> bool
  123. LogicalIEqual, /// (int a, int b) -> bool
  124. LogicalILessEqual, /// (int a, int b) -> bool
  125. LogicalIGreaterThan, /// (int a, int b) -> bool
  126. LogicalINotEqual, /// (int a, int b) -> bool
  127. LogicalIGreaterEqual, /// (int a, int b) -> bool
  128. LogicalULessThan, /// (uint a, uint b) -> bool
  129. LogicalUEqual, /// (uint a, uint b) -> bool
  130. LogicalULessEqual, /// (uint a, uint b) -> bool
  131. LogicalUGreaterThan, /// (uint a, uint b) -> bool
  132. LogicalUNotEqual, /// (uint a, uint b) -> bool
  133. LogicalUGreaterEqual, /// (uint a, uint b) -> bool
  134. Logical2HLessThan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  135. Logical2HEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  136. Logical2HLessEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  137. Logical2HGreaterThan, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  138. Logical2HNotEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  139. Logical2HGreaterEqual, /// (MetaHalfArithmetic, f16vec2 a, f16vec2) -> bool2
  140. Texture, /// (MetaTexture, float[N] coords) -> float4
  141. TextureLod, /// (MetaTexture, float[N] coords) -> float4
  142. TextureGather, /// (MetaTexture, float[N] coords) -> float4
  143. TextureQueryDimensions, /// (MetaTexture, float a) -> float4
  144. TextureQueryLod, /// (MetaTexture, float[N] coords) -> float4
  145. TexelFetch, /// (MetaTexture, int[N], int) -> float4
  146. Branch, /// (uint branch_target) -> void
  147. PushFlowStack, /// (uint branch_target) -> void
  148. PopFlowStack, /// () -> void
  149. Exit, /// () -> void
  150. Discard, /// () -> void
  151. EmitVertex, /// () -> void
  152. EndPrimitive, /// () -> void
  153. YNegate, /// () -> float
  154. Amount,
  155. };
  156. enum class InternalFlag {
  157. Zero = 0,
  158. Sign = 1,
  159. Carry = 2,
  160. Overflow = 3,
  161. Amount = 4,
  162. };
  163. /// Describes the behaviour of code path of a given entry point and a return point.
  164. enum class ExitMethod {
  165. Undetermined, ///< Internal value. Only occur when analyzing JMP loop.
  166. AlwaysReturn, ///< All code paths reach the return point.
  167. Conditional, ///< Code path reaches the return point or an END instruction conditionally.
  168. AlwaysEnd, ///< All code paths reach a END instruction.
  169. };
  170. class Sampler {
  171. public:
  172. explicit Sampler(std::size_t offset, std::size_t index, Tegra::Shader::TextureType type,
  173. bool is_array, bool is_shadow)
  174. : offset{offset}, index{index}, type{type}, is_array{is_array}, is_shadow{is_shadow} {}
  175. std::size_t GetOffset() const {
  176. return offset;
  177. }
  178. std::size_t GetIndex() const {
  179. return index;
  180. }
  181. Tegra::Shader::TextureType GetType() const {
  182. return type;
  183. }
  184. bool IsArray() const {
  185. return is_array;
  186. }
  187. bool IsShadow() const {
  188. return is_shadow;
  189. }
  190. bool operator<(const Sampler& rhs) const {
  191. return std::tie(offset, index, type, is_array, is_shadow) <
  192. std::tie(rhs.offset, rhs.index, rhs.type, rhs.is_array, rhs.is_shadow);
  193. }
  194. private:
  195. /// Offset in TSC memory from which to read the sampler object, as specified by the sampling
  196. /// instruction.
  197. std::size_t offset{};
  198. std::size_t index{}; ///< Value used to index into the generated GLSL sampler array.
  199. Tegra::Shader::TextureType type{}; ///< The type used to sample this texture (Texture2D, etc)
  200. bool is_array{}; ///< Whether the texture is being sampled as an array texture or not.
  201. bool is_shadow{}; ///< Whether the texture is being sampled as a depth texture or not.
  202. };
  203. class ConstBuffer {
  204. public:
  205. explicit ConstBuffer(u32 max_offset, bool is_indirect)
  206. : max_offset{max_offset}, is_indirect{is_indirect} {}
  207. ConstBuffer() = default;
  208. void MarkAsUsed(u64 offset) {
  209. max_offset = std::max(max_offset, static_cast<u32>(offset));
  210. }
  211. void MarkAsUsedIndirect() {
  212. is_indirect = true;
  213. }
  214. bool IsIndirect() const {
  215. return is_indirect;
  216. }
  217. u32 GetSize() const {
  218. return max_offset + sizeof(float);
  219. }
  220. u32 GetMaxOffset() const {
  221. return max_offset;
  222. }
  223. private:
  224. u32 max_offset{};
  225. bool is_indirect{};
  226. };
  227. struct GlobalMemoryBase {
  228. u32 cbuf_index{};
  229. u32 cbuf_offset{};
  230. bool operator<(const GlobalMemoryBase& rhs) const {
  231. return std::tie(cbuf_index, cbuf_offset) < std::tie(rhs.cbuf_index, rhs.cbuf_offset);
  232. }
  233. };
  234. struct MetaArithmetic {
  235. bool precise{};
  236. };
  237. struct MetaHalfArithmetic {
  238. bool precise{};
  239. std::array<Tegra::Shader::HalfType, 3> types = {Tegra::Shader::HalfType::H0_H1,
  240. Tegra::Shader::HalfType::H0_H1,
  241. Tegra::Shader::HalfType::H0_H1};
  242. };
  243. struct MetaTexture {
  244. const Sampler& sampler;
  245. Node array{};
  246. Node depth_compare{};
  247. std::vector<Node> aoffi;
  248. Node bias{};
  249. Node lod{};
  250. Node component{};
  251. u32 element{};
  252. };
  253. constexpr MetaArithmetic PRECISE = {true};
  254. constexpr MetaArithmetic NO_PRECISE = {false};
  255. constexpr MetaHalfArithmetic HALF_NO_PRECISE = {false};
  256. using Meta = std::variant<MetaArithmetic, MetaHalfArithmetic, MetaTexture>;
  257. /// Holds any kind of operation that can be done in the IR
  258. class OperationNode final {
  259. public:
  260. template <typename... T>
  261. explicit constexpr OperationNode(OperationCode code) : code{code}, meta{} {}
  262. template <typename... T>
  263. explicit constexpr OperationNode(OperationCode code, Meta&& meta)
  264. : code{code}, meta{std::move(meta)} {}
  265. template <typename... T>
  266. explicit constexpr OperationNode(OperationCode code, const T*... operands)
  267. : OperationNode(code, {}, operands...) {}
  268. template <typename... T>
  269. explicit constexpr OperationNode(OperationCode code, Meta&& meta, const T*... operands_)
  270. : code{code}, meta{std::move(meta)} {
  271. auto operands_list = {operands_...};
  272. for (auto& operand : operands_list) {
  273. operands.push_back(operand);
  274. }
  275. }
  276. explicit OperationNode(OperationCode code, Meta&& meta, std::vector<Node>&& operands)
  277. : code{code}, meta{meta}, operands{std::move(operands)} {}
  278. explicit OperationNode(OperationCode code, std::vector<Node>&& operands)
  279. : code{code}, meta{}, operands{std::move(operands)} {}
  280. OperationCode GetCode() const {
  281. return code;
  282. }
  283. const Meta& GetMeta() const {
  284. return meta;
  285. }
  286. std::size_t GetOperandsCount() const {
  287. return operands.size();
  288. }
  289. Node operator[](std::size_t operand_index) const {
  290. return operands.at(operand_index);
  291. }
  292. private:
  293. const OperationCode code;
  294. const Meta meta;
  295. std::vector<Node> operands;
  296. };
  297. /// Encloses inside any kind of node that returns a boolean conditionally-executed code
  298. class ConditionalNode final {
  299. public:
  300. explicit ConditionalNode(Node condition, std::vector<Node>&& code)
  301. : condition{condition}, code{std::move(code)} {}
  302. Node GetCondition() const {
  303. return condition;
  304. }
  305. const std::vector<Node>& GetCode() const {
  306. return code;
  307. }
  308. private:
  309. const Node condition; ///< Condition to be satisfied
  310. std::vector<Node> code; ///< Code to execute
  311. };
  312. /// A general purpose register
  313. class GprNode final {
  314. public:
  315. explicit constexpr GprNode(Tegra::Shader::Register index) : index{index} {}
  316. u32 GetIndex() const {
  317. return static_cast<u32>(index);
  318. }
  319. private:
  320. const Tegra::Shader::Register index;
  321. };
  322. /// A 32-bits value that represents an immediate value
  323. class ImmediateNode final {
  324. public:
  325. explicit constexpr ImmediateNode(u32 value) : value{value} {}
  326. u32 GetValue() const {
  327. return value;
  328. }
  329. private:
  330. const u32 value;
  331. };
  332. /// One of Maxwell's internal flags
  333. class InternalFlagNode final {
  334. public:
  335. explicit constexpr InternalFlagNode(InternalFlag flag) : flag{flag} {}
  336. InternalFlag GetFlag() const {
  337. return flag;
  338. }
  339. private:
  340. const InternalFlag flag;
  341. };
  342. /// A predicate register, it can be negated without additional nodes
  343. class PredicateNode final {
  344. public:
  345. explicit constexpr PredicateNode(Tegra::Shader::Pred index, bool negated)
  346. : index{index}, negated{negated} {}
  347. Tegra::Shader::Pred GetIndex() const {
  348. return index;
  349. }
  350. bool IsNegated() const {
  351. return negated;
  352. }
  353. private:
  354. const Tegra::Shader::Pred index;
  355. const bool negated;
  356. };
  357. /// Attribute buffer memory (known as attributes or varyings in GLSL terms)
  358. class AbufNode final {
  359. public:
  360. explicit constexpr AbufNode(Tegra::Shader::Attribute::Index index, u32 element,
  361. const Tegra::Shader::IpaMode& input_mode, Node buffer = {})
  362. : input_mode{input_mode}, buffer{buffer}, index{index}, element{element} {}
  363. explicit constexpr AbufNode(Tegra::Shader::Attribute::Index index, u32 element,
  364. Node buffer = {})
  365. : input_mode{}, buffer{buffer}, index{index}, element{element} {}
  366. Tegra::Shader::IpaMode GetInputMode() const {
  367. return input_mode;
  368. }
  369. Tegra::Shader::Attribute::Index GetIndex() const {
  370. return index;
  371. }
  372. u32 GetElement() const {
  373. return element;
  374. }
  375. Node GetBuffer() const {
  376. return buffer;
  377. }
  378. private:
  379. const Tegra::Shader::IpaMode input_mode;
  380. const Node buffer;
  381. const Tegra::Shader::Attribute::Index index;
  382. const u32 element;
  383. };
  384. /// Constant buffer node, usually mapped to uniform buffers in GLSL
  385. class CbufNode final {
  386. public:
  387. explicit constexpr CbufNode(u32 index, Node offset) : index{index}, offset{offset} {}
  388. u32 GetIndex() const {
  389. return index;
  390. }
  391. Node GetOffset() const {
  392. return offset;
  393. }
  394. private:
  395. const u32 index;
  396. const Node offset;
  397. };
  398. /// Local memory node
  399. class LmemNode final {
  400. public:
  401. explicit constexpr LmemNode(Node address) : address{address} {}
  402. Node GetAddress() const {
  403. return address;
  404. }
  405. private:
  406. const Node address;
  407. };
  408. /// Global memory node
  409. class GmemNode final {
  410. public:
  411. explicit constexpr GmemNode(Node real_address, Node base_address,
  412. const GlobalMemoryBase& descriptor)
  413. : real_address{real_address}, base_address{base_address}, descriptor{descriptor} {}
  414. Node GetRealAddress() const {
  415. return real_address;
  416. }
  417. Node GetBaseAddress() const {
  418. return base_address;
  419. }
  420. const GlobalMemoryBase& GetDescriptor() const {
  421. return descriptor;
  422. }
  423. private:
  424. const Node real_address;
  425. const Node base_address;
  426. const GlobalMemoryBase descriptor;
  427. };
  428. /// Commentary, can be dropped
  429. class CommentNode final {
  430. public:
  431. explicit CommentNode(std::string text) : text{std::move(text)} {}
  432. const std::string& GetText() const {
  433. return text;
  434. }
  435. private:
  436. std::string text;
  437. };
  438. class ShaderIR final {
  439. public:
  440. explicit ShaderIR(const ProgramCode& program_code, u32 main_offset)
  441. : program_code{program_code}, main_offset{main_offset} {
  442. Decode();
  443. }
  444. const std::map<u32, NodeBlock>& GetBasicBlocks() const {
  445. return basic_blocks;
  446. }
  447. const std::set<u32>& GetRegisters() const {
  448. return used_registers;
  449. }
  450. const std::set<Tegra::Shader::Pred>& GetPredicates() const {
  451. return used_predicates;
  452. }
  453. const std::map<Tegra::Shader::Attribute::Index, std::set<Tegra::Shader::IpaMode>>&
  454. GetInputAttributes() const {
  455. return used_input_attributes;
  456. }
  457. const std::set<Tegra::Shader::Attribute::Index>& GetOutputAttributes() const {
  458. return used_output_attributes;
  459. }
  460. const std::map<u32, ConstBuffer>& GetConstantBuffers() const {
  461. return used_cbufs;
  462. }
  463. const std::set<Sampler>& GetSamplers() const {
  464. return used_samplers;
  465. }
  466. const std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances>& GetClipDistances()
  467. const {
  468. return used_clip_distances;
  469. }
  470. const std::set<GlobalMemoryBase>& GetGlobalMemoryBases() const {
  471. return used_global_memory_bases;
  472. }
  473. std::size_t GetLength() const {
  474. return static_cast<std::size_t>(coverage_end * sizeof(u64));
  475. }
  476. const Tegra::Shader::Header& GetHeader() const {
  477. return header;
  478. }
  479. private:
  480. void Decode();
  481. ExitMethod Scan(u32 begin, u32 end, std::set<u32>& labels);
  482. NodeBlock DecodeRange(u32 begin, u32 end);
  483. /**
  484. * Decodes a single instruction from Tegra to IR.
  485. * @param bb Basic block where the nodes will be written to.
  486. * @param pc Program counter. Offset to decode.
  487. * @return Next address to decode.
  488. */
  489. u32 DecodeInstr(NodeBlock& bb, u32 pc);
  490. u32 DecodeArithmetic(NodeBlock& bb, u32 pc);
  491. u32 DecodeArithmeticImmediate(NodeBlock& bb, u32 pc);
  492. u32 DecodeBfe(NodeBlock& bb, u32 pc);
  493. u32 DecodeBfi(NodeBlock& bb, u32 pc);
  494. u32 DecodeShift(NodeBlock& bb, u32 pc);
  495. u32 DecodeArithmeticInteger(NodeBlock& bb, u32 pc);
  496. u32 DecodeArithmeticIntegerImmediate(NodeBlock& bb, u32 pc);
  497. u32 DecodeArithmeticHalf(NodeBlock& bb, u32 pc);
  498. u32 DecodeArithmeticHalfImmediate(NodeBlock& bb, u32 pc);
  499. u32 DecodeFfma(NodeBlock& bb, u32 pc);
  500. u32 DecodeHfma2(NodeBlock& bb, u32 pc);
  501. u32 DecodeConversion(NodeBlock& bb, u32 pc);
  502. u32 DecodeMemory(NodeBlock& bb, u32 pc);
  503. u32 DecodeTexture(NodeBlock& bb, u32 pc);
  504. u32 DecodeFloatSetPredicate(NodeBlock& bb, u32 pc);
  505. u32 DecodeIntegerSetPredicate(NodeBlock& bb, u32 pc);
  506. u32 DecodeHalfSetPredicate(NodeBlock& bb, u32 pc);
  507. u32 DecodePredicateSetRegister(NodeBlock& bb, u32 pc);
  508. u32 DecodePredicateSetPredicate(NodeBlock& bb, u32 pc);
  509. u32 DecodeRegisterSetPredicate(NodeBlock& bb, u32 pc);
  510. u32 DecodeFloatSet(NodeBlock& bb, u32 pc);
  511. u32 DecodeIntegerSet(NodeBlock& bb, u32 pc);
  512. u32 DecodeHalfSet(NodeBlock& bb, u32 pc);
  513. u32 DecodeVideo(NodeBlock& bb, u32 pc);
  514. u32 DecodeXmad(NodeBlock& bb, u32 pc);
  515. u32 DecodeOther(NodeBlock& bb, u32 pc);
  516. /// Internalizes node's data and returns a managed pointer to a clone of that node
  517. Node StoreNode(NodeData&& node_data);
  518. /// Creates a conditional node
  519. Node Conditional(Node condition, std::vector<Node>&& code);
  520. /// Creates a commentary
  521. Node Comment(const std::string& text);
  522. /// Creates an u32 immediate
  523. Node Immediate(u32 value);
  524. /// Creates a s32 immediate
  525. Node Immediate(s32 value) {
  526. return Immediate(static_cast<u32>(value));
  527. }
  528. /// Creates a f32 immediate
  529. Node Immediate(f32 value) {
  530. u32 integral;
  531. std::memcpy(&integral, &value, sizeof(u32));
  532. return Immediate(integral);
  533. }
  534. /// Generates a node for a passed register.
  535. Node GetRegister(Tegra::Shader::Register reg);
  536. /// Generates a node representing a 19-bit immediate value
  537. Node GetImmediate19(Tegra::Shader::Instruction instr);
  538. /// Generates a node representing a 32-bit immediate value
  539. Node GetImmediate32(Tegra::Shader::Instruction instr);
  540. /// Generates a node representing a constant buffer
  541. Node GetConstBuffer(u64 index, u64 offset);
  542. /// Generates a node representing a constant buffer with a variadic offset
  543. Node GetConstBufferIndirect(u64 index, u64 offset, Node node);
  544. /// Generates a node for a passed predicate. It can be optionally negated
  545. Node GetPredicate(u64 pred, bool negated = false);
  546. /// Generates a predicate node for an immediate true or false value
  547. Node GetPredicate(bool immediate);
  548. /// Generates a node representing an input attribute. Keeps track of used attributes.
  549. Node GetInputAttribute(Tegra::Shader::Attribute::Index index, u64 element,
  550. const Tegra::Shader::IpaMode& input_mode, Node buffer = {});
  551. /// Generates a node representing an output attribute. Keeps track of used attributes.
  552. Node GetOutputAttribute(Tegra::Shader::Attribute::Index index, u64 element, Node buffer);
  553. /// Generates a node representing an internal flag
  554. Node GetInternalFlag(InternalFlag flag, bool negated = false);
  555. /// Generates a node representing a local memory address
  556. Node GetLocalMemory(Node address);
  557. /// Generates a temporal, internally it uses a post-RZ register
  558. Node GetTemporal(u32 id);
  559. /// Sets a register. src value must be a number-evaluated node.
  560. void SetRegister(NodeBlock& bb, Tegra::Shader::Register dest, Node src);
  561. /// Sets a predicate. src value must be a bool-evaluated node
  562. void SetPredicate(NodeBlock& bb, u64 dest, Node src);
  563. /// Sets an internal flag. src value must be a bool-evaluated node
  564. void SetInternalFlag(NodeBlock& bb, InternalFlag flag, Node value);
  565. /// Sets a local memory address. address and value must be a number-evaluated node
  566. void SetLocalMemory(NodeBlock& bb, Node address, Node value);
  567. /// Sets a temporal. Internally it uses a post-RZ register
  568. void SetTemporal(NodeBlock& bb, u32 id, Node value);
  569. /// Sets internal flags from a float
  570. void SetInternalFlagsFromFloat(NodeBlock& bb, Node value, bool sets_cc = true);
  571. /// Sets internal flags from an integer
  572. void SetInternalFlagsFromInteger(NodeBlock& bb, Node value, bool sets_cc = true);
  573. /// Conditionally absolute/negated float. Absolute is applied first
  574. Node GetOperandAbsNegFloat(Node value, bool absolute, bool negate);
  575. /// Conditionally saturates a float
  576. Node GetSaturatedFloat(Node value, bool saturate = true);
  577. /// Converts an integer to different sizes.
  578. Node ConvertIntegerSize(Node value, Tegra::Shader::Register::Size size, bool is_signed);
  579. /// Conditionally absolute/negated integer. Absolute is applied first
  580. Node GetOperandAbsNegInteger(Node value, bool absolute, bool negate, bool is_signed);
  581. /// Unpacks a half immediate from an instruction
  582. Node UnpackHalfImmediate(Tegra::Shader::Instruction instr, bool has_negation);
  583. /// Merges a half pair into another value
  584. Node HalfMerge(Node dest, Node src, Tegra::Shader::HalfMerge merge);
  585. /// Conditionally absolute/negated half float pair. Absolute is applied first
  586. Node GetOperandAbsNegHalf(Node value, bool absolute, bool negate);
  587. /// Returns a predicate comparing two floats
  588. Node GetPredicateComparisonFloat(Tegra::Shader::PredCondition condition, Node op_a, Node op_b);
  589. /// Returns a predicate comparing two integers
  590. Node GetPredicateComparisonInteger(Tegra::Shader::PredCondition condition, bool is_signed,
  591. Node op_a, Node op_b);
  592. /// Returns a predicate comparing two half floats. meta consumes how both pairs will be compared
  593. Node GetPredicateComparisonHalf(Tegra::Shader::PredCondition condition,
  594. const MetaHalfArithmetic& meta, Node op_a, Node op_b);
  595. /// Returns a predicate combiner operation
  596. OperationCode GetPredicateCombiner(Tegra::Shader::PredOperation operation);
  597. /// Returns a condition code evaluated from internal flags
  598. Node GetConditionCode(Tegra::Shader::ConditionCode cc);
  599. /// Accesses a texture sampler
  600. const Sampler& GetSampler(const Tegra::Shader::Sampler& sampler,
  601. Tegra::Shader::TextureType type, bool is_array, bool is_shadow);
  602. /// Extracts a sequence of bits from a node
  603. Node BitfieldExtract(Node value, u32 offset, u32 bits);
  604. void WriteTexInstructionFloat(NodeBlock& bb, Tegra::Shader::Instruction instr,
  605. const Node4& components);
  606. void WriteTexsInstructionFloat(NodeBlock& bb, Tegra::Shader::Instruction instr,
  607. const Node4& components);
  608. void WriteTexsInstructionHalfFloat(NodeBlock& bb, Tegra::Shader::Instruction instr,
  609. const Node4& components);
  610. Node4 GetTexCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  611. Tegra::Shader::TextureProcessMode process_mode, bool depth_compare,
  612. bool is_array, bool is_aoffi);
  613. Node4 GetTexsCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  614. Tegra::Shader::TextureProcessMode process_mode, bool depth_compare,
  615. bool is_array);
  616. Node4 GetTld4Code(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  617. bool depth_compare, bool is_array, bool is_aoffi);
  618. Node4 GetTldsCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  619. bool is_array);
  620. std::tuple<std::size_t, std::size_t> ValidateAndGetCoordinateElement(
  621. Tegra::Shader::TextureType texture_type, bool depth_compare, bool is_array,
  622. bool lod_bias_enabled, std::size_t max_coords, std::size_t max_inputs);
  623. std::vector<Node> GetAoffiCoordinates(Node aoffi_reg, std::size_t coord_count, bool is_tld4);
  624. Node4 GetTextureCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  625. Tegra::Shader::TextureProcessMode process_mode, std::vector<Node> coords,
  626. Node array, Node depth_compare, u32 bias_offset, std::vector<Node> aoffi);
  627. Node GetVideoOperand(Node op, bool is_chunk, bool is_signed, Tegra::Shader::VideoType type,
  628. u64 byte_height);
  629. void WriteLogicOperation(NodeBlock& bb, Tegra::Shader::Register dest,
  630. Tegra::Shader::LogicOperation logic_op, Node op_a, Node op_b,
  631. Tegra::Shader::PredicateResultMode predicate_mode,
  632. Tegra::Shader::Pred predicate, bool sets_cc);
  633. void WriteLop3Instruction(NodeBlock& bb, Tegra::Shader::Register dest, Node op_a, Node op_b,
  634. Node op_c, Node imm_lut, bool sets_cc);
  635. Node TrackCbuf(Node tracked, const NodeBlock& code, s64 cursor);
  636. std::optional<u32> TrackImmediate(Node tracked, const NodeBlock& code, s64 cursor);
  637. std::pair<Node, s64> TrackRegister(const GprNode* tracked, const NodeBlock& code, s64 cursor);
  638. template <typename... T>
  639. Node Operation(OperationCode code, const T*... operands) {
  640. return StoreNode(OperationNode(code, operands...));
  641. }
  642. template <typename... T>
  643. Node Operation(OperationCode code, Meta&& meta, const T*... operands) {
  644. return StoreNode(OperationNode(code, std::move(meta), operands...));
  645. }
  646. template <typename... T>
  647. Node Operation(OperationCode code, std::vector<Node>&& operands) {
  648. return StoreNode(OperationNode(code, std::move(operands)));
  649. }
  650. template <typename... T>
  651. Node Operation(OperationCode code, Meta&& meta, std::vector<Node>&& operands) {
  652. return StoreNode(OperationNode(code, std::move(meta), std::move(operands)));
  653. }
  654. template <typename... T>
  655. Node SignedOperation(OperationCode code, bool is_signed, const T*... operands) {
  656. return StoreNode(OperationNode(SignedToUnsignedCode(code, is_signed), operands...));
  657. }
  658. template <typename... T>
  659. Node SignedOperation(OperationCode code, bool is_signed, Meta&& meta, const T*... operands) {
  660. return StoreNode(
  661. OperationNode(SignedToUnsignedCode(code, is_signed), std::move(meta), operands...));
  662. }
  663. static OperationCode SignedToUnsignedCode(OperationCode operation_code, bool is_signed);
  664. const ProgramCode& program_code;
  665. const u32 main_offset;
  666. u32 coverage_begin{};
  667. u32 coverage_end{};
  668. std::map<std::pair<u32, u32>, ExitMethod> exit_method_map;
  669. std::map<u32, NodeBlock> basic_blocks;
  670. NodeBlock global_code;
  671. std::vector<std::unique_ptr<NodeData>> stored_nodes;
  672. std::set<u32> used_registers;
  673. std::set<Tegra::Shader::Pred> used_predicates;
  674. std::map<Tegra::Shader::Attribute::Index, std::set<Tegra::Shader::IpaMode>>
  675. used_input_attributes;
  676. std::set<Tegra::Shader::Attribute::Index> used_output_attributes;
  677. std::map<u32, ConstBuffer> used_cbufs;
  678. std::set<Sampler> used_samplers;
  679. std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances> used_clip_distances{};
  680. std::set<GlobalMemoryBase> used_global_memory_bases;
  681. Tegra::Shader::Header header;
  682. };
  683. } // namespace VideoCommon::Shader