Operand.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Shader.IntermediateRepresentation
  5. {
  6. class Operand
  7. {
  8. private const int CbufSlotBits = 5;
  9. private const int CbufSlotLsb = 32 - CbufSlotBits;
  10. private const int CbufSlotMask = (1 << CbufSlotBits) - 1;
  11. public OperandType Type { get; }
  12. public int Value { get; }
  13. public INode AsgOp { get; set; }
  14. public HashSet<INode> UseOps { get; }
  15. private Operand()
  16. {
  17. UseOps = new HashSet<INode>();
  18. }
  19. public Operand(OperandType type) : this()
  20. {
  21. Type = type;
  22. }
  23. public Operand(OperandType type, int value) : this()
  24. {
  25. Type = type;
  26. Value = value;
  27. }
  28. public Operand(Register reg) : this()
  29. {
  30. Type = OperandType.Register;
  31. Value = PackRegInfo(reg.Index, reg.Type);
  32. }
  33. public Operand(int slot, int offset) : this()
  34. {
  35. Type = OperandType.ConstantBuffer;
  36. Value = PackCbufInfo(slot, offset);
  37. }
  38. private static int PackCbufInfo(int slot, int offset)
  39. {
  40. return (slot << CbufSlotLsb) | offset;
  41. }
  42. private static int PackRegInfo(int index, RegisterType type)
  43. {
  44. return ((int)type << 24) | index;
  45. }
  46. public int GetCbufSlot()
  47. {
  48. return (Value >> CbufSlotLsb) & CbufSlotMask;
  49. }
  50. public int GetCbufOffset()
  51. {
  52. return Value & ~(CbufSlotMask << CbufSlotLsb);
  53. }
  54. public Register GetRegister()
  55. {
  56. return new Register(Value & 0xffffff, (RegisterType)(Value >> 24));
  57. }
  58. public float AsFloat()
  59. {
  60. return BitConverter.Int32BitsToSingle(Value);
  61. }
  62. }
  63. }