TranslatorContext.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  6. using static Ryujinx.Graphics.Shader.Translation.Translator;
  7. namespace Ryujinx.Graphics.Shader.Translation
  8. {
  9. public class TranslatorContext
  10. {
  11. private readonly DecodedProgram _program;
  12. private ShaderConfig _config;
  13. public ulong Address { get; }
  14. public ShaderStage Stage => _config.Stage;
  15. public int Size => _config.Size;
  16. public int Cb1DataSize => _config.Cb1DataSize;
  17. public IGpuAccessor GpuAccessor => _config.GpuAccessor;
  18. internal TranslatorContext(ulong address, DecodedProgram program, ShaderConfig config)
  19. {
  20. Address = address;
  21. _program = program;
  22. _config = config;
  23. }
  24. private static bool IsUserAttribute(Operand operand)
  25. {
  26. if (operand != null && operand.Type.IsAttribute())
  27. {
  28. int value = operand.Value & AttributeConsts.Mask;
  29. return value >= AttributeConsts.UserAttributeBase && value < AttributeConsts.UserAttributeEnd;
  30. }
  31. return false;
  32. }
  33. private static FunctionCode[] Combine(FunctionCode[] a, FunctionCode[] b, int aStart)
  34. {
  35. // Here we combine two shaders.
  36. // For shader A:
  37. // - All user attribute stores on shader A are turned into copies to a
  38. // temporary variable. It's assumed that shader B will consume them.
  39. // - All return instructions are turned into branch instructions, the
  40. // branch target being the start of the shader B code.
  41. // For shader B:
  42. // - All user attribute loads on shader B are turned into copies from a
  43. // temporary variable, as long that attribute is written by shader A.
  44. FunctionCode[] output = new FunctionCode[a.Length + b.Length - 1];
  45. List<Operation> ops = new List<Operation>(a.Length + b.Length);
  46. Operand[] temps = new Operand[AttributeConsts.UserAttributesCount * 4];
  47. Operand lblB = Label();
  48. for (int index = aStart; index < a[0].Code.Length; index++)
  49. {
  50. Operation operation = a[0].Code[index];
  51. if (IsUserAttribute(operation.Dest))
  52. {
  53. int tIndex = (operation.Dest.Value - AttributeConsts.UserAttributeBase) / 4;
  54. Operand temp = temps[tIndex];
  55. if (temp == null)
  56. {
  57. temp = Local();
  58. temps[tIndex] = temp;
  59. }
  60. operation.Dest = temp;
  61. }
  62. if (operation.Inst == Instruction.Return)
  63. {
  64. ops.Add(new Operation(Instruction.Branch, lblB));
  65. }
  66. else
  67. {
  68. ops.Add(operation);
  69. }
  70. }
  71. ops.Add(new Operation(Instruction.MarkLabel, lblB));
  72. for (int index = 0; index < b[0].Code.Length; index++)
  73. {
  74. Operation operation = b[0].Code[index];
  75. for (int srcIndex = 0; srcIndex < operation.SourcesCount; srcIndex++)
  76. {
  77. Operand src = operation.GetSource(srcIndex);
  78. if (IsUserAttribute(src))
  79. {
  80. Operand temp = temps[(src.Value - AttributeConsts.UserAttributeBase) / 4];
  81. if (temp != null)
  82. {
  83. operation.SetSource(srcIndex, temp);
  84. }
  85. }
  86. }
  87. ops.Add(operation);
  88. }
  89. output[0] = new FunctionCode(ops.ToArray());
  90. for (int i = 1; i < a.Length; i++)
  91. {
  92. output[i] = a[i];
  93. }
  94. for (int i = 1; i < b.Length; i++)
  95. {
  96. output[a.Length + i - 1] = b[i];
  97. }
  98. return output;
  99. }
  100. public void SetNextStage(TranslatorContext nextStage)
  101. {
  102. _config.MergeFromtNextStage(nextStage._config);
  103. }
  104. public ShaderProgram Translate(TranslatorContext other = null)
  105. {
  106. FunctionCode[] code = EmitShader(_program, _config, initializeOutputs: other == null, out _);
  107. if (other != null)
  108. {
  109. other._config.MergeOutputUserAttributes(_config.UsedOutputAttributes, Enumerable.Empty<int>());
  110. FunctionCode[] otherCode = EmitShader(other._program, other._config, initializeOutputs: true, out int aStart);
  111. code = Combine(otherCode, code, aStart);
  112. _config.InheritFrom(other._config);
  113. }
  114. return Translator.Translate(code, _config);
  115. }
  116. }
  117. }