InstEmitHelper.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  6. namespace Ryujinx.Graphics.Shader.Instructions
  7. {
  8. static class InstEmitHelper
  9. {
  10. public static Operand GetZF()
  11. {
  12. return Register(0, RegisterType.Flag);
  13. }
  14. public static Operand GetNF()
  15. {
  16. return Register(1, RegisterType.Flag);
  17. }
  18. public static Operand GetCF()
  19. {
  20. return Register(2, RegisterType.Flag);
  21. }
  22. public static Operand GetVF()
  23. {
  24. return Register(3, RegisterType.Flag);
  25. }
  26. public static Operand GetDest(EmitterContext context)
  27. {
  28. return Register(((IOpCodeRd)context.CurrOp).Rd);
  29. }
  30. public static Operand GetDest2(EmitterContext context)
  31. {
  32. Register rd = ((IOpCodeRd)context.CurrOp).Rd;
  33. return Register(rd.Index | 1, rd.Type);
  34. }
  35. public static Operand GetSrcA(EmitterContext context, bool isFP64 = false)
  36. {
  37. IOpCodeRa op = (IOpCodeRa)context.CurrOp;
  38. if (isFP64)
  39. {
  40. return context.PackDouble2x32(Register(op.Ra.Index, op.Ra.Type), Register(op.Ra.Index | 1, op.Ra.Type));
  41. }
  42. else
  43. {
  44. return Register(op.Ra);
  45. }
  46. }
  47. public static Operand GetSrcB(EmitterContext context, FPType floatType)
  48. {
  49. if (floatType == FPType.FP32)
  50. {
  51. return GetSrcB(context);
  52. }
  53. else if (floatType == FPType.FP16)
  54. {
  55. int h = context.CurrOp.RawOpCode.Extract(41, 1);
  56. return GetHalfUnpacked(context, GetSrcB(context), FPHalfSwizzle.FP16)[h];
  57. }
  58. else if (floatType == FPType.FP64)
  59. {
  60. return GetSrcB(context, true);
  61. }
  62. throw new ArgumentException($"Invalid floating point type \"{floatType}\".");
  63. }
  64. public static Operand GetSrcB(EmitterContext context, bool isFP64 = false)
  65. {
  66. if (isFP64)
  67. {
  68. switch (context.CurrOp)
  69. {
  70. case IOpCodeCbuf op:
  71. return context.PackDouble2x32(Cbuf(op.Slot, op.Offset), Cbuf(op.Slot, op.Offset + 1));
  72. case IOpCodeImmF op:
  73. return context.FP32ConvertToFP64(ConstF(op.Immediate));
  74. case IOpCodeReg op:
  75. return context.PackDouble2x32(Register(op.Rb.Index, op.Rb.Type), Register(op.Rb.Index | 1, op.Rb.Type));
  76. case IOpCodeRegCbuf op:
  77. return context.PackDouble2x32(Register(op.Rc.Index, op.Rc.Type), Register(op.Rc.Index | 1, op.Rc.Type));
  78. }
  79. }
  80. else
  81. {
  82. switch (context.CurrOp)
  83. {
  84. case IOpCodeCbuf op:
  85. return Cbuf(op.Slot, op.Offset);
  86. case IOpCodeImm op:
  87. return Const(op.Immediate);
  88. case IOpCodeImmF op:
  89. return ConstF(op.Immediate);
  90. case IOpCodeReg op:
  91. return Register(op.Rb);
  92. case IOpCodeRegCbuf op:
  93. return Register(op.Rc);
  94. }
  95. }
  96. throw new InvalidOperationException($"Unexpected opcode type \"{context.CurrOp.GetType().Name}\".");
  97. }
  98. public static Operand GetSrcC(EmitterContext context, bool isFP64 = false)
  99. {
  100. if (isFP64)
  101. {
  102. switch (context.CurrOp)
  103. {
  104. case IOpCodeRegCbuf op:
  105. return context.PackDouble2x32(Cbuf(op.Slot, op.Offset), Cbuf(op.Slot, op.Offset + 1));
  106. case IOpCodeRc op:
  107. return context.PackDouble2x32(Register(op.Rc.Index, op.Rc.Type), Register(op.Rc.Index | 1, op.Rc.Type));
  108. }
  109. }
  110. else
  111. {
  112. switch (context.CurrOp)
  113. {
  114. case IOpCodeRegCbuf op:
  115. return Cbuf(op.Slot, op.Offset);
  116. case IOpCodeRc op:
  117. return Register(op.Rc);
  118. }
  119. }
  120. throw new InvalidOperationException($"Unexpected opcode type \"{context.CurrOp.GetType().Name}\".");
  121. }
  122. public static Operand[] GetHalfSrcA(EmitterContext context, bool isAdd = false)
  123. {
  124. OpCode op = context.CurrOp;
  125. bool absoluteA = false, negateA = false;
  126. if (op is OpCodeAluImm32 && isAdd)
  127. {
  128. negateA = op.RawOpCode.Extract(56);
  129. }
  130. else if (isAdd || op is IOpCodeCbuf || op is IOpCodeImm)
  131. {
  132. negateA = op.RawOpCode.Extract(43);
  133. absoluteA = op.RawOpCode.Extract(44);
  134. }
  135. else if (op is IOpCodeReg)
  136. {
  137. absoluteA = op.RawOpCode.Extract(44);
  138. }
  139. FPHalfSwizzle swizzle = (FPHalfSwizzle)op.RawOpCode.Extract(47, 2);
  140. Operand[] operands = GetHalfUnpacked(context, GetSrcA(context), swizzle);
  141. return FPAbsNeg(context, operands, absoluteA, negateA);
  142. }
  143. public static Operand[] GetHalfSrcB(EmitterContext context, bool isMul = false)
  144. {
  145. OpCode op = context.CurrOp;
  146. FPHalfSwizzle swizzle = FPHalfSwizzle.FP16;
  147. bool absoluteB = false, negateB = false;
  148. if (op is IOpCodeReg)
  149. {
  150. swizzle = (FPHalfSwizzle)op.RawOpCode.Extract(28, 2);
  151. absoluteB = op.RawOpCode.Extract(30);
  152. negateB = op.RawOpCode.Extract(31);
  153. }
  154. else if (op is IOpCodeCbuf)
  155. {
  156. swizzle = FPHalfSwizzle.FP32;
  157. absoluteB = op.RawOpCode.Extract(54);
  158. if (!isMul)
  159. {
  160. negateB = op.RawOpCode.Extract(56);
  161. }
  162. }
  163. Operand[] operands = GetHalfUnpacked(context, GetSrcB(context), swizzle);
  164. return FPAbsNeg(context, operands, absoluteB, negateB);
  165. }
  166. public static Operand[] FPAbsNeg(EmitterContext context, Operand[] operands, bool abs, bool neg)
  167. {
  168. for (int index = 0; index < operands.Length; index++)
  169. {
  170. operands[index] = context.FPAbsNeg(operands[index], abs, neg);
  171. }
  172. return operands;
  173. }
  174. public static Operand[] GetHalfUnpacked(EmitterContext context, Operand src, FPHalfSwizzle swizzle)
  175. {
  176. switch (swizzle)
  177. {
  178. case FPHalfSwizzle.FP16:
  179. return new Operand[]
  180. {
  181. context.UnpackHalf2x16Low (src),
  182. context.UnpackHalf2x16High(src)
  183. };
  184. case FPHalfSwizzle.FP32: return new Operand[] { src, src };
  185. case FPHalfSwizzle.DupH0:
  186. return new Operand[]
  187. {
  188. context.UnpackHalf2x16Low(src),
  189. context.UnpackHalf2x16Low(src)
  190. };
  191. case FPHalfSwizzle.DupH1:
  192. return new Operand[]
  193. {
  194. context.UnpackHalf2x16High(src),
  195. context.UnpackHalf2x16High(src)
  196. };
  197. }
  198. throw new ArgumentException($"Invalid swizzle \"{swizzle}\".");
  199. }
  200. public static Operand GetHalfPacked(EmitterContext context, Operand[] results)
  201. {
  202. OpCode op = context.CurrOp;
  203. FPHalfSwizzle swizzle = FPHalfSwizzle.FP16;
  204. if (!(op is OpCodeAluImm32))
  205. {
  206. swizzle = (FPHalfSwizzle)context.CurrOp.RawOpCode.Extract(49, 2);
  207. }
  208. switch (swizzle)
  209. {
  210. case FPHalfSwizzle.FP16: return context.PackHalf2x16(results[0], results[1]);
  211. case FPHalfSwizzle.FP32: return results[0];
  212. case FPHalfSwizzle.DupH0:
  213. {
  214. Operand h1 = GetHalfDest(context, isHigh: true);
  215. return context.PackHalf2x16(results[0], h1);
  216. }
  217. case FPHalfSwizzle.DupH1:
  218. {
  219. Operand h0 = GetHalfDest(context, isHigh: false);
  220. return context.PackHalf2x16(h0, results[1]);
  221. }
  222. }
  223. throw new ArgumentException($"Invalid swizzle \"{swizzle}\".");
  224. }
  225. public static Operand GetHalfDest(EmitterContext context, bool isHigh)
  226. {
  227. if (isHigh)
  228. {
  229. return context.UnpackHalf2x16High(GetDest(context));
  230. }
  231. else
  232. {
  233. return context.UnpackHalf2x16Low(GetDest(context));
  234. }
  235. }
  236. public static Operand GetPredicate39(EmitterContext context)
  237. {
  238. IOpCodePredicate39 op = (IOpCodePredicate39)context.CurrOp;
  239. Operand local = Register(op.Predicate39);
  240. if (op.InvertP)
  241. {
  242. local = context.BitwiseNot(local);
  243. }
  244. return local;
  245. }
  246. public static Operand SignExtendTo32(EmitterContext context, Operand src, int srcBits)
  247. {
  248. return context.BitfieldExtractS32(src, Const(0), Const(srcBits));
  249. }
  250. public static Operand ZeroExtendTo32(EmitterContext context, Operand src, int srcBits)
  251. {
  252. int mask = (int)(0xffffffffu >> (32 - srcBits));
  253. return context.BitwiseAnd(src, Const(mask));
  254. }
  255. }
  256. }