mirror of
https://git.ryujinx.app/ryubing/ryujinx.git
synced 2025-07-27 03:47:11 +02:00
initial commit
This commit is contained in:
parent
d688fed7d2
commit
be8f4897a2
10 changed files with 935 additions and 407 deletions
|
@ -110,7 +110,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
ulong size,
|
||||
BufferStage stage,
|
||||
bool sparseCompatible,
|
||||
IEnumerable<Buffer> baseBuffers = null)
|
||||
IEnumerable<RangeItem<Buffer>> baseBuffers = null)
|
||||
{
|
||||
_context = context;
|
||||
_physicalMemory = physicalMemory;
|
||||
|
@ -132,13 +132,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
{
|
||||
baseHandles = baseBuffers.SelectMany(buffer =>
|
||||
{
|
||||
if (buffer._useGranular)
|
||||
if (buffer.Value._useGranular)
|
||||
{
|
||||
return buffer._memoryTrackingGranular.GetHandles();
|
||||
return buffer.Value._memoryTrackingGranular.GetHandles();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Enumerable.Repeat(buffer._memoryTracking, 1);
|
||||
return Enumerable.Repeat(buffer.Value._memoryTracking, 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Ryujinx.Graphics.GAL;
|
||||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
@ -56,7 +57,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// <param name="parent">Parent buffer</param>
|
||||
/// <param name="stage">Initial buffer stage</param>
|
||||
/// <param name="baseBuffers">Buffers to inherit state from</param>
|
||||
public BufferBackingState(GpuContext context, Buffer parent, BufferStage stage, IEnumerable<Buffer> baseBuffers = null)
|
||||
public BufferBackingState(GpuContext context, Buffer parent, BufferStage stage, IEnumerable<RangeItem<Buffer>> baseBuffers = null)
|
||||
{
|
||||
_size = (int)parent.Size;
|
||||
_systemMemoryType = context.Capabilities.MemoryType;
|
||||
|
@ -102,9 +103,9 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
|
||||
if (baseBuffers != null)
|
||||
{
|
||||
foreach (Buffer buffer in baseBuffers)
|
||||
foreach (RangeItem<Buffer> buffer in baseBuffers)
|
||||
{
|
||||
CombineState(buffer.BackingState);
|
||||
CombineState(buffer.Value.BackingState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ using Ryujinx.Graphics.GAL;
|
|||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Memory
|
||||
|
@ -42,7 +41,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
private readonly RangeList<Buffer> _buffers;
|
||||
private readonly MultiRangeList<MultiRangeBuffer> _multiRangeBuffers;
|
||||
|
||||
private Buffer[] _bufferOverlaps;
|
||||
private RangeItem<Buffer>[] _bufferOverlaps;
|
||||
|
||||
private readonly Dictionary<ulong, BufferCacheEntry> _dirtyCache;
|
||||
private readonly Dictionary<ulong, BufferCacheEntry> _modifiedCache;
|
||||
|
@ -64,7 +63,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
_buffers = [];
|
||||
_multiRangeBuffers = [];
|
||||
|
||||
_bufferOverlaps = new Buffer[OverlapsBufferInitialCapacity];
|
||||
_bufferOverlaps = new RangeItem<Buffer>[OverlapsBufferInitialCapacity];
|
||||
|
||||
_dirtyCache = new Dictionary<ulong, BufferCacheEntry>();
|
||||
|
||||
|
@ -79,7 +78,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// <param name="e">Event arguments</param>
|
||||
public void MemoryUnmappedHandler(object sender, UnmapEventArgs e)
|
||||
{
|
||||
Buffer[] overlaps = new Buffer[10];
|
||||
RangeItem<Buffer>[] overlaps = new RangeItem<Buffer>[10];
|
||||
int overlapCount;
|
||||
|
||||
MultiRange range = ((MemoryManager)sender).GetPhysicalRegions(e.Address, e.Size);
|
||||
|
@ -90,12 +89,12 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
|
||||
lock (_buffers)
|
||||
{
|
||||
overlapCount = _buffers.FindOverlaps(subRange.Address, subRange.Size, ref overlaps);
|
||||
overlapCount = _buffers.FindOverlaps(subRange.Address, subRange.Size, ref overlaps).Count;
|
||||
}
|
||||
|
||||
for (int i = 0; i < overlapCount; i++)
|
||||
{
|
||||
overlaps[i].Unmapped(subRange.Address, subRange.Size);
|
||||
overlaps[i].Value.Unmapped(subRange.Address, subRange.Size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -495,10 +494,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// <param name="stage">The type of usage that created the buffer</param>
|
||||
private void CreateBufferAligned(ulong address, ulong size, BufferStage stage)
|
||||
{
|
||||
Buffer[] overlaps = _bufferOverlaps;
|
||||
int overlapsCount = _buffers.FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||
_buffers.Lock.EnterWriteLock();
|
||||
RangeItem<Buffer>[] overlaps = _bufferOverlaps;
|
||||
OverlapResult result = _buffers.FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||
|
||||
if (overlapsCount != 0)
|
||||
if (result.Count != 0)
|
||||
{
|
||||
// The buffer already exists. We can just return the existing buffer
|
||||
// if the buffer we need is fully contained inside the overlapping buffer.
|
||||
|
@ -507,7 +507,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
// old buffer(s) to the new buffer.
|
||||
|
||||
ulong endAddress = address + size;
|
||||
Buffer overlap0 = overlaps[0];
|
||||
RangeItem<Buffer> overlap0 = overlaps[0];
|
||||
|
||||
if (overlap0.Address > address || overlap0.EndAddress < endAddress)
|
||||
{
|
||||
|
@ -522,39 +522,36 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
// sequential memory.
|
||||
// Allowing for 2 pages (rather than just one) is necessary to catch cases where the
|
||||
// range crosses a page, and after alignment, ends having a size of 2 pages.
|
||||
if (overlapsCount == 1 &&
|
||||
if (result.Count == 1 &&
|
||||
address >= overlap0.Address &&
|
||||
endAddress - overlap0.EndAddress <= BufferAlignmentSize * 2)
|
||||
{
|
||||
// Try to grow the buffer by 1.5x of its current size.
|
||||
// This improves performance in the cases where the buffer is resized often by small amounts.
|
||||
ulong existingSize = overlap0.Size;
|
||||
ulong existingSize = overlap0.Value.Size;
|
||||
ulong growthSize = (existingSize + Math.Min(existingSize >> 1, MaxDynamicGrowthSize)) & ~BufferAlignmentMask;
|
||||
|
||||
size = Math.Max(size, growthSize);
|
||||
endAddress = address + size;
|
||||
|
||||
overlapsCount = _buffers.FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||
result = _buffers.FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||
}
|
||||
|
||||
_buffers.RemoveRange(result);
|
||||
|
||||
address = Math.Min(address, overlaps[0].Address);
|
||||
endAddress = Math.Max(endAddress, overlaps[^1].EndAddress);
|
||||
|
||||
for (int index = 0; index < overlapsCount; index++)
|
||||
for (int index = 0; index < result.Count; index++)
|
||||
{
|
||||
Buffer buffer = overlaps[index];
|
||||
RangeItem<Buffer> buffer = overlaps[index];
|
||||
|
||||
anySparseCompatible |= buffer.SparseCompatible;
|
||||
|
||||
address = Math.Min(address, buffer.Address);
|
||||
endAddress = Math.Max(endAddress, buffer.EndAddress);
|
||||
|
||||
lock (_buffers)
|
||||
{
|
||||
_buffers.Remove(buffer);
|
||||
}
|
||||
anySparseCompatible |= buffer.Value.SparseCompatible;
|
||||
}
|
||||
|
||||
ulong newSize = endAddress - address;
|
||||
|
||||
CreateBufferAligned(address, newSize, stage, anySparseCompatible, overlaps, overlapsCount);
|
||||
CreateBufferAligned(address, newSize, stage, anySparseCompatible, overlaps, result.Count);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -562,11 +559,10 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
// No overlap, just create a new buffer.
|
||||
Buffer buffer = new(_context, _physicalMemory, address, size, stage, sparseCompatible: false);
|
||||
|
||||
lock (_buffers)
|
||||
{
|
||||
_buffers.Add(buffer);
|
||||
}
|
||||
_buffers.Add(buffer);
|
||||
}
|
||||
|
||||
_buffers.Lock.ExitWriteLock();
|
||||
|
||||
ShrinkOverlapsBufferIfNeeded();
|
||||
}
|
||||
|
@ -582,22 +578,23 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// <param name="alignment">Alignment of the start address of the buffer</param>
|
||||
private void CreateBufferAligned(ulong address, ulong size, BufferStage stage, ulong alignment)
|
||||
{
|
||||
Buffer[] overlaps = _bufferOverlaps;
|
||||
int overlapsCount = _buffers.FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||
_buffers.Lock.EnterWriteLock();
|
||||
RangeItem<Buffer>[] overlaps = _bufferOverlaps;
|
||||
OverlapResult result = _buffers.FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||
bool sparseAligned = alignment >= SparseBufferAlignmentSize;
|
||||
|
||||
if (overlapsCount != 0)
|
||||
if (result.Count != 0)
|
||||
{
|
||||
// If the buffer already exists, make sure if covers the entire range,
|
||||
// and make sure it is properly aligned, otherwise sparse mapping may fail.
|
||||
|
||||
ulong endAddress = address + size;
|
||||
Buffer overlap0 = overlaps[0];
|
||||
RangeItem<Buffer> overlap0 = overlaps[0];
|
||||
|
||||
if (overlap0.Address > address ||
|
||||
overlap0.EndAddress < endAddress ||
|
||||
(overlap0.Address & (alignment - 1)) != 0 ||
|
||||
(!overlap0.SparseCompatible && sparseAligned))
|
||||
(!overlap0.Value.SparseCompatible && sparseAligned))
|
||||
{
|
||||
// We need to make sure the new buffer is properly aligned.
|
||||
// However, after the range is aligned, it is possible that it
|
||||
|
@ -605,35 +602,24 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
// and ensure we cover all overlaps.
|
||||
|
||||
int oldOverlapsCount;
|
||||
endAddress = Math.Max(endAddress, overlaps[^1].EndAddress);
|
||||
|
||||
do
|
||||
{
|
||||
for (int index = 0; index < overlapsCount; index++)
|
||||
{
|
||||
Buffer buffer = overlaps[index];
|
||||
|
||||
address = Math.Min(address, buffer.Address);
|
||||
endAddress = Math.Max(endAddress, buffer.EndAddress);
|
||||
}
|
||||
address = Math.Min(address, overlaps[0].Address);
|
||||
|
||||
address &= ~(alignment - 1);
|
||||
|
||||
oldOverlapsCount = overlapsCount;
|
||||
overlapsCount = _buffers.FindOverlapsNonOverlapping(address, endAddress - address, ref overlaps);
|
||||
oldOverlapsCount = result.Count;
|
||||
result = _buffers.FindOverlapsNonOverlapping(address, endAddress - address, ref overlaps);
|
||||
}
|
||||
while (oldOverlapsCount != overlapsCount);
|
||||
while (oldOverlapsCount != result.Count);
|
||||
|
||||
lock (_buffers)
|
||||
{
|
||||
for (int index = 0; index < overlapsCount; index++)
|
||||
{
|
||||
_buffers.Remove(overlaps[index]);
|
||||
}
|
||||
}
|
||||
_buffers.RemoveRange(result);
|
||||
|
||||
ulong newSize = endAddress - address;
|
||||
|
||||
CreateBufferAligned(address, newSize, stage, sparseAligned, overlaps, overlapsCount);
|
||||
CreateBufferAligned(address, newSize, stage, sparseAligned, overlaps, result.Count);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -641,11 +627,9 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
// No overlap, just create a new buffer.
|
||||
Buffer buffer = new(_context, _physicalMemory, address, size, stage, sparseAligned);
|
||||
|
||||
lock (_buffers)
|
||||
{
|
||||
_buffers.Add(buffer);
|
||||
}
|
||||
_buffers.Add(buffer);
|
||||
}
|
||||
_buffers.Lock.ExitWriteLock();
|
||||
|
||||
ShrinkOverlapsBufferIfNeeded();
|
||||
}
|
||||
|
@ -661,18 +645,15 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// <param name="sparseCompatible">Indicates if the buffer can be used in a sparse buffer mapping</param>
|
||||
/// <param name="overlaps">Buffers overlapping the range</param>
|
||||
/// <param name="overlapsCount">Total of overlaps</param>
|
||||
private void CreateBufferAligned(ulong address, ulong size, BufferStage stage, bool sparseCompatible, Buffer[] overlaps, int overlapsCount)
|
||||
private void CreateBufferAligned(ulong address, ulong size, BufferStage stage, bool sparseCompatible, RangeItem<Buffer>[] overlaps, int overlapsCount)
|
||||
{
|
||||
Buffer newBuffer = new(_context, _physicalMemory, address, size, stage, sparseCompatible, overlaps.Take(overlapsCount));
|
||||
Buffer newBuffer = new(_context, _physicalMemory, address, size, stage, sparseCompatible, overlaps);
|
||||
|
||||
lock (_buffers)
|
||||
{
|
||||
_buffers.Add(newBuffer);
|
||||
}
|
||||
_buffers.Add(newBuffer);
|
||||
|
||||
for (int index = 0; index < overlapsCount; index++)
|
||||
{
|
||||
Buffer buffer = overlaps[index];
|
||||
Buffer buffer = overlaps[index].Value;
|
||||
|
||||
int dstOffset = (int)(buffer.Address - newBuffer.Address);
|
||||
|
||||
|
|
|
@ -761,7 +761,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
|
||||
if (!bounds.IsUnmapped)
|
||||
{
|
||||
bool isWrite = bounds.Flags.HasFlag(BufferUsageFlags.Write);
|
||||
bool isWrite = (bounds.Flags & BufferUsageFlags.Write) == BufferUsageFlags.Write;
|
||||
BufferRange range = isStorage
|
||||
? bufferCache.GetBufferRangeAligned(bounds.Range, bufferStage | BufferStageUtils.FromUsage(bounds.Flags), isWrite)
|
||||
: bufferCache.GetBufferRange(bounds.Range, bufferStage);
|
||||
|
@ -798,7 +798,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
|
||||
if (!bounds.IsUnmapped)
|
||||
{
|
||||
bool isWrite = bounds.Flags.HasFlag(BufferUsageFlags.Write);
|
||||
bool isWrite = (bounds.Flags & BufferUsageFlags.Write) == BufferUsageFlags.Write;
|
||||
BufferRange range = isStorage
|
||||
? bufferCache.GetBufferRangeAligned(bounds.Range, BufferStageUtils.ComputeStorage(bounds.Flags), isWrite)
|
||||
: bufferCache.GetBufferRange(bounds.Range, BufferStage.Compute);
|
||||
|
|
|
@ -14,12 +14,12 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// <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.
|
||||
|
@ -77,8 +77,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 +84,10 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
{
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return Count > 0;
|
||||
}
|
||||
Lock.EnterReadLock();
|
||||
bool result = Count > 0;
|
||||
Lock.ExitReadLock();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,33 +112,39 @@ 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);
|
||||
FindOverlapsNonOverlappingAsSpan(address, size, out ReadOnlySpan<RangeItem<BufferModifiedRange>> overlaps);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
for (int i = 0; i < overlaps.Length; i++)
|
||||
{
|
||||
BufferModifiedRange overlap = overlaps[i].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;
|
||||
}
|
||||
|
||||
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 +156,102 @@ 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();
|
||||
OverlapResult result = FindOverlapsNonOverlappingAsSpan(address, size,
|
||||
out ReadOnlySpan<RangeItem<BufferModifiedRange>> overlaps);
|
||||
|
||||
ulong endAddress = address + size;
|
||||
ulong syncNumber = _context.SyncNumber;
|
||||
|
||||
if (overlaps.Length == 0)
|
||||
{
|
||||
// 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 (overlaps.Length == 1)
|
||||
{
|
||||
if (overlaps[0].Address == address && overlaps[0].EndAddress == endAddress)
|
||||
{
|
||||
// The overlaps must be removed or split.
|
||||
overlaps[0].Value.SyncNumber = syncNumber;
|
||||
overlaps[0].Value.Parent = this;
|
||||
Lock.ExitWriteLock();
|
||||
return;
|
||||
}
|
||||
|
||||
BufferModifiedRange overlap = overlaps[i];
|
||||
if (overlaps[0].Address < address)
|
||||
{
|
||||
overlaps[0].Value.Size = address - overlaps[0].Address;
|
||||
|
||||
if (overlap.Address == address && overlap.Size == size)
|
||||
extendsPre = true;
|
||||
|
||||
if (overlaps[0].EndAddress > endAddress)
|
||||
{
|
||||
// Region already exists. Just update the existing sync number.
|
||||
overlap.SyncNumber = syncNumber;
|
||||
overlap.Parent = this;
|
||||
|
||||
return;
|
||||
buffPost = new BufferModifiedRange(endAddress, overlaps[0].EndAddress - endAddress,
|
||||
overlaps[0].Value.SyncNumber, overlaps[0].Value.Parent);
|
||||
extendsPost = true;
|
||||
}
|
||||
|
||||
Remove(overlap);
|
||||
|
||||
if (overlap.Address < address && overlap.EndAddress > address)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (overlaps[0].EndAddress > endAddress)
|
||||
{
|
||||
// A split item must be created behind this overlap.
|
||||
|
||||
Add(new BufferModifiedRange(overlap.Address, address - overlap.Address, overlap.SyncNumber, overlap.Parent));
|
||||
overlaps[0].Value.Size = overlaps[0].EndAddress - endAddress;
|
||||
overlaps[0].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));
|
||||
RemoveAt(result.StartIndex);
|
||||
}
|
||||
}
|
||||
|
||||
if (extendsPre && extendsPost)
|
||||
{
|
||||
Add(buffPost);
|
||||
}
|
||||
|
||||
Add(new BufferModifiedRange(address, size, syncNumber, this));
|
||||
Lock.ExitWriteLock();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
BufferModifiedRange buffPre = null;
|
||||
|
||||
if (overlaps[0].Address < address)
|
||||
{
|
||||
buffPre = new BufferModifiedRange(overlaps[0].Address, address - overlaps[0].Address,
|
||||
overlaps[0].Value.SyncNumber, overlaps[0].Value.Parent);
|
||||
extendsPre = true;
|
||||
}
|
||||
|
||||
if (overlaps[^1].EndAddress > endAddress)
|
||||
{
|
||||
buffPost = new BufferModifiedRange(endAddress, overlaps[^1].EndAddress - endAddress,
|
||||
overlaps[^1].Value.SyncNumber, overlaps[^1].Value.Parent);
|
||||
extendsPost = true;
|
||||
}
|
||||
|
||||
RemoveRange(result);
|
||||
|
||||
if (extendsPre)
|
||||
{
|
||||
Add(buffPre);
|
||||
}
|
||||
|
||||
if (extendsPost)
|
||||
{
|
||||
Add(buffPost);
|
||||
}
|
||||
|
||||
Add(new BufferModifiedRange(address, size, syncNumber, this));
|
||||
Lock.ExitWriteLock();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -208,25 +263,20 @@ 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();
|
||||
FindOverlapsNonOverlappingAsSpan(address, size, out ReadOnlySpan<RangeItem<BufferModifiedRange>> overlaps);
|
||||
|
||||
ref BufferModifiedRange[] overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
|
||||
|
||||
// Range list must be consistent for this operation.
|
||||
lock (_lock)
|
||||
for (int i = 0; i < overlaps.Length; i++)
|
||||
{
|
||||
count = FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
BufferModifiedRange overlap = overlaps[i];
|
||||
BufferModifiedRange overlap = overlaps[i].Value;
|
||||
|
||||
if (overlap.SyncNumber == syncNumber)
|
||||
{
|
||||
rangeAction(overlap.Address, overlap.Size);
|
||||
}
|
||||
}
|
||||
|
||||
Lock.ExitReadLock();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -237,19 +287,15 @@ 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;
|
||||
RangeItem<BufferModifiedRange>[] overlaps = new RangeItem<BufferModifiedRange>[1];
|
||||
|
||||
Lock.EnterReadLock();
|
||||
OverlapResult result = FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||
Lock.ExitReadLock();
|
||||
|
||||
ref BufferModifiedRange[] overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
|
||||
|
||||
// Range list must be consistent for this operation.
|
||||
lock (_lock)
|
||||
for (int i = 0; i < result.Count; i++)
|
||||
{
|
||||
count = FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
BufferModifiedRange overlap = overlaps[i];
|
||||
BufferModifiedRange overlap = overlaps[i].Value;
|
||||
rangeAction(overlap.Address, overlap.Size);
|
||||
}
|
||||
}
|
||||
|
@ -262,11 +308,10 @@ 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();
|
||||
bool result = FindOverlapsNonOverlapping(address, size).Count > 0;
|
||||
Lock.ExitReadLock();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -298,38 +343,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.
|
||||
|
@ -353,22 +397,21 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
ulong endAddress = address + size;
|
||||
ulong currentSync = _context.SyncNumber;
|
||||
|
||||
int rangeCount = 0;
|
||||
int rangeCount;
|
||||
|
||||
ref BufferModifiedRange[] overlaps = ref ThreadStaticArray<BufferModifiedRange>.Get();
|
||||
ref RangeItem<BufferModifiedRange>[] overlaps = ref ThreadStaticArray<RangeItem<BufferModifiedRange>>.Get();
|
||||
|
||||
// Range list must be consistent for this operation
|
||||
lock (_lock)
|
||||
Lock.EnterReadLock();
|
||||
if (_migrationTarget != null)
|
||||
{
|
||||
if (_migrationTarget != null)
|
||||
{
|
||||
rangeCount = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
rangeCount = FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||
}
|
||||
rangeCount = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
rangeCount = FindOverlapsNonOverlapping(address, size, ref overlaps).Count;
|
||||
}
|
||||
Lock.ExitReadLock();
|
||||
|
||||
if (rangeCount == -1)
|
||||
{
|
||||
|
@ -376,7 +419,8 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
|
||||
return;
|
||||
}
|
||||
else if (rangeCount == 0)
|
||||
|
||||
if (rangeCount == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
@ -388,7 +432,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);
|
||||
|
||||
|
@ -419,42 +463,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 +514,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 +534,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,22 +567,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();
|
||||
OverlapResult result = FindOverlapsNonOverlappingAsSpan(address, size, out ReadOnlySpan<RangeItem<BufferModifiedRange>> overlaps);
|
||||
|
||||
if (overlaps.Length == 0)
|
||||
{
|
||||
// This function can be called from any thread, so it cannot use the arrays for background or foreground.
|
||||
BufferModifiedRange[] toClear = new BufferModifiedRange[1];
|
||||
|
||||
int rangeCount = FindOverlapsNonOverlapping(address, size, ref toClear);
|
||||
|
||||
ulong endAddress = address + size;
|
||||
|
||||
for (int i = 0; i < rangeCount; i++)
|
||||
{
|
||||
BufferModifiedRange overlap = toClear[i];
|
||||
|
||||
ClearPart(overlap, address, endAddress);
|
||||
}
|
||||
Lock.ExitWriteLock();
|
||||
return;
|
||||
}
|
||||
|
||||
BufferModifiedRange buffPost = null;
|
||||
bool extendsPost = false;
|
||||
bool extendsPre = false;
|
||||
|
||||
if (overlaps.Length == 1)
|
||||
{
|
||||
if (overlaps[0].Address < address)
|
||||
{
|
||||
overlaps[0].Value.Size = address - overlaps[0].Address;
|
||||
extendsPre = true;
|
||||
|
||||
if (overlaps[0].EndAddress > endAddress)
|
||||
{
|
||||
buffPost = new BufferModifiedRange(endAddress, overlaps[0].EndAddress - endAddress,
|
||||
overlaps[0].Value.SyncNumber, overlaps[0].Value.Parent);
|
||||
extendsPost = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (overlaps[^1].EndAddress > endAddress)
|
||||
{
|
||||
overlaps[0].Value.Size = overlaps[0].EndAddress - endAddress;
|
||||
overlaps[0].Value.Address = endAddress;
|
||||
}
|
||||
else
|
||||
{
|
||||
RemoveAt(result.StartIndex);
|
||||
}
|
||||
}
|
||||
|
||||
if (extendsPre && extendsPost)
|
||||
{
|
||||
Add(buffPost);
|
||||
}
|
||||
|
||||
Lock.ExitWriteLock();
|
||||
return;
|
||||
}
|
||||
|
||||
BufferModifiedRange buffPre = null;
|
||||
|
||||
if (overlaps[0].Address < address)
|
||||
{
|
||||
buffPre = new BufferModifiedRange(overlaps[0].Address, address - overlaps[0].Address,
|
||||
overlaps[0].Value.SyncNumber, overlaps[0].Value.Parent);
|
||||
extendsPre = true;
|
||||
}
|
||||
|
||||
if (overlaps[^1].EndAddress > endAddress)
|
||||
{
|
||||
buffPost = new BufferModifiedRange(endAddress, overlaps[^1].EndAddress - endAddress,
|
||||
overlaps[^1].Value.SyncNumber, overlaps[^1].Value.Parent);
|
||||
extendsPost = true;
|
||||
}
|
||||
|
||||
RemoveRange(result);
|
||||
|
||||
if (extendsPre)
|
||||
{
|
||||
Add(buffPre);
|
||||
}
|
||||
|
||||
if (extendsPost)
|
||||
{
|
||||
Add(buffPost);
|
||||
}
|
||||
|
||||
Lock.ExitWriteLock();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -549,10 +653,9 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
Count = 0;
|
||||
}
|
||||
Lock.EnterWriteLock();
|
||||
Count = 0;
|
||||
Lock.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -690,11 +690,8 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
if (_pageTable[l0] == null)
|
||||
{
|
||||
_pageTable[l0] = new ulong[PtLvl1Size];
|
||||
|
||||
for (ulong index = 0; index < PtLvl1Size; index++)
|
||||
{
|
||||
_pageTable[l0][index] = PteUnmapped;
|
||||
}
|
||||
|
||||
Array.Fill(_pageTable[l0], PteUnmapped);
|
||||
}
|
||||
|
||||
_pageTable[l0][l1] = pte;
|
||||
|
|
|
@ -63,7 +63,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
}
|
||||
|
||||
private readonly RangeList<VirtualRange> _virtualRanges;
|
||||
private VirtualRange[] _virtualRangeOverlaps;
|
||||
private RangeItem<VirtualRange>[] _virtualRangeOverlaps;
|
||||
private readonly ConcurrentQueue<VirtualRange> _deferredUnmaps;
|
||||
private int _hasDeferredUnmaps;
|
||||
|
||||
|
@ -75,7 +75,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
{
|
||||
_memoryManager = memoryManager;
|
||||
_virtualRanges = [];
|
||||
_virtualRangeOverlaps = new VirtualRange[BufferCache.OverlapsBufferInitialCapacity];
|
||||
_virtualRangeOverlaps = new RangeItem<VirtualRange>[BufferCache.OverlapsBufferInitialCapacity];
|
||||
_deferredUnmaps = new ConcurrentQueue<VirtualRange>();
|
||||
}
|
||||
|
||||
|
@ -106,18 +106,17 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
/// <returns>True if the range already existed, false if a new one was created and added</returns>
|
||||
public bool TryGetOrAddRange(ulong gpuVa, ulong size, out MultiRange range)
|
||||
{
|
||||
VirtualRange[] overlaps = _virtualRangeOverlaps;
|
||||
int overlapsCount;
|
||||
|
||||
OverlapResult result;
|
||||
|
||||
if (Interlocked.Exchange(ref _hasDeferredUnmaps, 0) != 0)
|
||||
{
|
||||
while (_deferredUnmaps.TryDequeue(out VirtualRange unmappedRange))
|
||||
{
|
||||
overlapsCount = _virtualRanges.FindOverlapsNonOverlapping(unmappedRange.Address, unmappedRange.Size, ref overlaps);
|
||||
result = _virtualRanges.FindOverlapsNonOverlapping(unmappedRange.Address, unmappedRange.Size);
|
||||
|
||||
for (int index = 0; index < overlapsCount; index++)
|
||||
if (result.StartIndex >= 0)
|
||||
{
|
||||
_virtualRanges.Remove(overlaps[index]);
|
||||
_virtualRanges.RemoveRange(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -126,27 +125,23 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
|
||||
ulong originalVa = gpuVa;
|
||||
|
||||
overlapsCount = _virtualRanges.FindOverlapsNonOverlapping(gpuVa, size, ref overlaps);
|
||||
|
||||
if (overlapsCount != 0)
|
||||
_virtualRanges.Lock.EnterWriteLock();
|
||||
result = _virtualRanges.FindOverlapsNonOverlappingAsSpan(gpuVa, size, out ReadOnlySpan<RangeItem<VirtualRange>> overlaps);
|
||||
|
||||
if (overlaps.Length != 0)
|
||||
{
|
||||
// The virtual range already exists. We just need to check if our range fits inside
|
||||
// the existing one, and if not, we must extend the existing one.
|
||||
|
||||
ulong endAddress = gpuVa + size;
|
||||
VirtualRange overlap0 = overlaps[0];
|
||||
VirtualRange overlap0 = overlaps[0].Value;
|
||||
|
||||
if (overlap0.Address > gpuVa || overlap0.EndAddress < endAddress)
|
||||
{
|
||||
for (int index = 0; index < overlapsCount; index++)
|
||||
{
|
||||
VirtualRange virtualRange = overlaps[index];
|
||||
|
||||
gpuVa = Math.Min(gpuVa, virtualRange.Address);
|
||||
endAddress = Math.Max(endAddress, virtualRange.EndAddress);
|
||||
|
||||
_virtualRanges.Remove(virtualRange);
|
||||
}
|
||||
gpuVa = Math.Min(gpuVa, overlaps[0].Address);
|
||||
endAddress = Math.Max(endAddress, overlaps[^1].EndAddress);
|
||||
|
||||
_virtualRanges.RemoveRange(result);
|
||||
|
||||
ulong newSize = endAddress - gpuVa;
|
||||
MultiRange newRange = _memoryManager.GetPhysicalRegions(gpuVa, newSize);
|
||||
|
@ -170,6 +165,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
|||
|
||||
_virtualRanges.Add(virtualRange);
|
||||
}
|
||||
_virtualRanges.Lock.ExitWriteLock();
|
||||
|
||||
ShrinkOverlapsBufferIfNeeded();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue