InstGen.cs 8.3 KB

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