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