GpuContext.cs 16 KB

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