Translator.cs 11 KB

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