GpuChannel.cs 5.0 KB

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