Translator.cs 11 KB

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