GpuContext.cs 3.5 KB

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