shader_bytecode.h 52 KB

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