Translator.cs 11 KB

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