shader_ir.h 28 KB

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