InstGenHelper.cs 12 KB

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