Allow multiple buffer types to share the same page, aways use the physical address as cache key

This commit is contained in:
gdkchan 2018-07-06 13:26:55 -03:00
commit cbf7bd07f6
4 changed files with 73 additions and 64 deletions

View file

@ -312,19 +312,17 @@ namespace Ryujinx.HLE.Gpu.Engines
GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition); GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition);
long TextureAddress = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff; long Key = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff;
long Key = TextureAddress; Key = Vmm.GetPhysicalAddress(Key);
TextureAddress = Vmm.GetPhysicalAddress(TextureAddress); if (IsFrameBufferPosition(Key))
if (IsFrameBufferPosition(TextureAddress))
{ {
//This texture is a frame buffer texture, //This texture is a frame buffer texture,
//we shouldn't read anything from memory and bind //we shouldn't read anything from memory and bind
//the frame buffer texture instead, since we're not //the frame buffer texture instead, since we're not
//really writing anything to memory. //really writing anything to memory.
Gpu.Renderer.FrameBuffer.BindTexture(TextureAddress, TexIndex); Gpu.Renderer.FrameBuffer.BindTexture(Key, TexIndex);
} }
else else
{ {
@ -392,6 +390,8 @@ namespace Ryujinx.HLE.Gpu.Engines
{ {
long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
long IboKey = Vmm.GetPhysicalAddress(IndexPosition);
int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst); int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
@ -409,16 +409,16 @@ namespace Ryujinx.HLE.Gpu.Engines
{ {
int IbSize = IndexCount * IndexEntrySize; int IbSize = IndexCount * IndexEntrySize;
bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IndexPosition, (uint)IbSize); bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IboKey, (uint)IbSize);
if (!IboCached || Vmm.IsRegionModified(IndexPosition, (uint)IbSize, NvGpuBufferType.Index)) if (!IboCached || Vmm.IsRegionModified(IboKey, (uint)IbSize, NvGpuBufferType.Index))
{ {
byte[] Data = Vmm.ReadBytes(IndexPosition, (uint)IbSize); byte[] Data = Vmm.ReadBytes(IndexPosition, (uint)IbSize);
Gpu.Renderer.Rasterizer.CreateIbo(IndexPosition, Data); Gpu.Renderer.Rasterizer.CreateIbo(IboKey, Data);
} }
Gpu.Renderer.Rasterizer.SetIndexArray(IndexPosition, IbSize, IndexFormat); Gpu.Renderer.Rasterizer.SetIndexArray(IboKey, IbSize, IndexFormat);
} }
List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32]; List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32];
@ -467,20 +467,22 @@ namespace Ryujinx.HLE.Gpu.Engines
continue; continue;
} }
long VboKey = Vmm.GetPhysicalAddress(VertexPosition);
int Stride = Control & 0xfff; int Stride = Control & 0xfff;
long VbSize = (VertexEndPos - VertexPosition) + 1; long VbSize = (VertexEndPos - VertexPosition) + 1;
bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VertexPosition, VbSize); bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VboKey, VbSize);
if (!VboCached || Vmm.IsRegionModified(VertexPosition, VbSize, NvGpuBufferType.Vertex)) if (!VboCached || Vmm.IsRegionModified(VboKey, VbSize, NvGpuBufferType.Vertex))
{ {
byte[] Data = Vmm.ReadBytes(VertexPosition, VbSize); byte[] Data = Vmm.ReadBytes(VertexPosition, VbSize);
Gpu.Renderer.Rasterizer.CreateVbo(VertexPosition, Data); Gpu.Renderer.Rasterizer.CreateVbo(VboKey, Data);
} }
Gpu.Renderer.Rasterizer.SetVertexArray(Index, Stride, VertexPosition, Attribs[Index].ToArray()); Gpu.Renderer.Rasterizer.SetVertexArray(Index, Stride, VboKey, Attribs[Index].ToArray());
} }
GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff); GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
@ -489,7 +491,7 @@ namespace Ryujinx.HLE.Gpu.Engines
{ {
int VertexBase = ReadRegister(NvGpuEngine3dReg.VertexArrayElemBase); int VertexBase = ReadRegister(NvGpuEngine3dReg.VertexArrayElemBase);
Gpu.Renderer.Rasterizer.DrawElements(IndexPosition, IndexFirst, VertexBase, PrimType); Gpu.Renderer.Rasterizer.DrawElements(IboKey, IndexFirst, VertexBase, PrimType);
} }
else else
{ {

View file

@ -4,6 +4,7 @@ namespace Ryujinx.HLE.Gpu.Memory
{ {
Index, Index,
Vertex, Vertex,
Texture Texture,
Count
} }
} }

View file

@ -274,11 +274,9 @@ namespace Ryujinx.HLE.Gpu.Memory
PageTable[L0][L1] = TgtAddr; PageTable[L0][L1] = TgtAddr;
} }
public bool IsRegionModified(long Position, long Size, NvGpuBufferType BufferType) public bool IsRegionModified(long PA, long Size, NvGpuBufferType BufferType)
{ {
long PA = GetPhysicalAddress(Position); return Cache.IsRegionModified(Memory, BufferType, PA, Size);
return Cache.IsRegionModified(Memory, BufferType, Position, PA, Size);
} }
public byte ReadByte(long Position) public byte ReadByte(long Position)

View file

@ -11,43 +11,53 @@ namespace Ryujinx.HLE.Gpu.Memory
private class CachedPage private class CachedPage
{ {
private List<(long Start, long End)> Regions; 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;
public LinkedListNode<long> Node { get; set; } public LinkedListNode<long> Node { get; set; }
public int Count => Regions.Count;
public int Timestamp { get; private set; } public int Timestamp { get; private set; }
public long PABase { get; private set; } public CachedPage()
public NvGpuBufferType BufferType { get; private set; }
public CachedPage(long PABase, NvGpuBufferType BufferType)
{ {
this.PABase = PABase; Regions = new List<Range>[(int)NvGpuBufferType.Count];
this.BufferType = BufferType;
Regions = new List<(long, long)>(); for (int Index = 0; Index < Regions.Length; Index++)
{
Regions[Index] = new List<Range>();
}
} }
public bool AddRange(long Start, long End) public bool AddRange(long Start, long End, NvGpuBufferType BufferType)
{ {
for (int Index = 0; Index < Regions.Count; Index++) List<Range> BtRegions = Regions[(int)BufferType];
{
(long RgStart, long RgEnd) = Regions[Index];
if (Start >= RgStart && End <= RgEnd) for (int Index = 0; Index < BtRegions.Count; Index++)
{
Range Rg = BtRegions[Index];
if (Start >= Rg.Start && End <= Rg.End)
{ {
return false; return false;
} }
if (Start <= RgEnd && RgStart <= End) if (Start <= Rg.End && Rg.Start <= End)
{ {
long MinStart = Math.Min(RgStart, Start); long MinStart = Math.Min(Rg.Start, Start);
long MaxEnd = Math.Max(RgEnd, End); long MaxEnd = Math.Max(Rg.End, End);
Regions[Index] = (MinStart, MaxEnd); BtRegions[Index] = new Range(MinStart, MaxEnd);
Timestamp = Environment.TickCount; Timestamp = Environment.TickCount;
@ -55,12 +65,24 @@ namespace Ryujinx.HLE.Gpu.Memory
} }
} }
Regions.Add((Start, End)); BtRegions.Add(new Range(Start, End));
Timestamp = Environment.TickCount; Timestamp = Environment.TickCount;
return true; 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 Dictionary<long, CachedPage> Cache;
@ -76,12 +98,7 @@ namespace Ryujinx.HLE.Gpu.Memory
SortedCache = new LinkedList<long>(); SortedCache = new LinkedList<long>();
} }
public bool IsRegionModified( public bool IsRegionModified(AMemory Memory, NvGpuBufferType BufferType, long PA, long Size)
AMemory Memory,
NvGpuBufferType BufferType,
long VA,
long PA,
long Size)
{ {
bool[] Modified = Memory.IsRegionModified(PA, Size); bool[] Modified = Memory.IsRegionModified(PA, Size);
@ -96,19 +113,16 @@ namespace Ryujinx.HLE.Gpu.Memory
long Mask = PageSize - 1; long Mask = PageSize - 1;
long VAEnd = VA + Size;
long PAEnd = PA + Size; long PAEnd = PA + Size;
bool RegMod = false; bool RegMod = false;
int Index = 0; int Index = 0;
while (VA < VAEnd) while (PA < PAEnd)
{ {
long Key = VA & ~Mask; long Key = PA & ~Mask;
long PABase = PA & ~Mask;
long VAPgEnd = Math.Min((VA + PageSize) & ~Mask, VAEnd);
long PAPgEnd = Math.Min((PA + PageSize) & ~Mask, PAEnd); long PAPgEnd = Math.Min((PA + PageSize) & ~Mask, PAEnd);
bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp); bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp);
@ -117,38 +131,32 @@ namespace Ryujinx.HLE.Gpu.Memory
if (!IsCached) if (!IsCached)
{ {
Cp = new CachedPage(PABase, BufferType); Cp = new CachedPage();
Cache.Add(Key, Cp); Cache.Add(Key, Cp);
} }
else else
{ {
CpCount -= Cp.Count; CpCount -= Cp.GetTotalCount();
SortedCache.Remove(Cp.Node); SortedCache.Remove(Cp.Node);
if (Cp.PABase != PABase || Cp.BufferType != BufferType)
{
PgReset = true;
}
} }
PgReset |= Modified[Index++] && IsCached; PgReset |= Modified[Index++] && IsCached;
if (PgReset) if (PgReset)
{ {
Cp = new CachedPage(PABase, BufferType); Cp = new CachedPage();
Cache[Key] = Cp; Cache[Key] = Cp;
} }
Cp.Node = SortedCache.AddLast(Key); Cp.Node = SortedCache.AddLast(Key);
RegMod |= Cp.AddRange(VA, VAPgEnd); RegMod |= Cp.AddRange(PA, PAPgEnd, BufferType);
CpCount += Cp.Count; CpCount += Cp.GetTotalCount();
VA = VAPgEnd;
PA = PAPgEnd; PA = PAPgEnd;
} }
@ -177,7 +185,7 @@ namespace Ryujinx.HLE.Gpu.Memory
Cache.Remove(Key); Cache.Remove(Key);
CpCount -= Cp.Count; CpCount -= Cp.GetTotalCount();
TimeDelta = RingDelta(Cp.Timestamp, Timestamp); TimeDelta = RingDelta(Cp.Timestamp, Timestamp);
} }