GPFifoClass.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 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. /// MME Shadow RAM Control.
  24. /// </summary>
  25. public ShadowRamControl ShadowCtrl { get; private set; }
  26. /// <summary>
  27. /// Creates a new instance of the GPU General Purpose FIFO class.
  28. /// </summary>
  29. /// <param name="context">GPU context</param>
  30. public GPFifoClass(GpuContext context)
  31. {
  32. _context = context;
  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.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.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. _context.Methods.PerformDeferredDraws();
  132. _context.Renderer.Pipeline.Barrier();
  133. }
  134. /// <summary>
  135. /// Send macro code/data to the MME
  136. /// </summary>
  137. /// <param name="argument">Method call argument</param>
  138. public void LoadMmeInstructionRam(int argument)
  139. {
  140. _macroCode[_state.State.LoadMmeInstructionRamPointer++] = argument;
  141. }
  142. /// <summary>
  143. /// Bind a macro index to a position for the MME
  144. /// </summary>
  145. /// <param name="argument">Method call argument</param>
  146. public void LoadMmeStartAddressRam(int argument)
  147. {
  148. _macros[_state.State.LoadMmeStartAddressRamPointer++] = new Macro(argument);
  149. }
  150. /// <summary>
  151. /// Change the shadow RAM setting
  152. /// </summary>
  153. /// <param name="argument">Method call argument</param>
  154. public void SetMmeShadowRamControl(int argument)
  155. {
  156. ShadowCtrl = (ShadowRamControl)argument;
  157. }
  158. /// <summary>
  159. /// Pushes an argument to a macro.
  160. /// </summary>
  161. /// <param name="index">Index of the macro</param>
  162. /// <param name="argument">Argument to be pushed to the macro</param>
  163. public void MmePushArgument(int index, int argument)
  164. {
  165. _macros[index].PushArgument(argument);
  166. }
  167. /// <summary>
  168. /// Prepares a macro for execution.
  169. /// </summary>
  170. /// <param name="index">Index of the macro</param>
  171. /// <param name="argument">Initial argument passed to the macro</param>
  172. public void MmeStart(int index, int argument)
  173. {
  174. _macros[index].StartExecution(argument);
  175. }
  176. /// <summary>
  177. /// Executes a macro.
  178. /// </summary>
  179. /// <param name="index">Index of the macro</param>
  180. /// <param name="state">Current GPU state</param>
  181. public void CallMme(int index, GpuState state)
  182. {
  183. _macros[index].Execute(_macroCode, ShadowCtrl, state);
  184. }
  185. }
  186. }