EmitterContext.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 attachment = 0; attachment < 8; attachment++)
  92. {
  93. OmapTarget target = Config.OmapTargets[attachment];
  94. for (int component = 0; component < 4; component++)
  95. {
  96. if (target.ComponentEnabled(component))
  97. {
  98. Operand dest = Attribute(AttributeConsts.FragmentOutputColorBase + attachment * 16 + component * 4);
  99. Operand src = Register(regIndex, RegisterType.Gpr);
  100. this.Copy(dest, src);
  101. regIndex++;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. public Operation[] GetOperations()
  108. {
  109. return _operations.ToArray();
  110. }
  111. }
  112. }