GpuContext.cs 5.7 KB

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