Translator.cs 9.3 KB

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