Translator.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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, TranslationConfig translationConfig)
  42. {
  43. bool compute = (translationConfig.Flags & TranslationFlags.Compute) != 0;
  44. bool debugMode = (translationConfig.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. translationConfig.Flags,
  70. translationConfig.MaxCBufferSize,
  71. maxOutputVertexCount,
  72. outputTopology);
  73. return Translate(ops, config, size);
  74. }
  75. public static ShaderProgram Translate(Span<byte> vpACode, Span<byte> vpBCode, TranslationConfig translationConfig)
  76. {
  77. bool debugMode = (translationConfig.Flags & TranslationFlags.DebugMode) != 0;
  78. Operation[] vpAOps = DecodeShader(vpACode, compute: false, debugMode, out _, out _);
  79. Operation[] vpBOps = DecodeShader(vpBCode, compute: false, debugMode, out ShaderHeader header, out int sizeB);
  80. ShaderConfig config = new ShaderConfig(
  81. header.Stage,
  82. translationConfig.Flags,
  83. translationConfig.MaxCBufferSize,
  84. header.MaxOutputVertexCount,
  85. header.OutputTopology);
  86. return Translate(Combine(vpAOps, vpBOps), config, sizeB);
  87. }
  88. private static ShaderProgram Translate(Operation[] ops, ShaderConfig config, int size)
  89. {
  90. BasicBlock[] irBlocks = ControlFlowGraph.MakeCfg(ops);
  91. if (irBlocks.Length > 0)
  92. {
  93. Dominance.FindDominators(irBlocks[0], irBlocks.Length);
  94. Dominance.FindDominanceFrontiers(irBlocks);
  95. Ssa.Rename(irBlocks);
  96. Optimizer.Optimize(irBlocks, config.Stage);
  97. }
  98. StructuredProgramInfo sInfo = StructuredProgram.MakeStructuredProgram(irBlocks, 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.Emitter == InstEmit.Bra;
  170. if (op is OpCodeBranchPop opBranchPop)
  171. {
  172. // If the instruction is a SYNC 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. }