shader_bytecode.h 50 KB

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