InstGenHelper.cs 11 KB

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