InstEmitAttribute.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 (Internal Stage Buffer Entry) memory.
  127. // Here, we just propagate the offset, as the result from this instruction is usually
  128. // used with ALD to perform vertex load on geometry or tessellation shaders.
  129. // The offset is calculated as (PrimitiveIndex * VerticesPerPrimitive) + VertexIndex.
  130. // Since we hardcode PrimitiveIndex to zero, then the offset will be just VertexIndex.
  131. context.Copy(GetDest(op.Dest), GetSrcReg(context, op.SrcA));
  132. }
  133. public static void OutR(EmitterContext context)
  134. {
  135. InstOutR op = context.GetOp<InstOutR>();
  136. EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
  137. }
  138. public static void OutI(EmitterContext context)
  139. {
  140. InstOutI op = context.GetOp<InstOutI>();
  141. EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
  142. }
  143. public static void OutC(EmitterContext context)
  144. {
  145. InstOutC op = context.GetOp<InstOutC>();
  146. EmitOut(context, op.OutType.HasFlag(OutType.Emit), op.OutType.HasFlag(OutType.Cut));
  147. }
  148. private static void EmitOut(EmitterContext context, bool emit, bool cut)
  149. {
  150. if (!(emit || cut))
  151. {
  152. context.Config.GpuAccessor.Log("Invalid OUT encoding.");
  153. }
  154. if (emit)
  155. {
  156. if (context.Config.LastInVertexPipeline)
  157. {
  158. context.PrepareForVertexReturn(out var tempXLocal, out var tempYLocal, out var tempZLocal);
  159. context.EmitVertex();
  160. // Restore output position value before transformation.
  161. if (tempXLocal != null)
  162. {
  163. context.Copy(Attribute(AttributeConsts.PositionX), tempXLocal);
  164. }
  165. if (tempYLocal != null)
  166. {
  167. context.Copy(Attribute(AttributeConsts.PositionY), tempYLocal);
  168. }
  169. if (tempZLocal != null)
  170. {
  171. context.Copy(Attribute(AttributeConsts.PositionZ), tempZLocal);
  172. }
  173. }
  174. else
  175. {
  176. context.EmitVertex();
  177. }
  178. }
  179. if (cut)
  180. {
  181. context.EndPrimitive();
  182. }
  183. }
  184. private static bool TryFixedFuncToUserAttributeIpa(EmitterContext context, int attr, out Operand selectedAttr)
  185. {
  186. if (attr >= AttributeConsts.FrontColorDiffuseR && attr < AttributeConsts.BackColorDiffuseR)
  187. {
  188. // TODO: If two sided rendering is enabled, then this should return
  189. // FrontColor if the fragment is front facing, and back color otherwise.
  190. int index = (attr - AttributeConsts.FrontColorDiffuseR) >> 4;
  191. int userAttrIndex = context.Config.GetFreeUserAttribute(isOutput: false, index);
  192. Operand frontAttr = Attribute(AttributeConsts.UserAttributeBase + userAttrIndex * 16 + (attr & 0xf));
  193. context.Config.SetInputUserAttributeFixedFunc(userAttrIndex);
  194. selectedAttr = frontAttr;
  195. return true;
  196. }
  197. else if (attr >= AttributeConsts.BackColorDiffuseR && attr < AttributeConsts.ClipDistance0)
  198. {
  199. selectedAttr = ConstF(((attr >> 2) & 3) == 3 ? 1f : 0f);
  200. return true;
  201. }
  202. else if (attr >= AttributeConsts.TexCoordBase && attr < AttributeConsts.TexCoordEnd)
  203. {
  204. selectedAttr = Attribute(FixedFuncToUserAttribute(context.Config, attr, AttributeConsts.TexCoordBase, 4, isOutput: false));
  205. return true;
  206. }
  207. selectedAttr = Attribute(attr);
  208. return false;
  209. }
  210. private static int FixedFuncToUserAttribute(ShaderConfig config, int attr, bool isOutput)
  211. {
  212. if (attr >= AttributeConsts.FrontColorDiffuseR && attr < AttributeConsts.ClipDistance0)
  213. {
  214. attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.FrontColorDiffuseR, 0, isOutput);
  215. }
  216. else if (attr >= AttributeConsts.TexCoordBase && attr < AttributeConsts.TexCoordEnd)
  217. {
  218. attr = FixedFuncToUserAttribute(config, attr, AttributeConsts.TexCoordBase, 4, isOutput);
  219. }
  220. return attr;
  221. }
  222. private static int FixedFuncToUserAttribute(ShaderConfig config, int attr, int baseAttr, int baseIndex, bool isOutput)
  223. {
  224. int index = (attr - baseAttr) >> 4;
  225. int userAttrIndex = config.GetFreeUserAttribute(isOutput, index);
  226. if ((uint)userAttrIndex < Constants.MaxAttributes)
  227. {
  228. userAttrIndex += baseIndex;
  229. attr = AttributeConsts.UserAttributeBase + userAttrIndex * 16 + (attr & 0xf);
  230. if (isOutput)
  231. {
  232. config.SetOutputUserAttributeFixedFunc(userAttrIndex);
  233. }
  234. else
  235. {
  236. config.SetInputUserAttributeFixedFunc(userAttrIndex);
  237. }
  238. }
  239. return attr;
  240. }
  241. }
  242. }