See merge request ryubing/ryujinx!46
This commit is contained in:
LotP
2025-08-06 15:57:08 -05:00
parent 54d4d184f4
commit 5613d3f35d
17 changed files with 1596 additions and 748 deletions

View File

@@ -1,25 +1,24 @@
using Ryujinx.Common.Pools;
using Ryujinx.Memory.Range;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Ryujinx.Graphics.Gpu.Memory
{
/// <summary>
/// A range within a buffer that has been modified by the GPU.
/// </summary>
class BufferModifiedRange : IRange
class BufferModifiedRange : INonOverlappingRange
{
/// <summary>
/// Start address of the range in guest memory.
/// </summary>
public ulong Address { get; }
public ulong Address { get; internal set; }
/// <summary>
/// Size of the range in bytes.
/// </summary>
public ulong Size { get; }
public ulong Size { get; internal set; }
/// <summary>
/// End address of the range in guest memory.
@@ -61,14 +60,19 @@ namespace Ryujinx.Graphics.Gpu.Memory
{
return Address < address + size && address < EndAddress;
}
public INonOverlappingRange Split(ulong splitAddress)
{
throw new NotImplementedException();
}
}
/// <summary>
/// A structure used to track GPU modified ranges within a buffer.
/// </summary>
class BufferModifiedRangeList : RangeList<BufferModifiedRange>
class BufferModifiedRangeList : NonOverlappingRangeList<BufferModifiedRange>
{
private const int BackingInitialSize = 8;
private new const int BackingInitialSize = 8;
private readonly GpuContext _context;
private readonly Buffer _parent;
@@ -77,8 +81,6 @@ namespace Ryujinx.Graphics.Gpu.Memory
private BufferMigration _source;
private BufferModifiedRangeList _migrationTarget;
private readonly Lock _lock = new();
/// <summary>
/// Whether the modified range list has any entries or not.
/// </summary>
@@ -86,10 +88,10 @@ namespace Ryujinx.Graphics.Gpu.Memory
{
get
{
lock (_lock)
{
return Count > 0;
}
Lock.EnterReadLock();
bool result = Count > 0;
Lock.ExitReadLock();
return result;
}
}
@@ -114,33 +116,41 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <param name="action">Action to perform for each remaining sub-range of the input range</param>
public void ExcludeModifiedRegions(ulong address, ulong size, Action<ulong, ulong> action)
{
lock (_lock)
// Slices a given region using the modified regions in the list. Calls the action for the new slices.
bool lockOwner = Lock.IsReadLockHeld;
if (!lockOwner)
{
// Slices a given region using the modified regions in the list. Calls the action for the new slices.
ref BufferModifiedRange[] overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
Lock.EnterReadLock();
}
int count = FindOverlapsNonOverlapping(address, size, ref overlaps);
(RangeItem<BufferModifiedRange> first, RangeItem<BufferModifiedRange> last) = FindOverlaps(address, size);
for (int i = 0; i < count; i++)
RangeItem<BufferModifiedRange> current = first;
while (last != null && current != last.Next)
{
BufferModifiedRange overlap = current.Value;
if (overlap.Address > address)
{
BufferModifiedRange overlap = overlaps[i];
if (overlap.Address > address)
{
// The start of the remaining region is uncovered by this overlap. Call the action for it.
action(address, overlap.Address - address);
}
// Remaining region is after this overlap.
size -= overlap.EndAddress - address;
address = overlap.EndAddress;
// The start of the remaining region is uncovered by this overlap. Call the action for it.
action(address, overlap.Address - address);
}
if ((long)size > 0)
{
// If there is any region left after removing the overlaps, signal it.
action(address, size);
}
// Remaining region is after this overlap.
size -= overlap.EndAddress - address;
address = overlap.EndAddress;
current = current.Next;
}
if (!lockOwner)
{
Lock.ExitReadLock();
}
if ((long)size > 0)
{
// If there is any region left after removing the overlaps, signal it.
action(address, size);
}
}
@@ -152,51 +162,101 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <param name="size">Size of the modified region in bytes</param>
public void SignalModified(ulong address, ulong size)
{
// Must lock, as this can affect flushes from the background thread.
lock (_lock)
// We may overlap with some existing modified regions. They must be cut into by the new entry.
Lock.EnterWriteLock();
(RangeItem<BufferModifiedRange> first, RangeItem<BufferModifiedRange> last) = FindOverlaps(address, size);
ulong endAddress = address + size;
ulong syncNumber = _context.SyncNumber;
if (first is null)
{
// We may overlap with some existing modified regions. They must be cut into by the new entry.
ref BufferModifiedRange[] overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
Add(new BufferModifiedRange(address, size, syncNumber, this));
Lock.ExitWriteLock();
return;
}
int count = FindOverlapsNonOverlapping(address, size, ref overlaps);
BufferModifiedRange buffPost = null;
bool extendsPost = false;
bool extendsPre = false;
ulong endAddress = address + size;
ulong syncNumber = _context.SyncNumber;
for (int i = 0; i < count; i++)
if (first == last)
{
if (first.Address == address && first.EndAddress == endAddress)
{
// The overlaps must be removed or split.
first.Value.SyncNumber = syncNumber;
first.Value.Parent = this;
Lock.ExitWriteLock();
return;
}
BufferModifiedRange overlap = overlaps[i];
if (first.Address < address)
{
first.Value.Size = address - first.Address;
if (overlap.Address == address && overlap.Size == size)
extendsPre = true;
if (first.EndAddress > endAddress)
{
// Region already exists. Just update the existing sync number.
overlap.SyncNumber = syncNumber;
overlap.Parent = this;
return;
buffPost = new BufferModifiedRange(endAddress, first.EndAddress - endAddress,
first.Value.SyncNumber, first.Value.Parent);
extendsPost = true;
}
Remove(overlap);
if (overlap.Address < address && overlap.EndAddress > address)
}
else
{
if (first.EndAddress > endAddress)
{
// A split item must be created behind this overlap.
Add(new BufferModifiedRange(overlap.Address, address - overlap.Address, overlap.SyncNumber, overlap.Parent));
first.Value.Size = first.EndAddress - endAddress;
first.Value.Address = endAddress;
}
if (overlap.Address < endAddress && overlap.EndAddress > endAddress)
else
{
// A split item must be created after this overlap.
Add(new BufferModifiedRange(endAddress, overlap.EndAddress - endAddress, overlap.SyncNumber, overlap.Parent));
Remove(first.Value);
}
}
if (extendsPre && extendsPost)
{
Add(buffPost);
}
Add(new BufferModifiedRange(address, size, syncNumber, this));
Lock.ExitWriteLock();
return;
}
BufferModifiedRange buffPre = null;
if (first.Address < address)
{
buffPre = new BufferModifiedRange(first.Address, address - first.Address,
first.Value.SyncNumber, first.Value.Parent);
extendsPre = true;
}
if (last.EndAddress > endAddress)
{
buffPost = new BufferModifiedRange(endAddress, last.EndAddress - endAddress,
last.Value.SyncNumber, last.Value.Parent);
extendsPost = true;
}
RemoveRange(first, last);
if (extendsPre)
{
Add(buffPre);
}
if (extendsPost)
{
Add(buffPost);
}
Add(new BufferModifiedRange(address, size, syncNumber, this));
Lock.ExitWriteLock();
}
/// <summary>
@@ -208,25 +268,23 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <param name="rangeAction">The action to call for each modified range</param>
public void GetRangesAtSync(ulong address, ulong size, ulong syncNumber, Action<ulong, ulong> rangeAction)
{
int count = 0;
Lock.EnterReadLock();
(RangeItem<BufferModifiedRange> first, RangeItem<BufferModifiedRange> last) = FindOverlaps(address, size);
ref BufferModifiedRange[] overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
// Range list must be consistent for this operation.
lock (_lock)
RangeItem<BufferModifiedRange> current = first;
while (last != null && current != last.Next)
{
count = FindOverlapsNonOverlapping(address, size, ref overlaps);
}
for (int i = 0; i < count; i++)
{
BufferModifiedRange overlap = overlaps[i];
BufferModifiedRange overlap = current.Value;
if (overlap.SyncNumber == syncNumber)
{
rangeAction(overlap.Address, overlap.Size);
}
current = current.Next;
}
Lock.ExitReadLock();
}
/// <summary>
@@ -237,19 +295,23 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <param name="rangeAction">The action to call for each modified range</param>
public void GetRanges(ulong address, ulong size, Action<ulong, ulong> rangeAction)
{
int count = 0;
ref BufferModifiedRange[] overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
// Range list must be consistent for this operation.
lock (_lock)
List<RangeItem<BufferModifiedRange>> overlaps = [];
// We use the non-span method here because keeping the lock will cause a deadlock.
Lock.EnterReadLock();
(RangeItem<BufferModifiedRange> first, RangeItem<BufferModifiedRange> last) = FindOverlaps(address, size);
RangeItem<BufferModifiedRange> current = first;
while (last != null && current != last.Next)
{
count = FindOverlapsNonOverlapping(address, size, ref overlaps);
overlaps.Add(current);
current = current.Next;
}
Lock.ExitReadLock();
for (int i = 0; i < count; i++)
for (int i = 0; i < overlaps.Count; i++)
{
BufferModifiedRange overlap = overlaps[i];
BufferModifiedRange overlap = overlaps[i].Value;
rangeAction(overlap.Address, overlap.Size);
}
}
@@ -262,11 +324,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <returns>True if a range exists in the specified region, false otherwise</returns>
public bool HasRange(ulong address, ulong size)
{
// Range list must be consistent for this operation.
lock (_lock)
{
return FindOverlapsNonOverlapping(address, size, ref ThreadStaticArray<BufferModifiedRange>.Get()) > 0;
}
Lock.EnterReadLock();
(RangeItem<BufferModifiedRange> first, RangeItem<BufferModifiedRange> _) = FindOverlaps(address, size);
bool result = first is not null;
Lock.ExitReadLock();
return result;
}
/// <summary>
@@ -298,38 +360,37 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <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,
RangeItem<BufferModifiedRange>[] overlaps,
int rangeCount,
long highestDiff,
ulong currentSync,
ulong address,
ulong endAddress)
{
lock (_lock)
if (_migrationTarget == null)
{
if (_migrationTarget == null)
ulong waitSync = currentSync + (ulong)highestDiff;
for (int i = 0; i < rangeCount; i++)
{
ulong waitSync = currentSync + (ulong)highestDiff;
BufferModifiedRange overlap = overlaps[i].Value;
for (int i = 0; i < rangeCount; i++)
long diff = (long)(overlap.SyncNumber - currentSync);
if (diff <= highestDiff)
{
BufferModifiedRange overlap = overlaps[i];
ulong clampAddress = Math.Max(address, overlap.Address);
ulong clampEnd = Math.Min(endAddress, overlap.EndAddress);
long diff = (long)(overlap.SyncNumber - currentSync);
Lock.EnterWriteLock();
ClearPart(overlap, clampAddress, clampEnd);
Lock.ExitWriteLock();
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, _flushAction);
}
RangeActionWithMigration(clampAddress, clampEnd - clampAddress, waitSync, _flushAction);
}
return;
}
return;
}
// There is a migration target to call instead. This can't be changed after set so accessing it outside the lock is fine.
@@ -355,28 +416,37 @@ namespace Ryujinx.Graphics.Gpu.Memory
int rangeCount = 0;
ref BufferModifiedRange[] overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
List<RangeItem<BufferModifiedRange>> overlaps = [];
// Range list must be consistent for this operation
lock (_lock)
Lock.EnterReadLock();
if (_migrationTarget != null)
{
if (_migrationTarget != null)
rangeCount = -1;
}
else
{
// We use the non-span method here because the array is partially modified by the code, which would invalidate a span.
(RangeItem<BufferModifiedRange> first, RangeItem<BufferModifiedRange> last) = FindOverlaps(address, size);
RangeItem<BufferModifiedRange> current = first;
while (last != null && current != last.Next)
{
rangeCount = -1;
}
else
{
rangeCount = FindOverlapsNonOverlapping(address, size, ref overlaps);
rangeCount++;
overlaps.Add(current);
current = current.Next;
}
}
Lock.ExitReadLock();
if (rangeCount == -1)
{
_migrationTarget.WaitForAndFlushRanges(address, size);
_migrationTarget!.WaitForAndFlushRanges(address, size);
return;
}
else if (rangeCount == 0)
if (rangeCount == 0)
{
return;
}
@@ -388,7 +458,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
for (int i = 0; i < rangeCount; i++)
{
BufferModifiedRange overlap = overlaps[i];
BufferModifiedRange overlap = overlaps[i].Value;
long diff = (long)(overlap.SyncNumber - currentSync);
@@ -406,7 +476,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
// Wait for the syncpoint.
_context.Renderer.WaitSync(currentSync + (ulong)highestDiff);
RemoveRangesAndFlush(overlaps, rangeCount, highestDiff, currentSync, address, endAddress);
RemoveRangesAndFlush(overlaps.ToArray(), rangeCount, highestDiff, currentSync, address, endAddress);
}
/// <summary>
@@ -419,42 +489,39 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <param name="registerRangeAction">The action to call for each modified range</param>
public void InheritRanges(BufferModifiedRangeList ranges, Action<ulong, ulong> registerRangeAction)
{
BufferModifiedRange[] inheritRanges;
ranges.Lock.EnterReadLock();
BufferModifiedRange[] inheritRanges = ranges.ToArray();
ranges.Lock.ExitReadLock();
lock (ranges._lock)
// Copy over the migration from the previous range list
BufferMigration oldMigration = ranges._source;
BufferMigrationSpan span = new(ranges._parent, ranges._flushAction, oldMigration);
ranges._parent.IncrementReferenceCount();
if (_source == null)
{
inheritRanges = ranges.ToArray();
// Create a new migration.
_source = new BufferMigration([span], this, _context.SyncNumber);
lock (_lock)
{
// Copy over the migration from the previous range list
BufferMigration oldMigration = ranges._source;
BufferMigrationSpan span = new(ranges._parent, ranges._flushAction, oldMigration);
ranges._parent.IncrementReferenceCount();
if (_source == null)
{
// Create a new migration.
_source = new BufferMigration([span], this, _context.SyncNumber);
_context.RegisterBufferMigration(_source);
}
else
{
// Extend the migration
_source.AddSpanToEnd(span);
}
ranges._migrationTarget = this;
foreach (BufferModifiedRange range in inheritRanges)
{
Add(range);
}
}
_context.RegisterBufferMigration(_source);
}
else
{
// Extend the migration
_source.AddSpanToEnd(span);
}
ranges._migrationTarget = this;
Lock.EnterWriteLock();
foreach (BufferModifiedRange range in inheritRanges)
{
Add(range);
}
Lock.ExitWriteLock();
ulong currentSync = _context.SyncNumber;
foreach (BufferModifiedRange range in inheritRanges)
@@ -473,18 +540,18 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// </summary>
public void SelfMigration()
{
lock (_lock)
{
BufferMigrationSpan span = new(_parent, _parent.GetSnapshotDisposeAction(), _parent.GetSnapshotFlushAction(), _source);
BufferMigration migration = new([span], this, _context.SyncNumber);
BufferMigrationSpan span = new(_parent, _parent.GetSnapshotDisposeAction(),
_parent.GetSnapshotFlushAction(), _source);
BufferMigration migration = new([span], this, _context.SyncNumber);
// Migration target is used to redirect flush actions to the latest range list,
// so we don't need to set it here. (this range list is still the latest)
// Migration target is used to redirect flush actions to the latest range list,
// so we don't need to set it here. (this range list is still the latest)
_context.RegisterBufferMigration(migration);
_context.RegisterBufferMigration(migration);
_source = migration;
}
Lock.EnterWriteLock();
_source = migration;
Lock.ExitWriteLock();
}
/// <summary>
@@ -493,13 +560,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <param name="migration">The migration to remove</param>
public void RemoveMigration(BufferMigration migration)
{
lock (_lock)
Lock.EnterWriteLock();
if (_source == migration)
{
if (_source == migration)
{
_source = null;
}
_source = null;
}
Lock.ExitWriteLock();
}
private void ClearPart(BufferModifiedRange overlap, ulong address, ulong endAddress)
@@ -526,33 +593,85 @@ namespace Ryujinx.Graphics.Gpu.Memory
/// <param name="size">Size to clear</param>
public void Clear(ulong address, ulong size)
{
lock (_lock)
ulong endAddress = address + size;
Lock.EnterWriteLock();
(RangeItem<BufferModifiedRange> first, RangeItem<BufferModifiedRange> last) = FindOverlaps(address, size);
if (first is null)
{
// This function can be called from any thread, so it cannot use the arrays for background or foreground.
BufferModifiedRange[] toClear = new BufferModifiedRange[1];
Lock.ExitWriteLock();
return;
}
int rangeCount = FindOverlapsNonOverlapping(address, size, ref toClear);
BufferModifiedRange buffPost = null;
bool extendsPost = false;
bool extendsPre = false;
ulong endAddress = address + size;
for (int i = 0; i < rangeCount; i++)
if (first == last)
{
if (first.Address < address)
{
BufferModifiedRange overlap = toClear[i];
first.Value.Size = address - first.Address;
extendsPre = true;
ClearPart(overlap, address, endAddress);
if (first.EndAddress > endAddress)
{
buffPost = new BufferModifiedRange(endAddress, first.EndAddress - endAddress,
first.Value.SyncNumber, first.Value.Parent);
extendsPost = true;
}
}
else
{
if (first.EndAddress > endAddress)
{
first.Value.Size = first.EndAddress - endAddress;
first.Value.Address = endAddress;
}
else
{
Remove(first.Value);
}
}
}
}
/// <summary>
/// Clear all modified ranges.
/// </summary>
public void Clear()
{
lock (_lock)
{
Count = 0;
if (extendsPre && extendsPost)
{
Add(buffPost);
}
Lock.ExitWriteLock();
return;
}
BufferModifiedRange buffPre = null;
if (first.Address < address)
{
buffPre = new BufferModifiedRange(first.Address, address - first.Address,
first.Value.SyncNumber, first.Value.Parent);
extendsPre = true;
}
if (last.EndAddress > endAddress)
{
buffPost = new BufferModifiedRange(endAddress, last.EndAddress - endAddress,
last.Value.SyncNumber, last.Value.Parent);
extendsPost = true;
}
RemoveRange(first, last);
if (extendsPre)
{
Add(buffPre);
}
if (extendsPost)
{
Add(buffPost);
}
Lock.ExitWriteLock();
}
}
}