GpuContext.cs 3.7 KB

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