InstEmitVideoMinMax.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.IntermediateRepresentation.OperandHelper;
  6. namespace Ryujinx.Graphics.Shader.Instructions
  7. {
  8. static partial class InstEmit
  9. {
  10. public static void Vmnmx(EmitterContext context)
  11. {
  12. InstVmnmx op = context.GetOp<InstVmnmx>();
  13. Operand srcA = InstEmitAluHelper.Extend(context, GetSrcReg(context, op.SrcA), op.ASelect);
  14. Operand srcC = GetSrcReg(context, op.SrcC);
  15. Operand srcB;
  16. if (op.BVideo)
  17. {
  18. srcB = InstEmitAluHelper.Extend(context, GetSrcReg(context, op.SrcB), op.BSelect);
  19. }
  20. else
  21. {
  22. int imm = op.Imm16;
  23. if ((op.BSelect & VectorSelect.S8B0) != 0)
  24. {
  25. imm = (imm << 16) >> 16;
  26. }
  27. srcB = Const(imm);
  28. }
  29. Operand res;
  30. bool resSigned;
  31. if ((op.ASelect & VectorSelect.S8B0) != (op.BSelect & VectorSelect.S8B0))
  32. {
  33. // Signedness is different, but for max, result will always fit a U32,
  34. // since one of the inputs can't be negative, and the result is the one
  35. // with highest value. For min, it will always fit on a S32, since
  36. // one of the input can't be greater than INT_MAX and we want the lowest value.
  37. resSigned = !op.Mn;
  38. res = op.Mn ? context.IMaximumU32(srcA, srcB) : context.IMinimumS32(srcA, srcB);
  39. if ((op.ASelect & VectorSelect.S8B0) != 0)
  40. {
  41. Operand isBGtIntMax = context.ICompareLess(srcB, Const(0));
  42. res = context.ConditionalSelect(isBGtIntMax, srcB, res);
  43. }
  44. else
  45. {
  46. Operand isAGtIntMax = context.ICompareLess(srcA, Const(0));
  47. res = context.ConditionalSelect(isAGtIntMax, srcA, res);
  48. }
  49. }
  50. else
  51. {
  52. // Ra and Rb have the same signedness, so doesn't matter which one we test.
  53. resSigned = (op.ASelect & VectorSelect.S8B0) != 0;
  54. if (op.Mn)
  55. {
  56. res = resSigned
  57. ? context.IMaximumS32(srcA, srcB)
  58. : context.IMaximumU32(srcA, srcB);
  59. }
  60. else
  61. {
  62. res = resSigned
  63. ? context.IMinimumS32(srcA, srcB)
  64. : context.IMinimumU32(srcA, srcB);
  65. }
  66. }
  67. if (op.Sat)
  68. {
  69. if (op.DFormat && !resSigned)
  70. {
  71. res = context.IMinimumU32(res, Const(int.MaxValue));
  72. }
  73. else if (!op.DFormat && resSigned)
  74. {
  75. res = context.IMaximumS32(res, Const(0));
  76. }
  77. }
  78. switch (op.VideoOp)
  79. {
  80. case VideoOp.Acc:
  81. res = context.IAdd(res, srcC);
  82. break;
  83. case VideoOp.Max:
  84. res = op.DFormat ? context.IMaximumS32(res, srcC) : context.IMaximumU32(res, srcC);
  85. break;
  86. case VideoOp.Min:
  87. res = op.DFormat ? context.IMinimumS32(res, srcC) : context.IMinimumU32(res, srcC);
  88. break;
  89. case VideoOp.Mrg16h:
  90. res = context.BitfieldInsert(srcC, res, Const(16), Const(16));
  91. break;
  92. case VideoOp.Mrg16l:
  93. res = context.BitfieldInsert(srcC, res, Const(0), Const(16));
  94. break;
  95. case VideoOp.Mrg8b0:
  96. res = context.BitfieldInsert(srcC, res, Const(0), Const(8));
  97. break;
  98. case VideoOp.Mrg8b2:
  99. res = context.BitfieldInsert(srcC, res, Const(16), Const(8));
  100. break;
  101. }
  102. context.Copy(GetDest(op.Dest), res);
  103. }
  104. public static void Vsetp(EmitterContext context)
  105. {
  106. InstVsetp op = context.GetOp<InstVsetp>();
  107. Operand srcA = InstEmitAluHelper.Extend(context, GetSrcReg(context, op.SrcA), op.ASelect);
  108. Operand srcB;
  109. if (op.BVideo)
  110. {
  111. srcB = InstEmitAluHelper.Extend(context, GetSrcReg(context, op.SrcB), op.BSelect);
  112. }
  113. else
  114. {
  115. int imm = op.Imm16;
  116. if ((op.BSelect & VectorSelect.S8B0) != 0)
  117. {
  118. imm = (imm << 16) >> 16;
  119. }
  120. srcB = Const(imm);
  121. }
  122. Operand p0Res;
  123. bool signedA = (op.ASelect & VectorSelect.S8B0) != 0;
  124. bool signedB = (op.BSelect & VectorSelect.S8B0) != 0;
  125. if (signedA != signedB)
  126. {
  127. bool a32 = (op.ASelect & ~VectorSelect.S8B0) == VectorSelect.U32;
  128. bool b32 = (op.BSelect & ~VectorSelect.S8B0) == VectorSelect.U32;
  129. if (!a32 && !b32)
  130. {
  131. // Both values are extended small integer and can always fit in a S32, just do a signed comparison.
  132. p0Res = GetIntComparison(context, op.VComp, srcA, srcB, isSigned: true, extended: false);
  133. }
  134. else
  135. {
  136. // TODO: Mismatching sign case.
  137. p0Res = Const(0);
  138. }
  139. }
  140. else
  141. {
  142. // Sign matches, just do a regular comparison.
  143. p0Res = GetIntComparison(context, op.VComp, srcA, srcB, signedA, extended: false);
  144. }
  145. Operand p1Res = context.BitwiseNot(p0Res);
  146. Operand pred = GetPredicate(context, op.SrcPred, op.SrcPredInv);
  147. p0Res = InstEmitAluHelper.GetPredLogicalOp(context, op.BoolOp, p0Res, pred);
  148. p1Res = InstEmitAluHelper.GetPredLogicalOp(context, op.BoolOp, p1Res, pred);
  149. context.Copy(Register(op.DestPred, RegisterType.Predicate), p0Res);
  150. context.Copy(Register(op.DestPredInv, RegisterType.Predicate), p1Res);
  151. }
  152. }
  153. }