InstEmitAttribute.cs 6.3 KB

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