GpuChannel.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Engine.GPFifo;
  3. using Ryujinx.Graphics.Gpu.Image;
  4. using Ryujinx.Graphics.Gpu.Memory;
  5. using System;
  6. using System.Threading;
  7. namespace Ryujinx.Graphics.Gpu
  8. {
  9. /// <summary>
  10. /// Represents a GPU channel.
  11. /// </summary>
  12. public class GpuChannel : IDisposable
  13. {
  14. private readonly GpuContext _context;
  15. private readonly GPFifoDevice _device;
  16. private readonly GPFifoProcessor _processor;
  17. private MemoryManager _memoryManager;
  18. /// <summary>
  19. /// Channel buffer bindings manager.
  20. /// </summary>
  21. internal BufferManager BufferManager { get; }
  22. /// <summary>
  23. /// Channel texture bindings manager.
  24. /// </summary>
  25. internal TextureManager TextureManager { get; }
  26. /// <summary>
  27. /// Current channel memory manager.
  28. /// </summary>
  29. internal MemoryManager MemoryManager => _memoryManager;
  30. /// <summary>
  31. /// Host hardware capabilities from the GPU context.
  32. /// </summary>
  33. internal ref Capabilities Capabilities => ref _context.Capabilities;
  34. /// <summary>
  35. /// Creates a new instance of a GPU channel.
  36. /// </summary>
  37. /// <param name="context">GPU context that the channel belongs to</param>
  38. internal GpuChannel(GpuContext context)
  39. {
  40. _context = context;
  41. _device = context.GPFifo;
  42. _processor = new GPFifoProcessor(context, this);
  43. BufferManager = new BufferManager(context, this);
  44. TextureManager = new TextureManager(context, this);
  45. }
  46. /// <summary>
  47. /// Binds a memory manager to the channel.
  48. /// All submitted and in-flight commands will use the specified memory manager for any memory operations.
  49. /// </summary>
  50. /// <param name="memoryManager">The new memory manager to be bound</param>
  51. public void BindMemory(MemoryManager memoryManager)
  52. {
  53. var oldMemoryManager = Interlocked.Exchange(ref _memoryManager, memoryManager ?? throw new ArgumentNullException(nameof(memoryManager)));
  54. memoryManager.Physical.IncrementReferenceCount();
  55. if (oldMemoryManager != null)
  56. {
  57. oldMemoryManager.Physical.BufferCache.NotifyBuffersModified -= BufferManager.Rebind;
  58. oldMemoryManager.Physical.DecrementReferenceCount();
  59. oldMemoryManager.MemoryUnmapped -= MemoryUnmappedHandler;
  60. }
  61. memoryManager.Physical.BufferCache.NotifyBuffersModified += BufferManager.Rebind;
  62. memoryManager.MemoryUnmapped += MemoryUnmappedHandler;
  63. // Since the memory manager changed, make sure we will get pools from addresses of the new memory manager.
  64. TextureManager.ReloadPools();
  65. memoryManager.Physical.BufferCache.QueuePrune();
  66. }
  67. /// <summary>
  68. /// Memory mappings change event handler.
  69. /// </summary>
  70. /// <param name="sender">Memory manager where the mappings changed</param>
  71. /// <param name="e">Information about the region that is being changed</param>
  72. private void MemoryUnmappedHandler(object sender, UnmapEventArgs e)
  73. {
  74. TextureManager.ReloadPools();
  75. var memoryManager = Volatile.Read(ref _memoryManager);
  76. memoryManager?.Physical.BufferCache.QueuePrune();
  77. }
  78. /// <summary>
  79. /// Writes data directly to the state of the specified class.
  80. /// </summary>
  81. /// <param name="classId">ID of the class to write the data into</param>
  82. /// <param name="offset">State offset in bytes</param>
  83. /// <param name="value">Value to be written</param>
  84. public void Write(ClassId classId, int offset, uint value)
  85. {
  86. _processor.Write(classId, offset, (int)value);
  87. }
  88. /// <summary>
  89. /// Push a GPFIFO entry in the form of a prefetched command buffer.
  90. /// It is intended to be used by nvservices to handle special cases.
  91. /// </summary>
  92. /// <param name="commandBuffer">The command buffer containing the prefetched commands</param>
  93. public void PushHostCommandBuffer(int[] commandBuffer)
  94. {
  95. _device.PushHostCommandBuffer(_processor, commandBuffer);
  96. }
  97. /// <summary>
  98. /// Pushes GPFIFO entries.
  99. /// </summary>
  100. /// <param name="entries">GPFIFO entries</param>
  101. public void PushEntries(ReadOnlySpan<ulong> entries)
  102. {
  103. _device.PushEntries(_processor, entries);
  104. }
  105. /// <summary>
  106. /// Disposes the GPU channel.
  107. /// It's an error to use the GPU channel after disposal.
  108. /// </summary>
  109. public void Dispose()
  110. {
  111. _context.DeferredActions.Enqueue(Destroy);
  112. }
  113. /// <summary>
  114. /// Performs disposal of the host GPU resources used by this channel, that are not shared.
  115. /// This must only be called from the render thread.
  116. /// </summary>
  117. private void Destroy()
  118. {
  119. TextureManager.Dispose();
  120. var oldMemoryManager = Interlocked.Exchange(ref _memoryManager, null);
  121. if (oldMemoryManager != null)
  122. {
  123. oldMemoryManager.Physical.BufferCache.NotifyBuffersModified -= BufferManager.Rebind;
  124. oldMemoryManager.Physical.DecrementReferenceCount();
  125. oldMemoryManager.MemoryUnmapped -= MemoryUnmappedHandler;
  126. }
  127. }
  128. }
  129. }