InstGen.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. for (int argIndex = 0; argIndex < arity; argIndex++)
  57. {
  58. // For shared memory access, the second argument is unused and should be ignored.
  59. // It is there to make both storage and shared access have the same number of arguments.
  60. // For storage, both inputs are consumed when the argument index is 0, so we should skip it here.
  61. if (argIndex == 1 && (atomic || (inst & Instruction.MrMask) == Instruction.MrShared))
  62. {
  63. continue;
  64. }
  65. if (argIndex != 0)
  66. {
  67. args += ", ";
  68. }
  69. if (argIndex == 0 && atomic)
  70. {
  71. Instruction memRegion = inst & Instruction.MrMask;
  72. switch (memRegion)
  73. {
  74. case Instruction.MrShared: args += LoadShared(context, operation); break;
  75. case Instruction.MrStorage: args += LoadStorage(context, operation); break;
  76. default: throw new InvalidOperationException($"Invalid memory region \"{memRegion}\".");
  77. }
  78. }
  79. else
  80. {
  81. AggregateType dstType = GetSrcVarType(inst, argIndex);
  82. args += GetSoureExpr(context, operation.GetSource(argIndex), dstType);
  83. }
  84. }
  85. return info.OpName + '(' + args + ')';
  86. }
  87. else if ((info.Type & InstType.Op) != 0)
  88. {
  89. string op = info.OpName;
  90. // Return may optionally have a return value (and in this case it is unary).
  91. if (inst == Instruction.Return && operation.SourcesCount != 0)
  92. {
  93. return $"{op} {GetSoureExpr(context, operation.GetSource(0), context.CurrentFunction.ReturnType)}";
  94. }
  95. int arity = (int)(info.Type & InstType.ArityMask);
  96. string[] expr = new string[arity];
  97. for (int index = 0; index < arity; index++)
  98. {
  99. IAstNode src = operation.GetSource(index);
  100. string srcExpr = GetSoureExpr(context, src, GetSrcVarType(inst, index));
  101. bool isLhs = arity == 2 && index == 0;
  102. expr[index] = Enclose(srcExpr, src, inst, info, isLhs);
  103. }
  104. switch (arity)
  105. {
  106. case 0:
  107. return op;
  108. case 1:
  109. return op + expr[0];
  110. case 2:
  111. return $"{expr[0]} {op} {expr[1]}";
  112. case 3:
  113. return $"{expr[0]} {op[0]} {expr[1]} {op[1]} {expr[2]}";
  114. }
  115. }
  116. else if ((info.Type & InstType.Special) != 0)
  117. {
  118. switch (inst & Instruction.Mask)
  119. {
  120. case Instruction.Ballot:
  121. return Ballot(context, operation);
  122. case Instruction.Call:
  123. return Call(context, operation);
  124. case Instruction.FSIBegin:
  125. return FSIBegin(context);
  126. case Instruction.FSIEnd:
  127. return FSIEnd(context);
  128. case Instruction.ImageLoad:
  129. case Instruction.ImageStore:
  130. case Instruction.ImageAtomic:
  131. return ImageLoadOrStore(context, operation);
  132. case Instruction.LoadAttribute:
  133. return LoadAttribute(context, operation);
  134. case Instruction.LoadConstant:
  135. return LoadConstant(context, operation);
  136. case Instruction.LoadLocal:
  137. return LoadLocal(context, operation);
  138. case Instruction.LoadShared:
  139. return LoadShared(context, operation);
  140. case Instruction.LoadStorage:
  141. return LoadStorage(context, operation);
  142. case Instruction.Lod:
  143. return Lod(context, operation);
  144. case Instruction.Negate:
  145. return Negate(context, operation, info);
  146. case Instruction.PackDouble2x32:
  147. return PackDouble2x32(context, operation);
  148. case Instruction.PackHalf2x16:
  149. return PackHalf2x16(context, operation);
  150. case Instruction.StoreAttribute:
  151. return StoreAttribute(context, operation);
  152. case Instruction.StoreLocal:
  153. return StoreLocal(context, operation);
  154. case Instruction.StoreShared:
  155. return StoreShared(context, operation);
  156. case Instruction.StoreShared16:
  157. return StoreShared16(context, operation);
  158. case Instruction.StoreShared8:
  159. return StoreShared8(context, operation);
  160. case Instruction.StoreStorage:
  161. return StoreStorage(context, operation);
  162. case Instruction.StoreStorage16:
  163. return StoreStorage16(context, operation);
  164. case Instruction.StoreStorage8:
  165. return StoreStorage8(context, operation);
  166. case Instruction.TextureSample:
  167. return TextureSample(context, operation);
  168. case Instruction.TextureSize:
  169. return TextureSize(context, operation);
  170. case Instruction.UnpackDouble2x32:
  171. return UnpackDouble2x32(context, operation);
  172. case Instruction.UnpackHalf2x16:
  173. return UnpackHalf2x16(context, operation);
  174. case Instruction.VectorExtract:
  175. return VectorExtract(context, operation);
  176. }
  177. }
  178. throw new InvalidOperationException($"Unexpected instruction type \"{info.Type}\".");
  179. }
  180. }
  181. }