GPFifoClass.cs 8.9 KB

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