Cache changes (#302)

* Skip repeated cache tests between same sync

* Skip some checks for regions where just one resource is resident

* Dehardcode residency page size

* Some cleanup
This commit is contained in:
ReinUsesLisp 2018-07-29 01:39:15 -03:00 committed by gdkchan
parent fdda67d476
commit 3208173620
3 changed files with 127 additions and 10 deletions

View file

@ -19,12 +19,14 @@ namespace Ryujinx.HLE.Gpu.Memory
public Range(long Start, long End)
{
this.Start = Start;
this.End = End;
this.End = End;
}
}
private List<Range>[] Regions;
private HashSet<long> ResidencyKeys;
public LinkedListNode<long> Node { get; set; }
public int Timestamp { get; private set; }
@ -37,6 +39,27 @@ namespace Ryujinx.HLE.Gpu.Memory
{
Regions[Index] = new List<Range>();
}
ResidencyKeys = new HashSet<long>();
}
public void AddResidency(long Key)
{
ResidencyKeys.Add(Key);
}
public void RemoveResidency(HashSet<long>[] Residency, long PageSize)
{
for (int i = 0; i < (int)NvGpuBufferType.Count; i++)
{
foreach (Range Region in Regions[i])
{
foreach (long Key in ResidencyKeys)
{
Residency[Region.Start / PageSize].Remove(Key);
}
}
}
}
public bool AddRange(long Start, long End, NvGpuBufferType BufferType)
@ -89,6 +112,10 @@ namespace Ryujinx.HLE.Gpu.Memory
private LinkedList<long> SortedCache;
private HashSet<long>[] Residency;
private long ResidencyPageSize;
private int CpCount;
public NvGpuVmmCache()
@ -100,7 +127,7 @@ namespace Ryujinx.HLE.Gpu.Memory
public bool IsRegionModified(AMemory Memory, NvGpuBufferType BufferType, long PA, long Size)
{
bool[] Modified = Memory.IsRegionModified(PA, Size);
(bool[] Modified, long ModifiedCount) = Memory.IsRegionModified(PA, Size);
if (Modified == null)
{
@ -111,8 +138,19 @@ namespace Ryujinx.HLE.Gpu.Memory
long PageSize = Memory.GetHostPageSize();
EnsureResidencyInitialized(PageSize);
bool HasResidents = AddResidency(PA, Size);
if (!HasResidents && ModifiedCount == 0)
{
return false;
}
long Mask = PageSize - 1;
long ResidencyKey = PA;
long PAEnd = PA + Size;
bool RegMod = false;
@ -147,6 +185,8 @@ namespace Ryujinx.HLE.Gpu.Memory
Cache[Key] = Cp;
}
Cp.AddResidency(ResidencyKey);
Cp.Node = SortedCache.AddLast(Key);
RegMod |= Cp.AddRange(PA, PAPgEnd, BufferType);
@ -159,6 +199,53 @@ namespace Ryujinx.HLE.Gpu.Memory
return RegMod;
}
private bool AddResidency(long PA, long Size)
{
long PageSize = ResidencyPageSize;
long Mask = PageSize - 1;
long Key = PA;
bool ResidentFound = false;
for (long Cursor = PA & ~Mask; Cursor < ((PA + Size + PageSize - 1) & ~Mask); Cursor += PageSize)
{
long PageIndex = Cursor / PageSize;
Residency[PageIndex].Add(Key);
if (Residency[PageIndex].Count > 1)
{
ResidentFound = true;
}
}
return ResidentFound;
}
private void EnsureResidencyInitialized(long PageSize)
{
if (Residency == null)
{
Residency = new HashSet<long>[AMemoryMgr.RamSize / PageSize];
for (int i = 0; i < Residency.Length; i++)
{
Residency[i] = new HashSet<long>();
}
ResidencyPageSize = PageSize;
}
else
{
if (ResidencyPageSize != PageSize)
{
throw new InvalidOperationException("Tried to change residency page size");
}
}
}
private void ClearCachedPagesIfNeeded()
{
if (CpCount <= MaxCpCount)
@ -179,6 +266,8 @@ namespace Ryujinx.HLE.Gpu.Memory
CachedPage Cp = Cache[Key];
Cp.RemoveResidency(Residency, ResidencyPageSize);
Cache.Remove(Key);
CpCount -= Cp.GetTotalCount();