Fix MME shadow RAM implementation (#1051)

This commit is contained in:
gdkchan 2020-04-25 10:56:56 -03:00 committed by GitHub
parent 1c9aba6de1
commit 9261ec6bc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 59 deletions

View file

@ -82,8 +82,7 @@ namespace Ryujinx.Graphics.Gpu
/// <param name="param">Optional argument passed to the program, 0 if not used</param> /// <param name="param">Optional argument passed to the program, 0 if not used</param>
/// <param name="shadowCtrl">Shadow RAM control register value</param> /// <param name="shadowCtrl">Shadow RAM control register value</param>
/// <param name="state">Current GPU state</param> /// <param name="state">Current GPU state</param>
/// <param name="shadowState">Shadow GPU state</param> public void Execute(int[] mme, int position, int param, ShadowRamControl shadowCtrl, GpuState state)
public void Execute(int[] mme, int position, int param, ShadowRamControl shadowCtrl, GpuState state, GpuState shadowState)
{ {
Reset(); Reset();
@ -95,11 +94,11 @@ namespace Ryujinx.Graphics.Gpu
FetchOpCode(mme); FetchOpCode(mme);
while (Step(mme, state, shadowState)); while (Step(mme, state));
// Due to the delay slot, we still need to execute // Due to the delay slot, we still need to execute
// one more instruction before we actually exit. // one more instruction before we actually exit.
Step(mme, state, shadowState); Step(mme, state);
} }
/// <summary> /// <summary>
@ -124,9 +123,8 @@ namespace Ryujinx.Graphics.Gpu
/// </summary> /// </summary>
/// <param name="mme">Program code to execute</param> /// <param name="mme">Program code to execute</param>
/// <param name="state">Current GPU state</param> /// <param name="state">Current GPU state</param>
/// <param name="shadowState">Shadow GPU state</param>
/// <returns>True to continue execution, false if the program exited</returns> /// <returns>True to continue execution, false if the program exited</returns>
private bool Step(int[] mme, GpuState state, GpuState shadowState) private bool Step(int[] mme, GpuState state)
{ {
int baseAddr = _pc - 1; int baseAddr = _pc - 1;
@ -172,7 +170,7 @@ namespace Ryujinx.Graphics.Gpu
{ {
SetDstGpr(FetchParam()); SetDstGpr(FetchParam());
Send(state, shadowState, result); Send(state, result);
break; break;
} }
@ -182,7 +180,7 @@ namespace Ryujinx.Graphics.Gpu
{ {
SetDstGpr(result); SetDstGpr(result);
Send(state, shadowState, result); Send(state, result);
break; break;
} }
@ -204,7 +202,7 @@ namespace Ryujinx.Graphics.Gpu
SetMethAddr(result); SetMethAddr(result);
Send(state, shadowState, FetchParam()); Send(state, FetchParam());
break; break;
} }
@ -216,7 +214,7 @@ namespace Ryujinx.Graphics.Gpu
SetMethAddr(result); SetMethAddr(result);
Send(state, shadowState,(result >> 12) & 0x3f); Send(state, (result >> 12) & 0x3f);
break; break;
} }
@ -489,24 +487,12 @@ namespace Ryujinx.Graphics.Gpu
/// Performs a GPU method call. /// Performs a GPU method call.
/// </summary> /// </summary>
/// <param name="state">Current GPU state</param> /// <param name="state">Current GPU state</param>
/// <param name="shadowState">Shadow GPU state</param>
/// <param name="value">Call argument</param> /// <param name="value">Call argument</param>
private void Send(GpuState state, GpuState shadowState, int value) private void Send(GpuState state, int value)
{ {
// TODO: Figure out what TrackWithFilter does, compared to Track.
if (_shadowCtrl == ShadowRamControl.Track ||
_shadowCtrl == ShadowRamControl.TrackWithFilter)
{
shadowState.Write(_methAddr, value);
}
else if (_shadowCtrl == ShadowRamControl.Replay)
{
value = shadowState.Read(_methAddr);
}
MethodParams meth = new MethodParams(_methAddr, value); MethodParams meth = new MethodParams(_methAddr, value);
state.CallMethod(meth); state.CallMethod(meth, _shadowCtrl);
_methAddr += _methIncr; _methAddr += _methIncr;
} }

View file

