shader_bytecode.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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/assert.h"
  12. #include "common/bit_field.h"
  13. #include "common/common_types.h"
  14. namespace Tegra::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. Attribute_31 = 39,
  59. PointCoord = 46,
  60. // This attribute contains a tuple of (~, ~, InstanceId, VertexId) when inside a vertex
  61. // shader, and a tuple of (TessCoord.x, TessCoord.y, TessCoord.z, ~) when inside a Tess Eval
  62. // shader.
  63. TessCoordInstanceIDVertexID = 47,
  64. // This attribute contains a tuple of (Unk, Unk, Unk, gl_FrontFacing) when inside a fragment
  65. // shader. It is unknown what the other values contain.
  66. FrontFacing = 63,
  67. };
  68. union {
  69. BitField<22, 2, u64> element;
  70. BitField<24, 6, Index> index;
  71. BitField<47, 3, u64> size;
  72. } fmt20;
  73. union {
  74. BitField<30, 2, u64> element;
  75. BitField<32, 6, Index> index;
  76. } fmt28;
  77. BitField<39, 8, u64> reg;
  78. u64 value{};
  79. };
  80. union Sampler {
  81. Sampler() = default;
  82. constexpr explicit Sampler(u64 value) : value(value) {}
  83. enum class Index : u64 {
  84. Sampler_0 = 8,
  85. };
  86. BitField<36, 13, Index> index;
  87. u64 value{};
  88. };
  89. } // namespace Tegra::Shader
  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::Shader {
  102. enum class Pred : u64 {
  103. UnusedIndex = 0x7,
  104. NeverExecute = 0xF,
  105. };
  106. enum class PredCondition : u64 {
  107. LessThan = 1,
  108. Equal = 2,
  109. LessEqual = 3,
  110. GreaterThan = 4,
  111. NotEqual = 5,
  112. GreaterEqual = 6,
  113. LessThanWithNan = 9,
  114. GreaterThanWithNan = 12,
  115. NotEqualWithNan = 13,
  116. GreaterEqualWithNan = 14,
  117. // TODO(Subv): Other condition types
  118. };
  119. enum class PredOperation : u64 {
  120. And = 0,
  121. Or = 1,
  122. Xor = 2,
  123. };
  124. enum class LogicOperation : u64 {
  125. And = 0,
  126. Or = 1,
  127. Xor = 2,
  128. PassB = 3,
  129. };
  130. enum class SubOp : u64 {
  131. Cos = 0x0,
  132. Sin = 0x1,
  133. Ex2 = 0x2,
  134. Lg2 = 0x3,
  135. Rcp = 0x4,
  136. Rsq = 0x5,
  137. Sqrt = 0x8,
  138. };
  139. enum class F2iRoundingOp : u64 {
  140. None = 0,
  141. Floor = 1,
  142. Ceil = 2,
  143. Trunc = 3,
  144. };
  145. enum class F2fRoundingOp : u64 {
  146. None = 0,
  147. Pass = 3,
  148. Round = 8,
  149. Floor = 9,
  150. Ceil = 10,
  151. Trunc = 11,
  152. };
  153. enum class UniformType : u64 {
  154. UnsignedByte = 0,
  155. SignedByte = 1,
  156. UnsignedShort = 2,
  157. SignedShort = 3,
  158. Single = 4,
  159. Double = 5,
  160. };
  161. enum class IMinMaxExchange : u64 {
  162. None = 0,
  163. XLo = 1,
  164. XMed = 2,
  165. XHi = 3,
  166. };
  167. enum class XmadMode : u64 {
  168. None = 0,
  169. CLo = 1,
  170. CHi = 2,
  171. CSfu = 3,
  172. CBcc = 4,
  173. };
  174. enum class IAdd3Mode : u64 {
  175. None = 0,
  176. RightShift = 1,
  177. LeftShift = 2,
  178. };
  179. enum class IAdd3Height : u64 {
  180. None = 0,
  181. LowerHalfWord = 1,
  182. UpperHalfWord = 2,
  183. };
  184. enum class FlowCondition : u64 {
  185. Always = 0xF,
  186. Fcsm_Tr = 0x1C, // TODO(bunnei): What is this used for?
  187. };
  188. enum class PredicateResultMode : u64 {
  189. None = 0x0,
  190. NotZero = 0x3,
  191. };
  192. enum class TextureType : u64 {
  193. Texture1D = 0,
  194. Texture2D = 1,
  195. Texture3D = 2,
  196. TextureCube = 3,
  197. };
  198. enum class IpaInterpMode : u64 { Linear = 0, Perspective = 1, Flat = 2, Sc = 3 };
  199. enum class IpaSampleMode : u64 { Default = 0, Centroid = 1, Offset = 2 };
  200. struct IpaMode {
  201. IpaInterpMode interpolation_mode;
  202. IpaSampleMode sampling_mode;
  203. inline bool operator==(const IpaMode& a) {
  204. return (a.interpolation_mode == interpolation_mode) && (a.sampling_mode == sampling_mode);
  205. }
  206. inline bool operator!=(const IpaMode& a) {
  207. return !((*this) == a);
  208. }
  209. };
  210. union Instruction {
  211. Instruction& operator=(const Instruction& instr) {
  212. value = instr.value;
  213. return *this;
  214. }
  215. constexpr Instruction(u64 value) : value{value} {}
  216. BitField<0, 8, Register> gpr0;
  217. BitField<8, 8, Register> gpr8;
  218. union {
  219. BitField<16, 4, Pred> full_pred;
  220. BitField<16, 3, u64> pred_index;
  221. } pred;
  222. BitField<19, 1, u64> negate_pred;
  223. BitField<20, 8, Register> gpr20;
  224. BitField<20, 4, SubOp> sub_op;
  225. BitField<28, 8, Register> gpr28;
  226. BitField<39, 8, Register> gpr39;
  227. BitField<48, 16, u64> opcode;
  228. union {
  229. BitField<20, 19, u64> imm20_19;
  230. BitField<20, 32, s64> imm20_32;
  231. BitField<45, 1, u64> negate_b;
  232. BitField<46, 1, u64> abs_a;
  233. BitField<48, 1, u64> negate_a;
  234. BitField<49, 1, u64> abs_b;
  235. BitField<50, 1, u64> saturate_d;
  236. BitField<56, 1, u64> negate_imm;
  237. union {
  238. BitField<39, 3, u64> pred;
  239. BitField<42, 1, u64> negate_pred;
  240. } fmnmx;
  241. union {
  242. BitField<39, 1, u64> invert_a;
  243. BitField<40, 1, u64> invert_b;
  244. BitField<41, 2, LogicOperation> operation;
  245. BitField<44, 2, PredicateResultMode> pred_result_mode;
  246. BitField<48, 3, Pred> pred48;
  247. } lop;
  248. union {
  249. BitField<53, 2, LogicOperation> operation;
  250. BitField<55, 1, u64> invert_a;
  251. BitField<56, 1, u64> invert_b;
  252. } lop32i;
  253. union {
  254. BitField<28, 8, u64> imm_lut28;
  255. BitField<48, 8, u64> imm_lut48;
  256. u32 GetImmLut28() const {
  257. return static_cast<u32>(imm_lut28);
  258. }
  259. u32 GetImmLut48() const {
  260. return static_cast<u32>(imm_lut48);
  261. }
  262. } lop3;
  263. u32 GetImm20_19() const {
  264. u32 imm{static_cast<u32>(imm20_19)};
  265. imm <<= 12;
  266. imm |= negate_imm ? 0x80000000 : 0;
  267. return imm;
  268. }
  269. u32 GetImm20_32() const {
  270. return static_cast<u32>(imm20_32);
  271. }
  272. s32 GetSignedImm20_20() const {
  273. u32 immediate = static_cast<u32>(imm20_19 | (negate_imm << 19));
  274. // Sign extend the 20-bit value.
  275. u32 mask = 1U << (20 - 1);
  276. return static_cast<s32>((immediate ^ mask) - mask);
  277. }
  278. } alu;
  279. union {
  280. BitField<51, 1, u64> saturate;
  281. BitField<52, 2, IpaSampleMode> sample_mode;
  282. BitField<54, 2, IpaInterpMode> interp_mode;
  283. } ipa;
  284. union {
  285. BitField<39, 2, u64> tab5cb8_2;
  286. BitField<41, 3, u64> tab5c68_1;
  287. BitField<44, 2, u64> tab5c68_0;
  288. BitField<47, 1, u64> cc;
  289. BitField<48, 1, u64> negate_b;
  290. } fmul;
  291. union {
  292. BitField<48, 1, u64> is_signed;
  293. } shift;
  294. union {
  295. BitField<39, 5, u64> shift_amount;
  296. BitField<48, 1, u64> negate_b;
  297. BitField<49, 1, u64> negate_a;
  298. } alu_integer;
  299. union {
  300. BitField<40, 1, u64> invert;
  301. } popc;
  302. union {
  303. BitField<39, 3, u64> pred;
  304. BitField<42, 1, u64> neg_pred;
  305. } sel;
  306. union {
  307. BitField<39, 3, u64> pred;
  308. BitField<42, 1, u64> negate_pred;
  309. BitField<43, 2, IMinMaxExchange> exchange;
  310. BitField<48, 1, u64> is_signed;
  311. } imnmx;
  312. union {
  313. BitField<31, 2, IAdd3Height> height_c;
  314. BitField<33, 2, IAdd3Height> height_b;
  315. BitField<35, 2, IAdd3Height> height_a;
  316. BitField<37, 2, IAdd3Mode> mode;
  317. BitField<49, 1, u64> neg_c;
  318. BitField<50, 1, u64> neg_b;
  319. BitField<51, 1, u64> neg_a;
  320. } iadd3;
  321. union {
  322. BitField<54, 1, u64> saturate;
  323. BitField<56, 1, u64> negate_a;
  324. } iadd32i;
  325. union {
  326. BitField<53, 1, u64> negate_b;
  327. BitField<54, 1, u64> abs_a;
  328. BitField<56, 1, u64> negate_a;
  329. BitField<57, 1, u64> abs_b;
  330. } fadd32i;
  331. union {
  332. BitField<20, 8, u64> shift_position;
  333. BitField<28, 8, u64> shift_length;
  334. BitField<48, 1, u64> negate_b;
  335. BitField<49, 1, u64> negate_a;
  336. u64 GetLeftShiftValue() const {
  337. return 32 - (shift_position + shift_length);
  338. }
  339. } bfe;
  340. union {
  341. BitField<0, 5, FlowCondition> cond;
  342. } flow;
  343. union {
  344. BitField<47, 1, u64> cc;
  345. BitField<48, 1, u64> negate_b;
  346. BitField<49, 1, u64> negate_c;
  347. BitField<51, 2, u64> tab5980_1;
  348. BitField<53, 2, u64> tab5980_0;
  349. } ffma;
  350. union {
  351. BitField<48, 3, UniformType> type;
  352. BitField<44, 2, u64> unknown;
  353. } ld_c;
  354. union {
  355. BitField<0, 3, u64> pred0;
  356. BitField<3, 3, u64> pred3;
  357. BitField<7, 1, u64> abs_a;
  358. BitField<39, 3, u64> pred39;
  359. BitField<42, 1, u64> neg_pred;
  360. BitField<43, 1, u64> neg_a;
  361. BitField<44, 1, u64> abs_b;
  362. BitField<45, 2, PredOperation> op;
  363. BitField<47, 1, u64> ftz;
  364. BitField<48, 4, PredCondition> cond;
  365. BitField<56, 1, u64> neg_b;
  366. } fsetp;
  367. union {
  368. BitField<0, 3, u64> pred0;
  369. BitField<3, 3, u64> pred3;
  370. BitField<39, 3, u64> pred39;
  371. BitField<42, 1, u64> neg_pred;
  372. BitField<45, 2, PredOperation> op;
  373. BitField<48, 1, u64> is_signed;
  374. BitField<49, 3, PredCondition> cond;
  375. } isetp;
  376. union {
  377. BitField<0, 3, u64> pred0;
  378. BitField<3, 3, u64> pred3;
  379. BitField<12, 3, u64> pred12;
  380. BitField<15, 1, u64> neg_pred12;
  381. BitField<24, 2, PredOperation> cond;
  382. BitField<29, 3, u64> pred29;
  383. BitField<32, 1, u64> neg_pred29;
  384. BitField<39, 3, u64> pred39;
  385. BitField<42, 1, u64> neg_pred39;
  386. BitField<45, 2, PredOperation> op;
  387. } psetp;
  388. union {
  389. BitField<39, 3, u64> pred39;
  390. BitField<42, 1, u64> neg_pred;
  391. BitField<43, 1, u64> neg_a;
  392. BitField<44, 1, u64> abs_b;
  393. BitField<45, 2, PredOperation> op;
  394. BitField<48, 4, PredCondition> cond;
  395. BitField<52, 1, u64> bf;
  396. BitField<53, 1, u64> neg_b;
  397. BitField<54, 1, u64> abs_a;
  398. BitField<55, 1, u64> ftz;
  399. BitField<56, 1, u64> neg_imm;
  400. } fset;
  401. union {
  402. BitField<39, 3, u64> pred39;
  403. BitField<42, 1, u64> neg_pred;
  404. BitField<44, 1, u64> bf;
  405. BitField<45, 2, PredOperation> op;
  406. BitField<48, 1, u64> is_signed;
  407. BitField<49, 3, PredCondition> cond;
  408. } iset;
  409. union {
  410. BitField<8, 2, Register::Size> dest_size;
  411. BitField<10, 2, Register::Size> src_size;
  412. BitField<12, 1, u64> is_output_signed;
  413. BitField<13, 1, u64> is_input_signed;
  414. BitField<41, 2, u64> selector;
  415. BitField<45, 1, u64> negate_a;
  416. BitField<49, 1, u64> abs_a;
  417. union {
  418. BitField<39, 2, F2iRoundingOp> rounding;
  419. } f2i;
  420. union {
  421. BitField<39, 4, F2fRoundingOp> rounding;
  422. } f2f;
  423. } conversion;
  424. union {
  425. BitField<28, 1, u64> array;
  426. BitField<29, 2, TextureType> texture_type;
  427. BitField<31, 4, u64> component_mask;
  428. bool IsComponentEnabled(size_t component) const {
  429. return ((1ull << component) & component_mask) != 0;
  430. }
  431. } tex;
  432. union {
  433. BitField<28, 1, u64> array;
  434. BitField<29, 2, TextureType> texture_type;
  435. BitField<56, 2, u64> component;
  436. } tld4;
  437. union {
  438. BitField<52, 2, u64> component;
  439. } tld4s;
  440. union {
  441. BitField<0, 8, Register> gpr0;
  442. BitField<28, 8, Register> gpr28;
  443. BitField<49, 1, u64> nodep;
  444. BitField<50, 3, u64> component_mask_selector;
  445. BitField<53, 4, u64> texture_info;
  446. TextureType GetTextureType() const {
  447. // The TEXS instruction has a weird encoding for the texture type.
  448. if (texture_info == 0)
  449. return TextureType::Texture1D;
  450. if (texture_info >= 1 && texture_info <= 9)
  451. return TextureType::Texture2D;
  452. if (texture_info >= 10 && texture_info <= 11)
  453. return TextureType::Texture3D;
  454. if (texture_info >= 12 && texture_info <= 13)
  455. return TextureType::TextureCube;
  456. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}",
  457. static_cast<u32>(texture_info.Value()));
  458. UNREACHABLE();
  459. }
  460. bool IsArrayTexture() const {
  461. // TEXS only supports Texture2D arrays.
  462. return texture_info >= 7 && texture_info <= 9;
  463. }
  464. bool HasTwoDestinations() const {
  465. return gpr28.Value() != Register::ZeroIndex;
  466. }
  467. bool IsComponentEnabled(size_t component) const {
  468. static constexpr std::array<std::array<u32, 8>, 4> mask_lut{{
  469. {},
  470. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  471. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  472. {0x7, 0xb, 0xd, 0xe, 0xf},
  473. }};
  474. size_t index{gpr0.Value() != Register::ZeroIndex ? 1U : 0U};
  475. index |= gpr28.Value() != Register::ZeroIndex ? 2 : 0;
  476. u32 mask = mask_lut[index][component_mask_selector];
  477. // A mask of 0 means this instruction uses an unimplemented mask.
  478. ASSERT(mask != 0);
  479. return ((1ull << component) & mask) != 0;
  480. }
  481. } texs;
  482. union {
  483. BitField<53, 4, u64> texture_info;
  484. TextureType GetTextureType() const {
  485. // The TLDS instruction has a weird encoding for the texture type.
  486. if (texture_info >= 0 && texture_info <= 1) {
  487. return TextureType::Texture1D;
  488. }
  489. if (texture_info == 2 || texture_info == 8 || texture_info == 12 ||
  490. (texture_info >= 4 && texture_info <= 6)) {
  491. return TextureType::Texture2D;
  492. }
  493. if (texture_info == 7) {
  494. return TextureType::Texture3D;
  495. }
  496. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}",
  497. static_cast<u32>(texture_info.Value()));
  498. UNREACHABLE();
  499. }
  500. bool IsArrayTexture() const {
  501. // TEXS only supports Texture2D arrays.
  502. return texture_info == 8;
  503. }
  504. } tlds;
  505. union {
  506. BitField<20, 24, u64> target;
  507. BitField<5, 1, u64> constant_buffer;
  508. s32 GetBranchTarget() const {
  509. // Sign extend the branch target offset
  510. u32 mask = 1U << (24 - 1);
  511. u32 value = static_cast<u32>(target);
  512. // The branch offset is relative to the next instruction and is stored in bytes, so
  513. // divide it by the size of an instruction and add 1 to it.
  514. return static_cast<s32>((value ^ mask) - mask) / sizeof(Instruction) + 1;
  515. }
  516. } bra;
  517. union {
  518. BitField<20, 16, u64> imm20_16;
  519. BitField<36, 1, u64> product_shift_left;
  520. BitField<37, 1, u64> merge_37;
  521. BitField<48, 1, u64> sign_a;
  522. BitField<49, 1, u64> sign_b;
  523. BitField<50, 3, XmadMode> mode;
  524. BitField<52, 1, u64> high_b;
  525. BitField<53, 1, u64> high_a;
  526. BitField<56, 1, u64> merge_56;
  527. } xmad;
  528. union {
  529. BitField<20, 14, u64> offset;
  530. BitField<34, 5, u64> index;
  531. } cbuf34;
  532. union {
  533. BitField<20, 16, s64> offset;
  534. BitField<36, 5, u64> index;
  535. } cbuf36;
  536. BitField<61, 1, u64> is_b_imm;
  537. BitField<60, 1, u64> is_b_gpr;
  538. BitField<59, 1, u64> is_c_gpr;
  539. Attribute attribute;
  540. Sampler sampler;
  541. u64 value;
  542. };
  543. static_assert(sizeof(Instruction) == 0x8, "Incorrect structure size");
  544. static_assert(std::is_standard_layout_v<Instruction>, "Instruction is not standard layout");
  545. class OpCode {
  546. public:
  547. enum class Id {
  548. KIL,
  549. SSY,
  550. SYNC,
  551. DEPBAR,
  552. BFE_C,
  553. BFE_R,
  554. BFE_IMM,
  555. BRA,
  556. LD_A,
  557. LD_C,
  558. ST_A,
  559. LDG, // Load from global memory
  560. STG, // Store in global memory
  561. TEX,
  562. TXQ, // Texture Query
  563. TEXS, // Texture Fetch with scalar/non-vec4 source/destinations
  564. TLDS, // Texture Load with scalar/non-vec4 source/destinations
  565. TLD4, // Texture Load 4
  566. TLD4S, // Texture Load 4 with scalar / non - vec4 source / destinations
  567. EXIT,
  568. IPA,
  569. FFMA_IMM, // Fused Multiply and Add
  570. FFMA_CR,
  571. FFMA_RC,
  572. FFMA_RR,
  573. FADD_C,
  574. FADD_R,
  575. FADD_IMM,
  576. FADD32I,
  577. FMUL_C,
  578. FMUL_R,
  579. FMUL_IMM,
  580. FMUL32_IMM,
  581. IADD_C,
  582. IADD_R,
  583. IADD_IMM,
  584. IADD3_C, // Add 3 Integers
  585. IADD3_R,
  586. IADD3_IMM,
  587. IADD32I,
  588. ISCADD_C, // Scale and Add
  589. ISCADD_R,
  590. ISCADD_IMM,
  591. POPC_C,
  592. POPC_R,
  593. POPC_IMM,
  594. SEL_C,
  595. SEL_R,
  596. SEL_IMM,
  597. MUFU, // Multi-Function Operator
  598. RRO_C, // Range Reduction Operator
  599. RRO_R,
  600. RRO_IMM,
  601. F2F_C,
  602. F2F_R,
  603. F2F_IMM,
  604. F2I_C,
  605. F2I_R,
  606. F2I_IMM,
  607. I2F_C,
  608. I2F_R,
  609. I2F_IMM,
  610. I2I_C,
  611. I2I_R,
  612. I2I_IMM,
  613. LOP_C,
  614. LOP_R,
  615. LOP_IMM,
  616. LOP32I,
  617. LOP3_C,
  618. LOP3_R,
  619. LOP3_IMM,
  620. MOV_C,
  621. MOV_R,
  622. MOV_IMM,
  623. MOV32_IMM,
  624. SHL_C,
  625. SHL_R,
  626. SHL_IMM,
  627. SHR_C,
  628. SHR_R,
  629. SHR_IMM,
  630. FMNMX_C,
  631. FMNMX_R,
  632. FMNMX_IMM,
  633. IMNMX_C,
  634. IMNMX_R,
  635. IMNMX_IMM,
  636. FSETP_C, // Set Predicate
  637. FSETP_R,
  638. FSETP_IMM,
  639. FSET_C,
  640. FSET_R,
  641. FSET_IMM,
  642. ISETP_C,
  643. ISETP_IMM,
  644. ISETP_R,
  645. ISET_R,
  646. ISET_C,
  647. ISET_IMM,
  648. PSETP,
  649. XMAD_IMM,
  650. XMAD_CR,
  651. XMAD_RC,
  652. XMAD_RR,
  653. };
  654. enum class Type {
  655. Trivial,
  656. Arithmetic,
  657. ArithmeticImmediate,
  658. ArithmeticInteger,
  659. ArithmeticIntegerImmediate,
  660. Bfe,
  661. Shift,
  662. Ffma,
  663. Flow,
  664. Synch,
  665. Memory,
  666. FloatSet,
  667. FloatSetPredicate,
  668. IntegerSet,
  669. IntegerSetPredicate,
  670. PredicateSetPredicate,
  671. Conversion,
  672. Xmad,
  673. Unknown,
  674. };
  675. /// Returns whether an opcode has an execution predicate field or not (ie, whether it can be
  676. /// conditionally executed).
  677. static bool IsPredicatedInstruction(Id opcode) {
  678. // TODO(Subv): Add the rest of unpredicated instructions.
  679. return opcode != Id::SSY;
  680. }
  681. class Matcher {
  682. public:
  683. Matcher(const char* const name, u16 mask, u16 expected, OpCode::Id id, OpCode::Type type)
  684. : name{name}, mask{mask}, expected{expected}, id{id}, type{type} {}
  685. const char* GetName() const {
  686. return name;
  687. }
  688. u16 GetMask() const {
  689. return mask;
  690. }
  691. Id GetId() const {
  692. return id;
  693. }
  694. Type GetType() const {
  695. return type;
  696. }
  697. /**
  698. * Tests to see if the given instruction is the instruction this matcher represents.
  699. * @param instruction The instruction to test
  700. * @returns true if the given instruction matches.
  701. */
  702. bool Matches(u16 instruction) const {
  703. return (instruction & mask) == expected;
  704. }
  705. private:
  706. const char* name;
  707. u16 mask;
  708. u16 expected;
  709. Id id;
  710. Type type;
  711. };
  712. static boost::optional<const Matcher&> Decode(Instruction instr) {
  713. static const auto table{GetDecodeTable()};
  714. const auto matches_instruction = [instr](const auto& matcher) {
  715. return matcher.Matches(static_cast<u16>(instr.opcode));
  716. };
  717. auto iter = std::find_if(table.begin(), table.end(), matches_instruction);
  718. return iter != table.end() ? boost::optional<const Matcher&>(*iter) : boost::none;
  719. }
  720. private:
  721. struct Detail {
  722. private:
  723. static constexpr size_t opcode_bitsize = 16;
  724. /**
  725. * Generates the mask and the expected value after masking from a given bitstring.
  726. * A '0' in a bitstring indicates that a zero must be present at that bit position.
  727. * A '1' in a bitstring indicates that a one must be present at that bit position.
  728. */
  729. static auto GetMaskAndExpect(const char* const bitstring) {
  730. u16 mask = 0, expect = 0;
  731. for (size_t i = 0; i < opcode_bitsize; i++) {
  732. const size_t bit_position = opcode_bitsize - i - 1;
  733. switch (bitstring[i]) {
  734. case '0':
  735. mask |= 1 << bit_position;
  736. break;
  737. case '1':
  738. expect |= 1 << bit_position;
  739. mask |= 1 << bit_position;
  740. break;
  741. default:
  742. // Ignore
  743. break;
  744. }
  745. }
  746. return std::make_tuple(mask, expect);
  747. }
  748. public:
  749. /// Creates a matcher that can match and parse instructions based on bitstring.
  750. static auto GetMatcher(const char* const bitstring, OpCode::Id op, OpCode::Type type,
  751. const char* const name) {
  752. const auto mask_expect = GetMaskAndExpect(bitstring);
  753. return Matcher(name, std::get<0>(mask_expect), std::get<1>(mask_expect), op, type);
  754. }
  755. };
  756. static std::vector<Matcher> GetDecodeTable() {
  757. std::vector<Matcher> table = {
  758. #define INST(bitstring, op, type, name) Detail::GetMatcher(bitstring, op, type, name)
  759. INST("111000110011----", Id::KIL, Type::Flow, "KIL"),
  760. INST("111000101001----", Id::SSY, Type::Flow, "SSY"),
  761. INST("111000100100----", Id::BRA, Type::Flow, "BRA"),
  762. INST("1111000011110---", Id::DEPBAR, Type::Synch, "DEPBAR"),
  763. INST("1111000011111---", Id::SYNC, Type::Synch, "SYNC"),
  764. INST("1110111111011---", Id::LD_A, Type::Memory, "LD_A"),
  765. INST("1110111110010---", Id::LD_C, Type::Memory, "LD_C"),
  766. INST("1110111111110---", Id::ST_A, Type::Memory, "ST_A"),
  767. INST("1110111011010---", Id::LDG, Type::Memory, "LDG"),
  768. INST("1110111011011---", Id::STG, Type::Memory, "STG"),
  769. INST("110000----111---", Id::TEX, Type::Memory, "TEX"),
  770. INST("1101111101001---", Id::TXQ, Type::Memory, "TXQ"),
  771. INST("1101100---------", Id::TEXS, Type::Memory, "TEXS"),
  772. INST("1101101---------", Id::TLDS, Type::Memory, "TLDS"),
  773. INST("110010----111---", Id::TLD4, Type::Memory, "TLD4"),
  774. INST("1101111100------", Id::TLD4S, Type::Memory, "TLD4S"),
  775. INST("111000110000----", Id::EXIT, Type::Trivial, "EXIT"),
  776. INST("11100000--------", Id::IPA, Type::Trivial, "IPA"),
  777. INST("0011001-1-------", Id::FFMA_IMM, Type::Ffma, "FFMA_IMM"),
  778. INST("010010011-------", Id::FFMA_CR, Type::Ffma, "FFMA_CR"),
  779. INST("010100011-------", Id::FFMA_RC, Type::Ffma, "FFMA_RC"),
  780. INST("010110011-------", Id::FFMA_RR, Type::Ffma, "FFMA_RR"),
  781. INST("0100110001011---", Id::FADD_C, Type::Arithmetic, "FADD_C"),
  782. INST("0101110001011---", Id::FADD_R, Type::Arithmetic, "FADD_R"),
  783. INST("0011100-01011---", Id::FADD_IMM, Type::Arithmetic, "FADD_IMM"),
  784. INST("000010----------", Id::FADD32I, Type::ArithmeticImmediate, "FADD32I"),
  785. INST("0100110001101---", Id::FMUL_C, Type::Arithmetic, "FMUL_C"),
  786. INST("0101110001101---", Id::FMUL_R, Type::Arithmetic, "FMUL_R"),
  787. INST("0011100-01101---", Id::FMUL_IMM, Type::Arithmetic, "FMUL_IMM"),
  788. INST("00011110--------", Id::FMUL32_IMM, Type::ArithmeticImmediate, "FMUL32_IMM"),
  789. INST("0100110000010---", Id::IADD_C, Type::ArithmeticInteger, "IADD_C"),
  790. INST("0101110000010---", Id::IADD_R, Type::ArithmeticInteger, "IADD_R"),
  791. INST("0011100-00010---", Id::IADD_IMM, Type::ArithmeticInteger, "IADD_IMM"),
  792. INST("010011001100----", Id::IADD3_C, Type::ArithmeticInteger, "IADD3_C"),
  793. INST("010111001100----", Id::IADD3_R, Type::ArithmeticInteger, "IADD3_R"),
  794. INST("0011100-1100----", Id::IADD3_IMM, Type::ArithmeticInteger, "IADD3_IMM"),
  795. INST("0001110---------", Id::IADD32I, Type::ArithmeticIntegerImmediate, "IADD32I"),
  796. INST("0100110000011---", Id::ISCADD_C, Type::ArithmeticInteger, "ISCADD_C"),
  797. INST("0101110000011---", Id::ISCADD_R, Type::ArithmeticInteger, "ISCADD_R"),
  798. INST("0011100-00011---", Id::ISCADD_IMM, Type::ArithmeticInteger, "ISCADD_IMM"),
  799. INST("0100110000001---", Id::POPC_C, Type::ArithmeticInteger, "POPC_C"),
  800. INST("0101110000001---", Id::POPC_R, Type::ArithmeticInteger, "POPC_R"),
  801. INST("0011100-00001---", Id::POPC_IMM, Type::ArithmeticInteger, "POPC_IMM"),
  802. INST("0100110010100---", Id::SEL_C, Type::ArithmeticInteger, "SEL_C"),
  803. INST("0101110010100---", Id::SEL_R, Type::ArithmeticInteger, "SEL_R"),
  804. INST("0011100-10100---", Id::SEL_IMM, Type::ArithmeticInteger, "SEL_IMM"),
  805. INST("0101000010000---", Id::MUFU, Type::Arithmetic, "MUFU"),
  806. INST("0100110010010---", Id::RRO_C, Type::Arithmetic, "RRO_C"),
  807. INST("0101110010010---", Id::RRO_R, Type::Arithmetic, "RRO_R"),
  808. INST("0011100-10010---", Id::RRO_IMM, Type::Arithmetic, "RRO_IMM"),
  809. INST("0100110010101---", Id::F2F_C, Type::Conversion, "F2F_C"),
  810. INST("0101110010101---", Id::F2F_R, Type::Conversion, "F2F_R"),
  811. INST("0011100-10101---", Id::F2F_IMM, Type::Conversion, "F2F_IMM"),
  812. INST("0100110010110---", Id::F2I_C, Type::Conversion, "F2I_C"),
  813. INST("0101110010110---", Id::F2I_R, Type::Conversion, "F2I_R"),
  814. INST("0011100-10110---", Id::F2I_IMM, Type::Conversion, "F2I_IMM"),
  815. INST("0100110010011---", Id::MOV_C, Type::Arithmetic, "MOV_C"),
  816. INST("0101110010011---", Id::MOV_R, Type::Arithmetic, "MOV_R"),
  817. INST("0011100-10011---", Id::MOV_IMM, Type::Arithmetic, "MOV_IMM"),
  818. INST("000000010000----", Id::MOV32_IMM, Type::ArithmeticImmediate, "MOV32_IMM"),
  819. INST("0100110001100---", Id::FMNMX_C, Type::Arithmetic, "FMNMX_C"),
  820. INST("0101110001100---", Id::FMNMX_R, Type::Arithmetic, "FMNMX_R"),
  821. INST("0011100-01100---", Id::FMNMX_IMM, Type::Arithmetic, "FMNMX_IMM"),
  822. INST("0100110000100---", Id::IMNMX_C, Type::ArithmeticInteger, "IMNMX_C"),
  823. INST("0101110000100---", Id::IMNMX_R, Type::ArithmeticInteger, "IMNMX_R"),
  824. INST("0011100-00100---", Id::IMNMX_IMM, Type::ArithmeticInteger, "IMNMX_IMM"),
  825. INST("0100110000000---", Id::BFE_C, Type::Bfe, "BFE_C"),
  826. INST("0101110000000---", Id::BFE_R, Type::Bfe, "BFE_R"),
  827. INST("0011100-00000---", Id::BFE_IMM, Type::Bfe, "BFE_IMM"),
  828. INST("0100110001000---", Id::LOP_C, Type::ArithmeticInteger, "LOP_C"),
  829. INST("0101110001000---", Id::LOP_R, Type::ArithmeticInteger, "LOP_R"),
  830. INST("0011100001000---", Id::LOP_IMM, Type::ArithmeticInteger, "LOP_IMM"),
  831. INST("000001----------", Id::LOP32I, Type::ArithmeticIntegerImmediate, "LOP32I"),
  832. INST("0000001---------", Id::LOP3_C, Type::ArithmeticInteger, "LOP3_C"),
  833. INST("0101101111100---", Id::LOP3_R, Type::ArithmeticInteger, "LOP3_R"),
  834. INST("0011110---------", Id::LOP3_IMM, Type::ArithmeticInteger, "LOP3_IMM"),
  835. INST("0100110001001---", Id::SHL_C, Type::Shift, "SHL_C"),
  836. INST("0101110001001---", Id::SHL_R, Type::Shift, "SHL_R"),
  837. INST("0011100-01001---", Id::SHL_IMM, Type::Shift, "SHL_IMM"),
  838. INST("0100110000101---", Id::SHR_C, Type::Shift, "SHR_C"),
  839. INST("0101110000101---", Id::SHR_R, Type::Shift, "SHR_R"),
  840. INST("0011100-00101---", Id::SHR_IMM, Type::Shift, "SHR_IMM"),
  841. INST("0100110011100---", Id::I2I_C, Type::Conversion, "I2I_C"),
  842. INST("0101110011100---", Id::I2I_R, Type::Conversion, "I2I_R"),
  843. INST("01110001-1000---", Id::I2I_IMM, Type::Conversion, "I2I_IMM"),
  844. INST("0100110010111---", Id::I2F_C, Type::Conversion, "I2F_C"),
  845. INST("0101110010111---", Id::I2F_R, Type::Conversion, "I2F_R"),
  846. INST("0011100-10111---", Id::I2F_IMM, Type::Conversion, "I2F_IMM"),
  847. INST("01011000--------", Id::FSET_R, Type::FloatSet, "FSET_R"),
  848. INST("0100100---------", Id::FSET_C, Type::FloatSet, "FSET_C"),
  849. INST("0011000---------", Id::FSET_IMM, Type::FloatSet, "FSET_IMM"),
  850. INST("010010111011----", Id::FSETP_C, Type::FloatSetPredicate, "FSETP_C"),
  851. INST("010110111011----", Id::FSETP_R, Type::FloatSetPredicate, "FSETP_R"),
  852. INST("0011011-1011----", Id::FSETP_IMM, Type::FloatSetPredicate, "FSETP_IMM"),
  853. INST("010010110110----", Id::ISETP_C, Type::IntegerSetPredicate, "ISETP_C"),
  854. INST("010110110110----", Id::ISETP_R, Type::IntegerSetPredicate, "ISETP_R"),
  855. INST("0011011-0110----", Id::ISETP_IMM, Type::IntegerSetPredicate, "ISETP_IMM"),
  856. INST("010110110101----", Id::ISET_R, Type::IntegerSet, "ISET_R"),
  857. INST("010010110101----", Id::ISET_C, Type::IntegerSet, "ISET_C"),
  858. INST("0011011-0101----", Id::ISET_IMM, Type::IntegerSet, "ISET_IMM"),
  859. INST("0101000010010---", Id::PSETP, Type::PredicateSetPredicate, "PSETP"),
  860. INST("0011011-00------", Id::XMAD_IMM, Type::Xmad, "XMAD_IMM"),
  861. INST("0100111---------", Id::XMAD_CR, Type::Xmad, "XMAD_CR"),
  862. INST("010100010-------", Id::XMAD_RC, Type::Xmad, "XMAD_RC"),
  863. INST("0101101100------", Id::XMAD_RR, Type::Xmad, "XMAD_RR"),
  864. };
  865. #undef INST
  866. std::stable_sort(table.begin(), table.end(), [](const auto& a, const auto& b) {
  867. // If a matcher has more bits in its mask it is more specific, so it
  868. // should come first.
  869. return std::bitset<16>(a.GetMask()).count() > std::bitset<16>(b.GetMask()).count();
  870. });
  871. return table;
  872. }
  873. };
  874. } // namespace Tegra::Shader