GpuContext.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Ryujinx.Common.Configuration;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Engine;
  4. using Ryujinx.Graphics.Gpu.Engine.GPFifo;
  5. using Ryujinx.Graphics.Gpu.Memory;
  6. using Ryujinx.Graphics.Gpu.Synchronization;
  7. using System;
  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. /// Event signaled when the gpu context is ready to be used.
  22. /// </summary>
  23. public ManualResetEvent ReadyEvent { get; }
  24. /// <summary>
  25. /// Host renderer.
  26. /// </summary>
  27. public IRenderer Renderer { get; }
  28. /// <summary>
  29. /// Physical memory access (it actually accesses the process memory, not actual physical memory).
  30. /// </summary>
  31. internal PhysicalMemory PhysicalMemory { get; private set; }
  32. /// <summary>
  33. /// GPU memory manager.
  34. /// </summary>
  35. public MemoryManager MemoryManager { get; }
  36. /// <summary>
  37. /// GPU engine methods processing.
  38. /// </summary>
  39. internal Methods Methods { get; }
  40. /// <summary>
  41. /// GPU General Purpose FIFO queue.
  42. /// </summary>
  43. public GPFifoDevice GPFifo { get; }
  44. /// <summary>
  45. /// GPU synchronization manager.
  46. /// </summary>
  47. public SynchronizationManager Synchronization { get; }
  48. /// <summary>
  49. /// Presentation window.
  50. /// </summary>
  51. public Window Window { get; }
  52. /// <summary>
  53. /// Internal sequence number, used to avoid needless resource data updates
  54. /// in the middle of a command buffer before synchronizations.
  55. /// </summary>
  56. internal int SequenceNumber { get; private set; }
  57. private readonly Lazy<Capabilities> _caps;
  58. /// <summary>
  59. /// Host hardware capabilities.
  60. /// </summary>
  61. internal Capabilities Capabilities => _caps.Value;
  62. /// <summary>
  63. /// Creates a new instance of the GPU emulation context.
  64. /// </summary>
  65. /// <param name="renderer">Host renderer</param>
  66. public GpuContext(IRenderer renderer)
  67. {
  68. Renderer = renderer;
  69. MemoryManager = new MemoryManager(this);
  70. Methods = new Methods(this);
  71. GPFifo = new GPFifoDevice(this);
  72. Synchronization = new SynchronizationManager();
  73. Window = new Window(this);
  74. _caps = new Lazy<Capabilities>(Renderer.GetCapabilities);
  75. HostInitalized = new ManualResetEvent(false);
  76. ReadyEvent = new ManualResetEvent(false);
  77. }
  78. /// <summary>
  79. /// Initialize the GPU emulation context.
  80. /// </summary>
  81. /// <param name="logLevel">The log level required.</param>
  82. public void Initialize(GraphicsDebugLevel logLevel)
  83. {
  84. HostInitalized.WaitOne();
  85. Renderer.Initialize(logLevel);
  86. Methods.ShaderCache.Initialize();
  87. ReadyEvent.Set();
  88. }
  89. /// <summary>
  90. /// Advances internal sequence number.
  91. /// This forces the update of any modified GPU resource.
  92. /// </summary>
  93. internal void AdvanceSequence()
  94. {
  95. SequenceNumber++;
  96. }
  97. /// <summary>
  98. /// Sets the process memory manager, after the application process is initialized.
  99. /// This is required for any GPU memory access.
  100. /// </summary>
  101. /// <param name="cpuMemory">CPU memory manager</param>
  102. public void SetVmm(Cpu.MemoryManager cpuMemory)
  103. {
  104. PhysicalMemory = new PhysicalMemory(cpuMemory);
  105. }
  106. /// <summary>
  107. /// Disposes all GPU resources currently cached.
  108. /// It's an error to push any GPU commands after disposal.
  109. /// Additionally, the GPU commands FIFO must be empty for disposal,
  110. /// and processing of all commands must have finished.
  111. /// </summary>
  112. public void Dispose()
  113. {
  114. Methods.ShaderCache.Dispose();
  115. Methods.BufferManager.Dispose();
  116. Methods.TextureManager.Dispose();
  117. Renderer.Dispose();
  118. GPFifo.Dispose();
  119. HostInitalized.Dispose();
  120. ReadyEvent.Dispose();
  121. }
  122. }
  123. }