EmitterContext.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 void Add(Operation operation)
  31. {
  32. _operations.Add(operation);
  33. }
  34. public void FlagAttributeRead(int attribute)
  35. {
  36. if (Config.Stage == ShaderStage.Fragment)
  37. {
  38. switch (attribute)
  39. {
  40. case AttributeConsts.PositionX:
  41. case AttributeConsts.PositionY:
  42. Config.SetUsedFeature(FeatureFlags.FragCoordXY);
  43. break;
  44. }
  45. }
  46. }
  47. public void MarkLabel(Operand label)
  48. {
  49. Add(Instruction.MarkLabel, label);
  50. }
  51. public Operand GetLabel(ulong address)
  52. {
  53. if (!_labels.TryGetValue(address, out Operand label))
  54. {
  55. label = Label();
  56. _labels.Add(address, label);
  57. }
  58. return label;
  59. }
  60. public int GetFunctionId(ulong address)
  61. {
  62. return _funcs[address];
  63. }
  64. public void PrepareForReturn()
  65. {
  66. if (Config.Stage == ShaderStage.Fragment)
  67. {
  68. if (Config.OmapDepth)
  69. {
  70. Operand dest = Attribute(AttributeConsts.FragmentOutputDepth);
  71. Operand src = Register(Config.GetDepthRegister(), RegisterType.Gpr);
  72. this.Copy(dest, src);
  73. }
  74. int regIndexBase = 0;
  75. for (int rtIndex = 0; rtIndex < 8; rtIndex++)
  76. {
  77. OmapTarget target = Config.OmapTargets[rtIndex];
  78. for (int component = 0; component < 4; component++)
  79. {
  80. if (!target.ComponentEnabled(component))
  81. {
  82. continue;
  83. }
  84. int fragmentOutputColorAttr = AttributeConsts.FragmentOutputColorBase + rtIndex * 16;
  85. Operand src = Register(regIndexBase + component, RegisterType.Gpr);
  86. // Perform B <-> R swap if needed, for BGRA formats (not supported on OpenGL).
  87. if (component == 0 || component == 2)
  88. {
  89. Operand isBgra = Attribute(AttributeConsts.FragmentOutputIsBgraBase + rtIndex * 4);
  90. Operand lblIsBgra = Label();
  91. Operand lblEnd = Label();
  92. this.BranchIfTrue(lblIsBgra, isBgra);
  93. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  94. this.Branch(lblEnd);
  95. MarkLabel(lblIsBgra);
  96. this.Copy(Attribute(fragmentOutputColorAttr + (2 - component) * 4), src);
  97. MarkLabel(lblEnd);
  98. }
  99. else
  100. {
  101. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  102. }
  103. }
  104. if (target.Enabled)
  105. {
  106. regIndexBase += 4;
  107. }
  108. }
  109. }
  110. }
  111. public Operation[] GetOperations()
  112. {
  113. return _operations.ToArray();
  114. }
  115. }
  116. }