From 458452279cee03bfe1bbf2c3daf3fc9722b03a74 Mon Sep 17 00:00:00 2001 From: riperiperi Date: Thu, 1 Dec 2022 15:30:13 +0000 Subject: GPU: Track buffer migrations and flush source on incomplete copy (#3952) * Track buffer migrations and flush source on incomplete copy Makes sure that the modified range list is always from the latest iteration of the buffer, and flushes earlier iterations of a buffer if the data has not been migrated yet. * Cleanup 1 * Reduce cost for redundant signal checks on Vulkan * Only inherit the range list if there are pending ranges. * Fix OpenGL * Address Feedback * Whoops --- Ryujinx.Graphics.Gpu/Memory/BufferMigration.cs | 125 +++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 Ryujinx.Graphics.Gpu/Memory/BufferMigration.cs (limited to 'Ryujinx.Graphics.Gpu/Memory/BufferMigration.cs') diff --git a/Ryujinx.Graphics.Gpu/Memory/BufferMigration.cs b/Ryujinx.Graphics.Gpu/Memory/BufferMigration.cs new file mode 100644 index 00000000..e020c0c3 --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Memory/BufferMigration.cs @@ -0,0 +1,125 @@ +using System; + +namespace Ryujinx.Graphics.Gpu.Memory +{ + /// + /// A record of when buffer data was copied from one buffer to another, along with the SyncNumber when the migration will be complete. + /// Keeps the source buffer alive for data flushes until the migration is complete. + /// + internal class BufferMigration : IDisposable + { + /// + /// The offset for the migrated region. + /// + private readonly ulong _offset; + + /// + /// The size for the migrated region. + /// + private readonly ulong _size; + + /// + /// The buffer that was migrated from. + /// + private readonly Buffer _buffer; + + /// + /// The source range action, to be called on overlap with an unreached sync number. + /// + private readonly Action _sourceRangeAction; + + /// + /// The source range list. + /// + private readonly BufferModifiedRangeList _source; + + /// + /// The destination range list. This range list must be updated when flushing the source. + /// + public readonly BufferModifiedRangeList Destination; + + /// + /// The sync number that needs to be reached for this migration to be removed. This is set to the pending sync number on creation. + /// + public readonly ulong SyncNumber; + + /// + /// Creates a record for a buffer migration. + /// + /// The source buffer for this migration + /// The flush action for the source buffer + /// The modified range list for the source buffer + /// The modified range list for the destination buffer + /// The sync number for when the migration is complete + public BufferMigration( + Buffer buffer, + Action sourceRangeAction, + BufferModifiedRangeList source, + BufferModifiedRangeList dest, + ulong syncNumber) + { + _offset = buffer.Address; + _size = buffer.Size; + _buffer = buffer; + _sourceRangeAction = sourceRangeAction; + _source = source; + Destination = dest; + SyncNumber = syncNumber; + } + + /// + /// Determine if the given range overlaps this migration, and has not been completed yet. + /// + /// Start offset + /// Range size + /// The sync number that was waited on + /// True if overlapping and in progress, false otherwise + public bool Overlaps(ulong offset, ulong size, ulong syncNumber) + { + ulong end = offset + size; + ulong destEnd = _offset + _size; + long syncDiff = (long)(syncNumber - SyncNumber); // syncNumber is less if the copy has not completed. + + return !(end <= _offset || offset >= destEnd) && syncDiff < 0; + } + + /// + /// Determine if the given range matches this migration. + /// + /// Start offset + /// Range size + /// True if the range exactly matches, false otherwise + public bool FullyMatches(ulong offset, ulong size) + { + return _offset == offset && _size == size; + } + + /// + /// Perform the migration source's range action on the range provided, clamped to the bounds of the source buffer. + /// + /// Start offset + /// Range size + /// Current sync number + /// The modified range list that originally owned this range + public void RangeActionWithMigration(ulong offset, ulong size, ulong syncNumber, BufferModifiedRangeList parent) + { + ulong end = offset + size; + end = Math.Min(_offset + _size, end); + offset = Math.Max(_offset, offset); + + size = end - offset; + + _source.RangeActionWithMigration(offset, size, syncNumber, parent, _sourceRangeAction); + } + + /// + /// Removes this reference to the range list, potentially allowing for the source buffer to be disposed. + /// + public void Dispose() + { + Destination.RemoveMigration(this); + + _buffer.DecrementReferenceCount(); + } + } +} -- cgit v1.2.3