shader_bytecode.h 14 KB

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