EmitterContext.cs 5.9 KB

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