InstructionInfo.cs 15 KB

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