InstGenHelper.cs 9.4 KB

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