2021-06-29 19:32:02 +02:00
|
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
|
|
|
|
using System;
|
2020-07-24 04:53:25 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine.GPFifo
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a GPU General Purpose FIFO device.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class GPFifoDevice : IDisposable
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Indicates if the command buffer has pre-fetch enabled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private enum CommandBufferType
|
|
|
|
|
{
|
|
|
|
|
Prefetch,
|
|
|
|
|
NoPrefetch
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Command buffer data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private struct CommandBuffer
|
|
|
|
|
{
|
2021-06-24 01:51:41 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processor used to process the command buffer. Contains channel state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public GPFifoProcessor Processor;
|
|
|
|
|
|
2020-07-24 04:53:25 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The type of the command buffer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public CommandBufferType Type;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetched data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int[] Words;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The GPFIFO entry address (used in <see cref="CommandBufferType.NoPrefetch"/> mode).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ulong EntryAddress;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The count of entries inside this GPFIFO entry.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public uint EntryCount;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetch the command buffer.
|
|
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
|
public void Fetch(MemoryManager memoryManager)
|
2020-07-24 04:53:25 +02:00
|
|
|
|
{
|
|
|
|
|
if (Words == null)
|
|
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
|
Words = MemoryMarshal.Cast<byte, int>(memoryManager.GetSpan(EntryAddress, (int)EntryCount * 4, true)).ToArray();
|
2020-07-24 04:53:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly ConcurrentQueue<CommandBuffer> _commandBufferQueue;
|
|
|
|
|
|
|
|
|
|
private CommandBuffer _currentCommandBuffer;
|
2021-06-24 01:51:41 +02:00
|
|
|
|
private GPFifoProcessor _prevChannelProcessor;
|
2020-07-24 04:53:25 +02:00
|
|
|
|
|
|
|
|
|
private readonly bool _ibEnable;
|
|
|
|
|
private readonly GpuContext _context;
|
|
|
|
|
private readonly AutoResetEvent _event;
|
|
|
|
|
|
2020-12-17 19:39:52 +01:00
|
|
|
|
private bool _interrupt;
|
|
|
|
|
|
2020-07-24 04:53:25 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new instance of the GPU General Purpose FIFO device.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">GPU context that the GPFIFO belongs to</param>
|
|
|
|
|
internal GPFifoDevice(GpuContext context)
|
|
|
|
|
{
|
|
|
|
|
_commandBufferQueue = new ConcurrentQueue<CommandBuffer>();
|
|
|
|
|
_ibEnable = true;
|
|
|
|
|
_context = context;
|
|
|
|
|
_event = new AutoResetEvent(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Signal the FIFO that there are new entries to process.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SignalNewEntries()
|
|
|
|
|
{
|
|
|
|
|
_event.Set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2021-06-24 01:51:41 +02:00
|
|
|
|
/// <param name="processor">Processor used to process <paramref name="commandBuffer"/></param>
|
2020-07-24 04:53:25 +02:00
|
|
|
|
/// <param name="commandBuffer">The command buffer containing the prefetched commands</param>
|
2021-06-24 01:51:41 +02:00
|
|
|
|
internal void PushHostCommandBuffer(GPFifoProcessor processor, int[] commandBuffer)
|
2020-07-24 04:53:25 +02:00
|
|
|
|
{
|
|
|
|
|
_commandBufferQueue.Enqueue(new CommandBuffer
|
|
|
|
|
{
|
2021-06-24 01:51:41 +02:00
|
|
|
|
Processor = processor,
|
2020-07-24 04:53:25 +02:00
|
|
|
|
Type = CommandBufferType.Prefetch,
|
|
|
|
|
Words = commandBuffer,
|
|
|
|
|
EntryAddress = ulong.MaxValue,
|
|
|
|
|
EntryCount = (uint)commandBuffer.Length
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a CommandBuffer from a GPFIFO entry.
|
|
|
|
|
/// </summary>
|
2021-06-24 01:51:41 +02:00
|
|
|
|
/// <param name="processor">Processor used to process the command buffer pointed to by <paramref name="entry"/></param>
|
2020-07-24 04:53:25 +02:00
|
|
|
|
/// <param name="entry">The GPFIFO entry</param>
|
|
|
|
|
/// <returns>A new CommandBuffer based on the GPFIFO entry</returns>
|
2021-06-24 01:51:41 +02:00
|
|
|
|
private static CommandBuffer CreateCommandBuffer(GPFifoProcessor processor, GPEntry entry)
|
2020-07-24 04:53:25 +02:00
|
|
|
|
{
|
|
|
|
|
CommandBufferType type = CommandBufferType.Prefetch;
|
|
|
|
|
|
|
|
|
|
if (entry.Entry1Sync == Entry1Sync.Wait)
|
|
|
|
|
{
|
|
|
|
|
type = CommandBufferType.NoPrefetch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ulong startAddress = ((ulong)entry.Entry0Get << 2) | ((ulong)entry.Entry1GetHi << 32);
|
|
|
|
|
|
|
|
|
|
return new CommandBuffer
|
|
|
|
|
{
|
2021-06-24 01:51:41 +02:00
|
|
|
|
Processor = processor,
|
2020-07-24 04:53:25 +02:00
|
|
|
|
Type = type,
|
|
|
|
|
Words = null,
|
|
|
|
|
EntryAddress = startAddress,
|
|
|
|
|
EntryCount = (uint)entry.Entry1Length
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pushes GPFIFO entries.
|
|
|
|
|
/// </summary>
|
2021-06-24 01:51:41 +02:00
|
|
|
|
/// <param name="processor">Processor used to process the command buffers pointed to by <paramref name="entries"/></param>
|
2020-07-24 04:53:25 +02:00
|
|
|
|
/// <param name="entries">GPFIFO entries</param>
|
2021-06-24 01:51:41 +02:00
|
|
|
|
internal void PushEntries(GPFifoProcessor processor, ReadOnlySpan<ulong> entries)
|
2020-07-24 04:53:25 +02:00
|
|
|
|
{
|
|
|
|
|
bool beforeBarrier = true;
|
|
|
|
|
|
|
|
|
|
for (int index = 0; index < entries.Length; index++)
|
|
|
|
|
{
|
|
|
|
|
ulong entry = entries[index];
|
|
|
|
|
|
2021-06-24 01:51:41 +02:00
|
|
|
|
CommandBuffer commandBuffer = CreateCommandBuffer(processor, Unsafe.As<ulong, GPEntry>(ref entry));
|
2020-07-24 04:53:25 +02:00
|
|
|
|
|
|
|
|
|
if (beforeBarrier && commandBuffer.Type == CommandBufferType.Prefetch)
|
|
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
|
commandBuffer.Fetch(processor.MemoryManager);
|
2020-07-24 04:53:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (commandBuffer.Type == CommandBufferType.NoPrefetch)
|
|
|
|
|
{
|
|
|
|
|
beforeBarrier = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_commandBufferQueue.Enqueue(commandBuffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Waits until commands are pushed to the FIFO.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if commands were received, false if wait timed out</returns>
|
|
|
|
|
public bool WaitForCommands()
|
|
|
|
|
{
|
2020-12-17 19:39:52 +01:00
|
|
|
|
return !_commandBufferQueue.IsEmpty || (_event.WaitOne(8) && !_commandBufferQueue.IsEmpty);
|
2020-07-24 04:53:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes commands pushed to the FIFO.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void DispatchCalls()
|
|
|
|
|
{
|
2021-06-24 01:51:41 +02:00
|
|
|
|
// Use this opportunity to also dispose any pending channels that were closed.
|
2021-06-29 19:32:02 +02:00
|
|
|
|
_context.RunDeferredActions();
|
2021-06-24 01:51:41 +02:00
|
|
|
|
|
|
|
|
|
// Process command buffers.
|
2020-12-17 19:39:52 +01:00
|
|
|
|
while (_ibEnable && !_interrupt && _commandBufferQueue.TryDequeue(out CommandBuffer entry))
|
2020-07-24 04:53:25 +02:00
|
|
|
|
{
|
|
|
|
|
_currentCommandBuffer = entry;
|
2021-06-29 19:32:02 +02:00
|
|
|
|
_currentCommandBuffer.Fetch(entry.Processor.MemoryManager);
|
2020-07-24 04:53:25 +02:00
|
|
|
|
|
2021-06-24 01:51:41 +02:00
|
|
|
|
// If we are changing the current channel,
|
|
|
|
|
// we need to force all the host state to be updated.
|
|
|
|
|
if (_prevChannelProcessor != entry.Processor)
|
|
|
|
|
{
|
|
|
|
|
_prevChannelProcessor = entry.Processor;
|
|
|
|
|
entry.Processor.ForceAllDirty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry.Processor.Process(_currentCommandBuffer.Words);
|
2020-07-24 04:53:25 +02:00
|
|
|
|
}
|
2020-12-17 19:39:52 +01:00
|
|
|
|
|
|
|
|
|
_interrupt = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Interrupts command processing. This will break out of the DispatchCalls loop.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Interrupt()
|
|
|
|
|
{
|
|
|
|
|
_interrupt = true;
|
2020-07-24 04:53:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes of resources used for GPFifo command processing.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose() => _event.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|