shader_bytecode.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. union {
  154. BitField<39, 3, u64> pred;
  155. BitField<42, 1, u64> negate_pred;
  156. } fmnmx;
  157. float GetImm20_19() const {
  158. float result{};
  159. u32 imm{static_cast<u32>(imm20_19)};
  160. imm <<= 12;
  161. imm |= negate_imm ? 0x80000000 : 0;
  162. std::memcpy(&result, &imm, sizeof(imm));
  163. return result;
  164. }
  165. float GetImm20_32() const {
  166. float result{};
  167. u32 imm{static_cast<u32>(imm20_32)};
  168. std::memcpy(&result, &imm, sizeof(imm));
  169. return result;
  170. }
  171. } alu;
  172. union {
  173. BitField<48, 1, u64> negate_b;
  174. BitField<49, 1, u64> negate_c;
  175. } ffma;
  176. union {
  177. BitField<0, 3, u64> pred0;
  178. BitField<3, 3, u64> pred3;
  179. BitField<7, 1, u64> abs_a;
  180. BitField<39, 3, u64> pred39;
  181. BitField<42, 1, u64> neg_pred;
  182. BitField<43, 1, u64> neg_a;
  183. BitField<44, 1, u64> abs_b;
  184. BitField<45, 2, PredOperation> op;
  185. BitField<47, 1, u64> ftz;
  186. BitField<48, 4, PredCondition> cond;
  187. BitField<56, 1, u64> neg_b;
  188. } fsetp;
  189. union {
  190. BitField<39, 3, u64> pred39;
  191. BitField<42, 1, u64> neg_pred;
  192. BitField<43, 1, u64> neg_a;
  193. BitField<44, 1, u64> abs_b;
  194. BitField<45, 2, PredOperation> op;
  195. BitField<48, 4, PredCondition> cond;
  196. BitField<53, 1, u64> neg_b;
  197. BitField<54, 1, u64> abs_a;
  198. BitField<52, 1, u64> bf;
  199. BitField<55, 1, u64> ftz;
  200. BitField<56, 1, u64> neg_imm;
  201. } fset;
  202. union {
  203. BitField<10, 2, Register::Size> size;
  204. BitField<13, 1, u64> is_signed;
  205. BitField<41, 2, u64> selector;
  206. BitField<45, 1, u64> negate_a;
  207. BitField<49, 1, u64> abs_a;
  208. BitField<50, 1, u64> saturate_a;
  209. } conversion;
  210. BitField<61, 1, u64> is_b_imm;
  211. BitField<60, 1, u64> is_b_gpr;
  212. BitField<59, 1, u64> is_c_gpr;
  213. Attribute attribute;
  214. Uniform uniform;
  215. Sampler sampler;
  216. u64 value;
  217. };
  218. static_assert(sizeof(Instruction) == 0x8, "Incorrect structure size");
  219. static_assert(std::is_standard_layout<Instruction>::value,
  220. "Structure does not have standard layout");
  221. class OpCode {
  222. public:
  223. enum class Id {
  224. KIL,
  225. LD_A,
  226. ST_A,
  227. TEXQ, // Texture Query
  228. TEXS, // Texture Fetch with scalar/non-vec4 source/destinations
  229. TLDS, // Texture Load with scalar/non-vec4 source/destinations
  230. EXIT,
  231. IPA,
  232. FFMA_IMM, // Fused Multiply and Add
  233. FFMA_CR,
  234. FFMA_RC,
  235. FFMA_RR,
  236. FADD_C,
  237. FADD_R,
  238. FADD_IMM,
  239. FMUL_C,
  240. FMUL_R,
  241. FMUL_IMM,
  242. FMUL32_IMM,
  243. MUFU, // Multi-Function Operator
  244. RRO, // Range Reduction Operator
  245. F2F_C,
  246. F2F_R,
  247. F2F_IMM,
  248. F2I_C,
  249. F2I_R,
  250. F2I_IMM,
  251. I2F_C,
  252. I2F_R,
  253. I2F_IMM,
  254. I2I_C,
  255. I2I_R,
  256. I2I_IMM,
  257. LOP32I,
  258. MOV_C,
  259. MOV_R,
  260. MOV_IMM,
  261. MOV32_IMM,
  262. SHR_C,
  263. SHR_R,
  264. SHR_IMM,
  265. FMNMX_C,
  266. FMNMX_R,
  267. FMNMX_IMM,
  268. FSETP_C, // Set Predicate
  269. FSETP_R,
  270. FSETP_IMM,
  271. FSET_C,
  272. FSET_R,
  273. FSET_IMM,
  274. ISETP_C,
  275. ISETP_IMM,
  276. ISETP_R,
  277. PSETP,
  278. };
  279. enum class Type {
  280. Trivial,
  281. Arithmetic,
  282. Ffma,
  283. Flow,
  284. Memory,
  285. FloatSet,
  286. FloatSetPredicate,
  287. IntegerSetPredicate,
  288. PredicateSetPredicate,
  289. Conversion,
  290. Unknown,
  291. };
  292. class Matcher {
  293. public:
  294. Matcher(const char* const name, u16 mask, u16 expected, OpCode::Id id, OpCode::Type type)
  295. : name{name}, mask{mask}, expected{expected}, id{id}, type{type} {}
  296. const char* GetName() const {
  297. return name;
  298. }
  299. u16 GetMask() const {
  300. return mask;
  301. }
  302. Id GetId() const {
  303. return id;
  304. }
  305. Type GetType() const {
  306. return type;
  307. }
  308. /**
  309. * Tests to see if the given instruction is the instruction this matcher represents.
  310. * @param instruction The instruction to test
  311. * @returns true if the given instruction matches.
  312. */
  313. bool Matches(u16 instruction) const {
  314. return (instruction & mask) == expected;
  315. }
  316. private:
  317. const char* name;
  318. u16 mask;
  319. u16 expected;
  320. Id id;
  321. Type type;
  322. };
  323. static boost::optional<const Matcher&> Decode(Instruction instr) {
  324. static const auto table{GetDecodeTable()};
  325. const auto matches_instruction = [instr](const auto& matcher) {
  326. return matcher.Matches(static_cast<u16>(instr.opcode));
  327. };
  328. auto iter = std::find_if(table.begin(), table.end(), matches_instruction);
  329. return iter != table.end() ? boost::optional<const Matcher&>(*iter) : boost::none;
  330. }
  331. private:
  332. struct Detail {
  333. private:
  334. static constexpr size_t opcode_bitsize = 16;
  335. /**
  336. * Generates the mask and the expected value after masking from a given bitstring.
  337. * A '0' in a bitstring indicates that a zero must be present at that bit position.
  338. * A '1' in a bitstring indicates that a one must be present at that bit position.
  339. */
  340. static auto GetMaskAndExpect(const char* const bitstring) {
  341. u16 mask = 0, expect = 0;
  342. for (size_t i = 0; i < opcode_bitsize; i++) {
  343. const size_t bit_position = opcode_bitsize - i - 1;
  344. switch (bitstring[i]) {
  345. case '0':
  346. mask |= 1 << bit_position;
  347. break;
  348. case '1':
  349. expect |= 1 << bit_position;
  350. mask |= 1 << bit_position;
  351. break;
  352. default:
  353. // Ignore
  354. break;
  355. }
  356. }
  357. return std::make_tuple(mask, expect);
  358. }
  359. public:
  360. /// Creates a matcher that can match and parse instructions based on bitstring.
  361. static auto GetMatcher(const char* const bitstring, OpCode::Id op, OpCode::Type type,
  362. const char* const name) {
  363. const auto mask_expect = GetMaskAndExpect(bitstring);
  364. return Matcher(name, std::get<0>(mask_expect), std::get<1>(mask_expect), op, type);
  365. }
  366. };
  367. static std::vector<Matcher> GetDecodeTable() {
  368. std::vector<Matcher> table = {
  369. #define INST(bitstring, op, type, name) Detail::GetMatcher(bitstring, op, type, name)
  370. INST("111000110011----", Id::KIL, Type::Flow, "KIL"),
  371. INST("1110111111011---", Id::LD_A, Type::Memory, "LD_A"),
  372. INST("1110111111110---", Id::ST_A, Type::Memory, "ST_A"),
  373. INST("1101111101001---", Id::TEXQ, Type::Memory, "TEXQ"),
  374. INST("1101100---------", Id::TEXS, Type::Memory, "TEXS"),
  375. INST("1101101---------", Id::TLDS, Type::Memory, "TLDS"),
  376. INST("111000110000----", Id::EXIT, Type::Trivial, "EXIT"),
  377. INST("11100000--------", Id::IPA, Type::Trivial, "IPA"),
  378. INST("001100101-------", Id::FFMA_IMM, Type::Ffma, "FFMA_IMM"),
  379. INST("010010011-------", Id::FFMA_CR, Type::Ffma, "FFMA_CR"),
  380. INST("010100011-------", Id::FFMA_RC, Type::Ffma, "FFMA_RC"),
  381. INST("010110011-------", Id::FFMA_RR, Type::Ffma, "FFMA_RR"),
  382. INST("0100110001011---", Id::FADD_C, Type::Arithmetic, "FADD_C"),
  383. INST("0101110001011---", Id::FADD_R, Type::Arithmetic, "FADD_R"),
  384. INST("0011100-01011---", Id::FADD_IMM, Type::Arithmetic, "FADD_IMM"),
  385. INST("0100110001101---", Id::FMUL_C, Type::Arithmetic, "FMUL_C"),
  386. INST("0101110001101---", Id::FMUL_R, Type::Arithmetic, "FMUL_R"),
  387. INST("0011100-01101---", Id::FMUL_IMM, Type::Arithmetic, "FMUL_IMM"),
  388. INST("00011110--------", Id::FMUL32_IMM, Type::Arithmetic, "FMUL32_IMM"),
  389. INST("0101000010000---", Id::MUFU, Type::Arithmetic, "MUFU"),
  390. INST("0101110010010---", Id::RRO, Type::Arithmetic, "RRO"),
  391. INST("0100110010101---", Id::F2F_C, Type::Arithmetic, "F2F_C"),
  392. INST("0101110010101---", Id::F2F_R, Type::Arithmetic, "F2F_R"),
  393. INST("0011100-10101---", Id::F2F_IMM, Type::Arithmetic, "F2F_IMM"),
  394. INST("0100110010110---", Id::F2I_C, Type::Arithmetic, "F2I_C"),
  395. INST("0101110010110---", Id::F2I_R, Type::Arithmetic, "F2I_R"),
  396. INST("0011100-10110---", Id::F2I_IMM, Type::Arithmetic, "F2I_IMM"),
  397. INST("000001----------", Id::LOP32I, Type::Arithmetic, "LOP32I"),
  398. INST("0100110010011---", Id::MOV_C, Type::Arithmetic, "MOV_C"),
  399. INST("0101110010011---", Id::MOV_R, Type::Arithmetic, "MOV_R"),
  400. INST("0011100-10011---", Id::MOV_IMM, Type::Arithmetic, "MOV_IMM"),
  401. INST("000000010000----", Id::MOV32_IMM, Type::Arithmetic, "MOV32_IMM"),
  402. INST("0100110000101---", Id::SHR_C, Type::Arithmetic, "SHR_C"),
  403. INST("0101110000101---", Id::SHR_R, Type::Arithmetic, "SHR_R"),
  404. INST("0011100-00101---", Id::SHR_IMM, Type::Arithmetic, "SHR_IMM"),
  405. INST("0100110001100---", Id::FMNMX_C, Type::Arithmetic, "FMNMX_C"),
  406. INST("0101110001100---", Id::FMNMX_R, Type::Arithmetic, "FMNMX_R"),
  407. INST("0011100-01100---", Id::FMNMX_IMM, Type::Arithmetic, "FMNMX_IMM"),
  408. INST("0100110011100---", Id::I2I_C, Type::Conversion, "I2I_C"),
  409. INST("0101110011100---", Id::I2I_R, Type::Conversion, "I2I_R"),
  410. INST("01110001-1000---", Id::I2I_IMM, Type::Conversion, "I2I_IMM"),
  411. INST("0100110010111---", Id::I2F_C, Type::Conversion, "I2F_C"),
  412. INST("0101110010111---", Id::I2F_R, Type::Conversion, "I2F_R"),
  413. INST("0011100-10111---", Id::I2F_IMM, Type::Conversion, "I2F_IMM"),
  414. INST("01011000--------", Id::FSET_R, Type::FloatSet, "FSET_R"),
  415. INST("0100100---------", Id::FSET_C, Type::FloatSet, "FSET_C"),
  416. INST("0011000---------", Id::FSET_IMM, Type::FloatSet, "FSET_IMM"),
  417. INST("010010111011----", Id::FSETP_C, Type::FloatSetPredicate, "FSETP_C"),
  418. INST("010110111011----", Id::FSETP_R, Type::FloatSetPredicate, "FSETP_R"),
  419. INST("0011011-1011----", Id::FSETP_IMM, Type::FloatSetPredicate, "FSETP_IMM"),
  420. INST("010010110110----", Id::ISETP_C, Type::IntegerSetPredicate, "ISETP_C"),
  421. INST("010110110110----", Id::ISETP_R, Type::IntegerSetPredicate, "ISETP_R"),
  422. INST("0011011-0110----", Id::ISETP_IMM, Type::IntegerSetPredicate, "ISETP_IMM"),
  423. INST("0101000010010---", Id::PSETP, Type::PredicateSetPredicate, "PSETP"),
  424. };
  425. #undef INST
  426. std::stable_sort(table.begin(), table.end(), [](const auto& a, const auto& b) {
  427. // If a matcher has more bits in its mask it is more specific, so it
  428. // should come first.
  429. return std::bitset<16>(a.GetMask()).count() > std::bitset<16>(b.GetMask()).count();
  430. });
  431. return table;
  432. }
  433. };
  434. } // namespace Shader
  435. } // namespace Tegra