StateUpdateTracker.cs 5.5 KB

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