EmitterContext.cs 4.2 KB

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