GpuChannel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /// Writes data directly to the state of the specified class.
  59. /// </summary>
  60. /// <param name="classId">ID of the class to write the data into</param>
  61. /// <param name="offset">State offset in bytes</param>
  62. /// <param name="value">Value to be written</param>
  63. public void Write(ClassId classId, int offset, uint value)
  64. {
  65. _processor.Write(classId, offset, (int)value);
  66. }
  67. /// <summary>
  68. /// Push a GPFIFO entry in the form of a prefetched command buffer.
  69. /// It is intended to be used by nvservices to handle special cases.
  70. /// </summary>
  71. /// <param name="commandBuffer">The command buffer containing the prefetched commands</param>
  72. public void PushHostCommandBuffer(int[] commandBuffer)
  73. {
  74. _device.PushHostCommandBuffer(_processor, commandBuffer);
  75. }
  76. /// <summary>
  77. /// Pushes GPFIFO entries.
  78. /// </summary>
  79. /// <param name="entries">GPFIFO entries</param>
  80. public void PushEntries(ReadOnlySpan<ulong> entries)
  81. {
  82. _device.PushEntries(_processor, entries);
  83. }
  84. /// <summary>
  85. /// Disposes the GPU channel.
  86. /// It's an error to use the GPU channel after disposal.
  87. /// </summary>
  88. public void Dispose()
  89. {
  90. _context.DeferredActions.Enqueue(Destroy);
  91. }
  92. /// <summary>
  93. /// Performs disposal of the host GPU resources used by this channel, that are not shared.
  94. /// This must only be called from the render thread.
  95. /// </summary>
  96. private void Destroy()
  97. {
  98. TextureManager.Dispose();
  99. var oldMemoryManager = Interlocked.Exchange(ref _memoryManager, null);
  100. if (oldMemoryManager != null)
  101. {
  102. oldMemoryManager.Physical.BufferCache.NotifyBuffersModified -= BufferManager.Rebind;
  103. oldMemoryManager.Physical.DecrementReferenceCount();
  104. }
  105. }
  106. }
  107. }