diff options
| author | gdkchan <gab.dark.100@gmail.com> | 2021-07-11 17:20:40 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-11 17:20:40 -0300 |
| commit | 40b21cc3c4d2622bbd4f88d43073341854d9a671 (patch) | |
| tree | 6e9dc6a42e7c0bae5b03db468481771d5a6937ef /Ryujinx.Graphics.Device | |
| parent | b5190f16810eb77388c861d1d1773e19644808db (diff) | |
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
Diffstat (limited to 'Ryujinx.Graphics.Device')
| -rw-r--r-- | Ryujinx.Graphics.Device/AccessControl.cs | 10 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Device/DeviceState.cs | 124 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Device/RegisterAttribute.cs | 15 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Device/SizeCalculator.cs | 2 |
4 files changed, 73 insertions, 78 deletions
diff --git a/Ryujinx.Graphics.Device/AccessControl.cs b/Ryujinx.Graphics.Device/AccessControl.cs deleted file mode 100644 index 02203783..00000000 --- a/Ryujinx.Graphics.Device/AccessControl.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Ryujinx.Graphics.Device -{ - public enum AccessControl - { - None = 0, - ReadOnly = 1 << 0, - WriteOnly = 1 << 1, - ReadWrite = ReadOnly | WriteOnly - } -} 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<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; - } - - 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<TState>()); } - 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<int> read)) + 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; + Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_writeCallbacks), (IntPtr)index)?.Invoke(data); + } + } - if (_writeCallbacks.TryGetValue(alignedOffset, out Action<int> 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<TState>(); + 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); } } } diff --git a/Ryujinx.Graphics.Device/RegisterAttribute.cs b/Ryujinx.Graphics.Device/RegisterAttribute.cs deleted file mode 100644 index 6e198963..00000000 --- a/Ryujinx.Graphics.Device/RegisterAttribute.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace Ryujinx.Graphics.Device -{ - [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] - public sealed class RegisterAttribute : Attribute - { - public AccessControl AccessControl { get; } - - public RegisterAttribute(AccessControl ac) - { - AccessControl = ac; - } - } -} diff --git a/Ryujinx.Graphics.Device/SizeCalculator.cs b/Ryujinx.Graphics.Device/SizeCalculator.cs index 7cc48915..7409c041 100644 --- a/Ryujinx.Graphics.Device/SizeCalculator.cs +++ b/Ryujinx.Graphics.Device/SizeCalculator.cs @@ -3,7 +3,7 @@ using System.Reflection; namespace Ryujinx.Graphics.Device { - static class SizeCalculator + public static class SizeCalculator { public static int SizeOf(Type type) { |
