EmitterContext.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. namespace Ryujinx.Graphics.Shader.Translation
  6. {
  7. class EmitterContext
  8. {
  9. public Block CurrBlock { get; set; }
  10. public OpCode CurrOp { get; set; }
  11. public ShaderConfig Config { get; }
  12. public bool IsNonMain { get; }
  13. private readonly IReadOnlyDictionary<ulong, int> _funcs;
  14. private readonly List<Operation> _operations;
  15. private readonly Dictionary<ulong, Operand> _labels;
  16. public EmitterContext(ShaderConfig config, bool isNonMain, IReadOnlyDictionary<ulong, int> funcs)
  17. {
  18. Config = config;
  19. IsNonMain = isNonMain;
  20. _funcs = funcs;
  21. _operations = new List<Operation>();
  22. _labels = new Dictionary<ulong, Operand>();
  23. }
  24. public Operand Add(Instruction inst, Operand dest = null, params Operand[] sources)
  25. {
  26. Operation operation = new Operation(inst, dest, sources);
  27. Add(operation);
  28. return dest;
  29. }
  30. public (Operand, Operand) Add(Instruction inst, (Operand, Operand) dest, params Operand[] sources)
  31. {
  32. Operand[] dests = new[] { dest.Item1, dest.Item2 };
  33. Operation operation = new Operation(inst, 0, dests, sources);
  34. Add(operation);
  35. return dest;
  36. }
  37. public void Add(Operation operation)
  38. {
  39. _operations.Add(operation);
  40. }
  41. public void FlagAttributeRead(int attribute)
  42. {
  43. if (Config.Stage == ShaderStage.Fragment)
  44. {
  45. switch (attribute)
  46. {
  47. case AttributeConsts.PositionX:
  48. case AttributeConsts.PositionY:
  49. Config.SetUsedFeature(FeatureFlags.FragCoordXY);
  50. break;
  51. }
  52. }
  53. }
  54. public void MarkLabel(Operand label)
  55. {
  56. Add(Instruction.MarkLabel, label);
  57. }
  58. public Operand GetLabel(ulong address)
  59. {
  60. if (!_labels.TryGetValue(address, out Operand label))
  61. {
  62. label = Label();
  63. _labels.Add(address, label);
  64. }
  65. return label;
  66. }
  67. public int GetFunctionId(ulong address)
  68. {
  69. return _funcs[address];
  70. }
  71. public void PrepareForReturn()
  72. {
  73. if (!IsNonMain && Config.Stage == ShaderStage.Fragment)
  74. {
  75. if (Config.OmapDepth)
  76. {
  77. Operand dest = Attribute(AttributeConsts.FragmentOutputDepth);
  78. Operand src = Register(Config.GetDepthRegister(), RegisterType.Gpr);
  79. this.Copy(dest, src);
  80. }
  81. int regIndexBase = 0;
  82. for (int rtIndex = 0; rtIndex < 8; rtIndex++)
  83. {
  84. OmapTarget target = Config.OmapTargets[rtIndex];
  85. for (int component = 0; component < 4; component++)
  86. {
  87. if (!target.ComponentEnabled(component))
  88. {
  89. continue;
  90. }
  91. int fragmentOutputColorAttr = AttributeConsts.FragmentOutputColorBase + rtIndex * 16;
  92. Operand src = Register(regIndexBase + component, RegisterType.Gpr);
  93. // Perform B <-> R swap if needed, for BGRA formats (not supported on OpenGL).
  94. if (component == 0 || component == 2)
  95. {
  96. Operand isBgra = Attribute(AttributeConsts.FragmentOutputIsBgraBase + rtIndex * 4);
  97. Operand lblIsBgra = Label();
  98. Operand lblEnd = Label();
  99. this.BranchIfTrue(lblIsBgra, isBgra);
  100. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  101. this.Branch(lblEnd);
  102. MarkLabel(lblIsBgra);
  103. this.Copy(Attribute(fragmentOutputColorAttr + (2 - component) * 4), src);
  104. MarkLabel(lblEnd);
  105. }
  106. else
  107. {
  108. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  109. }
  110. }
  111. if (target.Enabled)
  112. {
  113. regIndexBase += 4;
  114. }
  115. }
  116. }
  117. }
  118. public Operation[] GetOperations()
  119. {
  120. return _operations.ToArray();
  121. }
  122. }
  123. }