AstOperation.cs
 1  using Ryujinx.Graphics.Shader.IntermediateRepresentation;
 2  using Ryujinx.Graphics.Shader.Translation;
 3  using System.Numerics;
 4  
 5  using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
 6  
 7  namespace Ryujinx.Graphics.Shader.StructuredIr
 8  {
 9      class AstOperation : AstNode
10      {
11          public Instruction Inst { get; }
12          public StorageKind StorageKind { get; }
13          public bool ForcePrecise { get; }
14  
15          public int Index { get; }
16  
17          private readonly IAstNode[] _sources;
18  
19          public int SourcesCount => _sources.Length;
20  
21          public AstOperation(Instruction inst, StorageKind storageKind, bool forcePrecise, IAstNode[] sources, int sourcesCount)
22          {
23              Inst = inst;
24              StorageKind = storageKind;
25              ForcePrecise = forcePrecise;
26              _sources = sources;
27  
28              for (int index = 0; index < sources.Length; index++)
29              {
30                  if (index < sourcesCount)
31                  {
32                      AddUse(sources[index], this);
33                  }
34                  else
35                  {
36                      AddDef(sources[index], this);
37                  }
38              }
39  
40              Index = 0;
41          }
42  
43          public AstOperation(
44              Instruction inst,
45              StorageKind storageKind,
46              bool forcePrecise,
47              int index,
48              IAstNode[] sources,
49              int sourcesCount) : this(inst, storageKind, forcePrecise, sources, sourcesCount)
50          {
51              Index = index;
52          }
53  
54          public AstOperation(Instruction inst, params IAstNode[] sources) : this(inst, StorageKind.None, false, sources, sources.Length)
55          {
56          }
57  
58          public IAstNode GetSource(int index)
59          {
60              return _sources[index];
61          }
62  
63          public void SetSource(int index, IAstNode source)
64          {
65              RemoveUse(_sources[index], this);
66  
67              AddUse(source, this);
68  
69              _sources[index] = source;
70          }
71  
72          public AggregateType GetVectorType(AggregateType scalarType)
73          {
74              int componentsCount = BitOperations.PopCount((uint)Index);
75  
76              AggregateType type = scalarType;
77  
78              switch (componentsCount)
79              {
80                  case 2:
81                      type |= AggregateType.Vector2;
82                      break;
83                  case 3:
84                      type |= AggregateType.Vector3;
85                      break;
86                  case 4:
87                      type |= AggregateType.Vector4;
88                      break;
89              }
90  
91              return type;
92          }
93      }
94  }