InstGen.cs 8.8 KB

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