InstEmitMove.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 MovR(EmitterContext context)
  11. {
  12. InstMovR op = context.GetOp<InstMovR>();
  13. context.Copy(GetDest(op.Dest), GetSrcReg(context, op.SrcA));
  14. }
  15. public static void MovI(EmitterContext context)
  16. {
  17. InstMovI op = context.GetOp<InstMovI>();
  18. context.Copy(GetDest(op.Dest), GetSrcImm(context, op.Imm20));
  19. }
  20. public static void MovC(EmitterContext context)
  21. {
  22. InstMovC op = context.GetOp<InstMovC>();
  23. context.Copy(GetDest(op.Dest), GetSrcCbuf(context, op.CbufSlot, op.CbufOffset));
  24. }
  25. public static void Mov32i(EmitterContext context)
  26. {
  27. InstMov32i op = context.GetOp<InstMov32i>();
  28. context.Copy(GetDest(op.Dest), GetSrcImm(context, op.Imm32));
  29. }
  30. public static void R2pR(EmitterContext context)
  31. {
  32. InstR2pR op = context.GetOp<InstR2pR>();
  33. Operand value = GetSrcReg(context, op.SrcA);
  34. Operand mask = GetSrcReg(context, op.SrcB);
  35. EmitR2p(context, value, mask, op.ByteSel, op.Ccpr);
  36. }
  37. public static void R2pI(EmitterContext context)
  38. {
  39. InstR2pI op = context.GetOp<InstR2pI>();
  40. Operand value = GetSrcReg(context, op.SrcA);
  41. Operand mask = GetSrcImm(context, Imm20ToSInt(op.Imm20));
  42. EmitR2p(context, value, mask, op.ByteSel, op.Ccpr);
  43. }
  44. public static void R2pC(EmitterContext context)
  45. {
  46. InstR2pC op = context.GetOp<InstR2pC>();
  47. Operand value = GetSrcReg(context, op.SrcA);
  48. Operand mask = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  49. EmitR2p(context, value, mask, op.ByteSel, op.Ccpr);
  50. }
  51. public static void S2r(EmitterContext context)
  52. {
  53. InstS2r op = context.GetOp<InstS2r>();
  54. Operand src;
  55. switch (op.SReg)
  56. {
  57. case SReg.LaneId:
  58. src = Attribute(AttributeConsts.LaneId);
  59. break;
  60. case SReg.InvocationId:
  61. src = Attribute(AttributeConsts.InvocationId);
  62. break;
  63. case SReg.YDirection:
  64. src = ConstF(1); // TODO: Use value from Y direction GPU register.
  65. break;
  66. case SReg.ThreadKill:
  67. src = context.Config.Stage == ShaderStage.Fragment ? Attribute(AttributeConsts.ThreadKill) : Const(0);
  68. break;
  69. case SReg.InvocationInfo:
  70. if (context.Config.Stage != ShaderStage.Compute && context.Config.Stage != ShaderStage.Fragment)
  71. {
  72. Operand primitiveId = Attribute(AttributeConsts.PrimitiveId);
  73. Operand patchVerticesIn = Attribute(AttributeConsts.PatchVerticesIn);
  74. patchVerticesIn = context.ShiftLeft(patchVerticesIn, Const(16));
  75. src = context.BitwiseOr(primitiveId, patchVerticesIn);
  76. }
  77. else
  78. {
  79. src = Const(0);
  80. }
  81. break;
  82. case SReg.TId:
  83. Operand tidX = Attribute(AttributeConsts.ThreadIdX);
  84. Operand tidY = Attribute(AttributeConsts.ThreadIdY);
  85. Operand tidZ = Attribute(AttributeConsts.ThreadIdZ);
  86. tidY = context.ShiftLeft(tidY, Const(16));
  87. tidZ = context.ShiftLeft(tidZ, Const(26));
  88. src = context.BitwiseOr(tidX, context.BitwiseOr(tidY, tidZ));
  89. break;
  90. case SReg.TIdX:
  91. src = Attribute(AttributeConsts.ThreadIdX);
  92. break;
  93. case SReg.TIdY:
  94. src = Attribute(AttributeConsts.ThreadIdY);
  95. break;
  96. case SReg.TIdZ:
  97. src = Attribute(AttributeConsts.ThreadIdZ);
  98. break;
  99. case SReg.CtaIdX:
  100. src = Attribute(AttributeConsts.CtaIdX);
  101. break;
  102. case SReg.CtaIdY:
  103. src = Attribute(AttributeConsts.CtaIdY);
  104. break;
  105. case SReg.CtaIdZ:
  106. src = Attribute(AttributeConsts.CtaIdZ);
  107. break;
  108. case SReg.EqMask:
  109. src = Attribute(AttributeConsts.EqMask);
  110. break;
  111. case SReg.LtMask:
  112. src = Attribute(AttributeConsts.LtMask);
  113. break;
  114. case SReg.LeMask:
  115. src = Attribute(AttributeConsts.LeMask);
  116. break;
  117. case SReg.GtMask:
  118. src = Attribute(AttributeConsts.GtMask);
  119. break;
  120. case SReg.GeMask:
  121. src = Attribute(AttributeConsts.GeMask);
  122. break;
  123. default:
  124. src = Const(0);
  125. break;
  126. }
  127. context.Copy(GetDest(op.Dest), src);
  128. }
  129. public static void SelR(EmitterContext context)
  130. {
  131. InstSelR op = context.GetOp<InstSelR>();
  132. Operand srcA = GetSrcReg(context, op.SrcA);
  133. Operand srcB = GetSrcReg(context, op.SrcB);
  134. Operand srcPred = GetPredicate(context, op.SrcPred, op.SrcPredInv);
  135. EmitSel(context, srcA, srcB, srcPred, op.Dest);
  136. }
  137. public static void SelI(EmitterContext context)
  138. {
  139. InstSelI op = context.GetOp<InstSelI>();
  140. Operand srcA = GetSrcReg(context, op.SrcA);
  141. Operand srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
  142. Operand srcPred = GetPredicate(context, op.SrcPred, op.SrcPredInv);
  143. EmitSel(context, srcA, srcB, srcPred, op.Dest);
  144. }
  145. public static void SelC(EmitterContext context)
  146. {
  147. InstSelC op = context.GetOp<InstSelC>();
  148. Operand srcA = GetSrcReg(context, op.SrcA);
  149. Operand srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  150. Operand srcPred = GetPredicate(context, op.SrcPred, op.SrcPredInv);
  151. EmitSel(context, srcA, srcB, srcPred, op.Dest);
  152. }
  153. private static void EmitR2p(EmitterContext context, Operand value, Operand mask, ByteSel byteSel, bool ccpr)
  154. {
  155. Operand Test(Operand value, int bit)
  156. {
  157. return context.ICompareNotEqual(context.BitwiseAnd(value, Const(1 << bit)), Const(0));
  158. }
  159. if (ccpr)
  160. {
  161. // TODO: Support Register to condition code flags copy.
  162. context.Config.GpuAccessor.Log("R2P.CC not implemented.");
  163. }
  164. else
  165. {
  166. int shift = (int)byteSel * 8;
  167. for (int bit = 0; bit < RegisterConsts.PredsCount; bit++)
  168. {
  169. Operand pred = Register(bit, RegisterType.Predicate);
  170. Operand res = context.ConditionalSelect(Test(mask, bit), Test(value, bit + shift), pred);
  171. context.Copy(pred, res);
  172. }
  173. }
  174. }
  175. private static void EmitSel(EmitterContext context, Operand srcA, Operand srcB, Operand srcPred, int rd)
  176. {
  177. Operand res = context.ConditionalSelect(srcPred, srcA, srcB);
  178. context.Copy(GetDest(rd), res);
  179. }
  180. }
  181. }