GpuContext.cs 4.1 KB

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