Translator.cs 10 KB

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