2020-07-23 22:53:25 -04:00
|
|
|
|
using Ryujinx.Graphics.Device;
|
|
|
|
|
using Ryujinx.Graphics.Gpu.Engine.MME;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine.GPFifo
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a GPU General Purpose FIFO class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
class GPFifoClass : IDeviceState
|
|
|
|
|
{
|
|
|
|
|
private readonly GpuContext _context;
|
2020-08-02 21:36:57 -04:00
|
|
|
|
private readonly GPFifoProcessor _parent;
|
2020-07-23 22:53:25 -04:00
|
|
|
|
private readonly DeviceState<GPFifoClassState> _state;
|
|
|
|
|
|
|
|
|
|
private const int MacrosCount = 0x80;
|
|
|
|
|
|
|
|
|
|
// Note: The size of the macro memory is unknown, we just make
|
|
|
|
|
// a guess here and use 256kb as the size. Increase if needed.
|
|
|
|
|
private const int MacroCodeSize = 256 * 256;
|
|
|
|
|
|
|
|
|
|
private readonly Macro[] _macros;
|
|
|
|
|
private readonly int[] _macroCode;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new instance of the GPU General Purpose FIFO class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context">GPU context</param>
|
2020-08-02 21:36:57 -04:00
|
|
|
|
/// <param name="parent">Parent GPU General Purpose FIFO processor</param>
|
|
|
|
|
public GPFifoClass(GpuContext context, GPFifoProcessor parent)
|
2020-07-23 22:53:25 -04:00
|
|
|
|
{
|
|
|
|
|
_context = context;
|
2020-08-02 21:36:57 -04:00
|
|
|
|
_parent = parent;
|
2020-07-23 22:53:25 -04:00
|
|
|
|
_state = new DeviceState<GPFifoClassState>(new Dictionary<string, RwCallback>
|
|
|
|
|
{
|
|
|
|
|
{ nameof(GPFifoClassState.Semaphored), new RwCallback(Semaphored, null) },
|
|
|
|
|
{ nameof(GPFifoClassState.Syncpointb), new RwCallback(Syncpointb, null) },
|
|
|
|
|
{ nameof(GPFifoClassState.WaitForIdle), new RwCallback(WaitForIdle, null) },
|
2021-01-17 15:08:06 -05:00
|
|
|
|
{ nameof(GPFifoClassState.SetReference), new RwCallback(SetReference, null) },
|
2020-07-23 22:53:25 -04:00
|
|
|
|
{ nameof(GPFifoClassState.LoadMmeInstructionRam), new RwCallback(LoadMmeInstructionRam, null) },
|
|
|
|
|
{ nameof(GPFifoClassState.LoadMmeStartAddressRam), new RwCallback(LoadMmeStartAddressRam, null) },
|
|
|
|
|
{ nameof(GPFifoClassState.SetMmeShadowRamControl), new RwCallback(SetMmeShadowRamControl, null) }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_macros = new Macro[MacrosCount];
|
|
|
|
|
_macroCode = new int[MacroCodeSize];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads data from the class registers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="offset">Register byte offset</param>
|
|
|
|
|
/// <returns>Data at the specified offset</returns>
|
|
|
|
|
public int Read(int offset) => _state.Read(offset);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes data to the class registers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="offset">Register byte offset</param>
|
|
|
|
|
/// <param name="data">Data to be written</param>
|
|
|
|
|
public void Write(int offset, int data) => _state.Write(offset, data);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes a GPU counter to guest memory.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
|
public void Semaphored(int argument)
|
|
|
|
|
{
|
|
|
|
|
ulong address = ((ulong)_state.State.SemaphorebOffsetLower << 2) |
|
|
|
|
|
((ulong)_state.State.SemaphoreaOffsetUpper << 32);
|
|
|
|
|
|
|
|
|
|
int value = _state.State.SemaphorecPayload;
|
|
|
|
|
|
|
|
|
|
SemaphoredOperation operation = _state.State.SemaphoredOperation;
|
|
|
|
|
|
2022-01-27 16:50:32 -05:00
|
|
|
|
if (_state.State.SemaphoredReleaseSize == SemaphoredReleaseSize.SixteenBytes)
|
|
|
|
|
{
|
|
|
|
|
_parent.MemoryManager.Write(address + 4, 0);
|
|
|
|
|
_parent.MemoryManager.Write(address + 8, _context.GetTimestamp());
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-23 22:53:25 -04:00
|
|
|
|
// TODO: Acquire operations (Wait), interrupts for invalid combinations.
|
|
|
|
|
if (operation == SemaphoredOperation.Release)
|
|
|
|
|
{
|
2021-06-29 13:32:02 -04:00
|
|
|
|
_parent.MemoryManager.Write(address, value);
|
2020-07-23 22:53:25 -04:00
|
|
|
|
}
|
|
|
|
|
else if (operation == SemaphoredOperation.Reduction)
|
|
|
|
|
{
|
|
|
|
|
bool signed = _state.State.SemaphoredFormat == SemaphoredFormat.Signed;
|
|
|
|
|
|
2021-06-29 13:32:02 -04:00
|
|
|
|
int mem = _parent.MemoryManager.Read<int>(address);
|
2020-07-23 22:53:25 -04:00
|
|
|
|
|
|
|
|
|
switch (_state.State.SemaphoredReduction)
|
|
|
|
|
{
|
|
|
|
|
case SemaphoredReduction.Min:
|
|
|
|
|
value = signed ? Math.Min(mem, value) : (int)Math.Min((uint)mem, (uint)value);
|
|
|
|
|
break;
|
|
|
|
|
case SemaphoredReduction.Max:
|
|
|
|
|
value = signed ? Math.Max(mem, value) : (int)Math.Max((uint)mem, (uint)value);
|
|
|
|
|
break;
|
|
|
|
|
case SemaphoredReduction.Xor:
|
|
|
|
|
value ^= mem;
|
|
|
|
|
break;
|
|
|
|
|
case SemaphoredReduction.And:
|
|
|
|
|
value &= mem;
|
|
|
|
|
break;
|
|
|
|
|
case SemaphoredReduction.Or:
|
|
|
|
|
value |= mem;
|
|
|
|
|
break;
|
|
|
|
|
case SemaphoredReduction.Add:
|
|
|
|
|
value += mem;
|
|
|
|
|
break;
|
|
|
|
|
case SemaphoredReduction.Inc:
|
|
|
|
|
value = (uint)mem < (uint)value ? mem + 1 : 0;
|
|
|
|
|
break;
|
|
|
|
|
case SemaphoredReduction.Dec:
|
|
|
|
|
value = (uint)mem > 0 && (uint)mem <= (uint)value ? mem - 1 : value;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 13:32:02 -04:00
|
|
|
|
_parent.MemoryManager.Write(address, value);
|
2020-07-23 22:53:25 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Apply a fence operation on a syncpoint.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
|
public void Syncpointb(int argument)
|
|
|
|
|
{
|
|
|
|
|
SyncpointbOperation operation = _state.State.SyncpointbOperation;
|
|
|
|
|
|
|
|
|
|
uint syncpointId = (uint)_state.State.SyncpointbSyncptIndex;
|
|
|
|
|
|
|
|
|
|
if (operation == SyncpointbOperation.Wait)
|
|
|
|
|
{
|
|
|
|
|
uint threshold = (uint)_state.State.SyncpointaPayload;
|
|
|
|
|
|
|
|
|
|
_context.Synchronization.WaitOnSyncpoint(syncpointId, threshold, Timeout.InfiniteTimeSpan);
|
|
|
|
|
}
|
|
|
|
|
else if (operation == SyncpointbOperation.Incr)
|
|
|
|
|
{
|
2022-01-09 11:28:48 -05:00
|
|
|
|
_context.CreateHostSyncIfNeeded(true);
|
2020-07-23 22:53:25 -04:00
|
|
|
|
_context.Synchronization.IncrementSyncpoint(syncpointId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_context.AdvanceSequence();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Waits for the GPU to be idle.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
|
public void WaitForIdle(int argument)
|
|
|
|
|
{
|
2021-07-11 16:20:40 -04:00
|
|
|
|
_parent.PerformDeferredDraws();
|
2020-07-23 22:53:25 -04:00
|
|
|
|
_context.Renderer.Pipeline.Barrier();
|
2021-01-17 15:08:06 -05:00
|
|
|
|
|
2022-01-09 11:28:48 -05:00
|
|
|
|
_context.CreateHostSyncIfNeeded(false);
|
2021-01-17 15:08:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Used as an indirect data barrier on NVN. When used, access to previously written data must be coherent.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
|
public void SetReference(int argument)
|
|
|
|
|
{
|
2021-08-26 17:50:28 -04:00
|
|
|
|
_context.Renderer.Pipeline.CommandBufferBarrier();
|
|
|
|
|
|
2022-01-09 11:28:48 -05:00
|
|
|
|
_context.CreateHostSyncIfNeeded(false);
|
2020-07-23 22:53:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-02 21:36:57 -04:00
|
|
|
|
/// Sends macro code/data to the MME.
|
2020-07-23 22:53:25 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
|
public void LoadMmeInstructionRam(int argument)
|
|
|
|
|
{
|
|
|
|
|
_macroCode[_state.State.LoadMmeInstructionRamPointer++] = argument;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-02 21:36:57 -04:00
|
|
|
|
/// Binds a macro index to a position for the MME
|
2020-07-23 22:53:25 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
|
public void LoadMmeStartAddressRam(int argument)
|
|
|
|
|
{
|
|
|
|
|
_macros[_state.State.LoadMmeStartAddressRamPointer++] = new Macro(argument);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-02 21:36:57 -04:00
|
|
|
|
/// Changes the shadow RAM control.
|
2020-07-23 22:53:25 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="argument">Method call argument</param>
|
|
|
|
|
public void SetMmeShadowRamControl(int argument)
|
|
|
|
|
{
|
2021-07-11 16:20:40 -04:00
|
|
|
|
_parent.SetShadowRamControl(argument);
|
2020-07-23 22:53:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pushes an argument to a macro.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="index">Index of the macro</param>
|
2021-08-26 17:50:28 -04:00
|
|
|
|
/// <param name="gpuVa">GPU virtual address where the command word is located</param>
|
2020-07-23 22:53:25 -04:00
|
|
|
|
/// <param name="argument">Argument to be pushed to the macro</param>
|
2021-08-26 17:50:28 -04:00
|
|
|
|
public void MmePushArgument(int index, ulong gpuVa, int argument)
|
2020-07-23 22:53:25 -04:00
|
|
|
|
{
|
2021-08-26 17:50:28 -04:00
|
|
|
|
_macros[index].PushArgument(gpuVa, argument);
|
2020-07-23 22:53:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prepares a macro for execution.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="index">Index of the macro</param>
|
|
|
|
|
/// <param name="argument">Initial argument passed to the macro</param>
|
|
|
|
|
public void MmeStart(int index, int argument)
|
|
|
|
|
{
|
2021-08-26 17:50:28 -04:00
|
|
|
|
_macros[index].StartExecution(_context, _parent, _macroCode, argument);
|
2020-07-23 22:53:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes a macro.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="index">Index of the macro</param>
|
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2021-07-11 16:20:40 -04:00
|
|
|
|
public void CallMme(int index, IDeviceState state)
|
2020-07-23 22:53:25 -04:00
|
|
|
|
{
|
2020-08-02 21:36:57 -04:00
|
|
|
|
_macros[index].Execute(_macroCode, state);
|
2020-07-23 22:53:25 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|