InstEmitHelper.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 GetSrcA(EmitterContext context)
  31. {
  32. return Register(((IOpCodeRa)context.CurrOp).Ra);
  33. }
  34. public static Operand GetSrcB(EmitterContext context, FPType floatType)
  35. {
  36. if (floatType == FPType.FP32)
  37. {
  38. return GetSrcB(context);
  39. }
  40. else if (floatType == FPType.FP16)
  41. {
  42. int h = context.CurrOp.RawOpCode.Extract(41, 1);
  43. return GetHalfUnpacked(context, GetSrcB(context), FPHalfSwizzle.FP16)[h];
  44. }
  45. else if (floatType == FPType.FP64)
  46. {
  47. // TODO: Double floating-point type support.
  48. }
  49. context.Config.PrintLog($"Invalid floating point type: {floatType}.");
  50. return ConstF(0);
  51. }
  52. public static Operand GetSrcB(EmitterContext context)
  53. {
  54. switch (context.CurrOp)
  55. {
  56. case IOpCodeCbuf op:
  57. return Cbuf(op.Slot, op.Offset);
  58. case IOpCodeImm op:
  59. return Const(op.Immediate);
  60. case IOpCodeImmF op:
  61. return ConstF(op.Immediate);
  62. case IOpCodeReg op:
  63. return Register(op.Rb);
  64. case IOpCodeRegCbuf op:
  65. return Register(op.Rc);
  66. }
  67. throw new InvalidOperationException($"Unexpected opcode type \"{context.CurrOp.GetType().Name}\".");
  68. }
  69. public static Operand GetSrcC(EmitterContext context)
  70. {
  71. switch (context.CurrOp)
  72. {
  73. case IOpCodeRegCbuf op:
  74. return Cbuf(op.Slot, op.Offset);
  75. case IOpCodeRc op:
  76. return Register(op.Rc);
  77. }
  78. throw new InvalidOperationException($"Unexpected opcode type \"{context.CurrOp.GetType().Name}\".");
  79. }
  80. public static Operand[] GetHalfSrcA(EmitterContext context, bool isAdd = false)
  81. {
  82. OpCode op = context.CurrOp;
  83. bool absoluteA = false, negateA = false;
  84. if (op is OpCodeAluImm32 && isAdd)
  85. {
  86. negateA = op.RawOpCode.Extract(56);
  87. }
  88. else if (isAdd || op is IOpCodeCbuf || op is IOpCodeImm)
  89. {
  90. negateA = op.RawOpCode.Extract(43);
  91. absoluteA = op.RawOpCode.Extract(44);
  92. }
  93. else if (op is IOpCodeReg)
  94. {
  95. absoluteA = op.RawOpCode.Extract(44);
  96. }
  97. FPHalfSwizzle swizzle = (FPHalfSwizzle)op.RawOpCode.Extract(47, 2);
  98. Operand[] operands = GetHalfUnpacked(context, GetSrcA(context), swizzle);
  99. return FPAbsNeg(context, operands, absoluteA, negateA);
  100. }
  101. public static Operand[] GetHalfSrcB(EmitterContext context)
  102. {
  103. OpCode op = context.CurrOp;
  104. FPHalfSwizzle swizzle = FPHalfSwizzle.FP16;
  105. bool absoluteB = false, negateB = false;
  106. if (op is IOpCodeReg)
  107. {
  108. swizzle = (FPHalfSwizzle)op.RawOpCode.Extract(28, 2);
  109. absoluteB = op.RawOpCode.Extract(30);
  110. negateB = op.RawOpCode.Extract(31);
  111. }
  112. else if (op is IOpCodeCbuf)
  113. {
  114. swizzle = FPHalfSwizzle.FP32;
  115. absoluteB = op.RawOpCode.Extract(54);
  116. }
  117. Operand[] operands = GetHalfUnpacked(context, GetSrcB(context), swizzle);
  118. return FPAbsNeg(context, operands, absoluteB, negateB);
  119. }
  120. public static Operand[] FPAbsNeg(EmitterContext context, Operand[] operands, bool abs, bool neg)
  121. {
  122. for (int index = 0; index < operands.Length; index++)
  123. {
  124. operands[index] = context.FPAbsNeg(operands[index], abs, neg);
  125. }
  126. return operands;
  127. }
  128. public static Operand[] GetHalfUnpacked(EmitterContext context, Operand src, FPHalfSwizzle swizzle)
  129. {
  130. switch (swizzle)
  131. {
  132. case FPHalfSwizzle.FP16:
  133. return new Operand[]
  134. {
  135. context.UnpackHalf2x16Low (src),
  136. context.UnpackHalf2x16High(src)
  137. };
  138. case FPHalfSwizzle.FP32: return new Operand[] { src, src };
  139. case FPHalfSwizzle.DupH0:
  140. return new Operand[]
  141. {
  142. context.UnpackHalf2x16Low(src),
  143. context.UnpackHalf2x16Low(src)
  144. };
  145. case FPHalfSwizzle.DupH1:
  146. return new Operand[]
  147. {
  148. context.UnpackHalf2x16High(src),
  149. context.UnpackHalf2x16High(src)
  150. };
  151. }
  152. throw new ArgumentException($"Invalid swizzle \"{swizzle}\".");
  153. }
  154. public static Operand GetHalfPacked(EmitterContext context, Operand[] results)
  155. {
  156. OpCode op = context.CurrOp;
  157. FPHalfSwizzle swizzle = FPHalfSwizzle.FP16;
  158. if (!(op is OpCodeAluImm32))
  159. {
  160. swizzle = (FPHalfSwizzle)context.CurrOp.RawOpCode.Extract(49, 2);
  161. }
  162. switch (swizzle)
  163. {
  164. case FPHalfSwizzle.FP16: return context.PackHalf2x16(results[0], results[1]);
  165. case FPHalfSwizzle.FP32: return results[0];
  166. case FPHalfSwizzle.DupH0:
  167. {
  168. Operand h1 = GetHalfDest(context, isHigh: true);
  169. return context.PackHalf2x16(results[0], h1);
  170. }
  171. case FPHalfSwizzle.DupH1:
  172. {
  173. Operand h0 = GetHalfDest(context, isHigh: false);
  174. return context.PackHalf2x16(h0, results[1]);
  175. }
  176. }
  177. throw new ArgumentException($"Invalid swizzle \"{swizzle}\".");
  178. }
  179. public static Operand GetHalfDest(EmitterContext context, bool isHigh)
  180. {
  181. if (isHigh)
  182. {
  183. return context.UnpackHalf2x16High(GetDest(context));
  184. }
  185. else
  186. {
  187. return context.UnpackHalf2x16Low(GetDest(context));
  188. }
  189. }
  190. public static Operand GetPredicate39(EmitterContext context)
  191. {
  192. IOpCodePredicate39 op = (IOpCodePredicate39)context.CurrOp;
  193. Operand local = Register(op.Predicate39);
  194. if (op.InvertP)
  195. {
  196. local = context.BitwiseNot(local);
  197. }
  198. return local;
  199. }
  200. public static Operand SignExtendTo32(EmitterContext context, Operand src, int srcBits)
  201. {
  202. return context.BitfieldExtractS32(src, Const(0), Const(srcBits));
  203. }
  204. public static Operand ZeroExtendTo32(EmitterContext context, Operand src, int srcBits)
  205. {
  206. int mask = (int)(0xffffffffu >> (32 - srcBits));
  207. return context.BitwiseAnd(src, Const(mask));
  208. }
  209. }
  210. }