InstGen.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenBallot;
  6. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenCall;
  7. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenFSI;
  8. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenHelper;
  9. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenMemory;
  10. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenPacking;
  11. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenVector;
  12. using static Ryujinx.Graphics.Shader.StructuredIr.InstructionInfo;
  13. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
  14. {
  15. static class InstGen
  16. {
  17. public static string GetExpression(CodeGenContext context, IAstNode node)
  18. {
  19. if (node is AstOperation operation)
  20. {
  21. return GetExpression(context, operation);
  22. }
  23. else if (node is AstOperand operand)
  24. {
  25. return context.OperandManager.GetExpression(context, operand);
  26. }
  27. throw new ArgumentException($"Invalid node type \"{node?.GetType().Name ?? "null"}\".");
  28. }
  29. public static string Negate(CodeGenContext context, AstOperation operation, InstInfo info)
  30. {
  31. IAstNode src = operation.GetSource(0);
  32. AggregateType type = GetSrcVarType(operation.Inst, 0);
  33. string srcExpr = GetSoureExpr(context, src, type);
  34. string zero;
  35. if (type == AggregateType.FP64)
  36. {
  37. zero = "0.0";
  38. }
  39. else
  40. {
  41. NumberFormatter.TryFormat(0, type, out zero);
  42. }
  43. // Starting in the 496.13 NVIDIA driver, there's an issue with assigning variables to negated expressions.
  44. // (-expr) does not work, but (0.0 - expr) does. This should be removed once the issue is resolved.
  45. return $"{zero} - {Enclose(srcExpr, src, operation.Inst, info, false)}";
  46. }
  47. private static string GetExpression(CodeGenContext context, AstOperation operation)
  48. {
  49. Instruction inst = operation.Inst;
  50. InstInfo info = GetInstructionInfo(inst);
  51. if ((info.Type & InstType.Call) != 0)
  52. {
  53. bool atomic = (info.Type & InstType.Atomic) != 0;
  54. int arity = (int)(info.Type & InstType.ArityMask);
  55. string args = string.Empty;
  56. if (atomic && (operation.StorageKind == StorageKind.StorageBuffer || operation.StorageKind == StorageKind.SharedMemory))
  57. {
  58. args = GenerateLoadOrStore(context, operation, isStore: false);
  59. AggregateType dstType = operation.Inst == Instruction.AtomicMaxS32 || operation.Inst == Instruction.AtomicMinS32
  60. ? AggregateType.S32
  61. : AggregateType.U32;
  62. for (int argIndex = operation.SourcesCount - arity + 2; argIndex < operation.SourcesCount; argIndex++)
  63. {
  64. args += ", " + GetSoureExpr(context, operation.GetSource(argIndex), dstType);
  65. }
  66. }
  67. else
  68. {
  69. for (int argIndex = 0; argIndex < arity; argIndex++)
  70. {
  71. if (argIndex != 0)
  72. {
  73. args += ", ";
  74. }
  75. AggregateType dstType = GetSrcVarType(inst, argIndex);
  76. args += GetSoureExpr(context, operation.GetSource(argIndex), dstType);
  77. }
  78. }
  79. return info.OpName + '(' + args + ')';
  80. }
  81. else if ((info.Type & InstType.Op) != 0)
  82. {
  83. string op = info.OpName;
  84. // Return may optionally have a return value (and in this case it is unary).
  85. if (inst == Instruction.Return && operation.SourcesCount != 0)
  86. {
  87. return $"{op} {GetSoureExpr(context, operation.GetSource(0), context.CurrentFunction.ReturnType)}";
  88. }
  89. int arity = (int)(info.Type & InstType.ArityMask);
  90. string[] expr = new string[arity];
  91. for (int index = 0; index < arity; index++)
  92. {
  93. IAstNode src = operation.GetSource(index);
  94. string srcExpr = GetSoureExpr(context, src, GetSrcVarType(inst, index));
  95. bool isLhs = arity == 2 && index == 0;
  96. expr[index] = Enclose(srcExpr, src, inst, info, isLhs);
  97. }
  98. switch (arity)
  99. {
  100. case 0:
  101. return op;
  102. case 1:
  103. return op + expr[0];
  104. case 2:
  105. return $"{expr[0]} {op} {expr[1]}";
  106. case 3:
  107. return $"{expr[0]} {op[0]} {expr[1]} {op[1]} {expr[2]}";
  108. }
  109. }
  110. else if ((info.Type & InstType.Special) != 0)
  111. {
  112. switch (inst & Instruction.Mask)
  113. {
  114. case Instruction.Ballot:
  115. return Ballot(context, operation);
  116. case Instruction.Call:
  117. return Call(context, operation);
  118. case Instruction.FSIBegin:
  119. return FSIBegin(context);
  120. case Instruction.FSIEnd:
  121. return FSIEnd(context);
  122. case Instruction.ImageLoad:
  123. case Instruction.ImageStore:
  124. case Instruction.ImageAtomic:
  125. return ImageLoadOrStore(context, operation);
  126. case Instruction.Load:
  127. return Load(context, operation);
  128. case Instruction.Lod:
  129. return Lod(context, operation);
  130. case Instruction.Negate:
  131. return Negate(context, operation, info);
  132. case Instruction.PackDouble2x32:
  133. return PackDouble2x32(context, operation);
  134. case Instruction.PackHalf2x16:
  135. return PackHalf2x16(context, operation);
  136. case Instruction.Store:
  137. return Store(context, operation);
  138. case Instruction.TextureSample:
  139. return TextureSample(context, operation);
  140. case Instruction.TextureSize:
  141. return TextureSize(context, operation);
  142. case Instruction.UnpackDouble2x32:
  143. return UnpackDouble2x32(context, operation);
  144. case Instruction.UnpackHalf2x16:
  145. return UnpackHalf2x16(context, operation);
  146. case Instruction.VectorExtract:
  147. return VectorExtract(context, operation);
  148. }
  149. }
  150. throw new InvalidOperationException($"Unexpected instruction type \"{info.Type}\".");
  151. }
  152. }
  153. }