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
This commit is contained in:
parent
817b89767a
commit
458452279c
11 changed files with 451 additions and 87 deletions
|
@ -29,6 +29,7 @@ namespace Ryujinx.Graphics.GAL
|
||||||
ReadOnlySpan<byte> GetBufferData(BufferHandle buffer, int offset, int size);
|
ReadOnlySpan<byte> GetBufferData(BufferHandle buffer, int offset, int size);
|
||||||
|
|
||||||
Capabilities GetCapabilities();
|
Capabilities GetCapabilities();
|
||||||
|
ulong GetCurrentSync();
|
||||||
HardwareInfo GetHardwareInfo();
|
HardwareInfo GetHardwareInfo();
|
||||||
|
|
||||||
IProgram LoadProgramBinary(byte[] programBinary, bool hasFragmentShader, ShaderInfo info);
|
IProgram LoadProgramBinary(byte[] programBinary, bool hasFragmentShader, ShaderInfo info);
|
||||||
|
|
|
@ -338,6 +338,11 @@ namespace Ryujinx.Graphics.GAL.Multithreading
|
||||||
return box.Result;
|
return box.Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ulong GetCurrentSync()
|
||||||
|
{
|
||||||
|
return _baseRenderer.GetCurrentSync();
|
||||||
|
}
|
||||||
|
|
||||||
public HardwareInfo GetHardwareInfo()
|
public HardwareInfo GetHardwareInfo()
|
||||||
{
|
{
|
||||||
return _baseRenderer.GetHardwareInfo();
|
return _baseRenderer.GetHardwareInfo();
|
||||||
|
|
|
@ -69,6 +69,12 @@ namespace Ryujinx.Graphics.Gpu
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal List<Action> SyncpointActions { get; }
|
internal List<Action> SyncpointActions { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Buffer migrations that are currently in-flight. These are checked whenever sync is created to determine if buffer migration
|
||||||
|
/// copies have completed on the GPU, and their data can be freed.
|
||||||
|
/// </summary>
|
||||||
|
internal List<BufferMigration> BufferMigrations { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Queue with deferred actions that must run on the render thread.
|
/// Queue with deferred actions that must run on the render thread.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -90,6 +96,7 @@ namespace Ryujinx.Graphics.Gpu
|
||||||
public event Action<ShaderCacheState, int, int> ShaderCacheStateChanged;
|
public event Action<ShaderCacheState, int, int> ShaderCacheStateChanged;
|
||||||
|
|
||||||
private Thread _gpuThread;
|
private Thread _gpuThread;
|
||||||
|
private bool _pendingSync;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the GPU emulation context.
|
/// Creates a new instance of the GPU emulation context.
|
||||||
|
@ -109,6 +116,7 @@ namespace Ryujinx.Graphics.Gpu
|
||||||
|
|
||||||
SyncActions = new List<Action>();
|
SyncActions = new List<Action>();
|
||||||
SyncpointActions = new List<Action>();
|
SyncpointActions = new List<Action>();
|
||||||
|
BufferMigrations = new List<BufferMigration>();
|
||||||
|
|
||||||
DeferredActions = new Queue<Action>();
|
DeferredActions = new Queue<Action>();
|
||||||
|
|
||||||
|
@ -273,6 +281,17 @@ namespace Ryujinx.Graphics.Gpu
|
||||||
SequenceNumber++;
|
SequenceNumber++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registers a buffer migration. These are checked to see if they can be disposed when the sync number increases,
|
||||||
|
/// and the migration copy has completed.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="migration">The buffer migration</param>
|
||||||
|
internal void RegisterBufferMigration(BufferMigration migration)
|
||||||
|
{
|
||||||
|
BufferMigrations.Add(migration);
|
||||||
|
_pendingSync = true;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers an action to be performed the next time a syncpoint is incremented.
|
/// Registers an action to be performed the next time a syncpoint is incremented.
|
||||||
/// This will also ensure a host sync object is created, and <see cref="SyncNumber"/> is incremented.
|
/// This will also ensure a host sync object is created, and <see cref="SyncNumber"/> is incremented.
|
||||||
|
@ -288,6 +307,7 @@ namespace Ryujinx.Graphics.Gpu
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SyncActions.Add(action);
|
SyncActions.Add(action);
|
||||||
|
_pendingSync = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -298,7 +318,24 @@ namespace Ryujinx.Graphics.Gpu
|
||||||
/// <param name="syncpoint">True if host sync is being created by a syncpoint</param>
|
/// <param name="syncpoint">True if host sync is being created by a syncpoint</param>
|
||||||
public void CreateHostSyncIfNeeded(bool syncpoint)
|
public void CreateHostSyncIfNeeded(bool syncpoint)
|
||||||
{
|
{
|
||||||
if (SyncActions.Count > 0 || (syncpoint && SyncpointActions.Count > 0))
|
if (BufferMigrations.Count > 0)
|
||||||
|
{
|
||||||
|
ulong currentSyncNumber = Renderer.GetCurrentSync();
|
||||||
|
|
||||||
|
for (int i = 0; i < BufferMigrations.Count; i++)
|
||||||
|
{
|
||||||
|
BufferMigration migration = BufferMigrations[i];
|
||||||
|
long diff = (long)(currentSyncNumber - migration.SyncNumber);
|
||||||
|
|
||||||
|
if (diff >= 0)
|
||||||
|
{
|
||||||
|
migration.Dispose();
|
||||||
|
BufferMigrations.RemoveAt(i--);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_pendingSync || (syncpoint && SyncpointActions.Count > 0))
|
||||||
{
|
{
|
||||||
Renderer.CreateSync(SyncNumber);
|
Renderer.CreateSync(SyncNumber);
|
||||||
|
|
||||||
|
@ -317,6 +354,8 @@ namespace Ryujinx.Graphics.Gpu
|
||||||
SyncActions.Clear();
|
SyncActions.Clear();
|
||||||
SyncpointActions.Clear();
|
SyncpointActions.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_pendingSync = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -65,6 +65,8 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
private bool _useGranular;
|
private bool _useGranular;
|
||||||
private bool _syncActionRegistered;
|
private bool _syncActionRegistered;
|
||||||
|
|
||||||
|
private int _referenceCount = 1;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the buffer.
|
/// Creates a new instance of the buffer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -229,7 +231,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
{
|
{
|
||||||
if (_modifiedRanges == null)
|
if (_modifiedRanges == null)
|
||||||
{
|
{
|
||||||
_modifiedRanges = new BufferModifiedRangeList(_context);
|
_modifiedRanges = new BufferModifiedRangeList(_context, this, Flush);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,7 +292,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
/// <param name="from">The buffer to inherit from</param>
|
/// <param name="from">The buffer to inherit from</param>
|
||||||
public void InheritModifiedRanges(Buffer from)
|
public void InheritModifiedRanges(Buffer from)
|
||||||
{
|
{
|
||||||
if (from._modifiedRanges != null)
|
if (from._modifiedRanges != null && from._modifiedRanges.HasRanges)
|
||||||
{
|
{
|
||||||
if (from._syncActionRegistered && !_syncActionRegistered)
|
if (from._syncActionRegistered && !_syncActionRegistered)
|
||||||
{
|
{
|
||||||
|
@ -310,19 +312,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (_modifiedRanges == null)
|
EnsureRangeList();
|
||||||
{
|
|
||||||
_modifiedRanges = from._modifiedRanges;
|
|
||||||
_modifiedRanges.ReregisterRanges(registerRangeAction);
|
|
||||||
|
|
||||||
from._modifiedRanges = null;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_modifiedRanges.InheritRanges(from._modifiedRanges, registerRangeAction);
|
_modifiedRanges.InheritRanges(from._modifiedRanges, registerRangeAction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determine if a given region of the buffer has been modified, and must be flushed.
|
/// Determine if a given region of the buffer has been modified, and must be flushed.
|
||||||
|
@ -456,7 +450,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
if (ranges != null)
|
if (ranges != null)
|
||||||
{
|
{
|
||||||
(address, size) = PageAlign(address, size);
|
(address, size) = PageAlign(address, size);
|
||||||
ranges.WaitForAndGetRanges(address, size, Flush);
|
ranges.WaitForAndFlushRanges(address, size);
|
||||||
}
|
}
|
||||||
}, true);
|
}, true);
|
||||||
}
|
}
|
||||||
|
@ -508,6 +502,25 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
UnmappedSequence++;
|
UnmappedSequence++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Increments the buffer reference count.
|
||||||
|
/// </summary>
|
||||||
|
public void IncrementReferenceCount()
|
||||||
|
{
|
||||||
|
_referenceCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decrements the buffer reference count.
|
||||||
|
/// </summary>
|
||||||
|
public void DecrementReferenceCount()
|
||||||
|
{
|
||||||
|
if (--_referenceCount == 0)
|
||||||
|
{
|
||||||
|
DisposeData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Disposes the host buffer's data, not its tracking handles.
|
/// Disposes the host buffer's data, not its tracking handles.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -528,7 +541,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
_memoryTrackingGranular?.Dispose();
|
_memoryTrackingGranular?.Dispose();
|
||||||
_memoryTracking?.Dispose();
|
_memoryTracking?.Dispose();
|
||||||
|
|
||||||
DisposeData();
|
DecrementReferenceCount();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -273,7 +273,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
buffer.CopyTo(newBuffer, dstOffset);
|
buffer.CopyTo(newBuffer, dstOffset);
|
||||||
newBuffer.InheritModifiedRanges(buffer);
|
newBuffer.InheritModifiedRanges(buffer);
|
||||||
|
|
||||||
buffer.DisposeData();
|
buffer.DecrementReferenceCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
newBuffer.SynchronizeMemory(address, newSize);
|
newBuffer.SynchronizeMemory(address, newSize);
|
||||||
|
|
125
Ryujinx.Graphics.Gpu/Memory/BufferMigration.cs
Normal file
125
Ryujinx.Graphics.Gpu/Memory/BufferMigration.cs
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 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.
|
||||||
|
/// </summary>
|
||||||
|
internal class BufferMigration : IDisposable
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The offset for the migrated region.
|
||||||
|
/// </summary>
|
||||||
|
private readonly ulong _offset;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The size for the migrated region.
|
||||||
|
/// </summary>
|
||||||
|
private readonly ulong _size;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The buffer that was migrated from.
|
||||||
|
/// </summary>
|
||||||
|
private readonly Buffer _buffer;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The source range action, to be called on overlap with an unreached sync number.
|
||||||
|
/// </summary>
|
||||||
|
private readonly Action<ulong, ulong> _sourceRangeAction;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The source range list.
|
||||||
|
/// </summary>
|
||||||
|
private readonly BufferModifiedRangeList _source;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The destination range list. This range list must be updated when flushing the source.
|
||||||
|
/// </summary>
|
||||||
|
public readonly BufferModifiedRangeList Destination;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The sync number that needs to be reached for this migration to be removed. This is set to the pending sync number on creation.
|
||||||
|
/// </summary>
|
||||||
|
public readonly ulong SyncNumber;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a record for a buffer migration.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="buffer">The source buffer for this migration</param>
|
||||||
|
/// <param name="sourceRangeAction">The flush action for the source buffer</param>
|
||||||
|
/// <param name="source">The modified range list for the source buffer</param>
|
||||||
|
/// <param name="dest">The modified range list for the destination buffer</param>
|
||||||
|
/// <param name="syncNumber">The sync number for when the migration is complete</param>
|
||||||
|
public BufferMigration(
|
||||||
|
Buffer buffer,
|
||||||
|
Action<ulong, ulong> sourceRangeAction,
|
||||||
|
BufferModifiedRangeList source,
|
||||||
|
BufferModifiedRangeList dest,
|
||||||
|
ulong syncNumber)
|
||||||
|
{
|
||||||
|
_offset = buffer.Address;
|
||||||
|
_size = buffer.Size;
|
||||||
|
_buffer = buffer;
|
||||||
|
_sourceRangeAction = sourceRangeAction;
|
||||||
|
_source = source;
|
||||||
|
Destination = dest;
|
||||||
|
SyncNumber = syncNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determine if the given range overlaps this migration, and has not been completed yet.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="offset">Start offset</param>
|
||||||
|
/// <param name="size">Range size</param>
|
||||||
|
/// <param name="syncNumber">The sync number that was waited on</param>
|
||||||
|
/// <returns>True if overlapping and in progress, false otherwise</returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determine if the given range matches this migration.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="offset">Start offset</param>
|
||||||
|
/// <param name="size">Range size</param>
|
||||||
|
/// <returns>True if the range exactly matches, false otherwise</returns>
|
||||||
|
public bool FullyMatches(ulong offset, ulong size)
|
||||||
|
{
|
||||||
|
return _offset == offset && _size == size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Perform the migration source's range action on the range provided, clamped to the bounds of the source buffer.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="offset">Start offset</param>
|
||||||
|
/// <param name="size">Range size</param>
|
||||||
|
/// <param name="syncNumber">Current sync number</param>
|
||||||
|
/// <param name="parent">The modified range list that originally owned this range</param>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes this reference to the range list, potentially allowing for the source buffer to be disposed.
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Destination.RemoveMigration(this);
|
||||||
|
|
||||||
|
_buffer.DecrementReferenceCount();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,8 @@
|
||||||
using Ryujinx.Common.Pools;
|
using Ryujinx.Common.Logging;
|
||||||
|
using Ryujinx.Common.Pools;
|
||||||
using Ryujinx.Memory.Range;
|
using Ryujinx.Memory.Range;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace Ryujinx.Graphics.Gpu.Memory
|
namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
|
@ -30,17 +32,24 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ulong SyncNumber { get; internal set; }
|
public ulong SyncNumber { get; internal set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The range list that originally owned this range.
|
||||||
|
/// </summary>
|
||||||
|
public BufferModifiedRangeList Parent { get; internal set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of a modified range.
|
/// Creates a new instance of a modified range.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="address">Start address of the range</param>
|
/// <param name="address">Start address of the range</param>
|
||||||
/// <param name="size">Size of the range in bytes</param>
|
/// <param name="size">Size of the range in bytes</param>
|
||||||
/// <param name="syncNumber">The GPU sync number at the time of creation</param>
|
/// <param name="syncNumber">The GPU sync number at the time of creation</param>
|
||||||
public BufferModifiedRange(ulong address, ulong size, ulong syncNumber)
|
/// <param name="parent">The range list that owns this range</param>
|
||||||
|
public BufferModifiedRange(ulong address, ulong size, ulong syncNumber, BufferModifiedRangeList parent)
|
||||||
{
|
{
|
||||||
Address = address;
|
Address = address;
|
||||||
Size = size;
|
Size = size;
|
||||||
SyncNumber = syncNumber;
|
SyncNumber = syncNumber;
|
||||||
|
Parent = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -63,16 +72,39 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
private const int BackingInitialSize = 8;
|
private const int BackingInitialSize = 8;
|
||||||
|
|
||||||
private GpuContext _context;
|
private GpuContext _context;
|
||||||
|
private Buffer _parent;
|
||||||
|
private Action<ulong, ulong> _flushAction;
|
||||||
|
|
||||||
|
private List<BufferMigration> _sources;
|
||||||
|
private BufferMigration _migrationTarget;
|
||||||
|
|
||||||
private object _lock = new object();
|
private object _lock = new object();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether the modified range list has any entries or not.
|
||||||
|
/// </summary>
|
||||||
|
public bool HasRanges
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
return Count > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of a modified range list.
|
/// Creates a new instance of a modified range list.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context">GPU context that the buffer range list belongs to</param>
|
/// <param name="context">GPU context that the buffer range list belongs to</param>
|
||||||
public BufferModifiedRangeList(GpuContext context) : base(BackingInitialSize)
|
/// <param name="parent">The parent buffer that owns this range list</param>
|
||||||
|
/// <param name="flushAction">The flush action for the parent buffer</param>
|
||||||
|
public BufferModifiedRangeList(GpuContext context, Buffer parent, Action<ulong, ulong> flushAction) : base(BackingInitialSize)
|
||||||
{
|
{
|
||||||
_context = context;
|
_context = context;
|
||||||
|
_parent = parent;
|
||||||
|
_flushAction = flushAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -142,6 +174,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
{
|
{
|
||||||
// Region already exists. Just update the existing sync number.
|
// Region already exists. Just update the existing sync number.
|
||||||
overlap.SyncNumber = syncNumber;
|
overlap.SyncNumber = syncNumber;
|
||||||
|
overlap.Parent = this;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -152,18 +185,18 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
{
|
{
|
||||||
// A split item must be created behind this overlap.
|
// A split item must be created behind this overlap.
|
||||||
|
|
||||||
Add(new BufferModifiedRange(overlap.Address, address - overlap.Address, overlap.SyncNumber));
|
Add(new BufferModifiedRange(overlap.Address, address - overlap.Address, overlap.SyncNumber, overlap.Parent));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (overlap.Address < endAddress && overlap.EndAddress > endAddress)
|
if (overlap.Address < endAddress && overlap.EndAddress > endAddress)
|
||||||
{
|
{
|
||||||
// A split item must be created after this overlap.
|
// A split item must be created after this overlap.
|
||||||
|
|
||||||
Add(new BufferModifiedRange(endAddress, overlap.EndAddress - endAddress, overlap.SyncNumber));
|
Add(new BufferModifiedRange(endAddress, overlap.EndAddress - endAddress, overlap.SyncNumber, overlap.Parent));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Add(new BufferModifiedRange(address, size, syncNumber));
|
Add(new BufferModifiedRange(address, size, syncNumber, this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,9 +240,102 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Performs the given range action, or one from a migration that overlaps and has not synced yet.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="offset">The offset to pass to the action</param>
|
||||||
|
/// <param name="size">The size to pass to the action</param>
|
||||||
|
/// <param name="syncNumber">The sync number that has been reached</param>
|
||||||
|
/// <param name="parent">The modified range list that originally owned this range</param>
|
||||||
|
/// <param name="rangeAction">The action to perform</param>
|
||||||
|
public void RangeActionWithMigration(ulong offset, ulong size, ulong syncNumber, BufferModifiedRangeList parent, Action<ulong, ulong> rangeAction)
|
||||||
|
{
|
||||||
|
bool firstSource = true;
|
||||||
|
|
||||||
|
if (parent != this)
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
if (_sources != null)
|
||||||
|
{
|
||||||
|
foreach (BufferMigration source in _sources)
|
||||||
|
{
|
||||||
|
if (source.Overlaps(offset, size, syncNumber))
|
||||||
|
{
|
||||||
|
if (firstSource && !source.FullyMatches(offset, size))
|
||||||
|
{
|
||||||
|
// Perform this buffer's action first. The migrations will run after.
|
||||||
|
rangeAction(offset, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
source.RangeActionWithMigration(offset, size, syncNumber, parent);
|
||||||
|
|
||||||
|
firstSource = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (firstSource)
|
||||||
|
{
|
||||||
|
// No overlapping migrations, or they are not meant for this range, flush the data using the given action.
|
||||||
|
rangeAction(offset, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes modified ranges ready by the sync number from the list, and flushes their buffer data within a given address range.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="overlaps">Overlapping ranges to check</param>
|
||||||
|
/// <param name="rangeCount">Number of overlapping ranges</param>
|
||||||
|
/// <param name="highestDiff">The highest difference between an overlapping range's sync number and the current one</param>
|
||||||
|
/// <param name="currentSync">The current sync number</param>
|
||||||
|
/// <param name="address">The start address of the flush range</param>
|
||||||
|
/// <param name="endAddress">The end address of the flush range</param>
|
||||||
|
private void RemoveRangesAndFlush(
|
||||||
|
BufferModifiedRange[] overlaps,
|
||||||
|
int rangeCount,
|
||||||
|
long highestDiff,
|
||||||
|
ulong currentSync,
|
||||||
|
ulong address,
|
||||||
|
ulong endAddress)
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
if (_migrationTarget == null)
|
||||||
|
{
|
||||||
|
ulong waitSync = currentSync + (ulong)highestDiff;
|
||||||
|
|
||||||
|
for (int i = 0; i < rangeCount; i++)
|
||||||
|
{
|
||||||
|
BufferModifiedRange overlap = overlaps[i];
|
||||||
|
|
||||||
|
long diff = (long)(overlap.SyncNumber - currentSync);
|
||||||
|
|
||||||
|
if (diff <= highestDiff)
|
||||||
|
{
|
||||||
|
ulong clampAddress = Math.Max(address, overlap.Address);
|
||||||
|
ulong clampEnd = Math.Min(endAddress, overlap.EndAddress);
|
||||||
|
|
||||||
|
ClearPart(overlap, clampAddress, clampEnd);
|
||||||
|
|
||||||
|
RangeActionWithMigration(clampAddress, clampEnd - clampAddress, waitSync, overlap.Parent, _flushAction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// There is a migration target to call instead. This can't be changed after set so accessing it outside the lock is fine.
|
||||||
|
|
||||||
|
_migrationTarget.Destination.RemoveRangesAndFlush(overlaps, rangeCount, highestDiff, currentSync, address, endAddress);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets modified ranges within the specified region, waits on ones from a previous sync number,
|
/// Gets modified ranges within the specified region, waits on ones from a previous sync number,
|
||||||
/// and then fires the given action for each range individually.
|
/// and then fires the flush action for each range individually.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This function assumes it is called from the background thread.
|
/// This function assumes it is called from the background thread.
|
||||||
|
@ -218,8 +344,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="address">Start address to query</param>
|
/// <param name="address">Start address to query</param>
|
||||||
/// <param name="size">Size to query</param>
|
/// <param name="size">Size to query</param>
|
||||||
/// <param name="rangeAction">The action to call for each modified range</param>
|
public void WaitForAndFlushRanges(ulong address, ulong size)
|
||||||
public void WaitForAndGetRanges(ulong address, ulong size, Action<ulong, ulong> rangeAction)
|
|
||||||
{
|
{
|
||||||
ulong endAddress = address + size;
|
ulong endAddress = address + size;
|
||||||
ulong currentSync = _context.SyncNumber;
|
ulong currentSync = _context.SyncNumber;
|
||||||
|
@ -230,11 +355,24 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
|
|
||||||
// Range list must be consistent for this operation
|
// Range list must be consistent for this operation
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
|
{
|
||||||
|
if (_migrationTarget != null)
|
||||||
|
{
|
||||||
|
rangeCount = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
rangeCount = FindOverlapsNonOverlapping(address, size, ref overlaps);
|
rangeCount = FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (rangeCount == 0)
|
if (rangeCount == -1)
|
||||||
|
{
|
||||||
|
_migrationTarget.Destination.WaitForAndFlushRanges(address, size);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (rangeCount == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -264,93 +402,59 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
// Wait for the syncpoint.
|
// Wait for the syncpoint.
|
||||||
_context.Renderer.WaitSync(currentSync + (ulong)highestDiff);
|
_context.Renderer.WaitSync(currentSync + (ulong)highestDiff);
|
||||||
|
|
||||||
// Flush and remove all regions with the older syncpoint.
|
RemoveRangesAndFlush(overlaps, rangeCount, highestDiff, currentSync, address, endAddress);
|
||||||
lock (_lock)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < rangeCount; i++)
|
|
||||||
{
|
|
||||||
BufferModifiedRange overlap = overlaps[i];
|
|
||||||
|
|
||||||
long diff = (long)(overlap.SyncNumber - currentSync);
|
|
||||||
|
|
||||||
if (diff <= highestDiff)
|
|
||||||
{
|
|
||||||
ulong clampAddress = Math.Max(address, overlap.Address);
|
|
||||||
ulong clampEnd = Math.Min(endAddress, overlap.EndAddress);
|
|
||||||
|
|
||||||
ClearPart(overlap, clampAddress, clampEnd);
|
|
||||||
|
|
||||||
rangeAction(clampAddress, clampEnd - clampAddress);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Inherit ranges from another modified range list.
|
/// Inherit ranges from another modified range list.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="ranges">The range list to inherit from</param>
|
/// <param name="ranges">The range list to inherit from</param>
|
||||||
/// <param name="rangeAction">The action to call for each modified range</param>
|
/// <param name="registerRangeAction">The action to call for each modified range</param>
|
||||||
public void InheritRanges(BufferModifiedRangeList ranges, Action<ulong, ulong> rangeAction)
|
public void InheritRanges(BufferModifiedRangeList ranges, Action<ulong, ulong> registerRangeAction)
|
||||||
{
|
{
|
||||||
BufferModifiedRange[] inheritRanges;
|
BufferModifiedRange[] inheritRanges;
|
||||||
|
|
||||||
lock (ranges._lock)
|
lock (ranges._lock)
|
||||||
{
|
{
|
||||||
|
BufferMigration migration = new(ranges._parent, ranges._flushAction, ranges, this, _context.SyncNumber);
|
||||||
|
|
||||||
|
ranges._parent.IncrementReferenceCount();
|
||||||
|
ranges._migrationTarget = migration;
|
||||||
|
|
||||||
|
_context.RegisterBufferMigration(migration);
|
||||||
|
|
||||||
inheritRanges = ranges.ToArray();
|
inheritRanges = ranges.ToArray();
|
||||||
}
|
|
||||||
|
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
|
(_sources ??= new List<BufferMigration>()).Add(migration);
|
||||||
|
|
||||||
foreach (BufferModifiedRange range in inheritRanges)
|
foreach (BufferModifiedRange range in inheritRanges)
|
||||||
{
|
{
|
||||||
Add(range);
|
Add(range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ulong currentSync = _context.SyncNumber;
|
ulong currentSync = _context.SyncNumber;
|
||||||
foreach (BufferModifiedRange range in inheritRanges)
|
foreach (BufferModifiedRange range in inheritRanges)
|
||||||
{
|
{
|
||||||
if (range.SyncNumber != currentSync)
|
if (range.SyncNumber != currentSync)
|
||||||
{
|
{
|
||||||
rangeAction(range.Address, range.Size);
|
registerRangeAction(range.Address, range.Size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Calls the given action for modified ranges that aren't from the current sync number.
|
/// Removes a source buffer migration, indicating its copy has completed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="rangeAction">The action to call for each modified range</param>
|
/// <param name="migration">The migration to remove</param>
|
||||||
public void ReregisterRanges(Action<ulong, ulong> rangeAction)
|
public void RemoveMigration(BufferMigration migration)
|
||||||
{
|
{
|
||||||
ref var ranges = ref ThreadStaticArray<BufferModifiedRange>.Get();
|
|
||||||
int count;
|
|
||||||
|
|
||||||
// Range list must be consistent for this operation.
|
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
count = Count;
|
_sources.Remove(migration);
|
||||||
if (ranges.Length < count)
|
|
||||||
{
|
|
||||||
Array.Resize(ref ranges, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
foreach (BufferModifiedRange range in this)
|
|
||||||
{
|
|
||||||
ranges[i++] = range;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ulong currentSync = _context.SyncNumber;
|
|
||||||
for (int i = 0; i < count; i++)
|
|
||||||
{
|
|
||||||
BufferModifiedRange range = ranges[i];
|
|
||||||
if (range.SyncNumber != currentSync)
|
|
||||||
{
|
|
||||||
rangeAction(range.Address, range.Size);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -362,12 +466,12 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
|
|
||||||
if (overlap.Address < address)
|
if (overlap.Address < address)
|
||||||
{
|
{
|
||||||
Add(new BufferModifiedRange(overlap.Address, address - overlap.Address, overlap.SyncNumber));
|
Add(new BufferModifiedRange(overlap.Address, address - overlap.Address, overlap.SyncNumber, overlap.Parent));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (overlap.EndAddress > endAddress)
|
if (overlap.EndAddress > endAddress)
|
||||||
{
|
{
|
||||||
Add(new BufferModifiedRange(endAddress, overlap.EndAddress - endAddress, overlap.SyncNumber));
|
Add(new BufferModifiedRange(endAddress, overlap.EndAddress - endAddress, overlap.SyncNumber, overlap.Parent));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -238,6 +238,11 @@ namespace Ryujinx.Graphics.OpenGL
|
||||||
_sync.Wait(id);
|
_sync.Wait(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ulong GetCurrentSync()
|
||||||
|
{
|
||||||
|
return _sync.GetCurrent();
|
||||||
|
}
|
||||||
|
|
||||||
public void Screenshot()
|
public void Screenshot()
|
||||||
{
|
{
|
||||||
_window.ScreenCaptureRequested = true;
|
_window.ScreenCaptureRequested = true;
|
||||||
|
|
|
@ -40,6 +40,37 @@ namespace Ryujinx.Graphics.OpenGL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ulong GetCurrent()
|
||||||
|
{
|
||||||
|
lock (_handles)
|
||||||
|
{
|
||||||
|
ulong lastHandle = _firstHandle;
|
||||||
|
|
||||||
|
foreach (SyncHandle handle in _handles)
|
||||||
|
{
|
||||||
|
lock (handle)
|
||||||
|
{
|
||||||
|
if (handle.Handle == IntPtr.Zero)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (handle.ID > lastHandle)
|
||||||
|
{
|
||||||
|
WaitSyncStatus syncResult = GL.ClientWaitSync(handle.Handle, _syncFlags, 0);
|
||||||
|
|
||||||
|
if (syncResult == WaitSyncStatus.AlreadySignaled)
|
||||||
|
{
|
||||||
|
lastHandle = handle.ID;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastHandle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Wait(ulong id)
|
public void Wait(ulong id)
|
||||||
{
|
{
|
||||||
SyncHandle result = null;
|
SyncHandle result = null;
|
||||||
|
|
|
@ -11,6 +11,7 @@ namespace Ryujinx.Graphics.Vulkan
|
||||||
{
|
{
|
||||||
public ulong ID;
|
public ulong ID;
|
||||||
public MultiFenceHolder Waitable;
|
public MultiFenceHolder Waitable;
|
||||||
|
public bool Signalled;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ulong _firstHandle = 0;
|
private ulong _firstHandle = 0;
|
||||||
|
@ -45,6 +46,37 @@ namespace Ryujinx.Graphics.Vulkan
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ulong GetCurrent()
|
||||||
|
{
|
||||||
|
lock (_handles)
|
||||||
|
{
|
||||||
|
ulong lastHandle = _firstHandle;
|
||||||
|
|
||||||
|
foreach (SyncHandle handle in _handles)
|
||||||
|
{
|
||||||
|
lock (handle)
|
||||||
|
{
|
||||||
|
if (handle.Waitable == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (handle.ID > lastHandle)
|
||||||
|
{
|
||||||
|
bool signaled = handle.Signalled || handle.Waitable.WaitForFences(_gd.Api, _device, 0);
|
||||||
|
if (signaled)
|
||||||
|
{
|
||||||
|
lastHandle = handle.ID;
|
||||||
|
handle.Signalled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastHandle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Wait(ulong id)
|
public void Wait(ulong id)
|
||||||
{
|
{
|
||||||
SyncHandle result = null;
|
SyncHandle result = null;
|
||||||
|
@ -75,11 +107,15 @@ namespace Ryujinx.Graphics.Vulkan
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool signaled = result.Waitable.WaitForFences(_gd.Api, _device, 1000000000);
|
bool signaled = result.Signalled || result.Waitable.WaitForFences(_gd.Api, _device, 1000000000);
|
||||||
if (!signaled)
|
if (!signaled)
|
||||||
{
|
{
|
||||||
Logger.Error?.PrintMsg(LogClass.Gpu, $"VK Sync Object {result.ID} failed to signal within 1000ms. Continuing...");
|
Logger.Error?.PrintMsg(LogClass.Gpu, $"VK Sync Object {result.ID} failed to signal within 1000ms. Continuing...");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result.Signalled = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -565,6 +565,11 @@ namespace Ryujinx.Graphics.Vulkan
|
||||||
_syncManager.Wait(id);
|
_syncManager.Wait(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ulong GetCurrentSync()
|
||||||
|
{
|
||||||
|
return _syncManager.GetCurrent();
|
||||||
|
}
|
||||||
|
|
||||||
public void Screenshot()
|
public void Screenshot()
|
||||||
{
|
{
|
||||||
_window.ScreenCaptureRequested = true;
|
_window.ScreenCaptureRequested = true;
|
||||||
|
|
Reference in a new issue