GPFifoClass.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using Ryujinx.Graphics.Device;
  2. using Ryujinx.Graphics.Gpu.Engine.MME;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. namespace Ryujinx.Graphics.Gpu.Engine.GPFifo
  7. {
  8. /// <summary>
  9. /// Represents a GPU General Purpose FIFO class.
  10. /// </summary>
  11. class GPFifoClass : IDeviceState
  12. {
  13. private readonly GpuContext _context;
  14. private readonly GPFifoProcessor _parent;
  15. private readonly DeviceState<GPFifoClassState> _state;
  16. private int _previousSubChannel;
  17. private bool _createSyncPending;
  18. private const int MacrosCount = 0x80;
  19. // Note: The size of the macro memory is unknown, we just make
  20. // a guess here and use 256kb as the size. Increase if needed.
  21. private const int MacroCodeSize = 256 * 256;
  22. private readonly Macro[] _macros;
  23. private readonly int[] _macroCode;
  24. /// <summary>
  25. /// Creates a new instance of the GPU General Purpose FIFO class.
  26. /// </summary>
  27. /// <param name="context">GPU context</param>
  28. /// <param name="parent">Parent GPU General Purpose FIFO processor</param>
  29. public GPFifoClass(GpuContext context, GPFifoProcessor parent)
  30. {
  31. _context = context;
  32. _parent = parent;
  33. _state = new DeviceState<GPFifoClassState>(new Dictionary<string, RwCallback>
  34. {
  35. { nameof(GPFifoClassState.Semaphored), new RwCallback(Semaphored, null) },
  36. { nameof(GPFifoClassState.Syncpointb), new RwCallback(Syncpointb, null) },
  37. { nameof(GPFifoClassState.WaitForIdle), new RwCallback(WaitForIdle, null) },
  38. { nameof(GPFifoClassState.SetReference), new RwCallback(SetReference, null) },
  39. { nameof(GPFifoClassState.LoadMmeInstructionRam), new RwCallback(LoadMmeInstructionRam, null) },
  40. { nameof(GPFifoClassState.LoadMmeStartAddressRam), new RwCallback(LoadMmeStartAddressRam, null) },
  41. { nameof(GPFifoClassState.SetMmeShadowRamControl), new RwCallback(SetMmeShadowRamControl, null) }
  42. });
  43. _macros = new Macro[MacrosCount];
  44. _macroCode = new int[MacroCodeSize];
  45. }
  46. /// <summary>
  47. /// Create any syncs from WaitForIdle command that are currently pending.
  48. /// </summary>
  49. public void CreatePendingSyncs()
  50. {
  51. if (_createSyncPending)
  52. {
  53. _createSyncPending = false;
  54. _context.CreateHostSyncIfNeeded(false, false);
  55. }
  56. }
  57. /// <summary>
  58. /// Reads data from the class registers.
  59. /// </summary>
  60. /// <param name="offset">Register byte offset</param>
  61. /// <returns>Data at the specified offset</returns>
  62. public int Read(int offset) => _state.Read(offset);
  63. /// <summary>
  64. /// Writes data to the class registers.
  65. /// </summary>
  66. /// <param name="offset">Register byte offset</param>
  67. /// <param name="data">Data to be written</param>
  68. public void Write(int offset, int data) => _state.Write(offset, data);
  69. /// <summary>
  70. /// Writes a GPU counter to guest memory.
  71. /// </summary>
  72. /// <param name="argument">Method call argument</param>
  73. public void Semaphored(int argument)
  74. {
  75. ulong address = ((ulong)_state.State.SemaphorebOffsetLower << 2) |
  76. ((ulong)_state.State.SemaphoreaOffsetUpper << 32);
  77. int value = _state.State.SemaphorecPayload;
  78. SemaphoredOperation operation = _state.State.SemaphoredOperation;
  79. if (_state.State.SemaphoredReleaseSize == SemaphoredReleaseSize.SixteenBytes)
  80. {
  81. _parent.MemoryManager.Write(address + 4, 0);
  82. _parent.MemoryManager.Write(address + 8, _context.GetTimestamp());
  83. }
  84. // TODO: Acquire operations (Wait), interrupts for invalid combinations.
  85. if (operation == SemaphoredOperation.Release)
  86. {
  87. _parent.MemoryManager.Write(address, value);
  88. }
  89. else if (operation == SemaphoredOperation.Reduction)
  90. {
  91. bool signed = _state.State.SemaphoredFormat == SemaphoredFormat.Signed;
  92. int mem = _parent.MemoryManager.Read<int>(address);
  93. switch (_state.State.SemaphoredReduction)
  94. {
  95. case SemaphoredReduction.Min:
  96. value = signed ? Math.Min(mem, value) : (int)Math.Min((uint)mem, (uint)value);
  97. break;
  98. case SemaphoredReduction.Max:
  99. value = signed ? Math.Max(mem, value) : (int)Math.Max((uint)mem, (uint)value);
  100. break;
  101. case SemaphoredReduction.Xor:
  102. value ^= mem;
  103. break;
  104. case SemaphoredReduction.And:
  105. value &= mem;
  106. break;
  107. case SemaphoredReduction.Or:
  108. value |= mem;
  109. break;
  110. case SemaphoredReduction.Add:
  111. value += mem;
  112. break;
  113. case SemaphoredReduction.Inc:
  114. value = (uint)mem < (uint)value ? mem + 1 : 0;
  115. break;
  116. case SemaphoredReduction.Dec:
  117. value = (uint)mem > 0 && (uint)mem <= (uint)value ? mem - 1 : value;
  118. break;
  119. }
  120. _parent.MemoryManager.Write(address, value);
  121. }
  122. }
  123. /// <summary>
  124. /// Apply a fence operation on a syncpoint.
  125. /// </summary>
  126. /// <param name="argument">Method call argument</param>
  127. public void Syncpointb(int argument)
  128. {
  129. SyncpointbOperation operation = _state.State.SyncpointbOperation;
  130. uint syncpointId = (uint)_state.State.SyncpointbSyncptIndex;
  131. if (operation == SyncpointbOperation.Wait)
  132. {
  133. uint threshold = (uint)_state.State.SyncpointaPayload;
  134. _context.Synchronization.WaitOnSyncpoint(syncpointId, threshold, Timeout.InfiniteTimeSpan);
  135. }
  136. else if (operation == SyncpointbOperation.Incr)
  137. {
  138. _context.CreateHostSyncIfNeeded(true, true);
  139. _context.Synchronization.IncrementSyncpoint(syncpointId);
  140. }
  141. _context.AdvanceSequence();
  142. }
  143. /// <summary>
  144. /// Waits for the GPU to be idle.
  145. /// </summary>
  146. /// <param name="argument">Method call argument</param>
  147. public void WaitForIdle(int argument)
  148. {
  149. _parent.PerformDeferredDraws();
  150. _context.Renderer.Pipeline.Barrier();
  151. _createSyncPending = true;
  152. }
  153. /// <summary>
  154. /// Used as an indirect data barrier on NVN. When used, access to previously written data must be coherent.
  155. /// </summary>
  156. /// <param name="argument">Method call argument</param>
  157. public void SetReference(int argument)
  158. {
  159. _context.Renderer.Pipeline.CommandBufferBarrier();
  160. _context.CreateHostSyncIfNeeded(false, true);
  161. }
  162. /// <summary>
  163. /// Sends macro code/data to the MME.
  164. /// </summary>
  165. /// <param name="argument">Method call argument</param>
  166. public void LoadMmeInstructionRam(int argument)
  167. {
  168. _macroCode[_state.State.LoadMmeInstructionRamPointer++] = argument;
  169. }
  170. /// <summary>
  171. /// Binds a macro index to a position for the MME
  172. /// </summary>
  173. /// <param name="argument">Method call argument</param>
  174. public void LoadMmeStartAddressRam(int argument)
  175. {
  176. _macros[_state.State.LoadMmeStartAddressRamPointer++] = new Macro(argument);
  177. }
  178. /// <summary>
  179. /// Changes the shadow RAM control.
  180. /// </summary>
  181. /// <param name="argument">Method call argument</param>
  182. public void SetMmeShadowRamControl(int argument)
  183. {
  184. _parent.SetShadowRamControl(argument);
  185. }
  186. /// <summary>
  187. /// Pushes an argument to a macro.
  188. /// </summary>
  189. /// <param name="index">Index of the macro</param>
  190. /// <param name="gpuVa">GPU virtual address where the command word is located</param>
  191. /// <param name="argument">Argument to be pushed to the macro</param>
  192. public void MmePushArgument(int index, ulong gpuVa, int argument)
  193. {
  194. _macros[index].PushArgument(gpuVa, argument);
  195. }
  196. /// <summary>
  197. /// Prepares a macro for execution.
  198. /// </summary>
  199. /// <param name="index">Index of the macro</param>
  200. /// <param name="argument">Initial argument passed to the macro</param>
  201. public void MmeStart(int index, int argument)
  202. {
  203. _macros[index].StartExecution(_context, _parent, _macroCode, argument);
  204. }
  205. /// <summary>
  206. /// Executes a macro.
  207. /// </summary>
  208. /// <param name="index">Index of the macro</param>
  209. /// <param name="state">Current GPU state</param>
  210. public void CallMme(int index, IDeviceState state)
  211. {
  212. _macros[index].Execute(_macroCode, state);
  213. }
  214. }
  215. }