Translator.cs 11 KB

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