AstOperation.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using System.Numerics;
  4. using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
  5. namespace Ryujinx.Graphics.Shader.StructuredIr
  6. {
  7. class AstOperation : AstNode
  8. {
  9. public Instruction Inst { get; }
  10. public int Index { get; }
  11. private IAstNode[] _sources;
  12. public int SourcesCount => _sources.Length;
  13. public AstOperation(Instruction inst, IAstNode[] sources, int sourcesCount)
  14. {
  15. Inst = inst;
  16. _sources = sources;
  17. for (int index = 0; index < sources.Length; index++)
  18. {
  19. if (index < sourcesCount)
  20. {
  21. AddUse(sources[index], this);
  22. }
  23. else
  24. {
  25. AddDef(sources[index], this);
  26. }
  27. }
  28. Index = 0;
  29. }
  30. public AstOperation(Instruction inst, int index, IAstNode[] sources, int sourcesCount) : this(inst, sources, sourcesCount)
  31. {
  32. Index = index;
  33. }
  34. public AstOperation(Instruction inst, params IAstNode[] sources) : this(inst, sources, sources.Length)
  35. {
  36. }
  37. public IAstNode GetSource(int index)
  38. {
  39. return _sources[index];
  40. }
  41. public void SetSource(int index, IAstNode source)
  42. {
  43. RemoveUse(_sources[index], this);
  44. AddUse(source, this);
  45. _sources[index] = source;
  46. }
  47. public AggregateType GetVectorType(AggregateType scalarType)
  48. {
  49. int componentsCount = BitOperations.PopCount((uint)Index);
  50. AggregateType type = scalarType;
  51. switch (componentsCount)
  52. {
  53. case 2: type |= AggregateType.Vector2; break;
  54. case 3: type |= AggregateType.Vector3; break;
  55. case 4: type |= AggregateType.Vector4; break;
  56. }
  57. return type;
  58. }
  59. }
  60. }