StateUpdateTracker.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Ryujinx.Graphics.Device;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Numerics;
  7. using System.Runtime.CompilerServices;
  8. using System.Runtime.InteropServices;
  9. namespace Ryujinx.Graphics.Gpu.Engine.Threed
  10. {
  11. /// <summary>
  12. /// State update callback entry, with the callback function and associated field names.
  13. /// </summary>
  14. readonly struct StateUpdateCallbackEntry
  15. {
  16. /// <summary>
  17. /// Callback function, to be called if the register was written as the state needs to be updated.
  18. /// </summary>
  19. public Action Callback { get; }
  20. /// <summary>
  21. /// Name of the state fields (registers) associated with the callback function.
  22. /// </summary>
  23. public string[] FieldNames { get; }
  24. /// <summary>
  25. /// Creates a new state update callback entry.
  26. /// </summary>
  27. /// <param name="callback">Callback function, to be called if the register was written as the state needs to be updated</param>
  28. /// <param name="fieldNames">Name of the state fields (registers) associated with the callback function</param>
  29. public StateUpdateCallbackEntry(Action callback, params string[] fieldNames)
  30. {
  31. Callback = callback;
  32. FieldNames = fieldNames;
  33. }
  34. }
  35. /// <summary>
  36. /// GPU state update tracker.
  37. /// </summary>
  38. /// <typeparam name="TState">State type</typeparam>
  39. class StateUpdateTracker<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TState>
  40. {
  41. private const int BlockSize = 0xe00;
  42. private const int RegisterSize = sizeof(uint);
  43. private readonly byte[] _registerToGroupMapping;
  44. private readonly Action[] _callbacks;
  45. private ulong _dirtyMask;
  46. /// <summary>
  47. /// Creates a new instance of the state update tracker.
  48. /// </summary>
  49. /// <param name="entries">Update tracker callback entries</param>
  50. public StateUpdateTracker(StateUpdateCallbackEntry[] entries)
  51. {
  52. _registerToGroupMapping = new byte[BlockSize];
  53. _callbacks = new Action[entries.Length];
  54. var fieldToDelegate = new Dictionary<string, int>();
  55. for (int entryIndex = 0; entryIndex < entries.Length; entryIndex++)
  56. {
  57. var entry = entries[entryIndex];
  58. foreach (var fieldName in entry.FieldNames)
  59. {
  60. fieldToDelegate.Add(fieldName, entryIndex);
  61. }
  62. _callbacks[entryIndex] = entry.Callback;
  63. }
  64. var fields = typeof(TState).GetFields();
  65. int offset = 0;
  66. for (int fieldIndex = 0; fieldIndex < fields.Length; fieldIndex++)
  67. {
  68. var field = fields[fieldIndex];
  69. int sizeOfField = SizeCalculator.SizeOf(field.FieldType);
  70. if (fieldToDelegate.TryGetValue(field.Name, out int entryIndex))
  71. {
  72. for (int i = 0; i < ((sizeOfField + 3) & ~3); i += 4)
  73. {
  74. _registerToGroupMapping[(offset + i) / RegisterSize] = (byte)(entryIndex + 1);
  75. }
  76. }
  77. offset += sizeOfField;
  78. }
  79. Debug.Assert(offset == Unsafe.SizeOf<TState>());
  80. }
  81. /// <summary>
  82. /// Sets a register as modified.
  83. /// </summary>
  84. /// <param name="offset">Register offset in bytes</param>
  85. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  86. public void SetDirty(int offset)
  87. {
  88. uint index = (uint)offset / RegisterSize;
  89. if (index < BlockSize)
  90. {
  91. int groupIndex = Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_registerToGroupMapping), (IntPtr)index);
  92. if (groupIndex != 0)
  93. {
  94. groupIndex--;
  95. _dirtyMask |= 1UL << groupIndex;
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// Forces a register group as dirty, by index.
  101. /// </summary>
  102. /// <param name="groupIndex">Index of the group to be dirtied</param>
  103. public void ForceDirty(int groupIndex)
  104. {
  105. if ((uint)groupIndex >= _callbacks.Length)
  106. {
  107. throw new ArgumentOutOfRangeException(nameof(groupIndex));
  108. }
  109. _dirtyMask |= 1UL << groupIndex;
  110. }
  111. /// <summary>
  112. /// Forces all register groups as dirty, triggering a full update on the next call to <see cref="Update"/>.
  113. /// </summary>
  114. public void SetAllDirty()
  115. {
  116. Debug.Assert(_callbacks.Length <= sizeof(ulong) * 8);
  117. _dirtyMask = ulong.MaxValue >> ((sizeof(ulong) * 8) - _callbacks.Length);
  118. }
  119. /// <summary>
  120. /// Check if the given register group is dirty without clearing it.
  121. /// </summary>
  122. /// <param name="groupIndex">Index of the group to check</param>
  123. /// <returns>True if dirty, false otherwise</returns>
  124. public bool IsDirty(int groupIndex)
  125. {
  126. return (_dirtyMask & (1UL << groupIndex)) != 0;
  127. }
  128. /// <summary>
  129. /// Check all the groups specified by <paramref name="checkMask"/> for modification, and update if modified.
  130. /// </summary>
  131. /// <param name="checkMask">Mask, where each bit set corresponds to a group index that should be checked</param>
  132. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  133. public void Update(ulong checkMask)
  134. {
  135. ulong mask = _dirtyMask & checkMask;
  136. if (mask == 0)
  137. {
  138. return;
  139. }
  140. do
  141. {
  142. int groupIndex = BitOperations.TrailingZeroCount(mask);
  143. _callbacks[groupIndex]();
  144. mask &= ~(1UL << groupIndex);
  145. }
  146. while (mask != 0);
  147. _dirtyMask &= ~checkMask;
  148. }
  149. }
  150. }