2018-06-08 17:15:56 -07:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
namespace Ryujinx.HLE.Gpu.Memory
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
|
|
|
class NvGpuVmmCache
|
|
|
|
{
|
|
|
|
private const int MaxCpCount = 10000;
|
|
|
|
private const int MaxCpTimeDelta = 60000;
|
|
|
|
|
|
|
|
private class CachedPage
|
|
|
|
{
|
2018-07-08 12:55:15 -07:00
|
|
|
private struct Range
|
|
|
|
{
|
|
|
|
public long Start;
|
|
|
|
public long End;
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
public Range(long Start, long End)
|
|
|
|
{
|
|
|
|
this.Start = Start;
|
|
|
|
this.End = End;
|
|
|
|
}
|
|
|
|
}
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
private List<Range>[] Regions;
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
public LinkedListNode<long> Node { get; set; }
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
public int Timestamp { get; private set; }
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
public CachedPage()
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
2018-07-08 12:55:15 -07:00
|
|
|
Regions = new List<Range>[(int)NvGpuBufferType.Count];
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
for (int Index = 0; Index < Regions.Length; Index++)
|
|
|
|
{
|
|
|
|
Regions[Index] = new List<Range>();
|
|
|
|
}
|
2018-06-08 17:15:56 -07:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
public bool AddRange(long Start, long End, NvGpuBufferType BufferType)
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
2018-07-08 12:55:15 -07:00
|
|
|
List<Range> BtRegions = Regions[(int)BufferType];
|
|
|
|
|
|
|
|
for (int Index = 0; Index < BtRegions.Count; Index++)
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
2018-07-08 12:55:15 -07:00
|
|
|
Range Rg = BtRegions[Index];
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
if (Start >= Rg.Start && End <= Rg.End)
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
if (Start <= Rg.End && Rg.Start <= End)
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
2018-07-08 12:55:15 -07:00
|
|
|
long MinStart = Math.Min(Rg.Start, Start);
|
|
|
|
long MaxEnd = Math.Max(Rg.End, End);
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
BtRegions[Index] = new Range(MinStart, MaxEnd);
|
2018-06-08 17:15:56 -07:00
|
|
|
|
|
|
|
Timestamp = Environment.TickCount;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
BtRegions.Add(new Range(Start, End));
|
2018-06-08 17:15:56 -07:00
|
|
|
|
|
|
|
Timestamp = Environment.TickCount;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-08 12:55:15 -07:00
|
|
|
|
|
|
|
public int GetTotalCount()
|
|
|
|
{
|
|
|
|
int Count = 0;
|
|
|
|
|
|
|
|
for (int Index = 0; Index < Regions.Length; Index++)
|
|
|
|
{
|
|
|
|
Count += Regions[Index].Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Count;
|
|
|
|
}
|
2018-06-08 17:15:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private Dictionary<long, CachedPage> Cache;
|
|
|
|
|
|
|
|
private LinkedList<long> SortedCache;
|
|
|
|
|
|
|
|
private int CpCount;
|
|
|
|
|
|
|
|
public NvGpuVmmCache()
|
|
|
|
{
|
|
|
|
Cache = new Dictionary<long, CachedPage>();
|
|
|
|
|
|
|
|
SortedCache = new LinkedList<long>();
|
|
|
|
}
|
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
public bool IsRegionModified(AMemory Memory, NvGpuBufferType BufferType, long PA, long Size)
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
2018-07-08 12:55:15 -07:00
|
|
|
bool[] Modified = Memory.IsRegionModified(PA, Size);
|
|
|
|
|
|
|
|
if (Modified == null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-08 17:15:56 -07:00
|
|
|
ClearCachedPagesIfNeeded();
|
|
|
|
|
|
|
|
long PageSize = Memory.GetHostPageSize();
|
|
|
|
|
|
|
|
long Mask = PageSize - 1;
|
|
|
|
|
|
|
|
long PAEnd = PA + Size;
|
|
|
|
|
|
|
|
bool RegMod = false;
|
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
int Index = 0;
|
|
|
|
|
|
|
|
while (PA < PAEnd)
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
2018-07-08 12:55:15 -07:00
|
|
|
long Key = PA & ~Mask;
|
2018-06-08 17:15:56 -07:00
|
|
|
|
|
|
|
long PAPgEnd = Math.Min((PA + PageSize) & ~Mask, PAEnd);
|
|
|
|
|
|
|
|
bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp);
|
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
if (IsCached)
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
2018-07-08 12:55:15 -07:00
|
|
|
CpCount -= Cp.GetTotalCount();
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
SortedCache.Remove(Cp.Node);
|
2018-06-08 17:15:56 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-08 12:55:15 -07:00
|
|
|
Cp = new CachedPage();
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
Cache.Add(Key, Cp);
|
2018-06-08 17:15:56 -07:00
|
|
|
}
|
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
if (Modified[Index++] && IsCached)
|
2018-06-08 17:15:56 -07:00
|
|
|
{
|
2018-07-08 12:55:15 -07:00
|
|
|
Cp = new CachedPage();
|
2018-06-08 17:15:56 -07:00
|
|
|
|
|
|
|
Cache[Key] = Cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cp.Node = SortedCache.AddLast(Key);
|
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
RegMod |= Cp.AddRange(PA, PAPgEnd, BufferType);
|
2018-06-08 17:15:56 -07:00
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
CpCount += Cp.GetTotalCount();
|
2018-06-08 17:15:56 -07:00
|
|
|
|
|
|
|
PA = PAPgEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RegMod;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ClearCachedPagesIfNeeded()
|
|
|
|
{
|
|
|
|
if (CpCount <= MaxCpCount)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Timestamp = Environment.TickCount;
|
|
|
|
|
|
|
|
int TimeDelta;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!TryPopOldestCachedPageKey(Timestamp, out long Key))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedPage Cp = Cache[Key];
|
|
|
|
|
|
|
|
Cache.Remove(Key);
|
|
|
|
|
2018-07-08 12:55:15 -07:00
|
|
|
CpCount -= Cp.GetTotalCount();
|
2018-06-08 17:15:56 -07:00
|
|
|
|
|
|
|
TimeDelta = RingDelta(Cp.Timestamp, Timestamp);
|
|
|
|
}
|
|
|
|
while (CpCount > (MaxCpCount >> 1) || (uint)TimeDelta > (uint)MaxCpTimeDelta);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool TryPopOldestCachedPageKey(int Timestamp, out long Key)
|
|
|
|
{
|
|
|
|
LinkedListNode<long> Node = SortedCache.First;
|
|
|
|
|
|
|
|
if (Node == null)
|
|
|
|
{
|
|
|
|
Key = 0;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SortedCache.Remove(Node);
|
|
|
|
|
|
|
|
Key = Node.Value;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int RingDelta(int Old, int New)
|
|
|
|
{
|
|
|
|
if ((uint)New < (uint)Old)
|
|
|
|
{
|
|
|
|
return New + (~Old + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return New - Old;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|