GPFifoClass.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. // TODO: Acquire operations (Wait), interrupts for invalid combinations.
  67. if (operation == SemaphoredOperation.Release)
  68. {
  69. _parent.MemoryManager.Write(address, value);
  70. }
  71. else if (operation == SemaphoredOperation.Reduction)
  72. {
  73. bool signed = _state.State.SemaphoredFormat == SemaphoredFormat.Signed;
  74. int mem = _parent.MemoryManager.Read<int>(address);
  75. switch (_state.State.SemaphoredReduction)
  76. {
  77. case SemaphoredReduction.Min:
  78. value = signed ? Math.Min(mem, value) : (int)Math.Min((uint)mem, (uint)value);
  79. break;
  80. case SemaphoredReduction.Max:
  81. value = signed ? Math.Max(mem, value) : (int)Math.Max((uint)mem, (uint)value);
  82. break;
  83. case SemaphoredReduction.Xor:
  84. value ^= mem;
  85. break;
  86. case SemaphoredReduction.And:
  87. value &= mem;
  88. break;
  89. case SemaphoredReduction.Or:
  90. value |= mem;
  91. break;
  92. case SemaphoredReduction.Add:
  93. value += mem;
  94. break;
  95. case SemaphoredReduction.Inc:
  96. value = (uint)mem < (uint)value ? mem + 1 : 0;
  97. break;
  98. case SemaphoredReduction.Dec:
  99. value = (uint)mem > 0 && (uint)mem <= (uint)value ? mem - 1 : value;
  100. break;
  101. }
  102. _parent.MemoryManager.Write(address, value);
  103. }
  104. }
  105. /// <summary>
  106. /// Apply a fence operation on a syncpoint.
  107. /// </summary>
  108. /// <param name="argument">Method call argument</param>
  109. public void Syncpointb(int argument)
  110. {
  111. SyncpointbOperation operation = _state.State.SyncpointbOperation;
  112. uint syncpointId = (uint)_state.State.SyncpointbSyncptIndex;
  113. if (operation == SyncpointbOperation.Wait)
  114. {
  115. uint threshold = (uint)_state.State.SyncpointaPayload;
  116. _context.Synchronization.WaitOnSyncpoint(syncpointId, threshold, Timeout.InfiniteTimeSpan);
  117. }
  118. else if (operation == SyncpointbOperation.Incr)
  119. {
  120. _context.CreateHostSyncIfNeeded();
  121. _context.Synchronization.IncrementSyncpoint(syncpointId);
  122. }
  123. _context.AdvanceSequence();
  124. }
  125. /// <summary>
  126. /// Waits for the GPU to be idle.
  127. /// </summary>
  128. /// <param name="argument">Method call argument</param>
  129. public void WaitForIdle(int argument)
  130. {
  131. _parent.PerformDeferredDraws();
  132. _context.Renderer.Pipeline.Barrier();
  133. _context.CreateHostSyncIfNeeded();
  134. }
  135. /// <summary>
  136. /// Used as an indirect data barrier on NVN. When used, access to previously written data must be coherent.
  137. /// </summary>
  138. /// <param name="argument">Method call argument</param>
  139. public void SetReference(int argument)
  140. {
  141. _context.CreateHostSyncIfNeeded();
  142. }
  143. /// <summary>
  144. /// Sends macro code/data to the MME.
  145. /// </summary>
  146. /// <param name="argument">Method call argument</param>
  147. public void LoadMmeInstructionRam(int argument)
  148. {
  149. _macroCode[_state.State.LoadMmeInstructionRamPointer++] = argument;
  150. }
  151. /// <summary>
  152. /// Binds a macro index to a position for the MME
  153. /// </summary>
  154. /// <param name="argument">Method call argument</param>
  155. public void LoadMmeStartAddressRam(int argument)
  156. {
  157. _macros[_state.State.LoadMmeStartAddressRamPointer++] = new Macro(argument);
  158. }
  159. /// <summary>
  160. /// Changes the shadow RAM control.
  161. /// </summary>
  162. /// <param name="argument">Method call argument</param>
  163. public void SetMmeShadowRamControl(int argument)
  164. {
  165. _parent.SetShadowRamControl(argument);
  166. }
  167. /// <summary>
  168. /// Pushes an argument to a macro.
  169. /// </summary>
  170. /// <param name="index">Index of the macro</param>
  171. /// <param name="argument">Argument to be pushed to the macro</param>
  172. public void MmePushArgument(int index, int argument)
  173. {
  174. _macros[index].PushArgument(argument);
  175. }
  176. /// <summary>
  177. /// Prepares a macro for execution.
  178. /// </summary>
  179. /// <param name="index">Index of the macro</param>
  180. /// <param name="argument">Initial argument passed to the macro</param>
  181. public void MmeStart(int index, int argument)
  182. {
  183. _macros[index].StartExecution(argument);
  184. }
  185. /// <summary>
  186. /// Executes a macro.
  187. /// </summary>
  188. /// <param name="index">Index of the macro</param>
  189. /// <param name="state">Current GPU state</param>
  190. public void CallMme(int index, IDeviceState state)
  191. {
  192. _macros[index].Execute(_macroCode, state);
  193. }
  194. }
  195. }