2019-11-21 18:46:14 -08:00
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
2020-03-12 18:30:26 -07:00
|
|
|
using System.IO;
|
2019-11-21 18:46:14 -08:00
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
namespace Ryujinx.Graphics.Gpu
|
|
|
|
{
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
|
|
|
/// GPU commands FIFO.
|
|
|
|
/// </summary>
|
2019-10-12 23:02:07 -07:00
|
|
|
class NvGpuFifo
|
|
|
|
{
|
|
|
|
private const int MacrosCount = 0x80;
|
|
|
|
private const int MacroIndexMask = MacrosCount - 1;
|
|
|
|
|
|
|
|
// 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 MmeWords = 256 * 256;
|
|
|
|
|
|
|
|
private GpuContext _context;
|
|
|
|
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Cached GPU macro program.
|
|
|
|
/// </summary>
|
2019-10-12 23:02:07 -07:00
|
|
|
private struct CachedMacro
|
|
|
|
{
|
2019-12-31 14:09:49 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Word offset of the code on the code memory.
|
|
|
|
/// </summary>
|
2019-12-31 12:08:20 -08:00
|
|
|
public int Position { get; }
|
2019-10-12 23:02:07 -07:00
|
|
|
|
|
|
|
private bool _executionPending;
|
|
|
|
private int _argument;
|
|
|
|
|
|
|
|
private MacroInterpreter _interpreter;
|
|
|
|
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the GPU cached macro program.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="position">Macro code start position</param>
|
|
|
|
public CachedMacro(int position)
|
2019-10-12 23:02:07 -07:00
|
|
|
{
|
|
|
|
Position = position;
|
|
|
|
|
|
|
|
_executionPending = false;
|
|
|
|
_argument = 0;
|
|
|
|
|
2019-12-31 12:08:20 -08:00
|
|
|
_interpreter = new MacroInterpreter();
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Sets the first argument for the macro call.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="argument">First argument</param>
|
2019-10-12 23:02:07 -07:00
|
|
|
public void StartExecution(int argument)
|
|
|
|
{
|
|
|
|
_argument = argument;
|
|
|
|
|
|
|
|
_executionPending = true;
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Starts executing the macro program code.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mme">Program code</param>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2020-03-12 18:30:26 -07:00
|
|
|
public void Execute(int[] mme, ShadowRamControl shadowCtrl, GpuState state, GpuState shadowState)
|
2019-10-12 23:02:07 -07:00
|
|
|
{
|
|
|
|
if (_executionPending)
|
|
|
|
{
|
|
|
|
_executionPending = false;
|
|
|
|
|
2020-03-12 18:30:26 -07:00
|
|
|
_interpreter?.Execute(mme, Position, _argument, shadowCtrl, state, shadowState);
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
2020-01-01 15:14:18 -08:00
|
|
|
/// Pushes an argument to the macro call argument FIFO.
|
2019-12-31 12:08:20 -08:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="argument">Argument to be pushed</param>
|
2019-10-12 23:02:07 -07:00
|
|
|
public void PushArgument(int argument)
|
|
|
|
{
|
|
|
|
_interpreter?.Fifo.Enqueue(argument);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int _currMacroPosition;
|
|
|
|
private int _currMacroBindIndex;
|
|
|
|
|
2020-03-12 18:30:26 -07:00
|
|
|
private ShadowRamControl _shadowCtrl;
|
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
private CachedMacro[] _macros;
|
|
|
|
|
|
|
|
private int[] _mme;
|
|
|
|
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
|
|
|
/// GPU sub-channel information.
|
|
|
|
/// </summary>
|
2019-11-21 18:46:14 -08:00
|
|
|
private class SubChannel
|
|
|
|
{
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Sub-channel GPU state.
|
|
|
|
/// </summary>
|
2019-11-21 18:46:14 -08:00
|
|
|
public GpuState State { get; }
|
2019-12-31 12:08:20 -08:00
|
|
|
|
2020-03-12 18:30:26 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Sub-channel shadow GPU state (used as backup storage to restore MME changes).
|
|
|
|
/// </summary>
|
|
|
|
public GpuState ShadowState { get; }
|
|
|
|
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Engine bound to the sub-channel.
|
|
|
|
/// </summary>
|
2019-11-21 18:46:14 -08:00
|
|
|
public ClassId Class { get; set; }
|
|
|
|
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the GPU sub-channel.
|
|
|
|
/// </summary>
|
2019-11-21 18:46:14 -08:00
|
|
|
public SubChannel()
|
|
|
|
{
|
|
|
|
State = new GpuState();
|
2020-03-12 18:30:26 -07:00
|
|
|
ShadowState = new GpuState();
|
2019-11-21 18:46:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private SubChannel[] _subChannels;
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2020-04-18 18:25:57 -07:00
|
|
|
private SubChannel _fifoChannel;
|
|
|
|
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the GPU commands FIFO.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">GPU emulation context</param>
|
2019-10-12 23:02:07 -07:00
|
|
|
public NvGpuFifo(GpuContext context)
|
|
|
|
{
|
|
|
|
_context = context;
|
|
|
|
|
|
|
|
_macros = new CachedMacro[MacrosCount];
|
|
|
|
|
|
|
|
_mme = new int[MmeWords];
|
|
|
|
|
2020-04-18 18:25:57 -07:00
|
|
|
_fifoChannel = new SubChannel();
|
|
|
|
|
|
|
|
_context.Methods.RegisterCallbacksForFifo(_fifoChannel.State);
|
|
|
|
|
2019-11-21 18:46:14 -08:00
|
|
|
_subChannels = new SubChannel[8];
|
|
|
|
|
|
|
|
for (int index = 0; index < _subChannels.Length; index++)
|
|
|
|
{
|
|
|
|
_subChannels[index] = new SubChannel();
|
|
|
|
|
2020-04-18 18:25:57 -07:00
|
|
|
_context.Methods.RegisterCallbacks(_subChannels[index].State);
|
2019-11-21 18:46:14 -08:00
|
|
|
}
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
|
2020-04-18 18:25:57 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Send macro code/data to the MME
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">The index in the MME</param>
|
|
|
|
/// <param name="data">The data to use</param>
|
|
|
|
public void SendMacroCodeData(int index, int data)
|
|
|
|
{
|
|
|
|
_mme[index] = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Bind a macro index to a position for the MME
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">The macro index</param>
|
|
|
|
/// <param name="position">The position of the macro</param>
|
|
|
|
public void BindMacro(int index, int position)
|
|
|
|
{
|
|
|
|
_macros[index] = new CachedMacro(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Change the shadow RAM setting
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="shadowCtrl">The new Shadow RAM setting</param>
|
|
|
|
public void SetMmeShadowRamControl(ShadowRamControl shadowCtrl)
|
|
|
|
{
|
|
|
|
_shadowCtrl = shadowCtrl;
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:08:20 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Calls a GPU method.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="meth">GPU method call parameters</param>
|
2019-10-12 23:02:07 -07:00
|
|
|
public void CallMethod(MethodParams meth)
|
|
|
|
{
|
2020-04-18 18:25:57 -07:00
|
|
|
if ((MethodOffset)meth.Method == MethodOffset.BindChannel)
|
2019-10-12 23:02:07 -07:00
|
|
|
{
|
2020-04-18 18:25:57 -07:00
|
|
|
_subChannels[meth.SubChannel] = new SubChannel
|
|
|
|
{
|
|
|
|
Class = (ClassId)meth.Argument
|
|
|
|
};
|
|
|
|
|
|
|
|
_context.Methods.RegisterCallbacks(_subChannels[meth.SubChannel].State);
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
else if (meth.Method < 0x60)
|
|
|
|
{
|
2020-04-18 18:25:57 -07:00
|
|
|
// TODO: check if macros are shared between subchannels or not. For now let's assume they are.
|
|
|
|
_fifoChannel.State.CallMethod(meth);
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
else if (meth.Method < 0xe00)
|
|
|
|
{
|
2020-03-12 18:30:26 -07:00
|
|
|
SubChannel sc = _subChannels[meth.SubChannel];
|
|
|
|
|
|
|
|
sc.ShadowState.Write(meth.Method, meth.Argument);
|
|
|
|
|
|
|
|
sc.State.CallMethod(meth);
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int macroIndex = (meth.Method >> 1) & MacroIndexMask;
|
|
|
|
|
|
|
|
if ((meth.Method & 1) != 0)
|
|
|
|
{
|
|
|
|
_macros[macroIndex].PushArgument(meth.Argument);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_macros[macroIndex].StartExecution(meth.Argument);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meth.IsLastCall)
|
|
|
|
{
|
2020-03-12 18:30:26 -07:00
|
|
|
SubChannel sc = _subChannels[meth.SubChannel];
|
|
|
|
|
|
|
|
_macros[macroIndex].Execute(_mme, _shadowCtrl, sc.State, sc.ShadowState);
|
2019-10-12 23:02:07 -07:00
|
|
|
|
|
|
|
_context.Methods.PerformDeferredDraws();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|