@ -62,13 +62,13 @@ namespace Ryujinx.Graphics.Gpu
/// </summary> /// </summary>
/// <param name="mme">Program code</param> /// <param name="mme">Program code</param>
/// <param name="state">Current GPU state</param> /// <param name="state">Current GPU state</param>
public void Execute(int[] mme, ShadowRamControl shadowCtrl, GpuState state, GpuState shadowState) public void Execute(int[] mme, ShadowRamControl shadowCtrl, GpuState state)
{ {
if (_executionPending) if (_executionPending)
{ {
_executionPending = false; _executionPending = false;
_interpreter?.Execute(mme, Position, _argument, shadowCtrl, state, shadowState); _interpreter?.Execute(mme, Position, _argument, shadowCtrl, state);
} }
} }
@ -101,11 +101,6 @@ namespace Ryujinx.Graphics.Gpu
/// </summary> /// </summary>
public GpuState State { get; } public GpuState State { get; }
/// <summary>
/// Sub-channel shadow GPU state (used as backup storage to restore MME changes).
/// </summary>
public GpuState ShadowState { get; }
/// <summary> /// <summary>
/// Engine bound to the sub-channel. /// Engine bound to the sub-channel.
/// </summary> /// </summary>
@ -117,7 +112,6 @@ namespace Ryujinx.Graphics.Gpu
public SubChannel() public SubChannel()
{ {
State = new GpuState(); State = new GpuState();
ShadowState = new GpuState();
} }
} }
@ -202,11 +196,7 @@ namespace Ryujinx.Graphics.Gpu
} }
else if (meth.Method < 0xe00) else if (meth.Method < 0xe00)
{ {
SubChannel sc = _subChannels[meth.SubChannel]; _subChannels[meth.SubChannel].State.CallMethod(meth, _shadowCtrl);
sc.ShadowState.Write(meth.Method, meth.Argument);
sc.State.CallMethod(meth);
} }
else else
{ {
@ -223,9 +213,7 @@ namespace Ryujinx.Graphics.Gpu
if (meth.IsLastCall) if (meth.IsLastCall)
{ {
SubChannel sc = _subChannels[meth.SubChannel]; _macros[macroIndex].Execute(_mme, _shadowCtrl, _subChannels[meth.SubChannel].State);
_macros[macroIndex].Execute(_mme, _shadowCtrl, sc.State, sc.ShadowState);
_context.Methods.PerformDeferredDraws(); _context.Methods.PerformDeferredDraws();
} }

View file

@ -12,7 +12,8 @@ namespace Ryujinx.Graphics.Gpu.State
public delegate void MethodCallback(GpuState state, int argument); public delegate void MethodCallback(GpuState state, int argument);
private int[] _backingMemory; private readonly int[] _memory;
private readonly int[] _shadow;
/// <summary> /// <summary>
/// GPU register information. /// GPU register information.
@ -29,14 +30,15 @@ namespace Ryujinx.Graphics.Gpu.State
public bool Modified; public bool Modified;
} }
private Register[] _registers; private readonly Register[] _registers;
/// <summary> /// <summary>
/// Creates a new instance of the GPU state. /// Creates a new instance of the GPU state.
/// </summary> /// </summary>
public GpuState() public GpuState()
{ {
_backingMemory = new int[RegistersCount]; _memory = new int[RegistersCount];
_shadow = new int[RegistersCount];
_registers = new Register[RegistersCount]; _registers = new Register[RegistersCount];
@ -62,25 +64,40 @@ namespace Ryujinx.Graphics.Gpu.State
} }
} }
InitializeDefaultState(); InitializeDefaultState(_memory);
InitializeDefaultState(_shadow);
} }
/// <summary> /// <summary>
/// Calls a GPU method, using this state. /// Calls a GPU method, using this state.
/// </summary> /// </summary>
/// <param name="meth">The GPU method to be called</param> /// <param name="meth">The GPU method to be called</param>
public void CallMethod(MethodParams meth) /// <param name="shadowCtrl">Shadow RAM control register value</param>
public void CallMethod(MethodParams meth, ShadowRamControl shadowCtrl)
{ {
int value = meth.Argument;
// TODO: Figure out what TrackWithFilter does, compared to Track.
if (shadowCtrl == ShadowRamControl.Track ||
shadowCtrl == ShadowRamControl.TrackWithFilter)
{
_shadow[meth.Method] = value;
}
else if (shadowCtrl == ShadowRamControl.Replay)
{
value = _shadow[meth.Method];
}
Register register = _registers[meth.Method]; Register register = _registers[meth.Method];
if (_backingMemory[meth.Method] != meth.Argument) if (_memory[meth.Method] != value)
{ {
_registers[(int)register.BaseOffset].Modified = true; _registers[(int)register.BaseOffset].Modified = true;
} }
_backingMemory[meth.Method] = meth.Argument; _memory[meth.Method] = value;
register.Callback?.Invoke(this, meth.Argument); register.Callback?.Invoke(this, value);
} }
/// <summary> /// <summary>
@ -90,7 +107,7 @@ namespace Ryujinx.Graphics.Gpu.State
/// <returns>Data at the register</returns> /// <returns>Data at the register</returns>
public int Read(int offset) public int Read(int offset)
{ {
return _backingMemory[offset]; return _memory[offset];
} }
/// <summary> /// <summary>
@ -100,7 +117,7 @@ namespace Ryujinx.Graphics.Gpu.State
/// <param name="value">Value to be written</param> /// <param name="value">Value to be written</param>
public void Write(int offset, int value) public void Write(int offset, int value)
{ {
_backingMemory[offset] = value; _memory[offset] = value;
} }
/// <summary> /// <summary>
@ -109,29 +126,29 @@ namespace Ryujinx.Graphics.Gpu.State
/// <param name="offset">The offset to be written</param> /// <param name="offset">The offset to be written</param>
public void SetUniformBufferOffset(int offset) public void SetUniformBufferOffset(int offset)
{ {
_backingMemory[(int)MethodOffset.UniformBufferState + 3] = offset; _memory[(int)MethodOffset.UniformBufferState + 3] = offset;
} }
/// <summary> /// <summary>
/// Initializes registers with the default state. /// Initializes registers with the default state.
/// </summary> /// </summary>
private void InitializeDefaultState() private static void InitializeDefaultState(int[] memory)
{ {
// Enable Rasterizer // Enable Rasterizer
_backingMemory[(int)MethodOffset.RasterizeEnable] = 1; memory[(int)MethodOffset.RasterizeEnable] = 1;
// Depth ranges. // Depth ranges.
for (int index = 0; index < Constants.TotalViewports; index++) for (int index = 0; index < Constants.TotalViewports; index++)
{ {
_backingMemory[(int)MethodOffset.ViewportExtents + index * 4 + 2] = 0; memory[(int)MethodOffset.ViewportExtents + index * 4 + 2] = 0;
_backingMemory[(int)MethodOffset.ViewportExtents + index * 4 + 3] = 0x3F800000; memory[(int)MethodOffset.ViewportExtents + index * 4 + 3] = 0x3F800000;
} }
// Viewport transform enable. // Viewport transform enable.
_backingMemory[(int)MethodOffset.ViewportTransformEnable] = 1; memory[(int)MethodOffset.ViewportTransformEnable] = 1;
// Default front stencil mask. // Default front stencil mask.
_backingMemory[0x4e7] = 0xff; memory[0x4e7] = 0xff;
// Conditional rendering condition. // Conditional rendering condition.
_backingMemory[0x556] = (int)Condition.Always; _backingMemory[0x556] = (int)Condition.Always;
@ -139,7 +156,7 @@ namespace Ryujinx.Graphics.Gpu.State
// Default color mask. // Default color mask.
for (int index = 0; index < Constants.TotalRenderTargets; index++) for (int index = 0; index < Constants.TotalRenderTargets; index++)
{ {
_backingMemory[(int)MethodOffset.RtColorMask + index] = 0x1111; memory[(int)MethodOffset.RtColorMask + index] = 0x1111;
} }
// Default blend states // Default blend states
@ -342,7 +359,7 @@ namespace Ryujinx.Graphics.Gpu.State
/// <returns>The data at the specified location</returns> /// <returns>The data at the specified location</returns>
public T Get<T>(MethodOffset offset) where T : struct public T Get<T>(MethodOffset offset) where T : struct
{ {
return MemoryMarshal.Cast<int, T>(_backingMemory.AsSpan().Slice((int)offset))[0]; return MemoryMarshal.Cast<int, T>(_memory.AsSpan().Slice((int)offset))[0];
} }
/// <summary> /// <summary>