GpuContext.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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<Action> 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<Action> 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. /// Host hardware capabilities.
  76. /// </summary>
  77. internal Capabilities Capabilities;
  78. /// <summary>
  79. /// Event for signalling shader cache loading progress.
  80. /// </summary>
  81. public event Action<ShaderCacheState, int, int> ShaderCacheStateChanged;
  82. private Thread _gpuThread;
  83. private bool _pendingSync;
  84. /// <summary>
  85. /// Creates a new instance of the GPU emulation context.
  86. /// </summary>
  87. /// <param name="renderer">Host renderer</param>
  88. public GpuContext(IRenderer renderer)
  89. {
  90. Renderer = renderer;
  91. GPFifo = new GPFifoDevice(this);
  92. Synchronization = new SynchronizationManager();
  93. Window = new Window(this);
  94. HostInitalized = new ManualResetEvent(false);
  95. SyncActions = new List<Action>();
  96. SyncpointActions = new List<Action>();
  97. BufferMigrations = new List<BufferMigration>();
  98. DeferredActions = new Queue<Action>();
  99. PhysicalMemoryRegistry = new ConcurrentDictionary<ulong, PhysicalMemory>();
  100. }
  101. /// <summary>
  102. /// Creates a new GPU channel.
  103. /// </summary>
  104. /// <returns>The GPU channel</returns>
  105. public GpuChannel CreateChannel()
  106. {
  107. return new GpuChannel(this);
  108. }
  109. /// <summary>
  110. /// Creates a new GPU memory manager.
  111. /// </summary>
  112. /// <param name="pid">ID of the process that owns the memory manager</param>
  113. /// <returns>The memory manager</returns>
  114. /// <exception cref="ArgumentException">Thrown when <paramref name="pid"/> is invalid</exception>
  115. public MemoryManager CreateMemoryManager(ulong pid)
  116. {
  117. if (!PhysicalMemoryRegistry.TryGetValue(pid, out var physicalMemory))
  118. {
  119. throw new ArgumentException("The PID is invalid or the process was not registered", nameof(pid));
  120. }
  121. return new MemoryManager(physicalMemory);
  122. }
  123. /// <summary>
  124. /// Registers virtual memory used by a process for GPU memory access, caching and read/write tracking.
  125. /// </summary>
  126. /// <param name="pid">ID of the process that owns <paramref name="cpuMemory"/></param>
  127. /// <param name="cpuMemory">Virtual memory owned by the process</param>
  128. /// <exception cref="ArgumentException">Thrown if <paramref name="pid"/> was already registered</exception>
  129. public void RegisterProcess(ulong pid, Cpu.IVirtualMemoryManagerTracked cpuMemory)
  130. {
  131. var physicalMemory = new PhysicalMemory(this, cpuMemory);
  132. if (!PhysicalMemoryRegistry.TryAdd(pid, physicalMemory))
  133. {
  134. throw new ArgumentException("The PID was already registered", nameof(pid));
  135. }
  136. physicalMemory.ShaderCache.ShaderCacheStateChanged += ShaderCacheStateUpdate;
  137. }
  138. /// <summary>
  139. /// Unregisters a process, indicating that its memory will no longer be used, and that caches can be freed.
  140. /// </summary>
  141. /// <param name="pid">ID of the process</param>
  142. public void UnregisterProcess(ulong pid)
  143. {
  144. if (PhysicalMemoryRegistry.TryRemove(pid, out var physicalMemory))
  145. {
  146. physicalMemory.ShaderCache.ShaderCacheStateChanged -= ShaderCacheStateUpdate;
  147. physicalMemory.Dispose();
  148. }
  149. }
  150. /// <summary>
  151. /// Converts a nanoseconds timestamp value to Maxwell time ticks.
  152. /// </summary>
  153. /// <remarks>
  154. /// The frequency is 614400000 Hz.
  155. /// </remarks>
  156. /// <param name="nanoseconds">Timestamp in nanoseconds</param>
  157. /// <returns>Maxwell ticks</returns>
  158. private static ulong ConvertNanosecondsToTicks(ulong nanoseconds)
  159. {
  160. // We need to divide first to avoid overflows.
  161. // We fix up the result later by calculating the difference and adding
  162. // that to the result.
  163. ulong divided = nanoseconds / NsToTicksFractionDenominator;
  164. ulong rounded = divided * NsToTicksFractionDenominator;
  165. ulong errorBias = (nanoseconds - rounded) * NsToTicksFractionNumerator / NsToTicksFractionDenominator;
  166. return divided * NsToTicksFractionNumerator + errorBias;
  167. }
  168. /// <summary>
  169. /// Gets the value of the GPU timer.
  170. /// </summary>
  171. /// <returns>The current GPU timestamp</returns>
  172. public ulong GetTimestamp()
  173. {
  174. ulong ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
  175. if (GraphicsConfig.FastGpuTime)
  176. {
  177. // Divide by some amount to report time as if operations were performed faster than they really are.
  178. // This can prevent some games from switching to a lower resolution because rendering is too slow.
  179. ticks /= 256;
  180. }
  181. return ticks;
  182. }
  183. /// <summary>
  184. /// Shader cache state update handler.
  185. /// </summary>
  186. /// <param name="state">Current state of the shader cache load process</param>
  187. /// <param name="current">Number of the current shader being processed</param>
  188. /// <param name="total">Total number of shaders to process</param>
  189. private void ShaderCacheStateUpdate(ShaderCacheState state, int current, int total)
  190. {
  191. ShaderCacheStateChanged?.Invoke(state, current, total);
  192. }
  193. /// <summary>
  194. /// Initialize the GPU shader cache.
  195. /// </summary>
  196. public void InitializeShaderCache(CancellationToken cancellationToken)
  197. {
  198. HostInitalized.WaitOne();
  199. foreach (var physicalMemory in PhysicalMemoryRegistry.Values)
  200. {
  201. physicalMemory.ShaderCache.Initialize(cancellationToken);
  202. }
  203. }
  204. /// <summary>
  205. /// Sets the current thread as the main GPU thread.
  206. /// </summary>
  207. public void SetGpuThread()
  208. {
  209. _gpuThread = Thread.CurrentThread;
  210. Capabilities = Renderer.GetCapabilities();
  211. }
  212. /// <summary>
  213. /// Checks if the current thread is the GPU thread.
  214. /// </summary>
  215. /// <returns>True if the thread is the GPU thread, false otherwise</returns>
  216. public bool IsGpuThread()
  217. {
  218. return _gpuThread == Thread.CurrentThread;
  219. }
  220. /// <summary>
  221. /// Processes the queue of shaders that must save their binaries to the disk cache.
  222. /// </summary>
  223. public void ProcessShaderCacheQueue()
  224. {
  225. foreach (var physicalMemory in PhysicalMemoryRegistry.Values)
  226. {
  227. physicalMemory.ShaderCache.ProcessShaderCacheQueue();
  228. }
  229. }
  230. /// <summary>
  231. /// Advances internal sequence number.
  232. /// This forces the update of any modified GPU resource.
  233. /// </summary>
  234. internal void AdvanceSequence()
  235. {
  236. SequenceNumber++;
  237. }
  238. /// <summary>
  239. /// Registers a buffer migration. These are checked to see if they can be disposed when the sync number increases,
  240. /// and the migration copy has completed.
  241. /// </summary>
  242. /// <param name="migration">The buffer migration</param>
  243. internal void RegisterBufferMigration(BufferMigration migration)
  244. {
  245. BufferMigrations.Add(migration);
  246. _pendingSync = true;
  247. }
  248. /// <summary>
  249. /// Registers an action to be performed the next time a syncpoint is incremented.
  250. /// This will also ensure a host sync object is created, and <see cref="SyncNumber"/> is incremented.
  251. /// </summary>
  252. /// <param name="action">The action to be performed on sync object creation</param>
  253. /// <param name="syncpointOnly">True if the sync action should only run when syncpoints are incremented</param>
  254. public void RegisterSyncAction(Action action, bool syncpointOnly = false)
  255. {
  256. if (syncpointOnly)
  257. {
  258. SyncpointActions.Add(action);
  259. }
  260. else
  261. {
  262. SyncActions.Add(action);
  263. _pendingSync = true;
  264. }
  265. }
  266. /// <summary>
  267. /// Creates a host sync object if there are any pending sync actions. The actions will then be called.
  268. /// If no actions are present, a host sync object is not created.
  269. /// </summary>
  270. /// <param name="syncpoint">True if host sync is being created by a syncpoint</param>
  271. public void CreateHostSyncIfNeeded(bool syncpoint)
  272. {
  273. if (BufferMigrations.Count > 0)
  274. {
  275. ulong currentSyncNumber = Renderer.GetCurrentSync();
  276. for (int i = 0; i < BufferMigrations.Count; i++)
  277. {
  278. BufferMigration migration = BufferMigrations[i];
  279. long diff = (long)(currentSyncNumber - migration.SyncNumber);
  280. if (diff >= 0)
  281. {
  282. migration.Dispose();
  283. BufferMigrations.RemoveAt(i--);
  284. }
  285. }
  286. }
  287. if (_pendingSync || (syncpoint && SyncpointActions.Count > 0))
  288. {
  289. Renderer.CreateSync(SyncNumber);
  290. SyncNumber++;
  291. foreach (Action action in SyncActions)
  292. {
  293. action();
  294. }
  295. foreach (Action action in SyncpointActions)
  296. {
  297. action();
  298. }
  299. SyncActions.Clear();
  300. SyncpointActions.Clear();
  301. }
  302. _pendingSync = false;
  303. }
  304. /// <summary>
  305. /// Performs deferred actions.
  306. /// This is useful for actions that must run on the render thread, such as resource disposal.
  307. /// </summary>
  308. internal void RunDeferredActions()
  309. {
  310. while (DeferredActions.TryDequeue(out Action action))
  311. {
  312. action();
  313. }
  314. }
  315. /// <summary>
  316. /// Disposes all GPU resources currently cached.
  317. /// It's an error to push any GPU commands after disposal.
  318. /// Additionally, the GPU commands FIFO must be empty for disposal,
  319. /// and processing of all commands must have finished.
  320. /// </summary>
  321. public void Dispose()
  322. {
  323. Renderer.Dispose();
  324. GPFifo.Dispose();
  325. HostInitalized.Dispose();
  326. // Has to be disposed before processing deferred actions, as it will produce some.
  327. foreach (var physicalMemory in PhysicalMemoryRegistry.Values)
  328. {
  329. physicalMemory.Dispose();
  330. }
  331. PhysicalMemoryRegistry.Clear();
  332. RunDeferredActions();
  333. }
  334. }
  335. }