GPFifoProcessor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using Ryujinx.Graphics.Device;
  2. using Ryujinx.Graphics.Gpu.Engine.Compute;
  3. using Ryujinx.Graphics.Gpu.Engine.Dma;
  4. using Ryujinx.Graphics.Gpu.Engine.InlineToMemory;
  5. using Ryujinx.Graphics.Gpu.Engine.Threed;
  6. using Ryujinx.Graphics.Gpu.Engine.Twod;
  7. using Ryujinx.Graphics.Gpu.Memory;
  8. using System;
  9. using System.Runtime.CompilerServices;
  10. namespace Ryujinx.Graphics.Gpu.Engine.GPFifo
  11. {
  12. /// <summary>
  13. /// Represents a GPU General Purpose FIFO command processor.
  14. /// </summary>
  15. class GPFifoProcessor
  16. {
  17. private const int MacrosCount = 0x80;
  18. private const int MacroIndexMask = MacrosCount - 1;
  19. private const int LoadInlineDataMethodOffset = 0x6d;
  20. private const int UniformBufferUpdateDataMethodOffset = 0x8e4;
  21. private readonly GpuChannel _channel;
  22. /// <summary>
  23. /// Channel memory manager.
  24. /// </summary>
  25. public MemoryManager MemoryManager => _channel.MemoryManager;
  26. /// <summary>
  27. /// Internal GPFIFO state.
  28. /// </summary>
  29. private struct DmaState
  30. {
  31. public int Method;
  32. public int SubChannel;
  33. public int MethodCount;
  34. public bool NonIncrementing;
  35. public bool IncrementOnce;
  36. }
  37. private DmaState _state;
  38. private readonly ThreedClass _3dClass;
  39. private readonly ComputeClass _computeClass;
  40. private readonly InlineToMemoryClass _i2mClass;
  41. private readonly TwodClass _2dClass;
  42. private readonly DmaClass _dmaClass;
  43. private readonly GPFifoClass _fifoClass;
  44. /// <summary>
  45. /// Creates a new instance of the GPU General Purpose FIFO command processor.
  46. /// </summary>
  47. /// <param name="context">GPU context</param>
  48. /// <param name="channel">Channel that the GPFIFO processor belongs to</param>
  49. public GPFifoProcessor(GpuContext context, GpuChannel channel)
  50. {
  51. _channel = channel;
  52. _fifoClass = new GPFifoClass(context, this);
  53. _3dClass = new ThreedClass(context, channel);
  54. _computeClass = new ComputeClass(context, channel, _3dClass);
  55. _i2mClass = new InlineToMemoryClass(context, channel);
  56. _2dClass = new TwodClass(channel);
  57. _dmaClass = new DmaClass(context, channel, _3dClass);
  58. }
  59. /// <summary>
  60. /// Processes a command buffer.
  61. /// </summary>
  62. /// <param name="commandBuffer">Command buffer</param>
  63. public void Process(ReadOnlySpan<int> commandBuffer)
  64. {
  65. for (int index = 0; index < commandBuffer.Length; index++)
  66. {
  67. int command = commandBuffer[index];
  68. if (_state.MethodCount != 0)
  69. {
  70. if (TryFastI2mBufferUpdate(commandBuffer, ref index))
  71. {
  72. continue;
  73. }
  74. Send(_state.Method, command, _state.SubChannel, _state.MethodCount <= 1);
  75. if (!_state.NonIncrementing)
  76. {
  77. _state.Method++;
  78. }
  79. if (_state.IncrementOnce)
  80. {
  81. _state.NonIncrementing = true;
  82. }
  83. _state.MethodCount--;
  84. }
  85. else
  86. {
  87. CompressedMethod meth = Unsafe.As<int, CompressedMethod>(ref command);
  88. if (TryFastUniformBufferUpdate(meth, commandBuffer, index))
  89. {
  90. index += meth.MethodCount;
  91. continue;
  92. }
  93. switch (meth.SecOp)
  94. {
  95. case SecOp.IncMethod:
  96. case SecOp.NonIncMethod:
  97. case SecOp.OneInc:
  98. _state.Method = meth.MethodAddress;
  99. _state.SubChannel = meth.MethodSubchannel;
  100. _state.MethodCount = meth.MethodCount;
  101. _state.IncrementOnce = meth.SecOp == SecOp.OneInc;
  102. _state.NonIncrementing = meth.SecOp == SecOp.NonIncMethod;
  103. break;
  104. case SecOp.ImmdDataMethod:
  105. Send(meth.MethodAddress, meth.ImmdData, meth.MethodSubchannel, true);
  106. break;
  107. }
  108. }
  109. }
  110. _3dClass.FlushUboDirty();
  111. }
  112. /// <summary>
  113. /// Tries to perform a fast Inline-to-Memory data update.
  114. /// If successful, all data will be copied at once, and <see cref="DmaState.MethodCount"/>
  115. /// command buffer entries will be consumed.
  116. /// </summary>
  117. /// <param name="commandBuffer">Command buffer where the data is contained</param>
  118. /// <param name="offset">Offset at <paramref name="commandBuffer"/> where the data is located, auto-incremented on success</param>
  119. /// <returns>True if the fast copy was successful, false otherwise</returns>
  120. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  121. private bool TryFastI2mBufferUpdate(ReadOnlySpan<int> commandBuffer, ref int offset)
  122. {
  123. if (_state.Method == LoadInlineDataMethodOffset && _state.NonIncrementing && _state.SubChannel <= 2)
  124. {
  125. int availableCount = commandBuffer.Length - offset;
  126. int consumeCount = Math.Min(_state.MethodCount, availableCount);
  127. var data = commandBuffer.Slice(offset, consumeCount);
  128. if (_state.SubChannel == 0)
  129. {
  130. _3dClass.LoadInlineData(data);
  131. }
  132. else if (_state.SubChannel == 1)
  133. {
  134. _computeClass.LoadInlineData(data);
  135. }
  136. else /* if (_state.SubChannel == 2) */
  137. {
  138. _i2mClass.LoadInlineData(data);
  139. }
  140. offset += consumeCount - 1;
  141. _state.MethodCount -= consumeCount;
  142. return true;
  143. }
  144. return false;
  145. }
  146. /// <summary>
  147. /// Tries to perform a fast constant buffer data update.
  148. /// If successful, all data will be copied at once, and <see cref="CompressedMethod.MethodCount"/> + 1
  149. /// command buffer entries will be consumed.
  150. /// </summary>
  151. /// <param name="meth">Compressed method to be checked</param>
  152. /// <param name="commandBuffer">Command buffer where <paramref name="meth"/> is contained</param>
  153. /// <param name="offset">Offset at <paramref name="commandBuffer"/> where <paramref name="meth"/> is located</param>
  154. /// <returns>True if the fast copy was successful, false otherwise</returns>
  155. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  156. private bool TryFastUniformBufferUpdate(CompressedMethod meth, ReadOnlySpan<int> commandBuffer, int offset)
  157. {
  158. int availableCount = commandBuffer.Length - offset;
  159. if (meth.MethodAddress == UniformBufferUpdateDataMethodOffset &&
  160. meth.MethodCount < availableCount &&
  161. meth.SecOp == SecOp.NonIncMethod)
  162. {
  163. _3dClass.ConstantBufferUpdate(commandBuffer.Slice(offset + 1, meth.MethodCount));
  164. return true;
  165. }
  166. return false;
  167. }
  168. /// <summary>
  169. /// Sends a uncompressed method for processing by the graphics pipeline.
  170. /// </summary>
  171. /// <param name="meth">Method to be processed</param>
  172. private void Send(int offset, int argument, int subChannel, bool isLastCall)
  173. {
  174. if (offset < 0x60)
  175. {
  176. _fifoClass.Write(offset * 4, argument);
  177. }
  178. else if (offset < 0xe00)
  179. {
  180. offset *= 4;
  181. switch (subChannel)
  182. {
  183. case 0:
  184. _3dClass.Write(offset, argument);
  185. break;
  186. case 1:
  187. _computeClass.Write(offset, argument);
  188. break;
  189. case 2:
  190. _i2mClass.Write(offset, argument);
  191. break;
  192. case 3:
  193. _2dClass.Write(offset, argument);
  194. break;
  195. case 4:
  196. _dmaClass.Write(offset, argument);
  197. break;
  198. }
  199. }
  200. else
  201. {
  202. IDeviceState state = subChannel switch
  203. {
  204. 0 => _3dClass,
  205. 3 => _2dClass,
  206. _ => null
  207. };
  208. if (state != null)
  209. {
  210. int macroIndex = (offset >> 1) & MacroIndexMask;
  211. if ((offset & 1) != 0)
  212. {
  213. _fifoClass.MmePushArgument(macroIndex, argument);
  214. }
  215. else
  216. {
  217. _fifoClass.MmeStart(macroIndex, argument);
  218. }
  219. if (isLastCall)
  220. {
  221. _fifoClass.CallMme(macroIndex, state);
  222. _3dClass.PerformDeferredDraws();
  223. }
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// Writes data directly to the state of the specified class.
  229. /// </summary>
  230. /// <param name="classId">ID of the class to write the data into</param>
  231. /// <param name="offset">State offset in bytes</param>
  232. /// <param name="value">Value to be written</param>
  233. public void Write(ClassId classId, int offset, int value)
  234. {
  235. switch (classId)
  236. {
  237. case ClassId.Threed:
  238. _3dClass.Write(offset, value);
  239. break;
  240. case ClassId.Compute:
  241. _computeClass.Write(offset, value);
  242. break;
  243. case ClassId.InlineToMemory:
  244. _i2mClass.Write(offset, value);
  245. break;
  246. case ClassId.Twod:
  247. _2dClass.Write(offset, value);
  248. break;
  249. case ClassId.Dma:
  250. _dmaClass.Write(offset, value);
  251. break;
  252. case ClassId.GPFifo:
  253. _fifoClass.Write(offset, value);
  254. break;
  255. }
  256. }
  257. /// <summary>
  258. /// Sets the shadow ram control value of all sub-channels.
  259. /// </summary>
  260. /// <param name="control">New shadow ram control value</param>
  261. public void SetShadowRamControl(int control)
  262. {
  263. _3dClass.SetShadowRamControl(control);
  264. }
  265. /// <summary>
  266. /// Forces a full host state update by marking all state as modified,
  267. /// and also requests all GPU resources in use to be rebound.
  268. /// </summary>
  269. public void ForceAllDirty()
  270. {
  271. _3dClass.ForceStateDirty();
  272. _channel.BufferManager.Rebind();
  273. _channel.TextureManager.Rebind();
  274. }
  275. /// <summary>
  276. /// Perform any deferred draws.
  277. /// </summary>
  278. public void PerformDeferredDraws()
  279. {
  280. _3dClass.PerformDeferredDraws();
  281. }
  282. }
  283. }