GpuContext.cs 6.1 KB

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