InstGenHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.TypeConversion;
  4. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
  5. {
  6. static class InstGenHelper
  7. {
  8. private static InstInfo[] _infoTbl;
  9. static InstGenHelper()
  10. {
  11. _infoTbl = new InstInfo[(int)Instruction.Count];
  12. Add(Instruction.AtomicAdd, InstType.AtomicBinary, "atomicAdd");
  13. Add(Instruction.AtomicAnd, InstType.AtomicBinary, "atomicAnd");
  14. Add(Instruction.AtomicCompareAndSwap, InstType.AtomicTernary, "atomicCompSwap");
  15. Add(Instruction.AtomicMaxS32, InstType.CallTernary, HelperFunctionNames.AtomicMaxS32);
  16. Add(Instruction.AtomicMaxU32, InstType.AtomicBinary, "atomicMax");
  17. Add(Instruction.AtomicMinS32, InstType.CallTernary, HelperFunctionNames.AtomicMinS32);
  18. Add(Instruction.AtomicMinU32, InstType.AtomicBinary, "atomicMin");
  19. Add(Instruction.AtomicOr, InstType.AtomicBinary, "atomicOr");
  20. Add(Instruction.AtomicSwap, InstType.AtomicBinary, "atomicExchange");
  21. Add(Instruction.AtomicXor, InstType.AtomicBinary, "atomicXor");
  22. Add(Instruction.Absolute, InstType.CallUnary, "abs");
  23. Add(Instruction.Add, InstType.OpBinaryCom, "+", 2);
  24. Add(Instruction.Ballot, InstType.CallUnary, "ballotARB");
  25. Add(Instruction.Barrier, InstType.CallNullary, "barrier");
  26. Add(Instruction.BitCount, InstType.CallUnary, "bitCount");
  27. Add(Instruction.BitfieldExtractS32, InstType.CallTernary, "bitfieldExtract");
  28. Add(Instruction.BitfieldExtractU32, InstType.CallTernary, "bitfieldExtract");
  29. Add(Instruction.BitfieldInsert, InstType.CallQuaternary, "bitfieldInsert");
  30. Add(Instruction.BitfieldReverse, InstType.CallUnary, "bitfieldReverse");
  31. Add(Instruction.BitwiseAnd, InstType.OpBinaryCom, "&", 6);
  32. Add(Instruction.BitwiseExclusiveOr, InstType.OpBinaryCom, "^", 7);
  33. Add(Instruction.BitwiseNot, InstType.OpUnary, "~", 0);
  34. Add(Instruction.BitwiseOr, InstType.OpBinaryCom, "|", 8);
  35. Add(Instruction.Call, InstType.Special);
  36. Add(Instruction.Ceiling, InstType.CallUnary, "ceil");
  37. Add(Instruction.Clamp, InstType.CallTernary, "clamp");
  38. Add(Instruction.ClampU32, InstType.CallTernary, "clamp");
  39. Add(Instruction.CompareEqual, InstType.OpBinaryCom, "==", 5);
  40. Add(Instruction.CompareGreater, InstType.OpBinary, ">", 4);
  41. Add(Instruction.CompareGreaterOrEqual, InstType.OpBinary, ">=", 4);
  42. Add(Instruction.CompareGreaterOrEqualU32, InstType.OpBinary, ">=", 4);
  43. Add(Instruction.CompareGreaterU32, InstType.OpBinary, ">", 4);
  44. Add(Instruction.CompareLess, InstType.OpBinary, "<", 4);
  45. Add(Instruction.CompareLessOrEqual, InstType.OpBinary, "<=", 4);
  46. Add(Instruction.CompareLessOrEqualU32, InstType.OpBinary, "<=", 4);
  47. Add(Instruction.CompareLessU32, InstType.OpBinary, "<", 4);
  48. Add(Instruction.CompareNotEqual, InstType.OpBinaryCom, "!=", 5);
  49. Add(Instruction.ConditionalSelect, InstType.OpTernary, "?:", 12);
  50. Add(Instruction.ConvertFP32ToFP64, InstType.CallUnary, "double");
  51. Add(Instruction.ConvertFP64ToFP32, InstType.CallUnary, "float");
  52. Add(Instruction.ConvertFPToS32, InstType.CallUnary, "int");
  53. Add(Instruction.ConvertFPToU32, InstType.CallUnary, "uint");
  54. Add(Instruction.ConvertS32ToFP, InstType.CallUnary, "float");
  55. Add(Instruction.ConvertU32ToFP, InstType.CallUnary, "float");
  56. Add(Instruction.Cosine, InstType.CallUnary, "cos");
  57. Add(Instruction.Ddx, InstType.CallUnary, "dFdx");
  58. Add(Instruction.Ddy, InstType.CallUnary, "dFdy");
  59. Add(Instruction.Discard, InstType.OpNullary, "discard");
  60. Add(Instruction.Divide, InstType.OpBinary, "/", 1);
  61. Add(Instruction.EmitVertex, InstType.CallNullary, "EmitVertex");
  62. Add(Instruction.EndPrimitive, InstType.CallNullary, "EndPrimitive");
  63. Add(Instruction.ExponentB2, InstType.CallUnary, "exp2");
  64. Add(Instruction.FindFirstSetS32, InstType.CallUnary, "findMSB");
  65. Add(Instruction.FindFirstSetU32, InstType.CallUnary, "findMSB");
  66. Add(Instruction.Floor, InstType.CallUnary, "floor");
  67. Add(Instruction.FusedMultiplyAdd, InstType.CallTernary, "fma");
  68. Add(Instruction.GroupMemoryBarrier, InstType.CallNullary, "groupMemoryBarrier");
  69. Add(Instruction.ImageLoad, InstType.Special);
  70. Add(Instruction.ImageStore, InstType.Special);
  71. Add(Instruction.IsNan, InstType.CallUnary, "isnan");
  72. Add(Instruction.LoadAttribute, InstType.Special);
  73. Add(Instruction.LoadConstant, InstType.Special);
  74. Add(Instruction.LoadLocal, InstType.Special);
  75. Add(Instruction.LoadShared, InstType.Special);
  76. Add(Instruction.LoadStorage, InstType.Special);
  77. Add(Instruction.Lod, InstType.Special);
  78. Add(Instruction.LogarithmB2, InstType.CallUnary, "log2");
  79. Add(Instruction.LogicalAnd, InstType.OpBinaryCom, "&&", 9);
  80. Add(Instruction.LogicalExclusiveOr, InstType.OpBinaryCom, "^^", 10);
  81. Add(Instruction.LogicalNot, InstType.OpUnary, "!", 0);
  82. Add(Instruction.LogicalOr, InstType.OpBinaryCom, "||", 11);
  83. Add(Instruction.LoopBreak, InstType.OpNullary, "break");
  84. Add(Instruction.LoopContinue, InstType.OpNullary, "continue");
  85. Add(Instruction.PackDouble2x32, InstType.Special);
  86. Add(Instruction.PackHalf2x16, InstType.Special);
  87. Add(Instruction.Maximum, InstType.CallBinary, "max");
  88. Add(Instruction.MaximumU32, InstType.CallBinary, "max");
  89. Add(Instruction.MemoryBarrier, InstType.CallNullary, "memoryBarrier");
  90. Add(Instruction.Minimum, InstType.CallBinary, "min");
  91. Add(Instruction.MinimumU32, InstType.CallBinary, "min");
  92. Add(Instruction.Multiply, InstType.OpBinaryCom, "*", 1);
  93. Add(Instruction.MultiplyHighS32, InstType.CallBinary, HelperFunctionNames.MultiplyHighS32);
  94. Add(Instruction.MultiplyHighU32, InstType.CallBinary, HelperFunctionNames.MultiplyHighU32);
  95. Add(Instruction.Negate, InstType.OpUnary, "-", 0);
  96. Add(Instruction.ReciprocalSquareRoot, InstType.CallUnary, "inversesqrt");
  97. Add(Instruction.Return, InstType.OpNullary, "return");
  98. Add(Instruction.Round, InstType.CallUnary, "roundEven");
  99. Add(Instruction.ShiftLeft, InstType.OpBinary, "<<", 3);
  100. Add(Instruction.ShiftRightS32, InstType.OpBinary, ">>", 3);
  101. Add(Instruction.ShiftRightU32, InstType.OpBinary, ">>", 3);
  102. Add(Instruction.Shuffle, InstType.CallQuaternary, HelperFunctionNames.Shuffle);
  103. Add(Instruction.ShuffleDown, InstType.CallQuaternary, HelperFunctionNames.ShuffleDown);
  104. Add(Instruction.ShuffleUp, InstType.CallQuaternary, HelperFunctionNames.ShuffleUp);
  105. Add(Instruction.ShuffleXor, InstType.CallQuaternary, HelperFunctionNames.ShuffleXor);
  106. Add(Instruction.Sine, InstType.CallUnary, "sin");
  107. Add(Instruction.SquareRoot, InstType.CallUnary, "sqrt");
  108. Add(Instruction.StoreLocal, InstType.Special);
  109. Add(Instruction.StoreShared, InstType.Special);
  110. Add(Instruction.StoreStorage, InstType.Special);
  111. Add(Instruction.Subtract, InstType.OpBinary, "-", 2);
  112. Add(Instruction.SwizzleAdd, InstType.CallTernary, HelperFunctionNames.SwizzleAdd);
  113. Add(Instruction.TextureSample, InstType.Special);
  114. Add(Instruction.TextureSize, InstType.Special);
  115. Add(Instruction.Truncate, InstType.CallUnary, "trunc");
  116. Add(Instruction.UnpackDouble2x32, InstType.Special);
  117. Add(Instruction.UnpackHalf2x16, InstType.Special);
  118. Add(Instruction.VoteAll, InstType.CallUnary, "allInvocationsARB");
  119. Add(Instruction.VoteAllEqual, InstType.CallUnary, "allInvocationsEqualARB");
  120. Add(Instruction.VoteAny, InstType.CallUnary, "anyInvocationARB");
  121. }
  122. private static void Add(Instruction inst, InstType flags, string opName = null, int precedence = 0)
  123. {
  124. _infoTbl[(int)inst] = new InstInfo(flags, opName, precedence);
  125. }
  126. public static InstInfo GetInstructionInfo(Instruction inst)
  127. {
  128. return _infoTbl[(int)(inst & Instruction.Mask)];
  129. }
  130. public static string GetSoureExpr(CodeGenContext context, IAstNode node, VariableType dstType)
  131. {
  132. return ReinterpretCast(context, node, OperandManager.GetNodeDestType(context, node), dstType);
  133. }
  134. public static string Enclose(string expr, IAstNode node, Instruction pInst, bool isLhs)
  135. {
  136. InstInfo pInfo = GetInstructionInfo(pInst);
  137. return Enclose(expr, node, pInst, pInfo, isLhs);
  138. }
  139. public static string Enclose(string expr, IAstNode node, Instruction pInst, InstInfo pInfo, bool isLhs = false)
  140. {
  141. if (NeedsParenthesis(node, pInst, pInfo, isLhs))
  142. {
  143. expr = "(" + expr + ")";
  144. }
  145. return expr;
  146. }
  147. public static bool NeedsParenthesis(IAstNode node, Instruction pInst, InstInfo pInfo, bool isLhs)
  148. {
  149. // If the node isn't a operation, then it can only be a operand,
  150. // and those never needs to be surrounded in parenthesis.
  151. if (!(node is AstOperation operation))
  152. {
  153. // This is sort of a special case, if this is a negative constant,
  154. // and it is consumed by a unary operation, we need to put on the parenthesis,
  155. // as in GLSL a sequence like --2 or ~-1 is not valid.
  156. if (IsNegativeConst(node) && pInfo.Type == InstType.OpUnary)
  157. {
  158. return true;
  159. }
  160. return false;
  161. }
  162. if ((pInfo.Type & (InstType.Call | InstType.Special)) != 0)
  163. {
  164. return false;
  165. }
  166. InstInfo info = _infoTbl[(int)(operation.Inst & Instruction.Mask)];
  167. if ((info.Type & (InstType.Call | InstType.Special)) != 0)
  168. {
  169. return false;
  170. }
  171. if (info.Precedence < pInfo.Precedence)
  172. {
  173. return false;
  174. }
  175. if (info.Precedence == pInfo.Precedence && isLhs)
  176. {
  177. return false;
  178. }
  179. if (pInst == operation.Inst && info.Type == InstType.OpBinaryCom)
  180. {
  181. return false;
  182. }
  183. return true;
  184. }
  185. private static bool IsNegativeConst(IAstNode node)
  186. {
  187. if (!(node is AstOperand operand))
  188. {
  189. return false;
  190. }
  191. return operand.Type == OperandType.Constant && operand.Value < 0;
  192. }
  193. }
  194. }