shader_bytecode.h 66 KB

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