shader_bytecode.h 15 KB

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