InstEmitMove.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // Note: Lowest 8-bits seems to contain some primitive index,
  73. // but it seems to be NVIDIA implementation specific as it's only used
  74. // to calculate ISBE offsets, so we can just keep it as zero.
  75. if (context.Config.Stage == ShaderStage.TessellationControl ||
  76. context.Config.Stage == ShaderStage.TessellationEvaluation)
  77. {
  78. src = context.ShiftLeft(Attribute(AttributeConsts.PatchVerticesIn), Const(16));
  79. }
  80. else
  81. {
  82. src = Const(context.Config.GpuAccessor.QueryPrimitiveTopology().ToInputVertices() << 16);
  83. }
  84. }
  85. else
  86. {
  87. src = Const(0);
  88. }
  89. break;
  90. case SReg.TId:
  91. Operand tidX = Attribute(AttributeConsts.ThreadIdX);
  92. Operand tidY = Attribute(AttributeConsts.ThreadIdY);
  93. Operand tidZ = Attribute(AttributeConsts.ThreadIdZ);
  94. tidY = context.ShiftLeft(tidY, Const(16));
  95. tidZ = context.ShiftLeft(tidZ, Const(26));
  96. src = context.BitwiseOr(tidX, context.BitwiseOr(tidY, tidZ));
  97. break;
  98. case SReg.TIdX:
  99. src = Attribute(AttributeConsts.ThreadIdX);
  100. break;
  101. case SReg.TIdY:
  102. src = Attribute(AttributeConsts.ThreadIdY);
  103. break;
  104. case SReg.TIdZ:
  105. src = Attribute(AttributeConsts.ThreadIdZ);
  106. break;
  107. case SReg.CtaIdX:
  108. src = Attribute(AttributeConsts.CtaIdX);
  109. break;
  110. case SReg.CtaIdY:
  111. src = Attribute(AttributeConsts.CtaIdY);
  112. break;
  113. case SReg.CtaIdZ:
  114. src = Attribute(AttributeConsts.CtaIdZ);
  115. break;
  116. case SReg.EqMask:
  117. src = Attribute(AttributeConsts.EqMask);
  118. break;
  119. case SReg.LtMask:
  120. src = Attribute(AttributeConsts.LtMask);
  121. break;
  122. case SReg.LeMask:
  123. src = Attribute(AttributeConsts.LeMask);
  124. break;
  125. case SReg.GtMask:
  126. src = Attribute(AttributeConsts.GtMask);
  127. break;
  128. case SReg.GeMask:
  129. src = Attribute(AttributeConsts.GeMask);
  130. break;
  131. default:
  132. src = Const(0);
  133. break;
  134. }
  135. context.Copy(GetDest(op.Dest), src);
  136. }
  137. public static void SelR(EmitterContext context)
  138. {
  139. InstSelR op = context.GetOp<InstSelR>();
  140. Operand srcA = GetSrcReg(context, op.SrcA);
  141. Operand srcB = GetSrcReg(context, op.SrcB);
  142. Operand srcPred = GetPredicate(context, op.SrcPred, op.SrcPredInv);
  143. EmitSel(context, srcA, srcB, srcPred, op.Dest);
  144. }
  145. public static void SelI(EmitterContext context)
  146. {
  147. InstSelI op = context.GetOp<InstSelI>();
  148. Operand srcA = GetSrcReg(context, op.SrcA);
  149. Operand srcB = GetSrcImm(context, Imm20ToSInt(op.Imm20));
  150. Operand srcPred = GetPredicate(context, op.SrcPred, op.SrcPredInv);
  151. EmitSel(context, srcA, srcB, srcPred, op.Dest);
  152. }
  153. public static void SelC(EmitterContext context)
  154. {
  155. InstSelC op = context.GetOp<InstSelC>();
  156. Operand srcA = GetSrcReg(context, op.SrcA);
  157. Operand srcB = GetSrcCbuf(context, op.CbufSlot, op.CbufOffset);
  158. Operand srcPred = GetPredicate(context, op.SrcPred, op.SrcPredInv);
  159. EmitSel(context, srcA, srcB, srcPred, op.Dest);
  160. }
  161. private static void EmitR2p(EmitterContext context, Operand value, Operand mask, ByteSel byteSel, bool ccpr)
  162. {
  163. Operand Test(Operand value, int bit)
  164. {
  165. return context.ICompareNotEqual(context.BitwiseAnd(value, Const(1 << bit)), Const(0));
  166. }
  167. if (ccpr)
  168. {
  169. // TODO: Support Register to condition code flags copy.
  170. context.Config.GpuAccessor.Log("R2P.CC not implemented.");
  171. }
  172. else
  173. {
  174. int shift = (int)byteSel * 8;
  175. for (int bit = 0; bit < RegisterConsts.PredsCount; bit++)
  176. {
  177. Operand pred = Register(bit, RegisterType.Predicate);
  178. Operand res = context.ConditionalSelect(Test(mask, bit), Test(value, bit + shift), pred);
  179. context.Copy(pred, res);
  180. }
  181. }
  182. }
  183. private static void EmitSel(EmitterContext context, Operand srcA, Operand srcB, Operand srcPred, int rd)
  184. {
  185. Operand res = context.ConditionalSelect(srcPred, srcA, srcB);
  186. context.Copy(GetDest(rd), res);
  187. }
  188. }
  189. }