InstGen.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using System;
  4. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenBallot;
  5. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenCall;
  6. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenFSI;
  7. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenHelper;
  8. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenMemory;
  9. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenPacking;
  10. using static Ryujinx.Graphics.Shader.StructuredIr.InstructionInfo;
  11. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
  12. {
  13. static class InstGen
  14. {
  15. public static string GetExpression(CodeGenContext context, IAstNode node)
  16. {
  17. if (node is AstOperation operation)
  18. {
  19. return GetExpression(context, operation);
  20. }
  21. else if (node is AstOperand operand)
  22. {
  23. return context.OperandManager.GetExpression(operand, context.Config);
  24. }
  25. throw new ArgumentException($"Invalid node type \"{node?.GetType().Name ?? "null"}\".");
  26. }
  27. public static string Negate(CodeGenContext context, AstOperation operation, InstInfo info)
  28. {
  29. IAstNode src = operation.GetSource(0);
  30. VariableType type = GetSrcVarType(operation.Inst, 0);
  31. string srcExpr = GetSoureExpr(context, src, type);
  32. NumberFormatter.TryFormat(0, type, out string zero);
  33. // Starting in the 496.13 NVIDIA driver, there's an issue with assigning variables to negated expressions.
  34. // (-expr) does not work, but (0.0 - expr) does. This should be removed once the issue is resolved.
  35. return $"{zero} - {Enclose(srcExpr, src, operation.Inst, info, false)}";
  36. }
  37. private static string GetExpression(CodeGenContext context, AstOperation operation)
  38. {
  39. Instruction inst = operation.Inst;
  40. InstInfo info = GetInstructionInfo(inst);
  41. if ((info.Type & InstType.Call) != 0)
  42. {
  43. bool atomic = (info.Type & InstType.Atomic) != 0;
  44. int arity = (int)(info.Type & InstType.ArityMask);
  45. string args = string.Empty;
  46. for (int argIndex = 0; argIndex < arity; argIndex++)
  47. {
  48. // For shared memory access, the second argument is unused and should be ignored.
  49. // It is there to make both storage and shared access have the same number of arguments.
  50. // For storage, both inputs are consumed when the argument index is 0, so we should skip it here.
  51. if (argIndex == 1 && (atomic || (inst & Instruction.MrMask) == Instruction.MrShared))
  52. {
  53. continue;
  54. }
  55. if (argIndex != 0)
  56. {
  57. args += ", ";
  58. }
  59. if (argIndex == 0 && atomic)
  60. {
  61. Instruction memRegion = inst & Instruction.MrMask;
  62. switch (memRegion)
  63. {
  64. case Instruction.MrShared: args += LoadShared(context, operation); break;
  65. case Instruction.MrStorage: args += LoadStorage(context, operation); break;
  66. default: throw new InvalidOperationException($"Invalid memory region \"{memRegion}\".");
  67. }
  68. }
  69. else
  70. {
  71. VariableType dstType = GetSrcVarType(inst, argIndex);
  72. args += GetSoureExpr(context, operation.GetSource(argIndex), dstType);
  73. }
  74. }
  75. return info.OpName + '(' + args + ')';
  76. }
  77. else if ((info.Type & InstType.Op) != 0)
  78. {
  79. string op = info.OpName;
  80. // Return may optionally have a return value (and in this case it is unary).
  81. if (inst == Instruction.Return && operation.SourcesCount != 0)
  82. {
  83. return $"{op} {GetSoureExpr(context, operation.GetSource(0), context.CurrentFunction.ReturnType)}";
  84. }
  85. int arity = (int)(info.Type & InstType.ArityMask);
  86. string[] expr = new string[arity];
  87. for (int index = 0; index < arity; index++)
  88. {
  89. IAstNode src = operation.GetSource(index);
  90. string srcExpr = GetSoureExpr(context, src, GetSrcVarType(inst, index));
  91. bool isLhs = arity == 2 && index == 0;
  92. expr[index] = Enclose(srcExpr, src, inst, info, isLhs);
  93. }
  94. switch (arity)
  95. {
  96. case 0:
  97. return op;
  98. case 1:
  99. return op + expr[0];
  100. case 2:
  101. return $"{expr[0]} {op} {expr[1]}";
  102. case 3:
  103. return $"{expr[0]} {op[0]} {expr[1]} {op[1]} {expr[2]}";
  104. }
  105. }
  106. else if ((info.Type & InstType.Special) != 0)
  107. {
  108. switch (inst & Instruction.Mask)
  109. {
  110. case Instruction.Ballot:
  111. return Ballot(context, operation);
  112. case Instruction.Call:
  113. return Call(context, operation);
  114. case Instruction.FSIBegin:
  115. return FSIBegin(context);
  116. case Instruction.FSIEnd:
  117. return FSIEnd(context);
  118. case Instruction.ImageLoad:
  119. case Instruction.ImageStore:
  120. case Instruction.ImageAtomic:
  121. return ImageLoadOrStore(context, operation);
  122. case Instruction.LoadAttribute:
  123. return LoadAttribute(context, operation);
  124. case Instruction.LoadConstant:
  125. return LoadConstant(context, operation);
  126. case Instruction.LoadLocal:
  127. return LoadLocal(context, operation);
  128. case Instruction.LoadShared:
  129. return LoadShared(context, operation);
  130. case Instruction.LoadStorage:
  131. return LoadStorage(context, operation);
  132. case Instruction.Lod:
  133. return Lod(context, operation);
  134. case Instruction.Negate:
  135. return Negate(context, operation, info);
  136. case Instruction.PackDouble2x32:
  137. return PackDouble2x32(context, operation);
  138. case Instruction.PackHalf2x16:
  139. return PackHalf2x16(context, operation);
  140. case Instruction.StoreAttribute:
  141. return StoreAttribute(context, operation);
  142. case Instruction.StoreLocal:
  143. return StoreLocal(context, operation);
  144. case Instruction.StoreShared:
  145. return StoreShared(context, operation);
  146. case Instruction.StoreShared16:
  147. return StoreShared16(context, operation);
  148. case Instruction.StoreShared8:
  149. return StoreShared8(context, operation);
  150. case Instruction.StoreStorage:
  151. return StoreStorage(context, operation);
  152. case Instruction.StoreStorage16:
  153. return StoreStorage16(context, operation);
  154. case Instruction.StoreStorage8:
  155. return StoreStorage8(context, operation);
  156. case Instruction.TextureSample:
  157. return TextureSample(context, operation);
  158. case Instruction.TextureSize:
  159. return TextureSize(context, operation);
  160. case Instruction.UnpackDouble2x32:
  161. return UnpackDouble2x32(context, operation);
  162. case Instruction.UnpackHalf2x16:
  163. return UnpackHalf2x16(context, operation);
  164. }
  165. }
  166. throw new InvalidOperationException($"Unexpected instruction type \"{info.Type}\".");
  167. }
  168. }
  169. }