shader_bytecode.h 51 KB

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