shader_bytecode.h 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  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 <array>
  6. #include <bitset>
  7. #include <optional>
  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. [[nodiscard]] constexpr operator u64() const {
  28. return value;
  29. }
  30. template <typename T>
  31. [[nodiscard]] constexpr u64 operator-(const T& oth) const {
  32. return value - oth;
  33. }
  34. template <typename T>
  35. [[nodiscard]] constexpr u64 operator&(const T& oth) const {
  36. return value & oth;
  37. }
  38. [[nodiscard]] constexpr u64 operator&(const Register& oth) const {
  39. return value & oth.value;
  40. }
  41. [[nodiscard]] constexpr u64 operator~() const {
  42. return ~value;
  43. }
  44. [[nodiscard]] 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. LayerViewportPointSize = 6,
  62. Position = 7,
  63. Attribute_0 = 8,
  64. Attribute_31 = 39,
  65. FrontColor = 40,
  66. FrontSecondaryColor = 41,
  67. BackColor = 42,
  68. BackSecondaryColor = 43,
  69. ClipDistances0123 = 44,
  70. ClipDistances4567 = 45,
  71. PointCoord = 46,
  72. // This attribute contains a tuple of (~, ~, InstanceId, VertexId) when inside a vertex
  73. // shader, and a tuple of (TessCoord.x, TessCoord.y, TessCoord.z, ~) when inside a Tess Eval
  74. // shader.
  75. TessCoordInstanceIDVertexID = 47,
  76. TexCoord_0 = 48,
  77. TexCoord_7 = 55,
  78. // This attribute contains a tuple of (Unk, Unk, Unk, gl_FrontFacing) when inside a fragment
  79. // shader. It is unknown what the other values contain.
  80. FrontFacing = 63,
  81. };
  82. union {
  83. BitField<20, 10, u64> immediate;
  84. BitField<22, 2, u64> element;
  85. BitField<24, 6, Index> index;
  86. BitField<31, 1, u64> patch;
  87. BitField<47, 3, AttributeSize> size;
  88. [[nodiscard]] bool IsPhysical() const {
  89. return patch == 0 && element == 0 && static_cast<u64>(index.Value()) == 0;
  90. }
  91. } fmt20;
  92. union {
  93. BitField<30, 2, u64> element;
  94. BitField<32, 6, Index> index;
  95. } fmt28;
  96. BitField<39, 8, u64> reg;
  97. u64 value{};
  98. };
  99. union Sampler {
  100. Sampler() = default;
  101. constexpr explicit Sampler(u64 value_) : value(value_) {}
  102. enum class Index : u64 {
  103. Sampler_0 = 8,
  104. };
  105. BitField<36, 13, Index> index;
  106. u64 value{};
  107. };
  108. union Image {
  109. Image() = default;
  110. constexpr explicit Image(u64 value_) : value{value_} {}
  111. BitField<36, 13, u64> index;
  112. u64 value;
  113. };
  114. } // namespace Tegra::Shader
  115. namespace std {
  116. // TODO(bunnei): The below is forbidden by the C++ standard, but works fine. See #330.
  117. template <>
  118. struct make_unsigned<Tegra::Shader::Attribute> {
  119. using type = Tegra::Shader::Attribute;
  120. };
  121. template <>
  122. struct make_unsigned<Tegra::Shader::Register> {
  123. using type = Tegra::Shader::Register;
  124. };
  125. } // namespace std
  126. namespace Tegra::Shader {
  127. enum class Pred : u64 {
  128. UnusedIndex = 0x7,
  129. NeverExecute = 0xF,
  130. };
  131. enum class PredCondition : u64 {
  132. F = 0, // Always false
  133. LT = 1, // Ordered less than
  134. EQ = 2, // Ordered equal
  135. LE = 3, // Ordered less than or equal
  136. GT = 4, // Ordered greater than
  137. NE = 5, // Ordered not equal
  138. GE = 6, // Ordered greater than or equal
  139. NUM = 7, // Ordered
  140. NAN_ = 8, // Unordered
  141. LTU = 9, // Unordered less than
  142. EQU = 10, // Unordered equal
  143. LEU = 11, // Unordered less than or equal
  144. GTU = 12, // Unordered greater than
  145. NEU = 13, // Unordered not equal
  146. GEU = 14, // Unordered greater than or equal
  147. T = 15, // Always true
  148. };
  149. enum class PredOperation : u64 {
  150. And = 0,
  151. Or = 1,
  152. Xor = 2,
  153. };
  154. enum class LogicOperation : u64 {
  155. And = 0,
  156. Or = 1,
  157. Xor = 2,
  158. PassB = 3,
  159. };
  160. enum class SubOp : u64 {
  161. Cos = 0x0,
  162. Sin = 0x1,
  163. Ex2 = 0x2,
  164. Lg2 = 0x3,
  165. Rcp = 0x4,
  166. Rsq = 0x5,
  167. Sqrt = 0x8,
  168. };
  169. enum class F2iRoundingOp : u64 {
  170. RoundEven = 0,
  171. Floor = 1,
  172. Ceil = 2,
  173. Trunc = 3,
  174. };
  175. enum class F2fRoundingOp : u64 {
  176. None = 0,
  177. Pass = 3,
  178. Round = 8,
  179. Floor = 9,
  180. Ceil = 10,
  181. Trunc = 11,
  182. };
  183. enum class AtomicOp : u64 {
  184. Add = 0,
  185. Min = 1,
  186. Max = 2,
  187. Inc = 3,
  188. Dec = 4,
  189. And = 5,
  190. Or = 6,
  191. Xor = 7,
  192. Exch = 8,
  193. SafeAdd = 10,
  194. };
  195. enum class GlobalAtomicType : u64 {
  196. U32 = 0,
  197. S32 = 1,
  198. U64 = 2,
  199. F32_FTZ_RN = 3,
  200. F16x2_FTZ_RN = 4,
  201. S64 = 5,
  202. };
  203. enum class UniformType : u64 {
  204. UnsignedByte = 0,
  205. SignedByte = 1,
  206. UnsignedShort = 2,
  207. SignedShort = 3,
  208. Single = 4,
  209. Double = 5,
  210. Quad = 6,
  211. UnsignedQuad = 7,
  212. };
  213. enum class StoreType : u64 {
  214. Unsigned8 = 0,
  215. Signed8 = 1,
  216. Unsigned16 = 2,
  217. Signed16 = 3,
  218. Bits32 = 4,
  219. Bits64 = 5,
  220. Bits128 = 6,
  221. };
  222. enum class AtomicType : u64 {
  223. U32 = 0,
  224. S32 = 1,
  225. U64 = 2,
  226. S64 = 3,
  227. };
  228. enum class IMinMaxExchange : u64 {
  229. None = 0,
  230. XLo = 1,
  231. XMed = 2,
  232. XHi = 3,
  233. };
  234. enum class VideoType : u64 {
  235. Size16_Low = 0,
  236. Size16_High = 1,
  237. Size32 = 2,
  238. Invalid = 3,
  239. };
  240. enum class VmadShr : u64 {
  241. Shr7 = 1,
  242. Shr15 = 2,
  243. };
  244. enum class VmnmxType : u64 {
  245. Bits8,
  246. Bits16,
  247. Bits32,
  248. };
  249. enum class VmnmxOperation : u64 {
  250. Mrg_16H = 0,
  251. Mrg_16L = 1,
  252. Mrg_8B0 = 2,
  253. Mrg_8B2 = 3,
  254. Acc = 4,
  255. Min = 5,
  256. Max = 6,
  257. Nop = 7,
  258. };
  259. enum class XmadMode : u64 {
  260. None = 0,
  261. CLo = 1,
  262. CHi = 2,
  263. CSfu = 3,
  264. CBcc = 4,
  265. };
  266. enum class IAdd3Mode : u64 {
  267. None = 0,
  268. RightShift = 1,
  269. LeftShift = 2,
  270. };
  271. enum class IAdd3Height : u64 {
  272. None = 0,
  273. LowerHalfWord = 1,
  274. UpperHalfWord = 2,
  275. };
  276. enum class FlowCondition : u64 {
  277. Always = 0xF,
  278. Fcsm_Tr = 0x1C, // TODO(bunnei): What is this used for?
  279. };
  280. enum class ConditionCode : u64 {
  281. F = 0,
  282. LT = 1,
  283. EQ = 2,
  284. LE = 3,
  285. GT = 4,
  286. NE = 5,
  287. GE = 6,
  288. Num = 7,
  289. Nan = 8,
  290. LTU = 9,
  291. EQU = 10,
  292. LEU = 11,
  293. GTU = 12,
  294. NEU = 13,
  295. GEU = 14,
  296. T = 15,
  297. OFF = 16,
  298. LO = 17,
  299. SFF = 18,
  300. LS = 19,
  301. HI = 20,
  302. SFT = 21,
  303. HS = 22,
  304. OFT = 23,
  305. CSM_TA = 24,
  306. CSM_TR = 25,
  307. CSM_MX = 26,
  308. FCSM_TA = 27,
  309. FCSM_TR = 28,
  310. FCSM_MX = 29,
  311. RLE = 30,
  312. RGT = 31,
  313. };
  314. enum class PredicateResultMode : u64 {
  315. None = 0x0,
  316. NotZero = 0x3,
  317. };
  318. enum class TextureType : u64 {
  319. Texture1D = 0,
  320. Texture2D = 1,
  321. Texture3D = 2,
  322. TextureCube = 3,
  323. };
  324. enum class TextureQueryType : u64 {
  325. Dimension = 1,
  326. TextureType = 2,
  327. SamplePosition = 5,
  328. Filter = 16,
  329. LevelOfDetail = 18,
  330. Wrap = 20,
  331. BorderColor = 22,
  332. };
  333. enum class TextureProcessMode : u64 {
  334. None = 0,
  335. LZ = 1, // Load LOD of zero.
  336. LB = 2, // Load Bias.
  337. LL = 3, // Load LOD.
  338. LBA = 6, // Load Bias. The A is unknown, does not appear to differ with LB.
  339. LLA = 7 // Load LOD. The A is unknown, does not appear to differ with LL.
  340. };
  341. enum class TextureMiscMode : u64 {
  342. DC,
  343. AOFFI, // Uses Offset
  344. NDV,
  345. NODEP,
  346. MZ,
  347. PTP,
  348. };
  349. enum class SurfaceDataMode : u64 {
  350. P = 0,
  351. D_BA = 1,
  352. };
  353. enum class OutOfBoundsStore : u64 {
  354. Ignore = 0,
  355. Clamp = 1,
  356. Trap = 2,
  357. };
  358. enum class ImageType : u64 {
  359. Texture1D = 0,
  360. TextureBuffer = 1,
  361. Texture1DArray = 2,
  362. Texture2D = 3,
  363. Texture2DArray = 4,
  364. Texture3D = 5,
  365. };
  366. enum class IsberdMode : u64 {
  367. None = 0,
  368. Patch = 1,
  369. Prim = 2,
  370. Attr = 3,
  371. };
  372. enum class IsberdShift : u64 { None = 0, U16 = 1, B32 = 2 };
  373. enum class MembarType : u64 {
  374. CTA = 0,
  375. GL = 1,
  376. SYS = 2,
  377. VC = 3,
  378. };
  379. enum class MembarUnknown : u64 { Default = 0, IVALLD = 1, IVALLT = 2, IVALLTD = 3 };
  380. enum class HalfType : u64 {
  381. H0_H1 = 0,
  382. F32 = 1,
  383. H0_H0 = 2,
  384. H1_H1 = 3,
  385. };
  386. enum class HalfMerge : u64 {
  387. H0_H1 = 0,
  388. F32 = 1,
  389. Mrg_H0 = 2,
  390. Mrg_H1 = 3,
  391. };
  392. enum class HalfPrecision : u64 {
  393. None = 0,
  394. FTZ = 1,
  395. FMZ = 2,
  396. };
  397. enum class R2pMode : u64 {
  398. Pr = 0,
  399. Cc = 1,
  400. };
  401. enum class IpaInterpMode : u64 {
  402. Pass = 0,
  403. Multiply = 1,
  404. Constant = 2,
  405. Sc = 3,
  406. };
  407. enum class IpaSampleMode : u64 {
  408. Default = 0,
  409. Centroid = 1,
  410. Offset = 2,
  411. };
  412. enum class LmemLoadCacheManagement : u64 {
  413. Default = 0,
  414. LU = 1,
  415. CI = 2,
  416. CV = 3,
  417. };
  418. enum class StoreCacheManagement : u64 {
  419. Default = 0,
  420. CG = 1,
  421. CS = 2,
  422. WT = 3,
  423. };
  424. struct IpaMode {
  425. IpaInterpMode interpolation_mode;
  426. IpaSampleMode sampling_mode;
  427. [[nodiscard]] bool operator==(const IpaMode& a) const {
  428. return std::tie(interpolation_mode, sampling_mode) ==
  429. std::tie(a.interpolation_mode, a.sampling_mode);
  430. }
  431. [[nodiscard]] bool operator!=(const IpaMode& a) const {
  432. return !operator==(a);
  433. }
  434. [[nodiscard]] bool operator<(const IpaMode& a) const {
  435. return std::tie(interpolation_mode, sampling_mode) <
  436. std::tie(a.interpolation_mode, a.sampling_mode);
  437. }
  438. };
  439. enum class SystemVariable : u64 {
  440. LaneId = 0x00,
  441. VirtCfg = 0x02,
  442. VirtId = 0x03,
  443. Pm0 = 0x04,
  444. Pm1 = 0x05,
  445. Pm2 = 0x06,
  446. Pm3 = 0x07,
  447. Pm4 = 0x08,
  448. Pm5 = 0x09,
  449. Pm6 = 0x0a,
  450. Pm7 = 0x0b,
  451. OrderingTicket = 0x0f,
  452. PrimType = 0x10,
  453. InvocationId = 0x11,
  454. Ydirection = 0x12,
  455. ThreadKill = 0x13,
  456. ShaderType = 0x14,
  457. DirectBeWriteAddressLow = 0x15,
  458. DirectBeWriteAddressHigh = 0x16,
  459. DirectBeWriteEnabled = 0x17,
  460. MachineId0 = 0x18,
  461. MachineId1 = 0x19,
  462. MachineId2 = 0x1a,
  463. MachineId3 = 0x1b,
  464. Affinity = 0x1c,
  465. InvocationInfo = 0x1d,
  466. WscaleFactorXY = 0x1e,
  467. WscaleFactorZ = 0x1f,
  468. Tid = 0x20,
  469. TidX = 0x21,
  470. TidY = 0x22,
  471. TidZ = 0x23,
  472. CtaParam = 0x24,
  473. CtaIdX = 0x25,
  474. CtaIdY = 0x26,
  475. CtaIdZ = 0x27,
  476. NtId = 0x28,
  477. CirQueueIncrMinusOne = 0x29,
  478. Nlatc = 0x2a,
  479. SmSpaVersion = 0x2c,
  480. MultiPassShaderInfo = 0x2d,
  481. LwinHi = 0x2e,
  482. SwinHi = 0x2f,
  483. SwinLo = 0x30,
  484. SwinSz = 0x31,
  485. SmemSz = 0x32,
  486. SmemBanks = 0x33,
  487. LwinLo = 0x34,
  488. LwinSz = 0x35,
  489. LmemLosz = 0x36,
  490. LmemHioff = 0x37,
  491. EqMask = 0x38,
  492. LtMask = 0x39,
  493. LeMask = 0x3a,
  494. GtMask = 0x3b,
  495. GeMask = 0x3c,
  496. RegAlloc = 0x3d,
  497. CtxAddr = 0x3e, // .fmask = F_SM50
  498. BarrierAlloc = 0x3e, // .fmask = F_SM60
  499. GlobalErrorStatus = 0x40,
  500. WarpErrorStatus = 0x42,
  501. WarpErrorStatusClear = 0x43,
  502. PmHi0 = 0x48,
  503. PmHi1 = 0x49,
  504. PmHi2 = 0x4a,
  505. PmHi3 = 0x4b,
  506. PmHi4 = 0x4c,
  507. PmHi5 = 0x4d,
  508. PmHi6 = 0x4e,
  509. PmHi7 = 0x4f,
  510. ClockLo = 0x50,
  511. ClockHi = 0x51,
  512. GlobalTimerLo = 0x52,
  513. GlobalTimerHi = 0x53,
  514. HwTaskId = 0x60,
  515. CircularQueueEntryIndex = 0x61,
  516. CircularQueueEntryAddressLow = 0x62,
  517. CircularQueueEntryAddressHigh = 0x63,
  518. };
  519. enum class PhysicalAttributeDirection : u64 {
  520. Input = 0,
  521. Output = 1,
  522. };
  523. enum class VoteOperation : u64 {
  524. All = 0, // allThreadsNV
  525. Any = 1, // anyThreadNV
  526. Eq = 2, // allThreadsEqualNV
  527. };
  528. enum class ImageAtomicOperationType : u64 {
  529. U32 = 0,
  530. S32 = 1,
  531. U64 = 2,
  532. F32 = 3,
  533. S64 = 5,
  534. SD32 = 6,
  535. SD64 = 7,
  536. };
  537. enum class ImageAtomicOperation : u64 {
  538. Add = 0,
  539. Min = 1,
  540. Max = 2,
  541. Inc = 3,
  542. Dec = 4,
  543. And = 5,
  544. Or = 6,
  545. Xor = 7,
  546. Exch = 8,
  547. };
  548. enum class ShuffleOperation : u64 {
  549. Idx = 0, // shuffleNV
  550. Up = 1, // shuffleUpNV
  551. Down = 2, // shuffleDownNV
  552. Bfly = 3, // shuffleXorNV
  553. };
  554. enum class ShfType : u64 {
  555. Bits32 = 0,
  556. U64 = 2,
  557. S64 = 3,
  558. };
  559. enum class ShfXmode : u64 {
  560. None = 0,
  561. HI = 1,
  562. X = 2,
  563. XHI = 3,
  564. };
  565. union Instruction {
  566. constexpr Instruction& operator=(const Instruction& instr) {
  567. value = instr.value;
  568. return *this;
  569. }
  570. constexpr Instruction(u64 value_) : value{value_} {}
  571. constexpr Instruction(const Instruction& instr) : value(instr.value) {}
  572. [[nodiscard]] constexpr bool Bit(u64 offset) const {
  573. return ((value >> offset) & 1) != 0;
  574. }
  575. BitField<0, 8, Register> gpr0;
  576. BitField<8, 8, Register> gpr8;
  577. union {
  578. BitField<16, 4, Pred> full_pred;
  579. BitField<16, 3, u64> pred_index;
  580. } pred;
  581. BitField<19, 1, u64> negate_pred;
  582. BitField<20, 8, Register> gpr20;
  583. BitField<20, 4, SubOp> sub_op;
  584. BitField<28, 8, Register> gpr28;
  585. BitField<39, 8, Register> gpr39;
  586. BitField<48, 16, u64> opcode;
  587. union {
  588. BitField<8, 5, ConditionCode> cc;
  589. BitField<13, 1, u64> trigger;
  590. } nop;
  591. union {
  592. BitField<48, 2, VoteOperation> operation;
  593. BitField<45, 3, u64> dest_pred;
  594. BitField<39, 3, u64> value;
  595. BitField<42, 1, u64> negate_value;
  596. } vote;
  597. union {
  598. BitField<30, 2, ShuffleOperation> operation;
  599. BitField<48, 3, u64> pred48;
  600. BitField<28, 1, u64> is_index_imm;
  601. BitField<29, 1, u64> is_mask_imm;
  602. BitField<20, 5, u64> index_imm;
  603. BitField<34, 13, u64> mask_imm;
  604. } shfl;
  605. union {
  606. BitField<44, 1, u64> ftz;
  607. BitField<39, 2, u64> tab5cb8_2;
  608. BitField<38, 1, u64> ndv;
  609. BitField<47, 1, u64> cc;
  610. BitField<28, 8, u64> swizzle;
  611. } fswzadd;
  612. union {
  613. BitField<8, 8, Register> gpr;
  614. BitField<20, 24, s64> offset;
  615. } gmem;
  616. union {
  617. BitField<20, 16, u64> imm20_16;
  618. BitField<20, 19, u64> imm20_19;
  619. BitField<20, 32, s64> imm20_32;
  620. BitField<45, 1, u64> negate_b;
  621. BitField<46, 1, u64> abs_a;
  622. BitField<48, 1, u64> negate_a;
  623. BitField<49, 1, u64> abs_b;
  624. BitField<50, 1, u64> saturate_d;
  625. BitField<56, 1, u64> negate_imm;
  626. union {
  627. BitField<39, 3, u64> pred;
  628. BitField<42, 1, u64> negate_pred;
  629. } fmnmx;
  630. union {
  631. BitField<39, 1, u64> invert_a;
  632. BitField<40, 1, u64> invert_b;
  633. BitField<41, 2, LogicOperation> operation;
  634. BitField<44, 2, PredicateResultMode> pred_result_mode;
  635. BitField<48, 3, Pred> pred48;
  636. } lop;
  637. union {
  638. BitField<53, 2, LogicOperation> operation;
  639. BitField<55, 1, u64> invert_a;
  640. BitField<56, 1, u64> invert_b;
  641. } lop32i;
  642. union {
  643. BitField<28, 8, u64> imm_lut28;
  644. BitField<48, 8, u64> imm_lut48;
  645. [[nodiscard]] u32 GetImmLut28() const {
  646. return static_cast<u32>(imm_lut28);
  647. }
  648. [[nodiscard]] u32 GetImmLut48() const {
  649. return static_cast<u32>(imm_lut48);
  650. }
  651. } lop3;
  652. [[nodiscard]] u16 GetImm20_16() const {
  653. return static_cast<u16>(imm20_16);
  654. }
  655. [[nodiscard]] u32 GetImm20_19() const {
  656. u32 imm{static_cast<u32>(imm20_19)};
  657. imm <<= 12;
  658. imm |= negate_imm ? 0x80000000 : 0;
  659. return imm;
  660. }
  661. [[nodiscard]] u32 GetImm20_32() const {
  662. return static_cast<u32>(imm20_32);
  663. }
  664. [[nodiscard]] s32 GetSignedImm20_20() const {
  665. const auto immediate = static_cast<u32>(imm20_19 | (negate_imm << 19));
  666. // Sign extend the 20-bit value.
  667. const auto mask = 1U << (20 - 1);
  668. return static_cast<s32>((immediate ^ mask) - mask);
  669. }
  670. } alu;
  671. union {
  672. BitField<38, 1, u64> idx;
  673. BitField<51, 1, u64> saturate;
  674. BitField<52, 2, IpaSampleMode> sample_mode;
  675. BitField<54, 2, IpaInterpMode> interp_mode;
  676. } ipa;
  677. union {
  678. BitField<39, 2, u64> tab5cb8_2;
  679. BitField<41, 3, u64> postfactor;
  680. BitField<44, 2, u64> tab5c68_0;
  681. BitField<48, 1, u64> negate_b;
  682. } fmul;
  683. union {
  684. BitField<55, 1, u64> saturate;
  685. } fmul32;
  686. union {
  687. BitField<52, 1, u64> generates_cc;
  688. } op_32;
  689. union {
  690. BitField<48, 1, u64> is_signed;
  691. } shift;
  692. union {
  693. BitField<39, 1, u64> wrap;
  694. } shr;
  695. union {
  696. BitField<37, 2, ShfType> type;
  697. BitField<48, 2, ShfXmode> xmode;
  698. BitField<50, 1, u64> wrap;
  699. BitField<20, 6, u64> immediate;
  700. } shf;
  701. union {
  702. BitField<39, 5, u64> shift_amount;
  703. BitField<48, 1, u64> negate_b;
  704. BitField<49, 1, u64> negate_a;
  705. } alu_integer;
  706. union {
  707. BitField<43, 1, u64> x;
  708. } iadd;
  709. union {
  710. BitField<39, 1, u64> ftz;
  711. BitField<32, 1, u64> saturate;
  712. BitField<49, 2, HalfMerge> merge;
  713. BitField<44, 1, u64> abs_a;
  714. BitField<47, 2, HalfType> type_a;
  715. BitField<30, 1, u64> abs_b;
  716. BitField<28, 2, HalfType> type_b;
  717. BitField<35, 2, HalfType> type_c;
  718. } alu_half;
  719. union {
  720. BitField<39, 2, HalfPrecision> precision;
  721. BitField<39, 1, u64> ftz;
  722. BitField<52, 1, u64> saturate;
  723. BitField<49, 2, HalfMerge> merge;
  724. BitField<43, 1, u64> negate_a;
  725. BitField<44, 1, u64> abs_a;
  726. BitField<47, 2, HalfType> type_a;
  727. } alu_half_imm;
  728. union {
  729. BitField<29, 1, u64> first_negate;
  730. BitField<20, 9, u64> first;
  731. BitField<56, 1, u64> second_negate;
  732. BitField<30, 9, u64> second;
  733. [[nodiscard]] u32 PackImmediates() const {
  734. // Immediates are half floats shifted.
  735. constexpr u32 imm_shift = 6;
  736. return static_cast<u32>((first << imm_shift) | (second << (16 + imm_shift)));
  737. }
  738. } half_imm;
  739. union {
  740. union {
  741. BitField<37, 2, HalfPrecision> precision;
  742. BitField<32, 1, u64> saturate;
  743. BitField<31, 1, u64> negate_b;
  744. BitField<30, 1, u64> negate_c;
  745. BitField<35, 2, HalfType> type_c;
  746. } rr;
  747. BitField<57, 2, HalfPrecision> precision;
  748. BitField<52, 1, u64> saturate;
  749. BitField<49, 2, HalfMerge> merge;
  750. BitField<47, 2, HalfType> type_a;
  751. BitField<56, 1, u64> negate_b;
  752. BitField<28, 2, HalfType> type_b;
  753. BitField<51, 1, u64> negate_c;
  754. BitField<53, 2, HalfType> type_reg39;
  755. } hfma2;
  756. union {
  757. BitField<40, 1, u64> invert;
  758. } popc;
  759. union {
  760. BitField<41, 1, u64> sh;
  761. BitField<40, 1, u64> invert;
  762. BitField<48, 1, u64> is_signed;
  763. } flo;
  764. union {
  765. BitField<39, 3, u64> pred;
  766. BitField<42, 1, u64> neg_pred;
  767. } sel;
  768. union {
  769. BitField<39, 3, u64> pred;
  770. BitField<42, 1, u64> negate_pred;
  771. BitField<43, 2, IMinMaxExchange> exchange;
  772. BitField<48, 1, u64> is_signed;
  773. } imnmx;
  774. union {
  775. BitField<31, 2, IAdd3Height> height_c;
  776. BitField<33, 2, IAdd3Height> height_b;
  777. BitField<35, 2, IAdd3Height> height_a;
  778. BitField<37, 2, IAdd3Mode> mode;
  779. BitField<49, 1, u64> neg_c;
  780. BitField<50, 1, u64> neg_b;
  781. BitField<51, 1, u64> neg_a;
  782. } iadd3;
  783. union {
  784. BitField<54, 1, u64> saturate;
  785. BitField<56, 1, u64> negate_a;
  786. } iadd32i;
  787. union {
  788. BitField<53, 1, u64> negate_b;
  789. BitField<54, 1, u64> abs_a;
  790. BitField<56, 1, u64> negate_a;
  791. BitField<57, 1, u64> abs_b;
  792. } fadd32i;
  793. union {
  794. BitField<40, 1, u64> brev;
  795. BitField<47, 1, u64> rd_cc;
  796. BitField<48, 1, u64> is_signed;
  797. } bfe;
  798. union {
  799. BitField<48, 3, u64> pred48;
  800. union {
  801. BitField<20, 20, u64> entry_a;
  802. BitField<39, 5, u64> entry_b;
  803. BitField<45, 1, u64> neg;
  804. BitField<46, 1, u64> uses_cc;
  805. } imm;
  806. union {
  807. BitField<20, 14, u64> cb_index;
  808. BitField<34, 5, u64> cb_offset;
  809. BitField<56, 1, u64> neg;
  810. BitField<57, 1, u64> uses_cc;
  811. } hi;
  812. union {
  813. BitField<20, 14, u64> cb_index;
  814. BitField<34, 5, u64> cb_offset;
  815. BitField<39, 5, u64> entry_a;
  816. BitField<45, 1, u64> neg;
  817. BitField<46, 1, u64> uses_cc;
  818. } rz;
  819. union {
  820. BitField<39, 5, u64> entry_a;
  821. BitField<45, 1, u64> neg;
  822. BitField<46, 1, u64> uses_cc;
  823. } r1;
  824. union {
  825. BitField<28, 8, u64> entry_a;
  826. BitField<37, 1, u64> neg;
  827. BitField<38, 1, u64> uses_cc;
  828. } r2;
  829. } lea;
  830. union {
  831. BitField<0, 5, FlowCondition> cond;
  832. } flow;
  833. union {
  834. BitField<47, 1, u64> cc;
  835. BitField<48, 1, u64> negate_b;
  836. BitField<49, 1, u64> negate_c;
  837. BitField<51, 2, u64> tab5980_1;
  838. BitField<53, 2, u64> tab5980_0;
  839. } ffma;
  840. union {
  841. BitField<48, 3, UniformType> type;
  842. BitField<44, 2, u64> unknown;
  843. } ld_c;
  844. union {
  845. BitField<48, 3, StoreType> type;
  846. } ldst_sl;
  847. union {
  848. BitField<44, 2, u64> unknown;
  849. } ld_l;
  850. union {
  851. BitField<44, 2, StoreCacheManagement> cache_management;
  852. } st_l;
  853. union {
  854. BitField<48, 3, UniformType> type;
  855. BitField<46, 2, u64> cache_mode;
  856. } ldg;
  857. union {
  858. BitField<48, 3, UniformType> type;
  859. BitField<46, 2, u64> cache_mode;
  860. } stg;
  861. union {
  862. BitField<23, 3, AtomicOp> operation;
  863. BitField<48, 1, u64> extended;
  864. BitField<20, 3, GlobalAtomicType> type;
  865. } red;
  866. union {
  867. BitField<52, 4, AtomicOp> operation;
  868. BitField<49, 3, GlobalAtomicType> type;
  869. BitField<28, 20, s64> offset;
  870. } atom;
  871. union {
  872. BitField<52, 4, AtomicOp> operation;
  873. BitField<28, 2, AtomicType> type;
  874. BitField<30, 22, s64> offset;
  875. [[nodiscard]] s32 GetImmediateOffset() const {
  876. return static_cast<s32>(offset << 2);
  877. }
  878. } atoms;
  879. union {
  880. BitField<32, 1, PhysicalAttributeDirection> direction;
  881. BitField<47, 3, AttributeSize> size;
  882. BitField<20, 11, u64> address;
  883. } al2p;
  884. union {
  885. BitField<53, 3, UniformType> type;
  886. BitField<52, 1, u64> extended;
  887. } generic;
  888. union {
  889. BitField<0, 3, u64> pred0;
  890. BitField<3, 3, u64> pred3;
  891. BitField<6, 1, u64> neg_b;
  892. BitField<7, 1, u64> abs_a;
  893. BitField<39, 3, u64> pred39;
  894. BitField<42, 1, u64> neg_pred;
  895. BitField<43, 1, u64> neg_a;
  896. BitField<44, 1, u64> abs_b;
  897. BitField<45, 2, PredOperation> op;
  898. BitField<47, 1, u64> ftz;
  899. BitField<48, 4, PredCondition> cond;
  900. } fsetp;
  901. union {
  902. BitField<0, 3, u64> pred0;
  903. BitField<3, 3, u64> pred3;
  904. BitField<39, 3, u64> pred39;
  905. BitField<42, 1, u64> neg_pred;
  906. BitField<45, 2, PredOperation> op;
  907. BitField<48, 1, u64> is_signed;
  908. BitField<49, 3, PredCondition> cond;
  909. } isetp;
  910. union {
  911. BitField<48, 1, u64> is_signed;
  912. BitField<49, 3, PredCondition> cond;
  913. } icmp;
  914. union {
  915. BitField<0, 3, u64> pred0;
  916. BitField<3, 3, u64> pred3;
  917. BitField<12, 3, u64> pred12;
  918. BitField<15, 1, u64> neg_pred12;
  919. BitField<24, 2, PredOperation> cond;
  920. BitField<29, 3, u64> pred29;
  921. BitField<32, 1, u64> neg_pred29;
  922. BitField<39, 3, u64> pred39;
  923. BitField<42, 1, u64> neg_pred39;
  924. BitField<45, 2, PredOperation> op;
  925. } psetp;
  926. union {
  927. BitField<43, 4, PredCondition> cond;
  928. BitField<45, 2, PredOperation> op;
  929. BitField<3, 3, u64> pred3;
  930. BitField<0, 3, u64> pred0;
  931. BitField<39, 3, u64> pred39;
  932. } vsetp;
  933. union {
  934. BitField<12, 3, u64> pred12;
  935. BitField<15, 1, u64> neg_pred12;
  936. BitField<24, 2, PredOperation> cond;
  937. BitField<29, 3, u64> pred29;
  938. BitField<32, 1, u64> neg_pred29;
  939. BitField<39, 3, u64> pred39;
  940. BitField<42, 1, u64> neg_pred39;
  941. BitField<44, 1, u64> bf;
  942. BitField<45, 2, PredOperation> op;
  943. } pset;
  944. union {
  945. BitField<0, 3, u64> pred0;
  946. BitField<3, 3, u64> pred3;
  947. BitField<8, 5, ConditionCode> cc; // flag in cc
  948. BitField<39, 3, u64> pred39;
  949. BitField<42, 1, u64> neg_pred39;
  950. BitField<45, 4, PredOperation> op; // op with pred39
  951. } csetp;
  952. union {
  953. BitField<6, 1, u64> ftz;
  954. BitField<45, 2, PredOperation> op;
  955. BitField<3, 3, u64> pred3;
  956. BitField<0, 3, u64> pred0;
  957. BitField<43, 1, u64> negate_a;
  958. BitField<44, 1, u64> abs_a;
  959. BitField<47, 2, HalfType> type_a;
  960. union {
  961. BitField<35, 4, PredCondition> cond;
  962. BitField<49, 1, u64> h_and;
  963. BitField<31, 1, u64> negate_b;
  964. BitField<30, 1, u64> abs_b;
  965. BitField<28, 2, HalfType> type_b;
  966. } reg;
  967. union {
  968. BitField<56, 1, u64> negate_b;
  969. BitField<54, 1, u64> abs_b;
  970. } cbuf;
  971. union {
  972. BitField<49, 4, PredCondition> cond;
  973. BitField<53, 1, u64> h_and;
  974. } cbuf_and_imm;
  975. BitField<42, 1, u64> neg_pred;
  976. BitField<39, 3, u64> pred39;
  977. } hsetp2;
  978. union {
  979. BitField<40, 1, R2pMode> mode;
  980. BitField<41, 2, u64> byte;
  981. BitField<20, 7, u64> immediate_mask;
  982. } p2r_r2p;
  983. union {
  984. BitField<39, 3, u64> pred39;
  985. BitField<42, 1, u64> neg_pred;
  986. BitField<43, 1, u64> neg_a;
  987. BitField<44, 1, u64> abs_b;
  988. BitField<45, 2, PredOperation> op;
  989. BitField<48, 4, PredCondition> cond;
  990. BitField<52, 1, u64> bf;
  991. BitField<53, 1, u64> neg_b;
  992. BitField<54, 1, u64> abs_a;
  993. BitField<55, 1, u64> ftz;
  994. } fset;
  995. union {
  996. BitField<47, 1, u64> ftz;
  997. BitField<48, 4, PredCondition> cond;
  998. } fcmp;
  999. union {
  1000. BitField<49, 1, u64> bf;
  1001. BitField<35, 3, PredCondition> cond;
  1002. BitField<50, 1, u64> ftz;
  1003. BitField<45, 2, PredOperation> op;
  1004. BitField<43, 1, u64> negate_a;
  1005. BitField<44, 1, u64> abs_a;
  1006. BitField<47, 2, HalfType> type_a;
  1007. BitField<31, 1, u64> negate_b;
  1008. BitField<30, 1, u64> abs_b;
  1009. BitField<28, 2, HalfType> type_b;
  1010. BitField<42, 1, u64> neg_pred;
  1011. BitField<39, 3, u64> pred39;
  1012. } hset2;
  1013. union {
  1014. BitField<39, 3, u64> pred39;
  1015. BitField<42, 1, u64> neg_pred;
  1016. BitField<44, 1, u64> bf;
  1017. BitField<45, 2, PredOperation> op;
  1018. BitField<48, 1, u64> is_signed;
  1019. BitField<49, 3, PredCondition> cond;
  1020. } iset;
  1021. union {
  1022. BitField<45, 1, u64> negate_a;
  1023. BitField<49, 1, u64> abs_a;
  1024. BitField<10, 2, Register::Size> src_size;
  1025. BitField<13, 1, u64> is_input_signed;
  1026. BitField<8, 2, Register::Size> dst_size;
  1027. BitField<12, 1, u64> is_output_signed;
  1028. union {
  1029. BitField<39, 2, u64> tab5cb8_2;
  1030. } i2f;
  1031. union {
  1032. BitField<39, 2, F2iRoundingOp> rounding;
  1033. } f2i;
  1034. union {
  1035. BitField<39, 4, u64> rounding;
  1036. // H0, H1 extract for F16 missing
  1037. BitField<41, 1, u64> selector; // Guessed as some games set it, TODO: reverse this value
  1038. [[nodiscard]] F2fRoundingOp GetRoundingMode() const {
  1039. constexpr u64 rounding_mask = 0x0B;
  1040. return static_cast<F2fRoundingOp>(rounding.Value() & rounding_mask);
  1041. }
  1042. } f2f;
  1043. union {
  1044. BitField<41, 2, u64> selector;
  1045. } int_src;
  1046. union {
  1047. BitField<41, 1, u64> selector;
  1048. } float_src;
  1049. } conversion;
  1050. union {
  1051. BitField<28, 1, u64> array;
  1052. BitField<29, 2, TextureType> texture_type;
  1053. BitField<31, 4, u64> component_mask;
  1054. BitField<49, 1, u64> nodep_flag;
  1055. BitField<50, 1, u64> dc_flag;
  1056. BitField<54, 1, u64> aoffi_flag;
  1057. BitField<55, 3, TextureProcessMode> process_mode;
  1058. [[nodiscard]] bool IsComponentEnabled(std::size_t component) const {
  1059. return ((1ULL << component) & component_mask) != 0;
  1060. }
  1061. [[nodiscard]] TextureProcessMode GetTextureProcessMode() const {
  1062. return process_mode;
  1063. }
  1064. [[nodiscard]] bool UsesMiscMode(TextureMiscMode mode) const {
  1065. switch (mode) {
  1066. case TextureMiscMode::DC:
  1067. return dc_flag != 0;
  1068. case TextureMiscMode::NODEP:
  1069. return nodep_flag != 0;
  1070. case TextureMiscMode::AOFFI:
  1071. return aoffi_flag != 0;
  1072. default:
  1073. break;
  1074. }
  1075. return false;
  1076. }
  1077. } tex;
  1078. union {
  1079. BitField<28, 1, u64> array;
  1080. BitField<29, 2, TextureType> texture_type;
  1081. BitField<31, 4, u64> component_mask;
  1082. BitField<49, 1, u64> nodep_flag;
  1083. BitField<50, 1, u64> dc_flag;
  1084. BitField<36, 1, u64> aoffi_flag;
  1085. BitField<37, 3, TextureProcessMode> process_mode;
  1086. [[nodiscard]] bool IsComponentEnabled(std::size_t component) const {
  1087. return ((1ULL << component) & component_mask) != 0;
  1088. }
  1089. [[nodiscard]] TextureProcessMode GetTextureProcessMode() const {
  1090. return process_mode;
  1091. }
  1092. [[nodiscard]] bool UsesMiscMode(TextureMiscMode mode) const {
  1093. switch (mode) {
  1094. case TextureMiscMode::DC:
  1095. return dc_flag != 0;
  1096. case TextureMiscMode::NODEP:
  1097. return nodep_flag != 0;
  1098. case TextureMiscMode::AOFFI:
  1099. return aoffi_flag != 0;
  1100. default:
  1101. break;
  1102. }
  1103. return false;
  1104. }
  1105. } tex_b;
  1106. union {
  1107. BitField<22, 6, TextureQueryType> query_type;
  1108. BitField<31, 4, u64> component_mask;
  1109. BitField<49, 1, u64> nodep_flag;
  1110. [[nodiscard]] bool UsesMiscMode(TextureMiscMode mode) const {
  1111. switch (mode) {
  1112. case TextureMiscMode::NODEP:
  1113. return nodep_flag != 0;
  1114. default:
  1115. break;
  1116. }
  1117. return false;
  1118. }
  1119. [[nodiscard]] bool IsComponentEnabled(std::size_t component) const {
  1120. return ((1ULL << component) & component_mask) != 0;
  1121. }
  1122. } txq;
  1123. union {
  1124. BitField<28, 1, u64> array;
  1125. BitField<29, 2, TextureType> texture_type;
  1126. BitField<31, 4, u64> component_mask;
  1127. BitField<35, 1, u64> ndv_flag;
  1128. BitField<49, 1, u64> nodep_flag;
  1129. [[nodiscard]] bool IsComponentEnabled(std::size_t component) const {
  1130. return ((1ULL << component) & component_mask) != 0;
  1131. }
  1132. [[nodiscard]] bool UsesMiscMode(TextureMiscMode mode) const {
  1133. switch (mode) {
  1134. case TextureMiscMode::NDV:
  1135. return (ndv_flag != 0);
  1136. case TextureMiscMode::NODEP:
  1137. return (nodep_flag != 0);
  1138. default:
  1139. break;
  1140. }
  1141. return false;
  1142. }
  1143. } tmml;
  1144. union {
  1145. BitField<28, 1, u64> array;
  1146. BitField<29, 2, TextureType> texture_type;
  1147. BitField<35, 1, u64> ndv_flag;
  1148. BitField<49, 1, u64> nodep_flag;
  1149. BitField<50, 1, u64> dc_flag;
  1150. BitField<54, 2, u64> offset_mode;
  1151. BitField<56, 2, u64> component;
  1152. [[nodiscard]] bool UsesMiscMode(TextureMiscMode mode) const {
  1153. switch (mode) {
  1154. case TextureMiscMode::NDV:
  1155. return ndv_flag != 0;
  1156. case TextureMiscMode::NODEP:
  1157. return nodep_flag != 0;
  1158. case TextureMiscMode::DC:
  1159. return dc_flag != 0;
  1160. case TextureMiscMode::AOFFI:
  1161. return offset_mode == 1;
  1162. case TextureMiscMode::PTP:
  1163. return offset_mode == 2;
  1164. default:
  1165. break;
  1166. }
  1167. return false;
  1168. }
  1169. } tld4;
  1170. union {
  1171. BitField<35, 1, u64> ndv_flag;
  1172. BitField<49, 1, u64> nodep_flag;
  1173. BitField<50, 1, u64> dc_flag;
  1174. BitField<33, 2, u64> offset_mode;
  1175. BitField<37, 2, u64> component;
  1176. [[nodiscard]] bool UsesMiscMode(TextureMiscMode mode) const {
  1177. switch (mode) {
  1178. case TextureMiscMode::NDV:
  1179. return ndv_flag != 0;
  1180. case TextureMiscMode::NODEP:
  1181. return nodep_flag != 0;
  1182. case TextureMiscMode::DC:
  1183. return dc_flag != 0;
  1184. case TextureMiscMode::AOFFI:
  1185. return offset_mode == 1;
  1186. case TextureMiscMode::PTP:
  1187. return offset_mode == 2;
  1188. default:
  1189. break;
  1190. }
  1191. return false;
  1192. }
  1193. } tld4_b;
  1194. union {
  1195. BitField<49, 1, u64> nodep_flag;
  1196. BitField<50, 1, u64> dc_flag;
  1197. BitField<51, 1, u64> aoffi_flag;
  1198. BitField<52, 2, u64> component;
  1199. BitField<55, 1, u64> fp16_flag;
  1200. [[nodiscard]] bool UsesMiscMode(TextureMiscMode mode) const {
  1201. switch (mode) {
  1202. case TextureMiscMode::DC:
  1203. return dc_flag != 0;
  1204. case TextureMiscMode::NODEP:
  1205. return nodep_flag != 0;
  1206. case TextureMiscMode::AOFFI:
  1207. return aoffi_flag != 0;
  1208. default:
  1209. break;
  1210. }
  1211. return false;
  1212. }
  1213. } tld4s;
  1214. union {
  1215. BitField<0, 8, Register> gpr0;
  1216. BitField<28, 8, Register> gpr28;
  1217. BitField<49, 1, u64> nodep_flag;
  1218. BitField<50, 3, u64> component_mask_selector;
  1219. BitField<53, 4, u64> texture_info;
  1220. BitField<59, 1, u64> fp32_flag;
  1221. [[nodiscard]] TextureType GetTextureType() const {
  1222. // The TEXS instruction has a weird encoding for the texture type.
  1223. if (texture_info == 0) {
  1224. return TextureType::Texture1D;
  1225. }
  1226. if (texture_info >= 1 && texture_info <= 9) {
  1227. return TextureType::Texture2D;
  1228. }
  1229. if (texture_info >= 10 && texture_info <= 11) {
  1230. return TextureType::Texture3D;
  1231. }
  1232. if (texture_info >= 12 && texture_info <= 13) {
  1233. return TextureType::TextureCube;
  1234. }
  1235. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}", texture_info.Value());
  1236. UNREACHABLE();
  1237. return TextureType::Texture1D;
  1238. }
  1239. [[nodiscard]] TextureProcessMode GetTextureProcessMode() const {
  1240. switch (texture_info) {
  1241. case 0:
  1242. case 2:
  1243. case 6:
  1244. case 8:
  1245. case 9:
  1246. case 11:
  1247. return TextureProcessMode::LZ;
  1248. case 3:
  1249. case 5:
  1250. case 13:
  1251. return TextureProcessMode::LL;
  1252. default:
  1253. break;
  1254. }
  1255. return TextureProcessMode::None;
  1256. }
  1257. [[nodiscard]] bool UsesMiscMode(TextureMiscMode mode) const {
  1258. switch (mode) {
  1259. case TextureMiscMode::DC:
  1260. return (texture_info >= 4 && texture_info <= 6) || texture_info == 9;
  1261. case TextureMiscMode::NODEP:
  1262. return nodep_flag != 0;
  1263. default:
  1264. break;
  1265. }
  1266. return false;
  1267. }
  1268. [[nodiscard]] bool IsArrayTexture() const {
  1269. // TEXS only supports Texture2D arrays.
  1270. return texture_info >= 7 && texture_info <= 9;
  1271. }
  1272. [[nodiscard]] bool HasTwoDestinations() const {
  1273. return gpr28.Value() != Register::ZeroIndex;
  1274. }
  1275. [[nodiscard]] bool IsComponentEnabled(std::size_t component) const {
  1276. static constexpr std::array<std::array<u32, 8>, 4> mask_lut{{
  1277. {},
  1278. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  1279. {0x1, 0x2, 0x4, 0x8, 0x3, 0x9, 0xa, 0xc},
  1280. {0x7, 0xb, 0xd, 0xe, 0xf},
  1281. }};
  1282. std::size_t index{gpr0.Value() != Register::ZeroIndex ? 1U : 0U};
  1283. index |= gpr28.Value() != Register::ZeroIndex ? 2 : 0;
  1284. u32 mask = mask_lut[index][component_mask_selector];
  1285. // A mask of 0 means this instruction uses an unimplemented mask.
  1286. ASSERT(mask != 0);
  1287. return ((1ull << component) & mask) != 0;
  1288. }
  1289. } texs;
  1290. union {
  1291. BitField<28, 1, u64> is_array;
  1292. BitField<29, 2, TextureType> texture_type;
  1293. BitField<35, 1, u64> aoffi;
  1294. BitField<49, 1, u64> nodep_flag;
  1295. BitField<50, 1, u64> ms; // Multisample?
  1296. BitField<54, 1, u64> cl;
  1297. BitField<55, 1, u64> process_mode;
  1298. [[nodiscard]] TextureProcessMode GetTextureProcessMode() const {
  1299. return process_mode == 0 ? TextureProcessMode::LZ : TextureProcessMode::LL;
  1300. }
  1301. } tld;
  1302. union {
  1303. BitField<49, 1, u64> nodep_flag;
  1304. BitField<53, 4, u64> texture_info;
  1305. BitField<59, 1, u64> fp32_flag;
  1306. [[nodiscard]] TextureType GetTextureType() const {
  1307. // The TLDS instruction has a weird encoding for the texture type.
  1308. if (texture_info <= 1) {
  1309. return TextureType::Texture1D;
  1310. }
  1311. if (texture_info == 2 || texture_info == 8 || texture_info == 12 ||
  1312. (texture_info >= 4 && texture_info <= 6)) {
  1313. return TextureType::Texture2D;
  1314. }
  1315. if (texture_info == 7) {
  1316. return TextureType::Texture3D;
  1317. }
  1318. LOG_CRITICAL(HW_GPU, "Unhandled texture_info: {}", texture_info.Value());
  1319. UNREACHABLE();
  1320. return TextureType::Texture1D;
  1321. }
  1322. [[nodiscard]] TextureProcessMode GetTextureProcessMode() const {
  1323. if (texture_info == 1 || texture_info == 5 || texture_info == 12) {
  1324. return TextureProcessMode::LL;
  1325. }
  1326. return TextureProcessMode::LZ;
  1327. }
  1328. [[nodiscard]] bool UsesMiscMode(TextureMiscMode mode) const {
  1329. switch (mode) {
  1330. case TextureMiscMode::AOFFI:
  1331. return texture_info == 12 || texture_info == 4;
  1332. case TextureMiscMode::MZ:
  1333. return texture_info == 5;
  1334. case TextureMiscMode::NODEP:
  1335. return nodep_flag != 0;
  1336. default:
  1337. break;
  1338. }
  1339. return false;
  1340. }
  1341. [[nodiscard]] bool IsArrayTexture() const {
  1342. // TEXS only supports Texture2D arrays.
  1343. return texture_info == 8;
  1344. }
  1345. } tlds;
  1346. union {
  1347. BitField<28, 1, u64> is_array;
  1348. BitField<29, 2, TextureType> texture_type;
  1349. BitField<35, 1, u64> aoffi_flag;
  1350. BitField<49, 1, u64> nodep_flag;
  1351. [[nodiscard]] bool UsesMiscMode(TextureMiscMode mode) const {
  1352. switch (mode) {
  1353. case TextureMiscMode::AOFFI:
  1354. return aoffi_flag != 0;
  1355. case TextureMiscMode::NODEP:
  1356. return nodep_flag != 0;
  1357. default:
  1358. break;
  1359. }
  1360. return false;
  1361. }
  1362. } txd;
  1363. union {
  1364. BitField<24, 2, StoreCacheManagement> cache_management;
  1365. BitField<33, 3, ImageType> image_type;
  1366. BitField<49, 2, OutOfBoundsStore> out_of_bounds_store;
  1367. BitField<51, 1, u64> is_immediate;
  1368. BitField<52, 1, SurfaceDataMode> mode;
  1369. BitField<20, 3, StoreType> store_data_layout;
  1370. BitField<20, 4, u64> component_mask_selector;
  1371. [[nodiscard]] bool IsComponentEnabled(std::size_t component) const {
  1372. ASSERT(mode == SurfaceDataMode::P);
  1373. constexpr u8 R = 0b0001;
  1374. constexpr u8 G = 0b0010;
  1375. constexpr u8 B = 0b0100;
  1376. constexpr u8 A = 0b1000;
  1377. constexpr std::array<u8, 16> mask = {
  1378. 0, (R), (G), (R | G), (B), (R | B),
  1379. (G | B), (R | G | B), (A), (R | A), (G | A), (R | G | A),
  1380. (B | A), (R | B | A), (G | B | A), (R | G | B | A)};
  1381. return std::bitset<4>{mask.at(component_mask_selector)}.test(component);
  1382. }
  1383. [[nodiscard]] StoreType GetStoreDataLayout() const {
  1384. ASSERT(mode == SurfaceDataMode::D_BA);
  1385. return store_data_layout;
  1386. }
  1387. } suldst;
  1388. union {
  1389. BitField<28, 1, u64> is_ba;
  1390. BitField<51, 3, ImageAtomicOperationType> operation_type;
  1391. BitField<33, 3, ImageType> image_type;
  1392. BitField<29, 4, ImageAtomicOperation> operation;
  1393. BitField<49, 2, OutOfBoundsStore> out_of_bounds_store;
  1394. } suatom_d;
  1395. union {
  1396. BitField<20, 24, u64> target;
  1397. BitField<5, 1, u64> constant_buffer;
  1398. [[nodiscard]] s32 GetBranchTarget() const {
  1399. // Sign extend the branch target offset
  1400. const auto mask = 1U << (24 - 1);
  1401. const auto target_value = static_cast<u32>(target);
  1402. constexpr auto instruction_size = static_cast<s32>(sizeof(Instruction));
  1403. // The branch offset is relative to the next instruction and is stored in bytes, so
  1404. // divide it by the size of an instruction and add 1 to it.
  1405. return static_cast<s32>((target_value ^ mask) - mask) / instruction_size + 1;
  1406. }
  1407. } bra;
  1408. union {
  1409. BitField<20, 24, u64> target;
  1410. BitField<5, 1, u64> constant_buffer;
  1411. [[nodiscard]] s32 GetBranchExtend() const {
  1412. // Sign extend the branch target offset
  1413. const auto mask = 1U << (24 - 1);
  1414. const auto target_value = static_cast<u32>(target);
  1415. constexpr auto instruction_size = static_cast<s32>(sizeof(Instruction));
  1416. // The branch offset is relative to the next instruction and is stored in bytes, so
  1417. // divide it by the size of an instruction and add 1 to it.
  1418. return static_cast<s32>((target_value ^ mask) - mask) / instruction_size + 1;
  1419. }
  1420. } brx;
  1421. union {
  1422. BitField<39, 1, u64> emit; // EmitVertex
  1423. BitField<40, 1, u64> cut; // EndPrimitive
  1424. } out;
  1425. union {
  1426. BitField<31, 1, u64> skew;
  1427. BitField<32, 1, u64> o;
  1428. BitField<33, 2, IsberdMode> mode;
  1429. BitField<47, 2, IsberdShift> shift;
  1430. } isberd;
  1431. union {
  1432. BitField<8, 2, MembarType> type;
  1433. BitField<0, 2, MembarUnknown> unknown;
  1434. } membar;
  1435. union {
  1436. BitField<48, 1, u64> signed_a;
  1437. BitField<38, 1, u64> is_byte_chunk_a;
  1438. BitField<36, 2, VideoType> type_a;
  1439. BitField<36, 2, u64> byte_height_a;
  1440. BitField<49, 1, u64> signed_b;
  1441. BitField<50, 1, u64> use_register_b;
  1442. BitField<30, 1, u64> is_byte_chunk_b;
  1443. BitField<28, 2, VideoType> type_b;
  1444. BitField<28, 2, u64> byte_height_b;
  1445. } video;
  1446. union {
  1447. BitField<51, 2, VmadShr> shr;
  1448. BitField<55, 1, u64> saturate; // Saturates the result (a * b + c)
  1449. BitField<47, 1, u64> cc;
  1450. } vmad;
  1451. union {
  1452. BitField<54, 1, u64> is_dest_signed;
  1453. BitField<48, 1, u64> is_src_a_signed;
  1454. BitField<49, 1, u64> is_src_b_signed;
  1455. BitField<37, 2, u64> src_format_a;
  1456. BitField<29, 2, u64> src_format_b;
  1457. BitField<56, 1, u64> mx;
  1458. BitField<55, 1, u64> sat;
  1459. BitField<36, 2, u64> selector_a;
  1460. BitField<28, 2, u64> selector_b;
  1461. BitField<50, 1, u64> is_op_b_register;
  1462. BitField<51, 3, VmnmxOperation> operation;
  1463. [[nodiscard]] VmnmxType SourceFormatA() const {
  1464. switch (src_format_a) {
  1465. case 0b11:
  1466. return VmnmxType::Bits32;
  1467. case 0b10:
  1468. return VmnmxType::Bits16;
  1469. default:
  1470. return VmnmxType::Bits8;
  1471. }
  1472. }
  1473. [[nodiscard]] VmnmxType SourceFormatB() const {
  1474. switch (src_format_b) {
  1475. case 0b11:
  1476. return VmnmxType::Bits32;
  1477. case 0b10:
  1478. return VmnmxType::Bits16;
  1479. default:
  1480. return VmnmxType::Bits8;
  1481. }
  1482. }
  1483. } vmnmx;
  1484. union {
  1485. BitField<20, 16, u64> imm20_16;
  1486. BitField<35, 1, u64> high_b_rr; // used on RR
  1487. BitField<36, 1, u64> product_shift_left;
  1488. BitField<37, 1, u64> merge_37;
  1489. BitField<48, 1, u64> sign_a;
  1490. BitField<49, 1, u64> sign_b;
  1491. BitField<50, 2, XmadMode> mode_cbf; // used by CR, RC
  1492. BitField<50, 3, XmadMode> mode;
  1493. BitField<52, 1, u64> high_b;
  1494. BitField<53, 1, u64> high_a;
  1495. BitField<55, 1, u64> product_shift_left_second; // used on CR
  1496. BitField<56, 1, u64> merge_56;
  1497. } xmad;
  1498. union {
  1499. BitField<20, 14, u64> shifted_offset;
  1500. BitField<34, 5, u64> index;
  1501. [[nodiscard]] u64 GetOffset() const {
  1502. return shifted_offset * 4;
  1503. }
  1504. } cbuf34;
  1505. union {
  1506. BitField<20, 16, s64> offset;
  1507. BitField<36, 5, u64> index;
  1508. [[nodiscard]] s64 GetOffset() const {
  1509. return offset;
  1510. }
  1511. } cbuf36;
  1512. // Unsure about the size of this one.
  1513. // It's always used with a gpr0, so any size should be fine.
  1514. BitField<20, 8, SystemVariable> sys20;
  1515. BitField<47, 1, u64> generates_cc;
  1516. BitField<61, 1, u64> is_b_imm;
  1517. BitField<60, 1, u64> is_b_gpr;
  1518. BitField<59, 1, u64> is_c_gpr;
  1519. BitField<20, 24, s64> smem_imm;
  1520. BitField<0, 5, ConditionCode> flow_condition_code;
  1521. Attribute attribute;
  1522. Sampler sampler;
  1523. Image image;
  1524. u64 value;
  1525. };
  1526. static_assert(sizeof(Instruction) == 0x8, "Incorrect structure size");
  1527. static_assert(std::is_standard_layout_v<Instruction>, "Instruction is not standard layout");
  1528. class OpCode {
  1529. public:
  1530. enum class Id {
  1531. KIL,
  1532. SSY,
  1533. SYNC,
  1534. BRK,
  1535. DEPBAR,
  1536. VOTE,
  1537. VOTE_VTG,
  1538. SHFL,
  1539. FSWZADD,
  1540. BFE_C,
  1541. BFE_R,
  1542. BFE_IMM,
  1543. BFI_RC,
  1544. BFI_IMM_R,
  1545. BRA,
  1546. BRX,
  1547. PBK,
  1548. LD_A,
  1549. LD_L,
  1550. LD_S,
  1551. LD_C,
  1552. LD, // Load from generic memory
  1553. LDG, // Load from global memory
  1554. ST_A,
  1555. ST_L,
  1556. ST_S,
  1557. ST, // Store in generic memory
  1558. STG, // Store in global memory
  1559. RED, // Reduction operation
  1560. ATOM, // Atomic operation on global memory
  1561. ATOMS, // Atomic operation on shared memory
  1562. AL2P, // Transforms attribute memory into physical memory
  1563. TEX,
  1564. TEX_B, // Texture Load Bindless
  1565. TXQ, // Texture Query
  1566. TXQ_B, // Texture Query Bindless
  1567. TEXS, // Texture Fetch with scalar/non-vec4 source/destinations
  1568. TLD, // Texture Load
  1569. TLDS, // Texture Load with scalar/non-vec4 source/destinations
  1570. TLD4, // Texture Gather 4
  1571. TLD4_B, // Texture Gather 4 Bindless
  1572. TLD4S, // Texture Load 4 with scalar / non - vec4 source / destinations
  1573. TMML_B, // Texture Mip Map Level
  1574. TMML, // Texture Mip Map Level
  1575. TXD, // Texture Gradient/Load with Derivates
  1576. TXD_B, // Texture Gradient/Load with Derivates Bindless
  1577. SUST, // Surface Store
  1578. SULD, // Surface Load
  1579. SUATOM, // Surface Atomic Operation
  1580. EXIT,
  1581. NOP,
  1582. IPA,
  1583. OUT_R, // Emit vertex/primitive
  1584. ISBERD,
  1585. BAR,
  1586. MEMBAR,
  1587. VMAD,
  1588. VSETP,
  1589. VMNMX,
  1590. FFMA_IMM, // Fused Multiply and Add
  1591. FFMA_CR,
  1592. FFMA_RC,
  1593. FFMA_RR,
  1594. FADD_C,
  1595. FADD_R,
  1596. FADD_IMM,
  1597. FADD32I,
  1598. FMUL_C,
  1599. FMUL_R,
  1600. FMUL_IMM,
  1601. FMUL32_IMM,
  1602. IADD_C,
  1603. IADD_R,
  1604. IADD_IMM,
  1605. IADD3_C, // Add 3 Integers
  1606. IADD3_R,
  1607. IADD3_IMM,
  1608. IADD32I,
  1609. ISCADD_C, // Scale and Add
  1610. ISCADD_R,
  1611. ISCADD_IMM,
  1612. FLO_R,
  1613. FLO_C,
  1614. FLO_IMM,
  1615. LEA_R1,
  1616. LEA_R2,
  1617. LEA_RZ,
  1618. LEA_IMM,
  1619. LEA_HI,
  1620. HADD2_C,
  1621. HADD2_R,
  1622. HADD2_IMM,
  1623. HMUL2_C,
  1624. HMUL2_R,
  1625. HMUL2_IMM,
  1626. HFMA2_CR,
  1627. HFMA2_RC,
  1628. HFMA2_RR,
  1629. HFMA2_IMM_R,
  1630. HSETP2_C,
  1631. HSETP2_R,
  1632. HSETP2_IMM,
  1633. HSET2_C,
  1634. HSET2_R,
  1635. HSET2_IMM,
  1636. POPC_C,
  1637. POPC_R,
  1638. POPC_IMM,
  1639. SEL_C,
  1640. SEL_R,
  1641. SEL_IMM,
  1642. ICMP_RC,
  1643. ICMP_R,
  1644. ICMP_CR,
  1645. ICMP_IMM,
  1646. FCMP_RR,
  1647. FCMP_RC,
  1648. FCMP_IMMR,
  1649. MUFU, // Multi-Function Operator
  1650. RRO_C, // Range Reduction Operator
  1651. RRO_R,
  1652. RRO_IMM,
  1653. F2F_C,
  1654. F2F_R,
  1655. F2F_IMM,
  1656. F2I_C,
  1657. F2I_R,
  1658. F2I_IMM,
  1659. I2F_C,
  1660. I2F_R,
  1661. I2F_IMM,
  1662. I2I_C,
  1663. I2I_R,
  1664. I2I_IMM,
  1665. LOP_C,
  1666. LOP_R,
  1667. LOP_IMM,
  1668. LOP32I,
  1669. LOP3_C,
  1670. LOP3_R,
  1671. LOP3_IMM,
  1672. MOV_C,
  1673. MOV_R,
  1674. MOV_IMM,
  1675. S2R,
  1676. MOV32_IMM,
  1677. SHL_C,
  1678. SHL_R,
  1679. SHL_IMM,
  1680. SHR_C,
  1681. SHR_R,
  1682. SHR_IMM,
  1683. SHF_RIGHT_R,
  1684. SHF_RIGHT_IMM,
  1685. SHF_LEFT_R,
  1686. SHF_LEFT_IMM,
  1687. FMNMX_C,
  1688. FMNMX_R,
  1689. FMNMX_IMM,
  1690. IMNMX_C,
  1691. IMNMX_R,
  1692. IMNMX_IMM,
  1693. FSETP_C, // Set Predicate
  1694. FSETP_R,
  1695. FSETP_IMM,
  1696. FSET_C,
  1697. FSET_R,
  1698. FSET_IMM,
  1699. ISETP_C,
  1700. ISETP_IMM,
  1701. ISETP_R,
  1702. ISET_R,
  1703. ISET_C,
  1704. ISET_IMM,
  1705. PSETP,
  1706. PSET,
  1707. CSETP,
  1708. R2P_IMM,
  1709. P2R_IMM,
  1710. XMAD_IMM,
  1711. XMAD_CR,
  1712. XMAD_RC,
  1713. XMAD_RR,
  1714. };
  1715. enum class Type {
  1716. Trivial,
  1717. Arithmetic,
  1718. ArithmeticImmediate,
  1719. ArithmeticInteger,
  1720. ArithmeticIntegerImmediate,
  1721. ArithmeticHalf,
  1722. ArithmeticHalfImmediate,
  1723. Bfe,
  1724. Bfi,
  1725. Shift,
  1726. Ffma,
  1727. Hfma2,
  1728. Flow,
  1729. Synch,
  1730. Warp,
  1731. Memory,
  1732. Texture,
  1733. Image,
  1734. FloatSet,
  1735. FloatSetPredicate,
  1736. IntegerSet,
  1737. IntegerSetPredicate,
  1738. HalfSet,
  1739. HalfSetPredicate,
  1740. PredicateSetPredicate,
  1741. PredicateSetRegister,
  1742. RegisterSetPredicate,
  1743. Conversion,
  1744. Video,
  1745. Xmad,
  1746. Unknown,
  1747. };
  1748. /// Returns whether an opcode has an execution predicate field or not (ie, whether it can be
  1749. /// conditionally executed).
  1750. [[nodiscard]] static bool IsPredicatedInstruction(Id opcode) {
  1751. // TODO(Subv): Add the rest of unpredicated instructions.
  1752. return opcode != Id::SSY && opcode != Id::PBK;
  1753. }
  1754. class Matcher {
  1755. public:
  1756. constexpr Matcher(const char* const name_, u16 mask_, u16 expected_, Id id_, Type type_)
  1757. : name{name_}, mask{mask_}, expected{expected_}, id{id_}, type{type_} {}
  1758. [[nodiscard]] constexpr const char* GetName() const {
  1759. return name;
  1760. }
  1761. [[nodiscard]] constexpr u16 GetMask() const {
  1762. return mask;
  1763. }
  1764. [[nodiscard]] constexpr Id GetId() const {
  1765. return id;
  1766. }
  1767. [[nodiscard]] constexpr Type GetType() const {
  1768. return type;
  1769. }
  1770. /**
  1771. * Tests to see if the given instruction is the instruction this matcher represents.
  1772. * @param instruction The instruction to test
  1773. * @returns true if the given instruction matches.
  1774. */
  1775. [[nodiscard]] constexpr bool Matches(u16 instruction) const {
  1776. return (instruction & mask) == expected;
  1777. }
  1778. private:
  1779. const char* name;
  1780. u16 mask;
  1781. u16 expected;
  1782. Id id;
  1783. Type type;
  1784. };
  1785. using DecodeResult = std::optional<std::reference_wrapper<const Matcher>>;
  1786. [[nodiscard]] static DecodeResult Decode(Instruction instr) {
  1787. static const auto table{GetDecodeTable()};
  1788. const auto matches_instruction = [instr](const auto& matcher) {
  1789. return matcher.Matches(static_cast<u16>(instr.opcode));
  1790. };
  1791. auto iter = std::find_if(table.begin(), table.end(), matches_instruction);
  1792. return iter != table.end() ? std::optional<std::reference_wrapper<const Matcher>>(*iter)
  1793. : std::nullopt;
  1794. }
  1795. private:
  1796. struct Detail {
  1797. private:
  1798. static constexpr std::size_t opcode_bitsize = 16;
  1799. /**
  1800. * Generates the mask and the expected value after masking from a given bitstring.
  1801. * A '0' in a bitstring indicates that a zero must be present at that bit position.
  1802. * A '1' in a bitstring indicates that a one must be present at that bit position.
  1803. */
  1804. [[nodiscard]] static constexpr auto GetMaskAndExpect(const char* const bitstring) {
  1805. u16 mask = 0, expect = 0;
  1806. for (std::size_t i = 0; i < opcode_bitsize; i++) {
  1807. const std::size_t bit_position = opcode_bitsize - i - 1;
  1808. switch (bitstring[i]) {
  1809. case '0':
  1810. mask |= static_cast<u16>(1U << bit_position);
  1811. break;
  1812. case '1':
  1813. expect |= static_cast<u16>(1U << bit_position);
  1814. mask |= static_cast<u16>(1U << bit_position);
  1815. break;
  1816. default:
  1817. // Ignore
  1818. break;
  1819. }
  1820. }
  1821. return std::make_pair(mask, expect);
  1822. }
  1823. public:
  1824. /// Creates a matcher that can match and parse instructions based on bitstring.
  1825. [[nodiscard]] static constexpr auto GetMatcher(const char* const bitstring, Id op,
  1826. Type type, const char* const name) {
  1827. const auto [mask, expected] = GetMaskAndExpect(bitstring);
  1828. return Matcher(name, mask, expected, op, type);
  1829. }
  1830. };
  1831. [[nodiscard]] static std::vector<Matcher> GetDecodeTable() {
  1832. std::vector<Matcher> table = {
  1833. #define INST(bitstring, op, type, name) Detail::GetMatcher(bitstring, op, type, name)
  1834. INST("111000110011----", Id::KIL, Type::Flow, "KIL"),
  1835. INST("111000101001----", Id::SSY, Type::Flow, "SSY"),
  1836. INST("111000101010----", Id::PBK, Type::Flow, "PBK"),
  1837. INST("111000100100----", Id::BRA, Type::Flow, "BRA"),
  1838. INST("111000100101----", Id::BRX, Type::Flow, "BRX"),
  1839. INST("1111000011111---", Id::SYNC, Type::Flow, "SYNC"),
  1840. INST("111000110100----", Id::BRK, Type::Flow, "BRK"),
  1841. INST("111000110000----", Id::EXIT, Type::Flow, "EXIT"),
  1842. INST("1111000011110---", Id::DEPBAR, Type::Synch, "DEPBAR"),
  1843. INST("0101000011011---", Id::VOTE, Type::Warp, "VOTE"),
  1844. INST("0101000011100---", Id::VOTE_VTG, Type::Warp, "VOTE_VTG"),
  1845. INST("1110111100010---", Id::SHFL, Type::Warp, "SHFL"),
  1846. INST("0101000011111---", Id::FSWZADD, Type::Warp, "FSWZADD"),
  1847. INST("1110111111011---", Id::LD_A, Type::Memory, "LD_A"),
  1848. INST("1110111101001---", Id::LD_S, Type::Memory, "LD_S"),
  1849. INST("1110111101000---", Id::LD_L, Type::Memory, "LD_L"),
  1850. INST("1110111110010---", Id::LD_C, Type::Memory, "LD_C"),
  1851. INST("100-------------", Id::LD, Type::Memory, "LD"),
  1852. INST("1110111011010---", Id::LDG, Type::Memory, "LDG"),
  1853. INST("1110111111110---", Id::ST_A, Type::Memory, "ST_A"),
  1854. INST("1110111101011---", Id::ST_S, Type::Memory, "ST_S"),
  1855. INST("1110111101010---", Id::ST_L, Type::Memory, "ST_L"),
  1856. INST("101-------------", Id::ST, Type::Memory, "ST"),
  1857. INST("1110111011011---", Id::STG, Type::Memory, "STG"),
  1858. INST("1110101111111---", Id::RED, Type::Memory, "RED"),
  1859. INST("11101101--------", Id::ATOM, Type::Memory, "ATOM"),
  1860. INST("11101100--------", Id::ATOMS, Type::Memory, "ATOMS"),
  1861. INST("1110111110100---", Id::AL2P, Type::Memory, "AL2P"),
  1862. INST("110000----111---", Id::TEX, Type::Texture, "TEX"),
  1863. INST("1101111010111---", Id::TEX_B, Type::Texture, "TEX_B"),
  1864. INST("1101111101001---", Id::TXQ, Type::Texture, "TXQ"),
  1865. INST("1101111101010---", Id::TXQ_B, Type::Texture, "TXQ_B"),
  1866. INST("1101-00---------", Id::TEXS, Type::Texture, "TEXS"),
  1867. INST("11011100--11----", Id::TLD, Type::Texture, "TLD"),
  1868. INST("1101-01---------", Id::TLDS, Type::Texture, "TLDS"),
  1869. INST("110010----111---", Id::TLD4, Type::Texture, "TLD4"),
  1870. INST("1101111011111---", Id::TLD4_B, Type::Texture, "TLD4_B"),
  1871. INST("11011111-0------", Id::TLD4S, Type::Texture, "TLD4S"),
  1872. INST("110111110110----", Id::TMML_B, Type::Texture, "TMML_B"),
  1873. INST("1101111101011---", Id::TMML, Type::Texture, "TMML"),
  1874. INST("11011110011110--", Id::TXD_B, Type::Texture, "TXD_B"),
  1875. INST("11011110001110--", Id::TXD, Type::Texture, "TXD"),
  1876. INST("11101011001-----", Id::SUST, Type::Image, "SUST"),
  1877. INST("11101011000-----", Id::SULD, Type::Image, "SULD"),
  1878. INST("1110101000------", Id::SUATOM, Type::Image, "SUATOM_D"),
  1879. INST("0101000010110---", Id::NOP, Type::Trivial, "NOP"),
  1880. INST("11100000--------", Id::IPA, Type::Trivial, "IPA"),
  1881. INST("1111101111100---", Id::OUT_R, Type::Trivial, "OUT_R"),
  1882. INST("1110111111010---", Id::ISBERD, Type::Trivial, "ISBERD"),
  1883. INST("1111000010101---", Id::BAR, Type::Trivial, "BAR"),
  1884. INST("1110111110011---", Id::MEMBAR, Type::Trivial, "MEMBAR"),
  1885. INST("01011111--------", Id::VMAD, Type::Video, "VMAD"),
  1886. INST("0101000011110---", Id::VSETP, Type::Video, "VSETP"),
  1887. INST("0011101---------", Id::VMNMX, Type::Video, "VMNMX"),
  1888. INST("0011001-1-------", Id::FFMA_IMM, Type::Ffma, "FFMA_IMM"),
  1889. INST("010010011-------", Id::FFMA_CR, Type::Ffma, "FFMA_CR"),
  1890. INST("010100011-------", Id::FFMA_RC, Type::Ffma, "FFMA_RC"),
  1891. INST("010110011-------", Id::FFMA_RR, Type::Ffma, "FFMA_RR"),
  1892. INST("0100110001011---", Id::FADD_C, Type::Arithmetic, "FADD_C"),
  1893. INST("0101110001011---", Id::FADD_R, Type::Arithmetic, "FADD_R"),
  1894. INST("0011100-01011---", Id::FADD_IMM, Type::Arithmetic, "FADD_IMM"),
  1895. INST("000010----------", Id::FADD32I, Type::ArithmeticImmediate, "FADD32I"),
  1896. INST("0100110001101---", Id::FMUL_C, Type::Arithmetic, "FMUL_C"),
  1897. INST("0101110001101---", Id::FMUL_R, Type::Arithmetic, "FMUL_R"),
  1898. INST("0011100-01101---", Id::FMUL_IMM, Type::Arithmetic, "FMUL_IMM"),
  1899. INST("00011110--------", Id::FMUL32_IMM, Type::ArithmeticImmediate, "FMUL32_IMM"),
  1900. INST("0100110000010---", Id::IADD_C, Type::ArithmeticInteger, "IADD_C"),
  1901. INST("0101110000010---", Id::IADD_R, Type::ArithmeticInteger, "IADD_R"),
  1902. INST("0011100-00010---", Id::IADD_IMM, Type::ArithmeticInteger, "IADD_IMM"),
  1903. INST("010011001100----", Id::IADD3_C, Type::ArithmeticInteger, "IADD3_C"),
  1904. INST("010111001100----", Id::IADD3_R, Type::ArithmeticInteger, "IADD3_R"),
  1905. INST("0011100-1100----", Id::IADD3_IMM, Type::ArithmeticInteger, "IADD3_IMM"),
  1906. INST("0001110---------", Id::IADD32I, Type::ArithmeticIntegerImmediate, "IADD32I"),
  1907. INST("0100110000011---", Id::ISCADD_C, Type::ArithmeticInteger, "ISCADD_C"),
  1908. INST("0101110000011---", Id::ISCADD_R, Type::ArithmeticInteger, "ISCADD_R"),
  1909. INST("0011100-00011---", Id::ISCADD_IMM, Type::ArithmeticInteger, "ISCADD_IMM"),
  1910. INST("0100110000001---", Id::POPC_C, Type::ArithmeticInteger, "POPC_C"),
  1911. INST("0101110000001---", Id::POPC_R, Type::ArithmeticInteger, "POPC_R"),
  1912. INST("0011100-00001---", Id::POPC_IMM, Type::ArithmeticInteger, "POPC_IMM"),
  1913. INST("0100110010100---", Id::SEL_C, Type::ArithmeticInteger, "SEL_C"),
  1914. INST("0101110010100---", Id::SEL_R, Type::ArithmeticInteger, "SEL_R"),
  1915. INST("0011100-10100---", Id::SEL_IMM, Type::ArithmeticInteger, "SEL_IMM"),
  1916. INST("010100110100----", Id::ICMP_RC, Type::ArithmeticInteger, "ICMP_RC"),
  1917. INST("010110110100----", Id::ICMP_R, Type::ArithmeticInteger, "ICMP_R"),
  1918. INST("010010110100----", Id::ICMP_CR, Type::ArithmeticInteger, "ICMP_CR"),
  1919. INST("0011011-0100----", Id::ICMP_IMM, Type::ArithmeticInteger, "ICMP_IMM"),
  1920. INST("0101110000110---", Id::FLO_R, Type::ArithmeticInteger, "FLO_R"),
  1921. INST("0100110000110---", Id::FLO_C, Type::ArithmeticInteger, "FLO_C"),
  1922. INST("0011100-00110---", Id::FLO_IMM, Type::ArithmeticInteger, "FLO_IMM"),
  1923. INST("0101101111011---", Id::LEA_R2, Type::ArithmeticInteger, "LEA_R2"),
  1924. INST("0101101111010---", Id::LEA_R1, Type::ArithmeticInteger, "LEA_R1"),
  1925. INST("001101101101----", Id::LEA_IMM, Type::ArithmeticInteger, "LEA_IMM"),
  1926. INST("010010111101----", Id::LEA_RZ, Type::ArithmeticInteger, "LEA_RZ"),
  1927. INST("00011000--------", Id::LEA_HI, Type::ArithmeticInteger, "LEA_HI"),
  1928. INST("0111101-1-------", Id::HADD2_C, Type::ArithmeticHalf, "HADD2_C"),
  1929. INST("0101110100010---", Id::HADD2_R, Type::ArithmeticHalf, "HADD2_R"),
  1930. INST("0111101-0-------", Id::HADD2_IMM, Type::ArithmeticHalfImmediate, "HADD2_IMM"),
  1931. INST("0111100-1-------", Id::HMUL2_C, Type::ArithmeticHalf, "HMUL2_C"),
  1932. INST("0101110100001---", Id::HMUL2_R, Type::ArithmeticHalf, "HMUL2_R"),
  1933. INST("0111100-0-------", Id::HMUL2_IMM, Type::ArithmeticHalfImmediate, "HMUL2_IMM"),
  1934. INST("01110---1-------", Id::HFMA2_CR, Type::Hfma2, "HFMA2_CR"),
  1935. INST("01100---1-------", Id::HFMA2_RC, Type::Hfma2, "HFMA2_RC"),
  1936. INST("0101110100000---", Id::HFMA2_RR, Type::Hfma2, "HFMA2_RR"),
  1937. INST("01110---0-------", Id::HFMA2_IMM_R, Type::Hfma2, "HFMA2_R_IMM"),
  1938. INST("0111111-1-------", Id::HSETP2_C, Type::HalfSetPredicate, "HSETP2_C"),
  1939. INST("0101110100100---", Id::HSETP2_R, Type::HalfSetPredicate, "HSETP2_R"),
  1940. INST("0111111-0-------", Id::HSETP2_IMM, Type::HalfSetPredicate, "HSETP2_IMM"),
  1941. INST("0111110-1-------", Id::HSET2_C, Type::HalfSet, "HSET2_C"),
  1942. INST("0101110100011---", Id::HSET2_R, Type::HalfSet, "HSET2_R"),
  1943. INST("0111110-0-------", Id::HSET2_IMM, Type::HalfSet, "HSET2_IMM"),
  1944. INST("010110111010----", Id::FCMP_RR, Type::Arithmetic, "FCMP_RR"),
  1945. INST("010010111010----", Id::FCMP_RC, Type::Arithmetic, "FCMP_RC"),
  1946. INST("0011011-1010----", Id::FCMP_IMMR, Type::Arithmetic, "FCMP_IMMR"),
  1947. INST("0101000010000---", Id::MUFU, Type::Arithmetic, "MUFU"),
  1948. INST("0100110010010---", Id::RRO_C, Type::Arithmetic, "RRO_C"),
  1949. INST("0101110010010---", Id::RRO_R, Type::Arithmetic, "RRO_R"),
  1950. INST("0011100-10010---", Id::RRO_IMM, Type::Arithmetic, "RRO_IMM"),
  1951. INST("0100110010101---", Id::F2F_C, Type::Conversion, "F2F_C"),
  1952. INST("0101110010101---", Id::F2F_R, Type::Conversion, "F2F_R"),
  1953. INST("0011100-10101---", Id::F2F_IMM, Type::Conversion, "F2F_IMM"),
  1954. INST("0100110010110---", Id::F2I_C, Type::Conversion, "F2I_C"),
  1955. INST("0101110010110---", Id::F2I_R, Type::Conversion, "F2I_R"),
  1956. INST("0011100-10110---", Id::F2I_IMM, Type::Conversion, "F2I_IMM"),
  1957. INST("0100110010011---", Id::MOV_C, Type::Arithmetic, "MOV_C"),
  1958. INST("0101110010011---", Id::MOV_R, Type::Arithmetic, "MOV_R"),
  1959. INST("0011100-10011---", Id::MOV_IMM, Type::Arithmetic, "MOV_IMM"),
  1960. INST("1111000011001---", Id::S2R, Type::Trivial, "S2R"),
  1961. INST("000000010000----", Id::MOV32_IMM, Type::ArithmeticImmediate, "MOV32_IMM"),
  1962. INST("0100110001100---", Id::FMNMX_C, Type::Arithmetic, "FMNMX_C"),
  1963. INST("0101110001100---", Id::FMNMX_R, Type::Arithmetic, "FMNMX_R"),
  1964. INST("0011100-01100---", Id::FMNMX_IMM, Type::Arithmetic, "FMNMX_IMM"),
  1965. INST("0100110000100---", Id::IMNMX_C, Type::ArithmeticInteger, "IMNMX_C"),
  1966. INST("0101110000100---", Id::IMNMX_R, Type::ArithmeticInteger, "IMNMX_R"),
  1967. INST("0011100-00100---", Id::IMNMX_IMM, Type::ArithmeticInteger, "IMNMX_IMM"),
  1968. INST("0100110000000---", Id::BFE_C, Type::Bfe, "BFE_C"),
  1969. INST("0101110000000---", Id::BFE_R, Type::Bfe, "BFE_R"),
  1970. INST("0011100-00000---", Id::BFE_IMM, Type::Bfe, "BFE_IMM"),
  1971. INST("0101001111110---", Id::BFI_RC, Type::Bfi, "BFI_RC"),
  1972. INST("0011011-11110---", Id::BFI_IMM_R, Type::Bfi, "BFI_IMM_R"),
  1973. INST("0100110001000---", Id::LOP_C, Type::ArithmeticInteger, "LOP_C"),
  1974. INST("0101110001000---", Id::LOP_R, Type::ArithmeticInteger, "LOP_R"),
  1975. INST("0011100-01000---", Id::LOP_IMM, Type::ArithmeticInteger, "LOP_IMM"),
  1976. INST("000001----------", Id::LOP32I, Type::ArithmeticIntegerImmediate, "LOP32I"),
  1977. INST("0000001---------", Id::LOP3_C, Type::ArithmeticInteger, "LOP3_C"),
  1978. INST("0101101111100---", Id::LOP3_R, Type::ArithmeticInteger, "LOP3_R"),
  1979. INST("0011110---------", Id::LOP3_IMM, Type::ArithmeticInteger, "LOP3_IMM"),
  1980. INST("0100110001001---", Id::SHL_C, Type::Shift, "SHL_C"),
  1981. INST("0101110001001---", Id::SHL_R, Type::Shift, "SHL_R"),
  1982. INST("0011100-01001---", Id::SHL_IMM, Type::Shift, "SHL_IMM"),
  1983. INST("0100110000101---", Id::SHR_C, Type::Shift, "SHR_C"),
  1984. INST("0101110000101---", Id::SHR_R, Type::Shift, "SHR_R"),
  1985. INST("0011100-00101---", Id::SHR_IMM, Type::Shift, "SHR_IMM"),
  1986. INST("0101110011111---", Id::SHF_RIGHT_R, Type::Shift, "SHF_RIGHT_R"),
  1987. INST("0011100-11111---", Id::SHF_RIGHT_IMM, Type::Shift, "SHF_RIGHT_IMM"),
  1988. INST("0101101111111---", Id::SHF_LEFT_R, Type::Shift, "SHF_LEFT_R"),
  1989. INST("0011011-11111---", Id::SHF_LEFT_IMM, Type::Shift, "SHF_LEFT_IMM"),
  1990. INST("0100110011100---", Id::I2I_C, Type::Conversion, "I2I_C"),
  1991. INST("0101110011100---", Id::I2I_R, Type::Conversion, "I2I_R"),
  1992. INST("0011100-11100---", Id::I2I_IMM, Type::Conversion, "I2I_IMM"),
  1993. INST("0100110010111---", Id::I2F_C, Type::Conversion, "I2F_C"),
  1994. INST("0101110010111---", Id::I2F_R, Type::Conversion, "I2F_R"),
  1995. INST("0011100-10111---", Id::I2F_IMM, Type::Conversion, "I2F_IMM"),
  1996. INST("01011000--------", Id::FSET_R, Type::FloatSet, "FSET_R"),
  1997. INST("0100100---------", Id::FSET_C, Type::FloatSet, "FSET_C"),
  1998. INST("0011000---------", Id::FSET_IMM, Type::FloatSet, "FSET_IMM"),
  1999. INST("010010111011----", Id::FSETP_C, Type::FloatSetPredicate, "FSETP_C"),
  2000. INST("010110111011----", Id::FSETP_R, Type::FloatSetPredicate, "FSETP_R"),
  2001. INST("0011011-1011----", Id::FSETP_IMM, Type::FloatSetPredicate, "FSETP_IMM"),
  2002. INST("010010110110----", Id::ISETP_C, Type::IntegerSetPredicate, "ISETP_C"),
  2003. INST("010110110110----", Id::ISETP_R, Type::IntegerSetPredicate, "ISETP_R"),
  2004. INST("0011011-0110----", Id::ISETP_IMM, Type::IntegerSetPredicate, "ISETP_IMM"),
  2005. INST("010110110101----", Id::ISET_R, Type::IntegerSet, "ISET_R"),
  2006. INST("010010110101----", Id::ISET_C, Type::IntegerSet, "ISET_C"),
  2007. INST("0011011-0101----", Id::ISET_IMM, Type::IntegerSet, "ISET_IMM"),
  2008. INST("0101000010001---", Id::PSET, Type::PredicateSetRegister, "PSET"),
  2009. INST("0101000010010---", Id::PSETP, Type::PredicateSetPredicate, "PSETP"),
  2010. INST("010100001010----", Id::CSETP, Type::PredicateSetPredicate, "CSETP"),
  2011. INST("0011100-11110---", Id::R2P_IMM, Type::RegisterSetPredicate, "R2P_IMM"),
  2012. INST("0011100-11101---", Id::P2R_IMM, Type::RegisterSetPredicate, "P2R_IMM"),
  2013. INST("0011011-00------", Id::XMAD_IMM, Type::Xmad, "XMAD_IMM"),
  2014. INST("0100111---------", Id::XMAD_CR, Type::Xmad, "XMAD_CR"),
  2015. INST("010100010-------", Id::XMAD_RC, Type::Xmad, "XMAD_RC"),
  2016. INST("0101101100------", Id::XMAD_RR, Type::Xmad, "XMAD_RR"),
  2017. };
  2018. #undef INST
  2019. std::stable_sort(table.begin(), table.end(), [](const auto& a, const auto& b) {
  2020. // If a matcher has more bits in its mask it is more specific, so it
  2021. // should come first.
  2022. return std::bitset<16>(a.GetMask()).count() > std::bitset<16>(b.GetMask()).count();
  2023. });
  2024. return table;
  2025. }
  2026. };
  2027. } // namespace Tegra::Shader