InstGen.cs 5.8 KB

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