InstEmitConversion.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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);
  20. if (round)
  21. {
  22. switch (op.RoundingMode)
  23. {
  24. case RoundingMode.ToNearest:
  25. srcB = context.FPRound(srcB);
  26. break;
  27. case RoundingMode.TowardsNegativeInfinity:
  28. srcB = context.FPFloor(srcB);
  29. break;
  30. case RoundingMode.TowardsPositiveInfinity:
  31. srcB = context.FPCeiling(srcB);
  32. break;
  33. case RoundingMode.TowardsZero:
  34. srcB = context.FPTruncate(srcB);
  35. break;
  36. }
  37. }
  38. srcB = context.FPSaturate(srcB, op.Saturate);
  39. WriteFP(context, dstType, srcB);
  40. // TODO: CC.
  41. }
  42. public static void F2I(EmitterContext context)
  43. {
  44. OpCodeFArith op = (OpCodeFArith)context.CurrOp;
  45. IntegerType intType = (IntegerType)op.RawOpCode.Extract(8, 2);
  46. if (intType == IntegerType.U64)
  47. {
  48. // TODO: Warning. This instruction supports 64-bits integers, but it is not implemented.
  49. return;
  50. }
  51. bool isSmallInt = intType <= IntegerType.U16;
  52. FPType floatType = (FPType)op.RawOpCode.Extract(10, 2);
  53. bool isSignedInt = op.RawOpCode.Extract(12);
  54. bool negateB = op.RawOpCode.Extract(45);
  55. bool absoluteB = op.RawOpCode.Extract(49);
  56. if (isSignedInt)
  57. {
  58. intType |= IntegerType.S8;
  59. }
  60. Operand srcB = context.FPAbsNeg(GetSrcB(context, floatType), absoluteB, negateB);
  61. switch (op.RoundingMode)
  62. {
  63. case RoundingMode.ToNearest:
  64. srcB = context.FPRound(srcB);
  65. break;
  66. case RoundingMode.TowardsNegativeInfinity:
  67. srcB = context.FPFloor(srcB);
  68. break;
  69. case RoundingMode.TowardsPositiveInfinity:
  70. srcB = context.FPCeiling(srcB);
  71. break;
  72. case RoundingMode.TowardsZero:
  73. srcB = context.FPTruncate(srcB);
  74. break;
  75. }
  76. if (!isSignedInt)
  77. {
  78. // Negative float to uint cast is undefined, so we clamp
  79. // the value before conversion.
  80. srcB = context.FPMaximum(srcB, ConstF(0));
  81. }
  82. srcB = isSignedInt
  83. ? context.FPConvertToS32(srcB)
  84. : context.FPConvertToU32(srcB);
  85. if (isSmallInt)
  86. {
  87. int min = (int)GetIntMin(intType);
  88. int max = (int)GetIntMax(intType);
  89. srcB = isSignedInt
  90. ? context.IClampS32(srcB, Const(min), Const(max))
  91. : context.IClampU32(srcB, Const(min), Const(max));
  92. }
  93. Operand dest = GetDest(context);
  94. context.Copy(dest, srcB);
  95. // TODO: CC.
  96. }
  97. public static void I2F(EmitterContext context)
  98. {
  99. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  100. FPType dstType = (FPType)op.RawOpCode.Extract(8, 2);
  101. IntegerType srcType = (IntegerType)op.RawOpCode.Extract(10, 2);
  102. // TODO: Handle S/U64.
  103. bool isSmallInt = srcType <= IntegerType.U16;
  104. bool isSignedInt = op.RawOpCode.Extract(13);
  105. bool negateB = op.RawOpCode.Extract(45);
  106. bool absoluteB = op.RawOpCode.Extract(49);
  107. Operand srcB = context.IAbsNeg(GetSrcB(context), absoluteB, negateB);
  108. if (isSmallInt)
  109. {
  110. int size = srcType == IntegerType.U16 ? 16 : 8;
  111. srcB = isSignedInt
  112. ? context.BitfieldExtractS32(srcB, Const(op.ByteSelection * 8), Const(size))
  113. : context.BitfieldExtractU32(srcB, Const(op.ByteSelection * 8), Const(size));
  114. }
  115. srcB = isSignedInt
  116. ? context.IConvertS32ToFP(srcB)
  117. : context.IConvertU32ToFP(srcB);
  118. WriteFP(context, dstType, srcB);
  119. // TODO: CC.
  120. }
  121. public static void I2I(EmitterContext context)
  122. {
  123. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  124. IntegerType dstType = (IntegerType)op.RawOpCode.Extract(8, 2);
  125. IntegerType srcType = (IntegerType)op.RawOpCode.Extract(10, 2);
  126. if (srcType == IntegerType.U64 || dstType == IntegerType.U64)
  127. {
  128. // TODO: Warning. This instruction doesn't support 64-bits integers.
  129. return;
  130. }
  131. bool srcIsSmallInt = srcType <= IntegerType.U16;
  132. bool dstIsSignedInt = op.RawOpCode.Extract(12);
  133. bool srcIsSignedInt = op.RawOpCode.Extract(13);
  134. bool negateB = op.RawOpCode.Extract(45);
  135. bool absoluteB = op.RawOpCode.Extract(49);
  136. Operand srcB = GetSrcB(context);
  137. if (srcIsSmallInt)
  138. {
  139. int size = srcType == IntegerType.U16 ? 16 : 8;
  140. srcB = srcIsSignedInt
  141. ? context.BitfieldExtractS32(srcB, Const(op.ByteSelection * 8), Const(size))
  142. : context.BitfieldExtractU32(srcB, Const(op.ByteSelection * 8), Const(size));
  143. }
  144. srcB = context.IAbsNeg(srcB, absoluteB, negateB);
  145. if (op.Saturate)
  146. {
  147. if (dstIsSignedInt)
  148. {
  149. dstType |= IntegerType.S8;
  150. }
  151. int min = (int)GetIntMin(dstType);
  152. int max = (int)GetIntMax(dstType);
  153. srcB = dstIsSignedInt
  154. ? context.IClampS32(srcB, Const(min), Const(max))
  155. : context.IClampU32(srcB, Const(min), Const(max));
  156. }
  157. context.Copy(GetDest(context), srcB);
  158. // TODO: CC.
  159. }
  160. private static void WriteFP(EmitterContext context, FPType type, Operand srcB)
  161. {
  162. Operand dest = GetDest(context);
  163. if (type == FPType.FP32)
  164. {
  165. context.Copy(dest, srcB);
  166. }
  167. else if (type == FPType.FP16)
  168. {
  169. context.Copy(dest, context.PackHalf2x16(srcB, ConstF(0)));
  170. }
  171. else
  172. {
  173. // TODO.
  174. }
  175. }
  176. }
  177. }