EmitterContext.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.Vertex && attribute == AttributeConsts.InstanceId)
  44. {
  45. Config.SetUsedFeature(FeatureFlags.InstanceId);
  46. }
  47. else if (Config.Stage == ShaderStage.Fragment)
  48. {
  49. switch (attribute)
  50. {
  51. case AttributeConsts.PositionX:
  52. case AttributeConsts.PositionY:
  53. Config.SetUsedFeature(FeatureFlags.FragCoordXY);
  54. break;
  55. }
  56. }
  57. }
  58. public void FlagAttributeWritten(int attribute)
  59. {
  60. if (Config.Stage == ShaderStage.Vertex)
  61. {
  62. switch (attribute)
  63. {
  64. case AttributeConsts.ClipDistance0:
  65. case AttributeConsts.ClipDistance1:
  66. case AttributeConsts.ClipDistance2:
  67. case AttributeConsts.ClipDistance3:
  68. case AttributeConsts.ClipDistance4:
  69. case AttributeConsts.ClipDistance5:
  70. case AttributeConsts.ClipDistance6:
  71. case AttributeConsts.ClipDistance7:
  72. Config.SetClipDistanceWritten((attribute - AttributeConsts.ClipDistance0) / 4);
  73. break;
  74. }
  75. }
  76. }
  77. public void MarkLabel(Operand label)
  78. {
  79. Add(Instruction.MarkLabel, label);
  80. }
  81. public Operand GetLabel(ulong address)
  82. {
  83. if (!_labels.TryGetValue(address, out Operand label))
  84. {
  85. label = Label();
  86. _labels.Add(address, label);
  87. }
  88. return label;
  89. }
  90. public int GetFunctionId(ulong address)
  91. {
  92. return _funcs[address];
  93. }
  94. public void PrepareForReturn()
  95. {
  96. if (!IsNonMain && Config.Stage == ShaderStage.Fragment)
  97. {
  98. if (Config.OmapDepth)
  99. {
  100. Operand dest = Attribute(AttributeConsts.FragmentOutputDepth);
  101. Operand src = Register(Config.GetDepthRegister(), RegisterType.Gpr);
  102. this.Copy(dest, src);
  103. }
  104. int regIndexBase = 0;
  105. for (int rtIndex = 0; rtIndex < 8; rtIndex++)
  106. {
  107. OmapTarget target = Config.OmapTargets[rtIndex];
  108. for (int component = 0; component < 4; component++)
  109. {
  110. if (!target.ComponentEnabled(component))
  111. {
  112. continue;
  113. }
  114. int fragmentOutputColorAttr = AttributeConsts.FragmentOutputColorBase + rtIndex * 16;
  115. Operand src = Register(regIndexBase + component, RegisterType.Gpr);
  116. // Perform B <-> R swap if needed, for BGRA formats (not supported on OpenGL).
  117. if (component == 0 || component == 2)
  118. {
  119. Operand isBgra = Attribute(AttributeConsts.FragmentOutputIsBgraBase + rtIndex * 4);
  120. Operand lblIsBgra = Label();
  121. Operand lblEnd = Label();
  122. this.BranchIfTrue(lblIsBgra, isBgra);
  123. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  124. this.Branch(lblEnd);
  125. MarkLabel(lblIsBgra);
  126. this.Copy(Attribute(fragmentOutputColorAttr + (2 - component) * 4), src);
  127. MarkLabel(lblEnd);
  128. }
  129. else
  130. {
  131. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  132. }
  133. }
  134. if (target.Enabled)
  135. {
  136. regIndexBase += 4;
  137. }
  138. }
  139. }
  140. }
  141. public Operation[] GetOperations()
  142. {
  143. return _operations.ToArray();
  144. }
  145. }
  146. }