GpuContext.cs 3.8 KB

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