using System;
using System.Runtime.InteropServices;
namespace Ryujinx.Graphics.Gpu.State
{
///
/// GPU state.
///
class GpuState
{
private const int RegistersCount = 0xe00;
public delegate void MethodCallback(GpuState state, int argument);
private readonly int[] _memory;
private readonly int[] _shadow;
///
/// GPU register information.
///
private struct Register
{
public MethodCallback Callback;
public MethodOffset BaseOffset;
public int Stride;
public int Count;
public bool Modified;
}
private readonly Register[] _registers;
///
/// Creates a new instance of the GPU state.
///
public GpuState()
{
_memory = new int[RegistersCount];
_shadow = new int[RegistersCount];
_registers = new Register[RegistersCount];
for (int index = 0; index < _registers.Length; index++)
{
_registers[index].BaseOffset = (MethodOffset)index;
_registers[index].Stride = 1;
_registers[index].Count = 1;
_registers[index].Modified = true;
}
foreach (var item in GpuStateTable.Table)
{
int totalRegs = item.Size * item.Count;
for (int regOffset = 0; regOffset < totalRegs; regOffset++)
{
int index = (int)item.Offset + regOffset;
_registers[index].BaseOffset = item.Offset;
_registers[index].Stride = item.Size;
_registers[index].Count = item.Count;
}
}
InitializeDefaultState(_memory);
InitializeDefaultState(_shadow);
}
///
/// Calls a GPU method, using this state.
///
/// The GPU method to be called
/// Shadow RAM control register value
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];
if (_memory[meth.Method] != value)
{
_registers[(int)register.BaseOffset].Modified = true;
}
_memory[meth.Method] = value;
register.Callback?.Invoke(this, value);
}
///
/// Reads data from a GPU register at the given offset.
///
/// Offset to be read
/// Data at the register
public int Read(int offset)
{
return _memory[offset];
}
///
/// Writes data to the GPU register at the given offset.
///
/// Offset to be written
/// Value to be written
public void Write(int offset, int value)
{
_memory[offset] = value;
}
///
/// Writes an offset value at the uniform buffer offset register.
///
/// The offset to be written
public void SetUniformBufferOffset(int offset)
{
_memory[(int)MethodOffset.UniformBufferState + 3] = offset;
}
///
/// Initializes registers with the default state.
///
private static void InitializeDefaultState(int[] memory)
{
// Enable Rasterizer
memory[(int)MethodOffset.RasterizeEnable] = 1;
// Depth ranges.
for (int index = 0; index < Constants.TotalViewports; index++)
{
memory[(int)MethodOffset.ViewportExtents + index * 4 + 2] = 0;
memory[(int)MethodOffset.ViewportExtents + index * 4 + 3] = 0x3F800000;
}
// Viewport transform enable.
memory[(int)MethodOffset.ViewportTransformEnable] = 1;
// Default front stencil mask.
memory[0x4e7] = 0xff;
// Conditional rendering condition.
_backingMemory[0x556] = (int)Condition.Always;
// Default color mask.
for (int index = 0; index < Constants.TotalRenderTargets; index++)
{
memory[(int)MethodOffset.RtColorMask + index] = 0x1111;
}
// Default blend states
Set(MethodOffset.BlendStateCommon, BlendStateCommon.Default);
for (int index = 0; index < Constants.TotalRenderTargets; index++)
{
Set(MethodOffset.BlendState, index, BlendState.Default);
}
}
///
/// Registers a callback that is called every time a GPU method, or methods are called.
///
/// Offset of the method
/// Word count of the methods region
/// Calllback to be called
public void RegisterCallback(MethodOffset offset, int count, MethodCallback callback)
{
for (int index = 0; index < count; index++)
{
_registers[(int)offset + index].Callback = callback;
}
}
///
/// Registers a callback that is called every time a GPU method is called.
///
/// Offset of the method
/// Calllback to be called
public void RegisterCallback(MethodOffset offset, MethodCallback callback)
{
_registers[(int)offset].Callback = callback;
}
///
/// Checks if a given register has been modified since the last call to this method.
///
/// Register offset
/// True if modified, false otherwise
public bool QueryModified(MethodOffset offset)
{
bool modified = _registers[(int)offset].Modified;
_registers[(int)offset].Modified = false;
return modified;
}
///
/// Checks if two registers have been modified since the last call to this method.
///
/// First register offset
/// Second register offset
/// True if any register was modified, false otherwise
public bool QueryModified(MethodOffset m1, MethodOffset m2)
{
bool modified = _registers[(int)m1].Modified ||
_registers[(int)m2].Modified;
_registers[(int)m1].Modified = false;
_registers[(int)m2].Modified = false;
return modified;
}
///
/// Checks if three registers have been modified since the last call to this method.
///
/// First register offset
/// Second register offset
/// Third register offset
/// True if any register was modified, false otherwise
public bool QueryModified(MethodOffset m1, MethodOffset m2, MethodOffset m3)
{
bool modified = _registers[(int)m1].Modified ||
_registers[(int)m2].Modified ||
_registers[(int)m3].Modified;
_registers[(int)m1].Modified = false;
_registers[(int)m2].Modified = false;
_registers[(int)m3].Modified = false;
return modified;
}
///
/// Checks if four registers have been modified since the last call to this method.
///
/// First register offset
/// Second register offset
/// Third register offset
/// Fourth register offset
/// True if any register was modified, false otherwise
public bool QueryModified(MethodOffset m1, MethodOffset m2, MethodOffset m3, MethodOffset m4)
{
bool modified = _registers[(int)m1].Modified ||
_registers[(int)m2].Modified ||
_registers[(int)m3].Modified ||
_registers[(int)m4].Modified;
_registers[(int)m1].Modified = false;
_registers[(int)m2].Modified = false;
_registers[(int)m3].Modified = false;
_registers[(int)m4].Modified = false;
return modified;
}
///
/// Checks if five registers have been modified since the last call to this method.
///
/// First register offset
/// Second register offset
/// Third register offset
/// Fourth register offset
/// Fifth register offset
/// True if any register was modified, false otherwise
public bool QueryModified(
MethodOffset m1,
MethodOffset m2,
MethodOffset m3,
MethodOffset m4,
MethodOffset m5)
{
bool modified = _registers[(int)m1].Modified ||
_registers[(int)m2].Modified ||
_registers[(int)m3].Modified ||
_registers[(int)m4].Modified ||
_registers[(int)m5].Modified;
_registers[(int)m1].Modified = false;
_registers[(int)m2].Modified = false;
_registers[(int)m3].Modified = false;
_registers[(int)m4].Modified = false;
_registers[(int)m5].Modified = false;
return modified;
}
///
/// Checks if six registers have been modified since the last call to this method.
///
/// First register offset
/// Second register offset
/// Third register offset
/// Fourth register offset
/// Fifth register offset
/// Sixth register offset
/// True if any register was modified, false otherwise
public bool QueryModified(
MethodOffset m1,
MethodOffset m2,
MethodOffset m3,
MethodOffset m4,
MethodOffset m5,
MethodOffset m6)
{
bool modified = _registers[(int)m1].Modified ||
_registers[(int)m2].Modified ||
_registers[(int)m3].Modified ||
_registers[(int)m4].Modified ||
_registers[(int)m5].Modified ||
_registers[(int)m6].Modified;
_registers[(int)m1].Modified = false;
_registers[(int)m2].Modified = false;
_registers[(int)m3].Modified = false;
_registers[(int)m4].Modified = false;
_registers[(int)m5].Modified = false;
_registers[(int)m6].Modified = false;
return modified;
}
///
/// Gets indexed data from a given register offset.
///
/// Type of the data
/// Register offset
/// Index for indexed data
/// The data at the specified location
public T Get(MethodOffset offset, int index) where T : struct
{
Register register = _registers[(int)offset];
if ((uint)index >= register.Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
return Get(offset + index * register.Stride);
}
///
/// Gets data from a given register offset.
///
/// Type of the data
/// Register offset
/// The data at the specified location
public T Get(MethodOffset offset) where T : struct
{
return MemoryMarshal.Cast(_memory.AsSpan().Slice((int)offset))[0];
}
///
/// Sets indexed data to a given register offset.
///
/// Type of the data
/// Register offset
/// Index for indexed data
/// The data to set
public void Set(MethodOffset offset, int index, T data) where T : struct
{
Register register = _registers[(int)offset];
if ((uint)index >= register.Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
Set(offset + index * register.Stride, data);
}
///
/// Sets data to a given register offset.
///
/// Type of the data
/// Register offset
/// The data to set
public void Set(MethodOffset offset, T data) where T : struct
{
ReadOnlySpan intSpan = MemoryMarshal.Cast(MemoryMarshal.CreateReadOnlySpan(ref data, 1));
intSpan.CopyTo(_backingMemory.AsSpan().Slice((int)offset, intSpan.Length));
}
}
}