Operation.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. namespace Ryujinx.Graphics.Shader.IntermediateRepresentation
  2. {
  3. class Operation : INode
  4. {
  5. public Instruction Inst { get; private set; }
  6. private Operand _dest;
  7. public Operand Dest
  8. {
  9. get => _dest;
  10. set => _dest = AssignDest(value);
  11. }
  12. private Operand[] _sources;
  13. public int SourcesCount => _sources.Length;
  14. public int ComponentIndex { get; }
  15. public Operation(Instruction inst, Operand dest, params Operand[] sources)
  16. {
  17. Inst = inst;
  18. Dest = dest;
  19. // The array may be modified externally, so we store a copy.
  20. _sources = (Operand[])sources.Clone();
  21. for (int index = 0; index < _sources.Length; index++)
  22. {
  23. Operand source = _sources[index];
  24. if (source.Type == OperandType.LocalVariable)
  25. {
  26. source.UseOps.Add(this);
  27. }
  28. }
  29. }
  30. public Operation(
  31. Instruction inst,
  32. int compIndex,
  33. Operand dest,
  34. params Operand[] sources) : this(inst, dest, sources)
  35. {
  36. ComponentIndex = compIndex;
  37. }
  38. private Operand AssignDest(Operand dest)
  39. {
  40. if (dest != null && dest.Type == OperandType.LocalVariable)
  41. {
  42. dest.AsgOp = this;
  43. }
  44. return dest;
  45. }
  46. public Operand GetSource(int index)
  47. {
  48. return _sources[index];
  49. }
  50. public void SetSource(int index, Operand source)
  51. {
  52. Operand oldSrc = _sources[index];
  53. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  54. {
  55. oldSrc.UseOps.Remove(this);
  56. }
  57. if (source != null && source.Type == OperandType.LocalVariable)
  58. {
  59. source.UseOps.Add(this);
  60. }
  61. _sources[index] = source;
  62. }
  63. public void TurnIntoCopy(Operand source)
  64. {
  65. TurnInto(Instruction.Copy, source);
  66. }
  67. public void TurnInto(Instruction newInst, Operand source)
  68. {
  69. Inst = newInst;
  70. foreach (Operand oldSrc in _sources)
  71. {
  72. if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
  73. {
  74. oldSrc.UseOps.Remove(this);
  75. }
  76. }
  77. if (source.Type == OperandType.LocalVariable)
  78. {
  79. source.UseOps.Add(this);
  80. }
  81. _sources = new Operand[] { source };
  82. }
  83. }
  84. }