Small OpenGL Renderer refactoring (#177)

* Call OpenGL functions directly, remove the pfifo thread, some refactoring

* Fix PerformanceStatistics calculating the wrong host fps, remove wait event on PFIFO as this wasn't exactly was causing the freezes (may replace with an exception later)

* Organized the Gpu folder a bit more, renamed a few things, address PR feedback

* Make PerformanceStatistics thread safe

* Remove unused constant

* Use unlimited update rate for better pref
This commit is contained in:
gdkchan 2018-06-23 21:39:25 -03:00 committed by GitHub
parent 69697957e6
commit e7559f128f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
58 changed files with 518 additions and 633 deletions

View file

@ -0,0 +1,9 @@
namespace Ryujinx.HLE.Gpu.Memory
{
enum NvGpuBufferType
{
Index,
Vertex,
Texture
}
}

View file

@ -0,0 +1,23 @@
using System;
using System.Collections.ObjectModel;
namespace Ryujinx.HLE.Gpu.Memory
{
struct NvGpuPBEntry
{
public int Method { get; private set; }
public int SubChannel { get; private set; }
private int[] m_Arguments;
public ReadOnlyCollection<int> Arguments => Array.AsReadOnly(m_Arguments);
public NvGpuPBEntry(int Method, int SubChannel, params int[] Arguments)
{
this.Method = Method;
this.SubChannel = SubChannel;
this.m_Arguments = Arguments;
}
}
}

View file

@ -0,0 +1,101 @@
using System.Collections.Generic;
using System.IO;
namespace Ryujinx.HLE.Gpu.Memory
{
static class NvGpuPushBuffer
{
private enum SubmissionMode
{
Incrementing = 1,
NonIncrementing = 3,
Immediate = 4,
IncrementOnce = 5
}
public static NvGpuPBEntry[] Decode(byte[] Data)
{
using (MemoryStream MS = new MemoryStream(Data))
{
BinaryReader Reader = new BinaryReader(MS);
List<NvGpuPBEntry> PushBuffer = new List<NvGpuPBEntry>();
bool CanRead() => MS.Position + 4 <= MS.Length;
while (CanRead())
{
int Packed = Reader.ReadInt32();
int Meth = (Packed >> 0) & 0x1fff;
int SubC = (Packed >> 13) & 7;
int Args = (Packed >> 16) & 0x1fff;
int Mode = (Packed >> 29) & 7;
switch ((SubmissionMode)Mode)
{
case SubmissionMode.Incrementing:
{
for (int Index = 0; Index < Args && CanRead(); Index++, Meth++)
{
PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32()));
}
break;
}
case SubmissionMode.NonIncrementing:
{
int[] Arguments = new int[Args];
for (int Index = 0; Index < Arguments.Length; Index++)
{
if (!CanRead())
{
break;
}
Arguments[Index] = Reader.ReadInt32();
}
PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Arguments));
break;
}
case SubmissionMode.Immediate:
{
PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Args));
break;
}
case SubmissionMode.IncrementOnce:
{
if (CanRead())
{
PushBuffer.Add(new NvGpuPBEntry(Meth, SubC, Reader.ReadInt32()));
}
if (CanRead() && Args > 1)
{
int[] Arguments = new int[Args - 1];
for (int Index = 0; Index < Arguments.Length && CanRead(); Index++)
{
Arguments[Index] = Reader.ReadInt32();
}
PushBuffer.Add(new NvGpuPBEntry(Meth + 1, SubC, Arguments));
}
break;
}
}
}
return PushBuffer.ToArray();
}
}
}
}

View file

