Translator.cs 11 KB

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