2021-06-24 01:51:41 +02:00
|
|
|
|
using Ryujinx.Graphics.Gpu.Engine.GPFifo;
|
|
|
|
|
using Ryujinx.Graphics.Gpu.Image;
|
|
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
|
|
|
|
using System;
|
2021-06-29 19:32:02 +02:00
|
|
|
|
using System.Threading;
|
2021-06-24 01:51:41 +02:00
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a GPU channel.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class GpuChannel : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly GpuContext _context;
|
|
|
|
|
private readonly GPFifoDevice _device;
|
|
|
|
|
private readonly GPFifoProcessor _processor;
|
2021-06-29 19:32:02 +02:00
|
|
|
|
private MemoryManager _memoryManager;
|
2021-06-24 01:51:41 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Channel buffer bindings manager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal BufferManager BufferManager { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Channel texture bindings manager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal TextureManager TextureManager { get; }
|
|
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Current channel memory manager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal MemoryManager MemoryManager => _memoryManager;
|
|
|
|
|
|
2021-06-24 01:51:41 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new instance of a GPU channel.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">GPU context that the channel belongs to</param>
|
|
|
|
|
internal GpuChannel(GpuContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
_device = context.GPFifo;
|
|
|
|
|
_processor = new GPFifoProcessor(context, this);
|
2021-06-29 19:32:02 +02:00
|
|
|
|
BufferManager = new BufferManager(context, this);
|
2021-06-24 01:51:41 +02:00
|
|
|
|
TextureManager = new TextureManager(context, this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Binds a memory manager to the channel.
|
|
|
|
|
/// All submitted and in-flight commands will use the specified memory manager for any memory operations.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="memoryManager">The new memory manager to be bound</param>
|
|
|
|
|
public void BindMemory(MemoryManager memoryManager)
|
|
|
|
|
{
|
|
|
|
|
var oldMemoryManager = Interlocked.Exchange(ref _memoryManager, memoryManager ?? throw new ArgumentNullException(nameof(memoryManager)));
|
|
|
|
|
|
|
|
|
|
memoryManager.Physical.IncrementReferenceCount();
|
|
|
|
|
|
|
|
|
|
if (oldMemoryManager != null)
|
|
|
|
|
{
|
|
|
|
|
oldMemoryManager.Physical.BufferCache.NotifyBuffersModified -= BufferManager.Rebind;
|
|
|
|
|
oldMemoryManager.Physical.DecrementReferenceCount();
|
2022-07-28 02:07:48 +02:00
|
|
|
|
oldMemoryManager.MemoryUnmapped -= MemoryUnmappedHandler;
|
2021-06-29 19:32:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memoryManager.Physical.BufferCache.NotifyBuffersModified += BufferManager.Rebind;
|
2022-07-28 02:07:48 +02:00
|
|
|
|
memoryManager.MemoryUnmapped += MemoryUnmappedHandler;
|
|
|
|
|
|
|
|
|
|
// Since the memory manager changed, make sure we will get pools from addresses of the new memory manager.
|
|
|
|
|
TextureManager.ReloadPools();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Memory mappings change event handler.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">Memory manager where the mappings changed</param>
|
|
|
|
|
/// <param name="e">Information about the region that is being changed</param>
|
|
|
|
|
private void MemoryUnmappedHandler(object sender, UnmapEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
TextureManager.ReloadPools();
|
2021-06-29 19:32:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes data directly to the state of the specified class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="classId">ID of the class to write the data into</param>
|
|
|
|
|
/// <param name="offset">State offset in bytes</param>
|
|
|
|
|
/// <param name="value">Value to be written</param>
|
|
|
|
|
public void Write(ClassId classId, int offset, uint value)
|
|
|
|
|
{
|
|
|
|
|
_processor.Write(classId, offset, (int)value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 01:51:41 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Push a GPFIFO entry in the form of a prefetched command buffer.
|
|
|
|
|
/// It is intended to be used by nvservices to handle special cases.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="commandBuffer">The command buffer containing the prefetched commands</param>
|
|
|
|
|
public void PushHostCommandBuffer(int[] commandBuffer)
|
|
|
|
|
{
|
|
|
|
|
_device.PushHostCommandBuffer(_processor, commandBuffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pushes GPFIFO entries.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="entries">GPFIFO entries</param>
|
|
|
|
|
public void PushEntries(ReadOnlySpan<ulong> entries)
|
|
|
|
|
{
|
|
|
|
|
_device.PushEntries(_processor, entries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the GPU channel.
|
|
|
|
|
/// It's an error to use the GPU channel after disposal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
|
_context.DeferredActions.Enqueue(Destroy);
|
2021-06-24 01:51:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs disposal of the host GPU resources used by this channel, that are not shared.
|
|
|
|
|
/// This must only be called from the render thread.
|
|
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
|
private void Destroy()
|
2021-06-24 01:51:41 +02:00
|
|
|
|
{
|
|
|
|
|
TextureManager.Dispose();
|
2021-06-29 19:32:02 +02:00
|
|
|
|
|
|
|
|
|
var oldMemoryManager = Interlocked.Exchange(ref _memoryManager, null);
|
|
|
|
|
if (oldMemoryManager != null)
|
|
|
|
|
{
|
|
|
|
|
oldMemoryManager.Physical.BufferCache.NotifyBuffersModified -= BufferManager.Rebind;
|
|
|
|
|
oldMemoryManager.Physical.DecrementReferenceCount();
|
|
|
|
|
}
|
2021-06-24 01:51:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|