TranslatorContext.cs 4.7 KB

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