GPFifoProcessor.cs 9.5 KB

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