GPFifoClass.cs 8.5 KB

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