shader_bytecode.h 49 KB

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