EmitterContext.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Ryujinx.Graphics.Gal;
  2. using Ryujinx.Graphics.Shader.Decoders;
  3. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  4. using System.Collections.Generic;
  5. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  6. namespace Ryujinx.Graphics.Shader.Translation
  7. {
  8. class EmitterContext
  9. {
  10. public Block CurrBlock { get; set; }
  11. public OpCode CurrOp { get; set; }
  12. private GalShaderType _shaderType;
  13. private ShaderHeader _header;
  14. private List<Operation> _operations;
  15. private Dictionary<ulong, Operand> _labels;
  16. public EmitterContext(GalShaderType shaderType, ShaderHeader header)
  17. {
  18. _shaderType = shaderType;
  19. _header = header;
  20. _operations = new List<Operation>();
  21. _labels = new Dictionary<ulong, Operand>();
  22. }
  23. public Operand Add(Instruction inst, Operand dest = null, params Operand[] sources)
  24. {
  25. Operation operation = new Operation(inst, dest, sources);
  26. Add(operation);
  27. return dest;
  28. }
  29. public void Add(Operation operation)
  30. {
  31. _operations.Add(operation);
  32. }
  33. public void MarkLabel(Operand label)
  34. {
  35. Add(Instruction.MarkLabel, label);
  36. }
  37. public Operand GetLabel(ulong address)
  38. {
  39. if (!_labels.TryGetValue(address, out Operand label))
  40. {
  41. label = Label();
  42. _labels.Add(address, label);
  43. }
  44. return label;
  45. }
  46. public void PrepareForReturn()
  47. {
  48. if (_shaderType == GalShaderType.Fragment)
  49. {
  50. if (_header.OmapDepth)
  51. {
  52. Operand dest = Attribute(AttributeConsts.FragmentOutputDepth);
  53. Operand src = Register(_header.DepthRegister, RegisterType.Gpr);
  54. this.Copy(dest, src);
  55. }
  56. int regIndex = 0;
  57. for (int attachment = 0; attachment < 8; attachment++)
  58. {
  59. OutputMapTarget target = _header.OmapTargets[attachment];
  60. for (int component = 0; component < 4; component++)
  61. {
  62. if (target.ComponentEnabled(component))
  63. {
  64. Operand dest = Attribute(AttributeConsts.FragmentOutputColorBase + regIndex * 4);
  65. Operand src = Register(regIndex, RegisterType.Gpr);
  66. this.Copy(dest, src);
  67. regIndex++;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. public Operation[] GetOperations()
  74. {
  75. return _operations.ToArray();
  76. }
  77. }
  78. }