DmaPusher.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System.Collections.Concurrent;
  2. using System.Threading;
  3. namespace Ryujinx.Graphics.Gpu
  4. {
  5. /// <summary>
  6. /// GPU DMA pusher, used to push commands to the GPU.
  7. /// </summary>
  8. public class DmaPusher
  9. {
  10. private ConcurrentQueue<ulong> _ibBuffer;
  11. private ulong _dmaPut;
  12. private ulong _dmaGet;
  13. /// <summary>
  14. /// Internal GPFIFO state.
  15. /// </summary>
  16. private struct DmaState
  17. {
  18. public int Method;
  19. public int SubChannel;
  20. public int MethodCount;
  21. public bool NonIncrementing;
  22. public bool IncrementOnce;
  23. public int LengthPending;
  24. }
  25. private DmaState _state;
  26. private bool _sliEnable;
  27. private bool _sliActive;
  28. private bool _ibEnable;
  29. private bool _nonMain;
  30. private ulong _dmaMGet;
  31. private GpuContext _context;
  32. private AutoResetEvent _event;
  33. /// <summary>
  34. /// Creates a new instance of the GPU DMA pusher.
  35. /// </summary>
  36. /// <param name="context">GPU context that the pusher belongs to</param>
  37. internal DmaPusher(GpuContext context)
  38. {
  39. _context = context;
  40. _ibBuffer = new ConcurrentQueue<ulong>();
  41. _ibEnable = true;
  42. _event = new AutoResetEvent(false);
  43. }
  44. /// <summary>
  45. /// Pushes a GPFIFO entry.
  46. /// </summary>
  47. /// <param name="entry">GPFIFO entry</param>
  48. public void Push(ulong entry)
  49. {
  50. _ibBuffer.Enqueue(entry);
  51. _event.Set();
  52. }
  53. /// <summary>
  54. /// Waits until commands are pushed to the FIFO.
  55. /// </summary>
  56. /// <returns>True if commands were received, false if wait timed out</returns>
  57. public bool WaitForCommands()
  58. {
  59. return _event.WaitOne(8);
  60. }
  61. /// <summary>
  62. /// Processes commands pushed to the FIFO.
  63. /// </summary>
  64. public void DispatchCalls()
  65. {
  66. while (Step());
  67. }
  68. /// <summary>
  69. /// Processes a single command on the FIFO.
  70. /// </summary>
  71. /// <returns>True if the FIFO still has commands to be processed, false otherwise</returns>
  72. private bool Step()
  73. {
  74. if (_dmaGet != _dmaPut)
  75. {
  76. int word = _context.MemoryAccessor.ReadInt32(_dmaGet);
  77. _dmaGet += 4;
  78. if (!_nonMain)
  79. {
  80. _dmaMGet = _dmaGet;
  81. }
  82. if (_state.LengthPending != 0)
  83. {
  84. _state.LengthPending = 0;
  85. _state.MethodCount = word & 0xffffff;
  86. }
  87. else if (_state.MethodCount != 0)
  88. {
  89. if (!_sliEnable || _sliActive)
  90. {
  91. CallMethod(word);
  92. }
  93. if (!_state.NonIncrementing)
  94. {
  95. _state.Method++;
  96. }
  97. if (_state.IncrementOnce)
  98. {
  99. _state.NonIncrementing = true;
  100. }
  101. _state.MethodCount--;
  102. }
  103. else
  104. {
  105. int submissionMode = (word >> 29) & 7;
  106. switch (submissionMode)
  107. {
  108. case 1:
  109. // Incrementing.
  110. SetNonImmediateState(word);
  111. _state.NonIncrementing = false;
  112. _state.IncrementOnce = false;
  113. break;
  114. case 3:
  115. // Non-incrementing.
  116. SetNonImmediateState(word);
  117. _state.NonIncrementing = true;
  118. _state.IncrementOnce = false;
  119. break;
  120. case 4:
  121. // Immediate.
  122. _state.Method = (word >> 0) & 0x1fff;
  123. _state.SubChannel = (word >> 13) & 7;
  124. _state.NonIncrementing = true;
  125. _state.IncrementOnce = false;
  126. CallMethod((word >> 16) & 0x1fff);
  127. break;
  128. case 5:
  129. // Increment-once.
  130. SetNonImmediateState(word);
  131. _state.NonIncrementing = false;
  132. _state.IncrementOnce = true;
  133. break;
  134. }
  135. }
  136. }
  137. else if (_ibEnable && _ibBuffer.TryDequeue(out ulong entry))
  138. {
  139. ulong length = (entry >> 42) & 0x1fffff;
  140. _dmaGet = entry & 0xfffffffffc;
  141. _dmaPut = _dmaGet + length * 4;
  142. _nonMain = (entry & (1UL << 41)) != 0;
  143. }
  144. else
  145. {
  146. return false;
  147. }
  148. return true;
  149. }
  150. /// <summary>
  151. /// Sets current non-immediate method call state.
  152. /// </summary>
  153. /// <param name="word">Compressed method word</param>
  154. private void SetNonImmediateState(int word)
  155. {
  156. _state.Method = (word >> 0) & 0x1fff;
  157. _state.SubChannel = (word >> 13) & 7;
  158. _state.MethodCount = (word >> 16) & 0x1fff;
  159. }
  160. /// <summary>
  161. /// Forwards the method call to GPU engines.
  162. /// </summary>
  163. /// <param name="argument">Call argument</param>
  164. private void CallMethod(int argument)
  165. {
  166. _context.Fifo.CallMethod(new MethodParams(
  167. _state.Method,
  168. argument,
  169. _state.SubChannel,
  170. _state.MethodCount));
  171. }
  172. }
  173. }