Translator.cs 9.7 KB

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