GpuChannel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Ryujinx.Graphics.Gpu.Engine.GPFifo;
  2. using Ryujinx.Graphics.Gpu.Image;
  3. using Ryujinx.Graphics.Gpu.Memory;
  4. using System;
  5. using System.Threading;
  6. namespace Ryujinx.Graphics.Gpu
  7. {
  8. /// <summary>
  9. /// Represents a GPU channel.
  10. /// </summary>
  11. public class GpuChannel : IDisposable
  12. {
  13. private readonly GpuContext _context;
  14. private readonly GPFifoDevice _device;
  15. private readonly GPFifoProcessor _processor;
  16. private MemoryManager _memoryManager;
  17. /// <summary>
  18. /// Channel buffer bindings manager.
  19. /// </summary>
  20. internal BufferManager BufferManager { get; }
  21. /// <summary>
  22. /// Channel texture bindings manager.
  23. /// </summary>
  24. internal TextureManager TextureManager { get; }
  25. /// <summary>
  26. /// Current channel memory manager.
  27. /// </summary>
  28. internal MemoryManager MemoryManager => _memoryManager;
  29. /// <summary>
  30. /// Creates a new instance of a GPU channel.
  31. /// </summary>
  32. /// <param name="context">GPU context that the channel belongs to</param>
  33. internal GpuChannel(GpuContext context)
  34. {
  35. _context = context;
  36. _device = context.GPFifo;
  37. _processor = new GPFifoProcessor(context, this);
  38. BufferManager = new BufferManager(context, this);
  39. TextureManager = new TextureManager(context, this);
  40. }
  41. /// <summary>
  42. /// Binds a memory manager to the channel.
  43. /// All submitted and in-flight commands will use the specified memory manager for any memory operations.
  44. /// </summary>
  45. /// <param name="memoryManager">The new memory manager to be bound</param>
  46. public void BindMemory(MemoryManager memoryManager)
  47. {
  48. var oldMemoryManager = Interlocked.Exchange(ref _memoryManager, memoryManager ?? throw new ArgumentNullException(nameof(memoryManager)));
  49. memoryManager.Physical.IncrementReferenceCount();
  50. if (oldMemoryManager != null)
  51. {
  52. oldMemoryManager.Physical.BufferCache.NotifyBuffersModified -= BufferManager.Rebind;
  53. oldMemoryManager.Physical.DecrementReferenceCount();
  54. }
  55. memoryManager.Physical.BufferCache.NotifyBuffersModified += BufferManager.Rebind;
  56. }
  57. /// <summary>
  58. /// Push a GPFIFO entry in the form of a prefetched command buffer.
  59. /// It is intended to be used by nvservices to handle special cases.
  60. /// </summary>
  61. /// <param name="commandBuffer">The command buffer containing the prefetched commands</param>
  62. public void PushHostCommandBuffer(int[] commandBuffer)
  63. {
  64. _device.PushHostCommandBuffer(_processor, commandBuffer);
  65. }
  66. /// <summary>
  67. /// Pushes GPFIFO entries.
  68. /// </summary>
  69. /// <param name="entries">GPFIFO entries</param>
  70. public void PushEntries(ReadOnlySpan<ulong> entries)
  71. {
  72. _device.PushEntries(_processor, entries);
  73. }
  74. /// <summary>
  75. /// Disposes the GPU channel.
  76. /// It's an error to use the GPU channel after disposal.
  77. /// </summary>
  78. public void Dispose()
  79. {
  80. _context.DeferredActions.Enqueue(Destroy);
  81. }
  82. /// <summary>
  83. /// Performs disposal of the host GPU resources used by this channel, that are not shared.
  84. /// This must only be called from the render thread.
  85. /// </summary>
  86. private void Destroy()
  87. {
  88. TextureManager.Dispose();
  89. var oldMemoryManager = Interlocked.Exchange(ref _memoryManager, null);
  90. if (oldMemoryManager != null)
  91. {
  92. oldMemoryManager.Physical.BufferCache.NotifyBuffersModified -= BufferManager.Rebind;
  93. oldMemoryManager.Physical.DecrementReferenceCount();
  94. }
  95. }
  96. }
  97. }