InstGen.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // For shared memory access, the second argument is unused and should be ignored.
  37. // It is there to make both storage and shared access have the same number of arguments.
  38. // For storage, both inputs are consumed when the argument index is 0, so we should skip it here.
  39. if (argIndex == 1 && (atomic || (inst & Instruction.MrMask) == Instruction.MrShared))
  40. {
  41. continue;
  42. }
  43. if (argIndex != 0)
  44. {
  45. args += ", ";
  46. }
  47. if (argIndex == 0 && atomic)
  48. {
  49. Instruction memRegion = inst & Instruction.MrMask;
  50. switch (memRegion)
  51. {
  52. case Instruction.MrShared: args += LoadShared (context, operation); break;
  53. case Instruction.MrStorage: args += LoadStorage(context, operation); break;
  54. default: throw new InvalidOperationException($"Invalid memory region \"{memRegion}\".");
  55. }
  56. }
  57. else
  58. {
  59. VariableType dstType = GetSrcVarType(inst, argIndex);
  60. args += GetSoureExpr(context, operation.GetSource(argIndex), dstType);
  61. }
  62. }
  63. if (inst == Instruction.Ballot)
  64. {
  65. return $"unpackUint2x32({info.OpName}({args})).x";
  66. }
  67. else
  68. {
  69. return info.OpName + "(" + args + ")";
  70. }
  71. }
  72. else if ((info.Type & InstType.Op) != 0)
  73. {
  74. string op = info.OpName;
  75. // Return may optionally have a return value (and in this case it is unary).
  76. if (inst == Instruction.Return && operation.SourcesCount != 0)
  77. {
  78. return $"{op} {GetSoureExpr(context, operation.GetSource(0), context.CurrentFunction.ReturnType)}";
  79. }
  80. int arity = (int)(info.Type & InstType.ArityMask);
  81. string[] expr = new string[arity];
  82. for (int index = 0; index < arity; index++)
  83. {
  84. IAstNode src = operation.GetSource(index);
  85. string srcExpr = GetSoureExpr(context, src, GetSrcVarType(inst, index));
  86. bool isLhs = arity == 2 && index == 0;
  87. expr[index] = Enclose(srcExpr, src, inst, info, isLhs);
  88. }
  89. switch (arity)
  90. {
  91. case 0:
  92. return op;
  93. case 1:
  94. return op + expr[0];
  95. case 2:
  96. return $"{expr[0]} {op} {expr[1]}";
  97. case 3:
  98. return $"{expr[0]} {op[0]} {expr[1]} {op[1]} {expr[2]}";
  99. }
  100. }
  101. else if ((info.Type & InstType.Special) != 0)
  102. {
  103. switch (inst)
  104. {
  105. case Instruction.Call:
  106. return Call(context, operation);
  107. case Instruction.ImageLoad:
  108. return ImageLoadOrStore(context, operation);
  109. case Instruction.ImageStore:
  110. return ImageLoadOrStore(context, operation);
  111. case Instruction.LoadAttribute:
  112. return LoadAttribute(context, operation);
  113. case Instruction.LoadConstant:
  114. return LoadConstant(context, operation);
  115. case Instruction.LoadLocal:
  116. return LoadLocal(context, operation);
  117. case Instruction.LoadShared:
  118. return LoadShared(context, operation);
  119. case Instruction.LoadStorage:
  120. return LoadStorage(context, operation);
  121. case Instruction.Lod:
  122. return Lod(context, operation);
  123. case Instruction.PackDouble2x32:
  124. return PackDouble2x32(context, operation);
  125. case Instruction.PackHalf2x16:
  126. return PackHalf2x16(context, operation);
  127. case Instruction.StoreLocal:
  128. return StoreLocal(context, operation);
  129. case Instruction.StoreShared:
  130. return StoreShared(context, operation);
  131. case Instruction.StoreStorage:
  132. return StoreStorage(context, operation);
  133. case Instruction.TextureSample:
  134. return TextureSample(context, operation);
  135. case Instruction.TextureSize:
  136. return TextureSize(context, operation);
  137. case Instruction.UnpackDouble2x32:
  138. return UnpackDouble2x32(context, operation);
  139. case Instruction.UnpackHalf2x16:
  140. return UnpackHalf2x16(context, operation);
  141. }
  142. }
  143. throw new InvalidOperationException($"Unexpected instruction type \"{info.Type}\".");
  144. }
  145. }
  146. }