InstEmitConversion.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. long min = GetIntMin(intType);
  77. long max = GetIntMax(intType);
  78. srcB = context.FPClamp(srcB, ConstF(min), ConstF(max));
  79. srcB = context.FPConvertToS32(srcB);
  80. Operand dest = GetDest(context);
  81. context.Copy(dest, srcB);
  82. // TODO: CC.
  83. }
  84. public static void I2F(EmitterContext context)
  85. {
  86. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  87. FPType dstType = (FPType)op.RawOpCode.Extract(8, 2);
  88. IntegerType srcType = (IntegerType)op.RawOpCode.Extract(10, 2);
  89. // TODO: Handle S/U64.
  90. bool isSmallInt = srcType <= IntegerType.U16;
  91. bool isSignedInt = op.RawOpCode.Extract(13);
  92. bool negateB = op.RawOpCode.Extract(45);
  93. bool absoluteB = op.RawOpCode.Extract(49);
  94. Operand srcB = context.IAbsNeg(GetSrcB(context), absoluteB, negateB);
  95. if (isSmallInt)
  96. {
  97. int size = srcType == IntegerType.U16 ? 16 : 8;
  98. srcB = isSignedInt
  99. ? context.BitfieldExtractS32(srcB, Const(op.ByteSelection * 8), Const(size))
  100. : context.BitfieldExtractU32(srcB, Const(op.ByteSelection * 8), Const(size));
  101. }
  102. srcB = isSignedInt
  103. ? context.IConvertS32ToFP(srcB)
  104. : context.IConvertU32ToFP(srcB);
  105. WriteFP(context, dstType, srcB);
  106. // TODO: CC.
  107. }
  108. public static void I2I(EmitterContext context)
  109. {
  110. OpCodeAlu op = (OpCodeAlu)context.CurrOp;
  111. IntegerType dstType = (IntegerType)op.RawOpCode.Extract(8, 2);
  112. IntegerType srcType = (IntegerType)op.RawOpCode.Extract(10, 2);
  113. if (srcType == IntegerType.U64 || dstType == IntegerType.U64)
  114. {
  115. // TODO: Warning. This instruction doesn't support 64-bits integers.
  116. return;
  117. }
  118. bool srcIsSmallInt = srcType <= IntegerType.U16;
  119. bool dstIsSignedInt = op.RawOpCode.Extract(12);
  120. bool srcIsSignedInt = op.RawOpCode.Extract(13);
  121. bool negateB = op.RawOpCode.Extract(45);
  122. bool absoluteB = op.RawOpCode.Extract(49);
  123. Operand srcB = GetSrcB(context);
  124. if (srcIsSmallInt)
  125. {
  126. int size = srcType == IntegerType.U16 ? 16 : 8;
  127. srcB = srcIsSignedInt
  128. ? context.BitfieldExtractS32(srcB, Const(op.ByteSelection * 8), Const(size))
  129. : context.BitfieldExtractU32(srcB, Const(op.ByteSelection * 8), Const(size));
  130. }
  131. srcB = context.IAbsNeg(srcB, absoluteB, negateB);
  132. if (op.Saturate)
  133. {
  134. if (dstIsSignedInt)
  135. {
  136. dstType |= IntegerType.S8;
  137. }
  138. int min = (int)GetIntMin(dstType);
  139. int max = (int)GetIntMax(dstType);
  140. srcB = dstIsSignedInt
  141. ? context.IClampS32(srcB, Const(min), Const(max))
  142. : context.IClampU32(srcB, Const(min), Const(max));
  143. }
  144. context.Copy(GetDest(context), srcB);
  145. // TODO: CC.
  146. }
  147. private static void WriteFP(EmitterContext context, FPType type, Operand srcB)
  148. {
  149. Operand dest = GetDest(context);
  150. if (type == FPType.FP32)
  151. {
  152. context.Copy(dest, srcB);
  153. }
  154. else if (type == FPType.FP16)
  155. {
  156. context.Copy(dest, context.PackHalf2x16(srcB, ConstF(0)));
  157. }
  158. else
  159. {
  160. // TODO.
  161. }
  162. }
  163. }
  164. }