InstEmitMemory.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
  5. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  6. namespace Ryujinx.Graphics.Shader.Instructions
  7. {
  8. static partial class InstEmit
  9. {
  10. private enum MemoryRegion
  11. {
  12. Local,
  13. Shared
  14. }
  15. public static void Ald(EmitterContext context)
  16. {
  17. OpCodeAttribute op = (OpCodeAttribute)context.CurrOp;
  18. Operand primVertex = context.Copy(GetSrcC(context));
  19. for (int index = 0; index < op.Count; index++)
  20. {
  21. Register rd = new Register(op.Rd.Index + index, RegisterType.Gpr);
  22. if (rd.IsRZ)
  23. {
  24. break;
  25. }
  26. Operand src = Attribute(op.AttributeOffset + index * 4);
  27. context.Copy(Register(rd), context.LoadAttribute(src, primVertex));
  28. }
  29. }
  30. public static void Ast(EmitterContext context)
  31. {
  32. OpCodeAttribute op = (OpCodeAttribute)context.CurrOp;
  33. for (int index = 0; index < op.Count; index++)
  34. {
  35. if (op.Rd.Index + index > RegisterConsts.RegisterZeroIndex)
  36. {
  37. break;
  38. }
  39. Register rd = new Register(op.Rd.Index + index, RegisterType.Gpr);
  40. Operand dest = Attribute(op.AttributeOffset + index * 4);
  41. context.Copy(dest, Register(rd));
  42. }
  43. }
  44. public static void Atoms(EmitterContext context)
  45. {
  46. OpCodeAtom op = (OpCodeAtom)context.CurrOp;
  47. Operand offset = context.ShiftRightU32(GetSrcA(context), Const(2));
  48. offset = context.IAdd(offset, Const(op.Offset));
  49. Operand value = GetSrcB(context);
  50. Operand res = EmitAtomicOp(
  51. context,
  52. Instruction.MrShared,
  53. op.AtomicOp,
  54. op.Type,
  55. offset,
  56. Const(0),
  57. value);
  58. context.Copy(GetDest(context), res);
  59. }
  60. public static void Ipa(EmitterContext context)
  61. {
  62. OpCodeIpa op = (OpCodeIpa)context.CurrOp;
  63. InterpolationQualifier iq = InterpolationQualifier.None;
  64. switch (op.Mode)
  65. {
  66. case InterpolationMode.Pass: iq = InterpolationQualifier.NoPerspective; break;
  67. }
  68. Operand srcA = Attribute(op.AttributeOffset, iq);
  69. Operand srcB = GetSrcB(context);
  70. Operand res = context.FPSaturate(srcA, op.Saturate);
  71. context.Copy(GetDest(context), res);
  72. }
  73. public static void Isberd(EmitterContext context)
  74. {
  75. // This instruction performs a load from ISBE memory,
  76. // however it seems to be only used to get some vertex
  77. // input data, so we instead propagate the offset so that
  78. // it can be used on the attribute load.
  79. context.Copy(GetDest(context), GetSrcA(context));
  80. }
  81. public static void Ld(EmitterContext context)
  82. {
  83. EmitLoad(context, MemoryRegion.Local);
  84. }
  85. public static void Ldc(EmitterContext context)
  86. {
  87. OpCodeLdc op = (OpCodeLdc)context.CurrOp;
  88. if (op.Size > IntegerSize.B64)
  89. {
  90. // TODO: Warning.
  91. }
  92. bool isSmallInt = op.Size < IntegerSize.B32;
  93. int count = op.Size == IntegerSize.B64 ? 2 : 1;
  94. Operand wordOffset = context.ShiftRightU32(GetSrcA(context), Const(2));
  95. wordOffset = context.IAdd(wordOffset, Const(op.Offset));
  96. Operand bitOffset = GetBitOffset(context, GetSrcA(context));
  97. for (int index = 0; index < count; index++)
  98. {
  99. Register rd = new Register(op.Rd.Index + index, RegisterType.Gpr);
  100. if (rd.IsRZ)
  101. {
  102. break;
  103. }
  104. Operand offset = context.IAdd(wordOffset, Const(index));
  105. Operand value = context.LoadConstant(Const(op.Slot), offset);
  106. if (isSmallInt)
  107. {
  108. value = ExtractSmallInt(context, op.Size, wordOffset, value);
  109. }
  110. context.Copy(Register(rd), value);
  111. }
  112. }
  113. public static void Ldg(EmitterContext context)
  114. {
  115. EmitLoadGlobal(context);
  116. }
  117. public static void Lds(EmitterContext context)
  118. {
  119. EmitLoad(context, MemoryRegion.Shared);
  120. }
  121. public static void Out(EmitterContext context)
  122. {
  123. OpCode op = context.CurrOp;
  124. bool emit = op.RawOpCode.Extract(39);
  125. bool cut = op.RawOpCode.Extract(40);
  126. if (!(emit || cut))
  127. {
  128. // TODO: Warning.
  129. }
  130. if (emit)
  131. {
  132. context.EmitVertex();
  133. }
  134. if (cut)
  135. {
  136. context.EndPrimitive();
  137. }
  138. }
  139. public static void Red(EmitterContext context)
  140. {
  141. OpCodeRed op = (OpCodeRed)context.CurrOp;
  142. (Operand addrLow, Operand addrHigh) = Get40BitsAddress(context, op.Ra, op.Extended, op.Offset);
  143. EmitAtomicOp(
  144. context,
  145. Instruction.MrGlobal,
  146. op.AtomicOp,
  147. op.Type,
  148. addrLow,
  149. addrHigh,
  150. GetDest(context));
  151. }
  152. public static void St(EmitterContext context)
  153. {
  154. EmitStore(context, MemoryRegion.Local);
  155. }
  156. public static void Stg(EmitterContext context)
  157. {
  158. EmitStoreGlobal(context);
  159. }
  160. public static void Sts(EmitterContext context)
  161. {
  162. EmitStore(context, MemoryRegion.Shared);
  163. }
  164. private static Operand EmitAtomicOp(
  165. EmitterContext context,
  166. Instruction mr,
  167. AtomicOp op,
  168. ReductionType type,
  169. Operand addrLow,
  170. Operand addrHigh,
  171. Operand value)
  172. {
  173. Operand res = Const(0);
  174. switch (op)
  175. {
  176. case AtomicOp.Add:
  177. if (type == ReductionType.S32 || type == ReductionType.U32)
  178. {
  179. res = context.AtomicAdd(mr, addrLow, addrHigh, value);
  180. }
  181. else
  182. {
  183. // Not supported or invalid.
  184. }
  185. break;
  186. case AtomicOp.BitwiseAnd:
  187. if (type == ReductionType.S32 || type == ReductionType.U32)
  188. {
  189. res = context.AtomicAnd(mr, addrLow, addrHigh, value);
  190. }
  191. else
  192. {
  193. // Not supported or invalid.
  194. }
  195. break;
  196. case AtomicOp.BitwiseExclusiveOr:
  197. if (type == ReductionType.S32 || type == ReductionType.U32)
  198. {
  199. res = context.AtomicXor(mr, addrLow, addrHigh, value);
  200. }
  201. else
  202. {
  203. // Not supported or invalid.
  204. }
  205. break;
  206. case AtomicOp.BitwiseOr:
  207. if (type == ReductionType.S32 || type == ReductionType.U32)
  208. {
  209. res = context.AtomicOr(mr, addrLow, addrHigh, value);
  210. }
  211. else
  212. {
  213. // Not supported or invalid.
  214. }
  215. break;
  216. case AtomicOp.Maximum:
  217. if (type == ReductionType.S32)
  218. {
  219. res = context.AtomicMaxS32(mr, addrLow, addrHigh, value);
  220. }
  221. else if (type == ReductionType.U32)
  222. {
  223. res = context.AtomicMaxU32(mr, addrLow, addrHigh, value);
  224. }
  225. else
  226. {
  227. // Not supported or invalid.
  228. }
  229. break;
  230. case AtomicOp.Minimum:
  231. if (type == ReductionType.S32)
  232. {
  233. res = context.AtomicMinS32(mr, addrLow, addrHigh, value);
  234. }
  235. else if (type == ReductionType.U32)
  236. {
  237. res = context.AtomicMinU32(mr, addrLow, addrHigh, value);
  238. }
  239. else
  240. {
  241. // Not supported or invalid.
  242. }
  243. break;
  244. }
  245. return res;
  246. }
  247. private static void EmitLoad(EmitterContext context, MemoryRegion region)
  248. {
  249. OpCodeMemory op = (OpCodeMemory)context.CurrOp;
  250. if (op.Size > IntegerSize.B128)
  251. {
  252. // TODO: Warning.
  253. }
  254. bool isSmallInt = op.Size < IntegerSize.B32;
  255. int count = 1;
  256. switch (op.Size)
  257. {
  258. case IntegerSize.B64: count = 2; break;
  259. case IntegerSize.B128: count = 4; break;
  260. }
  261. Operand baseOffset = context.IAdd(GetSrcA(context), Const(op.Offset));
  262. // Word offset = byte offset / 4 (one word = 4 bytes).
  263. Operand wordOffset = context.ShiftRightU32(baseOffset, Const(2));
  264. Operand bitOffset = GetBitOffset(context, baseOffset);
  265. for (int index = 0; index < count; index++)
  266. {
  267. Register rd = new Register(op.Rd.Index + index, RegisterType.Gpr);
  268. if (rd.IsRZ)
  269. {
  270. break;
  271. }
  272. Operand offset = context.IAdd(wordOffset, Const(index));
  273. Operand value = null;
  274. switch (region)
  275. {
  276. case MemoryRegion.Local: value = context.LoadLocal (offset); break;
  277. case MemoryRegion.Shared: value = context.LoadShared(offset); break;
  278. }
  279. if (isSmallInt)
  280. {
  281. value = ExtractSmallInt(context, op.Size, bitOffset, value);
  282. }
  283. context.Copy(Register(rd), value);
  284. }
  285. }
  286. private static void EmitLoadGlobal(EmitterContext context)
  287. {
  288. OpCodeMemory op = (OpCodeMemory)context.CurrOp;
  289. bool isSmallInt = op.Size < IntegerSize.B32;
  290. int count = GetVectorCount(op.Size);
  291. (Operand addrLow, Operand addrHigh) = Get40BitsAddress(context, op.Ra, op.Extended, op.Offset);
  292. Operand bitOffset = GetBitOffset(context, addrLow);
  293. for (int index = 0; index < count; index++)
  294. {
  295. Register rd = new Register(op.Rd.Index + index, RegisterType.Gpr);
  296. if (rd.IsRZ)
  297. {
  298. break;
  299. }
  300. Operand value = context.LoadGlobal(context.IAdd(addrLow, Const(index * 4)), addrHigh);
  301. if (isSmallInt)
  302. {
  303. value = ExtractSmallInt(context, op.Size, bitOffset, value);
  304. }
  305. context.Copy(Register(rd), value);
  306. }
  307. }
  308. private static void EmitStore(EmitterContext context, MemoryRegion region)
  309. {
  310. OpCodeMemory op = (OpCodeMemory)context.CurrOp;
  311. if (op.Size > IntegerSize.B128)
  312. {
  313. // TODO: Warning.
  314. }
  315. bool isSmallInt = op.Size < IntegerSize.B32;
  316. int count = 1;
  317. switch (op.Size)
  318. {
  319. case IntegerSize.B64: count = 2; break;
  320. case IntegerSize.B128: count = 4; break;
  321. }
  322. Operand baseOffset = context.IAdd(GetSrcA(context), Const(op.Offset));
  323. Operand wordOffset = context.ShiftRightU32(baseOffset, Const(2));
  324. Operand bitOffset = GetBitOffset(context, baseOffset);
  325. for (int index = 0; index < count; index++)
  326. {
  327. Register rd = new Register(op.Rd.Index + index, RegisterType.Gpr);
  328. Operand value = Register(rd);
  329. Operand offset = context.IAdd(wordOffset, Const(index));
  330. if (isSmallInt)
  331. {
  332. Operand word = null;
  333. switch (region)
  334. {
  335. case MemoryRegion.Local: word = context.LoadLocal (offset); break;
  336. case MemoryRegion.Shared: word = context.LoadShared(offset); break;
  337. }
  338. value = InsertSmallInt(context, op.Size, bitOffset, word, value);
  339. }
  340. switch (region)
  341. {
  342. case MemoryRegion.Local: context.StoreLocal (offset, value); break;
  343. case MemoryRegion.Shared: context.StoreShared(offset, value); break;
  344. }
  345. if (rd.IsRZ)
  346. {
  347. break;
  348. }
  349. }
  350. }
  351. private static void EmitStoreGlobal(EmitterContext context)
  352. {
  353. OpCodeMemory op = (OpCodeMemory)context.CurrOp;
  354. bool isSmallInt = op.Size < IntegerSize.B32;
  355. int count = GetVectorCount(op.Size);
  356. (Operand addrLow, Operand addrHigh) = Get40BitsAddress(context, op.Ra, op.Extended, op.Offset);
  357. Operand bitOffset = GetBitOffset(context, addrLow);
  358. for (int index = 0; index < count; index++)
  359. {
  360. Register rd = new Register(op.Rd.Index + index, RegisterType.Gpr);
  361. Operand value = Register(rd);
  362. if (isSmallInt)
  363. {
  364. Operand word = context.LoadGlobal(addrLow, addrHigh);
  365. value = InsertSmallInt(context, op.Size, bitOffset, word, value);
  366. }
  367. context.StoreGlobal(context.IAdd(addrLow, Const(index * 4)), addrHigh, value);
  368. if (rd.IsRZ)
  369. {
  370. break;
  371. }
  372. }
  373. }
  374. private static int GetVectorCount(IntegerSize size)
  375. {
  376. switch (size)
  377. {
  378. case IntegerSize.B64:
  379. return 2;
  380. case IntegerSize.B128:
  381. case IntegerSize.UB128:
  382. return 4;
  383. }
  384. return 1;
  385. }
  386. private static (Operand, Operand) Get40BitsAddress(
  387. EmitterContext context,
  388. Register ra,
  389. bool extended,
  390. int offset)
  391. {
  392. Operand addrLow = GetSrcA(context);
  393. Operand addrHigh;
  394. if (extended && !ra.IsRZ)
  395. {
  396. addrHigh = Register(ra.Index + 1, RegisterType.Gpr);
  397. }
  398. else
  399. {
  400. addrHigh = Const(0);
  401. }
  402. Operand offs = Const(offset);
  403. addrLow = context.IAdd(addrLow, offs);
  404. if (extended)
  405. {
  406. Operand carry = context.ICompareLessUnsigned(addrLow, offs);
  407. addrHigh = context.IAdd(addrHigh, context.ConditionalSelect(carry, Const(1), Const(0)));
  408. }
  409. return (addrLow, addrHigh);
  410. }
  411. private static Operand GetBitOffset(EmitterContext context, Operand baseOffset)
  412. {
  413. // Note: bit offset = (baseOffset & 0b11) * 8.
  414. // Addresses should be always aligned to the integer type,
  415. // so we don't need to take unaligned addresses into account.
  416. return context.ShiftLeft(context.BitwiseAnd(baseOffset, Const(3)), Const(3));
  417. }
  418. private static Operand ExtractSmallInt(
  419. EmitterContext context,
  420. IntegerSize size,
  421. Operand bitOffset,
  422. Operand value)
  423. {
  424. value = context.ShiftRightU32(value, bitOffset);
  425. switch (size)
  426. {
  427. case IntegerSize.U8: value = ZeroExtendTo32(context, value, 8); break;
  428. case IntegerSize.U16: value = ZeroExtendTo32(context, value, 16); break;
  429. case IntegerSize.S8: value = SignExtendTo32(context, value, 8); break;
  430. case IntegerSize.S16: value = SignExtendTo32(context, value, 16); break;
  431. }
  432. return value;
  433. }
  434. private static Operand InsertSmallInt(
  435. EmitterContext context,
  436. IntegerSize size,
  437. Operand bitOffset,
  438. Operand word,
  439. Operand value)
  440. {
  441. switch (size)
  442. {
  443. case IntegerSize.U8:
  444. case IntegerSize.S8:
  445. value = context.BitwiseAnd(value, Const(0xff));
  446. value = context.BitfieldInsert(word, value, bitOffset, Const(8));
  447. break;
  448. case IntegerSize.U16:
  449. case IntegerSize.S16:
  450. value = context.BitwiseAnd(value, Const(0xffff));
  451. value = context.BitfieldInsert(word, value, bitOffset, Const(16));
  452. break;
  453. }
  454. return value;
  455. }
  456. }
  457. }