shader_bytecode.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  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 <optional>
  7. #include <string>
  8. #include <tuple>
  9. #include <vector>
  10. #include "common/assert.h"
  11. #include "common/bit_field.h"
  12. #include "common/common_types.h"
  13. namespace Tegra::Shader {
  14. struct Register {
  15. /// Number of registers
  16. static constexpr std::size_t NumRegisters = 256;
  17. /// Register 255 is special cased to always be 0
  18. static constexpr std::size_t ZeroIndex = 255;
  19. enum class Size : u64 {
  20. Byte = 0,
  21. Short = 1,
  22. Word = 2,
  23. Long = 3,
  24. };
  25. constexpr Register() = default;
  26. constexpr Register(u64 value) : value(value) {}
  27. constexpr operator u64() const {
  28. return value;
  29. }
  30. template <typename T>
  31. constexpr u64 operator-(const T& oth) const {
  32. return value - oth;
  33. }
  34. template <typename T>
  35. constexpr u64 operator&(const T& oth) const {
  36. return value & oth;
  37. }
  38. constexpr u64 operator&(const Register& oth) const {
  39. return value & oth.value;
  40. }
  41. constexpr u64 operator~() const {
  42. return ~value;
  43. }
  44. u64 GetSwizzledIndex(u64 elem) const {
  45. elem = (value + elem) & 3;
  46. return (value & ~3) + elem;
  47. }
  48. private:
  49. u64 value{};
  50. };
  51. enum class AttributeSize : u64 {
  52. Word = 0,
  53. DoubleWord = 1,
  54. TripleWord = 2,
  55. QuadWord = 3,
  56. };
  57. union Attribute {
  58. Attribute() = default;
  59. constexpr explicit Attribute(u64 value) : value(value) {}
  60. enum class Index : u64 {
  61. PointSize = 6,
  62. Position = 7,
  63. Attribute_0 = 8,
  64. Attribute_31 = 39,
  65. PointCoord = 46,
  66. // This attribute contains a tuple of (~, ~, InstanceId, VertexId) when inside a vertex
  67. // shader, and a tuple of (TessCoord.x, TessCoord.y, TessCoord.z, ~) when inside a Tess Eval
  68. // shader.
  69. TessCoordInstanceIDVertexID = 47,
  70. // This attribute contains a tuple of (Unk, Unk, Unk, gl_FrontFacing) when inside a fragment
  71. // shader. It is unknown what the other values contain.
  72. FrontFacing = 63,
  73. };
  74. union {
  75. BitField<20, 10, u64> immediate;
  76. BitField<22, 2, u64> element;
  77. BitField<24, 6, Index> index;
  78. BitField<47, 3, AttributeSize> size;
  79. } fmt20;
  80. union {
  81. BitField<30, 2, u64> element;
  82. BitField<32, 6, Index> index;
  83. } fmt28;
  84. BitField<39, 8, u64> reg;
  85. u64 value{};
  86. };
  87. union Sampler {
  88. Sampler() = default;
  89. constexpr explicit Sampler(u64 value) : value(value) {}
  90. enum class Index : u64 {
  91. Sampler_0 = 8,
  92. };
  93. BitField<36, 13, Index> index;
  94. u64 value{};
  95. };
  96. } // namespace Tegra::Shader
  97. namespace std {
  98. // TODO(bunnei): The below is forbidden by the C++ standard, but works fine. See #330.
  99. template <>
  100. struct make_unsigned<Tegra::Shader::Attribute> {
  101. using type = Tegra::Shader::Attribute;
  102. };
  103. template <>
  104. struct make_unsigned<Tegra::Shader::Register> {
  105. using type = Tegra::Shader::Register;
  106. };
  107. } // namespace std
  108. namespace Tegra::Shader {
  109. enum class Pred : u64 {
  110. UnusedIndex = 0x7,
  111. NeverExecute = 0xF,
  112. };
  113. enum class PredCondition : u64 {
  114. LessThan = 1,
  115. Equal = 2,
  116. LessEqual = 3,
  117. GreaterThan = 4,
  118. NotEqual = 5,
  119. GreaterEqual = 6,
  120. LessThanWithNan = 9,
  121. GreaterThanWithNan = 12,
  122. NotEqualWithNan = 13,
  123. GreaterEqualWithNan = 14,
  124. // TODO(Subv): Other condition types
  125. };
  126. enum class PredOperation : u64 {
  127. And = 0,
  128. Or = 1,
  129. Xor = 2,
  130. };
  131. enum class LogicOperation : u64 {
  132. And = 0,
  133. Or = 1,
  134. Xor = 2,
  135. PassB = 3,
  136. };
  137. enum class SubOp : u64 {
  138. Cos = 0x0,
  139. Sin = 0x1,
  140. Ex2 = 0x2,
  141. Lg2 = 0x3,
  142. Rcp = 0x4,
  143. Rsq = 0x5,
  144. Sqrt = 0x8,
  145. };
  146. enum class F2iRoundingOp : u64 {
  147. None = 0,
  148. Floor = 1,
  149. Ceil = 2,
  150. Trunc = 3,
  151. };
  152. enum class F2fRoundingOp : u64 {
  153. None = 0,
  154. Pass = 3,
  155. Round = 8,
  156. Floor = 9,
  157. Ceil = 10,
  158. Trunc = 11,
  159. };
  160. enum class UniformType : u64 {
  161. UnsignedByte = 0,
  162. SignedByte = 1,
  163. UnsignedShort = 2,
  164. SignedShort = 3,
  165. Single = 4,
  166. Double = 5,
  167. };
  168. enum class StoreType : u64 {
  169. Unsigned8 = 0,
  170. Signed8 = 1,
  171. Unsigned16 = 2,
  172. Signed16 = 3,
  173. Bytes32 = 4,
  174. Bytes64 = 5,
  175. Bytes128 = 6,
  176. };
  177. enum class IMinMaxExchange : u64 {
  178. None = 0,
  179. XLo = 1,
  180. XMed = 2,
  181. XHi = 3,
  182. };
  183. enum class VideoType : u64 {
  184. Size16_Low = 0,
  185. Size16_High = 1,
  186. Size32 = 2,
  187. Invalid = 3,
  188. };
  189. enum class VmadShr : u64 {
  190. Shr7 = 1,
  191. Shr15 = 2,
  192. };
  193. enum class XmadMode : u64 {
  194. None = 0,
  195. CLo = 1,
  196. CHi = 2,
  197. CSfu = 3,
  198. CBcc = 4,
  199. };
  200. enum class IAdd3Mode : u64 {
  201. None = 0,
  202. RightShift = 1,
  203. LeftShift = 2,
  204. };
  205. enum class IAdd3Height : u64 {
  206. None = 0,
  207. LowerHalfWord = 1,
  208. UpperHalfWord = 2,
  209. };
  210. enum class FlowCondition : u64 {
  211. Always = 0xF,
  212. Fcsm_Tr = 0x1C, // TODO(bunnei): What is this used for?
  213. };
  214. enum class ConditionCode : u64 {
  215. F = 0,
  216. LT = 1,
  217. EQ = 2,
  218. LE = 3,
  219. GT = 4,
  220. NE = 5,
  221. GE = 6,
  222. Num = 7,
  223. Nan = 8,
  224. LTU = 9,
  225. EQU = 10,
  226. LEU = 11,
  227. GTU = 12,
  228. NEU = 13,
  229. GEU = 14,
  230. T = 15,
  231. OFF = 16,
  232. LO = 17,
  233. SFF = 18,
  234. LS = 19,
  235. HI = 20,
  236. SFT = 21,
  237. HS = 22,
  238. OFT = 23,
  239. CSM_TA = 24,
  240. CSM_TR = 25,
  241. CSM_MX = 26,
  242. FCSM_TA = 27,
  243. FCSM_TR = 28,
  244. FCSM_MX = 29,
  245. RLE = 30,
  246. RGT = 31,
  247. };
  248. enum class PredicateResultMode : u64 {
  249. None = 0x0,
  250. NotZero = 0x3,
  251. };
  252. enum class TextureType : u64 {
  253. Texture1D = 0,
  254. Texture2D = 1,
  255. Texture3D = 2,
  256. TextureCube = 3,
  257. };
  258. enum class TextureQueryType : u64 {
  259. Dimension = 1,
  260. TextureType = 2,
  261. SamplePosition = 5,
  262. Filter = 16,
  263. LevelOfDetail = 18,
  264. Wrap = 20,
  265. BorderColor = 22,
  266. };
  267. enum class TextureProcessMode : u64 {
  268. None = 0,
  269. LZ = 1, // Unknown, appears to be the same as none.
  270. LB = 2, // Load Bias.
  271. LL = 3, // Load LOD (LevelOfDetail)
  272. LBA = 6, // Load Bias. The A is unknown, does not appear to differ with LB
  273. LLA = 7 // Load LOD. The A is unknown, does not appear to differ with LL
  274. };
  275. enum class TextureMiscMode : u64 {
  276. DC,
  277. AOFFI, // Uses Offset
  278. NDV,
  279. NODEP,
  280. MZ,
  281. PTP,
  282. };
  283. enum class IsberdMode : u64 {
  284. None = 0,
  285. Patch = 1,
  286. Prim = 2,
  287. Attr = 3,
  288. };
  289. enum class IsberdShift : u64 { None = 0, U16 = 1, B32 = 2 };
  290. enum class HalfType : u64 {
  291. H0_H1 = 0,
  292. F32 = 1,
  293. H0_H0 = 2,
  294. H1_H1 = 3,
  295. };
  296. enum class HalfMerge : u64 {
  297. H0_H1 = 0,
  298. F32 = 1,
  299. Mrg_H0 = 2,
  300. Mrg_H1 = 3,
  301. };
  302. enum class HalfPrecision : u64 {
  303. None = 0,
  304. FTZ = 1,
  305. FMZ = 2,
  306. };
  307. enum class IpaInterpMode : u64 {
  308. Linear = 0,
  309. Perspective = 1,
  310. Flat = 2,
  311. Sc = 3,
  312. };
  313. enum class IpaSampleMode : u64 {
  314. Default = 0,
  315. Centroid = 1,
  316. Offset = 2,
  317. };
  318. struct IpaMode {
  319. IpaInterpMode interpolation_mode;
  320. IpaSampleMode sampling_mode;
  321. bool operator==(const IpaMode& a) const {
  322. return std::tie(interpolation_mode, sampling_mode) ==
  323. std::tie(a.interpolation_mode, a.sampling_mode);
  324. }
  325. bool operator!=(const IpaMode& a) const {
  326. return !operator==(a);
  327. }
  328. };
  329. enum class SystemVariable : u64 {
  330. LaneId = 0x00,
  331. VirtCfg = 0x02,
  332. VirtId = 0x03,
  333. Pm0 = 0x04,
  334. Pm1 = 0x05,
  335. Pm2 = 0x06,
  336. Pm3 = 0x07,
  337. Pm4 = 0x08,
  338. Pm5 = 0x09,
  339. Pm6 = 0x0a,
  340. Pm7 = 0x0b,
  341. OrderingTicket = 0x0f,
  342. PrimType = 0x10,
  343. InvocationId = 0x11,
  344. Ydirection = 0x12,
  345. ThreadKill = 0x13,
  346. ShaderType = 0x14,
  347. DirectBeWriteAddressLow = 0x15,
  348. DirectBeWriteAddressHigh = 0x16,
  349. DirectBeWriteEnabled = 0x17,
  350. MachineId0 = 0x18,
  351. MachineId1 = 0x19,
  352. MachineId2 = 0x1a,
  353. MachineId3 = 0x1b,
  354. Affinity = 0x1c,
  355. InvocationInfo = 0x1d,
  356. WscaleFactorXY = 0x1e,
  357. WscaleFactorZ = 0x1f,
  358. Tid = 0x20,
  359. TidX = 0x21,
  360. TidY = 0x22,
  361. TidZ = 0x23,
  362. CtaParam = 0x24,
  363. CtaIdX = 0x25,
  364. CtaIdY = 0x26,
  365. CtaIdZ = 0x27,
  366. NtId = 0x28,
  367. CirQueueIncrMinusOne = 0x29,
  368. Nlatc = 0x2a,
  369. SmSpaVersion = 0x2c,
  370. MultiPassShaderInfo = 0x2d,
  371. LwinHi = 0x2e,
  372. SwinHi = 0x2f,
  373. SwinLo = 0x30,
  374. SwinSz = 0x31,
  375. SmemSz = 0x32,
  376. SmemBanks = 0x33,
  377. LwinLo = 0x34,
  378. LwinSz = 0x35,
  379. LmemLosz = 0x36,
  380. LmemHioff = 0x37,
  381. EqMask = 0x38,
  382. LtMask = 0x39,
  383. LeMask = 0x3a,
  384. GtMask = 0x3b,
  385. GeMask = 0x3c,
  386. RegAlloc = 0x3d,
  387. CtxAddr = 0x3e, // .fmask = F_SM50
  388. BarrierAlloc = 0x3e, // .fmask = F_SM60
  389. GlobalErrorStatus = 0x40,
  390. WarpErrorStatus = 0x42,
  391. WarpErrorStatusClear = 0x43,
  392. PmHi0 = 0x48,
  393. PmHi1 = 0x49,
  394. PmHi2 = 0x4a,
  395. PmHi3 = 0x4b,
  396. PmHi4 = 0x4c,
  397. PmHi5 = 0x4d,
  398. PmHi6 = 0x4e,
  399. PmHi7 = 0x4f,
  400. ClockLo = 0x50,
  401. ClockHi = 0x51,
  402. GlobalTimerLo = 0x52,
  403. GlobalTimerHi = 0x53,
  404. HwTaskId = 0x60,
  405. CircularQueueEntryIndex = 0x61,
  406. CircularQueueEntryAddressLow = 0x62,
  407. CircularQueueEntryAddressHigh = 0x63,
  408. };
  409. union Instruction {
  410. Instruction& operator=(const Instruction& instr) {
  411. value = instr.value;
  412. return *this;
  413. }
  414. constexpr Instruction(u64 value) : value{value} {}
  415. BitField<0, 8, Register> gpr0;
  416. BitField<8, 8, Register> gpr8;
  417. union {
  418. BitField<16, 4, Pred> full_pred;
  419. BitField<16, 3, u64> pred_index;
  420. } pred;
  421. BitField<19, 1, u64> negate_pred;
  422. BitField<20, 8, Register> gpr20;
  423. BitField<20, 4, SubOp> sub_op;
  424. BitField<28, 8, Register> gpr28;
  425. BitField<39, 8, Register> gpr39;
  426. BitField<48, 16, u64> opcode;
  427. union {
  428. BitField<20, 16, u64> imm20_16;
  429. BitField<20, 19, u64> imm20_19;
  430. BitField<20, 32, s64> imm20_32;
  431. BitField<45, 1, u64> negate_b;
  432. BitField<46, 1, u64> abs_a;
  433. BitField<48, 1, u64> negate_a;
  434. BitField<49, 1, u64> abs_b;
  435. BitField<50, 1, u64> saturate_d;
  436. BitField<56, 1, u64> negate_imm;
  437. union {
  438. BitField<39, 3, u64> pred;
  439. BitField<42, 1, u64> negate_pred;
  440. } fmnmx;
  441. union {
  442. BitField<39, 1, u64> invert_a;
  443. BitField<40, 1, u64> invert_b;
  444. BitField<41, 2, LogicOperation> operation;
  445. BitField<44, 2, PredicateResultMode> pred_result_mode;
  446. BitField<48, 3, Pred> pred48;
  447. } lop;
  448. union {
  449. BitField<53, 2, LogicOperation> operation;
  450. BitField<55, 1, u64> invert_a;
  451. BitField<56, 1, u64> invert_b;
  452. } lop32i;
  453. union {
  454. BitField<28, 8, u64> imm_lut28;
  455. BitField<48, 8, u64> imm_lut48;
  456. u32 GetImmLut28() const {
  457. return static_cast<u32>(imm_lut28);
  458. }
  459. u32 GetImmLut48() const {
  460. return static_cast<u32>(imm_lut48);
  461. }
  462. } lop3;
  463. u16 GetImm20_16() const {
  464. return static_cast<u16>(imm20_16);
  465. }
  466. u32 GetImm20_19() const {
  467. u32 imm{static_cast<u32>(imm20_19)};
  468. imm <<= 12;
  469. imm |= negate_imm ? 0x80000000 : 0;
  470. return imm;
  471. }
  472. u32 GetImm20_32() const {
  473. return static_cast<u32>(imm20_32);
  474. }
  475. s32 GetSignedImm20_20() const {
  476. u32 immediate = static_cast<u32>(imm20_19 | (negate_imm << 19));
  477. // Sign extend the 20-bit value.
  478. u32 mask = 1U << (20 - 1);
  479. return static_cast<s32>((immediate ^ mask) - mask);
  480. }
  481. } alu;
  482. union {
  483. BitField<51, 1, u64> saturate;
  484. BitField<52, 2, IpaSampleMode> sample_mode;
  485. BitField<54, 2, IpaInterpMode> interp_mode;
  486. } ipa;
  487. union {
  488. BitField<39, 2, u64> tab5cb8_2;
  489. BitField<41, 3, u64> tab5c68_1;
  490. BitField<44, 2, u64> tab5c68_0;
  491. BitField<48, 1, u64> negate_b;
  492. } fmul;
  493. union {
  494. BitField<55, 1, u64> saturate;
  495. } fmul32;
  496. union {
  497. BitField<52, 1, u64> generates_cc;
  498. } op_32;
  499. union {
  500. BitField<48, 1, u64> is_signed;
  501. } shift;
  502. union {
  503. BitField<39, 5, u64> shift_amount;
  504. BitField<48, 1, u64> negate_b;
  505. BitField<49, 1, u64> negate_a;
  506. } alu_integer;
  507. union {
  508. BitField<39, 1, u64> ftz;
  509. BitField<32, 1, u64> saturate;
  510. BitField<49, 2, HalfMerge> merge;
  511. BitField<43, 1, u64> negate_a;
  512. BitField<44, 1, u64> abs_a;
  513. BitField<47, 2, HalfType> type_a;
  514. BitField<31, 1, u64> negate_b;
  515. BitField<30, 1, u64> abs_b;
  516. BitField<47, 2, HalfType> type_b;
  517. BitField<35, 2, HalfType> type_c;
  518. } alu_half;
  519. union {
  520. BitField<39, 2, HalfPrecision> precision;
  521. BitField<39, 1, u64> ftz;
  522. BitField<52, 1, u64> saturate;
  523. BitField<49, 2, HalfMerge> merge;
  524. BitField<43, 1, u64> negate_a;
  525. BitField<44, 1, u64> abs_a;
  526. BitField<47, 2, HalfType> type_a;
  527. } alu_half_imm;
  528. union {
  529. BitField<29, 1, u64> first_negate;
  530. BitField<20, 9, u64> first;
  531. BitField<56, 1, u64> second_negate;
  532. BitField<30, 9, u64> second;
  533. u32 PackImmediates() const {
  534. // Immediates are half floats shifted.
  535. constexpr u32 imm_shift = 6;
  536. return static_cast<u32>((first << imm_shift) | (second << (16 + imm_shift)));
  537. }
  538. } half_imm;
  539. union {
  540. union {
  541. BitField<37, 2, HalfPrecision> precision;
  542. BitField<32, 1, u64> saturate;
  543. BitField<30, 1, u64> negate_c;
  544. BitField<35, 2, HalfType> type_c;
  545. } rr;
  546. BitField<57, 2, HalfPrecision> precision;
  547. BitField<52, 1, u64> saturate;
  548. BitField<49, 2, HalfMerge> merge;
  549. BitField<47, 2, HalfType> type_a;
  550. BitField<56, 1, u64> negate_b;
  551. BitField<28, 2, HalfType> type_b;
  552. BitField<51, 1, u64> negate_c;
  553. BitField<53, 2, HalfType> type_reg39;
  554. } hfma2;
  555. union {
  556. BitField<40, 1, u64> invert;
  557. } popc;
  558. union {
  559. BitField<39, 3, u64> pred;
  560. BitField<42, 1, u64> neg_pred;
  561. } sel;
  562. union {
  563. BitField<39, 3, u64> pred;
  564. BitField<42, 1, u64> negate_pred;
  565. BitField<43, 2, IMinMaxExchange> exchange;
  566. BitField<48, 1, u64> is_signed;
  567. } imnmx;
  568. union {
  569. BitField<31, 2, IAdd3Height> height_c;
  570. BitField<33, 2, IAdd3Height> height_b;
  571. BitField<35, 2, IAdd3Height> height_a;
  572. BitField<37, 2, IAdd3Mode> mode;
  573. BitField<49, 1, u64> neg_c;
  574. BitField<50, 1, u64> neg_b;
  575. BitField<51, 1, u64> neg_a;
  576. } iadd3;
  577. union {
  578. BitField<54, 1, u64> saturate;
  579. BitField<56, 1, u64> negate_a;
  580. } iadd32i;
  581. union {
  582. BitField<53, 1, u64> negate_b;
  583. BitField<54, 1, u64> abs_a;
  584. BitField<56, 1, u64> negate_a;
  585. BitField<57, 1, u64> abs_b;
  586. } fadd32i;
  587. union {
  588. BitField<20, 8, u64> shift_position;
  589. BitField<28, 8, u64> shift_length;
  590. BitField<48, 1, u64> negate_b;
  591. BitField<49, 1, u64> negate_a;
  592. u64 GetLeftShiftValue() const {
  593. return 32 - (shift_position + shift_length);
  594. }
  595. } bfe;
  596. union {
  597. BitField<48, 3, u64> pred48;
  598. union {
  599. BitField<20, 20, u64> entry_a;
  600. BitField<39, 5, u64> entry_b;
  601. BitField<45, 1, u64> neg;
  602. BitField<46, 1, u64> uses_cc;
  603. } imm;
  604. union {
  605. BitField<20, 14, u64> cb_index;
  606. BitField<34, 5, u64> cb_offset;
  607. BitField<56, 1, u64> neg;
  608. BitField<57, 1, u64> uses_cc;
  609. } hi;
  610. union {
  611. BitField<20, 14, u64> cb_index;
  612. BitField<34, 5, u64> cb_offset;
  613. BitField<39, 5, u64> entry_a;
  614. BitField<45, 1, u64> neg;
  615. BitField<46, 1, u64> uses_cc;
  616. } rz;
  617. union {
  618. BitField<39, 5, u64> entry_a;
  619. BitField<45, 1, u64> neg;
  620. BitField<46, 1, u64> uses_cc;
  621. } r1;
  622. union {
  623. BitField<28, 8, u64> entry_a;
  624. BitField<37, 1, u64> neg;
  625. BitField<38, 1, u64> uses_cc;
  626. } r2;
  627. } lea;
  628. union {
  629. BitField<0, 5, FlowCondition> cond;
  630. } flow;
  631. union {
  632. BitField<47, 1, u64> cc;
  633. BitField<48, 1, u64> negate_b;
  634. BitField<49, 1, u64> negate_c;
  635. BitField<51, 2, u64> tab5980_1;
  636. BitField<53, 2, u64> tab5980_0;
  637. } ffma;
  638. union {
  639. BitField<48, 3, UniformType> type;
  640. BitField<44, 2, u64> unknown;
  641. } ld_c;
  642. union {
  643. BitField<48, 3, StoreType> type;
  644. } ldst_sl;
  645. union {
  646. BitField<44, 2, u64> unknown;
  647. } ld_l;
  648. union {
  649. BitField<44, 2, u64> unknown;
  650. } st_l;
  651. union {
  652. BitField<0, 3, u64> pred0;
  653. BitField<3, 3, u64> pred3;
  654. BitField<7, 1, u64> abs_a;
  655. BitField<39, 3, u64> pred39;
  656. BitField<42, 1, u64> neg_pred;
  657. BitField<43, 1, u64> neg_a;
  658. BitField<44, 1, u64> abs_b;
  659. BitField<45, 2, PredOperation> op;
  660. BitField<47, 1, u64> ftz;
  661. BitField<48, 4, PredCondition> cond;
  662. } fsetp;
  663. union {
  664. BitField<0, 3, u64> pred0;
  665. BitField<3, 3, u64> pred3;
  666. BitField<39, 3, u64> pred39;
  667. BitField<42, 1, u64> neg_pred;
  668. BitField<45, 2, PredOperation> op;
  669. BitField<48, 1, u64> is_signed;
  670. BitField<49, 3, PredCondition> cond;
  671. } isetp;
  672. union {
  673. BitField<0, 3, u64> pred0;
  674. BitField<3, 3, u64> pred3;
  675. BitField<12, 3, u64> pred12;
  676. BitField<15, 1, u64> neg_pred12;
  677. BitField<24, 2, PredOperation> cond;
  678. BitField<29, 3, u64> pred29;
  679. BitField<32, 1, u64> neg_pred29;
  680. BitField<39, 3, u64> pred39;
  681. BitField<42, 1, u64> neg_pred39;
  682. BitField<45, 2, PredOperation> op;
  683. } psetp;
  684. union {
  685. BitField<43, 4, PredCondition> cond;
  686. BitField<45, 2, PredOperation> op;
  687. BitField<3, 3, u64> pred3;
  688. BitField<0, 3, u64> pred0;
  689. BitField<39, 3, u64> pred39;
  690. } vsetp;
  691. union {
  692. BitField<12, 3, u64> pred12;
  693. BitField<15, 1, u64> neg_pred12;
  694. BitField<24, 2, PredOperation> cond;
  695. BitField<29, 3, u64> pred29;
  696. BitField<32, 1, u64> neg_pred29;
  697. BitField<39, 3, u64> pred39;
  698. BitField<42, 1, u64> neg_pred39;
  699. BitField<44, 1, u64> bf;
  700. BitField<45, 2, PredOperation> op;
  701. } pset;
  702. union {
  703. BitField<0, 3, u64> pred0;
  704. BitField<3, 3, u64> pred3;
  705. BitField<8, 5, ConditionCode> cc; // flag in cc
  706. BitField<39, 3, u64> pred39;
  707. BitField<42, 1, u64> neg_pred39;
  708. BitField<45, 4, PredOperation> op; // op with pred39
  709. } csetp;
  710. union {
  711. BitField<35, 4, PredCondition> cond;
  712. BitField<49, 1, u64> h_and;
  713. BitField<6, 1, u64> ftz;
  714. BitField<45, 2, PredOperation> op;
  715. BitField<3, 3, u64> pred3;
  716. BitField<0, 3, u64> pred0;
  717. BitField<43, 1, u64> negate_a;
  718. BitField<44, 1, u64> abs_a;
  719. BitField<47, 2, HalfType> type_a;
  720. BitField<31, 1, u64> negate_b;
  721. BitField<30, 1, u64> abs_b;
  722. BitField<28, 2, HalfType> type_b;
  723. BitField<42, 1, u64> neg_pred;
  724. BitField<39, 3, u64> pred39;
  725. } hsetp2;
  726. union {
  727. BitField<39, 3, u64> pred39;
  728. BitField<42, 1, u64> neg_pred;
  729. BitField<43, 1, u64> neg_a;
  730. BitField<44, 1, u64> abs_b;
  731. BitField<45, 2, PredOperation> op;
  732. BitField<48, 4, PredCondition> cond;
  733. BitField<52, 1, u64> bf;
  734. BitField<53, 1, u64> neg_b;
  735. BitField<54, 1, u64> abs_a;
  736. BitField<55, 1, u64> ftz;
  737. } fset;
  738. union {
  739. BitField<49, 1, u64> bf;
  740. BitField<35, 3, PredCondition> cond;
  741. BitField<50, 1, u64> ftz;
  742. BitField<45, 2, PredOperation> op;
  743. BitField<43, 1, u64> negate_a;
  744. BitField<44, 1, u64> abs_a;
  745. BitField<47, 2, HalfType> type_a;
  746. BitField<31, 1, u64> negate_b;
  747. BitField<30, 1, u64> abs_b;
  748. BitField<28, 2, HalfType> type_b;
  749. BitField<42, 1, u64> neg_pred;
  750. BitField<39, 3, u64> pred39;
  751. } hset2;
  752. union {
  753. BitField<39, 3, u64> pred39;
  754. BitField<42, 1, u64> neg_pred;
  755. BitField<44, 1, u64> bf;
  756. BitField<45, 2, PredOperation> op;
  757. BitField<48, 1, u64> is_signed;
  758. BitField<49, 3, PredCondition> cond;
  759. } iset;
  760. union {
  761. BitField<8, 2, Register::Size> dest_size;
  762. BitField<10, 2, Register::Size> src_size;
  763. BitField<12, 1, u64> is_output_signed;
  764. BitField<13, 1, u64> is_input_signed;
  765. BitField<41, 2, u64> selector;
  766. BitField<45, 1, u64> negate_a;
  767. BitField<49, 1, u64> abs_a;
  768. union {
  769. BitField<39, 2, F2iRoundingOp> rounding;
  770. } f2i;
  771. union {
  772. BitField<39, 4, F2fRoundingOp> rounding;
  773. } f2f;
  774. } conversion;
  775. union {
  776. BitField<28, 1, u64> array;
  777. BitField<29, 2, TextureType> texture_type;
  778. BitField<31, 4, u64> component_mask;
  779. BitField<49, 1, u64> nodep_flag;
  780. BitField<50, 1, u64> dc_flag;
  781. BitField<54, 1, u64> aoffi_flag;
  782. BitField<55, 3, TextureProcessMode> process_mode;
  783. bool IsComponentEnabled(std::size_t component) const {
  784. return ((1ull << component) & component_mask) != 0;
  785. }
  786. TextureProcessMode GetTextureProcessMode() const {
  787. return process_mode;
  788. }
  789. bool UsesMiscMode(TextureMiscMode mode) const {
  790. switch (mode) {
  791. case TextureMiscMode::DC:
  792. return dc_flag != 0;
  793. case TextureMiscMode::NODEP:
  794. return nodep_flag != 0;
  795. case TextureMiscMode::AOFFI:
  796. return aoffi_flag != 0;
  797. default:
  798. break;
  799. }
  800. return false;
  801. }
  802. } tex;
  803. union {
  804. BitField<22, 6, TextureQueryType> query_type;
  805. BitField<31, 4, u64> component_mask;
  806. BitField<49, 1, u64> nodep_flag;
  807. bool UsesMiscMode(TextureMiscMode mode) const {
  808. switch (mode) {
  809. case TextureMiscMode::NODEP:
  810. return nodep_flag != 0;
  811. default:
  812. break;
  813. }
  814. return false;
  815. }
  816. } txq;
  817. union {
  818. BitField<28, 1, u64> array;
  819. BitField<29, 2, TextureType> texture_type;
  820. BitField<31, 4, u64> component_mask;
  821. BitField<35, 1, u64> ndv_flag;
  822. BitField<49, 1, u64> nodep_flag;
  823. bool IsComponentEnabled(std::size_t component) const {
  824. return ((1ull << component) & component_mask) != 0;
  825. }
  826. bool UsesMiscMode(TextureMiscMode mode) const {
  827. switch (mode) {
  828. case TextureMiscMode::NDV:
  829. return (ndv_flag != 0);
  830. case TextureMiscMode::NODEP:
  831. return (nodep_flag != 0);
  832. default:
  833. break;
  834. }
  835. return false;
  836. }
  837. } tmml;
  838. union {
  839. BitField<28, 1, u64> array;
  840. BitField<29, 2, TextureType> texture_type;
  841. BitField<35, 1, u64> ndv_flag;
  842. BitField<49, 1, u64> nodep_flag;
  843. BitField<50, 1, u64> dc_flag;
  844. BitField<54, 2, u64> info;
  845. BitField<56, 2, u64> component;
  846. bool UsesMiscMode(TextureMiscMode mode) const {
  847. switch (mode) {
  848. case TextureMiscMode::NDV:
  849. return ndv_flag != 0;
  850. case TextureMiscMode::NODEP:
  851. return nodep_flag != 0;
  852. case TextureMiscMode::DC:
  853. return dc_flag != 0;
  854. case TextureMiscMode::AOFFI:
  855. return info == 1;
  856. case TextureMiscMode::PTP:
  857. return info == 2;
  858. default:
  859. break;
  860. }
  861. return false;
  862. }
  863. } tld4;
  864. union {
  865. BitField<49, 1, u64> nodep_flag;
  866. BitField<50, 1, u64> dc_flag;
  867. BitField<51, 1, u64> aoffi_flag;
  868. BitField<52, 2, u64> component;
  869. bool UsesMiscMode(TextureMiscMode mode) const {
  870. switch (mode) {
  871. case TextureMiscMode::DC:
  872. return dc_flag != 0;
  873. case TextureMiscMode::NODEP:
  874. return nodep_flag != 0;
  875. case TextureMiscMode::AOFFI:
  876. return aoffi_flag != 0;
  877. default:
  878. break;
  879. }
  880. return false;
  881. }
  882. } tld4s;
  883. union {
  884. BitField<0, 8, Register> gpr0;
  885. BitField<28, 8, Register> gpr28;
  886. BitField<49, 1, u64> nodep_flag;
  887. BitField<50, 3, u64> component_mask_selector;
  888. BitField<53, 4, u64> texture_info;
  889. TextureType GetTextureType() const {
  890. // The TEXS instruction has a weird encoding for the texture type.
  891. if (texture_info == 0)
  892. return TextureType::Texture1D;
  893. if (texture_info >= 1 && texture_info <= 9)
  894. return TextureType::Texture2D;
  895. if (texture_info >= 10 && texture_info <= 11)
  896. return TextureType::Texture3D;
  897. if (texture_info >= 12 && texture_info <= 13)
  898. return TextureType::TextureCube;
  899. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}",
  900. static_cast<u32>(texture_info.Value()));
  901. UNREACHABLE();
  902. }
  903. TextureProcessMode GetTextureProcessMode() const {
  904. switch (texture_info) {
  905. case 0:
  906. case 2:
  907. case 6:
  908. case 8:
  909. case 9:
  910. case 11:
  911. return TextureProcessMode::LZ;
  912. case 3:
  913. case 5:
  914. case 13:
  915. return TextureProcessMode::LL;
  916. default:
  917. break;
  918. }
  919. return TextureProcessMode::None;
  920. }
  921. bool UsesMiscMode(TextureMiscMode mode) const {
  922. switch (mode) {
  923. case TextureMiscMode::DC:
  924. return (texture_info >= 4 && texture_info <= 6) || texture_info == 9;
  925. case TextureMiscMode::NODEP:
  926. return nodep_flag != 0;
  927. default:
  928. break;
  929. }
  930. return false;
  931. }
  932. bool IsArrayTexture() const {
  933. // TEXS only supports Texture2D arrays.
  934. return texture_info >= 7 && texture_info <= 9;
  935. }
  936. bool HasTwoDestinations() const {
  937. return gpr28.Value() != Register::ZeroIndex;
  938. }
  939. bool IsComponentEnabled(std::size_t component) const {
  940. static constexpr std::array<std::array<u32, 8>, 4> mask_lut{{
  941. {},
  942. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  943. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  944. {0x7, 0xb, 0xd, 0xe, 0xf},
  945. }};
  946. std::size_t index{gpr0.Value() != Register::ZeroIndex ? 1U : 0U};
  947. index |= gpr28.Value() != Register::ZeroIndex ? 2 : 0;
  948. u32 mask = mask_lut[index][component_mask_selector];
  949. // A mask of 0 means this instruction uses an unimplemented mask.
  950. ASSERT(mask != 0);
  951. return ((1ull << component) & mask) != 0;
  952. }
  953. } texs;
  954. union {
  955. BitField<49, 1, u64> nodep_flag;
  956. BitField<53, 4, u64> texture_info;
  957. TextureType GetTextureType() const {
  958. // The TLDS instruction has a weird encoding for the texture type.
  959. if (texture_info >= 0 && texture_info <= 1) {
  960. return TextureType::Texture1D;
  961. }
  962. if (texture_info == 2 || texture_info == 8 || texture_info == 12 ||
  963. (texture_info >= 4 && texture_info <= 6)) {
  964. return TextureType::Texture2D;
  965. }
  966. if (texture_info == 7) {
  967. return TextureType::Texture3D;
  968. }
  969. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}",
  970. static_cast<u32>(texture_info.Value()));
  971. UNREACHABLE();
  972. }
  973. TextureProcessMode GetTextureProcessMode() const {
  974. if (texture_info == 1 || texture_info == 5 || texture_info == 12)
  975. return TextureProcessMode::LL;
  976. return TextureProcessMode::LZ;
  977. }
  978. bool UsesMiscMode(TextureMiscMode mode) const {
  979. switch (mode) {
  980. case TextureMiscMode::AOFFI:
  981. return texture_info == 12 || texture_info == 4;
  982. case TextureMiscMode::MZ:
  983. return texture_info == 5;
  984. case TextureMiscMode::NODEP:
  985. return nodep_flag != 0;
  986. default:
  987. break;
  988. }
  989. return false;
  990. }
  991. bool IsArrayTexture() const {
  992. // TEXS only supports Texture2D arrays.
  993. return texture_info == 8;
  994. }
  995. } tlds;
  996. union {
  997. BitField<20, 24, u64> target;
  998. BitField<5, 1, u64> constant_buffer;
  999. s32 GetBranchTarget() const {
  1000. // Sign extend the branch target offset
  1001. u32 mask = 1U << (24 - 1);
  1002. u32 value = static_cast<u32>(target);
  1003. // The branch offset is relative to the next instruction and is stored in bytes, so
  1004. // divide it by the size of an instruction and add 1 to it.
  1005. return static_cast<s32>((value ^ mask) - mask) / sizeof(Instruction) + 1;
  1006. }
  1007. } bra;
  1008. union {
  1009. BitField<39, 1, u64> emit; // EmitVertex
  1010. BitField<40, 1, u64> cut; // EndPrimitive
  1011. } out;
  1012. union {
  1013. BitField<31, 1, u64> skew;
  1014. BitField<32, 1, u64> o;
  1015. BitField<33, 2, IsberdMode> mode;
  1016. BitField<47, 2, IsberdShift> shift;
  1017. } isberd;
  1018. union {
  1019. BitField<48, 1, u64> signed_a;
  1020. BitField<38, 1, u64> is_byte_chunk_a;
  1021. BitField<36, 2, VideoType> type_a;
  1022. BitField<36, 2, u64> byte_height_a;
  1023. BitField<49, 1, u64> signed_b;
  1024. BitField<50, 1, u64> use_register_b;
  1025. BitField<30, 1, u64> is_byte_chunk_b;
  1026. BitField<28, 2, VideoType> type_b;
  1027. BitField<28, 2, u64> byte_height_b;
  1028. } video;
  1029. union {
  1030. BitField<51, 2, VmadShr> shr;
  1031. BitField<55, 1, u64> saturate; // Saturates the result (a * b + c)
  1032. BitField<47, 1, u64> cc;
  1033. } vmad;
  1034. union {
  1035. BitField<20, 16, u64> imm20_16;
  1036. BitField<36, 1, u64> product_shift_left;
  1037. BitField<37, 1, u64> merge_37;
  1038. BitField<48, 1, u64> sign_a;
  1039. BitField<49, 1, u64> sign_b;
  1040. BitField<50, 3, XmadMode> mode;
  1041. BitField<52, 1, u64> high_b;
  1042. BitField<53, 1, u64> high_a;
  1043. BitField<56, 1, u64> merge_56;
  1044. } xmad;
  1045. union {
  1046. BitField<20, 14, u64> offset;
  1047. BitField<34, 5, u64> index;
  1048. } cbuf34;
  1049. union {
  1050. BitField<20, 16, s64> offset;
  1051. BitField<36, 5, u64> index;
  1052. } cbuf36;
  1053. // Unsure about the size of this one.
  1054. // It's always used with a gpr0, so any size should be fine.
  1055. BitField<20, 8, SystemVariable> sys20;
  1056. BitField<47, 1, u64> generates_cc;
  1057. BitField<61, 1, u64> is_b_imm;
  1058. BitField<60, 1, u64> is_b_gpr;
  1059. BitField<59, 1, u64> is_c_gpr;
  1060. BitField<20, 24, s64> smem_imm;
  1061. BitField<0, 5, ConditionCode> flow_condition_code;
  1062. Attribute attribute;
  1063. Sampler sampler;
  1064. u64 value;
  1065. };
  1066. static_assert(sizeof(Instruction) == 0x8, "Incorrect structure size");
  1067. static_assert(std::is_standard_layout_v<Instruction>, "Instruction is not standard layout");
  1068. class OpCode {
  1069. public:
  1070. enum class Id {
  1071. KIL,
  1072. SSY,
  1073. SYNC,
  1074. BRK,
  1075. DEPBAR,
  1076. BFE_C,
  1077. BFE_R,
  1078. BFE_IMM,
  1079. BRA,
  1080. PBK,
  1081. LD_A,
  1082. LD_L,
  1083. LD_S,
  1084. LD_C,
  1085. ST_A,
  1086. ST_L,
  1087. ST_S,
  1088. LDG, // Load from global memory
  1089. STG, // Store in global memory
  1090. TEX,
  1091. TXQ, // Texture Query
  1092. TEXS, // Texture Fetch with scalar/non-vec4 source/destinations
  1093. TLDS, // Texture Load with scalar/non-vec4 source/destinations
  1094. TLD4, // Texture Load 4
  1095. TLD4S, // Texture Load 4 with scalar / non - vec4 source / destinations
  1096. TMML_B, // Texture Mip Map Level
  1097. TMML, // Texture Mip Map Level
  1098. EXIT,
  1099. IPA,
  1100. OUT_R, // Emit vertex/primitive
  1101. ISBERD,
  1102. VMAD,
  1103. VSETP,
  1104. FFMA_IMM, // Fused Multiply and Add
  1105. FFMA_CR,
  1106. FFMA_RC,
  1107. FFMA_RR,
  1108. FADD_C,
  1109. FADD_R,
  1110. FADD_IMM,
  1111. FADD32I,
  1112. FMUL_C,
  1113. FMUL_R,
  1114. FMUL_IMM,
  1115. FMUL32_IMM,
  1116. IADD_C,
  1117. IADD_R,
  1118. IADD_IMM,
  1119. IADD3_C, // Add 3 Integers
  1120. IADD3_R,
  1121. IADD3_IMM,
  1122. IADD32I,
  1123. ISCADD_C, // Scale and Add
  1124. ISCADD_R,
  1125. ISCADD_IMM,
  1126. LEA_R1,
  1127. LEA_R2,
  1128. LEA_RZ,
  1129. LEA_IMM,
  1130. LEA_HI,
  1131. HADD2_C,
  1132. HADD2_R,
  1133. HADD2_IMM,
  1134. HMUL2_C,
  1135. HMUL2_R,
  1136. HMUL2_IMM,
  1137. HFMA2_CR,
  1138. HFMA2_RC,
  1139. HFMA2_RR,
  1140. HFMA2_IMM_R,
  1141. HSETP2_R,
  1142. HSET2_R,
  1143. POPC_C,
  1144. POPC_R,
  1145. POPC_IMM,
  1146. SEL_C,
  1147. SEL_R,
  1148. SEL_IMM,
  1149. MUFU, // Multi-Function Operator
  1150. RRO_C, // Range Reduction Operator
  1151. RRO_R,
  1152. RRO_IMM,
  1153. F2F_C,
  1154. F2F_R,
  1155. F2F_IMM,
  1156. F2I_C,
  1157. F2I_R,
  1158. F2I_IMM,
  1159. I2F_C,
  1160. I2F_R,
  1161. I2F_IMM,
  1162. I2I_C,
  1163. I2I_R,
  1164. I2I_IMM,
  1165. LOP_C,
  1166. LOP_R,
  1167. LOP_IMM,
  1168. LOP32I,
  1169. LOP3_C,
  1170. LOP3_R,
  1171. LOP3_IMM,
  1172. MOV_C,
  1173. MOV_R,
  1174. MOV_IMM,
  1175. MOV_SYS,
  1176. MOV32_IMM,
  1177. SHL_C,
  1178. SHL_R,
  1179. SHL_IMM,
  1180. SHR_C,
  1181. SHR_R,
  1182. SHR_IMM,
  1183. FMNMX_C,
  1184. FMNMX_R,
  1185. FMNMX_IMM,
  1186. IMNMX_C,
  1187. IMNMX_R,
  1188. IMNMX_IMM,
  1189. FSETP_C, // Set Predicate
  1190. FSETP_R,
  1191. FSETP_IMM,
  1192. FSET_C,
  1193. FSET_R,
  1194. FSET_IMM,
  1195. ISETP_C,
  1196. ISETP_IMM,
  1197. ISETP_R,
  1198. ISET_R,
  1199. ISET_C,
  1200. ISET_IMM,
  1201. PSETP,
  1202. PSET,
  1203. CSETP,
  1204. XMAD_IMM,
  1205. XMAD_CR,
  1206. XMAD_RC,
  1207. XMAD_RR,
  1208. };
  1209. enum class Type {
  1210. Trivial,
  1211. Arithmetic,
  1212. ArithmeticImmediate,
  1213. ArithmeticInteger,
  1214. ArithmeticIntegerImmediate,
  1215. ArithmeticHalf,
  1216. ArithmeticHalfImmediate,
  1217. Bfe,
  1218. Shift,
  1219. Ffma,
  1220. Hfma2,
  1221. Flow,
  1222. Synch,
  1223. Memory,
  1224. FloatSet,
  1225. FloatSetPredicate,
  1226. IntegerSet,
  1227. IntegerSetPredicate,
  1228. HalfSet,
  1229. HalfSetPredicate,
  1230. PredicateSetPredicate,
  1231. PredicateSetRegister,
  1232. Conversion,
  1233. Xmad,
  1234. Unknown,
  1235. };
  1236. /// Returns whether an opcode has an execution predicate field or not (ie, whether it can be
  1237. /// conditionally executed).
  1238. static bool IsPredicatedInstruction(Id opcode) {
  1239. // TODO(Subv): Add the rest of unpredicated instructions.
  1240. return opcode != Id::SSY && opcode != Id::PBK;
  1241. }
  1242. class Matcher {
  1243. public:
  1244. Matcher(const char* const name, u16 mask, u16 expected, OpCode::Id id, OpCode::Type type)
  1245. : name{name}, mask{mask}, expected{expected}, id{id}, type{type} {}
  1246. const char* GetName() const {
  1247. return name;
  1248. }
  1249. u16 GetMask() const {
  1250. return mask;
  1251. }
  1252. Id GetId() const {
  1253. return id;
  1254. }
  1255. Type GetType() const {
  1256. return type;
  1257. }
  1258. /**
  1259. * Tests to see if the given instruction is the instruction this matcher represents.
  1260. * @param instruction The instruction to test
  1261. * @returns true if the given instruction matches.
  1262. */
  1263. bool Matches(u16 instruction) const {
  1264. return (instruction & mask) == expected;
  1265. }
  1266. private:
  1267. const char* name;
  1268. u16 mask;
  1269. u16 expected;
  1270. Id id;
  1271. Type type;
  1272. };
  1273. static std::optional<std::reference_wrapper<const Matcher>> Decode(Instruction instr) {
  1274. static const auto table{GetDecodeTable()};
  1275. const auto matches_instruction = [instr](const auto& matcher) {
  1276. return matcher.Matches(static_cast<u16>(instr.opcode));
  1277. };
  1278. auto iter = std::find_if(table.begin(), table.end(), matches_instruction);
  1279. return iter != table.end() ? std::optional<std::reference_wrapper<const Matcher>>(*iter)
  1280. : std::nullopt;
  1281. }
  1282. private:
  1283. struct Detail {
  1284. private:
  1285. static constexpr std::size_t opcode_bitsize = 16;
  1286. /**
  1287. * Generates the mask and the expected value after masking from a given bitstring.
  1288. * A '0' in a bitstring indicates that a zero must be present at that bit position.
  1289. * A '1' in a bitstring indicates that a one must be present at that bit position.
  1290. */
  1291. static auto GetMaskAndExpect(const char* const bitstring) {
  1292. u16 mask = 0, expect = 0;
  1293. for (std::size_t i = 0; i < opcode_bitsize; i++) {
  1294. const std::size_t bit_position = opcode_bitsize - i - 1;
  1295. switch (bitstring[i]) {
  1296. case '0':
  1297. mask |= 1 << bit_position;
  1298. break;
  1299. case '1':
  1300. expect |= 1 << bit_position;
  1301. mask |= 1 << bit_position;
  1302. break;
  1303. default:
  1304. // Ignore
  1305. break;
  1306. }
  1307. }
  1308. return std::make_tuple(mask, expect);
  1309. }
  1310. public:
  1311. /// Creates a matcher that can match and parse instructions based on bitstring.
  1312. static auto GetMatcher(const char* const bitstring, OpCode::Id op, OpCode::Type type,
  1313. const char* const name) {
  1314. const auto mask_expect = GetMaskAndExpect(bitstring);
  1315. return Matcher(name, std::get<0>(mask_expect), std::get<1>(mask_expect), op, type);
  1316. }
  1317. };
  1318. static std::vector<Matcher> GetDecodeTable() {
  1319. std::vector<Matcher> table = {
  1320. #define INST(bitstring, op, type, name) Detail::GetMatcher(bitstring, op, type, name)
  1321. INST("111000110011----", Id::KIL, Type::Flow, "KIL"),
  1322. INST("111000101001----", Id::SSY, Type::Flow, "SSY"),
  1323. INST("111000101010----", Id::PBK, Type::Flow, "PBK"),
  1324. INST("111000100100----", Id::BRA, Type::Flow, "BRA"),
  1325. INST("1111000011111---", Id::SYNC, Type::Flow, "SYNC"),
  1326. INST("111000110100---", Id::BRK, Type::Flow, "BRK"),
  1327. INST("1111000011110---", Id::DEPBAR, Type::Synch, "DEPBAR"),
  1328. INST("1110111111011---", Id::LD_A, Type::Memory, "LD_A"),
  1329. INST("1110111101001---", Id::LD_S, Type::Memory, "LD_S"),
  1330. INST("1110111101000---", Id::LD_L, Type::Memory, "LD_L"),
  1331. INST("1110111110010---", Id::LD_C, Type::Memory, "LD_C"),
  1332. INST("1110111111110---", Id::ST_A, Type::Memory, "ST_A"),
  1333. INST("1110111101011---", Id::ST_S, Type::Memory, "ST_S"),
  1334. INST("1110111101010---", Id::ST_L, Type::Memory, "ST_L"),
  1335. INST("1110111011010---", Id::LDG, Type::Memory, "LDG"),
  1336. INST("1110111011011---", Id::STG, Type::Memory, "STG"),
  1337. INST("110000----111---", Id::TEX, Type::Memory, "TEX"),
  1338. INST("1101111101001---", Id::TXQ, Type::Memory, "TXQ"),
  1339. INST("1101100---------", Id::TEXS, Type::Memory, "TEXS"),
  1340. INST("1101101---------", Id::TLDS, Type::Memory, "TLDS"),
  1341. INST("110010----111---", Id::TLD4, Type::Memory, "TLD4"),
  1342. INST("1101111100------", Id::TLD4S, Type::Memory, "TLD4S"),
  1343. INST("110111110110----", Id::TMML_B, Type::Memory, "TMML_B"),
  1344. INST("1101111101011---", Id::TMML, Type::Memory, "TMML"),
  1345. INST("111000110000----", Id::EXIT, Type::Trivial, "EXIT"),
  1346. INST("11100000--------", Id::IPA, Type::Trivial, "IPA"),
  1347. INST("1111101111100---", Id::OUT_R, Type::Trivial, "OUT_R"),
  1348. INST("1110111111010---", Id::ISBERD, Type::Trivial, "ISBERD"),
  1349. INST("01011111--------", Id::VMAD, Type::Trivial, "VMAD"),
  1350. INST("0101000011110---", Id::VSETP, Type::Trivial, "VSETP"),
  1351. INST("0011001-1-------", Id::FFMA_IMM, Type::Ffma, "FFMA_IMM"),
  1352. INST("010010011-------", Id::FFMA_CR, Type::Ffma, "FFMA_CR"),
  1353. INST("010100011-------", Id::FFMA_RC, Type::Ffma, "FFMA_RC"),
  1354. INST("010110011-------", Id::FFMA_RR, Type::Ffma, "FFMA_RR"),
  1355. INST("0100110001011---", Id::FADD_C, Type::Arithmetic, "FADD_C"),
  1356. INST("0101110001011---", Id::FADD_R, Type::Arithmetic, "FADD_R"),
  1357. INST("0011100-01011---", Id::FADD_IMM, Type::Arithmetic, "FADD_IMM"),
  1358. INST("000010----------", Id::FADD32I, Type::ArithmeticImmediate, "FADD32I"),
  1359. INST("0100110001101---", Id::FMUL_C, Type::Arithmetic, "FMUL_C"),
  1360. INST("0101110001101---", Id::FMUL_R, Type::Arithmetic, "FMUL_R"),
  1361. INST("0011100-01101---", Id::FMUL_IMM, Type::Arithmetic, "FMUL_IMM"),
  1362. INST("00011110--------", Id::FMUL32_IMM, Type::ArithmeticImmediate, "FMUL32_IMM"),
  1363. INST("0100110000010---", Id::IADD_C, Type::ArithmeticInteger, "IADD_C"),
  1364. INST("0101110000010---", Id::IADD_R, Type::ArithmeticInteger, "IADD_R"),
  1365. INST("0011100-00010---", Id::IADD_IMM, Type::ArithmeticInteger, "IADD_IMM"),
  1366. INST("010011001100----", Id::IADD3_C, Type::ArithmeticInteger, "IADD3_C"),
  1367. INST("010111001100----", Id::IADD3_R, Type::ArithmeticInteger, "IADD3_R"),
  1368. INST("0011100-1100----", Id::IADD3_IMM, Type::ArithmeticInteger, "IADD3_IMM"),
  1369. INST("0001110---------", Id::IADD32I, Type::ArithmeticIntegerImmediate, "IADD32I"),
  1370. INST("0100110000011---", Id::ISCADD_C, Type::ArithmeticInteger, "ISCADD_C"),
  1371. INST("0101110000011---", Id::ISCADD_R, Type::ArithmeticInteger, "ISCADD_R"),
  1372. INST("0011100-00011---", Id::ISCADD_IMM, Type::ArithmeticInteger, "ISCADD_IMM"),
  1373. INST("0100110000001---", Id::POPC_C, Type::ArithmeticInteger, "POPC_C"),
  1374. INST("0101110000001---", Id::POPC_R, Type::ArithmeticInteger, "POPC_R"),
  1375. INST("0011100-00001---", Id::POPC_IMM, Type::ArithmeticInteger, "POPC_IMM"),
  1376. INST("0100110010100---", Id::SEL_C, Type::ArithmeticInteger, "SEL_C"),
  1377. INST("0101110010100---", Id::SEL_R, Type::ArithmeticInteger, "SEL_R"),
  1378. INST("0011100-10100---", Id::SEL_IMM, Type::ArithmeticInteger, "SEL_IMM"),
  1379. INST("0101101111011---", Id::LEA_R2, Type::ArithmeticInteger, "LEA_R2"),
  1380. INST("0101101111010---", Id::LEA_R1, Type::ArithmeticInteger, "LEA_R1"),
  1381. INST("001101101101----", Id::LEA_IMM, Type::ArithmeticInteger, "LEA_IMM"),
  1382. INST("010010111101----", Id::LEA_RZ, Type::ArithmeticInteger, "LEA_RZ"),
  1383. INST("00011000--------", Id::LEA_HI, Type::ArithmeticInteger, "LEA_HI"),
  1384. INST("0111101-1-------", Id::HADD2_C, Type::ArithmeticHalf, "HADD2_C"),
  1385. INST("0101110100010---", Id::HADD2_R, Type::ArithmeticHalf, "HADD2_R"),
  1386. INST("0111101-0-------", Id::HADD2_IMM, Type::ArithmeticHalfImmediate, "HADD2_IMM"),
  1387. INST("0111100-1-------", Id::HMUL2_C, Type::ArithmeticHalf, "HMUL2_C"),
  1388. INST("0101110100001---", Id::HMUL2_R, Type::ArithmeticHalf, "HMUL2_R"),
  1389. INST("0111100-0-------", Id::HMUL2_IMM, Type::ArithmeticHalfImmediate, "HMUL2_IMM"),
  1390. INST("01110---1-------", Id::HFMA2_CR, Type::Hfma2, "HFMA2_CR"),
  1391. INST("01100---1-------", Id::HFMA2_RC, Type::Hfma2, "HFMA2_RC"),
  1392. INST("0101110100000---", Id::HFMA2_RR, Type::Hfma2, "HFMA2_RR"),
  1393. INST("01110---0-------", Id::HFMA2_IMM_R, Type::Hfma2, "HFMA2_R_IMM"),
  1394. INST("0101110100100---", Id::HSETP2_R, Type::HalfSetPredicate, "HSETP_R"),
  1395. INST("0101110100011---", Id::HSET2_R, Type::HalfSet, "HSET2_R"),
  1396. INST("0101000010000---", Id::MUFU, Type::Arithmetic, "MUFU"),
  1397. INST("0100110010010---", Id::RRO_C, Type::Arithmetic, "RRO_C"),
  1398. INST("0101110010010---", Id::RRO_R, Type::Arithmetic, "RRO_R"),
  1399. INST("0011100-10010---", Id::RRO_IMM, Type::Arithmetic, "RRO_IMM"),
  1400. INST("0100110010101---", Id::F2F_C, Type::Conversion, "F2F_C"),
  1401. INST("0101110010101---", Id::F2F_R, Type::Conversion, "F2F_R"),
  1402. INST("0011100-10101---", Id::F2F_IMM, Type::Conversion, "F2F_IMM"),
  1403. INST("0100110010110---", Id::F2I_C, Type::Conversion, "F2I_C"),
  1404. INST("0101110010110---", Id::F2I_R, Type::Conversion, "F2I_R"),
  1405. INST("0011100-10110---", Id::F2I_IMM, Type::Conversion, "F2I_IMM"),
  1406. INST("0100110010011---", Id::MOV_C, Type::Arithmetic, "MOV_C"),
  1407. INST("0101110010011---", Id::MOV_R, Type::Arithmetic, "MOV_R"),
  1408. INST("0011100-10011---", Id::MOV_IMM, Type::Arithmetic, "MOV_IMM"),
  1409. INST("1111000011001---", Id::MOV_SYS, Type::Trivial, "MOV_SYS"),
  1410. INST("000000010000----", Id::MOV32_IMM, Type::ArithmeticImmediate, "MOV32_IMM"),
  1411. INST("0100110001100---", Id::FMNMX_C, Type::Arithmetic, "FMNMX_C"),
  1412. INST("0101110001100---", Id::FMNMX_R, Type::Arithmetic, "FMNMX_R"),
  1413. INST("0011100-01100---", Id::FMNMX_IMM, Type::Arithmetic, "FMNMX_IMM"),
  1414. INST("0100110000100---", Id::IMNMX_C, Type::ArithmeticInteger, "IMNMX_C"),
  1415. INST("0101110000100---", Id::IMNMX_R, Type::ArithmeticInteger, "IMNMX_R"),
  1416. INST("0011100-00100---", Id::IMNMX_IMM, Type::ArithmeticInteger, "IMNMX_IMM"),
  1417. INST("0100110000000---", Id::BFE_C, Type::Bfe, "BFE_C"),
  1418. INST("0101110000000---", Id::BFE_R, Type::Bfe, "BFE_R"),
  1419. INST("0011100-00000---", Id::BFE_IMM, Type::Bfe, "BFE_IMM"),
  1420. INST("0100110001000---", Id::LOP_C, Type::ArithmeticInteger, "LOP_C"),
  1421. INST("0101110001000---", Id::LOP_R, Type::ArithmeticInteger, "LOP_R"),
  1422. INST("0011100001000---", Id::LOP_IMM, Type::ArithmeticInteger, "LOP_IMM"),
  1423. INST("000001----------", Id::LOP32I, Type::ArithmeticIntegerImmediate, "LOP32I"),
  1424. INST("0000001---------", Id::LOP3_C, Type::ArithmeticInteger, "LOP3_C"),
  1425. INST("0101101111100---", Id::LOP3_R, Type::ArithmeticInteger, "LOP3_R"),
  1426. INST("0011110---------", Id::LOP3_IMM, Type::ArithmeticInteger, "LOP3_IMM"),
  1427. INST("0100110001001---", Id::SHL_C, Type::Shift, "SHL_C"),
  1428. INST("0101110001001---", Id::SHL_R, Type::Shift, "SHL_R"),
  1429. INST("0011100-01001---", Id::SHL_IMM, Type::Shift, "SHL_IMM"),
  1430. INST("0100110000101---", Id::SHR_C, Type::Shift, "SHR_C"),
  1431. INST("0101110000101---", Id::SHR_R, Type::Shift, "SHR_R"),
  1432. INST("0011100-00101---", Id::SHR_IMM, Type::Shift, "SHR_IMM"),
  1433. INST("0100110011100---", Id::I2I_C, Type::Conversion, "I2I_C"),
  1434. INST("0101110011100---", Id::I2I_R, Type::Conversion, "I2I_R"),
  1435. INST("01110001-1000---", Id::I2I_IMM, Type::Conversion, "I2I_IMM"),
  1436. INST("0100110010111---", Id::I2F_C, Type::Conversion, "I2F_C"),
  1437. INST("0101110010111---", Id::I2F_R, Type::Conversion, "I2F_R"),
  1438. INST("0011100-10111---", Id::I2F_IMM, Type::Conversion, "I2F_IMM"),
  1439. INST("01011000--------", Id::FSET_R, Type::FloatSet, "FSET_R"),
  1440. INST("0100100---------", Id::FSET_C, Type::FloatSet, "FSET_C"),
  1441. INST("0011000---------", Id::FSET_IMM, Type::FloatSet, "FSET_IMM"),
  1442. INST("010010111011----", Id::FSETP_C, Type::FloatSetPredicate, "FSETP_C"),
  1443. INST("010110111011----", Id::FSETP_R, Type::FloatSetPredicate, "FSETP_R"),
  1444. INST("0011011-1011----", Id::FSETP_IMM, Type::FloatSetPredicate, "FSETP_IMM"),
  1445. INST("010010110110----", Id::ISETP_C, Type::IntegerSetPredicate, "ISETP_C"),
  1446. INST("010110110110----", Id::ISETP_R, Type::IntegerSetPredicate, "ISETP_R"),
  1447. INST("0011011-0110----", Id::ISETP_IMM, Type::IntegerSetPredicate, "ISETP_IMM"),
  1448. INST("010110110101----", Id::ISET_R, Type::IntegerSet, "ISET_R"),
  1449. INST("010010110101----", Id::ISET_C, Type::IntegerSet, "ISET_C"),
  1450. INST("0011011-0101----", Id::ISET_IMM, Type::IntegerSet, "ISET_IMM"),
  1451. INST("0101000010001---", Id::PSET, Type::PredicateSetRegister, "PSET"),
  1452. INST("0101000010010---", Id::PSETP, Type::PredicateSetPredicate, "PSETP"),
  1453. INST("010100001010----", Id::CSETP, Type::PredicateSetPredicate, "CSETP"),
  1454. INST("0011011-00------", Id::XMAD_IMM, Type::Xmad, "XMAD_IMM"),
  1455. INST("0100111---------", Id::XMAD_CR, Type::Xmad, "XMAD_CR"),
  1456. INST("010100010-------", Id::XMAD_RC, Type::Xmad, "XMAD_RC"),
  1457. INST("0101101100------", Id::XMAD_RR, Type::Xmad, "XMAD_RR"),
  1458. };
  1459. #undef INST
  1460. std::stable_sort(table.begin(), table.end(), [](const auto& a, const auto& b) {
  1461. // If a matcher has more bits in its mask it is more specific, so it
  1462. // should come first.
  1463. return std::bitset<16>(a.GetMask()).count() > std::bitset<16>(b.GetMask()).count();
  1464. });
  1465. return table;
  1466. }
  1467. };
  1468. } // namespace Tegra::Shader