shader_bytecode.h 66 KB

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