EmitterContext.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 FeatureFlags UsedFeatures { 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. UsedFeatures |= 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.Vertex && (Config.Flags & TranslationFlags.VertexA) == 0)
  60. {
  61. // Here we attempt to implement viewport swizzle on the vertex shader.
  62. // Perform permutation and negation of the output gl_Position components.
  63. // Note that per-viewport swizzling can't be supported using this approach.
  64. int swizzleX = Config.GpuAccessor.QueryViewportSwizzle(0);
  65. int swizzleY = Config.GpuAccessor.QueryViewportSwizzle(1);
  66. int swizzleZ = Config.GpuAccessor.QueryViewportSwizzle(2);
  67. int swizzleW = Config.GpuAccessor.QueryViewportSwizzle(3);
  68. bool nonStandardSwizzle = swizzleX != 0 || swizzleY != 2 || swizzleZ != 4 || swizzleW != 6;
  69. if (!Config.GpuAccessor.QuerySupportsViewportSwizzle() && nonStandardSwizzle)
  70. {
  71. Operand[] temp = new Operand[4];
  72. temp[0] = this.Copy(Attribute(AttributeConsts.PositionX));
  73. temp[1] = this.Copy(Attribute(AttributeConsts.PositionY));
  74. temp[2] = this.Copy(Attribute(AttributeConsts.PositionZ));
  75. temp[3] = this.Copy(Attribute(AttributeConsts.PositionW));
  76. this.Copy(Attribute(AttributeConsts.PositionX), this.FPNegate(temp[(swizzleX >> 1) & 3], (swizzleX & 1) != 0));
  77. this.Copy(Attribute(AttributeConsts.PositionY), this.FPNegate(temp[(swizzleY >> 1) & 3], (swizzleY & 1) != 0));
  78. this.Copy(Attribute(AttributeConsts.PositionZ), this.FPNegate(temp[(swizzleZ >> 1) & 3], (swizzleZ & 1) != 0));
  79. this.Copy(Attribute(AttributeConsts.PositionW), this.FPNegate(temp[(swizzleW >> 1) & 3], (swizzleW & 1) != 0));
  80. }
  81. }
  82. else if (Config.Stage == ShaderStage.Fragment)
  83. {
  84. if (Config.OmapDepth)
  85. {
  86. Operand dest = Attribute(AttributeConsts.FragmentOutputDepth);
  87. Operand src = Register(Config.GetDepthRegister(), RegisterType.Gpr);
  88. this.Copy(dest, src);
  89. }
  90. int regIndex = 0;
  91. for (int rtIndex = 0; rtIndex < 8; rtIndex++)
  92. {
  93. OmapTarget target = Config.OmapTargets[rtIndex];
  94. for (int component = 0; component < 4; component++)
  95. {
  96. if (!target.ComponentEnabled(component))
  97. {
  98. continue;
  99. }
  100. int fragmentOutputColorAttr = AttributeConsts.FragmentOutputColorBase + rtIndex * 16;
  101. Operand src = Register(regIndex, RegisterType.Gpr);
  102. // Perform B <-> R swap if needed, for BGRA formats (not supported on OpenGL).
  103. if (component == 0 || component == 2)
  104. {
  105. Operand isBgra = Attribute(AttributeConsts.FragmentOutputIsBgraBase + rtIndex * 4);
  106. Operand lblIsBgra = Label();
  107. Operand lblEnd = Label();
  108. this.BranchIfTrue(lblIsBgra, isBgra);
  109. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  110. this.Branch(lblEnd);
  111. MarkLabel(lblIsBgra);
  112. this.Copy(Attribute(fragmentOutputColorAttr + (2 - component) * 4), src);
  113. MarkLabel(lblEnd);
  114. }
  115. else
  116. {
  117. this.Copy(Attribute(fragmentOutputColorAttr + component * 4), src);
  118. }
  119. regIndex++;
  120. }
  121. }
  122. }
  123. }
  124. public Operation[] GetOperations()
  125. {
  126. return _operations.ToArray();
  127. }
  128. }
  129. }