InstEmitAttribute.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Al2p(EmitterContext context)
  11. {
  12. InstAl2p op = context.GetOp<InstAl2p>();
  13. context.Copy(GetDest(op.Dest), context.IAdd(GetSrcReg(context, op.SrcA), Const(op.Imm11)));
  14. }
  15. public static void Ald(EmitterContext context)
  16. {
  17. InstAld op = context.GetOp<InstAld>();
  18. Operand primVertex = context.Copy(GetSrcReg(context, op.SrcB));
  19. for (int index = 0; index < (int)op.AlSize + 1; index++)
  20. {
  21. Register rd = new Register(op.Dest + index, RegisterType.Gpr);
  22. if (rd.IsRZ)
  23. {
  24. break;
  25. }
  26. if (op.Phys)
  27. {
  28. Operand userAttrOffset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
  29. Operand userAttrIndex = context.ShiftRightU32(userAttrOffset, Const(2));
  30. context.Copy(Register(rd), context.LoadAttribute(Const(AttributeConsts.UserAttributeBase), userAttrIndex, primVertex));
  31. context.Config.SetUsedFeature(FeatureFlags.IaIndexing);
  32. }
  33. else if (op.SrcB == RegisterConsts.RegisterZeroIndex || op.P)
  34. {
  35. int offset = op.Imm11 + index * 4;
  36. context.FlagAttributeRead(offset);
  37. if (op.O)
  38. {
  39. offset |= AttributeConsts.LoadOutputMask;
  40. }
  41. Operand src = op.P ? AttributePerPatch(offset) : Attribute(offset);
  42. context.Copy(Register(rd), src);
  43. }
  44. else
  45. {
  46. int offset = op.Imm11 + index * 4;
  47. context.FlagAttributeRead(offset);
  48. if (op.O)
  49. {
  50. offset |= AttributeConsts.LoadOutputMask;
  51. }
  52. Operand src = Const(offset);
  53. context.Copy(Register(rd), context.LoadAttribute(src, Const(0), primVertex));
  54. }
  55. }
  56. }
  57. public static void Ast(EmitterContext context)
  58. {
  59. InstAst op = context.GetOp<InstAst>();
  60. for (int index = 0; index < (int)op.AlSize + 1; index++)
  61. {
  62. if (op.SrcB + index > RegisterConsts.RegisterZeroIndex)
  63. {
  64. break;
  65. }
  66. Register rd = new Register(op.SrcB + index, RegisterType.Gpr);
  67. if (op.Phys)
  68. {
  69. Operand userAttrOffset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
  70. Operand userAttrIndex = context.ShiftRightU32(userAttrOffset, Const(2));
  71. context.StoreAttribute(Const(AttributeConsts.UserAttributeBase), userAttrIndex, Register(rd));
  72. context.Config.SetUsedFeature(FeatureFlags.OaIndexing);
  73. }
  74. else
  75. {
  76. // TODO: Support indirect stores using Ra.
  77. int offset = op.Imm11 + index * 4;
  78. context.FlagAttributeWritten(offset);
  79. Operand dest = op.P ? AttributePerPatch(offset) : Attribute(offset);
  80. context.Copy(dest, Register(rd));
  81. }
  82. }
  83. }
  84. public static void Ipa(EmitterContext context)
  85. {
  86. InstIpa op = context.GetOp<InstIpa>();
  87. context.FlagAttributeRead(op.Imm10);
  88. Operand res;
  89. if (op.Idx)
  90. {
  91. Operand userAttrOffset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
  92. Operand userAttrIndex = context.ShiftRightU32(userAttrOffset, Const(2));
  93. res = context.LoadAttribute(Const(AttributeConsts.UserAttributeBase), userAttrIndex, Const(0));
  94. res = context.FPMultiply(res, Attribute(AttributeConsts.PositionW));
  95. context.Config.SetUsedFeature(FeatureFlags.IaIndexing);
  96. }
  97. else
  98. {
  99. res = Attribute(op.Imm10);
  100. if (op.Imm10 >= AttributeConsts.UserAttributeBase && op.Imm10 < AttributeConsts.UserAttributeEnd)
  101. {
  102. int index = (op.Imm10 - AttributeConsts.UserAttributeBase) >> 4;
  103. if (context.Config.ImapTypes[index].GetFirstUsedType() == PixelImap.Perspective)
  104. {
  105. res = context.FPMultiply(res, Attribute(AttributeConsts.PositionW));
  106. }
  107. }
  108. }
  109. if (op.IpaOp == IpaOp.Multiply)
  110. {
  111. Operand srcB = GetSrcReg(context, op.SrcB);
  112. res = context.FPMultiply(res, srcB);
  113. }
  114. res = context.FPSaturate(res, op.Sat);
  115. context.Copy(GetDest(op.Dest), res);
  116. }
  117. public static void Isberd(EmitterContext context)
  118. {
  119. InstIsberd op = context.GetOp<InstIsberd>();
  120. // This instruction performs a load from ISBE memory,
  121. // however it seems to be only used to get some vertex
  122. // input data, so we instead propagate the offset so that
  123. // it can be used on the attribute load.
  124. context.Copy(GetDest(op.Dest), GetSrcReg(context, op.SrcA));
  125. }
  126. public static void OutR(EmitterContext context)
  127. {
  128. InstOutR op = context.GetOp<InstOutR>();
  129. EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
  130. }
  131. public static void OutI(EmitterContext context)
  132. {
  133. InstOutI op = context.GetOp<InstOutI>();
  134. EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
  135. }
  136. public static void OutC(EmitterContext context)
  137. {
  138. InstOutC op = context.GetOp<InstOutC>();
  139. EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
  140. }
  141. private static void EmitOut(EmitterContext context, bool emit, bool cut)
  142. {
  143. if (!(emit || cut))
  144. {
  145. context.Config.GpuAccessor.Log("Invalid OUT encoding.");
  146. }
  147. if (emit)
  148. {
  149. context.EmitVertex();
  150. }
  151. if (cut)
  152. {
  153. context.EndPrimitive();
  154. }
  155. }
  156. }
  157. }