Translator.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using Ryujinx.Graphics.Gal;
  2. using Ryujinx.Graphics.Shader.CodeGen.Glsl;
  3. using Ryujinx.Graphics.Shader.Decoders;
  4. using Ryujinx.Graphics.Shader.Instructions;
  5. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  6. using Ryujinx.Graphics.Shader.StructuredIr;
  7. using Ryujinx.Graphics.Shader.Translation.Optimizations;
  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. public static ShaderProgram Translate(IGalMemory memory, ulong address, ShaderConfig config)
  15. {
  16. return Translate(memory, address, 0, config);
  17. }
  18. public static ShaderProgram Translate(
  19. IGalMemory memory,
  20. ulong address,
  21. ulong addressB,
  22. ShaderConfig config)
  23. {
  24. Operation[] shaderOps = DecodeShader(memory, address, config.Type);
  25. if (addressB != 0)
  26. {
  27. // Dual vertex shader.
  28. Operation[] shaderOpsB = DecodeShader(memory, addressB, config.Type);
  29. shaderOps = Combine(shaderOps, shaderOpsB);
  30. }
  31. BasicBlock[] irBlocks = ControlFlowGraph.MakeCfg(shaderOps);
  32. Dominance.FindDominators(irBlocks[0], irBlocks.Length);
  33. Dominance.FindDominanceFrontiers(irBlocks);
  34. Ssa.Rename(irBlocks);
  35. Optimizer.Optimize(irBlocks);
  36. StructuredProgramInfo sInfo = StructuredProgram.MakeStructuredProgram(irBlocks);
  37. GlslProgram program = GlslGenerator.Generate(sInfo, config);
  38. ShaderProgramInfo spInfo = new ShaderProgramInfo(
  39. program.CBufferDescriptors,
  40. program.TextureDescriptors);
  41. return new ShaderProgram(spInfo, program.Code);
  42. }
  43. private static Operation[] DecodeShader(IGalMemory memory, ulong address, GalShaderType shaderType)
  44. {
  45. ShaderHeader header = new ShaderHeader(memory, address);
  46. Block[] cfg = Decoder.Decode(memory, address);
  47. EmitterContext context = new EmitterContext(shaderType, header);
  48. for (int blkIndex = 0; blkIndex < cfg.Length; blkIndex++)
  49. {
  50. Block block = cfg[blkIndex];
  51. context.CurrBlock = block;
  52. context.MarkLabel(context.GetLabel(block.Address));
  53. for (int opIndex = 0; opIndex < block.OpCodes.Count; opIndex++)
  54. {
  55. OpCode op = block.OpCodes[opIndex];
  56. if (op.NeverExecute)
  57. {
  58. continue;
  59. }
  60. Operand predSkipLbl = null;
  61. bool skipPredicateCheck = op.Emitter == InstEmit.Bra;
  62. if (op is OpCodeSync opSync)
  63. {
  64. // If the instruction is a SYNC instruction with only one
  65. // possible target address, then the instruction is basically
  66. // just a simple branch, we can generate code similar to branch
  67. // instructions, with the condition check on the branch itself.
  68. skipPredicateCheck |= opSync.Targets.Count < 2;
  69. }
  70. if (!(op.Predicate.IsPT || skipPredicateCheck))
  71. {
  72. Operand label;
  73. if (opIndex == block.OpCodes.Count - 1 && block.Next != null)
  74. {
  75. label = context.GetLabel(block.Next.Address);
  76. }
  77. else
  78. {
  79. label = Label();
  80. predSkipLbl = label;
  81. }
  82. Operand pred = Register(op.Predicate);
  83. if (op.InvertPredicate)
  84. {
  85. context.BranchIfTrue(label, pred);
  86. }
  87. else
  88. {
  89. context.BranchIfFalse(label, pred);
  90. }
  91. }
  92. context.CurrOp = op;
  93. op.Emitter(context);
  94. if (predSkipLbl != null)
  95. {
  96. context.MarkLabel(predSkipLbl);
  97. }
  98. }
  99. }
  100. return context.GetOperations();
  101. }
  102. private static Operation[] Combine(Operation[] a, Operation[] b)
  103. {
  104. // Here we combine two shaders.
  105. // For shader A:
  106. // - All user attribute stores on shader A are turned into copies to a
  107. // temporary variable. It's assumed that shader B will consume them.
  108. // - All return instructions are turned into branch instructions, the
  109. // branch target being the start of the shader B code.
  110. // For shader B:
  111. // - All user attribute loads on shader B are turned into copies from a
  112. // temporary variable, as long that attribute is written by shader A.
  113. List<Operation> output = new List<Operation>(a.Length + b.Length);
  114. Operand[] temps = new Operand[AttributeConsts.UserAttributesCount * 4];
  115. Operand lblB = Label();
  116. for (int index = 0; index < a.Length; index++)
  117. {
  118. Operation operation = a[index];
  119. if (IsUserAttribute(operation.Dest))
  120. {
  121. int tIndex = (operation.Dest.Value - AttributeConsts.UserAttributeBase) / 4;
  122. Operand temp = temps[tIndex];
  123. if (temp == null)
  124. {
  125. temp = Local();
  126. temps[tIndex] = temp;
  127. }
  128. operation.Dest = temp;
  129. }
  130. if (operation.Inst == Instruction.Return)
  131. {
  132. output.Add(new Operation(Instruction.Branch, lblB));
  133. }
  134. else
  135. {
  136. output.Add(operation);
  137. }
  138. }
  139. output.Add(new Operation(Instruction.MarkLabel, lblB));
  140. for (int index = 0; index < b.Length; index++)
  141. {
  142. Operation operation = b[index];
  143. for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
  144. {
  145. Operand src = operation.GetSource(srcIndex);
  146. if (IsUserAttribute(src))
  147. {
  148. Operand temp = temps[(src.Value - AttributeConsts.UserAttributeBase) / 4];
  149. if (temp != null)
  150. {
  151. operation.SetSource(srcIndex, temp);
  152. }
  153. }
  154. }
  155. output.Add(operation);
  156. }
  157. return output.ToArray();
  158. }
  159. private static bool IsUserAttribute(Operand operand)
  160. {
  161. return operand != null &&
  162. operand.Type == OperandType.Attribute &&
  163. operand.Value >= AttributeConsts.UserAttributeBase &&
  164. operand.Value < AttributeConsts.UserAttributeEnd;
  165. }
  166. }
  167. }