GpuContext.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Engine;
  3. using Ryujinx.Graphics.Gpu.Engine.GPFifo;
  4. using Ryujinx.Graphics.Gpu.Memory;
  5. using Ryujinx.Graphics.Gpu.Synchronization;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading;
  9. namespace Ryujinx.Graphics.Gpu
  10. {
  11. /// <summary>
  12. /// GPU emulation context.
  13. /// </summary>
  14. public sealed class GpuContext : IDisposable
  15. {
  16. /// <summary>
  17. /// Event signaled when the host emulation context is ready to be used by the gpu context.
  18. /// </summary>
  19. public ManualResetEvent HostInitalized { get; }
  20. /// <summary>
  21. /// Host renderer.
  22. /// </summary>
  23. public IRenderer Renderer { get; }
  24. /// <summary>
  25. /// Physical memory access (it actually accesses the process memory, not actual physical memory).
  26. /// </summary>
  27. internal PhysicalMemory PhysicalMemory { get; private set; }
  28. /// <summary>
  29. /// GPU memory manager.
  30. /// </summary>
  31. public MemoryManager MemoryManager { get; }
  32. /// <summary>
  33. /// GPU engine methods processing.
  34. /// </summary>
  35. internal Methods Methods { get; }
  36. /// <summary>
  37. /// GPU General Purpose FIFO queue.
  38. /// </summary>
  39. public GPFifoDevice GPFifo { get; }
  40. /// <summary>
  41. /// GPU synchronization manager.
  42. /// </summary>
  43. public SynchronizationManager Synchronization { get; }
  44. /// <summary>
  45. /// Presentation window.
  46. /// </summary>
  47. public Window Window { get; }
  48. /// <summary>
  49. /// Internal sequence number, used to avoid needless resource data updates
  50. /// in the middle of a command buffer before synchronizations.
  51. /// </summary>
  52. internal int SequenceNumber { get; private set; }
  53. /// <summary>
  54. /// Internal sync number, used to denote points at which host synchronization can be requested.
  55. /// </summary>
  56. internal ulong SyncNumber { get; private set; }
  57. /// <summary>
  58. /// Actions to be performed when a CPU waiting sync point is triggered.
  59. /// If there are more than 0 items when this happens, a host sync object will be generated for the given <see cref="SyncNumber"/>,
  60. /// and the SyncNumber will be incremented.
  61. /// </summary>
  62. internal List<Action> SyncActions { get; }
  63. /// <summary>
  64. /// Queue with closed channels for deferred disposal from the render thread.
  65. /// </summary>
  66. internal Queue<GpuChannel> DisposedChannels { get; }
  67. private readonly Lazy<Capabilities> _caps;
  68. /// <summary>
  69. /// Host hardware capabilities.
  70. /// </summary>
  71. internal Capabilities Capabilities => _caps.Value;
  72. /// <summary>
  73. /// Event for signalling shader cache loading progress.
  74. /// </summary>
  75. public event Action<Shader.ShaderCacheState, int, int> ShaderCacheStateChanged
  76. {
  77. add => Methods.ShaderCache.ShaderCacheStateChanged += value;
  78. remove => Methods.ShaderCache.ShaderCacheStateChanged -= value;
  79. }
  80. /// <summary>
  81. /// Creates a new instance of the GPU emulation context.
  82. /// </summary>
  83. /// <param name="renderer">Host renderer</param>
  84. public GpuContext(IRenderer renderer)
  85. {
  86. Renderer = renderer;
  87. MemoryManager = new MemoryManager(this);
  88. Methods = new Methods(this);
  89. GPFifo = new GPFifoDevice(this);
  90. Synchronization = new SynchronizationManager();
  91. Window = new Window(this);
  92. _caps = new Lazy<Capabilities>(Renderer.GetCapabilities);
  93. HostInitalized = new ManualResetEvent(false);
  94. SyncActions = new List<Action>();
  95. DisposedChannels = new Queue<GpuChannel>();
  96. }
  97. public GpuChannel CreateChannel()
  98. {
  99. return new GpuChannel(this);
  100. }
  101. /// <summary>
  102. /// Initialize the GPU shader cache.
  103. /// </summary>
  104. public void InitializeShaderCache()
  105. {
  106. HostInitalized.WaitOne();
  107. Methods.ShaderCache.Initialize();
  108. }
  109. /// <summary>
  110. /// Advances internal sequence number.
  111. /// This forces the update of any modified GPU resource.
  112. /// </summary>
  113. internal void AdvanceSequence()
  114. {
  115. SequenceNumber++;
  116. }
  117. /// <summary>
  118. /// Sets the process memory manager, after the application process is initialized.
  119. /// This is required for any GPU memory access.
  120. /// </summary>
  121. /// <param name="cpuMemory">CPU memory manager</param>
  122. public void SetVmm(Cpu.IVirtualMemoryManagerTracked cpuMemory)
  123. {
  124. PhysicalMemory = new PhysicalMemory(cpuMemory);
  125. }
  126. /// <summary>
  127. /// Registers an action to be performed the next time a syncpoint is incremented.
  128. /// This will also ensure a host sync object is created, and <see cref="SyncNumber"/> is incremented.
  129. /// </summary>
  130. /// <param name="action">The action to be performed on sync object creation</param>
  131. public void RegisterSyncAction(Action action)
  132. {
  133. SyncActions.Add(action);
  134. }
  135. /// <summary>
  136. /// Creates a host sync object if there are any pending sync actions. The actions will then be called.
  137. /// If no actions are present, a host sync object is not created.
  138. /// </summary>
  139. public void CreateHostSyncIfNeeded()
  140. {
  141. if (SyncActions.Count > 0)
  142. {
  143. Renderer.CreateSync(SyncNumber);
  144. SyncNumber++;
  145. foreach (Action action in SyncActions)
  146. {
  147. action();
  148. }
  149. SyncActions.Clear();
  150. }
  151. }
  152. /// <summary>
  153. /// Performs deferred disposal of closed channels.
  154. /// This must only be called from the render thread.
  155. /// </summary>
  156. internal void DisposePendingChannels()
  157. {
  158. while (DisposedChannels.TryDequeue(out GpuChannel channel))
  159. {
  160. channel.Destroy();
  161. }
  162. }
  163. /// <summary>
  164. /// Disposes all GPU resources currently cached.
  165. /// It's an error to push any GPU commands after disposal.
  166. /// Additionally, the GPU commands FIFO must be empty for disposal,
  167. /// and processing of all commands must have finished.
  168. /// </summary>
  169. public void Dispose()
  170. {
  171. DisposePendingChannels();
  172. Methods.ShaderCache.Dispose();
  173. Methods.BufferCache.Dispose();
  174. Methods.TextureCache.Dispose();
  175. Renderer.Dispose();
  176. GPFifo.Dispose();
  177. HostInitalized.Dispose();
  178. PhysicalMemory.Dispose();
  179. }
  180. }
  181. }