GPFifoProcessor.cs 12 KB

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