Translator.cs 9.8 KB

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