@ -0,0 +1,410 @@
using ChocolArm64.Memory;
using Ryujinx.Graphics.Gal;
using System.Collections.Concurrent;
namespace Ryujinx.HLE.Gpu.Memory
{
class NvGpuVmm : IAMemory, IGalMemory
{
public const long AddrSize = 1L << 40;
private const int PTLvl0Bits = 14;
private const int PTLvl1Bits = 14;
private const int PTPageBits = 12;
private const int PTLvl0Size = 1 << PTLvl0Bits;
private const int PTLvl1Size = 1 << PTLvl1Bits;
public const int PageSize = 1 << PTPageBits;
private const int PTLvl0Mask = PTLvl0Size - 1;
private const int PTLvl1Mask = PTLvl1Size - 1;
public const int PageMask = PageSize - 1;
private const int PTLvl0Bit = PTPageBits + PTLvl1Bits;
private const int PTLvl1Bit = PTPageBits;
public AMemory Memory { get; private set; }
private struct MappedMemory
{
public long Size;
public MappedMemory(long Size)
{
this.Size = Size;
}
}
private ConcurrentDictionary<long, MappedMemory> Maps;
private NvGpuVmmCache Cache;
private const long PteUnmapped = -1;
private const long PteReserved = -2;
private long[][] PageTable;
public NvGpuVmm(AMemory Memory)
{
this.Memory = Memory;
Maps = new ConcurrentDictionary<long, MappedMemory>();
Cache = new NvGpuVmmCache();
PageTable = new long[PTLvl0Size][];
}
public long Map(long PA, long VA, long Size)
{
lock (PageTable)
{
for (long Offset = 0; Offset < Size; Offset += PageSize)
{
if (GetPte(VA + Offset) != PteReserved)
{
return Map(PA, Size);
}
}
for (long Offset = 0; Offset < Size; Offset += PageSize)
{
SetPte(VA + Offset, PA + Offset);
}
}
return VA;
}
public long Map(long PA, long Size)
{
lock (PageTable)
{
long VA = GetFreePosition(Size);
if (VA != -1)
{
MappedMemory Map = new MappedMemory(Size);
Maps.AddOrUpdate(VA, Map, (Key, Old) => Map);
for (long Offset = 0; Offset < Size; Offset += PageSize)
{
SetPte(VA + Offset, PA + Offset);
}
}
return VA;
}
}
public bool Unmap(long VA)
{
if (Maps.TryRemove(VA, out MappedMemory Map))
{
Free(VA, Map.Size);
return true;
}
return false;
}
public long Reserve(long VA, long Size, long Align)
{
lock (PageTable)
{
for (long Offset = 0; Offset < Size; Offset += PageSize)
{
if (IsPageInUse(VA + Offset))
{
return Reserve(Size, Align);
}
}
for (long Offset = 0; Offset < Size; Offset += PageSize)
{
SetPte(VA + Offset, PteReserved);
}
}
return VA;
}
public long Reserve(long Size, long Align)
{
lock (PageTable)
{
long Position = GetFreePosition(Size, Align);
if (Position != -1)
{
for (long Offset = 0; Offset < Size; Offset += PageSize)
{
SetPte(Position + Offset, PteReserved);
}
}
return Position;
}
}
public void Free(long VA, long Size)
{
lock (PageTable)
{
for (long Offset = 0; Offset < Size; Offset += PageSize)
{
SetPte(VA + Offset, PteUnmapped);
}
}
}
private long GetFreePosition(long Size, long Align = 1)
{
long Position = 0;
long FreeSize = 0;
if (Align < 1)
{
Align = 1;
}
Align = (Align + PageMask) & ~PageMask;
while (Position + FreeSize < AddrSize)
{
if (!IsPageInUse(Position + FreeSize))
{
FreeSize += PageSize;
if (FreeSize >= Size)
{
return Position;
}
}
else
{
Position += FreeSize + PageSize;
FreeSize = 0;
long Remainder = Position % Align;
if (Remainder != 0)
{
Position = (Position - Remainder) + Align;
}
}
}
return -1;
}
public long GetPhysicalAddress(long VA)
{
long BasePos = GetPte(VA);
if (BasePos < 0)
{
return -1;
}
return BasePos + (VA & PageMask);
}
public bool IsRegionFree(long VA, long Size)
{
for (long Offset = 0; Offset < Size; Offset += PageSize)
{
if (IsPageInUse(VA + Offset))
{
return false;
}
}
return true;
}
private bool IsPageInUse(long VA)
{
if (VA >> PTLvl0Bits + PTLvl1Bits + PTPageBits != 0)
{
return false;
}
long L0 = (VA >> PTLvl0Bit) & PTLvl0Mask;
long L1 = (VA >> PTLvl1Bit) & PTLvl1Mask;
if (PageTable[L0] == null)
{
return false;
}
return PageTable[L0][L1] != PteUnmapped;
}
private long GetPte(long Position)
{
long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
if (PageTable[L0] == null)
{
return -1;
}
return PageTable[L0][L1];
}
private void SetPte(long Position, long TgtAddr)
{
long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
if (PageTable[L0] == null)
{
PageTable[L0] = new long[PTLvl1Size];
for (int Index = 0; Index < PTLvl1Size; Index++)
{
PageTable[L0][Index] = PteUnmapped;
}
}
PageTable[L0][L1] = TgtAddr;
}
public bool IsRegionModified(long Position, long Size, NvGpuBufferType BufferType)
{
long PA = GetPhysicalAddress(Position);
return Cache.IsRegionModified(Memory, BufferType, Position, PA, Size);
}
public byte ReadByte(long Position)
{
Position = GetPhysicalAddress(Position);
return Memory.ReadByte(Position);
}
public ushort ReadUInt16(long Position)
{
Position = GetPhysicalAddress(Position);
return Memory.ReadUInt16(Position);
}
public uint ReadUInt32(long Position)
{
Position = GetPhysicalAddress(Position);
return Memory.ReadUInt32(Position);
}
public ulong ReadUInt64(long Position)
{
Position = GetPhysicalAddress(Position);
return Memory.ReadUInt64(Position);
}
public sbyte ReadSByte(long Position)
{
Position = GetPhysicalAddress(Position);
return Memory.ReadSByte(Position);
}
public short ReadInt16(long Position)
{
Position = GetPhysicalAddress(Position);
return Memory.ReadInt16(Position);
}
public int ReadInt32(long Position)
{
Position = GetPhysicalAddress(Position);
return Memory.ReadInt32(Position);
}
public long ReadInt64(long Position)
{
Position = GetPhysicalAddress(Position);
return Memory.ReadInt64(Position);
}
public byte[] ReadBytes(long Position, long Size)
{
Position = GetPhysicalAddress(Position);
return Memory.ReadBytes(Position, Size);
}
public void WriteByte(long Position, byte Value)
{
Position = GetPhysicalAddress(Position);
Memory.WriteByte(Position, Value);
}
public void WriteUInt16(long Position, ushort Value)
{
Position = GetPhysicalAddress(Position);
Memory.WriteUInt16(Position, Value);
}
public void WriteUInt32(long Position, uint Value)
{
Position = GetPhysicalAddress(Position);
Memory.WriteUInt32(Position, Value);
}
public void WriteUInt64(long Position, ulong Value)
{
Position = GetPhysicalAddress(Position);
Memory.WriteUInt64(Position, Value);
}
public void WriteSByte(long Position, sbyte Value)
{
Position = GetPhysicalAddress(Position);
Memory.WriteSByte(Position, Value);
}
public void WriteInt16(long Position, short Value)
{
Position = GetPhysicalAddress(Position);
Memory.WriteInt16(Position, Value);
}
public void WriteInt32(long Position, int Value)
{
Position = GetPhysicalAddress(Position);
Memory.WriteInt32(Position, Value);
}
public void WriteInt64(long Position, long Value)
{
Position = GetPhysicalAddress(Position);
Memory.WriteInt64(Position, Value);
}
public void WriteBytes(long Position, byte[] Data)
{
Position = GetPhysicalAddress(Position);
Memory.WriteBytes(Position, Data);
}
}
}

