GpuContext.cs 13 KB

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