GpuContext.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Engine.GPFifo;
  4. using Ryujinx.Graphics.Gpu.Memory;
  5. using Ryujinx.Graphics.Gpu.Shader;
  6. using Ryujinx.Graphics.Gpu.Synchronization;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Threading;
  11. namespace Ryujinx.Graphics.Gpu
  12. {
  13. /// <summary>
  14. /// GPU emulation context.
  15. /// </summary>
  16. public sealed class GpuContext : IDisposable
  17. {
  18. private const int NsToTicksFractionNumerator = 384;
  19. private const int NsToTicksFractionDenominator = 625;
  20. /// <summary>
  21. /// Event signaled when the host emulation context is ready to be used by the gpu context.
  22. /// </summary>
  23. public ManualResetEvent HostInitalized { get; }
  24. /// <summary>
  25. /// Host renderer.
  26. /// </summary>
  27. public IRenderer Renderer { get; }
  28. /// <summary>
  29. /// GPU General Purpose FIFO queue.
  30. /// </summary>
  31. public GPFifoDevice GPFifo { get; }
  32. /// <summary>
  33. /// GPU synchronization manager.
  34. /// </summary>
  35. public SynchronizationManager Synchronization { get; }
  36. /// <summary>
  37. /// Presentation window.
  38. /// </summary>
  39. public Window Window { get; }
  40. /// <summary>
  41. /// Internal sequence number, used to avoid needless resource data updates
  42. /// in the middle of a command buffer before synchronizations.
  43. /// </summary>
  44. internal int SequenceNumber { get; private set; }
  45. /// <summary>
  46. /// Internal sync number, used to denote points at which host synchronization can be requested.
  47. /// </summary>
  48. internal ulong SyncNumber { get; private set; }
  49. /// <summary>
  50. /// Actions to be performed when a CPU waiting syncpoint or barrier is triggered.
  51. /// If there are more than 0 items when this happens, a host sync object will be generated for the given <see cref="SyncNumber"/>,
  52. /// and the SyncNumber will be incremented.
  53. /// </summary>
  54. internal List<ISyncActionHandler> SyncActions { get; }
  55. /// <summary>
  56. /// Actions to be performed when a CPU waiting syncpoint is triggered.
  57. /// If there are more than 0 items when this happens, a host sync object will be generated for the given <see cref="SyncNumber"/>,
  58. /// and the SyncNumber will be incremented.
  59. /// </summary>
  60. internal List<ISyncActionHandler> SyncpointActions { get; }
  61. /// <summary>
  62. /// Buffer migrations that are currently in-flight. These are checked whenever sync is created to determine if buffer migration
  63. /// copies have completed on the GPU, and their data can be freed.
  64. /// </summary>
  65. internal List<BufferMigration> BufferMigrations { get; }
  66. /// <summary>
  67. /// Queue with deferred actions that must run on the render thread.
  68. /// </summary>
  69. internal Queue<Action> DeferredActions { get; }
  70. /// <summary>
  71. /// Registry with physical memories that can be used with this GPU context, keyed by owner process ID.
  72. /// </summary>
  73. internal ConcurrentDictionary<ulong, PhysicalMemory> PhysicalMemoryRegistry { get; }
  74. /// <summary>
  75. /// Support buffer updater.
  76. /// </summary>
  77. internal SupportBufferUpdater SupportBufferUpdater { get; }
  78. /// <summary>
  79. /// Host hardware capabilities.
  80. /// </summary>
  81. internal Capabilities Capabilities;
  82. /// <summary>
  83. /// Event for signalling shader cache loading progress.
  84. /// </summary>
  85. public event Action<ShaderCacheState, int, int> ShaderCacheStateChanged;
  86. private Thread _gpuThread;
  87. private bool _pendingSync;
  88. private long _modifiedSequence;
  89. private readonly ulong _firstTimestamp;
  90. private readonly ManualResetEvent _gpuReadyEvent;
  91. /// <summary>
  92. /// Creates a new instance of the GPU emulation context.
  93. /// </summary>
  94. /// <param name="renderer">Host renderer</param>
  95. public GpuContext(IRenderer renderer)
  96. {
  97. Renderer = renderer;
  98. GPFifo = new GPFifoDevice(this);
  99. Synchronization = new SynchronizationManager();
  100. Window = new Window(this);
  101. HostInitalized = new ManualResetEvent(false);
  102. _gpuReadyEvent = new ManualResetEvent(false);
  103. SyncActions = new List<ISyncActionHandler>();
  104. SyncpointActions = new List<ISyncActionHandler>();
  105. BufferMigrations = new List<BufferMigration>();
  106. DeferredActions = new Queue<Action>();
  107. PhysicalMemoryRegistry = new ConcurrentDictionary<ulong, PhysicalMemory>();
  108. SupportBufferUpdater = new SupportBufferUpdater(renderer);
  109. _firstTimestamp = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
  110. }
  111. /// <summary>
  112. /// Creates a new GPU channel.
  113. /// </summary>
  114. /// <returns>The GPU channel</returns>
  115. public GpuChannel CreateChannel()
  116. {
  117. return new GpuChannel(this);
  118. }
  119. /// <summary>
  120. /// Creates a new GPU memory manager.
  121. /// </summary>
  122. /// <param name="pid">ID of the process that owns the memory manager</param>
  123. /// <returns>The memory manager</returns>
  124. /// <exception cref="ArgumentException">Thrown when <paramref name="pid"/> is invalid</exception>
  125. public MemoryManager CreateMemoryManager(ulong pid)
  126. {
  127. if (!PhysicalMemoryRegistry.TryGetValue(pid, out var physicalMemory))
  128. {
  129. throw new ArgumentException("The PID is invalid or the process was not registered", nameof(pid));
  130. }
  131. return new MemoryManager(physicalMemory);
  132. }
  133. /// <summary>
  134. /// Registers virtual memory used by a process for GPU memory access, caching and read/write tracking.
  135. /// </summary>
  136. /// <param name="pid">ID of the process that owns <paramref name="cpuMemory"/></param>
  137. /// <param name="cpuMemory">Virtual memory owned by the process</param>
  138. /// <exception cref="ArgumentException">Thrown if <paramref name="pid"/> was already registered</exception>
  139. public void RegisterProcess(ulong pid, Cpu.IVirtualMemoryManagerTracked cpuMemory)
  140. {
  141. var physicalMemory = new PhysicalMemory(this, cpuMemory);
  142. if (!PhysicalMemoryRegistry.TryAdd(pid, physicalMemory))
  143. {
  144. throw new ArgumentException("The PID was already registered", nameof(pid));
  145. }
  146. physicalMemory.ShaderCache.ShaderCacheStateChanged += ShaderCacheStateUpdate;
  147. }
  148. /// <summary>
  149. /// Unregisters a process, indicating that its memory will no longer be used, and that caches can be freed.
  150. /// </summary>
  151. /// <param name="pid">ID of the process</param>
  152. public void UnregisterProcess(ulong pid)
  153. {
  154. if (PhysicalMemoryRegistry.TryRemove(pid, out var physicalMemory))
  155. {
  156. physicalMemory.ShaderCache.ShaderCacheStateChanged -= ShaderCacheStateUpdate;
  157. physicalMemory.Dispose();
  158. }
  159. }
  160. /// <summary>
  161. /// Converts a nanoseconds timestamp value to Maxwell time ticks.
  162. /// </summary>
  163. /// <remarks>
  164. /// The frequency is 614400000 Hz.
  165. /// </remarks>
  166. /// <param name="nanoseconds">Timestamp in nanoseconds</param>
  167. /// <returns>Maxwell ticks</returns>
  168. private static ulong ConvertNanosecondsToTicks(ulong nanoseconds)
  169. {
  170. // We need to divide first to avoid overflows.
  171. // We fix up the result later by calculating the difference and adding
  172. // that to the result.
  173. ulong divided = nanoseconds / NsToTicksFractionDenominator;
  174. ulong rounded = divided * NsToTicksFractionDenominator;
  175. ulong errorBias = (nanoseconds - rounded) * NsToTicksFractionNumerator / NsToTicksFractionDenominator;
  176. return divided * NsToTicksFractionNumerator + errorBias;
  177. }
  178. /// <summary>
  179. /// Gets a sequence number for resource modification ordering. This increments on each call.
  180. /// </summary>
  181. /// <returns>A sequence number for resource modification ordering</returns>
  182. internal long GetModifiedSequence()
  183. {
  184. return _modifiedSequence++;
  185. }
  186. /// <summary>
  187. /// Gets the value of the GPU timer.
  188. /// </summary>
  189. /// <returns>The current GPU timestamp</returns>
  190. internal ulong GetTimestamp()
  191. {
  192. // Guest timestamp will start at 0, instead of host value.
  193. ulong ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds) - _firstTimestamp;
  194. if (GraphicsConfig.FastGpuTime)
  195. {
  196. // Divide by some amount to report time as if operations were performed faster than they really are.
  197. // This can prevent some games from switching to a lower resolution because rendering is too slow.
  198. ticks /= 256;
  199. }
  200. return ticks;
  201. }
  202. /// <summary>
  203. /// Shader cache state update handler.
  204. /// </summary>
  205. /// <param name="state">Current state of the shader cache load process</param>
  206. /// <param name="current">Number of the current shader being processed</param>
  207. /// <param name="total">Total number of shaders to process</param>
  208. private void ShaderCacheStateUpdate(ShaderCacheState state, int current, int total)
  209. {
  210. ShaderCacheStateChanged?.Invoke(state, current, total);
  211. }
  212. /// <summary>
  213. /// Initialize the GPU shader cache.
  214. /// </summary>
  215. public void InitializeShaderCache(CancellationToken cancellationToken)
  216. {
  217. HostInitalized.WaitOne();
  218. foreach (var physicalMemory in PhysicalMemoryRegistry.Values)
  219. {
  220. physicalMemory.ShaderCache.Initialize(cancellationToken);
  221. }
  222. _gpuReadyEvent.Set();
  223. }
  224. /// <summary>
  225. /// Waits until the GPU is ready to receive commands.
  226. /// </summary>
  227. public void WaitUntilGpuReady()
  228. {
  229. _gpuReadyEvent.WaitOne();
  230. }
  231. /// <summary>
  232. /// Sets the current thread as the main GPU thread.
  233. /// </summary>
  234. public void SetGpuThread()
  235. {
  236. _gpuThread = Thread.CurrentThread;
  237. Capabilities = Renderer.GetCapabilities();
  238. }
  239. /// <summary>
  240. /// Checks if the current thread is the GPU thread.
  241. /// </summary>
  242. /// <returns>True if the thread is the GPU thread, false otherwise</returns>
  243. public bool IsGpuThread()
  244. {
  245. return _gpuThread == Thread.CurrentThread;
  246. }
  247. /// <summary>
  248. /// Processes the queue of shaders that must save their binaries to the disk cache.
  249. /// </summary>
  250. public void ProcessShaderCacheQueue()
  251. {
  252. foreach (var physicalMemory in PhysicalMemoryRegistry.Values)
  253. {
  254. physicalMemory.ShaderCache.ProcessShaderCacheQueue();
  255. }
  256. }
  257. /// <summary>
  258. /// Advances internal sequence number.
  259. /// This forces the update of any modified GPU resource.
  260. /// </summary>
  261. internal void AdvanceSequence()
  262. {
  263. SequenceNumber++;
  264. }
  265. /// <summary>
  266. /// Registers a buffer migration. These are checked to see if they can be disposed when the sync number increases,
  267. /// and the migration copy has completed.
  268. /// </summary>
  269. /// <param name="migration">The buffer migration</param>
  270. internal void RegisterBufferMigration(BufferMigration migration)
  271. {
  272. BufferMigrations.Add(migration);
  273. _pendingSync = true;
  274. }
  275. /// <summary>
  276. /// Registers an action to be performed the next time a syncpoint is incremented.
  277. /// This will also ensure a host sync object is created, and <see cref="SyncNumber"/> is incremented.
  278. /// </summary>
  279. /// <param name="action">The resource with action to be performed on sync object creation</param>
  280. /// <param name="syncpointOnly">True if the sync action should only run when syncpoints are incremented</param>
  281. internal void RegisterSyncAction(ISyncActionHandler action, bool syncpointOnly = false)
  282. {
  283. if (syncpointOnly)
  284. {
  285. SyncpointActions.Add(action);
  286. }
  287. else
  288. {
  289. SyncActions.Add(action);
  290. _pendingSync = true;
  291. }
  292. }
  293. /// <summary>
  294. /// Creates a host sync object if there are any pending sync actions. The actions will then be called.
  295. /// If no actions are present, a host sync object is not created.
  296. /// </summary>
  297. /// <param name="flags">Modifiers for how host sync should be created</param>
  298. internal void CreateHostSyncIfNeeded(HostSyncFlags flags)
  299. {
  300. bool syncpoint = flags.HasFlag(HostSyncFlags.Syncpoint);
  301. bool strict = flags.HasFlag(HostSyncFlags.Strict);
  302. bool force = flags.HasFlag(HostSyncFlags.Force);
  303. if (BufferMigrations.Count > 0)
  304. {
  305. ulong currentSyncNumber = Renderer.GetCurrentSync();
  306. for (int i = 0; i < BufferMigrations.Count; i++)
  307. {
  308. BufferMigration migration = BufferMigrations[i];
  309. long diff = (long)(currentSyncNumber - migration.SyncNumber);
  310. if (diff >= 0)
  311. {
  312. migration.Dispose();
  313. BufferMigrations.RemoveAt(i--);
  314. }
  315. }
  316. }
  317. if (force || _pendingSync || (syncpoint && SyncpointActions.Count > 0))
  318. {
  319. Renderer.CreateSync(SyncNumber, strict);
  320. SyncActions.ForEach(action => action.SyncPreAction(syncpoint));
  321. SyncpointActions.ForEach(action => action.SyncPreAction(syncpoint));
  322. SyncNumber++;
  323. SyncActions.RemoveAll(action => action.SyncAction(syncpoint));
  324. SyncpointActions.RemoveAll(action => action.SyncAction(syncpoint));
  325. }
  326. _pendingSync = false;
  327. }
  328. /// <summary>
  329. /// Performs deferred actions.
  330. /// This is useful for actions that must run on the render thread, such as resource disposal.
  331. /// </summary>
  332. internal void RunDeferredActions()
  333. {
  334. while (DeferredActions.TryDequeue(out Action action))
  335. {
  336. action();
  337. }
  338. }
  339. /// <summary>
  340. /// Disposes all GPU resources currently cached.
  341. /// It's an error to push any GPU commands after disposal.
  342. /// Additionally, the GPU commands FIFO must be empty for disposal,
  343. /// and processing of all commands must have finished.
  344. /// </summary>
  345. public void Dispose()
  346. {
  347. GPFifo.Dispose();
  348. HostInitalized.Dispose();
  349. _gpuReadyEvent.Dispose();
  350. // Has to be disposed before processing deferred actions, as it will produce some.
  351. foreach (var physicalMemory in PhysicalMemoryRegistry.Values)
  352. {
  353. physicalMemory.Dispose();
  354. }
  355. SupportBufferUpdater.Dispose();
  356. PhysicalMemoryRegistry.Clear();
  357. RunDeferredActions();
  358. Renderer.Dispose();
  359. }
  360. }
  361. }