InstEmitConversion.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.Instructions.InstEmitAluHelper;
  6. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  7. namespace Ryujinx.Graphics.Shader.Instructions
  8. {
  9. static partial class InstEmit
  10. {
  11. public static void F2F(EmitterContext context)
  12. {
  13. OpCodeFArith op = (OpCodeFArith)context.CurrOp;
  14. FPType dstType = (FPType)op.RawOpCode.Extract(8, 2);
  15. FPType srcType = (FPType)op.RawOpCode.Extract(10, 2);
  16. bool round = op.RawOpCode.Extract(42);
  17. bool negateB = op.RawOpCode.Extract(45);
  18. bool absoluteB = op.RawOpCode.Extract(49);
  19. Operand srcB = context.FPAbsNeg(GetSrcB(context, srcType), absoluteB, negateB, srcType.ToInstFPType());
  20. if (round && srcType == dstType)
  21. {
  22. switch (op.RoundingMode)
  23. {
  24. case RoundingMode.ToNearest:
  25. srcB = context.FPRound(srcB, srcType.ToInstFPType());
  26. break;
  27. case RoundingMode.TowardsNegativeInfinity:
  28. srcB = context.FPFloor(srcB, srcType.ToInstFPType());
  29. break;
  30. case RoundingMode.TowardsPositiveInfinity:
  31. srcB = context.FPCeiling(srcB, srcType.ToInstFPType());
  32. break;
  33. case RoundingMode.TowardsZero:
  34. srcB = context.FPTruncate(srcB, srcType.ToInstFPType());
  35. break;
  36. }
  37. }
  38. // We don't need to handle conversions between FP16 <-> FP32
  39. // since we do FP16 operations as FP32 directly.
  40. // FP16 <-> FP64 conversions are invalid.
  41. if (srcType == FPType.FP32 && dstType == FPType.FP64)
  42. {
  43. srcB = context.FP32ConvertToFP64(srcB);
  44. }
  45. else if (srcType == FPType.FP64 && dstType == FPType.FP32)
  46. {
  47. srcB = context.FP64ConvertToFP32(srcB);
  48. }
  49. srcB = context.FPSaturate(srcB, op.Saturate, dstType.ToInstFPType());
  50. WriteFP(context, dstType, srcB);
  51. // TODO: CC.
  52. }
  53. public static void F2I(EmitterContext context)
  54. {
  55. OpCodeFArith op = (OpCodeFArith)context.CurrOp;
  56. IntegerType intType = (IntegerType)op.RawOpCode.Extract(8, 2);
  57. if (intType == IntegerType.U64)
  58. {
  59. context.Config.GpuAccessor.Log("Unimplemented 64-bits F2I.");
  60. return;
  61. }
  62. bool isSmallInt = intType <= IntegerType.U16;
  63. FPType floatType = (FPType)op.RawOpCode.Extract(10, 2);
  64. bool isSignedInt = op.RawOpCode.Extract(12);
  65. bool negateB = op.RawOpCode.Extract(45);
  66. bool absoluteB = op.RawOpCode.Extract(49);
  67. if (isSignedInt)
  68. {
  69. intType |= IntegerType.S8;
  70. }
  71. Operand srcB = context.FPAbsNeg(GetSrcB(context, floatType), absoluteB, negateB);
  72. switch (op.RoundingMode)
  73. {
  74. case RoundingMode.ToNearest:
  75. srcB = context.FPRound(srcB);
  76. break;
  77. case RoundingMode.TowardsNegativeInfinity:
  78. srcB = context.FPFloor(srcB);
  79. break;
  80. case RoundingMode.TowardsPositiveInfinity:
  81. srcB = context.FPCeiling(srcB);
  82. break;
  83. case RoundingMode.TowardsZero:
  84. srcB = context.FPTruncate(srcB);
  85. break;
  86. }
  87. if (!isSignedInt)
  88. {
  89. // Negative float to uint cast is undefined, so we clamp
  90. // the value before conversion.
  91. srcB = context.FPMaximum(srcB, ConstF(0));
  92. }
  93. srcB = isSignedInt
  94. ? context.FPConvertToS32(srcB)
  95. : context.FPConvertToU32(srcB);
  96. if (isSmallInt)
  97. {
  98. int min = (int)GetIntMin(intType);
  99. int max = (int)GetIntMax(intType);
  100. srcB = isSignedInt
  101. ? context.IClampS32(srcB, Const(min), Const(max))
  102. : context.IClampU32(srcB, Const(min), Const(max));
  103. }
  104. Operand dest = GetDest(context);
  105. context.Copy(dest, srcB);
  106. // TODO: CC.
  107. }
  108. public static void I2F(EmitterContext context)
  109. {
  110. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  111. FPType dstType = (FPType)op.RawOpCode.Extract(8, 2);
  112. IntegerType srcType = (IntegerType)op.RawOpCode.Extract(10, 2);
  113. // TODO: Handle S/U64.
  114. bool isSmallInt = srcType <= IntegerType.U16;
  115. bool isSignedInt = op.RawOpCode.Extract(13);
  116. bool negateB = op.RawOpCode.Extract(45);
  117. bool absoluteB = op.RawOpCode.Extract(49);
  118. Operand srcB = context.IAbsNeg(GetSrcB(context), absoluteB, negateB);
  119. if (isSmallInt)
  120. {
  121. int size = srcType == IntegerType.U16 ? 16 : 8;
  122. srcB = isSignedInt
  123. ? context.BitfieldExtractS32(srcB, Const(op.ByteSelection * 8), Const(size))
  124. : context.BitfieldExtractU32(srcB, Const(op.ByteSelection * 8), Const(size));
  125. }
  126. srcB = isSignedInt
  127. ? context.IConvertS32ToFP(srcB)
  128. : context.IConvertU32ToFP(srcB);
  129. WriteFP(context, dstType, srcB);
  130. // TODO: CC.
  131. }
  132. public static void I2I(EmitterContext context)
  133. {
  134. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  135. IntegerType dstType = (IntegerType)op.RawOpCode.Extract(8, 2);
  136. IntegerType srcType = (IntegerType)op.RawOpCode.Extract(10, 2);
  137. if (srcType == IntegerType.U64 || dstType == IntegerType.U64)
  138. {
  139. context.Config.GpuAccessor.Log("Invalid I2I encoding.");
  140. return;
  141. }
  142. bool srcIsSmallInt = srcType <= IntegerType.U16;
  143. bool dstIsSignedInt = op.RawOpCode.Extract(12);
  144. bool srcIsSignedInt = op.RawOpCode.Extract(13);
  145. bool negateB = op.RawOpCode.Extract(45);
  146. bool absoluteB = op.RawOpCode.Extract(49);
  147. Operand srcB = GetSrcB(context);
  148. if (srcIsSmallInt)
  149. {
  150. int size = srcType == IntegerType.U16 ? 16 : 8;
  151. srcB = srcIsSignedInt
  152. ? context.BitfieldExtractS32(srcB, Const(op.ByteSelection * 8), Const(size))
  153. : context.BitfieldExtractU32(srcB, Const(op.ByteSelection * 8), Const(size));
  154. }
  155. srcB = context.IAbsNeg(srcB, absoluteB, negateB);
  156. if (op.Saturate)
  157. {
  158. if (dstIsSignedInt)
  159. {
  160. dstType |= IntegerType.S8;
  161. }
  162. int min = (int)GetIntMin(dstType);
  163. int max = (int)GetIntMax(dstType);
  164. srcB = dstIsSignedInt
  165. ? context.IClampS32(srcB, Const(min), Const(max))
  166. : context.IClampU32(srcB, Const(min), Const(max));
  167. }
  168. context.Copy(GetDest(context), srcB);
  169. // TODO: CC.
  170. }
  171. private static void WriteFP(EmitterContext context, FPType type, Operand srcB)
  172. {
  173. Operand dest = GetDest(context);
  174. if (type == FPType.FP32)
  175. {
  176. context.Copy(dest, srcB);
  177. }
  178. else if (type == FPType.FP16)
  179. {
  180. context.Copy(dest, context.PackHalf2x16(srcB, ConstF(0)));
  181. }
  182. else /* if (type == FPType.FP64) */
  183. {
  184. Operand dest2 = GetDest2(context);
  185. context.Copy(dest, context.UnpackDouble2x32Low(srcB));
  186. context.Copy(dest2, context.UnpackDouble2x32High(srcB));
  187. }
  188. }
  189. }
  190. }