GpuChannel.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. oldMemoryManager.MemoryUnmapped -= MemoryUnmappedHandler;
  55. }
  56. memoryManager.Physical.BufferCache.NotifyBuffersModified += BufferManager.Rebind;
  57. memoryManager.MemoryUnmapped += MemoryUnmappedHandler;
  58. // Since the memory manager changed, make sure we will get pools from addresses of the new memory manager.
  59. TextureManager.ReloadPools();
  60. MemoryManager.Physical.BufferCache.QueuePrune();
  61. }
  62. /// <summary>
  63. /// Memory mappings change event handler.
  64. /// </summary>
  65. /// <param name="sender">Memory manager where the mappings changed</param>
  66. /// <param name="e">Information about the region that is being changed</param>
  67. private void MemoryUnmappedHandler(object sender, UnmapEventArgs e)
  68. {
  69. TextureManager.ReloadPools();
  70. MemoryManager.Physical.BufferCache.QueuePrune();
  71. }
  72. /// <summary>
  73. /// Writes data directly to the state of the specified class.
  74. /// </summary>
  75. /// <param name="classId">ID of the class to write the data into</param>
  76. /// <param name="offset">State offset in bytes</param>
  77. /// <param name="value">Value to be written</param>
  78. public void Write(ClassId classId, int offset, uint value)
  79. {
  80. _processor.Write(classId, offset, (int)value);
  81. }
  82. /// <summary>
  83. /// Push a GPFIFO entry in the form of a prefetched command buffer.
  84. /// It is intended to be used by nvservices to handle special cases.
  85. /// </summary>
  86. /// <param name="commandBuffer">The command buffer containing the prefetched commands</param>
  87. public void PushHostCommandBuffer(int[] commandBuffer)
  88. {
  89. _device.PushHostCommandBuffer(_processor, commandBuffer);
  90. }
  91. /// <summary>
  92. /// Pushes GPFIFO entries.
  93. /// </summary>
  94. /// <param name="entries">GPFIFO entries</param>
  95. public void PushEntries(ReadOnlySpan<ulong> entries)
  96. {
  97. _device.PushEntries(_processor, entries);
  98. }
  99. /// <summary>
  100. /// Disposes the GPU channel.
  101. /// It's an error to use the GPU channel after disposal.
  102. /// </summary>
  103. public void Dispose()
  104. {
  105. _context.DeferredActions.Enqueue(Destroy);
  106. }
  107. /// <summary>
  108. /// Performs disposal of the host GPU resources used by this channel, that are not shared.
  109. /// This must only be called from the render thread.
  110. /// </summary>
  111. private void Destroy()
  112. {
  113. TextureManager.Dispose();
  114. var oldMemoryManager = Interlocked.Exchange(ref _memoryManager, null);
  115. if (oldMemoryManager != null)
  116. {
  117. oldMemoryManager.Physical.BufferCache.NotifyBuffersModified -= BufferManager.Rebind;
  118. oldMemoryManager.Physical.DecrementReferenceCount();
  119. oldMemoryManager.MemoryUnmapped -= MemoryUnmappedHandler;
  120. }
  121. }
  122. }
  123. }