Translator.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using Ryujinx.Graphics.Shader.CodeGen.Glsl;
  2. using Ryujinx.Graphics.Shader.Decoders;
  3. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  4. using Ryujinx.Graphics.Shader.StructuredIr;
  5. using Ryujinx.Graphics.Shader.Translation.Optimizations;
  6. using System.Collections.Generic;
  7. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  8. namespace Ryujinx.Graphics.Shader.Translation
  9. {
  10. public static class Translator
  11. {
  12. private const int HeaderSize = 0x50;
  13. internal struct FunctionCode
  14. {
  15. public Operation[] Code { get; }
  16. public FunctionCode(Operation[] code)
  17. {
  18. Code = code;
  19. }
  20. }
  21. public static TranslatorContext CreateContext(
  22. ulong address,
  23. IGpuAccessor gpuAccessor,
  24. TranslationFlags flags,
  25. TranslationCounts counts = null)
  26. {
  27. counts ??= new TranslationCounts();
  28. Block[][] cfg = DecodeShader(address, gpuAccessor, flags, counts, out ShaderConfig config);
  29. return new TranslatorContext(address, cfg, config);
  30. }
  31. public static TranslatorContext CreateContext(
  32. ulong addressA,
  33. ulong addressB,
  34. IGpuAccessor gpuAccessor,
  35. TranslationFlags flags,
  36. TranslationCounts counts = null)
  37. {
  38. counts ??= new TranslationCounts();
  39. Block[][] cfgA = DecodeShader(addressA, gpuAccessor, flags | TranslationFlags.VertexA, counts, out ShaderConfig configA);
  40. Block[][] cfgB = DecodeShader(addressB, gpuAccessor, flags, counts, out ShaderConfig configB);
  41. return new TranslatorContext(addressA, addressB, cfgA, cfgB, configA, configB);
  42. }
  43. internal static ShaderProgram Translate(FunctionCode[] functions, ShaderConfig config, out ShaderProgramInfo shaderProgramInfo, int sizeA = 0)
  44. {
  45. var cfgs = new ControlFlowGraph[functions.Length];
  46. var frus = new RegisterUsage.FunctionRegisterUsage[functions.Length];
  47. for (int i = 0; i < functions.Length; i++)
  48. {
  49. cfgs[i] = ControlFlowGraph.Create(functions[i].Code);
  50. if (i != 0)
  51. {
  52. frus[i] = RegisterUsage.RunPass(cfgs[i]);
  53. }
  54. }
  55. Function[] funcs = new Function[functions.Length];
  56. for (int i = 0; i < functions.Length; i++)
  57. {
  58. var cfg = cfgs[i];
  59. int inArgumentsCount = 0;
  60. int outArgumentsCount = 0;
  61. if (i != 0)
  62. {
  63. var fru = frus[i];
  64. inArgumentsCount = fru.InArguments.Length;
  65. outArgumentsCount = fru.OutArguments.Length;
  66. }
  67. if (cfg.Blocks.Length != 0)
  68. {
  69. RegisterUsage.FixupCalls(cfg.Blocks, frus);
  70. Dominance.FindDominators(cfg);
  71. Dominance.FindDominanceFrontiers(cfg.Blocks);
  72. Ssa.Rename(cfg.Blocks);
  73. Optimizer.RunPass(cfg.Blocks, config);
  74. Rewriter.RunPass(cfg.Blocks, config);
  75. }
  76. funcs[i] = new Function(cfg.Blocks, $"fun{i}", false, inArgumentsCount, outArgumentsCount);
  77. }
  78. StructuredProgramInfo sInfo = StructuredProgram.MakeStructuredProgram(funcs, config);
  79. GlslProgram program = GlslGenerator.Generate(sInfo, config);
  80. shaderProgramInfo = new ShaderProgramInfo(
  81. program.CBufferDescriptors,
  82. program.SBufferDescriptors,
  83. program.TextureDescriptors,
  84. program.ImageDescriptors,
  85. sInfo.UsesInstanceId);
  86. string glslCode = program.Code;
  87. return new ShaderProgram(config.Stage, glslCode, config.Size, sizeA);
  88. }
  89. private static Block[][] DecodeShader(
  90. ulong address,
  91. IGpuAccessor gpuAccessor,
  92. TranslationFlags flags,
  93. TranslationCounts counts,
  94. out ShaderConfig config)
  95. {
  96. Block[][] cfg;
  97. ulong maxEndAddress = 0;
  98. if ((flags & TranslationFlags.Compute) != 0)
  99. {
  100. config = new ShaderConfig(gpuAccessor, flags, counts);
  101. cfg = Decoder.Decode(gpuAccessor, address);
  102. }
  103. else
  104. {
  105. config = new ShaderConfig(new ShaderHeader(gpuAccessor, address), gpuAccessor, flags, counts);
  106. cfg = Decoder.Decode(gpuAccessor, address + HeaderSize);
  107. }
  108. for (int funcIndex = 0; funcIndex < cfg.Length; funcIndex++)
  109. {
  110. for (int blkIndex = 0; blkIndex < cfg[funcIndex].Length; blkIndex++)
  111. {
  112. Block block = cfg[funcIndex][blkIndex];
  113. if (maxEndAddress < block.EndAddress)
  114. {
  115. maxEndAddress = block.EndAddress;
  116. }
  117. for (int index = 0; index < block.OpCodes.Count; index++)
  118. {
  119. if (block.OpCodes[index] is OpCodeTextureBase texture)
  120. {
  121. config.TextureHandlesForCache.Add(texture.HandleOffset);
  122. }
  123. }
  124. }
  125. }
  126. config.SizeAdd((int)maxEndAddress + (flags.HasFlag(TranslationFlags.Compute) ? 0 : HeaderSize));
  127. return cfg;
  128. }
  129. internal static FunctionCode[] EmitShader(Block[][] cfg, ShaderConfig config)
  130. {
  131. Dictionary<ulong, int> funcIds = new Dictionary<ulong, int>();
  132. for (int funcIndex = 0; funcIndex < cfg.Length; funcIndex++)
  133. {
  134. funcIds.Add(cfg[funcIndex][0].Address, funcIndex);
  135. }
  136. List<FunctionCode> funcs = new List<FunctionCode>();
  137. for (int funcIndex = 0; funcIndex < cfg.Length; funcIndex++)
  138. {
  139. EmitterContext context = new EmitterContext(config, funcIndex != 0, funcIds);
  140. for (int blkIndex = 0; blkIndex < cfg[funcIndex].Length; blkIndex++)
  141. {
  142. Block block = cfg[funcIndex][blkIndex];
  143. context.CurrBlock = block;
  144. context.MarkLabel(context.GetLabel(block.Address));
  145. EmitOps(context, block);
  146. }
  147. funcs.Add(new FunctionCode(context.GetOperations()));
  148. }
  149. return funcs.ToArray();
  150. }
  151. private static void EmitOps(EmitterContext context, Block block)
  152. {
  153. for (int opIndex = 0; opIndex < block.OpCodes.Count; opIndex++)
  154. {
  155. OpCode op = block.OpCodes[opIndex];
  156. if ((context.Config.Flags & TranslationFlags.DebugMode) != 0)
  157. {
  158. string instName;
  159. if (op.Emitter != null)
  160. {
  161. instName = op.Emitter.Method.Name;
  162. }
  163. else
  164. {
  165. instName = "???";
  166. context.Config.GpuAccessor.Log($"Invalid instruction at 0x{op.Address:X6} (0x{op.RawOpCode:X16}).");
  167. }
  168. string dbgComment = $"0x{op.Address:X6}: 0x{op.RawOpCode:X16} {instName}";
  169. context.Add(new CommentNode(dbgComment));
  170. }
  171. if (op.NeverExecute)
  172. {
  173. continue;
  174. }
  175. Operand predSkipLbl = null;
  176. bool skipPredicateCheck = op is OpCodeBranch opBranch && !opBranch.PushTarget;
  177. if (op is OpCodeBranchPop opBranchPop)
  178. {
  179. // If the instruction is a SYNC or BRK instruction with only one
  180. // possible target address, then the instruction is basically
  181. // just a simple branch, we can generate code similar to branch
  182. // instructions, with the condition check on the branch itself.
  183. skipPredicateCheck = opBranchPop.Targets.Count < 2;
  184. }
  185. if (!(op.Predicate.IsPT || skipPredicateCheck))
  186. {
  187. Operand label;
  188. if (opIndex == block.OpCodes.Count - 1 && block.Next != null)
  189. {
  190. label = context.GetLabel(block.Next.Address);
  191. }
  192. else
  193. {
  194. label = Label();
  195. predSkipLbl = label;
  196. }
  197. Operand pred = Register(op.Predicate);
  198. if (op.InvertPredicate)
  199. {
  200. context.BranchIfTrue(label, pred);
  201. }
  202. else
  203. {
  204. context.BranchIfFalse(label, pred);
  205. }
  206. }
  207. context.CurrOp = op;
  208. op.Emitter?.Invoke(context);
  209. if (predSkipLbl != null)
  210. {
  211. context.MarkLabel(predSkipLbl);
  212. }
  213. }
  214. }
  215. }
  216. }