GPFifoProcessor.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Ryujinx.Graphics.Gpu.State;
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. namespace Ryujinx.Graphics.Gpu.Engine.GPFifo
  5. {
  6. /// <summary>
  7. /// Represents a GPU General Purpose FIFO command processor.
  8. /// </summary>
  9. class GPFifoProcessor
  10. {
  11. private const int MacrosCount = 0x80;
  12. private const int MacroIndexMask = MacrosCount - 1;
  13. private readonly GpuContext _context;
  14. /// <summary>
  15. /// Internal GPFIFO state.
  16. /// </summary>
  17. private struct DmaState
  18. {
  19. public int Method;
  20. public int SubChannel;
  21. public int MethodCount;
  22. public bool NonIncrementing;
  23. public bool IncrementOnce;
  24. }
  25. private DmaState _state;
  26. private readonly GpuState[] _subChannels;
  27. private readonly GPFifoClass _fifoClass;
  28. /// <summary>
  29. /// Creates a new instance of the GPU General Purpose FIFO command processor.
  30. /// </summary>
  31. /// <param name="context">GPU context</param>
  32. public GPFifoProcessor(GpuContext context)
  33. {
  34. _context = context;
  35. _fifoClass = new GPFifoClass(context, this);
  36. _subChannels = new GpuState[8];
  37. for (int index = 0; index < _subChannels.Length; index++)
  38. {
  39. _subChannels[index] = new GpuState();
  40. _context.Methods.RegisterCallbacks(_subChannels[index]);
  41. }
  42. }
  43. /// <summary>
  44. /// Processes a command buffer.
  45. /// </summary>
  46. /// <param name="commandBuffer">Command buffer</param>
  47. public void Process(ReadOnlySpan<int> commandBuffer)
  48. {
  49. for (int index = 0; index < commandBuffer.Length; index++)
  50. {
  51. int command = commandBuffer[index];
  52. if (_state.MethodCount != 0)
  53. {
  54. Send(new MethodParams(_state.Method, command, _state.SubChannel, _state.MethodCount));
  55. if (!_state.NonIncrementing)
  56. {
  57. _state.Method++;
  58. }
  59. if (_state.IncrementOnce)
  60. {
  61. _state.NonIncrementing = true;
  62. }
  63. _state.MethodCount--;
  64. }
  65. else
  66. {
  67. CompressedMethod meth = Unsafe.As<int, CompressedMethod>(ref command);
  68. if (TryFastUniformBufferUpdate(meth, commandBuffer, index))
  69. {
  70. index += meth.MethodCount;
  71. continue;
  72. }
  73. switch (meth.SecOp)
  74. {
  75. case SecOp.IncMethod:
  76. case SecOp.NonIncMethod:
  77. case SecOp.OneInc:
  78. _state.Method = meth.MethodAddress;
  79. _state.SubChannel = meth.MethodSubchannel;
  80. _state.MethodCount = meth.MethodCount;
  81. _state.IncrementOnce = meth.SecOp == SecOp.OneInc;
  82. _state.NonIncrementing = meth.SecOp == SecOp.NonIncMethod;
  83. break;
  84. case SecOp.ImmdDataMethod:
  85. Send(new MethodParams(meth.MethodAddress, meth.ImmdData, meth.MethodSubchannel, 1));
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// Tries to perform a fast constant buffer data update.
  93. /// If successful, all data will be copied at once, and <see cref="CompressedMethod.MethodCount"/> + 1
  94. /// command buffer entries will be consumed.
  95. /// </summary>
  96. /// <param name="meth">Compressed method to be checked</param>
  97. /// <param name="commandBuffer">Command buffer where <paramref name="meth"/> is contained</param>
  98. /// <param name="offset">Offset at <paramref name="commandBuffer"/> where <paramref name="meth"/> is located</param>
  99. /// <returns>True if the fast copy was successful, false otherwise</returns>
  100. private bool TryFastUniformBufferUpdate(CompressedMethod meth, ReadOnlySpan<int> commandBuffer, int offset)
  101. {
  102. int availableCount = commandBuffer.Length - offset;
  103. if (meth.MethodCount < availableCount &&
  104. meth.SecOp == SecOp.NonIncMethod &&
  105. meth.MethodAddress == (int)MethodOffset.UniformBufferUpdateData)
  106. {
  107. GpuState state = _subChannels[meth.MethodSubchannel];
  108. _context.Methods.UniformBufferUpdate(state, commandBuffer.Slice(offset + 1, meth.MethodCount));
  109. return true;
  110. }
  111. return false;
  112. }
  113. /// <summary>
  114. /// Sends a uncompressed method for processing by the graphics pipeline.
  115. /// </summary>
  116. /// <param name="meth">Method to be processed</param>
  117. private void Send(MethodParams meth)
  118. {
  119. if ((MethodOffset)meth.Method == MethodOffset.BindChannel)
  120. {
  121. _subChannels[meth.SubChannel] = new GpuState();
  122. _context.Methods.RegisterCallbacks(_subChannels[meth.SubChannel]);
  123. }
  124. else if (meth.Method < 0x60)
  125. {
  126. // TODO: check if macros are shared between subchannels or not. For now let's assume they are.
  127. _fifoClass.Write(meth.Method * 4, meth.Argument);
  128. }
  129. else if (meth.Method < 0xe00)
  130. {
  131. _subChannels[meth.SubChannel].CallMethod(meth);
  132. }
  133. else
  134. {
  135. int macroIndex = (meth.Method >> 1) & MacroIndexMask;
  136. if ((meth.Method & 1) != 0)
  137. {
  138. _fifoClass.MmePushArgument(macroIndex, meth.Argument);
  139. }
  140. else
  141. {
  142. _fifoClass.MmeStart(macroIndex, meth.Argument);
  143. }
  144. if (meth.IsLastCall)
  145. {
  146. _fifoClass.CallMme(macroIndex, _subChannels[meth.SubChannel]);
  147. _context.Methods.PerformDeferredDraws();
  148. }
  149. }
  150. }
  151. /// <summary>
  152. /// Sets the shadow ram control value of all sub-channels.
  153. /// </summary>
  154. /// <param name="control">New shadow ram control value</param>
  155. public void SetShadowRamControl(ShadowRamControl control)
  156. {
  157. for (int i = 0; i < _subChannels.Length; i++)
  158. {
  159. _subChannels[i].ShadowRamControl = control;
  160. }
  161. }
  162. }
  163. }