shader_bytecode.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 <bitset>
  6. #include <cstring>
  7. #include <map>
  8. #include <string>
  9. #include <vector>
  10. #include <boost/optional.hpp>
  11. #include "common/bit_field.h"
  12. #include "common/common_types.h"
  13. namespace Tegra {
  14. namespace Shader {
  15. struct Register {
  16. /// Number of registers
  17. static constexpr size_t NumRegisters = 256;
  18. /// Register 255 is special cased to always be 0
  19. static constexpr size_t ZeroIndex = 255;
  20. enum class Size : u64 {
  21. Byte = 0,
  22. Short = 1,
  23. Word = 2,
  24. Long = 3,
  25. };
  26. constexpr Register() = default;
  27. constexpr Register(u64 value) : value(value) {}
  28. constexpr operator u64() const {
  29. return value;
  30. }
  31. template <typename T>
  32. constexpr u64 operator-(const T& oth) const {
  33. return value - oth;
  34. }
  35. template <typename T>
  36. constexpr u64 operator&(const T& oth) const {
  37. return value & oth;
  38. }
  39. constexpr u64 operator&(const Register& oth) const {
  40. return value & oth.value;
  41. }
  42. constexpr u64 operator~() const {
  43. return ~value;
  44. }
  45. u64 GetSwizzledIndex(u64 elem) const {
  46. elem = (value + elem) & 3;
  47. return (value & ~3) + elem;
  48. }
  49. private:
  50. u64 value{};
  51. };
  52. union Attribute {
  53. Attribute() = default;
  54. constexpr explicit Attribute(u64 value) : value(value) {}
  55. enum class Index : u64 {
  56. Position = 7,
  57. Attribute_0 = 8,
  58. };
  59. union {
  60. BitField<22, 2, u64> element;
  61. BitField<24, 6, Index> index;
  62. BitField<47, 3, u64> size;
  63. } fmt20;
  64. union {
  65. BitField<30, 2, u64> element;
  66. BitField<32, 6, Index> index;
  67. } fmt28;
  68. BitField<39, 8, u64> reg;
  69. u64 value{};
  70. };
  71. union Sampler {
  72. Sampler() = default;
  73. constexpr explicit Sampler(u64 value) : value(value) {}
  74. enum class Index : u64 {
  75. Sampler_0 = 8,
  76. };
  77. BitField<36, 13, Index> index;
  78. u64 value{};
  79. };
  80. union Uniform {
  81. BitField<20, 14, u64> offset;
  82. BitField<34, 5, u64> index;
  83. };
  84. } // namespace Shader
  85. } // namespace Tegra
  86. namespace std {
  87. // TODO(bunnei): The below is forbidden by the C++ standard, but works fine. See #330.
  88. template <>
  89. struct make_unsigned<Tegra::Shader::Attribute> {
  90. using type = Tegra::Shader::Attribute;
  91. };
  92. template <>
  93. struct make_unsigned<Tegra::Shader::Register> {
  94. using type = Tegra::Shader::Register;
  95. };
  96. } // namespace std
  97. namespace Tegra {
  98. namespace Shader {
  99. enum class Pred : u64 {
  100. UnusedIndex = 0x7,
  101. NeverExecute = 0xF,
  102. };
  103. enum class PredCondition : u64 {
  104. LessThan = 1,
  105. Equal = 2,
  106. LessEqual = 3,
  107. GreaterThan = 4,
  108. NotEqual = 5,
  109. GreaterEqual = 6,
  110. // TODO(Subv): Other condition types
  111. };
  112. enum class PredOperation : u64 {
  113. And = 0,
  114. Or = 1,
  115. Xor = 2,
  116. };
  117. enum class SubOp : u64 {
  118. Cos = 0x0,
  119. Sin = 0x1,
  120. Ex2 = 0x2,
  121. Lg2 = 0x3,
  122. Rcp = 0x4,
  123. Rsq = 0x5,
  124. Min = 0x8,
  125. };
  126. union Instruction {
  127. Instruction& operator=(const Instruction& instr) {
  128. value = instr.value;
  129. return *this;
  130. }
  131. constexpr Instruction(u64 value) : value{value} {}
  132. BitField<0, 8, Register> gpr0;
  133. BitField<8, 8, Register> gpr8;
  134. union {
  135. BitField<16, 4, Pred> full_pred;
  136. BitField<16, 3, u64> pred_index;
  137. } pred;
  138. BitField<19, 1, u64> negate_pred;
  139. BitField<20, 8, Register> gpr20;
  140. BitField<20, 7, SubOp> sub_op;
  141. BitField<28, 8, Register> gpr28;
  142. BitField<39, 8, Register> gpr39;
  143. BitField<48, 16, u64> opcode;
  144. union {
  145. BitField<20, 19, u64> imm20_19;
  146. BitField<20, 32, u64> imm20_32;
  147. BitField<45, 1, u64> negate_b;
  148. BitField<46, 1, u64> abs_a;
  149. BitField<48, 1, u64> negate_a;
  150. BitField<49, 1, u64> abs_b;
  151. BitField<50, 1, u64> abs_d;
  152. BitField<56, 1, u64> negate_imm;
  153. float GetImm20_19() const {
  154. float result{};
  155. u32 imm{static_cast<u32>(imm20_19)};
  156. imm <<= 12;
  157. imm |= negate_imm ? 0x80000000 : 0;
  158. std::memcpy(&result, &imm, sizeof(imm));
  159. return result;
  160. }
  161. float GetImm20_32() const {
  162. float result{};
  163. u32 imm{static_cast<u32>(imm20_32)};
  164. std::memcpy(&result, &imm, sizeof(imm));
  165. return result;
  166. }
  167. } alu;
  168. union {
  169. BitField<48, 1, u64> negate_b;
  170. BitField<49, 1, u64> negate_c;
  171. } ffma;
  172. union {
  173. BitField<0, 3, u64> pred0;
  174. BitField<3, 3, u64> pred3;
  175. BitField<7, 1, u64> abs_a;
  176. BitField<39, 3, u64> pred39;
  177. BitField<42, 1, u64> neg_pred;
  178. BitField<43, 1, u64> neg_a;
  179. BitField<44, 1, u64> abs_b;
  180. BitField<45, 2, PredOperation> op;
  181. BitField<47, 1, u64> ftz;
  182. BitField<48, 4, PredCondition> cond;
  183. BitField<56, 1, u64> neg_b;
  184. } fsetp;
  185. union {
  186. BitField<39, 3, u64> pred39;
  187. BitField<42, 1, u64> neg_pred;
  188. BitField<43, 1, u64> neg_a;
  189. BitField<44, 1, u64> abs_b;
  190. BitField<45, 2, PredOperation> op;
  191. BitField<48, 4, PredCondition> cond;
  192. BitField<53, 1, u64> neg_b;
  193. BitField<54, 1, u64> abs_a;
  194. BitField<52, 1, u64> bf;
  195. BitField<55, 1, u64> ftz;
  196. BitField<56, 1, u64> neg_imm;
  197. } fset;
  198. union {
  199. BitField<10, 2, Register::Size> size;
  200. BitField<13, 1, u64> is_signed;
  201. BitField<41, 2, u64> selector;
  202. BitField<45, 1, u64> negate_a;
  203. BitField<49, 1, u64> abs_a;
  204. BitField<50, 1, u64> saturate_a;
  205. } i2i;
  206. BitField<61, 1, u64> is_b_imm;
  207. BitField<60, 1, u64> is_b_gpr;
  208. BitField<59, 1, u64> is_c_gpr;
  209. Attribute attribute;
  210. Uniform uniform;
  211. Sampler sampler;
  212. u64 value;
  213. };
  214. static_assert(sizeof(Instruction) == 0x8, "Incorrect structure size");
  215. static_assert(std::is_standard_layout<Instruction>::value,
  216. "Structure does not have standard layout");
  217. class OpCode {
  218. public:
  219. enum class Id {
  220. KIL,
  221. LD_A,
  222. ST_A,
  223. TEXQ, // Texture Query
  224. TEXS, // Texture Fetch with scalar/non-vec4 source/destinations
  225. TLDS, // Texture Load with scalar/non-vec4 source/destinations
  226. EXIT,
  227. IPA,
  228. FFMA_IMM, // Fused Multiply and Add
  229. FFMA_CR,
  230. FFMA_RC,
  231. FFMA_RR,
  232. FADD_C,
  233. FADD_R,
  234. FADD_IMM,
  235. FMUL_C,
  236. FMUL_R,
  237. FMUL_IMM,
  238. FMUL32_IMM,
  239. MUFU, // Multi-Function Operator
  240. RRO, // Range Reduction Operator
  241. F2F_C,
  242. F2F_R,
  243. F2F_IMM,
  244. F2I_C,
  245. F2I_R,
  246. F2I_IMM,
  247. I2F_C,
  248. I2F_R,
  249. I2F_IMM,
  250. I2I_C,
  251. I2I_R,
  252. I2I_IMM,
  253. LOP32I,
  254. MOV_C,
  255. MOV_R,
  256. MOV_IMM,
  257. MOV32_IMM,
  258. SHR_C,
  259. SHR_R,
  260. SHR_IMM,
  261. FSETP_C, // Set Predicate
  262. FSETP_R,
  263. FSETP_IMM,
  264. FSET_C,
  265. FSET_R,
  266. FSET_IMM,
  267. ISETP_C,
  268. ISETP_IMM,
  269. ISETP_R,
  270. };
  271. enum class Type {
  272. Trivial,
  273. Arithmetic,
  274. Ffma,
  275. Flow,
  276. Memory,
  277. FloatSet,
  278. FloatSetPredicate,
  279. IntegerSetPredicate,
  280. I2I,
  281. Unknown,
  282. };
  283. class Matcher {
  284. public:
  285. Matcher(const char* const name, u16 mask, u16 expected, OpCode::Id id, OpCode::Type type)
  286. : name{name}, mask{mask}, expected{expected}, id{id}, type{type} {}
  287. const char* GetName() const {
  288. return name;
  289. }
  290. u16 GetMask() const {
  291. return mask;
  292. }
  293. Id GetId() const {
  294. return id;
  295. }
  296. Type GetType() const {
  297. return type;
  298. }
  299. /**
  300. * Tests to see if the given instruction is the instruction this matcher represents.
  301. * @param instruction The instruction to test
  302. * @returns true if the given instruction matches.
  303. */
  304. bool Matches(u16 instruction) const {
  305. return (instruction & mask) == expected;
  306. }
  307. private:
  308. const char* name;
  309. u16 mask;
  310. u16 expected;
  311. Id id;
  312. Type type;
  313. };
  314. static boost::optional<const Matcher&> Decode(Instruction instr) {
  315. static const auto table{GetDecodeTable()};
  316. const auto matches_instruction = [instr](const auto& matcher) {
  317. return matcher.Matches(static_cast<u16>(instr.opcode));
  318. };
  319. auto iter = std::find_if(table.begin(), table.end(), matches_instruction);
  320. return iter != table.end() ? boost::optional<const Matcher&>(*iter) : boost::none;
  321. }
  322. private:
  323. struct Detail {
  324. private:
  325. static constexpr size_t opcode_bitsize = 16;
  326. /**
  327. * Generates the mask and the expected value after masking from a given bitstring.
  328. * A '0' in a bitstring indicates that a zero must be present at that bit position.
  329. * A '1' in a bitstring indicates that a one must be present at that bit position.
  330. */
  331. static auto GetMaskAndExpect(const char* const bitstring) {
  332. u16 mask = 0, expect = 0;
  333. for (size_t i = 0; i < opcode_bitsize; i++) {
  334. const size_t bit_position = opcode_bitsize - i - 1;
  335. switch (bitstring[i]) {
  336. case '0':
  337. mask |= 1 << bit_position;
  338. break;
  339. case '1':
  340. expect |= 1 << bit_position;
  341. mask |= 1 << bit_position;
  342. break;
  343. default:
  344. // Ignore
  345. break;
  346. }
  347. }
  348. return std::make_tuple(mask, expect);
  349. }
  350. public:
  351. /// Creates a matcher that can match and parse instructions based on bitstring.
  352. static auto GetMatcher(const char* const bitstring, OpCode::Id op, OpCode::Type type,
  353. const char* const name) {
  354. const auto mask_expect = GetMaskAndExpect(bitstring);
  355. return Matcher(name, std::get<0>(mask_expect), std::get<1>(mask_expect), op, type);
  356. }
  357. };
  358. static std::vector<Matcher> GetDecodeTable() {
  359. std::vector<Matcher> table = {
  360. #define INST(bitstring, op, type, name) Detail::GetMatcher(bitstring, op, type, name)
  361. INST("111000110011----", Id::KIL, Type::Flow, "KIL"),
  362. INST("1110111111011---", Id::LD_A, Type::Memory, "LD_A"),
  363. INST("1110111111110---", Id::ST_A, Type::Memory, "ST_A"),
  364. INST("1101111101001---", Id::TEXQ, Type::Memory, "TEXQ"),
  365. INST("1101100---------", Id::TEXS, Type::Memory, "TEXS"),
  366. INST("1101101---------", Id::TLDS, Type::Memory, "TLDS"),
  367. INST("111000110000----", Id::EXIT, Type::Trivial, "EXIT"),
  368. INST("11100000--------", Id::IPA, Type::Trivial, "IPA"),
  369. INST("001100101-------", Id::FFMA_IMM, Type::Ffma, "FFMA_IMM"),
  370. INST("010010011-------", Id::FFMA_CR, Type::Ffma, "FFMA_CR"),
  371. INST("010100011-------", Id::FFMA_RC, Type::Ffma, "FFMA_RC"),
  372. INST("010110011-------", Id::FFMA_RR, Type::Ffma, "FFMA_RR"),
  373. INST("0100110001011---", Id::FADD_C, Type::Arithmetic, "FADD_C"),
  374. INST("0101110001011---", Id::FADD_R, Type::Arithmetic, "FADD_R"),
  375. INST("0011100-01011---", Id::FADD_IMM, Type::Arithmetic, "FADD_IMM"),
  376. INST("0100110001101---", Id::FMUL_C, Type::Arithmetic, "FMUL_C"),
  377. INST("0101110001101---", Id::FMUL_R, Type::Arithmetic, "FMUL_R"),
  378. INST("0011100-01101---", Id::FMUL_IMM, Type::Arithmetic, "FMUL_IMM"),
  379. INST("00011110--------", Id::FMUL32_IMM, Type::Arithmetic, "FMUL32_IMM"),
  380. INST("0101000010000---", Id::MUFU, Type::Arithmetic, "MUFU"),
  381. INST("0101110010010---", Id::RRO, Type::Arithmetic, "RRO"),
  382. INST("0100110010101---", Id::F2F_C, Type::Arithmetic, "F2F_C"),
  383. INST("0101110010101---", Id::F2F_R, Type::Arithmetic, "F2F_R"),
  384. INST("0011100-10101---", Id::F2F_IMM, Type::Arithmetic, "F2F_IMM"),
  385. INST("0100110010110---", Id::F2I_C, Type::Arithmetic, "F2I_C"),
  386. INST("0101110010110---", Id::F2I_R, Type::Arithmetic, "F2I_R"),
  387. INST("0011100-10110---", Id::F2I_IMM, Type::Arithmetic, "F2I_IMM"),
  388. INST("0100110010111---", Id::I2F_C, Type::Arithmetic, "I2F_C"),
  389. INST("0101110010111---", Id::I2F_R, Type::Arithmetic, "I2F_R"),
  390. INST("0011100-10111---", Id::I2F_IMM, Type::Arithmetic, "I2F_IMM"),
  391. INST("000001----------", Id::LOP32I, Type::Arithmetic, "LOP32I"),
  392. INST("0100110010011---", Id::MOV_C, Type::Arithmetic, "MOV_C"),
  393. INST("0101110010011---", Id::MOV_R, Type::Arithmetic, "MOV_R"),
  394. INST("0011100-10011---", Id::MOV_IMM, Type::Arithmetic, "MOV_IMM"),
  395. INST("000000010000----", Id::MOV32_IMM, Type::Arithmetic, "MOV32_IMM"),
  396. INST("0100110000101---", Id::SHR_C, Type::Arithmetic, "SHR_C"),
  397. INST("0101110000101---", Id::SHR_R, Type::Arithmetic, "SHR_R"),
  398. INST("0011100-00101---", Id::SHR_IMM, Type::Arithmetic, "SHR_IMM"),
  399. INST("0100110011100---", Id::I2I_C, Type::I2I, "I2I_C"),
  400. INST("0101110011100---", Id::I2I_R, Type::I2I, "I2I_R"),
  401. INST("01110001-1000---", Id::I2I_IMM, Type::I2I, "I2I_IMM"),
  402. INST("01011000--------", Id::FSET_R, Type::FloatSet, "FSET_R"),
  403. INST("0100100---------", Id::FSET_C, Type::FloatSet, "FSET_C"),
  404. INST("0011000---------", Id::FSET_IMM, Type::FloatSet, "FSET_IMM"),
  405. INST("010010111011----", Id::FSETP_C, Type::FloatSetPredicate, "FSETP_C"),
  406. INST("010110111011----", Id::FSETP_R, Type::FloatSetPredicate, "FSETP_R"),
  407. INST("0011011-1011----", Id::FSETP_IMM, Type::FloatSetPredicate, "FSETP_IMM"),
  408. INST("010010110110----", Id::ISETP_C, Type::IntegerSetPredicate, "ISETP_C"),
  409. INST("010110110110----", Id::ISETP_R, Type::IntegerSetPredicate, "ISETP_R"),
  410. INST("0011011-0110----", Id::ISETP_IMM, Type::IntegerSetPredicate, "ISETP_IMM"),
  411. };
  412. #undef INST
  413. std::stable_sort(table.begin(), table.end(), [](const auto& a, const auto& b) {
  414. // If a matcher has more bits in its mask it is more specific, so it
  415. // should come first.
  416. return std::bitset<16>(a.GetMask()).count() > std::bitset<16>(b.GetMask()).count();
  417. });
  418. return table;
  419. }
  420. };
  421. } // namespace Shader
  422. } // namespace Tegra