shader_ir.h 30 KB

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