EmitterContext.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. private ShaderStage _stage;
  12. private ShaderHeader _header;
  13. private ShaderCapabilities _capabilities;
  14. private TranslationFlags _flags;
  15. private List<Operation> _operations;
  16. private Dictionary<ulong, Operand> _labels;
  17. public EmitterContext(
  18. ShaderStage stage,
  19. ShaderHeader header,
  20. ShaderCapabilities capabilities,
  21. TranslationFlags flags)
  22. {
  23. _stage = stage;
  24. _header = header;
  25. _capabilities = capabilities;
  26. _flags = flags;
  27. _operations = new List<Operation>();
  28. _labels = new Dictionary<ulong, Operand>();
  29. }
  30. public Operand Add(Instruction inst, Operand dest = null, params Operand[] sources)
  31. {
  32. Operation operation = new Operation(inst, dest, sources);
  33. Add(operation);
  34. return dest;
  35. }
  36. public void Add(Operation operation)
  37. {
  38. _operations.Add(operation);
  39. }
  40. public void MarkLabel(Operand label)
  41. {
  42. Add(Instruction.MarkLabel, label);
  43. }
  44. public Operand GetLabel(ulong address)
  45. {
  46. if (!_labels.TryGetValue(address, out Operand label))
  47. {
  48. label = Label();
  49. _labels.Add(address, label);
  50. }
  51. return label;
  52. }
  53. public void PrepareForReturn()
  54. {
  55. if (_stage == ShaderStage.Vertex)
  56. {
  57. if ((_flags & TranslationFlags.DividePosXY) != 0)
  58. {
  59. Operand posX = Attribute(AttributeConsts.PositionX);
  60. Operand posY = Attribute(AttributeConsts.PositionY);
  61. this.Copy(posX, this.FPDivide(posX, ConstF(_capabilities.MaximumViewportDimensions / 2)));
  62. this.Copy(posY, this.FPDivide(posY, ConstF(_capabilities.MaximumViewportDimensions / 2)));
  63. }
  64. }
  65. else if (_stage == ShaderStage.Fragment)
  66. {
  67. if (_header.OmapDepth)
  68. {
  69. Operand dest = Attribute(AttributeConsts.FragmentOutputDepth);
  70. Operand src = Register(_header.DepthRegister, RegisterType.Gpr);
  71. this.Copy(dest, src);
  72. }
  73. int regIndex = 0;
  74. for (int attachment = 0; attachment < 8; attachment++)
  75. {
  76. OutputMapTarget target = _header.OmapTargets[attachment];
  77. for (int component = 0; component < 4; component++)
  78. {
  79. if (target.ComponentEnabled(component))
  80. {
  81. Operand dest = Attribute(AttributeConsts.FragmentOutputColorBase + regIndex * 4);
  82. Operand src = Register(regIndex, RegisterType.Gpr);
  83. this.Copy(dest, src);
  84. regIndex++;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. public Operation[] GetOperations()
  91. {
  92. return _operations.ToArray();
  93. }
  94. }
  95. }