GPFifoClass.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.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. _context.MemoryManager.Write(address, value);
  70. }
  71. else if (operation == SemaphoredOperation.Reduction)
  72. {
  73. bool signed = _state.State.SemaphoredFormat == SemaphoredFormat.Signed;
  74. int mem = _context.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. _context.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.Synchronization.IncrementSyncpoint(syncpointId);
  121. }
  122. _context.AdvanceSequence();
  123. }
  124. /// <summary>
  125. /// Waits for the GPU to be idle.
  126. /// </summary>
  127. /// <param name="argument">Method call argument</param>
  128. public void WaitForIdle(int argument)
  129. {
  130. _context.Methods.PerformDeferredDraws();
  131. _context.Renderer.Pipeline.Barrier();
  132. }
  133. /// <summary>
  134. /// Sends macro code/data to the MME.
  135. /// </summary>
  136. /// <param name="argument">Method call argument</param>
  137. public void LoadMmeInstructionRam(int argument)
  138. {
  139. _macroCode[_state.State.LoadMmeInstructionRamPointer++] = argument;
  140. }
  141. /// <summary>
  142. /// Binds a macro index to a position for the MME
  143. /// </summary>
  144. /// <param name="argument">Method call argument</param>
  145. public void LoadMmeStartAddressRam(int argument)
  146. {
  147. _macros[_state.State.LoadMmeStartAddressRamPointer++] = new Macro(argument);
  148. }
  149. /// <summary>
  150. /// Changes the shadow RAM control.
  151. /// </summary>
  152. /// <param name="argument">Method call argument</param>
  153. public void SetMmeShadowRamControl(int argument)
  154. {
  155. _parent.SetShadowRamControl((ShadowRamControl)argument);
  156. }
  157. /// <summary>
  158. /// Pushes an argument to a macro.
  159. /// </summary>
  160. /// <param name="index">Index of the macro</param>
  161. /// <param name="argument">Argument to be pushed to the macro</param>
  162. public void MmePushArgument(int index, int argument)
  163. {
  164. _macros[index].PushArgument(argument);
  165. }
  166. /// <summary>
  167. /// Prepares a macro for execution.
  168. /// </summary>
  169. /// <param name="index">Index of the macro</param>
  170. /// <param name="argument">Initial argument passed to the macro</param>
  171. public void MmeStart(int index, int argument)
  172. {
  173. _macros[index].StartExecution(argument);
  174. }
  175. /// <summary>
  176. /// Executes a macro.
  177. /// </summary>
  178. /// <param name="index">Index of the macro</param>
  179. /// <param name="state">Current GPU state</param>
  180. public void CallMme(int index, GpuState state)
  181. {
  182. _macros[index].Execute(_macroCode, state);
  183. }
  184. }
  185. }