shader_ir.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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. enum class OperationCode {
  36. Assign, /// (float& dest, float src) -> void
  37. AssignComposite, /// (MetaComponents, float4 src, float&[4] dst) -> void
  38. AssignCompositeHalf, /// (MetaComponents, float4 src, float&[2] 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. 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. 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 + 1;
  215. }
  216. private:
  217. u32 max_offset{};
  218. bool is_indirect{};
  219. };
  220. struct MetaArithmetic {
  221. bool precise{};
  222. };
  223. struct MetaHalfArithmetic {
  224. bool precise{};
  225. std::array<Tegra::Shader::HalfType, 3> types = {Tegra::Shader::HalfType::H0_H1,
  226. Tegra::Shader::HalfType::H0_H1,
  227. Tegra::Shader::HalfType::H0_H1};
  228. };
  229. struct MetaTexture {
  230. const Sampler& sampler;
  231. u32 coords_count{};
  232. std::optional<u32> array_index;
  233. };
  234. struct MetaComponents {
  235. std::array<u32, 4> components_map{};
  236. u32 count{};
  237. u32 GetSourceComponent(u32 dest_index) const {
  238. return components_map[dest_index];
  239. }
  240. };
  241. constexpr MetaArithmetic PRECISE = {true};
  242. constexpr MetaArithmetic NO_PRECISE = {false};
  243. constexpr MetaHalfArithmetic HALF_NO_PRECISE = {false};
  244. using Meta = std::variant<MetaArithmetic, MetaHalfArithmetic, MetaTexture, MetaComponents>;
  245. /// Holds any kind of operation that can be done in the IR
  246. class OperationNode final {
  247. public:
  248. template <typename... T>
  249. explicit constexpr OperationNode(OperationCode code) : code{code}, meta{} {}
  250. template <typename... T>
  251. explicit constexpr OperationNode(OperationCode code, Meta&& meta)
  252. : code{code}, meta{std::move(meta)} {}
  253. template <typename... T>
  254. explicit constexpr OperationNode(OperationCode code, const T*... operands)
  255. : OperationNode(code, {}, operands...) {}
  256. template <typename... T>
  257. explicit constexpr OperationNode(OperationCode code, Meta&& meta, const T*... operands_)
  258. : code{code}, meta{std::move(meta)} {
  259. auto operands_list = {operands_...};
  260. for (auto& operand : operands_list) {
  261. operands.push_back(operand);
  262. }
  263. }
  264. explicit OperationNode(OperationCode code, Meta&& meta, std::vector<Node>&& operands)
  265. : code{code}, meta{meta}, operands{std::move(operands)} {}
  266. explicit OperationNode(OperationCode code, std::vector<Node>&& operands)
  267. : code{code}, meta{}, operands{std::move(operands)} {}
  268. OperationCode GetCode() const {
  269. return code;
  270. }
  271. const Meta& GetMeta() const {
  272. return meta;
  273. }
  274. std::size_t GetOperandsCount() const {
  275. return operands.size();
  276. }
  277. Node operator[](std::size_t operand_index) const {
  278. return operands.at(operand_index);
  279. }
  280. private:
  281. const OperationCode code;
  282. const Meta meta;
  283. std::vector<Node> operands;
  284. };
  285. /// Encloses inside any kind of node that returns a boolean conditionally-executed code
  286. class ConditionalNode final {
  287. public:
  288. explicit ConditionalNode(Node condition, std::vector<Node>&& code)
  289. : condition{condition}, code{std::move(code)} {}
  290. Node GetCondition() const {
  291. return condition;
  292. }
  293. const std::vector<Node>& GetCode() const {
  294. return code;
  295. }
  296. private:
  297. const Node condition; ///< Condition to be satisfied
  298. std::vector<Node> code; ///< Code to execute
  299. };
  300. /// A general purpose register
  301. class GprNode final {
  302. public:
  303. explicit constexpr GprNode(Tegra::Shader::Register index) : index{index} {}
  304. u32 GetIndex() const {
  305. return static_cast<u32>(index);
  306. }
  307. private:
  308. const Tegra::Shader::Register index;
  309. };
  310. /// A 32-bits value that represents an immediate value
  311. class ImmediateNode final {
  312. public:
  313. explicit constexpr ImmediateNode(u32 value) : value{value} {}
  314. u32 GetValue() const {
  315. return value;
  316. }
  317. private:
  318. const u32 value;
  319. };
  320. /// One of Maxwell's internal flags
  321. class InternalFlagNode final {
  322. public:
  323. explicit constexpr InternalFlagNode(InternalFlag flag) : flag{flag} {}
  324. InternalFlag GetFlag() const {
  325. return flag;
  326. }
  327. private:
  328. const InternalFlag flag;
  329. };
  330. /// A predicate register, it can be negated without aditional nodes
  331. class PredicateNode final {
  332. public:
  333. explicit constexpr PredicateNode(Tegra::Shader::Pred index, bool negated)
  334. : index{index}, negated{negated} {}
  335. Tegra::Shader::Pred GetIndex() const {
  336. return index;
  337. }
  338. bool IsNegated() const {
  339. return negated;
  340. }
  341. private:
  342. const Tegra::Shader::Pred index;
  343. const bool negated;
  344. };
  345. /// Attribute buffer memory (known as attributes or varyings in GLSL terms)
  346. class AbufNode final {
  347. public:
  348. explicit constexpr AbufNode(Tegra::Shader::Attribute::Index index, u32 element,
  349. const Tegra::Shader::IpaMode& input_mode, Node buffer = {})
  350. : input_mode{input_mode}, index{index}, element{element}, buffer{buffer} {}
  351. explicit constexpr AbufNode(Tegra::Shader::Attribute::Index index, u32 element,
  352. Node buffer = {})
  353. : input_mode{}, index{index}, element{element}, buffer{buffer} {}
  354. Tegra::Shader::IpaMode GetInputMode() const {
  355. return input_mode;
  356. }
  357. Tegra::Shader::Attribute::Index GetIndex() const {
  358. return index;
  359. }
  360. u32 GetElement() const {
  361. return element;
  362. }
  363. Node GetBuffer() const {
  364. return buffer;
  365. }
  366. private:
  367. const Tegra::Shader::IpaMode input_mode;
  368. const Node buffer;
  369. const Tegra::Shader::Attribute::Index index;
  370. const u32 element;
  371. };
  372. /// Constant buffer node, usually mapped to uniform buffers in GLSL
  373. class CbufNode final {
  374. public:
  375. explicit constexpr CbufNode(u32 index, Node offset) : index{index}, offset{offset} {}
  376. u32 GetIndex() const {
  377. return index;
  378. }
  379. Node GetOffset() const {
  380. return offset;
  381. }
  382. private:
  383. const u32 index;
  384. const Node offset;
  385. };
  386. /// Local memory node
  387. class LmemNode final {
  388. public:
  389. explicit constexpr LmemNode(Node address) : address{address} {}
  390. Node GetAddress() const {
  391. return address;
  392. }
  393. private:
  394. const Node address;
  395. };
  396. /// Global memory node
  397. class GmemNode final {
  398. public:
  399. explicit constexpr GmemNode(Node address) : address{address} {}
  400. Node GetAddress() const {
  401. return address;
  402. }
  403. private:
  404. const Node address;
  405. };
  406. /// Commentary, can be dropped
  407. class CommentNode final {
  408. public:
  409. explicit CommentNode(std::string text) : text{std::move(text)} {}
  410. const std::string& GetText() const {
  411. return text;
  412. }
  413. private:
  414. std::string text;
  415. };
  416. class ShaderIR final {
  417. public:
  418. explicit ShaderIR(const ProgramCode& program_code, u32 main_offset)
  419. : program_code{program_code}, main_offset{main_offset} {
  420. Decode();
  421. }
  422. const std::map<u32, BasicBlock>& GetBasicBlocks() const {
  423. return basic_blocks;
  424. }
  425. const std::set<u32>& GetRegisters() const {
  426. return used_registers;
  427. }
  428. const std::set<Tegra::Shader::Pred>& GetPredicates() const {
  429. return used_predicates;
  430. }
  431. const std::map<Tegra::Shader::Attribute::Index, std::set<Tegra::Shader::IpaMode>>&
  432. GetInputAttributes() const {
  433. return used_input_attributes;
  434. }
  435. const std::set<Tegra::Shader::Attribute::Index>& GetOutputAttributes() const {
  436. return used_output_attributes;
  437. }
  438. const std::map<u32, ConstBuffer>& GetConstantBuffers() const {
  439. return used_cbufs;
  440. }
  441. const std::set<Sampler>& GetSamplers() const {
  442. return used_samplers;
  443. }
  444. const std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances>& GetClipDistances()
  445. const {
  446. return used_clip_distances;
  447. }
  448. std::size_t GetLength() const {
  449. return static_cast<std::size_t>(coverage_end * sizeof(u64));
  450. }
  451. const Tegra::Shader::Header& GetHeader() const {
  452. return header;
  453. }
  454. private:
  455. void Decode();
  456. ExitMethod Scan(u32 begin, u32 end, std::set<u32>& labels);
  457. BasicBlock DecodeRange(u32 begin, u32 end);
  458. /**
  459. * Decodes a single instruction from Tegra to IR.
  460. * @param bb Basic block where the nodes will be written to.
  461. * @param pc Program counter. Offset to decode.
  462. * @return Next address to decode.
  463. */
  464. u32 DecodeInstr(BasicBlock& bb, u32 pc);
  465. u32 DecodeArithmetic(BasicBlock& bb, u32 pc);
  466. u32 DecodeArithmeticImmediate(BasicBlock& bb, u32 pc);
  467. u32 DecodeBfe(BasicBlock& bb, u32 pc);
  468. u32 DecodeBfi(BasicBlock& bb, u32 pc);
  469. u32 DecodeShift(BasicBlock& bb, u32 pc);
  470. u32 DecodeArithmeticInteger(BasicBlock& bb, u32 pc);
  471. u32 DecodeArithmeticIntegerImmediate(BasicBlock& bb, u32 pc);
  472. u32 DecodeArithmeticHalf(BasicBlock& bb, u32 pc);
  473. u32 DecodeArithmeticHalfImmediate(BasicBlock& bb, u32 pc);
  474. u32 DecodeFfma(BasicBlock& bb, u32 pc);
  475. u32 DecodeHfma2(BasicBlock& bb, u32 pc);
  476. u32 DecodeConversion(BasicBlock& bb, u32 pc);
  477. u32 DecodeMemory(BasicBlock& bb, u32 pc);
  478. u32 DecodeFloatSetPredicate(BasicBlock& bb, u32 pc);
  479. u32 DecodeIntegerSetPredicate(BasicBlock& bb, u32 pc);
  480. u32 DecodeHalfSetPredicate(BasicBlock& bb, u32 pc);
  481. u32 DecodePredicateSetRegister(BasicBlock& bb, u32 pc);
  482. u32 DecodePredicateSetPredicate(BasicBlock& bb, u32 pc);
  483. u32 DecodeRegisterSetPredicate(BasicBlock& bb, u32 pc);
  484. u32 DecodeFloatSet(BasicBlock& bb, u32 pc);
  485. u32 DecodeIntegerSet(BasicBlock& bb, u32 pc);
  486. u32 DecodeHalfSet(BasicBlock& bb, u32 pc);
  487. u32 DecodeVideo(BasicBlock& bb, u32 pc);
  488. u32 DecodeXmad(BasicBlock& bb, u32 pc);
  489. u32 DecodeOther(BasicBlock& bb, u32 pc);
  490. /// Internalizes node's data and returns a managed pointer to a clone of that node
  491. Node StoreNode(NodeData&& node_data);
  492. /// Creates a conditional node
  493. Node Conditional(Node condition, std::vector<Node>&& code);
  494. /// Creates a commentary
  495. Node Comment(const std::string& text);
  496. /// Creates an u32 immediate
  497. Node Immediate(u32 value);
  498. /// Creates a s32 immediate
  499. Node Immediate(s32 value) {
  500. return Immediate(static_cast<u32>(value));
  501. }
  502. /// Creates a f32 immediate
  503. Node Immediate(f32 value) {
  504. u32 integral;
  505. std::memcpy(&integral, &value, sizeof(u32));
  506. return Immediate(integral);
  507. }
  508. /// Generates a node for a passed register.
  509. Node GetRegister(Tegra::Shader::Register reg);
  510. /// Generates a node representing a 19-bit immediate value
  511. Node GetImmediate19(Tegra::Shader::Instruction instr);
  512. /// Generates a node representing a 32-bit immediate value
  513. Node GetImmediate32(Tegra::Shader::Instruction instr);
  514. /// Generates a node representing a constant buffer
  515. Node GetConstBuffer(u64 index, u64 offset);
  516. /// Generates a node representing a constant buffer with a variadic offset
  517. Node GetConstBufferIndirect(u64 index, u64 offset, Node node);
  518. /// Generates a node for a passed predicate. It can be optionally negated
  519. Node GetPredicate(u64 pred, bool negated = false);
  520. /// Generates a predicate node for an immediate true or false value
  521. Node GetPredicate(bool immediate);
  522. /// Generates a node representing an input atttribute. Keeps track of used attributes.
  523. Node GetInputAttribute(Tegra::Shader::Attribute::Index index, u64 element,
  524. const Tegra::Shader::IpaMode& input_mode, Node buffer = {});
  525. /// Generates a node representing an output atttribute. Keeps track of used attributes.
  526. Node GetOutputAttribute(Tegra::Shader::Attribute::Index index, u64 element, Node buffer);
  527. /// Generates a node representing an internal flag
  528. Node GetInternalFlag(InternalFlag flag, bool negated = false);
  529. /// Generates a node representing a local memory address
  530. Node GetLocalMemory(Node address);
  531. /// Sets a register. src value must be a number-evaluated node.
  532. void SetRegister(BasicBlock& bb, Tegra::Shader::Register dest, Node src);
  533. /// Sets a predicate. src value must be a bool-evaluated node
  534. void SetPredicate(BasicBlock& bb, u64 dest, Node src);
  535. /// Sets an internal flag. src value must be a bool-evaluated node
  536. void SetInternalFlag(BasicBlock& bb, InternalFlag flag, Node value);
  537. /// Sets a local memory address. address and value must be a number-evaluated node
  538. void SetLocalMemory(BasicBlock& bb, Node address, Node value);
  539. /// Conditionally absolute/negated float. Absolute is applied first
  540. Node GetOperandAbsNegFloat(Node value, bool absolute, bool negate);
  541. /// Conditionally saturates a float
  542. Node GetSaturatedFloat(Node value, bool saturate = true);
  543. /// Converts an integer to different sizes.
  544. Node ConvertIntegerSize(Node value, Tegra::Shader::Register::Size size, bool is_signed);
  545. /// Conditionally absolute/negated integer. Absolute is applied first
  546. Node GetOperandAbsNegInteger(Node value, bool absolute, bool negate, bool is_signed);
  547. /// Unpacks a half immediate from an instruction
  548. Node UnpackHalfImmediate(Tegra::Shader::Instruction instr, bool has_negation);
  549. /// Merges a half pair into another value
  550. Node HalfMerge(Node dest, Node src, Tegra::Shader::HalfMerge merge);
  551. /// Conditionally absolute/negated half float pair. Absolute is applied first
  552. Node GetOperandAbsNegHalf(Node value, bool absolute, bool negate);
  553. /// Returns a predicate comparing two floats
  554. Node GetPredicateComparisonFloat(Tegra::Shader::PredCondition condition, Node op_a, Node op_b);
  555. /// Returns a predicate comparing two integers
  556. Node GetPredicateComparisonInteger(Tegra::Shader::PredCondition condition, bool is_signed,
  557. Node op_a, Node op_b);
  558. /// Returns a predicate comparing two half floats. meta consumes how both pairs will be compared
  559. Node GetPredicateComparisonHalf(Tegra::Shader::PredCondition condition,
  560. const MetaHalfArithmetic& meta, Node op_a, Node op_b);
  561. /// Returns a predicate combiner operation
  562. OperationCode GetPredicateCombiner(Tegra::Shader::PredOperation operation);
  563. /// Returns a condition code evaluated from internal flags
  564. Node GetConditionCode(Tegra::Shader::ConditionCode cc);
  565. /// Accesses a texture sampler
  566. const Sampler& GetSampler(const Tegra::Shader::Sampler& sampler,
  567. Tegra::Shader::TextureType type, bool is_array, bool is_shadow);
  568. /// Extracts a sequence of bits from a node
  569. Node BitfieldExtract(Node value, u32 offset, u32 bits);
  570. void WriteTexsInstructionFloat(BasicBlock& bb, Tegra::Shader::Instruction instr, Node texture);
  571. void WriteTexsInstructionHalfFloat(BasicBlock& bb, Tegra::Shader::Instruction instr,
  572. Node texture);
  573. Node GetTexCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  574. Tegra::Shader::TextureProcessMode process_mode, bool depth_compare,
  575. bool is_array);
  576. Node GetTexsCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  577. Tegra::Shader::TextureProcessMode process_mode, bool depth_compare,
  578. bool is_array);
  579. Node GetTld4Code(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  580. bool depth_compare, bool is_array);
  581. Node GetTldsCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  582. bool is_array);
  583. std::tuple<std::size_t, std::size_t> ValidateAndGetCoordinateElement(
  584. Tegra::Shader::TextureType texture_type, bool depth_compare, bool is_array,
  585. bool lod_bias_enabled, std::size_t max_coords, std::size_t max_inputs);
  586. Node GetTextureCode(Tegra::Shader::Instruction instr, Tegra::Shader::TextureType texture_type,
  587. Tegra::Shader::TextureProcessMode process_mode, bool depth_compare,
  588. bool is_array, std::size_t array_offset, std::size_t bias_offset,
  589. std::vector<Node>&& coords);
  590. Node GetVideoOperand(Node op, bool is_chunk, bool is_signed, Tegra::Shader::VideoType type,
  591. u64 byte_height);
  592. void WriteLogicOperation(BasicBlock& bb, Tegra::Shader::Register dest,
  593. Tegra::Shader::LogicOperation logic_op, Node op_a, Node op_b,
  594. Tegra::Shader::PredicateResultMode predicate_mode,
  595. Tegra::Shader::Pred predicate);
  596. void WriteLop3Instruction(BasicBlock& bb, Tegra::Shader::Register dest, Node op_a, Node op_b,
  597. Node op_c, Node imm_lut);
  598. template <typename... T>
  599. Node Operation(OperationCode code, const T*... operands) {
  600. return StoreNode(OperationNode(code, operands...));
  601. }
  602. template <typename... T>
  603. Node Operation(OperationCode code, Meta&& meta, const T*... operands) {
  604. return StoreNode(OperationNode(code, std::move(meta), operands...));
  605. }
  606. template <typename... T>
  607. Node Operation(OperationCode code, std::vector<Node>&& operands) {
  608. return StoreNode(OperationNode(code, std::move(operands)));
  609. }
  610. template <typename... T>
  611. Node Operation(OperationCode code, Meta&& meta, std::vector<Node>&& operands) {
  612. return StoreNode(OperationNode(code, std::move(meta), std::move(operands)));
  613. }
  614. template <typename... T>
  615. Node SignedOperation(OperationCode code, bool is_signed, const T*... operands) {
  616. return StoreNode(OperationNode(SignedToUnsignedCode(code, is_signed), operands...));
  617. }
  618. template <typename... T>
  619. Node SignedOperation(OperationCode code, bool is_signed, Meta&& meta, const T*... operands) {
  620. return StoreNode(
  621. OperationNode(SignedToUnsignedCode(code, is_signed), std::move(meta), operands...));
  622. }
  623. static OperationCode SignedToUnsignedCode(OperationCode operation_code, bool is_signed);
  624. const ProgramCode& program_code;
  625. const u32 main_offset;
  626. u32 coverage_begin{};
  627. u32 coverage_end{};
  628. std::map<std::pair<u32, u32>, ExitMethod> exit_method_map;
  629. std::map<u32, BasicBlock> basic_blocks;
  630. std::vector<std::unique_ptr<NodeData>> stored_nodes;
  631. std::set<u32> used_registers;
  632. std::set<Tegra::Shader::Pred> used_predicates;
  633. std::map<Tegra::Shader::Attribute::Index, std::set<Tegra::Shader::IpaMode>>
  634. used_input_attributes;
  635. std::set<Tegra::Shader::Attribute::Index> used_output_attributes;
  636. std::map<u32, ConstBuffer> used_cbufs;
  637. std::set<Sampler> used_samplers;
  638. std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances> used_clip_distances{};
  639. Tegra::Shader::Header header;
  640. };
  641. } // namespace VideoCommon::Shader