InstGenHelper.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.ImageLoad, InstType.Special);
  48. Add(Instruction.ImageStore, InstType.Special);
  49. Add(Instruction.IsNan, InstType.CallUnary, "isnan");
  50. Add(Instruction.LoadAttribute, InstType.Special);
  51. Add(Instruction.LoadConstant, InstType.Special);
  52. Add(Instruction.LoadLocal, InstType.Special);
  53. Add(Instruction.LoadStorage, InstType.Special);
  54. Add(Instruction.LogarithmB2, InstType.CallUnary, "log2");
  55. Add(Instruction.LogicalAnd, InstType.OpBinaryCom, "&&", 9);
  56. Add(Instruction.LogicalExclusiveOr, InstType.OpBinaryCom, "^^", 10);
  57. Add(Instruction.LogicalNot, InstType.OpUnary, "!", 0);
  58. Add(Instruction.LogicalOr, InstType.OpBinaryCom, "||", 11);
  59. Add(Instruction.LoopBreak, InstType.OpNullary, "break");
  60. Add(Instruction.LoopContinue, InstType.OpNullary, "continue");
  61. Add(Instruction.PackHalf2x16, InstType.Special);
  62. Add(Instruction.ShiftLeft, InstType.OpBinary, "<<", 3);
  63. Add(Instruction.ShiftRightS32, InstType.OpBinary, ">>", 3);
  64. Add(Instruction.ShiftRightU32, InstType.OpBinary, ">>", 3);
  65. Add(Instruction.Maximum, InstType.CallBinary, "max");
  66. Add(Instruction.MaximumU32, InstType.CallBinary, "max");
  67. Add(Instruction.Minimum, InstType.CallBinary, "min");
  68. Add(Instruction.MinimumU32, InstType.CallBinary, "min");
  69. Add(Instruction.Multiply, InstType.OpBinaryCom, "*", 1);
  70. Add(Instruction.Negate, InstType.OpUnary, "-", 0);
  71. Add(Instruction.ReciprocalSquareRoot, InstType.CallUnary, "inversesqrt");
  72. Add(Instruction.Return, InstType.OpNullary, "return");
  73. Add(Instruction.Round, InstType.CallUnary, "roundEven");
  74. Add(Instruction.Sine, InstType.CallUnary, "sin");
  75. Add(Instruction.SquareRoot, InstType.CallUnary, "sqrt");
  76. Add(Instruction.StoreLocal, InstType.Special);
  77. Add(Instruction.StoreStorage, InstType.Special);
  78. Add(Instruction.Subtract, InstType.OpBinary, "-", 2);
  79. Add(Instruction.TextureSample, InstType.Special);
  80. Add(Instruction.TextureSize, InstType.Special);
  81. Add(Instruction.Truncate, InstType.CallUnary, "trunc");
  82. Add(Instruction.UnpackHalf2x16, InstType.Special);
  83. }
  84. private static void Add(Instruction inst, InstType flags, string opName = null, int precedence = 0)
  85. {
  86. _infoTbl[(int)inst] = new InstInfo(flags, opName, precedence);
  87. }
  88. public static InstInfo GetInstructionInfo(Instruction inst)
  89. {
  90. return _infoTbl[(int)(inst & Instruction.Mask)];
  91. }
  92. public static string GetSoureExpr(CodeGenContext context, IAstNode node, VariableType dstType)
  93. {
  94. return ReinterpretCast(context, node, OperandManager.GetNodeDestType(node), dstType);
  95. }
  96. public static string Enclose(string expr, IAstNode node, Instruction pInst, bool isLhs)
  97. {
  98. InstInfo pInfo = GetInstructionInfo(pInst);
  99. return Enclose(expr, node, pInst, pInfo, isLhs);
  100. }
  101. public static string Enclose(string expr, IAstNode node, Instruction pInst, InstInfo pInfo, bool isLhs = false)
  102. {
  103. if (NeedsParenthesis(node, pInst, pInfo, isLhs))
  104. {
  105. expr = "(" + expr + ")";
  106. }
  107. return expr;
  108. }
  109. public static bool NeedsParenthesis(IAstNode node, Instruction pInst, InstInfo pInfo, bool isLhs)
  110. {
  111. // If the node isn't a operation, then it can only be a operand,
  112. // and those never needs to be surrounded in parenthesis.
  113. if (!(node is AstOperation operation))
  114. {
  115. // This is sort of a special case, if this is a negative constant,
  116. // and it is consumed by a unary operation, we need to put on the parenthesis,
  117. // as in GLSL a sequence like --2 or ~-1 is not valid.
  118. if (IsNegativeConst(node) && pInfo.Type == InstType.OpUnary)
  119. {
  120. return true;
  121. }
  122. return false;
  123. }
  124. if ((pInfo.Type & (InstType.Call | InstType.Special)) != 0)
  125. {
  126. return false;
  127. }
  128. InstInfo info = _infoTbl[(int)(operation.Inst & Instruction.Mask)];
  129. if ((info.Type & (InstType.Call | InstType.Special)) != 0)
  130. {
  131. return false;
  132. }
  133. if (info.Precedence < pInfo.Precedence)
  134. {
  135. return false;
  136. }
  137. if (info.Precedence == pInfo.Precedence && isLhs)
  138. {
  139. return false;
  140. }
  141. if (pInst == operation.Inst && info.Type == InstType.OpBinaryCom)
  142. {
  143. return false;
  144. }
  145. return true;
  146. }
  147. private static bool IsNegativeConst(IAstNode node)
  148. {
  149. if (!(node is AstOperand operand))
  150. {
  151. return false;
  152. }
  153. return operand.Type == OperandType.Constant && operand.Value < 0;
  154. }
  155. }
  156. }