View file

@ -0,0 +1,209 @@
using ChocolArm64.Memory;
using System;
using System.Collections.Generic;
namespace Ryujinx.HLE.Gpu.Memory
{
class NvGpuVmmCache
{
private const int MaxCpCount = 10000;
private const int MaxCpTimeDelta = 60000;
private class CachedPage
{
private List<(long Start, long End)> Regions;
public LinkedListNode<long> Node { get; set; }
public int Count => Regions.Count;
public int Timestamp { get; private set; }
public long PABase { get; private set; }
public NvGpuBufferType BufferType { get; private set; }
public CachedPage(long PABase, NvGpuBufferType BufferType)
{
this.PABase = PABase;
this.BufferType = BufferType;
Regions = new List<(long, long)>();
}
public bool AddRange(long Start, long End)
{
for (int Index = 0; Index < Regions.Count; Index++)
{
(long RgStart, long RgEnd) = Regions[Index];
if (Start >= RgStart && End <= RgEnd)
{
return false;
}
if (Start <= RgEnd && RgStart <= End)
{
long MinStart = Math.Min(RgStart, Start);
long MaxEnd = Math.Max(RgEnd, End);
Regions[Index] = (MinStart, MaxEnd);
Timestamp = Environment.TickCount;
return true;
}
}
Regions.Add((Start, End));
Timestamp = Environment.TickCount;
return true;
}
}
private Dictionary<long, CachedPage> Cache;
private LinkedList<long> SortedCache;
private int CpCount;
public NvGpuVmmCache()
{
Cache = new Dictionary<long, CachedPage>();
SortedCache = new LinkedList<long>();
}
public bool IsRegionModified(
AMemory Memory,
NvGpuBufferType BufferType,
long VA,
long PA,
long Size)
{
ClearCachedPagesIfNeeded();
long PageSize = Memory.GetHostPageSize();
long Mask = PageSize - 1;
long VAEnd = VA + Size;
long PAEnd = PA + Size;
bool RegMod = false;
while (VA < VAEnd)
{
long Key = VA & ~Mask;
long PABase = PA & ~Mask;
long VAPgEnd = Math.Min((VA + PageSize) & ~Mask, VAEnd);
long PAPgEnd = Math.Min((PA + PageSize) & ~Mask, PAEnd);
bool IsCached = Cache.TryGetValue(Key, out CachedPage Cp);
bool PgReset = false;
if (!IsCached)
{
Cp = new CachedPage(PABase, BufferType);
Cache.Add(Key, Cp);
}
else
{
CpCount -= Cp.Count;
SortedCache.Remove(Cp.Node);
if (Cp.PABase != PABase ||
Cp.BufferType != BufferType)
{
PgReset = true;
}
}
PgReset |= Memory.IsRegionModified(PA, PAPgEnd - PA) && IsCached;
if (PgReset)
{
Cp = new CachedPage(PABase, BufferType);
Cache[Key] = Cp;
}
Cp.Node = SortedCache.AddLast(Key);
RegMod |= Cp.AddRange(VA, VAPgEnd);
CpCount += Cp.Count;
VA = VAPgEnd;
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);
CpCount -= Cp.Count;
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;
}
}
}
}