Host1xDevice.cs 4.7 KB

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