shader_bytecode.h 49 KB

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