DeviceState.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.Graphics.Device
  8. {
  9. public class DeviceState<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TState> : IDeviceState where TState : unmanaged
  10. {
  11. private const int RegisterSize = sizeof(int);
  12. public TState State;
  13. private uint Size => (uint)(Unsafe.SizeOf<TState>() + RegisterSize - 1) / RegisterSize;
  14. private readonly Func<int>[] _readCallbacks;
  15. private readonly Action<int>[] _writeCallbacks;
  16. private readonly Dictionary<uint, string> _fieldNamesForDebug;
  17. private readonly Action<string> _debugLogCallback;
  18. public DeviceState(IReadOnlyDictionary<string, RwCallback> callbacks = null, Action<string> debugLogCallback = null)
  19. {
  20. _readCallbacks = new Func<int>[Size];
  21. _writeCallbacks = new Action<int>[Size];
  22. if (debugLogCallback != null)
  23. {
  24. _fieldNamesForDebug = new Dictionary<uint, string>();
  25. _debugLogCallback = debugLogCallback;
  26. }
  27. var fields = typeof(TState).GetFields();
  28. int offset = 0;
  29. for (int fieldIndex = 0; fieldIndex < fields.Length; fieldIndex++)
  30. {
  31. var field = fields[fieldIndex];
  32. int sizeOfField = SizeCalculator.SizeOf(field.FieldType);
  33. for (int i = 0; i < ((sizeOfField + 3) & ~3); i += 4)
  34. {
  35. int index = (offset + i) / RegisterSize;
  36. if (callbacks != null && callbacks.TryGetValue(field.Name, out var cb))
  37. {
  38. if (cb.Read != null)
  39. {
  40. _readCallbacks[index] = cb.Read;
  41. }
  42. if (cb.Write != null)
  43. {
  44. _writeCallbacks[index] = cb.Write;
  45. }
  46. }
  47. }
  48. if (debugLogCallback != null)
  49. {
  50. _fieldNamesForDebug.Add((uint)offset, field.Name);
  51. }
  52. offset += sizeOfField;
  53. }
  54. Debug.Assert(offset == Unsafe.SizeOf<TState>());
  55. }
  56. public int Read(int offset)
  57. {
  58. uint index = (uint)offset / RegisterSize;
  59. if (index < Size)
  60. {
  61. uint alignedOffset = index * RegisterSize;
  62. var readCallback = Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_readCallbacks), (IntPtr)index);
  63. if (readCallback != null)
  64. {
  65. return readCallback();
  66. }
  67. else
  68. {
  69. return GetRefUnchecked<int>(alignedOffset);
  70. }
  71. }
  72. return 0;
  73. }
  74. public void Write(int offset, int data)
  75. {
  76. uint index = (uint)offset / RegisterSize;
  77. if (index < Size)
  78. {
  79. uint alignedOffset = index * RegisterSize;
  80. DebugWrite(alignedOffset, data);
  81. GetRefIntAlignedUncheck(index) = data;
  82. Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_writeCallbacks), (IntPtr)index)?.Invoke(data);
  83. }
  84. }
  85. public void WriteWithRedundancyCheck(int offset, int data, out bool changed)
  86. {
  87. uint index = (uint)offset / RegisterSize;
  88. if (index < Size)
  89. {
  90. uint alignedOffset = index * RegisterSize;
  91. DebugWrite(alignedOffset, data);
  92. ref var storage = ref GetRefIntAlignedUncheck(index);
  93. changed = storage != data;
  94. storage = data;
  95. Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_writeCallbacks), (IntPtr)index)?.Invoke(data);
  96. }
  97. else
  98. {
  99. changed = false;
  100. }
  101. }
  102. [Conditional("DEBUG")]
  103. private void DebugWrite(uint alignedOffset, int data)
  104. {
  105. if (_fieldNamesForDebug != null && _fieldNamesForDebug.TryGetValue(alignedOffset, out string fieldName))
  106. {
  107. _debugLogCallback($"{typeof(TState).Name}.{fieldName} = 0x{data:X}");
  108. }
  109. }
  110. public ref T GetRef<T>(int offset) where T : unmanaged
  111. {
  112. if ((uint)(offset + Unsafe.SizeOf<T>()) > Unsafe.SizeOf<TState>())
  113. {
  114. throw new ArgumentOutOfRangeException(nameof(offset));
  115. }
  116. return ref GetRefUnchecked<T>((uint)offset);
  117. }
  118. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  119. private ref T GetRefUnchecked<T>(uint offset) where T : unmanaged
  120. {
  121. return ref Unsafe.As<TState, T>(ref Unsafe.AddByteOffset(ref State, (IntPtr)offset));
  122. }
  123. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  124. private ref int GetRefIntAlignedUncheck(ulong index)
  125. {
  126. return ref Unsafe.Add(ref Unsafe.As<TState, int>(ref State), (IntPtr)index);
  127. }
  128. }
  129. }