Allow "reinterpretation" of framebuffer/zeta formats (#418)

* (Re)Implement format reinterpretation, other changes

* Implement writeback to guest memory, some refactoring

* More refactoring, implement reinterpretation the old way again

* Clean up

* Some fixes on M2MF (old Dma engine), added partial support for P2MF, fix conditional ssy, add Z24S8 zeta format, other fixes

* nit: Formatting

* Address PR feedback
This commit is contained in:
gdkchan 2018-09-18 01:30:35 -03:00 committed by GitHub
parent bec95cacc1
commit d4187aaa9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
52 changed files with 1594 additions and 1979 deletions

View file

@ -1,309 +1,82 @@
using ChocolArm64.Memory;
using System;
using System.Collections.Generic;
namespace Ryujinx.Graphics.Memory
{
class NvGpuVmmCache
{
private const long RamSize = 4L * 1024 * 1024 * 1024;
private const int MaxCpCount = 10000;
private const int MaxCpTimeDelta = 60000;
private class CachedPage
{
private struct Range
{
public long Start;
public long End;
public Range(long Start, long End)
{
this.Start = Start;
this.End = End;
}
}
private List<Range>[] Regions;
private HashSet<long> ResidencyKeys;
public LinkedListNode<long> Node { get; set; }
public int Timestamp { get; private set; }
public CachedPage()
{
Regions = new List<Range>[(int)NvGpuBufferType.Count];
for (int Index = 0; Index < Regions.Length; Index++)
{
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)
{
List<Range> BtRegions = Regions[(int)BufferType];
for (int Index = 0; Index < BtRegions.Count; Index++)
{
Range Rg = BtRegions[Index];
if (Start >= Rg.Start && End <= Rg.End)
{
return false;
}
if (Start <= Rg.End && Rg.Start <= End)
{
long MinStart = Math.Min(Rg.Start, Start);
long MaxEnd = Math.Max(Rg.End, End);
BtRegions[Index] = new Range(MinStart, MaxEnd);
Timestamp = Environment.TickCount;
return true;
}
}
BtRegions.Add(new Range(Start, End));
Timestamp = Environment.TickCount;
return true;
}
public int GetTotalCount()
{
int Count = 0;
for (int Index = 0; Index < Regions.Length; Index++)
{
Count += Regions[Index].Count;
}
return Count;
}
}
private Dictionary<long, CachedPage> Cache;
private LinkedList<long> SortedCache;
private HashSet<long>[] Residency;
private long ResidencyPageSize;
private int CpCount;
private ValueRangeSet<int> CachedRanges;
public NvGpuVmmCache()
{
Cache = new Dictionary<long, CachedPage>();
SortedCache = new LinkedList<long>();
CachedRanges = new ValueRangeSet<int>();
}
public bool IsRegionModified(AMemory Memory, NvGpuBufferType BufferType, long PA, long Size)
{
(bool[] Modified, long ModifiedCount) = Memory.IsRegionModified(PA, Size);
PA = Memory.GetPhysicalAddress(PA);
ClearCachedPagesIfNeeded();
long PageSize = AMemory.PageSize;
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;
//Remove all modified ranges.
int Index = 0;
while (PA < PAEnd)
long Position = PA & ~NvGpuVmm.PageMask;
while (ModifiedCount > 0)
{
long Key = PA & ~AMemory.PageMask;
long PAPgEnd = Math.Min((PA + AMemory.PageSize) & ~AMemory.PageMask, PAEnd);
bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp);
if (IsCached)
if (Modified[Index++])
{
CpCount -= Cp.GetTotalCount();
CachedRanges.Remove(new ValueRange<int>(Position, Position + NvGpuVmm.PageSize));
SortedCache.Remove(Cp.Node);
ModifiedCount--;
}
Position += NvGpuVmm.PageSize;
}
//Mask has the bit set for the current resource type.
//If the region is not yet present on the list, then a new ValueRange
//is directly added with the current resource type as the only bit set.
//Otherwise, it just sets the bit for this new resource type on the current mask.
int Mask = 1 << (int)BufferType;
ValueRange<int> NewCached = new ValueRange<int>(PA, PA + Size);
ValueRange<int>[] Ranges = CachedRanges.GetAllIntersections(NewCached);
long LastEnd = NewCached.Start;
long Coverage = 0;
for (Index = 0; Index < Ranges.Length; Index++)
{
ValueRange<int> Current = Ranges[Index];
long RgStart = Math.Max(Current.Start, NewCached.Start);
long RgEnd = Math.Min(Current.End, NewCached.End);
if ((Current.Value & Mask) == 0)
{
CachedRanges.Add(new ValueRange<int>(RgStart, RgEnd, Current.Value | Mask));
}
else
{
Cp = new CachedPage();
Cache.Add(Key, Cp);
Coverage += RgEnd - RgStart;
}
if (Modified[Index++] && IsCached)
if (RgStart > LastEnd)
{
Cp = new CachedPage();
Cache[Key] = Cp;
CachedRanges.Add(new ValueRange<int>(LastEnd, RgStart, Mask));
}
Cp.AddResidency(ResidencyKey);
Cp.Node = SortedCache.AddLast(Key);
RegMod |= Cp.AddRange(PA, PAPgEnd, BufferType);
CpCount += Cp.GetTotalCount();
PA = PAPgEnd;
LastEnd = RgEnd;
}
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)
if (LastEnd < NewCached.End)
{
long PageIndex = Cursor / PageSize;
Residency[PageIndex].Add(Key);
if (Residency[PageIndex].Count > 1)
{
ResidentFound = true;
}
CachedRanges.Add(new ValueRange<int>(LastEnd, NewCached.End, Mask));
}
return ResidentFound;
}
private void EnsureResidencyInitialized(long PageSize)
{
if (Residency == null)
{
Residency = new HashSet<long>[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)
{
return;
}
int Timestamp = Environment.TickCount;
int TimeDelta;
do
{
if (!TryPopOldestCachedPageKey(Timestamp, out long Key))
{
break;
}
CachedPage Cp = Cache[Key];
Cp.RemoveResidency(Residency, ResidencyPageSize);
Cache.Remove(Key);
CpCount -= Cp.GetTotalCount();
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;
}
return Coverage != Size;
}
}
}