shader_bytecode.h 49 KB

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