Host1xDevice.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.Device;
  4. using System;
  5. using System.Numerics;
  6. namespace Ryujinx.Graphics.Host1x
  7. {
  8. public sealed class Host1xDevice : IDisposable
  9. {
  10. private readonly struct Command
  11. {
  12. public int[] Buffer { get; }
  13. public long ContextId { get; }
  14. public Command(int[] buffer, long contextId)
  15. {
  16. Buffer = buffer;
  17. ContextId = contextId;
  18. }
  19. }
  20. private readonly SyncptIncrManager _syncptIncrMgr;
  21. private readonly AsyncWorkQueue<Command> _commandQueue;
  22. private readonly Devices _devices = new();
  23. public Host1xClass Class { get; }
  24. private IDeviceState _device;
  25. private int _count;
  26. private int _offset;
  27. private int _mask;
  28. private bool _incrementing;
  29. public Host1xDevice(ISynchronizationManager syncMgr)
  30. {
  31. _syncptIncrMgr = new SyncptIncrManager(syncMgr);
  32. _commandQueue = new AsyncWorkQueue<Command>(Process, "Ryujinx.Host1xProcessor");
  33. Class = new Host1xClass(syncMgr);
  34. _devices.RegisterDevice(ClassId.Host1x, Class);
  35. }
  36. public void RegisterDevice(ClassId classId, IDeviceState device)
  37. {
  38. var thi = new ThiDevice(classId, device ?? throw new ArgumentNullException(nameof(device)), _syncptIncrMgr);
  39. _devices.RegisterDevice(classId, thi);
  40. }
  41. public long CreateContext()
  42. {
  43. if (_devices.GetDevice(ClassId.Nvdec) is IDeviceStateWithContext nvdec)
  44. {
  45. return nvdec.CreateContext();
  46. }
  47. return -1;
  48. }
  49. public void DestroyContext(long id)
  50. {
  51. if (id == -1)
  52. {
  53. return;
  54. }
  55. if (_devices.GetDevice(ClassId.Nvdec) is IDeviceStateWithContext nvdec)
  56. {
  57. nvdec.DestroyContext(id);
  58. }
  59. }
  60. private void SetNvdecContext(long id)
  61. {
  62. if (id == -1)
  63. {
  64. return;
  65. }
  66. if (_devices.GetDevice(ClassId.Nvdec) is IDeviceStateWithContext nvdec)
  67. {
  68. nvdec.BindContext(id);
  69. }
  70. }
  71. public void Submit(ReadOnlySpan<int> commandBuffer, long contextId)
  72. {
  73. _commandQueue.Add(new Command(commandBuffer.ToArray(), contextId));
  74. }
  75. private void Process(Command command)
  76. {
  77. SetNvdecContext(command.ContextId);
  78. int[] commandBuffer = command.Buffer;
  79. for (int index = 0; index < commandBuffer.Length; index++)
  80. {
  81. Step(commandBuffer[index]);
  82. }
  83. }
  84. private void Step(int value)
  85. {
  86. if (_mask != 0)
  87. {
  88. int lbs = BitOperations.TrailingZeroCount(_mask);
  89. _mask &= ~(1 << lbs);
  90. DeviceWrite(_offset + lbs, value);
  91. return;
  92. }
  93. else if (_count != 0)
  94. {
  95. _count--;
  96. DeviceWrite(_offset, value);
  97. if (_incrementing)
  98. {
  99. _offset++;
  100. }
  101. return;
  102. }
  103. OpCode opCode = (OpCode)((value >> 28) & 0xf);
  104. switch (opCode)
  105. {
  106. case OpCode.SetClass:
  107. _mask = value & 0x3f;
  108. ClassId classId = (ClassId)((value >> 6) & 0x3ff);
  109. _offset = (value >> 16) & 0xfff;
  110. _device = _devices.GetDevice(classId);
  111. break;
  112. case OpCode.Incr:
  113. case OpCode.NonIncr:
  114. _count = value & 0xffff;
  115. _offset = (value >> 16) & 0xfff;
  116. _incrementing = opCode == OpCode.Incr;
  117. break;
  118. case OpCode.Mask:
  119. _mask = value & 0xffff;
  120. _offset = (value >> 16) & 0xfff;
  121. break;
  122. case OpCode.Imm:
  123. int data = value & 0xfff;
  124. _offset = (value >> 16) & 0xfff;
  125. DeviceWrite(_offset, data);
  126. break;
  127. default:
  128. Logger.Error?.Print(LogClass.Host1x, $"Unsupported opcode \"{opCode}\".");
  129. break;
  130. }
  131. }
  132. private void DeviceWrite(int offset, int data)
  133. {
  134. _device?.Write(offset * 4, data);
  135. }
  136. public void Dispose()
  137. {
  138. _commandQueue.Dispose();
  139. _devices.Dispose();
  140. }
  141. }
  142. }