TranslatorContext.cs 4.6 KB

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