EmitterContext.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. public ShaderConfig Config => _config;
  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 MarkLabel(Operand label)
  32. {
  33. Add(Instruction.MarkLabel, label);
  34. }
  35. public Operand GetLabel(ulong address)
  36. {
  37. if (!_labels.TryGetValue(address, out Operand label))
  38. {
  39. label = Label();
  40. _labels.Add(address, label);
  41. }
  42. return label;
  43. }
  44. public void PrepareForReturn()
  45. {
  46. if (_config.Stage == ShaderStage.Fragment)
  47. {
  48. if (_config.OmapDepth)
  49. {
  50. Operand dest = Attribute(AttributeConsts.FragmentOutputDepth);
  51. Operand src = Register(_config.GetDepthRegister(), RegisterType.Gpr);
  52. this.Copy(dest, src);
  53. }
  54. int regIndex = 0;
  55. for (int attachment = 0; attachment < 8; attachment++)
  56. {
  57. OutputMapTarget target = _config.OmapTargets[attachment];
  58. for (int component = 0; component < 4; component++)
  59. {
  60. if (target.ComponentEnabled(component))
  61. {
  62. Operand dest = Attribute(AttributeConsts.FragmentOutputColorBase + regIndex * 4);
  63. Operand src = Register(regIndex, RegisterType.Gpr);
  64. this.Copy(dest, src);
  65. regIndex++;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. public Operation[] GetOperations()
  72. {
  73. return _operations.ToArray();
  74. }
  75. }
  76. }