GpuContext.cs 13 KB

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