InstGen.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using System;
  4. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenCall;
  5. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenHelper;
  6. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenMemory;
  7. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenPacking;
  8. using static Ryujinx.Graphics.Shader.StructuredIr.InstructionInfo;
  9. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
  10. {
  11. static class InstGen
  12. {
  13. public static string GetExpression(CodeGenContext context, IAstNode node)
  14. {
  15. if (node is AstOperation operation)
  16. {
  17. return GetExpression(context, operation);
  18. }
  19. else if (node is AstOperand operand)
  20. {
  21. return context.OperandManager.GetExpression(operand, context.Config, context.CbIndexable);
  22. }
  23. throw new ArgumentException($"Invalid node type \"{node?.GetType().Name ?? "null"}\".");
  24. }
  25. private static string GetExpression(CodeGenContext context, AstOperation operation)
  26. {
  27. Instruction inst = operation.Inst;
  28. InstInfo info = GetInstructionInfo(inst);
  29. if ((info.Type & InstType.Call) != 0)
  30. {
  31. bool atomic = (info.Type & InstType.Atomic) != 0;
  32. int arity = (int)(info.Type & InstType.ArityMask);
  33. string args = string.Empty;
  34. for (int argIndex = 0; argIndex < arity; argIndex++)
  35. {
  36. if (argIndex != 0)
  37. {
  38. args += ", ";
  39. }
  40. VariableType dstType = GetSrcVarType(inst, argIndex);
  41. if (argIndex == 0 && atomic)
  42. {
  43. Instruction memRegion = inst & Instruction.MrMask;
  44. switch (memRegion)
  45. {
  46. case Instruction.MrShared: args += LoadShared (context, operation); break;
  47. case Instruction.MrStorage: args += LoadStorage(context, operation); break;
  48. default: throw new InvalidOperationException($"Invalid memory region \"{memRegion}\".");
  49. }
  50. // We use the first 2 operands above.
  51. argIndex++;
  52. }
  53. else
  54. {
  55. args += GetSoureExpr(context, operation.GetSource(argIndex), dstType);
  56. }
  57. }
  58. if (inst == Instruction.Ballot)
  59. {
  60. return $"unpackUint2x32({info.OpName}({args})).x";
  61. }
  62. else
  63. {
  64. return info.OpName + "(" + args + ")";
  65. }
  66. }
  67. else if ((info.Type & InstType.Op) != 0)
  68. {
  69. string op = info.OpName;
  70. // Return may optionally have a return value (and in this case it is unary).
  71. if (inst == Instruction.Return && operation.SourcesCount != 0)
  72. {
  73. return $"{op} {GetSoureExpr(context, operation.GetSource(0), context.CurrentFunction.ReturnType)}";
  74. }
  75. int arity = (int)(info.Type & InstType.ArityMask);
  76. string[] expr = new string[arity];
  77. for (int index = 0; index < arity; index++)
  78. {
  79. IAstNode src = operation.GetSource(index);
  80. string srcExpr = GetSoureExpr(context, src, GetSrcVarType(inst, index));
  81. bool isLhs = arity == 2 && index == 0;
  82. expr[index] = Enclose(srcExpr, src, inst, info, isLhs);
  83. }
  84. switch (arity)
  85. {
  86. case 0:
  87. return op;
  88. case 1:
  89. return op + expr[0];
  90. case 2:
  91. return $"{expr[0]} {op} {expr[1]}";
  92. case 3:
  93. return $"{expr[0]} {op[0]} {expr[1]} {op[1]} {expr[2]}";
  94. }
  95. }
  96. else if ((info.Type & InstType.Special) != 0)
  97. {
  98. switch (inst)
  99. {
  100. case Instruction.Call:
  101. return Call(context, operation);
  102. case Instruction.ImageLoad:
  103. return ImageLoadOrStore(context, operation);
  104. case Instruction.ImageStore:
  105. return ImageLoadOrStore(context, operation);
  106. case Instruction.LoadAttribute:
  107. return LoadAttribute(context, operation);
  108. case Instruction.LoadConstant:
  109. return LoadConstant(context, operation);
  110. case Instruction.LoadLocal:
  111. return LoadLocal(context, operation);
  112. case Instruction.LoadShared:
  113. return LoadShared(context, operation);
  114. case Instruction.LoadStorage:
  115. return LoadStorage(context, operation);
  116. case Instruction.Lod:
  117. return Lod(context, operation);
  118. case Instruction.PackDouble2x32:
  119. return PackDouble2x32(context, operation);
  120. case Instruction.PackHalf2x16:
  121. return PackHalf2x16(context, operation);
  122. case Instruction.StoreLocal:
  123. return StoreLocal(context, operation);
  124. case Instruction.StoreShared:
  125. return StoreShared(context, operation);
  126. case Instruction.StoreStorage:
  127. return StoreStorage(context, operation);
  128. case Instruction.TextureSample:
  129. return TextureSample(context, operation);
  130. case Instruction.TextureSize:
  131. return TextureSize(context, operation);
  132. case Instruction.UnpackDouble2x32:
  133. return UnpackDouble2x32(context, operation);
  134. case Instruction.UnpackHalf2x16:
  135. return UnpackHalf2x16(context, operation);
  136. }
  137. }
  138. throw new InvalidOperationException($"Unexpected instruction type \"{info.Type}\".");
  139. }
  140. }
  141. }