Translator.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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;
  7. using System.Collections.Generic;
  8. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  9. namespace Ryujinx.Graphics.Shader.Translation
  10. {
  11. public static class Translator
  12. {
  13. private const int HeaderSize = 0x50;
  14. public static ShaderProgram Translate(ulong address, IGpuAccessor gpuAccessor, TranslationFlags flags)
  15. {
  16. Operation[] ops = DecodeShader(address, gpuAccessor, flags, out ShaderConfig config, out int size);
  17. return Translate(ops, config, size);
  18. }
  19. public static ShaderProgram Translate(ulong addressA, ulong addressB, IGpuAccessor gpuAccessor, TranslationFlags flags)
  20. {
  21. Operation[] opsA = DecodeShader(addressA, gpuAccessor, flags, out _, out int sizeA);
  22. Operation[] opsB = DecodeShader(addressB, gpuAccessor, flags, out ShaderConfig config, out int sizeB);
  23. return Translate(Combine(opsA, opsB), config, sizeB, sizeA);
  24. }
  25. private static ShaderProgram Translate(Operation[] ops, ShaderConfig config, int size, int sizeA = 0)
  26. {
  27. BasicBlock[] blocks = ControlFlowGraph.MakeCfg(ops);
  28. if (blocks.Length > 0)
  29. {
  30. Dominance.FindDominators(blocks[0], blocks.Length);
  31. Dominance.FindDominanceFrontiers(blocks);
  32. Ssa.Rename(blocks);
  33. Optimizer.RunPass(blocks, config);
  34. Lowering.RunPass(blocks, config);
  35. }
  36. StructuredProgramInfo sInfo = StructuredProgram.MakeStructuredProgram(blocks, config);
  37. GlslProgram program = GlslGenerator.Generate(sInfo, config);
  38. ShaderProgramInfo spInfo = new ShaderProgramInfo(
  39. program.CBufferDescriptors,
  40. program.SBufferDescriptors,
  41. program.TextureDescriptors,
  42. program.ImageDescriptors,
  43. sInfo.UsesInstanceId);
  44. string glslCode = program.Code;
  45. return new ShaderProgram(spInfo, config.Stage, glslCode, size, sizeA);
  46. }
  47. private static Operation[] DecodeShader(
  48. ulong address,
  49. IGpuAccessor gpuAccessor,
  50. TranslationFlags flags,
  51. out ShaderConfig config,
  52. out int size)
  53. {
  54. Block[] cfg;
  55. if ((flags & TranslationFlags.Compute) != 0)
  56. {
  57. config = new ShaderConfig(gpuAccessor, flags);
  58. cfg = Decoder.Decode(gpuAccessor, address);
  59. }
  60. else
  61. {
  62. config = new ShaderConfig(new ShaderHeader(gpuAccessor, address), gpuAccessor, flags);
  63. cfg = Decoder.Decode(gpuAccessor, address + HeaderSize);
  64. }
  65. if (cfg == null)
  66. {
  67. gpuAccessor.Log("Invalid branch detected, failed to build CFG.");
  68. size = 0;
  69. return Array.Empty<Operation>();
  70. }
  71. EmitterContext context = new EmitterContext(config);
  72. ulong maxEndAddress = 0;
  73. for (int blkIndex = 0; blkIndex < cfg.Length; blkIndex++)
  74. {
  75. Block block = cfg[blkIndex];
  76. if (maxEndAddress < block.EndAddress)
  77. {
  78. maxEndAddress = block.EndAddress;
  79. }
  80. context.CurrBlock = block;
  81. context.MarkLabel(context.GetLabel(block.Address));
  82. for (int opIndex = 0; opIndex < block.OpCodes.Count; opIndex++)
  83. {
  84. OpCode op = block.OpCodes[opIndex];
  85. if ((flags & TranslationFlags.DebugMode) != 0)
  86. {
  87. string instName;
  88. if (op.Emitter != null)
  89. {
  90. instName = op.Emitter.Method.Name;
  91. }
  92. else
  93. {
  94. instName = "???";
  95. gpuAccessor.Log($"Invalid instruction at 0x{op.Address:X6} (0x{op.RawOpCode:X16}).");
  96. }
  97. string dbgComment = $"0x{op.Address:X6}: 0x{op.RawOpCode:X16} {instName}";
  98. context.Add(new CommentNode(dbgComment));
  99. }
  100. if (op.NeverExecute)
  101. {
  102. continue;
  103. }
  104. Operand predSkipLbl = null;
  105. bool skipPredicateCheck = op is OpCodeBranch opBranch && !opBranch.PushTarget;
  106. if (op is OpCodeBranchPop opBranchPop)
  107. {
  108. // If the instruction is a SYNC or BRK instruction with only one
  109. // possible target address, then the instruction is basically
  110. // just a simple branch, we can generate code similar to branch
  111. // instructions, with the condition check on the branch itself.
  112. skipPredicateCheck = opBranchPop.Targets.Count < 2;
  113. }
  114. if (!(op.Predicate.IsPT || skipPredicateCheck))
  115. {
  116. Operand label;
  117. if (opIndex == block.OpCodes.Count - 1 && block.Next != null)
  118. {
  119. label = context.GetLabel(block.Next.Address);
  120. }
  121. else
  122. {
  123. label = Label();
  124. predSkipLbl = label;
  125. }
  126. Operand pred = Register(op.Predicate);
  127. if (op.InvertPredicate)
  128. {
  129. context.BranchIfTrue(label, pred);
  130. }
  131. else
  132. {
  133. context.BranchIfFalse(label, pred);
  134. }
  135. }
  136. context.CurrOp = op;
  137. op.Emitter?.Invoke(context);
  138. if (predSkipLbl != null)
  139. {
  140. context.MarkLabel(predSkipLbl);
  141. }
  142. }
  143. }
  144. size = (int)maxEndAddress + (((flags & TranslationFlags.Compute) != 0) ? 0 : HeaderSize);
  145. return context.GetOperations();
  146. }
  147. private static Operation[] Combine(Operation[] a, Operation[] b)
  148. {
  149. // Here we combine two shaders.
  150. // For shader A:
  151. // - All user attribute stores on shader A are turned into copies to a
  152. // temporary variable. It's assumed that shader B will consume them.
  153. // - All return instructions are turned into branch instructions, the
  154. // branch target being the start of the shader B code.
  155. // For shader B:
  156. // - All user attribute loads on shader B are turned into copies from a
  157. // temporary variable, as long that attribute is written by shader A.
  158. List<Operation> output = new List<Operation>(a.Length + b.Length);
  159. Operand[] temps = new Operand[AttributeConsts.UserAttributesCount * 4];
  160. Operand lblB = Label();
  161. for (int index = 0; index < a.Length; index++)
  162. {
  163. Operation operation = a[index];
  164. if (IsUserAttribute(operation.Dest))
  165. {
  166. int tIndex = (operation.Dest.Value - AttributeConsts.UserAttributeBase) / 4;
  167. Operand temp = temps[tIndex];
  168. if (temp == null)
  169. {
  170. temp = Local();
  171. temps[tIndex] = temp;
  172. }
  173. operation.Dest = temp;
  174. }
  175. if (operation.Inst == Instruction.Return)
  176. {
  177. output.Add(new Operation(Instruction.Branch, lblB));
  178. }
  179. else
  180. {
  181. output.Add(operation);
  182. }
  183. }
  184. output.Add(new Operation(Instruction.MarkLabel, lblB));
  185. for (int index = 0; index < b.Length; index++)
  186. {
  187. Operation operation = b[index];
  188. for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
  189. {
  190. Operand src = operation.GetSource(srcIndex);
  191. if (IsUserAttribute(src))
  192. {
  193. Operand temp = temps[(src.Value - AttributeConsts.UserAttributeBase) / 4];
  194. if (temp != null)
  195. {
  196. operation.SetSource(srcIndex, temp);
  197. }
  198. }
  199. }
  200. output.Add(operation);
  201. }
  202. return output.ToArray();
  203. }
  204. private static bool IsUserAttribute(Operand operand)
  205. {
  206. return operand != null &&
  207. operand.Type == OperandType.Attribute &&
  208. operand.Value >= AttributeConsts.UserAttributeBase &&
  209. operand.Value < AttributeConsts.UserAttributeEnd;
  210. }
  211. }
  212. }