EmitterContext.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 ShaderConfig _config;
  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)
  46. {
  47. if (!_config.QueryInfoBool(QueryInfoName.ViewportTransformEnable))
  48. {
  49. Operand posX = Attribute(AttributeConsts.PositionX);
  50. Operand posY = Attribute(AttributeConsts.PositionY);
  51. this.Copy(posX, this.FPDivide(posX, ConstF(_config.QueryInfo(QueryInfoName.MaximumViewportDimensions) / 2)));
  52. this.Copy(posY, this.FPDivide(posY, ConstF(_config.QueryInfo(QueryInfoName.MaximumViewportDimensions) / 2)));
  53. }
  54. }
  55. else if (_config.Stage == ShaderStage.Fragment)
  56. {
  57. if (_config.OmapDepth)
  58. {
  59. Operand dest = Attribute(AttributeConsts.FragmentOutputDepth);
  60. Operand src = Register(_config.GetDepthRegister(), RegisterType.Gpr);
  61. this.Copy(dest, src);
  62. }
  63. int regIndex = 0;
  64. for (int attachment = 0; attachment < 8; attachment++)
  65. {
  66. OutputMapTarget target = _config.OmapTargets[attachment];
  67. for (int component = 0; component < 4; component++)
  68. {
  69. if (target.ComponentEnabled(component))
  70. {
  71. Operand dest = Attribute(AttributeConsts.FragmentOutputColorBase + regIndex * 4);
  72. Operand src = Register(regIndex, RegisterType.Gpr);
  73. this.Copy(dest, src);
  74. regIndex++;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. public Operation[] GetOperations()
  81. {
  82. return _operations.ToArray();
  83. }
  84. }
  85. }