InstEmitAttribute.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 = FixedFuncToUserAttribute(context.Config, op.Imm11 + index * 4, op.O);
  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 = FixedFuncToUserAttribute(context.Config, op.Imm11 + index * 4, op.O);
  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. if (!context.Config.IsUsedOutputAttribute(offset))
  79. {
  80. return;
  81. }
  82. offset = FixedFuncToUserAttribute(context.Config, offset, isOutput: true);
  83. context.FlagAttributeWritten(offset);
  84. Operand dest = op.P ? AttributePerPatch(offset) : Attribute(offset);
  85. context.Copy(dest, Register(rd));
  86. }
  87. }
  88. }
  89. public static void Ipa(EmitterContext context)
  90. {
  91. InstIpa op = context.GetOp<InstIpa>();
  92. context.FlagAttributeRead(op.Imm10);
  93. Operand res;
  94. bool isFixedFunc = false;
  95. if (op.Idx)
  96. {
  97. Operand userAttrOffset = context.ISubtract(GetSrcReg(context, op.SrcA), Const(AttributeConsts.UserAttributeBase));
  98. Operand userAttrIndex = context.ShiftRightU32(userAttrOffset, Const(2));
  99. res = context.LoadAttribute(Const(AttributeConsts.UserAttributeBase), userAttrIndex, Const(0));
  100. res = context.FPMultiply(res, Attribute(AttributeConsts.PositionW));
  101. context.Config.SetUsedFeature(FeatureFlags.IaIndexing);
  102. }
  103. else
  104. {
  105. isFixedFunc = TryFixedFuncToUserAttributeIpa(context, op.Imm10, out res);
  106. if (op.Imm10 >= AttributeConsts.UserAttributeBase && op.Imm10 < AttributeConsts.UserAttributeEnd)
  107. {
  108. int index = (op.Imm10 - AttributeConsts.UserAttributeBase) >> 4;
  109. if (context.Config.ImapTypes[index].GetFirstUsedType() == PixelImap.Perspective)
  110. {
  111. res = context.FPMultiply(res, Attribute(AttributeConsts.PositionW));
  112. }
  113. }
  114. }
  115. if (op.IpaOp == IpaOp.Multiply && !isFixedFunc)
  116. {
  117. Operand srcB = GetSrcReg(context, op.SrcB);
  118. res = context.FPMultiply(res, srcB);
  119. }
  120. res = context.FPSaturate(res, op.Sat);
  121. context.Copy(GetDest(op.Dest), res);
  122. }
  123. public static void Isberd(EmitterContext context)
  124. {
  125. InstIsberd op = context.GetOp<InstIsberd>();
  126. // This instruction performs a load from ISBE memory,
  127. // however it seems to be only used to get some vertex
  128. // input data, so we instead propagate the offset so that
  129. // it can be used on the attribute load.
  130. context.Copy(GetDest(op.Dest), GetSrcReg(context, op.SrcA));
  131. }
  132. public static void OutR(EmitterContext context)
  133. {
  134. InstOutR op = context.GetOp<InstOutR>();
  135. EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
  136. }
  137. public static void OutI(EmitterContext context)
  138. {
  139. InstOutI op = context.GetOp<InstOutI>();
  140. EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
  141. }
  142. public static void OutC(EmitterContext context)
  143. {
  144. InstOutC op = context.GetOp<InstOutC>();
  145. EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
  146. }
  147. private static void EmitOut(EmitterContext context, bool emit, bool cut)
  148. {
  149. if (!(emit || cut))
  150. {
  151. context.Config.GpuAccessor.Log("Invalid OUT encoding.");
  152. }
  153. if (emit)
  154. {
  155. context.EmitVertex();
  156. }
  157. if (cut)
  158. {
  159. context.EndPrimitive();
  160. }
  161. }
  162. private static bool TryFixedFuncToUserAttributeIpa(EmitterContext context, int attr, out Operand selectedAttr)
  163. {
  164. if (attr >= AttributeConsts.FrontColorDiffuseR && attr < AttributeConsts.BackColorDiffuseR)
  165. {
  166. // TODO: If two sided rendering is enabled, then this should return
  167. // FrontColor if the fragment is front facing, and back color otherwise.
  168. int index = (attr - AttributeConsts.FrontColorDiffuseR) >> 4;
  169. int userAttrIndex = context.Config.GetFreeUserAttribute(isOutput: false, index);
  170. Operand frontAttr = Attribute(AttributeConsts.UserAttributeBase + userAttrIndex * 16 + (attr & 0xf));
  171. context.Config.SetInputUserAttributeFixedFunc(userAttrIndex);
  172. selectedAttr = frontAttr;
  173. return true;
  174. }
  175. else if (attr >= AttributeConsts.BackColorDiffuseR && attr < AttributeConsts.ClipDistance0)
  176. {
  177. selectedAttr = ConstF(((attr >> 2) & 3) == 3 ? 1f : 0f);
  178. return true;
  179. }
  180. else if (attr >= AttributeConsts.TexCoordBase && attr < AttributeConsts.TexCoordEnd)
  181. {
  182. selectedAttr = Attribute(FixedFuncToUserAttribute(context.Config, attr, AttributeConsts.TexCoordBase, 4, isOutput: false));
  183. return true;
  184. }
  185. selectedAttr = Attribute(attr);
  186. return false;
  187. }
  188. private static int FixedFuncToUserAttribute(ShaderConfig config, int attr, bool isOutput)
  189. {
  190. if (attr >= AttributeConsts.FrontColorDiffuseR && attr < AttributeConsts.ClipDistance0)
  191. {
  192. attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.FrontColorDiffuseR, 0, isOutput);
  193. }
  194. else if (attr >= AttributeConsts.TexCoordBase && attr < AttributeConsts.TexCoordEnd)
  195. {
  196. attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.TexCoordBase, 4, isOutput);
  197. }
  198. return attr;
  199. }
  200. private static int FixedFuncToUserAttribute(ShaderConfig config, int attr, int baseAttr, int baseIndex, bool isOutput)
  201. {
  202. int index = (attr - baseAttr) >> 4;
  203. int userAttrIndex = config.GetFreeUserAttribute(isOutput, index);
  204. if ((uint)userAttrIndex < Constants.MaxAttributes)
  205. {
  206. userAttrIndex += baseIndex;
  207. attr = AttributeConsts.UserAttributeBase + userAttrIndex * 16 + (attr & 0xf);
  208. if (isOutput)
  209. {
  210. config.SetOutputUserAttributeFixedFunc(userAttrIndex);
  211. }
  212. else
  213. {
  214. config.SetInputUserAttributeFixedFunc(userAttrIndex);
  215. }
  216. }
  217. return attr;
  218. }
  219. }
  220. }