From 40b21cc3c4d2622bbd4f88d43073341854d9a671 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sun, 11 Jul 2021 17:20:40 -0300 Subject: 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 --- Ryujinx.Graphics.Device/DeviceState.cs | 124 +++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 52 deletions(-) (limited to 'Ryujinx.Graphics.Device/DeviceState.cs') diff --git a/Ryujinx.Graphics.Device/DeviceState.cs b/Ryujinx.Graphics.Device/DeviceState.cs index 1001d295..18100571 100644 --- a/Ryujinx.Graphics.Device/DeviceState.cs +++ b/Ryujinx.Graphics.Device/DeviceState.cs @@ -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() + RegisterSize - 1) / RegisterSize; - private readonly Dictionary> _readCallbacks; - private readonly Dictionary> _writeCallbacks; + private readonly Func[] _readCallbacks; + private readonly Action[] _writeCallbacks; - private readonly Dictionary _fieldNamesForDebug; + private readonly Dictionary _fieldNamesForDebug; private readonly Action _debugLogCallback; public DeviceState(IReadOnlyDictionary callbacks = null, Action debugLogCallback = null) { - int size = (Unsafe.SizeOf() + RegisterSize - 1) / RegisterSize; - - _readableRegisters = new BitArray(size); - _writableRegisters = new BitArray(size); - - _readCallbacks = new Dictionary>(); - _writeCallbacks = new Dictionary>(); + _readCallbacks = new Func[Size]; + _writeCallbacks = new Action[Size]; if (debugLogCallback != null) { - _fieldNamesForDebug = new Dictionary(); + _fieldNamesForDebug = new Dictionary(); _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(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; - } - - if (callbacks != null && callbacks.TryGetValue(field.Name, out var cb)) - { - if (cb.Read != null) - { - _readCallbacks.Add(offset, cb.Read); - } + int index = (offset + i) / RegisterSize; - if (cb.Write != null) + if (callbacks != null && callbacks.TryGetValue(field.Name, out var cb)) { - _writeCallbacks.Add(offset, cb.Write); + if (cb.Read != null) + { + _readCallbacks[index] = cb.Read; + } + + 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()); } - public virtual int Read(int offset) + public int Read(int offset) { - if (Check(offset) && _readableRegisters[offset / RegisterSize]) + uint index = (uint)offset / RegisterSize; + + if (index < Size) { - int alignedOffset = Align(offset); + uint alignedOffset = index * RegisterSize; - if (_readCallbacks.TryGetValue(alignedOffset, out Func read)) + var readCallback = Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_readCallbacks), (IntPtr)index); + if (readCallback != null) { - return read(); + return readCallback(); } else { - return GetRef(alignedOffset); + return GetRefUnchecked(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(alignedOffset) = data; + Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_writeCallbacks), (IntPtr)index)?.Invoke(data); + } + } - if (_writeCallbacks.TryGetValue(alignedOffset, out Action write)) - { - write(data); - } + public void WriteWithRedundancyCheck(int offset, int data, out bool changed) + { + 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; } } - private bool Check(int offset) + [Conditional("DEBUG")] + private void DebugWrite(uint alignedOffset, int data) { - return (uint)Align(offset) < Unsafe.SizeOf(); + if (_fieldNamesForDebug != null && _fieldNamesForDebug.TryGetValue(alignedOffset, out string fieldName)) + { + _debugLogCallback($"{typeof(TState).Name}.{fieldName} = 0x{data:X}"); + } } public ref T GetRef(int offset) where T : unmanaged @@ -130,12 +143,19 @@ namespace Ryujinx.Graphics.Device throw new ArgumentOutOfRangeException(nameof(offset)); } + return ref GetRefUnchecked((uint)offset); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private ref T GetRefUnchecked(uint offset) where T : unmanaged + { return ref Unsafe.As(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(ref State), (IntPtr)index); } } } -- cgit v1.2.3