Translator.cs 11 KB

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