Separate GPU engines (part 2/2) (#2440)
* 3D engine now uses DeviceState too, plus new state modification tracking * Remove old methods code * Remove GpuState and friends * Optimize DeviceState, force inline some functions * This change was not supposed to go in * Proper channel initialization * Optimize state read/write methods even more * Fix debug build * Do not dirty state if the write is redundant * The YControl register should dirty either the viewport or front face state too, to update the host origin * Avoid redundant vertex buffer updates * Move state and get rid of the Ryujinx.Graphics.Gpu.State namespace * Comments and nits * Fix rebase * PR feedback * Move changed = false to improve codegen * PR feedback * Carry RyuJIT a bit more
This commit is contained in:
parent
b5190f1681
commit
40b21cc3c4
111 changed files with 5262 additions and 4020 deletions
|
@ -1,10 +1,8 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Device
|
||||
{
|
||||
|
@ -14,28 +12,22 @@ namespace Ryujinx.Graphics.Device
|
|||
|
||||
public TState State;
|
||||
|
||||
private readonly BitArray _readableRegisters;
|
||||
private readonly BitArray _writableRegisters;
|
||||
private uint Size => (uint)(Unsafe.SizeOf<TState>() + RegisterSize - 1) / RegisterSize;
|
||||
|
||||
private readonly Dictionary<int, Func<int>> _readCallbacks;
|
||||
private readonly Dictionary<int, Action<int>> _writeCallbacks;
|
||||
private readonly Func<int>[] _readCallbacks;
|
||||
private readonly Action<int>[] _writeCallbacks;
|
||||
|
||||
private readonly Dictionary<int, string> _fieldNamesForDebug;
|
||||
private readonly Dictionary<uint, string> _fieldNamesForDebug;
|
||||
private readonly Action<string> _debugLogCallback;
|
||||
|
||||
public DeviceState(IReadOnlyDictionary<string, RwCallback> callbacks = null, Action<string> debugLogCallback = null)
|
||||
{
|
||||
int size = (Unsafe.SizeOf<TState>() + RegisterSize - 1) / RegisterSize;
|
||||
|
||||
_readableRegisters = new BitArray(size);
|
||||
_writableRegisters = new BitArray(size);
|
||||
|
||||
_readCallbacks = new Dictionary<int, Func<int>>();
|
||||
_writeCallbacks = new Dictionary<int, Action<int>>();
|
||||
_readCallbacks = new Func<int>[Size];
|
||||
_writeCallbacks = new Action<int>[Size];
|
||||
|
||||
if (debugLogCallback != null)
|
||||
{
|
||||
_fieldNamesForDebug = new Dictionary<int, string>();
|
||||
_fieldNamesForDebug = new Dictionary<uint, string>();
|
||||
_debugLogCallback = debugLogCallback;
|
||||
}
|
||||
|
||||
|
@ -45,32 +37,30 @@ namespace Ryujinx.Graphics.Device
|
|||
for (int fieldIndex = 0; fieldIndex < fields.Length; fieldIndex++)
|
||||
{
|
||||
var field = fields[fieldIndex];
|
||||
var regAttr = field.GetCustomAttributes<RegisterAttribute>(false).FirstOrDefault();
|
||||
|
||||
int sizeOfField = SizeCalculator.SizeOf(field.FieldType);
|
||||
|
||||
for (int i = 0; i < ((sizeOfField + 3) & ~3); i += 4)
|
||||
{
|
||||
_readableRegisters[(offset + i) / RegisterSize] = regAttr?.AccessControl.HasFlag(AccessControl.ReadOnly) ?? true;
|
||||
_writableRegisters[(offset + i) / RegisterSize] = regAttr?.AccessControl.HasFlag(AccessControl.WriteOnly) ?? true;
|
||||
}
|
||||
int index = (offset + i) / RegisterSize;
|
||||
|
||||
if (callbacks != null && callbacks.TryGetValue(field.Name, out var cb))
|
||||
{
|
||||
if (cb.Read != null)
|
||||
if (callbacks != null && callbacks.TryGetValue(field.Name, out var cb))
|
||||
{
|
||||
_readCallbacks.Add(offset, cb.Read);
|
||||
}
|
||||
if (cb.Read != null)
|
||||
{
|
||||
_readCallbacks[index] = cb.Read;
|
||||
}
|
||||
|
||||
if (cb.Write != null)
|
||||
{
|
||||
_writeCallbacks.Add(offset, cb.Write);
|
||||
if (cb.Write != null)
|
||||
{
|
||||
_writeCallbacks[index] = cb.Write;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (debugLogCallback != null)
|
||||
{
|
||||
_fieldNamesForDebug.Add(offset, field.Name);
|
||||
_fieldNamesForDebug.Add((uint)offset, field.Name);
|
||||
}
|
||||
|
||||
offset += sizeOfField;
|
||||
|
@ -79,48 +69,71 @@ namespace Ryujinx.Graphics.Device
|
|||
Debug.Assert(offset == Unsafe.SizeOf<TState>());
|
||||
}
|
||||
|
||||
public virtual int Read(int offset)
|
||||
public int Read(int offset)
|
||||
{
|
||||
if (Check(offset) && _readableRegisters[offset / RegisterSize])
|
||||
{
|
||||
int alignedOffset = Align(offset);
|
||||
uint index = (uint)offset / RegisterSize;
|
||||
|
||||
if (_readCallbacks.TryGetValue(alignedOffset, out Func<int> read))
|
||||
if (index < Size)
|
||||
{
|
||||
uint alignedOffset = index * RegisterSize;
|
||||
|
||||
var readCallback = Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_readCallbacks), (IntPtr)index);
|
||||
if (readCallback != null)
|
||||
{
|
||||
return read();
|
||||
return readCallback();
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetRef<int>(alignedOffset);
|
||||
return GetRefUnchecked<int>(alignedOffset);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public virtual void Write(int offset, int data)
|
||||
public void Write(int offset, int data)
|
||||
{
|
||||
if (Check(offset) && _writableRegisters[offset / RegisterSize])
|
||||
uint index = (uint)offset / RegisterSize;
|
||||
|
||||
if (index < Size)
|
||||
{
|
||||
int alignedOffset = Align(offset);
|
||||
uint alignedOffset = index * RegisterSize;
|
||||
DebugWrite(alignedOffset, data);
|
||||
|
||||
if (_fieldNamesForDebug != null && _fieldNamesForDebug.TryGetValue(alignedOffset, out string fieldName))
|
||||
{
|
||||
_debugLogCallback($"{typeof(TState).Name}.{fieldName} = 0x{data:X}");
|
||||
}
|
||||
GetRefIntAlignedUncheck(index) = data;
|
||||
|
||||
GetRef<int>(alignedOffset) = data;
|
||||
|
||||
if (_writeCallbacks.TryGetValue(alignedOffset, out Action<int> write))
|
||||
{
|
||||
write(data);
|
||||
}
|
||||
Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_writeCallbacks), (IntPtr)index)?.Invoke(data);
|
||||
}
|
||||
}
|
||||
|
||||
private bool Check(int offset)
|
||||
public void WriteWithRedundancyCheck(int offset, int data, out bool changed)
|
||||
{
|
||||
return (uint)Align(offset) < Unsafe.SizeOf<TState>();
|
||||
uint index = (uint)offset / RegisterSize;
|
||||
|
||||
if (index < Size)
|
||||
{
|
||||
uint alignedOffset = index * RegisterSize;
|
||||
DebugWrite(alignedOffset, data);
|
||||
|
||||
ref var storage = ref GetRefIntAlignedUncheck(index);
|
||||
changed = storage != data;
|
||||
storage = data;
|
||||
|
||||
Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_writeCallbacks), (IntPtr)index)?.Invoke(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
changed = false;
|
||||
}
|
||||
}
|
||||
|
||||
[Conditional("DEBUG")]
|
||||
private void DebugWrite(uint alignedOffset, int data)
|
||||
{
|
||||
if (_fieldNamesForDebug != null && _fieldNamesForDebug.TryGetValue(alignedOffset, out string fieldName))
|
||||
{
|
||||
_debugLogCallback($"{typeof(TState).Name}.{fieldName} = 0x{data:X}");
|
||||
}
|
||||
}
|
||||
|
||||
public ref T GetRef<T>(int offset) where T : unmanaged
|
||||
|
@ -130,12 +143,19 @@ namespace Ryujinx.Graphics.Device
|
|||
throw new ArgumentOutOfRangeException(nameof(offset));
|
||||
}
|
||||
|
||||
return ref GetRefUnchecked<T>((uint)offset);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private ref T GetRefUnchecked<T>(uint offset) where T : unmanaged
|
||||
{
|
||||
return ref Unsafe.As<TState, T>(ref Unsafe.AddByteOffset(ref State, (IntPtr)offset));
|
||||
}
|
||||
|
||||
private static int Align(int offset)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private ref int GetRefIntAlignedUncheck(ulong index)
|
||||
{
|
||||
return offset & ~(RegisterSize - 1);
|
||||
return ref Unsafe.Add(ref Unsafe.As<TState, int>(ref State), (IntPtr)index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue