mirror of
https://git.ryujinx.app/ryubing/ryujinx.git
synced 2025-06-28 06:46:24 +02:00
GPU: Migrate buffers on GPU project, pre-emptively flush device local mappings (#6794)
* GPU: Migrate buffers on GPU project, pre-emptively flush device local mappings Essentially retreading #4540, but it's on the GPU project now instead of the backend. This allows us to have a lot more control + knowledge of where the buffer backing has been changed and allows us to pre-emptively flush pages to host memory for quicker readback. It will allow us to do other stuff in the future, but we'll get there when we get there. Performance greatly improved in Hyrule Warriors: Age of Calamity. Performance notably improved in TOTK (average). Performance for BOTW restored to how it was before #4911, perhaps a bit better. - Rewrites a bunch of buffer migration stuff. Might want to tighten up how dispose stuff works. - Fixed an issue where the copy for texture pre-flush would happen _after_ the syncpoint. TODO: remove a page from pre-flush if it isn't flushed after a certain number of copies. * Add copy deactivation * Fix dependent virtual buffers * Remove logging * Fix format issues (maybe) * Vulkan: Remove backing swap * Add explicit memory access types for most buffers * Fix typo * Add device local force expiry, change buffer inheritance behaviour * General cleanup, OGL fix * BufferPreFlush comments * BufferBackingState comments * Add an extra precaution to BufferMigration This is very unlikely, but it's important to cover loose ends like this. * Address some feedback * Docs
This commit is contained in:
parent
2f427deb67
commit
eb1ce41b00
29 changed files with 1342 additions and 523 deletions
|
@ -10,6 +10,8 @@ using System.Threading;
|
|||
|
||||
namespace Ryujinx.Graphics.Gpu.Memory
|
||||
{
|
||||
delegate void BufferFlushAction(ulong address, ulong size, ulong syncNumber);
|
||||
|
||||
/// <summary>
|
||||
/// Buffer, used to store vertex and index data, uniform and storage buffers, and others.
|
||||
/// </summary>
|
||||
|
@ -23,7 +25,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// <summary>
|
||||
/// Host buffer handle.
|
||||
/// </summary>
|
||||
public BufferHandle Handle { get; }
|
||||
public BufferHandle Handle { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Start address of the buffer in guest memory.
|
||||
|
@ -60,6 +62,17 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// </remarks>
|
||||
private BufferModifiedRangeList _modifiedRanges = null;
|
||||
|
||||
/// <summary>
|
||||
/// A structure that is used to flush buffer data back to a host mapped buffer for cached readback.
|
||||
/// Only used if the buffer data is explicitly owned by device local memory.
|
||||
/// </summary>
|
||||
private BufferPreFlush _preFlush = null;
|
||||
|
||||
/// <summary>
|
||||
/// Usage tracking state that determines what type of backing the buffer should use.
|
||||
/// </summary>
|
||||
public BufferBackingState BackingState;
|
||||
|
||||
private readonly MultiRegionHandle _memoryTrackingGranular;
|
||||
private readonly RegionHandle _memoryTracking;
|
||||
|
||||
|
@ -87,6 +100,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// <param name="physicalMemory">Physical memory where the buffer is mapped</param>
|
||||
/// <param name="address">Start address of the buffer</param>
|
||||
/// <param name="size">Size of the buffer in bytes</param>
|
||||
/// <param name="stage">The type of usage that created the buffer</param>
|
||||
/// <param name="sparseCompatible">Indicates if the buffer can be used in a sparse buffer mapping</param>
|
||||
/// <param name="baseBuffers">Buffers which this buffer contains, and will inherit tracking handles from</param>
|
||||
public Buffer(
|
||||
|
@ -94,6 +108,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
PhysicalMemory physicalMemory,
|
||||
ulong address,
|
||||
ulong size,
|
||||
BufferStage stage,
|
||||
bool sparseCompatible,
|
||||
IEnumerable<Buffer> baseBuffers = null)
|
||||
{
|
||||
|
@ -103,9 +118,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
Size = size;
|
||||
SparseCompatible = sparseCompatible;
|
||||
|
||||
BufferAccess access = sparseCompatible ? BufferAccess.SparseCompatible : BufferAccess.Default;
|
||||
BackingState = new BufferBackingState(_context, this, stage, baseBuffers);
|
||||
|
||||
Handle = context.Renderer.CreateBuffer((int)size, access, baseBuffers?.MaxBy(x => x.Size).Handle ?? BufferHandle.Null);
|
||||
BufferAccess access = BackingState.SwitchAccess(this);
|
||||
|
||||
Handle = context.Renderer.CreateBuffer((int)size, access);
|
||||
|
||||
_useGranular = size > GranularBufferThreshold;
|
||||
|
||||
|
@ -161,6 +178,29 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
_virtualDependenciesLock = new ReaderWriterLockSlim();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recreates the backing buffer based on the desired access type
|
||||
/// reported by the backing state struct.
|
||||
/// </summary>
|
||||
private void ChangeBacking()
|
||||
{
|
||||
BufferAccess access = BackingState.SwitchAccess(this);
|
||||
|
||||
BufferHandle newHandle = _context.Renderer.CreateBuffer((int)Size, access);
|
||||
|
||||
_context.Renderer.Pipeline.CopyBuffer(Handle, newHandle, 0, 0, (int)Size);
|
||||
|
||||
_modifiedRanges?.SelfMigration();
|
||||
|
||||
// If swtiching from device local to host mapped, pre-flushing data no longer makes sense.
|
||||
// This is set to null and disposed when the migration fully completes.
|
||||
_preFlush = null;
|
||||
|
||||
Handle = newHandle;
|
||||
|
||||
_physicalMemory.BufferCache.BufferBackingChanged(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a sub-range from the buffer, from a start address til a page boundary after the given size.
|
||||
/// </summary>
|
||||
|
@ -246,6 +286,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
}
|
||||
else
|
||||
{
|
||||
BackingState.RecordSet();
|
||||
_context.Renderer.SetBufferData(Handle, 0, _physicalMemory.GetSpan(Address, (int)Size));
|
||||
CopyToDependantVirtualBuffers();
|
||||
}
|
||||
|
@ -283,15 +324,35 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
_modifiedRanges ??= new BufferModifiedRangeList(_context, this, Flush);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a backing change is deemed necessary from the given usage.
|
||||
/// If it is, queues a backing change to happen on the next sync action.
|
||||
/// </summary>
|
||||
/// <param name="stage">Buffer stage that can change backing type</param>
|
||||
private void TryQueueBackingChange(BufferStage stage)
|
||||
{
|
||||
if (BackingState.ShouldChangeBacking(stage))
|
||||
{
|
||||
if (!_syncActionRegistered)
|
||||
{
|
||||
_context.RegisterSyncAction(this);
|
||||
_syncActionRegistered = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Signal that the given region of the buffer has been modified.
|
||||
/// </summary>
|
||||
/// <param name="address">The start address of the modified region</param>
|
||||
/// <param name="size">The size of the modified region</param>
|
||||
public void SignalModified(ulong address, ulong size)
|
||||
/// <param name="stage">Buffer stage that triggered the modification</param>
|
||||
public void SignalModified(ulong address, ulong size, BufferStage stage)
|
||||
{
|
||||
EnsureRangeList();
|
||||
|
||||
TryQueueBackingChange(stage);
|
||||
|
||||
_modifiedRanges.SignalModified(address, size);
|
||||
|
||||
if (!_syncActionRegistered)
|
||||
|
@ -311,6 +372,37 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
_modifiedRanges?.Clear(address, size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Action to be performed immediately before sync is created.
|
||||
/// This will copy any buffer ranges designated for pre-flushing.
|
||||
/// </summary>
|
||||
/// <param name="syncpoint">True if the action is a guest syncpoint</param>
|
||||
public void SyncPreAction(bool syncpoint)
|
||||
{
|
||||
if (_referenceCount == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (BackingState.ShouldChangeBacking())
|
||||
{
|
||||
ChangeBacking();
|
||||
}
|
||||
|
||||
if (BackingState.IsDeviceLocal)
|
||||
{
|
||||
_preFlush ??= new BufferPreFlush(_context, this, FlushImpl);
|
||||
|
||||
if (_preFlush.ShouldCopy)
|
||||
{
|
||||
_modifiedRanges?.GetRangesAtSync(Address, Size, _context.SyncNumber, (address, size) =>
|
||||
{
|
||||
_preFlush.CopyModified(address, size);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Action to be performed when a syncpoint is reached after modification.
|
||||
/// This will register read/write tracking to flush the buffer from GPU when its memory is used.
|
||||
|
@ -466,6 +558,8 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// <param name="mSize">Size of the modified region</param>
|
||||
private void LoadRegion(ulong mAddress, ulong mSize)
|
||||
{
|
||||
BackingState.RecordSet();
|
||||
|
||||
int offset = (int)(mAddress - Address);
|
||||
|
||||
_context.Renderer.SetBufferData(Handle, offset, _physicalMemory.GetSpan(mAddress, (int)mSize));
|
||||
|
@ -539,18 +633,84 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// Flushes a range of the buffer.
|
||||
/// This writes the range data back into guest memory.
|
||||
/// </summary>
|
||||
/// <param name="handle">Buffer handle to flush data from</param>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
public void Flush(ulong address, ulong size)
|
||||
private void FlushImpl(BufferHandle handle, ulong address, ulong size)
|
||||
{
|
||||
int offset = (int)(address - Address);
|
||||
|
||||
using PinnedSpan<byte> data = _context.Renderer.GetBufferData(Handle, offset, (int)size);
|
||||
using PinnedSpan<byte> data = _context.Renderer.GetBufferData(handle, offset, (int)size);
|
||||
|
||||
// TODO: When write tracking shaders, they will need to be aware of changes in overlapping buffers.
|
||||
_physicalMemory.WriteUntracked(address, CopyFromDependantVirtualBuffers(data.Get(), address, size));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flushes a range of the buffer.
|
||||
/// This writes the range data back into guest memory.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
private void FlushImpl(ulong address, ulong size)
|
||||
{
|
||||
FlushImpl(Handle, address, size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flushes a range of the buffer from the most optimal source.
|
||||
/// This writes the range data back into guest memory.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
/// <param name="syncNumber">Sync number waited for before flushing the data</param>
|
||||
public void Flush(ulong address, ulong size, ulong syncNumber)
|
||||
{
|
||||
BackingState.RecordFlush();
|
||||
|
||||
BufferPreFlush preFlush = _preFlush;
|
||||
|
||||
if (preFlush != null)
|
||||
{
|
||||
preFlush.FlushWithAction(address, size, syncNumber);
|
||||
}
|
||||
else
|
||||
{
|
||||
FlushImpl(address, size);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets an action that disposes the backing buffer using its current handle.
|
||||
/// Useful for deleting an old copy of the buffer after the handle changes.
|
||||
/// </summary>
|
||||
/// <returns>An action that flushes data from the specified range, using the buffer handle at the time the method is generated</returns>
|
||||
public Action GetSnapshotDisposeAction()
|
||||
{
|
||||
BufferHandle handle = Handle;
|
||||
BufferPreFlush preFlush = _preFlush;
|
||||
|
||||
return () =>
|
||||
{
|
||||
_context.Renderer.DeleteBuffer(handle);
|
||||
preFlush?.Dispose();
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an action that flushes a range of the buffer using its current handle.
|
||||
/// Useful for flushing data from old copies of the buffer after the handle changes.
|
||||
/// </summary>
|
||||
/// <returns>An action that flushes data from the specified range, using the buffer handle at the time the method is generated</returns>
|
||||
public BufferFlushAction GetSnapshotFlushAction()
|
||||
{
|
||||
BufferHandle handle = Handle;
|
||||
|
||||
return (ulong address, ulong size, ulong _) =>
|
||||
{
|
||||
FlushImpl(handle, address, size);
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Align a given address and size region to page boundaries.
|
||||
/// </summary>
|
||||
|
@ -857,6 +1017,8 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
_modifiedRanges?.Clear();
|
||||
|
||||
_context.Renderer.DeleteBuffer(Handle);
|
||||
_preFlush?.Dispose();
|
||||
_preFlush = null;
|
||||
|
||||
UnmappedSequence++;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue