BufferMigration.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. namespace Ryujinx.Graphics.Gpu.Memory
  3. {
  4. /// <summary>
  5. /// A record of when buffer data was copied from one buffer to another, along with the SyncNumber when the migration will be complete.
  6. /// Keeps the source buffer alive for data flushes until the migration is complete.
  7. /// </summary>
  8. internal class BufferMigration : IDisposable
  9. {
  10. /// <summary>
  11. /// The offset for the migrated region.
  12. /// </summary>
  13. private readonly ulong _offset;
  14. /// <summary>
  15. /// The size for the migrated region.
  16. /// </summary>
  17. private readonly ulong _size;
  18. /// <summary>
  19. /// The buffer that was migrated from.
  20. /// </summary>
  21. private readonly Buffer _buffer;
  22. /// <summary>
  23. /// The source range action, to be called on overlap with an unreached sync number.
  24. /// </summary>
  25. private readonly Action<ulong, ulong> _sourceRangeAction;
  26. /// <summary>
  27. /// The source range list.
  28. /// </summary>
  29. private readonly BufferModifiedRangeList _source;
  30. /// <summary>
  31. /// The destination range list. This range list must be updated when flushing the source.
  32. /// </summary>
  33. public readonly BufferModifiedRangeList Destination;
  34. /// <summary>
  35. /// The sync number that needs to be reached for this migration to be removed. This is set to the pending sync number on creation.
  36. /// </summary>
  37. public readonly ulong SyncNumber;
  38. /// <summary>
  39. /// Creates a record for a buffer migration.
  40. /// </summary>
  41. /// <param name="buffer">The source buffer for this migration</param>
  42. /// <param name="sourceRangeAction">The flush action for the source buffer</param>
  43. /// <param name="source">The modified range list for the source buffer</param>
  44. /// <param name="dest">The modified range list for the destination buffer</param>
  45. /// <param name="syncNumber">The sync number for when the migration is complete</param>
  46. public BufferMigration(
  47. Buffer buffer,
  48. Action<ulong, ulong> sourceRangeAction,
  49. BufferModifiedRangeList source,
  50. BufferModifiedRangeList dest,
  51. ulong syncNumber)
  52. {
  53. _offset = buffer.Address;
  54. _size = buffer.Size;
  55. _buffer = buffer;
  56. _sourceRangeAction = sourceRangeAction;
  57. _source = source;
  58. Destination = dest;
  59. SyncNumber = syncNumber;
  60. }
  61. /// <summary>
  62. /// Determine if the given range overlaps this migration, and has not been completed yet.
  63. /// </summary>
  64. /// <param name="offset">Start offset</param>
  65. /// <param name="size">Range size</param>
  66. /// <param name="syncNumber">The sync number that was waited on</param>
  67. /// <returns>True if overlapping and in progress, false otherwise</returns>
  68. public bool Overlaps(ulong offset, ulong size, ulong syncNumber)
  69. {
  70. ulong end = offset + size;
  71. ulong destEnd = _offset + _size;
  72. long syncDiff = (long)(syncNumber - SyncNumber); // syncNumber is less if the copy has not completed.
  73. return !(end <= _offset || offset >= destEnd) && syncDiff < 0;
  74. }
  75. /// <summary>
  76. /// Determine if the given range matches this migration.
  77. /// </summary>
  78. /// <param name="offset">Start offset</param>
  79. /// <param name="size">Range size</param>
  80. /// <returns>True if the range exactly matches, false otherwise</returns>
  81. public bool FullyMatches(ulong offset, ulong size)
  82. {
  83. return _offset == offset && _size == size;
  84. }
  85. /// <summary>
  86. /// Perform the migration source's range action on the range provided, clamped to the bounds of the source buffer.
  87. /// </summary>
  88. /// <param name="offset">Start offset</param>
  89. /// <param name="size">Range size</param>
  90. /// <param name="syncNumber">Current sync number</param>
  91. /// <param name="parent">The modified range list that originally owned this range</param>
  92. public void RangeActionWithMigration(ulong offset, ulong size, ulong syncNumber, BufferModifiedRangeList parent)
  93. {
  94. ulong end = offset + size;
  95. end = Math.Min(_offset + _size, end);
  96. offset = Math.Max(_offset, offset);
  97. size = end - offset;
  98. _source.RangeActionWithMigration(offset, size, syncNumber, parent, _sourceRangeAction);
  99. }
  100. /// <summary>
  101. /// Removes this reference to the range list, potentially allowing for the source buffer to be disposed.
  102. /// </summary>
  103. public void Dispose()
  104. {
  105. Destination.RemoveMigration(this);
  106. _buffer.DecrementReferenceCount();
  107. }
  108. }
  109. }