EmitterContext.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 MarkLabel(Operand label)
  31. {
  32. Add(Instruction.MarkLabel, label);
  33. }
  34. public Operand GetLabel(ulong address)
  35. {
  36. if (!_labels.TryGetValue(address, out Operand label))
  37. {
  38. label = Label();
  39. _labels.Add(address, label);
  40. }
  41. return label;
  42. }
  43. public void PrepareForReturn()
  44. {
  45. if (Config.Stage == ShaderStage.Vertex && (Config.Flags & TranslationFlags.VertexA) == 0)
  46. {
  47. // Here we attempt to implement viewport swizzle on the vertex shader.
  48. // Perform permutation and negation of the output gl_Position components.
  49. // Note that per-viewport swizzling can't be supported using this approach.
  50. int swizzleX = Config.GpuAccessor.QueryViewportSwizzle(0);
  51. int swizzleY = Config.GpuAccessor.QueryViewportSwizzle(1);
  52. int swizzleZ = Config.GpuAccessor.QueryViewportSwizzle(2);
  53. int swizzleW = Config.GpuAccessor.QueryViewportSwizzle(3);
  54. bool nonStandardSwizzle = swizzleX != 0 || swizzleY != 2 || swizzleZ != 4 || swizzleW != 6;
  55. if (!Config.GpuAccessor.QuerySupportsViewportSwizzle() && nonStandardSwizzle)
  56. {
  57. Operand[] temp = new Operand[4];
  58. temp[0] = this.Copy(Attribute(AttributeConsts.PositionX));
  59. temp[1] = this.Copy(Attribute(AttributeConsts.PositionY));
  60. temp[2] = this.Copy(Attribute(AttributeConsts.PositionZ));
  61. temp[3] = this.Copy(Attribute(AttributeConsts.PositionW));
  62. this.Copy(Attribute(AttributeConsts.PositionX), this.FPNegate(temp[(swizzleX >> 1) & 3], (swizzleX & 1) != 0));
  63. this.Copy(Attribute(AttributeConsts.PositionY), this.FPNegate(temp[(swizzleY >> 1) & 3], (swizzleY & 1) != 0));
  64. this.Copy(Attribute(AttributeConsts.PositionZ), this.FPNegate(temp[(swizzleZ >> 1) & 3], (swizzleZ & 1) != 0));
  65. this.Copy(Attribute(AttributeConsts.PositionW), this.FPNegate(temp[(swizzleW >> 1) & 3], (swizzleW & 1) != 0));
  66. }
  67. }
  68. else if (Config.Stage == ShaderStage.Fragment)
  69. {
  70. if (Config.OmapDepth)
  71. {
  72. Operand dest = Attribute(AttributeConsts.FragmentOutputDepth);
  73. Operand src = Register(Config.GetDepthRegister(), RegisterType.Gpr);
  74. this.Copy(dest, src);
  75. }
  76. int regIndex = 0;
  77. for (int attachment = 0; attachment < 8; attachment++)
  78. {
  79. OmapTarget target = Config.OmapTargets[attachment];
  80. for (int component = 0; component < 4; component++)
  81. {
  82. if (target.ComponentEnabled(component))
  83. {
  84. Operand dest = Attribute(AttributeConsts.FragmentOutputColorBase + attachment * 16 + component * 4);
  85. Operand src = Register(regIndex, RegisterType.Gpr);
  86. this.Copy(dest, src);
  87. regIndex++;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. public Operation[] GetOperations()
  94. {
  95. return _operations.ToArray();
  96. }
  97. }
  98. }