InstEmitMove.cs 8.7 KB

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