InstructionInfo.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using System;
  4. namespace Ryujinx.Graphics.Shader.StructuredIr
  5. {
  6. static class InstructionInfo
  7. {
  8. private readonly struct InstInfo
  9. {
  10. public AggregateType DestType { get; }
  11. public AggregateType[] SrcTypes { get; }
  12. public InstInfo(AggregateType destType, params AggregateType[] srcTypes)
  13. {
  14. DestType = destType;
  15. SrcTypes = srcTypes;
  16. }
  17. }
  18. private static InstInfo[] _infoTbl;
  19. static InstructionInfo()
  20. {
  21. _infoTbl = new InstInfo[(int)Instruction.Count];
  22. // Inst Destination type Source 1 type Source 2 type Source 3 type Source 4 type
  23. Add(Instruction.AtomicAdd, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
  24. Add(Instruction.AtomicAnd, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
  25. Add(Instruction.AtomicCompareAndSwap, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32, AggregateType.U32);
  26. Add(Instruction.AtomicMaxS32, AggregateType.S32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  27. Add(Instruction.AtomicMaxU32, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
  28. Add(Instruction.AtomicMinS32, AggregateType.S32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  29. Add(Instruction.AtomicMinU32, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
  30. Add(Instruction.AtomicOr, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
  31. Add(Instruction.AtomicSwap, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
  32. Add(Instruction.AtomicXor, AggregateType.U32, AggregateType.S32, AggregateType.S32, AggregateType.U32);
  33. Add(Instruction.Absolute, AggregateType.Scalar, AggregateType.Scalar);
  34. Add(Instruction.Add, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
  35. Add(Instruction.Ballot, AggregateType.U32, AggregateType.Bool);
  36. Add(Instruction.BitCount, AggregateType.S32, AggregateType.S32);
  37. Add(Instruction.BitfieldExtractS32, AggregateType.S32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  38. Add(Instruction.BitfieldExtractU32, AggregateType.U32, AggregateType.U32, AggregateType.S32, AggregateType.S32);
  39. Add(Instruction.BitfieldInsert, AggregateType.S32, AggregateType.S32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  40. Add(Instruction.BitfieldReverse, AggregateType.S32, AggregateType.S32);
  41. Add(Instruction.BitwiseAnd, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  42. Add(Instruction.BitwiseExclusiveOr, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  43. Add(Instruction.BitwiseNot, AggregateType.S32, AggregateType.S32);
  44. Add(Instruction.BitwiseOr, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  45. Add(Instruction.BranchIfTrue, AggregateType.Void, AggregateType.Bool);
  46. Add(Instruction.BranchIfFalse, AggregateType.Void, AggregateType.Bool);
  47. Add(Instruction.Call, AggregateType.Scalar);
  48. Add(Instruction.Ceiling, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
  49. Add(Instruction.Clamp, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
  50. Add(Instruction.ClampU32, AggregateType.U32, AggregateType.U32, AggregateType.U32, AggregateType.U32);
  51. Add(Instruction.CompareEqual, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
  52. Add(Instruction.CompareGreater, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
  53. Add(Instruction.CompareGreaterOrEqual, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
  54. Add(Instruction.CompareGreaterOrEqualU32, AggregateType.Bool, AggregateType.U32, AggregateType.U32);
  55. Add(Instruction.CompareGreaterU32, AggregateType.Bool, AggregateType.U32, AggregateType.U32);
  56. Add(Instruction.CompareLess, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
  57. Add(Instruction.CompareLessOrEqual, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
  58. Add(Instruction.CompareLessOrEqualU32, AggregateType.Bool, AggregateType.U32, AggregateType.U32);
  59. Add(Instruction.CompareLessU32, AggregateType.Bool, AggregateType.U32, AggregateType.U32);
  60. Add(Instruction.CompareNotEqual, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
  61. Add(Instruction.ConditionalSelect, AggregateType.Scalar, AggregateType.Bool, AggregateType.Scalar, AggregateType.Scalar);
  62. Add(Instruction.ConvertFP32ToFP64, AggregateType.FP64, AggregateType.FP32);
  63. Add(Instruction.ConvertFP64ToFP32, AggregateType.FP32, AggregateType.FP64);
  64. Add(Instruction.ConvertFP32ToS32, AggregateType.S32, AggregateType.FP32);
  65. Add(Instruction.ConvertFP32ToU32, AggregateType.U32, AggregateType.FP32);
  66. Add(Instruction.ConvertFP64ToS32, AggregateType.S32, AggregateType.FP64);
  67. Add(Instruction.ConvertFP64ToU32, AggregateType.U32, AggregateType.FP64);
  68. Add(Instruction.ConvertS32ToFP32, AggregateType.FP32, AggregateType.S32);
  69. Add(Instruction.ConvertS32ToFP64, AggregateType.FP64, AggregateType.S32);
  70. Add(Instruction.ConvertU32ToFP32, AggregateType.FP32, AggregateType.U32);
  71. Add(Instruction.ConvertU32ToFP64, AggregateType.FP64, AggregateType.U32);
  72. Add(Instruction.Cosine, AggregateType.Scalar, AggregateType.Scalar);
  73. Add(Instruction.Ddx, AggregateType.FP32, AggregateType.FP32);
  74. Add(Instruction.Ddy, AggregateType.FP32, AggregateType.FP32);
  75. Add(Instruction.Divide, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
  76. Add(Instruction.ExponentB2, AggregateType.Scalar, AggregateType.Scalar);
  77. Add(Instruction.FindLSB, AggregateType.S32, AggregateType.S32);
  78. Add(Instruction.FindMSBS32, AggregateType.S32, AggregateType.S32);
  79. Add(Instruction.FindMSBU32, AggregateType.S32, AggregateType.U32);
  80. Add(Instruction.Floor, AggregateType.Scalar, AggregateType.Scalar);
  81. Add(Instruction.FusedMultiplyAdd, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
  82. Add(Instruction.ImageLoad, AggregateType.FP32);
  83. Add(Instruction.ImageStore, AggregateType.Void);
  84. Add(Instruction.ImageAtomic, AggregateType.S32);
  85. Add(Instruction.IsNan, AggregateType.Bool, AggregateType.Scalar);
  86. Add(Instruction.LoadAttribute, AggregateType.FP32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  87. Add(Instruction.LoadConstant, AggregateType.FP32, AggregateType.S32, AggregateType.S32);
  88. Add(Instruction.LoadGlobal, AggregateType.U32, AggregateType.S32, AggregateType.S32);
  89. Add(Instruction.LoadLocal, AggregateType.U32, AggregateType.S32);
  90. Add(Instruction.LoadShared, AggregateType.U32, AggregateType.S32);
  91. Add(Instruction.LoadStorage, AggregateType.U32, AggregateType.S32, AggregateType.S32);
  92. Add(Instruction.Lod, AggregateType.FP32);
  93. Add(Instruction.LogarithmB2, AggregateType.Scalar, AggregateType.Scalar);
  94. Add(Instruction.LogicalAnd, AggregateType.Bool, AggregateType.Bool, AggregateType.Bool);
  95. Add(Instruction.LogicalExclusiveOr, AggregateType.Bool, AggregateType.Bool, AggregateType.Bool);
  96. Add(Instruction.LogicalNot, AggregateType.Bool, AggregateType.Bool);
  97. Add(Instruction.LogicalOr, AggregateType.Bool, AggregateType.Bool, AggregateType.Bool);
  98. Add(Instruction.Maximum, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
  99. Add(Instruction.MaximumU32, AggregateType.U32, AggregateType.U32, AggregateType.U32);
  100. Add(Instruction.Minimum, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
  101. Add(Instruction.MinimumU32, AggregateType.U32, AggregateType.U32, AggregateType.U32);
  102. Add(Instruction.Multiply, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
  103. Add(Instruction.MultiplyHighS32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  104. Add(Instruction.MultiplyHighU32, AggregateType.U32, AggregateType.U32, AggregateType.U32);
  105. Add(Instruction.Negate, AggregateType.Scalar, AggregateType.Scalar);
  106. Add(Instruction.PackDouble2x32, AggregateType.FP64, AggregateType.U32, AggregateType.U32);
  107. Add(Instruction.PackHalf2x16, AggregateType.U32, AggregateType.FP32, AggregateType.FP32);
  108. Add(Instruction.ReciprocalSquareRoot, AggregateType.Scalar, AggregateType.Scalar);
  109. Add(Instruction.Round, AggregateType.Scalar, AggregateType.Scalar);
  110. Add(Instruction.ShiftLeft, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  111. Add(Instruction.ShiftRightS32, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  112. Add(Instruction.ShiftRightU32, AggregateType.U32, AggregateType.U32, AggregateType.S32);
  113. Add(Instruction.Shuffle, AggregateType.FP32, AggregateType.FP32, AggregateType.U32, AggregateType.U32, AggregateType.Bool);
  114. Add(Instruction.ShuffleDown, AggregateType.FP32, AggregateType.FP32, AggregateType.U32, AggregateType.U32, AggregateType.Bool);
  115. Add(Instruction.ShuffleUp, AggregateType.FP32, AggregateType.FP32, AggregateType.U32, AggregateType.U32, AggregateType.Bool);
  116. Add(Instruction.ShuffleXor, AggregateType.FP32, AggregateType.FP32, AggregateType.U32, AggregateType.U32, AggregateType.Bool);
  117. Add(Instruction.Sine, AggregateType.Scalar, AggregateType.Scalar);
  118. Add(Instruction.SquareRoot, AggregateType.Scalar, AggregateType.Scalar);
  119. Add(Instruction.StoreAttribute, AggregateType.Void, AggregateType.S32, AggregateType.S32, AggregateType.FP32);
  120. Add(Instruction.StoreGlobal, AggregateType.Void, AggregateType.S32, AggregateType.S32, AggregateType.U32);
  121. Add(Instruction.StoreLocal, AggregateType.Void, AggregateType.S32, AggregateType.U32);
  122. Add(Instruction.StoreShared, AggregateType.Void, AggregateType.S32, AggregateType.U32);
  123. Add(Instruction.StoreShared16, AggregateType.Void, AggregateType.S32, AggregateType.U32);
  124. Add(Instruction.StoreShared8, AggregateType.Void, AggregateType.S32, AggregateType.U32);
  125. Add(Instruction.StoreStorage, AggregateType.Void, AggregateType.S32, AggregateType.S32, AggregateType.U32);
  126. Add(Instruction.StoreStorage16, AggregateType.Void, AggregateType.S32, AggregateType.S32, AggregateType.U32);
  127. Add(Instruction.StoreStorage8, AggregateType.Void, AggregateType.S32, AggregateType.S32, AggregateType.U32);
  128. Add(Instruction.Subtract, AggregateType.Scalar, AggregateType.Scalar, AggregateType.Scalar);
  129. Add(Instruction.SwizzleAdd, AggregateType.FP32, AggregateType.FP32, AggregateType.FP32, AggregateType.S32);
  130. Add(Instruction.TextureSample, AggregateType.FP32);
  131. Add(Instruction.TextureSize, AggregateType.S32, AggregateType.S32, AggregateType.S32);
  132. Add(Instruction.Truncate, AggregateType.Scalar, AggregateType.Scalar);
  133. Add(Instruction.UnpackDouble2x32, AggregateType.U32, AggregateType.FP64);
  134. Add(Instruction.UnpackHalf2x16, AggregateType.FP32, AggregateType.U32);
  135. Add(Instruction.VectorExtract, AggregateType.Scalar, AggregateType.Vector4, AggregateType.S32);
  136. Add(Instruction.VoteAll, AggregateType.Bool, AggregateType.Bool);
  137. Add(Instruction.VoteAllEqual, AggregateType.Bool, AggregateType.Bool);
  138. Add(Instruction.VoteAny, AggregateType.Bool, AggregateType.Bool);
  139. }
  140. private static void Add(Instruction inst, AggregateType destType, params AggregateType[] srcTypes)
  141. {
  142. _infoTbl[(int)inst] = new InstInfo(destType, srcTypes);
  143. }
  144. public static AggregateType GetDestVarType(Instruction inst)
  145. {
  146. return GetFinalVarType(_infoTbl[(int)(inst & Instruction.Mask)].DestType, inst);
  147. }
  148. public static AggregateType GetSrcVarType(Instruction inst, int index)
  149. {
  150. // TODO: Return correct type depending on source index,
  151. // that can improve the decompiler output.
  152. if (inst == Instruction.ImageLoad ||
  153. inst == Instruction.ImageStore ||
  154. inst == Instruction.ImageAtomic ||
  155. inst == Instruction.Lod ||
  156. inst == Instruction.TextureSample)
  157. {
  158. return AggregateType.FP32;
  159. }
  160. else if (inst == Instruction.Call)
  161. {
  162. return AggregateType.S32;
  163. }
  164. return GetFinalVarType(_infoTbl[(int)(inst & Instruction.Mask)].SrcTypes[index], inst);
  165. }
  166. private static AggregateType GetFinalVarType(AggregateType type, Instruction inst)
  167. {
  168. if (type == AggregateType.Scalar)
  169. {
  170. if ((inst & Instruction.FP32) != 0)
  171. {
  172. return AggregateType.FP32;
  173. }
  174. else if ((inst & Instruction.FP64) != 0)
  175. {
  176. return AggregateType.FP64;
  177. }
  178. else
  179. {
  180. return AggregateType.S32;
  181. }
  182. }
  183. else if (type == AggregateType.Void)
  184. {
  185. throw new ArgumentException($"Invalid operand for instruction \"{inst}\".");
  186. }
  187. return type;
  188. }
  189. public static bool IsUnary(Instruction inst)
  190. {
  191. if (inst == Instruction.Copy)
  192. {
  193. return true;
  194. }
  195. else if (inst == Instruction.TextureSample)
  196. {
  197. return false;
  198. }
  199. return _infoTbl[(int)(inst & Instruction.Mask)].SrcTypes.Length == 1;
  200. }
  201. }
  202. }