GpuContext.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. private readonly Lazy<Capabilities> _caps;
  64. /// <summary>
  65. /// Host hardware capabilities.
  66. /// </summary>
  67. internal Capabilities Capabilities => _caps.Value;
  68. /// <summary>
  69. /// Signaled when shader cache begins and ends loading.
  70. /// Signals true when loading has started, false when ended.
  71. /// </summary>
  72. public event Action<bool> ShaderCacheStateChanged
  73. {
  74. add => Methods.ShaderCache.ShaderCacheStateChanged += value;
  75. remove => Methods.ShaderCache.ShaderCacheStateChanged -= value;
  76. }
  77. /// <summary>
  78. /// Signaled while shader cache is loading to indicate current progress.
  79. /// Provides current and total number of shaders loaded.
  80. /// </summary>
  81. public event Action<int, int> ShaderCacheProgressChanged
  82. {
  83. add => Methods.ShaderCache.ShaderCacheProgressChanged += value;
  84. remove => Methods.ShaderCache.ShaderCacheProgressChanged -= value;
  85. }
  86. /// <summary>
  87. /// Creates a new instance of the GPU emulation context.
  88. /// </summary>
  89. /// <param name="renderer">Host renderer</param>
  90. public GpuContext(IRenderer renderer)
  91. {
  92. Renderer = renderer;
  93. MemoryManager = new MemoryManager(this);
  94. Methods = new Methods(this);
  95. GPFifo = new GPFifoDevice(this);
  96. Synchronization = new SynchronizationManager();
  97. Window = new Window(this);
  98. _caps = new Lazy<Capabilities>(Renderer.GetCapabilities);
  99. HostInitalized = new ManualResetEvent(false);
  100. SyncActions = new List<Action>();
  101. }
  102. /// <summary>
  103. /// Initialize the GPU shader cache.
  104. /// </summary>
  105. public void InitializeShaderCache()
  106. {
  107. HostInitalized.WaitOne();
  108. Methods.ShaderCache.Initialize();
  109. }
  110. /// <summary>
  111. /// Advances internal sequence number.
  112. /// This forces the update of any modified GPU resource.
  113. /// </summary>
  114. internal void AdvanceSequence()
  115. {
  116. SequenceNumber++;
  117. }
  118. /// <summary>
  119. /// Sets the process memory manager, after the application process is initialized.
  120. /// This is required for any GPU memory access.
  121. /// </summary>
  122. /// <param name="cpuMemory">CPU memory manager</param>
  123. public void SetVmm(Cpu.MemoryManager cpuMemory)
  124. {
  125. PhysicalMemory = new PhysicalMemory(cpuMemory);
  126. }
  127. /// <summary>
  128. /// Registers an action to be performed the next time a syncpoint is incremented.
  129. /// This will also ensure a host sync object is created, and <see cref="SyncNumber"/> is incremented.
  130. /// </summary>
  131. /// <param name="action">The action to be performed on sync object creation</param>
  132. public void RegisterSyncAction(Action action)
  133. {
  134. SyncActions.Add(action);
  135. }
  136. /// <summary>
  137. /// Creates a host sync object if there are any pending sync actions. The actions will then be called.
  138. /// If no actions are present, a host sync object is not created.
  139. /// </summary>
  140. public void CreateHostSyncIfNeeded()
  141. {
  142. if (SyncActions.Count > 0)
  143. {
  144. Renderer.CreateSync(SyncNumber);
  145. SyncNumber++;
  146. foreach (Action action in SyncActions)
  147. {
  148. action();
  149. }
  150. SyncActions.Clear();
  151. }
  152. }
  153. /// <summary>
  154. /// Disposes all GPU resources currently cached.
  155. /// It's an error to push any GPU commands after disposal.
  156. /// Additionally, the GPU commands FIFO must be empty for disposal,
  157. /// and processing of all commands must have finished.
  158. /// </summary>
  159. public void Dispose()
  160. {
  161. Methods.ShaderCache.Dispose();
  162. Methods.BufferManager.Dispose();
  163. Methods.TextureManager.Dispose();
  164. Renderer.Dispose();
  165. GPFifo.Dispose();
  166. HostInitalized.Dispose();
  167. }
  168. }
  169. }